summaryrefslogtreecommitdiff
path: root/common/strutils.c
diff options
context:
space:
mode:
authorTomas Bzatek <tbzatek@users.sourceforge.net>2009-11-28 13:11:51 +0100
committerTomas Bzatek <tbzatek@users.sourceforge.net>2009-11-28 13:11:51 +0100
commit9382f127ccebdd59917c97c61d008ed0e127cd75 (patch)
tree0a48c8296199b343c76ef532eef014f908bc2e4d /common/strutils.c
parent70eeaa4ec712895539ca6ecd60a42b93ec9b0904 (diff)
downloadtuxcmd-modules-9382f127ccebdd59917c97c61d008ed0e127cd75.tar.xz
Engine and VFS API cleanupv0.6.72
Also enable symlink resolving by default.
Diffstat (limited to 'common/strutils.c')
-rw-r--r--common/strutils.c157
1 files changed, 77 insertions, 80 deletions
diff --git a/common/strutils.c b/common/strutils.c
index 5383b8a..3b0a1db 100644
--- a/common/strutils.c
+++ b/common/strutils.c
@@ -1,4 +1,4 @@
-/* Tux Commandern VFS: String utilities
+/* Tux Commander VFS: String utilities
* Copyright (C) 2007 Tomas Bzatek <tbzatek@users.sourceforge.net>
* Check for updates on tuxcmd.sourceforge.net
*
@@ -17,89 +17,102 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#include <stdio.h>
#include <string.h>
#include <glib.h>
+
#include "strutils.h"
-char* include_trailing_path_sep(const char *APath)
+
+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;
+ if (APath == NULL)
+ return NULL;
+ if (g_strcmp0 (APath + strlen (APath) - 1, "/") != 0)
+ return g_strdup_printf ("%s/", APath);
+ else
+ return g_strdup (APath);
}
-char* exclude_trailing_path_sep(const char *APath)
+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;
+ if (APath == NULL)
+ return NULL;
+ if (strlen (APath) > 1 && g_strcmp0 (APath + strlen (APath) - 1, "/") == 0)
+ return g_strndup (APath, strlen (APath) - 1);
+ else
+ return g_strdup(APath);
}
-char* include_leading_path_sep(const char *APath)
+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;
+ if (APath == NULL)
+ return NULL;
+ if (! g_path_is_absolute (APath))
+ return g_strdup_printf ("/%s", APath);
+ else
+ return g_strdup (APath);
}
-char* exclude_leading_path_sep(const char* APath)
+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;
+ if (APath == NULL)
+ return NULL;
+ if (g_path_is_absolute (APath))
+ return g_strdup (APath + 1);
+ else
+ return g_strdup (APath);
}
-char* extract_file_name(const char *APath)
+/* -------------------------------------------------------------------------------------- */
+
+char *
+resolve_relative (const char *source, const char *point_to)
{
- if (APath == NULL) return NULL;
-// log("xxx = %s\n", (APath + strlen(APath) - 1));
- const 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);
+ char *rel;
+ char *canon;
+
+ if (source == NULL)
+ return NULL;
+ if (point_to == NULL)
+ return g_strdup (source);
+ if (g_path_is_absolute (point_to))
+ return g_strdup (point_to);
+
+ rel = g_build_filename (source, point_to, NULL);
+ log ("resolve_relative: rel = '%s'\n", rel);
+ canon = canonicalize_filename (rel);
+ log ("resolve_relative: canon = '%s'\n", canon);
+ g_free (rel);
+
+ return canon;
}
-// Extracts file path starting with "/" and ending with "/"
-char* extract_file_path(const char *APath)
+void
+split_path (const char *path, char **first_part, char **last_part)
{
- if (APath == NULL) return NULL;
-// log("xxx = %s\n", (APath + strlen(APath) - 1));
- const 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;
+ const char *pos;
+
+ pos = strstr (path, "/");
+ if (pos) {
+ *first_part = g_strndup (path, pos - path);
+ *last_part = g_strdup (pos + 1);
+ } else {
+ *first_part = g_strdup (path);
+ *last_part = NULL;
+ }
}
+/* -------------------------------------------------------------------------------------- */
-
-
-
-
-
-// canonicalize_filename() is stolen from glib-2.16
-// Copyright (C) 2006-2007 Alexander Larsson <alexl@redhat.com>
+/* canonicalize_filename() has been stolen from glib-2.16 */
+/* Copyright (C) 2006-2007 Alexander Larsson <alexl@redhat.com> */
char *
canonicalize_filename (const char *filename)
{
@@ -179,34 +192,18 @@ canonicalize_filename (const char *filename)
}
-char* resolve_relative(const char *source, const char *point_to)
-{
- if (source == NULL) return NULL;
- if (point_to == NULL) return strdup(source);
- if (g_path_is_absolute(point_to)) return strdup(point_to);
-
- char *rel = g_build_filename(source, point_to, NULL);
- log("resolve_relative: rel = '%s'\n", rel);
- char *canon = canonicalize_filename(rel);
- log("resolve_relative: canon = '%s'\n", canon);
- free(rel);
-
- return canon;
-}
-
-
+/* -------------------------------------------------------------------------------------- */
-// Originally stolen from wine-0.9.19, utf8.c
-// Copyright 2000 Alexandre Julliard
-char*
+/* stolen from wine-0.9.19, utf8.c */
+/* Copyright 2000 Alexandre Julliard */
+char *
wide_to_utf8 (const wchar_t *src)
{
#define CONV_BUFF_MAX 32768
int len;
char *buf, *dst, *ret;
- buf = (char *) malloc (CONV_BUFF_MAX);
- memset (&buf[0], 0, CONV_BUFF_MAX);
+ buf = (char *) g_malloc0 (CONV_BUFF_MAX);
dst = buf;
if (src)
@@ -251,6 +248,6 @@ wide_to_utf8 (const wchar_t *src)
}
ret = g_strdup (buf);
- free (buf);
+ g_free (buf);
return ret;
}