Add tests and correctly handle using '/' in windows paths.
authorJonathan Chambers <joncham@gmail.com>
Wed, 2 May 2012 17:25:20 +0000 (13:25 -0400)
committerJonathan Chambers <joncham@gmail.com>
Wed, 2 May 2012 17:25:20 +0000 (13:25 -0400)
eglib/src/gpath.c
eglib/test/path.c

index 4ae2f8f5e9e8863f5e7fe704e3275dd8dd084be8..14343494d80eb5660d5aadd08fcea92814c5b7ac 100644 (file)
@@ -88,6 +88,24 @@ g_build_path (const gchar *separator, const gchar *first_element, ...)
        return g_string_free (path, FALSE);
 }
 
+static gchar*
+strrchr_seperator (const gchar* filename)
+{
+#ifdef G_OS_WIN32
+       char *p2;
+#endif
+       char *p;
+
+       p = strrchr (filename, G_DIR_SEPARATOR);
+#ifdef G_OS_WIN32
+       p2 = strrchr (filename, '/');
+       if (p2 > p)
+               p = p2;
+#endif
+
+       return p;
+}
+
 gchar *
 g_path_get_dirname (const gchar *filename)
 {
@@ -95,7 +113,7 @@ g_path_get_dirname (const gchar *filename)
        size_t count;
        g_return_val_if_fail (filename != NULL, NULL);
 
-       p = strrchr (filename, G_DIR_SEPARATOR);
+       p = strrchr_seperator (filename);
        if (p == NULL)
                return g_strdup (".");
        if (p == filename)
@@ -119,7 +137,7 @@ g_path_get_basename (const char *filename)
                return g_strdup (".");
 
        /* No separator -> filename */
-       r = strrchr (filename, G_DIR_SEPARATOR);
+       r = strrchr_seperator (filename);
        if (r == NULL)
                return g_strdup (filename);
 
@@ -127,7 +145,7 @@ g_path_get_basename (const char *filename)
        if (r [1] == 0){
                char *copy = g_strdup (filename);
                copy [r-filename] = 0;
-               r = strrchr (copy, G_DIR_SEPARATOR);
+               r = strrchr_seperator (copy);
 
                if (r == NULL){
                        g_free (copy);                  
index cd27b421f0b756fac028134e1b47c7b8f54fd388..e3832a04be493815e423203c66616f86418e33db 100644 (file)
@@ -158,6 +158,11 @@ test_dirname ()
                return FAILED ("Expected c:\\home, got %s", s);
        g_free (s);
 
+       s = g_path_get_dirname ("c:/home/miguel");
+       if (strcmp (s, "c:/home") != 0)
+               return FAILED ("Expected c:/home, got %s", s);
+       g_free (s);
+
        s = g_path_get_dirname ("c:\\home\\dingus\\");
        if (strcmp (s, "c:\\home\\dingus") != 0)
                return FAILED ("Expected c:\\home\\dingus, got %s", s);
@@ -210,10 +215,20 @@ test_basename ()
                return FAILED ("1 Expected dingus, got %s", s);
        g_free (s);
 
+       s = g_path_get_basename ("c:/home/dingus/");
+       if (strcmp (s, "dingus") != 0)
+               return FAILED ("1 Expected dingus, got %s", s);
+       g_free (s);
+
        s = g_path_get_basename ("c:\\home\\dingus");
        if (strcmp (s, "dingus") != 0)
                return FAILED ("2 Expected dingus, got %s", s);
        g_free (s);
+
+       s = g_path_get_basename ("c:/home/dingus");
+       if (strcmp (s, "dingus") != 0)
+               return FAILED ("2 Expected dingus, got %s", s);
+       g_free (s);
 #else
        s = g_path_get_basename ("");
        if (strcmp (s, ".") != 0)