new fuction g_dirname() which returns a newlly allocated string.
authorTim Janik <timj@gtk.org>
Tue, 14 Jul 1998 07:39:07 +0000 (07:39 +0000)
committerTim Janik <timj@src.gnome.org>
Tue, 14 Jul 1998 07:39:07 +0000 (07:39 +0000)
Tue Jul 14 09:05:18 1998  Tim Janik  <timj@gtk.org>

        * glib.h:
        * gutils.c: new fuction g_dirname() which returns a newlly
        allocated string.

14 files changed:
ChangeLog
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-2
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
glib.h
glib/glib.h
glib/gutils.c
gutils.c
testglib.c
tests/testglib.c

index 79ae63b..4f8b43f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Tue Jul 14 09:05:18 1998  Tim Janik  <timj@gtk.org>
+
+       * glib.h:
+       * gutils.c: new fuction g_dirname() which returns a newlly
+       allocated string.
+
 Fri Jul 10 06:33:43 1998  Tim Janik  <timj@gtk.org>
 
        * glib.h: 
index 79ae63b..4f8b43f 100644 (file)
@@ -1,3 +1,9 @@
+Tue Jul 14 09:05:18 1998  Tim Janik  <timj@gtk.org>
+
+       * glib.h:
+       * gutils.c: new fuction g_dirname() which returns a newlly
+       allocated string.
+
 Fri Jul 10 06:33:43 1998  Tim Janik  <timj@gtk.org>
 
        * glib.h: 
index 79ae63b..4f8b43f 100644 (file)
@@ -1,3 +1,9 @@
+Tue Jul 14 09:05:18 1998  Tim Janik  <timj@gtk.org>
+
+       * glib.h:
+       * gutils.c: new fuction g_dirname() which returns a newlly
+       allocated string.
+
 Fri Jul 10 06:33:43 1998  Tim Janik  <timj@gtk.org>
 
        * glib.h: 
index 79ae63b..4f8b43f 100644 (file)
@@ -1,3 +1,9 @@
+Tue Jul 14 09:05:18 1998  Tim Janik  <timj@gtk.org>
+
+       * glib.h:
+       * gutils.c: new fuction g_dirname() which returns a newlly
+       allocated string.
+
 Fri Jul 10 06:33:43 1998  Tim Janik  <timj@gtk.org>
 
        * glib.h: 
index 79ae63b..4f8b43f 100644 (file)
@@ -1,3 +1,9 @@
+Tue Jul 14 09:05:18 1998  Tim Janik  <timj@gtk.org>
+
+       * glib.h:
+       * gutils.c: new fuction g_dirname() which returns a newlly
+       allocated string.
+
 Fri Jul 10 06:33:43 1998  Tim Janik  <timj@gtk.org>
 
        * glib.h: 
index 79ae63b..4f8b43f 100644 (file)
@@ -1,3 +1,9 @@
+Tue Jul 14 09:05:18 1998  Tim Janik  <timj@gtk.org>
+
+       * glib.h:
+       * gutils.c: new fuction g_dirname() which returns a newlly
+       allocated string.
+
 Fri Jul 10 06:33:43 1998  Tim Janik  <timj@gtk.org>
 
        * glib.h: 
index 79ae63b..4f8b43f 100644 (file)
@@ -1,3 +1,9 @@
+Tue Jul 14 09:05:18 1998  Tim Janik  <timj@gtk.org>
+
+       * glib.h:
+       * gutils.c: new fuction g_dirname() which returns a newlly
+       allocated string.
+
 Fri Jul 10 06:33:43 1998  Tim Janik  <timj@gtk.org>
 
        * glib.h: 
index 79ae63b..4f8b43f 100644 (file)
@@ -1,3 +1,9 @@
+Tue Jul 14 09:05:18 1998  Tim Janik  <timj@gtk.org>
+
+       * glib.h:
+       * gutils.c: new fuction g_dirname() which returns a newlly
+       allocated string.
+
 Fri Jul 10 06:33:43 1998  Tim Janik  <timj@gtk.org>
 
        * glib.h: 
diff --git a/glib.h b/glib.h
index 4ff637a..ee81a61 100644 (file)
--- a/glib.h
+++ b/glib.h
@@ -840,6 +840,7 @@ gint        g_snprintf              (gchar       *string,
                                 ...) G_GNUC_PRINTF (3, 4);
 gchar* g_basename              (const gchar *file_name);
 gchar* g_getcwd                (void);
+gchar* g_dirname               (const gchar *file_name);
      
 
 /* We make the assumption that if memmove isn't available, then
index 4ff637a..ee81a61 100644 (file)
@@ -840,6 +840,7 @@ gint        g_snprintf              (gchar       *string,
                                 ...) G_GNUC_PRINTF (3, 4);
 gchar* g_basename              (const gchar *file_name);
 gchar* g_getcwd                (void);
+gchar* g_dirname               (const gchar *file_name);
      
 
 /* We make the assumption that if memmove isn't available, then
index 6aac98f..80e2ddb 100644 (file)
@@ -128,6 +128,28 @@ g_basename (const gchar       *file_name)
 }
 
 gchar*
+g_dirname (const gchar    *file_name)
+{
+  register gchar *base;
+  register guint len;
+  
+  g_return_val_if_fail (file_name != NULL, NULL);
+  
+  base = strrchr (file_name, '/');
+  if (!base)
+    return g_strdup (".");
+  while (base > file_name && *base == '/')
+    base--;
+  len = (guint) 1 + base - file_name;
+
+  base = g_new (gchar, len + 1);
+  g_memmove (base, file_name, len);
+  base[len] = 0;
+
+  return base;
+}
+
+gchar*
 g_getcwd (void)
 {
   static gchar g_getcwd_buf[MAXPATHLEN + 1] = { 0 };
index 6aac98f..80e2ddb 100644 (file)
--- a/gutils.c
+++ b/gutils.c
@@ -128,6 +128,28 @@ g_basename (const gchar       *file_name)
 }
 
 gchar*
+g_dirname (const gchar    *file_name)
+{
+  register gchar *base;
+  register guint len;
+  
+  g_return_val_if_fail (file_name != NULL, NULL);
+  
+  base = strrchr (file_name, '/');
+  if (!base)
+    return g_strdup (".");
+  while (base > file_name && *base == '/')
+    base--;
+  len = (guint) 1 + base - file_name;
+
+  base = g_new (gchar, len + 1);
+  g_memmove (base, file_name, len);
+  base[len] = 0;
+
+  return base;
+}
+
+gchar*
 g_getcwd (void)
 {
   static gchar g_getcwd_buf[MAXPATHLEN + 1] = { 0 };
index 818e49d..d89e763 100644 (file)
@@ -112,11 +112,48 @@ main (int   argc,
   GRelation *relation;
   GTuples *tuples;
   gint data [1024];
+  struct {
+    gchar *filename;
+    gchar *dirname;
+  } dirname_checks[] = {
+    { "/", "/" },
+    { "////", "/" },
+    { ".////", "." },
+    { ".", "." },
+    { "..", "." },
+    { "../", ".." },
+    { "..////", ".." },
+    { "", "." },
+    { "a/b", "a" },
+    { "a/b/", "a/b" },
+    { "c///", "c" },
+  };
+  guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
 
+  
   g_print ("checking size of gint8...%ld (should be 1)\n", (glong)sizeof (gint8));
   g_print ("checking size of gint16...%ld (should be 2)\n", (glong)sizeof (gint16));
   g_print ("checking size of gint32...%ld (should be 4)\n", (glong)sizeof (gint32));
 
+  g_print ("checking g_dirname()...");
+  for (i = 0; i < n_dirname_checks; i++)
+    {
+      gchar *dirname;
+
+      dirname = g_dirname (dirname_checks[i].filename);
+      if (strcmp (dirname, dirname_checks[i].dirname) != 0)
+       {
+         g_print ("failed for \"%s\"==\"%s\" (returned: \"%s\")\n",
+                  dirname_checks[i].filename,
+                  dirname_checks[i].dirname,
+                  dirname);
+         n_dirname_checks = 0;
+       }
+      g_free (dirname);
+    }
+  if (n_dirname_checks)
+    g_print ("ok\n");
+
   g_print ("checking doubly linked lists...");
 
   list = NULL;
index 818e49d..d89e763 100644 (file)
@@ -112,11 +112,48 @@ main (int   argc,
   GRelation *relation;
   GTuples *tuples;
   gint data [1024];
+  struct {
+    gchar *filename;
+    gchar *dirname;
+  } dirname_checks[] = {
+    { "/", "/" },
+    { "////", "/" },
+    { ".////", "." },
+    { ".", "." },
+    { "..", "." },
+    { "../", ".." },
+    { "..////", ".." },
+    { "", "." },
+    { "a/b", "a" },
+    { "a/b/", "a/b" },
+    { "c///", "c" },
+  };
+  guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
 
+  
   g_print ("checking size of gint8...%ld (should be 1)\n", (glong)sizeof (gint8));
   g_print ("checking size of gint16...%ld (should be 2)\n", (glong)sizeof (gint16));
   g_print ("checking size of gint32...%ld (should be 4)\n", (glong)sizeof (gint32));
 
+  g_print ("checking g_dirname()...");
+  for (i = 0; i < n_dirname_checks; i++)
+    {
+      gchar *dirname;
+
+      dirname = g_dirname (dirname_checks[i].filename);
+      if (strcmp (dirname, dirname_checks[i].dirname) != 0)
+       {
+         g_print ("failed for \"%s\"==\"%s\" (returned: \"%s\")\n",
+                  dirname_checks[i].filename,
+                  dirname_checks[i].dirname,
+                  dirname);
+         n_dirname_checks = 0;
+       }
+      g_free (dirname);
+    }
+  if (n_dirname_checks)
+    g_print ("ok\n");
+
   g_print ("checking doubly linked lists...");
 
   list = NULL;