]> git.datanom.net - vcard-parser.git/blobdiff - src/parser/vcard-parser.c
All features added
[vcard-parser.git] / src / parser / vcard-parser.c
index 380c19e15ebbd2fd83a187dd5842dd4f185282b1..fc6f6f5cf402d042fc6baa6b8e4d0654a164cabc 100644 (file)
@@ -80,14 +80,6 @@ static gchar* Properties[VCARD_PROPERTIES+1] = {
        NULL
 };
 
        NULL
 };
 
-static void vcard_property_free(gpointer data) {
-       if (!data) return;
-       VCardProperty* vp = (VCardProperty*) data;
-       g_free(vp->name);
-       g_free(vp->value);
-       g_free(vp);
-}
-
 static Property get_property(const gchar* property) {
        for (Property i = N; i < VCARD_PROPERTIES; i++) {
                if (g_strcmp0(Properties[i], property) == 0) return i;
 static Property get_property(const gchar* property) {
        for (Property i = N; i < VCARD_PROPERTIES; i++) {
                if (g_strcmp0(Properties[i], property) == 0) return i;
@@ -215,10 +207,27 @@ static VCardParserResponse vcard_parse(gchar** text, VCardVersion version, GHash
                        support = check_support(property, version);
                }
                if (support) {
                        support = check_support(property, version);
                }
                if (support) {
+                       GString* val = g_string_new(NULL);
+                       if (c[1]) {
+                               val = g_string_append(val, c[1]);
+                               guint l = g_strv_length(c);
+                               if (l > 2) {
+                                       for (guint j = 2; j < l; j++) {
+                                               val = g_string_append(val, ";");
+                                               val = g_string_append(val, c[j]);
+                                       }
+                               }
+                               if (b[1]) {
+                                       val = g_string_append(val, ":");
+                                       val = g_string_append(val, b[1]);
+                               }
+                       } else {
+                               val = g_string_append(val, b[1]);
+                       }
                        GSList* value = g_hash_table_lookup(*vcard, c[0]);
                        GSList* value = g_hash_table_lookup(*vcard, c[0]);
-                       VCardProperty* vp = g_new(VCardProperty, 1);
-                       vp->name = g_strdup(b[0]);
-                       vp->value = g_strdup(b[1]);
+                       VCardProperty* vp = g_new0(VCardProperty, 1);
+                       vp->name = g_strdup(c[0]); //b[0]
+                       vp->value = g_string_free_and_steal(val); //b[1]
                        value = g_slist_append(value, vp);
                        g_hash_table_insert(*vcard, g_strdup(c[0]), value);
                } else {
                        value = g_slist_append(value, vp);
                        g_hash_table_insert(*vcard, g_strdup(c[0]), value);
                } else {
@@ -251,29 +260,25 @@ Property vcard_max_property(VCardVersion version) {
 }
 
 void destroy_hash_table(GHashTable* ht) {
 }
 
 void destroy_hash_table(GHashTable* ht) {
-       GHashTableIter iter;
-       gpointer key, value;
-
-       g_hash_table_iter_init (&iter, ht);
-       while (g_hash_table_iter_next(&iter, &key, &value)) {
-               if (value)
-                       g_slist_free_full((GSList *) value, vcard_property_free);
-       }
-
        g_hash_table_destroy(ht);
 }
 
        g_hash_table_destroy(ht);
 }
 
-void init_g_hash_table(GHashTable** ht, VCardVersion version) {
-       if (!ht) return;
+GHashTable* init_g_hash_table(VCardVersion version) {
+       GHashTable* ht = NULL;
 
        Property property = vcard_max_property(version);
        if (property < VCARD_PROPERTIES) {
 
        Property property = vcard_max_property(version);
        if (property < VCARD_PROPERTIES) {
-               *ht = g_hash_table_new_full(
-                       g_str_hash, g_str_equal, (GDestroyNotify) g_free, NULL);
+
+               ht = g_hash_table_new_full(
+                       g_str_hash, g_str_equal, (GDestroyNotify) g_free,
+                       (GDestroyNotify) vcard_slist_property_free);
+
                for (Property i = N; i <= property; i++) {
                for (Property i = N; i <= property; i++) {
-                       g_hash_table_insert(*ht, g_strdup(Properties[i]), NULL);
+                       g_hash_table_insert(ht, g_strdup(Properties[i]), NULL);
                }
        }
                }
        }
+
+       return ht;
 }
 
 VCardVersion str_2_vcard_version(const gchar* version) {
 }
 
 VCardVersion str_2_vcard_version(const gchar* version) {
@@ -321,7 +326,7 @@ VCardParserResponse vcard_parse_text(const gchar* text, VCardVersion* version, G
                if (g_strcmp0(g_strchomp(buf[0]), "BEGIN:VCARD") == 0 && g_strcmp0(g_strchomp(buf[l-1]), "END:VCARD") == 0) {
                        r = has_required_version((const gchar **)buf, version);
                        if (r == VCARD_PARSER_OK) {
                if (g_strcmp0(g_strchomp(buf[0]), "BEGIN:VCARD") == 0 && g_strcmp0(g_strchomp(buf[l-1]), "END:VCARD") == 0) {
                        r = has_required_version((const gchar **)buf, version);
                        if (r == VCARD_PARSER_OK) {
-                               init_g_hash_table(vcard, *version);
+                               *vcard =init_g_hash_table(*version);
                                if (vcard && *vcard) {
                                        r = vcard_parse(buf, *version, vcard);
                                } else {
                                if (vcard && *vcard) {
                                        r = vcard_parse(buf, *version, vcard);
                                } else {
This page took 0.651267 seconds and 5 git commands to generate.