compression_wrapper: Add cr_compression_type() function.
authorTomas Mlcoch <tmlcoch@redhat.com>
Thu, 12 Sep 2013 14:01:24 +0000 (16:01 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Thu, 12 Sep 2013 14:01:24 +0000 (16:01 +0200)
src/compression_wrapper.c
src/compression_wrapper.h
tests/test_compression_wrapper.c

index cad0c90877b03d1a550de826bf020355e997e769..519d66452bf4b9b2366e1681f1e419f09227a8a6 100644 (file)
@@ -228,6 +228,25 @@ cr_detect_compression(const char *filename, GError **err)
     return type;
 }
 
+cr_CompressionType
+cr_compression_type(const char *name)
+{
+    if (!name)
+        return CR_CW_UNKNOWN_COMPRESSION;
+
+    gchar *name_lower = g_strdup(name);
+    for (gchar *c = name_lower; *c; c++)
+        *c = tolower(*c);
+
+    if (!g_strcmp0(name_lower, "gz") || !g_strcmp0(name_lower, "gzip"))
+        return CR_CW_GZ_COMPRESSION;
+    if (!g_strcmp0(name_lower, "bz2") || !g_strcmp0(name_lower, "bzip2"))
+        return CR_CW_BZ2_COMPRESSION;
+    if (!g_strcmp0(name_lower, "xz"))
+        return CR_CW_XZ_COMPRESSION;
+
+    return CR_CW_UNKNOWN_COMPRESSION;
+}
 
 const char *
 cr_compression_suffix(cr_CompressionType comtype)
index 992dcbba3b67a39036755037ed7103dfd29a1f0d..6a4f018fdda64f40c52b722e27de4b65ef6a6c26 100644 (file)
@@ -98,6 +98,12 @@ const char *cr_compression_suffix(cr_CompressionType comtype);
  */
 cr_CompressionType cr_detect_compression(const char* filename, GError **err);
 
+/** Return compression type.
+ * @param name      compression name
+ * @return          compression type
+ */
+cr_CompressionType cr_compression_type(const char *name);
+
 /** Open/Create the specified file.
  * @param FILENAME      filename
  * @param MODE          open mode
index e625a731dd1c028c57140b334c22bd27dcfb7824..0a3dfd9efa09a3743751e3406181e0b84f97a480 100644 (file)
@@ -99,6 +99,42 @@ test_cr_compression_suffix(void)
     g_assert_cmpstr(suffix, ==, ".xz");
 }
 
+static void
+test_cr_compression_type(void)
+{
+    cr_CompressionType type;
+
+    type = cr_compression_type(NULL);
+    g_assert_cmpint(type, ==, CR_CW_UNKNOWN_COMPRESSION);
+
+    type = cr_compression_type("");
+    g_assert_cmpint(type, ==, CR_CW_UNKNOWN_COMPRESSION);
+
+    type = cr_compression_type("foo");
+    g_assert_cmpint(type, ==, CR_CW_UNKNOWN_COMPRESSION);
+
+    type = cr_compression_type("gz");
+    g_assert_cmpint(type, ==, CR_CW_GZ_COMPRESSION);
+
+    type = cr_compression_type("gzip");
+    g_assert_cmpint(type, ==, CR_CW_GZ_COMPRESSION);
+
+    type = cr_compression_type("GZ");
+    g_assert_cmpint(type, ==, CR_CW_GZ_COMPRESSION);
+
+    type = cr_compression_type("Gz");
+    g_assert_cmpint(type, ==, CR_CW_GZ_COMPRESSION);
+
+    type = cr_compression_type("bz2");
+    g_assert_cmpint(type, ==, CR_CW_BZ2_COMPRESSION);
+
+    type = cr_compression_type("bzip2");
+    g_assert_cmpint(type, ==, CR_CW_BZ2_COMPRESSION);
+
+    type = cr_compression_type("xz");
+    g_assert_cmpint(type, ==, CR_CW_XZ_COMPRESSION);
+}
+
 static void
 test_cr_detect_compression(void)
 {
@@ -710,6 +746,8 @@ main(int argc, char *argv[])
             test_cr_compression_suffix);
     g_test_add_func("/compression_wrapper/test_cr_detect_compression",
             test_cr_detect_compression);
+    g_test_add_func("/compression_wrapper/test_cr_compression_type",
+            test_cr_compression_type);
     g_test_add_func("/compression_wrapper/test_cr_detect_compression_bad_suffix",
             test_cr_detect_compression_bad_suffix);
     g_test_add_func("/compression_wrapper/test_cr_read_with_autodetection",