X-Git-Url: http://git.datanom.net/vcard-parser.git/blobdiff_plain/c33ca4d81ed99f670f87ee9d9794fbddc592e173..HEAD:/src/parser/vcard-parser.c?ds=sidebyside diff --git a/src/parser/vcard-parser.c b/src/parser/vcard-parser.c index 380c19e..fc6f6f5 100644 --- a/src/parser/vcard-parser.c +++ b/src/parser/vcard-parser.c @@ -80,14 +80,6 @@ static gchar* Properties[VCARD_PROPERTIES+1] = { 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; @@ -215,10 +207,27 @@ static VCardParserResponse vcard_parse(gchar** text, VCardVersion version, GHash 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]); - 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 { @@ -251,29 +260,25 @@ Property vcard_max_property(VCardVersion version) { } 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); } -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) { - *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++) { - 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) { @@ -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) { - init_g_hash_table(vcard, *version); + *vcard =init_g_hash_table(*version); if (vcard && *vcard) { r = vcard_parse(buf, *version, vcard); } else {