Add function is_primary() into misc module.
authorTomas Mlcoch <tmlcoch@redhat.com>
Tue, 10 Jan 2012 14:17:19 +0000 (15:17 +0100)
committerTomas Mlcoch <tmlcoch@redhat.com>
Tue, 10 Jan 2012 14:17:19 +0000 (15:17 +0100)
Makefile
misc.c
misc.h
xml_dump.c
xml_dump_primary.c

index 1090d24..d464724 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -42,8 +42,8 @@ xml_dump_other.o: xml_dump_other.c xml_dump.h
 package.so: package_wrap.o package.o
        ld $(LINKFLAGS) -shared package.o package_wrap.o -o _package.so
 
-xml_dump.so: package.o xml_dump_wrap.o xml_dump.o xml_dump_primary.o xml_dump_filelists.o xml_dump_other.o
-       ld $(LINKFLAGS) -shared package.o xml_dump_wrap.o xml_dump.o xml_dump_primary.o xml_dump_filelists.o xml_dump_other.o -o _xml_dump.so
+xml_dump.so: package.o xml_dump_wrap.o xml_dump.o xml_dump_primary.o xml_dump_filelists.o xml_dump_other.o misc.o
+       ld $(LINKFLAGS) -shared package.o xml_dump_wrap.o xml_dump.o xml_dump_primary.o xml_dump_filelists.o xml_dump_other.o misc.o -o _xml_dump.so
 
 parsehdr.so: parsehdr_wrap.o parsehdr.o package.o xml_dump.o misc.o
        ld $(LINKFLAGS) -shared misc.o parsehdr_wrap.o parsehdr.o package.o xml_dump.o xml_dump_primary.o xml_dump_filelists.o xml_dump_other.o -o _parsehdr.so
diff --git a/misc.c b/misc.c
index 78ea27f..364a5ce 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -1,3 +1,4 @@
+#include <glib.h>
 #include <string.h>
 #include <stdlib.h>
 #include "misc.h"
@@ -26,10 +27,17 @@ const char *flag_to_string(gint64 flags)
 
 
 /*
- * BE CAREFULL! Returned structure had all strings malloced!!!
+ * BE CAREFULL!
+ *
+ * In case chunk param is NULL:
+ * Returned structure had all strings malloced!!!
  * Be so kind and don't forget use free() for all its element, before end of structure lifecycle.
+ *
+ * In case chunk is pointer to a GStringChunk:
+ * Returned structure had all string inserted in the passed chunk.
+ *
  */
-struct VersionStruct string_to_version(const char *string)
+struct VersionStruct string_to_version(const char *string, GStringChunk *chunk)
 {
     struct VersionStruct ver;
     ver.epoch = NULL;
@@ -51,42 +59,85 @@ struct VersionStruct string_to_version(const char *string)
         strtol(string, &p, 10);
         if (p == ptr) { // epoch str seems to be a number
             size_t len = ptr - string;
-            ver.epoch = malloc(sizeof(char) * (len + 1));
-            strncpy(ver.epoch, string, len);
-            ver.epoch[len] = '\0';
-        } //else { // epoch is not a number
-            //ver.epoch = NULL;
-        //}
-    } else {
-        //ver.epoch = NULL;
+            if (chunk) {
+                ver.epoch = g_string_chunk_insert_len(chunk, string, len);
+            } else {
+                ver.epoch = malloc(sizeof(char) * (len + 1));
+                strncpy(ver.epoch, string, len);
+                ver.epoch[len] = '\0';
+            }
+        }
+    } else { // There is no epoch
         ptr = string-1;
     }
 
+    if (!ver.epoch) {
+        if (chunk) {
+            ver.epoch = g_string_chunk_insert_const(chunk, "0");
+        } else {
+            ver.epoch = malloc(sizeof(char) * 2);
+            ver.epoch[0] = '0';
+            ver.epoch[1] = '\0';
+        }
+    }
+
     // Version + release
     ptr2 = strstr(ptr+1, "-");
     if (ptr2) {
-        size_t len = ptr2 - (ptr+1);
-        ver.version = malloc(sizeof(char) * (len + 1));
-        strncpy(ver.version, ptr+1, len);
-        ver.version[len] = '\0';
-        ver.release = malloc(sizeof(char) * (strlen(ptr2+1) + 1));
-        strcpy(ver.release, ptr2+1);
-    } else {
-        ver.version = malloc(sizeof(char) * (strlen(ptr+1) + 1));
-        strcpy(ver.version, ptr+1);
-    }
+        // Version
+        size_t version_len = ptr2 - (ptr+1);
+        if (chunk) {
+            ver.version = g_string_chunk_insert_len(chunk, ptr+1, version_len);
+        } else {
+            ver.version = malloc(sizeof(char) * (version_len + 1));
+            strncpy(ver.version, ptr+1, version_len);
+            ver.version[version_len] = '\0';
+        }
 
-    if (!ver.epoch) {
-        ver.epoch = malloc(sizeof(char) * 2);
-        ver.epoch[0] = '0';
-        ver.epoch[1] = '\0';
-    } //else if (!ver.version) {
-//        ver.version = malloc(sizeof(char));
-//        ver.version[0] = '\0';
-//    } else if (!ver.release) {
-//        ver.release = malloc(sizeof(char));
-//        ver.release[0] = '\0';
-//    }
+        // Release
+        size_t release_len = strlen(ptr2+1);
+        if (release_len) {
+            if (chunk) {
+                ver.release = g_string_chunk_insert_len(chunk, ptr2+1, release_len);
+            } else {
+                ver.release = malloc(sizeof(char) * (release_len+1));
+                strcpy(ver.release, ptr2+1);
+            }
+        }
+    } else { // Release is not here, just version
+        if (chunk) {
+            ver.version = g_string_chunk_insert_const(chunk, ptr+1);
+        } else {
+            ver.version = malloc(sizeof(char) * (strlen(ptr+1) + 1));
+            strcpy(ver.version, ptr+1);
+        }
+    }
 
     return ver;
 }
+
+
+GRegex *pri_re_1 = NULL;
+GRegex *pri_re_2 = NULL;
+GRegex *pri_re_3 = NULL;
+
+int is_primary(const char *filename)
+{
+    // Regex compilation
+    if (!pri_re_1) {
+        GRegexMatchFlags compile_flags = G_REGEX_OPTIMIZE|G_REGEX_MATCH_ANCHORED;
+        pri_re_1 = g_regex_new(".*bin/.*", compile_flags, 0, NULL);
+        pri_re_2 = g_regex_new("/etc/.*", compile_flags, 0, NULL);
+        pri_re_3 = g_regex_new("/usr/lib/sendmail$", compile_flags, 0, NULL);
+    }
+
+    if (g_regex_match(pri_re_1, filename, 0, NULL)
+        || g_regex_match(pri_re_2, filename, 0, NULL)
+        || g_regex_match(pri_re_3, filename, 0, NULL))
+    {
+        return 1;
+    }
+
+    return 0;
+}
+
diff --git a/misc.h b/misc.h
index ce84256..9f3254e 100644 (file)
--- a/misc.h
+++ b/misc.h
@@ -16,6 +16,7 @@ struct VersionStruct {
  * BE CAREFULL! Returned structure had all strings malloced!!!
  * Be so kind and don't forget use free() for all its element, before end of structure lifecycle.
  */
-struct VersionStruct string_to_version(const char *string);
+struct VersionStruct string_to_version(const char *string, GStringChunk *chunk);
+int is_primary(const char *filename);
 
 #endif /* __MISC__ */
index fa1c781..35e2e12 100644 (file)
@@ -1,6 +1,7 @@
 #include <libxml/encoding.h>
 #include <libxml/xmlwriter.h>
 #include <string.h>
+#include "misc.h"
 #include "xml_dump.h"
 
 //#define DEBUG
@@ -59,26 +60,14 @@ ConvertInput(const char *in, xmlCharEncodingHandlerPtr handler)
 }
 
 
-GRegex *pri_re_1 = NULL;
-GRegex *pri_re_2 = NULL;
-GRegex *pri_re_3 = NULL;
-
 void
-dump_files(xmlTextWriterPtr writer, Package *package, int primary, 
+dump_files(xmlTextWriterPtr writer, Package *package, int primary,
            xmlCharEncodingHandlerPtr handler)
 {
 #ifdef DEBUG
     printf("CALLED dump_files\n");
 #endif
 
-    // Regex compilation
-    if (!pri_re_1) {
-        GRegexMatchFlags compile_flags = G_REGEX_OPTIMIZE|G_REGEX_MATCH_ANCHORED;
-        pri_re_1 = g_regex_new(".*bin/.*", compile_flags, 0, NULL);
-        pri_re_2 = g_regex_new("/etc/.*", compile_flags, 0, NULL);
-        pri_re_3 = g_regex_new("/usr/lib/sendmail$", compile_flags, 0, NULL);
-    }
-
     xmlChar *tmp = NULL;
 
     if (!package->files) {
@@ -89,13 +78,8 @@ dump_files(xmlTextWriterPtr writer, Package *package, int primary,
     for(element = package->files; element; element=element->next) {
         PackageFile *entry = (PackageFile*) element->data;
 
-        if (primary) {
-            // Check if file name match pattern for primary files
-            if (!g_regex_match(pri_re_1, entry->name, 0, NULL)
-               && !g_regex_match(pri_re_2, entry->name, 0, NULL)
-               && !g_regex_match(pri_re_3, entry->name, 0, NULL)) {
-                continue;
-            }
+        if (primary && !is_primary(entry->name)) {
+            continue;
         }
 
 
index 5c92193..bc43bac 100644 (file)
@@ -140,7 +140,7 @@ dump_pco(xmlTextWriterPtr writer, Package *package, int pcotype,
             if (entry->pre) {
                 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "pre", "1");
             } else {
-                rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "pre", "0");
+                ; //rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "pre", "0");
             }
             if (rc < 0) {
                  printf("Error at xmlTextWriterWriteAttribute\n");