Add misc library with helper functions
authorTomas Mlcoch <tmlcoch@redhat.com>
Wed, 4 Jan 2012 12:56:06 +0000 (13:56 +0100)
committerTomas Mlcoch <tmlcoch@redhat.com>
Wed, 4 Jan 2012 12:56:06 +0000 (13:56 +0100)
misc.c [new file with mode: 0644]
misc.h [new file with mode: 0644]

diff --git a/misc.c b/misc.c
new file mode 100644 (file)
index 0000000..3f41e34
--- /dev/null
+++ b/misc.c
@@ -0,0 +1,97 @@
+#include <string.h>
+#include <stdlib.h>
+#include "misc.h"
+
+
+const char *flag_to_string(gint64 flags)
+{
+    flags &= 0xf;
+    switch(flags) {
+        case 0:
+            return "";
+        case 2:
+            return "LT";
+        case 4:
+            return "GT";
+        case 8:
+            return "EQ";
+        case 10:
+            return "LE";
+        case 12:
+            return "GE";
+        default:
+            return "";
+    }
+}
+
+
+/*
+ * 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 ver;
+    ver.epoch = NULL;
+    ver.version = NULL;
+    ver.release = NULL;
+
+    if (!string || !(strlen(string))) {
+        ver.epoch = malloc(sizeof(char));
+        ver.version = malloc(sizeof(char));
+        ver.release = malloc(sizeof(char));
+        ver.epoch[0] = '\0';
+        ver.version[0] = '\0';
+        ver.release[0] = '\0';
+        return ver;
+    }
+
+    char *ptr;  // These names are totally self explaining
+    char *ptr2; //
+
+    // Epoch
+    ptr = strstr(string, ":");
+    if (ptr) {
+        // Check if epoch str is a number
+        char *p = NULL;
+        strtol(ver.epoch, &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;
+    }
+
+    // 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);
+    }
+
+    // Malloc empty strings instead of NULL
+    if (!ver.epoch) {
+        ver.epoch = malloc(sizeof(char));
+        ver.epoch[0] = '\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';
+    }
+
+    return ver;
+}
diff --git a/misc.h b/misc.h
new file mode 100644 (file)
index 0000000..ce84256
--- /dev/null
+++ b/misc.h
@@ -0,0 +1,21 @@
+#ifndef __MISC__
+#define __MISC__
+
+#include <glib.h>
+
+const char *flag_to_string(gint64 flags);
+
+
+struct VersionStruct {
+    char *epoch;
+    char *version;
+    char *release;
+};
+
+/*
+ * 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);
+
+#endif /* __MISC__ */