misc: Add cr_cut_dirs()
authorTomas Mlcoch <tmlcoch@redhat.com>
Fri, 22 May 2015 13:26:21 +0000 (15:26 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Fri, 22 May 2015 13:33:54 +0000 (15:33 +0200)
src/misc.c
src/misc.h
tests/test_misc.c

index 14ea1f152b590fceb7ce1ee1b62b5236e21f1f6b..90e9d9c80d2c85824e7b0c0433b9e8661cecefdc 100644 (file)
@@ -1438,3 +1438,45 @@ cr_identical_files(const gchar *fn1,
     return TRUE;
 }
 
+gchar *
+cr_cut_dirs(gchar *path, gint cut_dirs)
+{
+    if (!path)
+        return NULL;
+
+    if (cut_dirs < 1)
+        return path;
+
+    gchar *last_component = NULL;
+    for (gchar *p = path; *p; p++) {
+        if (*p == '/')
+            last_component = p;
+    }
+
+    if (last_component == NULL)
+        return path;
+
+    gchar *cut = path;
+    gint n = 0;
+    gint state = 0;
+
+    for (gchar *p = path; p <= last_component; p++) {
+        if (state == 0) {
+            if (*p == '/') {
+                cut = p;
+            } else {
+                state = 1;
+                if (n == cut_dirs)
+                    break;
+            }
+        } else if (state == 1) {
+            if (*p == '/') {
+                cut = p;
+                state = 0;
+                n++;
+            }
+        }
+    }
+
+    return cut+1;
+}
index 4f9d2086d7872feb674d7a9c0370b4a6fa654770..7e45017f0f0e4cc439e0e101466690ff8c5771f9 100644 (file)
@@ -523,6 +523,12 @@ cr_identical_files(const gchar *fn1,
                    gboolean *identical,
                    GError **err);
 
+/** Cut first N components of path.
+ * Note: Basename is never cut out.
+ */
+gchar *
+cr_cut_dirs(gchar *path, gint cut_dirs);
+
 /** @} */
 
 #ifdef __cplusplus
index aacff0d7c08d8743aa069b416967e539fac5c3d1..f83a28de2c82d593010556207e2de16cb3da6a9d 100644 (file)
@@ -1179,6 +1179,56 @@ test_cr_cmp_evr(void)
     g_assert_cmpint(res, ==, 1);
 }
 
+static void
+test_cr_cut_dirs(void)
+{
+    char *res;
+
+    res = cr_cut_dirs(NULL, 1);
+    g_assert_cmpstr(res, ==, NULL);
+
+    res = cr_cut_dirs("", 1);
+    g_assert_cmpstr(res, ==, "");
+
+    res = cr_cut_dirs("foo.rpm", 1);
+    g_assert_cmpstr(res, ==, "foo.rpm");
+
+    res = cr_cut_dirs("/foo.rpm", 1);
+    g_assert_cmpstr(res, ==, "foo.rpm");
+
+    res = cr_cut_dirs("//foo.rpm", 1);
+    g_assert_cmpstr(res, ==, "foo.rpm");
+
+    res = cr_cut_dirs("///foo.rpm", 1);
+    g_assert_cmpstr(res, ==, "foo.rpm");
+
+    res = cr_cut_dirs("bar/foo.rpm", 1);
+    g_assert_cmpstr(res, ==, "foo.rpm");
+
+    res = cr_cut_dirs("/bar/foo.rpm", 1);
+    g_assert_cmpstr(res, ==, "foo.rpm");
+
+    res = cr_cut_dirs("bar//foo.rpm", 1);
+    g_assert_cmpstr(res, ==, "foo.rpm");
+
+    res = cr_cut_dirs("//bar//foo.rpm", 1);
+    g_assert_cmpstr(res, ==, "foo.rpm");
+
+    res = cr_cut_dirs("///a///b/foo.rpm", 1);
+    g_assert_cmpstr(res, ==, "b/foo.rpm");
+
+    res = cr_cut_dirs("a/b/c/foo.rpm", 1);
+    g_assert_cmpstr(res, ==, "b/c/foo.rpm");
+
+    res = cr_cut_dirs("a/b/c/foo.rpm", 2);
+    g_assert_cmpstr(res, ==, "c/foo.rpm");
+
+    res = cr_cut_dirs("a/b/c/foo.rpm", 3);
+    g_assert_cmpstr(res, ==, "foo.rpm");
+
+    res = cr_cut_dirs("a///b///c///foo.rpm", 3);
+    g_assert_cmpstr(res, ==, "foo.rpm");
+}
 
 int
 main(int argc, char *argv[])
@@ -1250,6 +1300,8 @@ main(int argc, char *argv[])
             test_cr_str_to_nevra);
     g_test_add_func("/misc/test_cr_cmp_evr",
             test_cr_cmp_evr);
+    g_test_add_func("/misc/test_cr_cut_dirs",
+            test_cr_cut_dirs);
 
     return g_test_run();
 }