diff options
| author | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2008-06-08 11:04:43 +0200 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2008-06-08 11:04:43 +0200 |
| commit | 16f738ecee689c6feb2acb7e4ef4d9bb4144ae7d (patch) | |
| tree | 3d22f54f7298f81b18ed66d05a62fa8bfab359ab /common/strutils.c | |
| download | tuxcmd-modules-release-0.6.36-dev.tar.xz | |
Initial commitv0.6.36release-0.6.36-dev
Diffstat (limited to 'common/strutils.c')
| -rw-r--r-- | common/strutils.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/common/strutils.c b/common/strutils.c new file mode 100644 index 0000000..3f390d9 --- /dev/null +++ b/common/strutils.c @@ -0,0 +1,91 @@ +/* Tux Commandern VFS: String utilities + * Copyright (C) 2007 Tomas Bzatek <tbzatek@users.sourceforge.net> + * Check for updates on tuxcmd.sourceforge.net + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <stdio.h> +#include <string.h> +#include "strutils.h" + + +char* include_trailing_path_sep(const char *APath) +{ + if (APath == NULL) return NULL; + char *ANewPath; +// log("xxx = %s\n", (APath + strlen(APath) - 1)); + if (strcmp(APath + strlen(APath) - 1, "/") != 0) { + ANewPath = (char*)malloc(strlen(APath) + 2); + snprintf(ANewPath, strlen(APath) + 2, "%s/", APath); + } else ANewPath = strdup(APath); + return ANewPath; +} + +char* exclude_trailing_path_sep(const char *APath) +{ + if (APath == NULL) return NULL; + char *ANewPath; +// log("orig = %s, len = %d, xxx = %s\n", APath, (int)strlen(APath), (APath + strlen(APath) - 1)); + if ((strcmp(APath + strlen(APath) - 1, "/") == 0) && (strlen(APath) > 1)) { + ANewPath = (char*)malloc(strlen(APath)); + snprintf(ANewPath, strlen(APath), "%s", APath); // The number of bytes copied includes the trailing \0 + } else ANewPath = strdup(APath); + return ANewPath; +} + +char* include_leading_path_sep(const char *APath) +{ + if (APath == NULL) return NULL; + char *ANewPath; +// log("xxx = %s, %lu, strlen(APath) = %d, index() = %ld\n", APath, (unsigned long int)APath, (int)strlen(APath), (unsigned long int)index(APath, 0x2f)); + if (index(APath, 0x2f) != APath) { + ANewPath = (char*)malloc(strlen(APath) + 2); + snprintf(ANewPath, strlen(APath) + 2, "/%s", APath); // The number of bytes copied includes the trailing \0 + } else ANewPath = strdup(APath); + return ANewPath; +} + +char* exclude_leading_path_sep(const char* APath) +{ + if (APath == NULL) return NULL; + char *s = strdup(APath); + char *ss; + if (IS_DIR_SEP(*s)) ss = strdup(s + 1); else ss = strdup(s); + free(s); + return ss; +} + + +char* extract_file_name(const char *APath) +{ + if (APath == NULL) return NULL; +// log("xxx = %s\n", (APath + strlen(APath) - 1)); + char *file_part = rindex(APath, 0x2f); // returns NULL if not found or if the file is in the root ( / ) + if (file_part == NULL) return NULL; + return strdup(file_part + 1); +} + +// Extracts file path starting with "/" and ending with "/" +char* extract_file_path(const char *APath) +{ + if (APath == NULL) return NULL; +// log("xxx = %s\n", (APath + strlen(APath) - 1)); + char *file_part = rindex(APath, 0x2f); // returns NULL if not found or if the file is in the root ( / ) + if (file_part == NULL) return NULL; + char *ANewPath = (char*)malloc(file_part - APath + 2); + snprintf(ANewPath, file_part - APath + 2, "%s", APath); + return ANewPath; +} |
