4 * Copyright 2019 Michael Rasmussen <mir@datanom.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include <vcard-parser.h>
28 static gchar
* Properties
[VCARD_PROPERTIES
+1] = {
83 static void vcard_property_free(gpointer data
) {
85 VCardProperty
* vp
= (VCardProperty
*) data
;
91 static Property
get_property(const gchar
* property
) {
92 for (Property i
= N
; i
< VCARD_PROPERTIES
; i
++) {
93 if (g_strcmp0(Properties
[i
], property
) == 0) return i
;
96 return VCARD_PROPERTIES
;
99 static gboolean
check_support(Property property
, VCardVersion version
) {
100 gboolean support
= FALSE
;
102 if (version
== VCARD_VERSION_2_1
) {
103 support
= property
<= TZ
? TRUE
: FALSE
;
104 } else if (version
== VCARD_VERSION_3_0
) {
105 support
= property
<= IMPP
? TRUE
: FALSE
;
106 } else if (version
== VCARD_VERSION_4_0
) {
107 support
= property
<= VCARD_PROPERTIES
? TRUE
: FALSE
;
113 static void remove_cr(gchar
**str
) {
116 for (src
= dst
= *str
; *src
!= '\0'; src
++) {
118 if (*dst
!= '\r') dst
++;
123 static VCardParserResponse
has_required_version(const gchar
** buf
, VCardVersion
* version
) {
124 const gchar
** tmp
= buf
;
125 gboolean found
= FALSE
;
126 VCardParserResponse result
= VCARD_PARSER_VERSION_MISSING
;
128 while (*tmp
&& !found
) {
129 if (!g_str_has_prefix(*tmp
, "VERSION")) {
133 gchar
** b
= g_strsplit(*tmp
, ":", 0);
134 if (g_strv_length(b
) == 2) {
135 if (*version
== VCARD_VERSION_DETECT
) {
136 *version
= str_2_vcard_version(b
[1]);
137 result
= VCARD_PARSER_OK
;
139 if (*version
== str_2_vcard_version(b
[1])) {
140 result
= VCARD_PARSER_OK
;
142 result
= VCARD_PARSER_VERSION_MISMATCH
;
152 static gchar
** vcard_text_normalize(const gchar
** text
) {
154 const gchar
** tmp
= text
;
158 if (text
== NULL
) return NULL
;
160 new = g_new0(gchar
*, g_strv_length((gchar
**) text
) + 1);
161 while (*tmp
&& g_strcmp0(*tmp
, "") != 0) {
162 if (*tmp
[0] == ' ') {
165 gchar
* s
= g_strdup(str
);
167 str
= g_strconcat(s
, "\n", *tmp
+1, NULL
);
169 str
= g_strdup(*tmp
+1);
173 new[count
++] = g_strdup(str
);
177 str
= g_strdup(*tmp
);
183 new[count
++] = g_strdup(str
);
190 static VCardParserResponse
vcard_parse(gchar
** text
, VCardVersion version
, GHashTable
** vcard
) {
191 VCardParserResponse r
= VCARD_PARSER_ERROR
;
192 guint l
= g_strv_length(text
);
195 for (guint i
= 0; i
< l
; i
++) {
196 gchar
** b
= g_strsplit(text
[i
], ":", 2);
197 if (g_strv_length(b
) != 2 || !b
[0] || !b
[1]) {
198 destroy_hash_table(*vcard
);
201 return VCARD_PARSER_ERROR
;
203 gchar
** c
= g_strsplit(b
[0], ";", 0);
204 if (g_strcmp0(c
[0], "BEGIN") == 0 ||
205 g_strcmp0(c
[0], "VERSION") == 0 ||
206 g_strcmp0(c
[0], "END") == 0) {
211 if (g_str_has_prefix(c
[0], "X-")) {
214 Property property
= get_property(c
[0]);
215 support
= check_support(property
, version
);
218 GSList
* value
= g_hash_table_lookup(*vcard
, c
[0]);
219 VCardProperty
* vp
= g_new(VCardProperty
, 1);
220 vp
->name
= g_strdup(b
[0]);
221 vp
->value
= g_strdup(b
[1]);
222 value
= g_slist_append(value
, vp
);
223 g_hash_table_insert(*vcard
, g_strdup(c
[0]), value
);
225 destroy_hash_table(*vcard
);
229 return VCARD_PARSER_ATTRIBUTE_VERSION_MISMATCH
;
239 Property
vcard_max_property(VCardVersion version
) {
240 Property p
= VCARD_PROPERTIES
;
242 if (version
== VCARD_VERSION_2_1
) {
244 } else if (version
== VCARD_VERSION_3_0
) {
246 } else if (version
== VCARD_VERSION_4_0
) {
247 p
= VCARD_PROPERTIES
- 1;
253 void destroy_hash_table(GHashTable
* ht
) {
257 g_hash_table_iter_init (&iter
, ht
);
258 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
260 g_slist_free_full((GSList
*) value
, vcard_property_free
);
263 g_hash_table_destroy(ht
);
266 void init_g_hash_table(GHashTable
** ht
, VCardVersion version
) {
269 Property property
= vcard_max_property(version
);
270 if (property
< VCARD_PROPERTIES
) {
271 *ht
= g_hash_table_new_full(
272 g_str_hash
, g_str_equal
, (GDestroyNotify
) g_free
, NULL
);
273 for (Property i
= N
; i
<= property
; i
++) {
274 g_hash_table_insert(*ht
, g_strdup(Properties
[i
]), NULL
);
279 VCardVersion
str_2_vcard_version(const gchar
* version
) {
280 VCardVersion vv
= VCARD_VERSION_DETECT
;
283 if (g_strcmp0("2.1", version
) == 0)
284 vv
= VCARD_VERSION_2_1
;
285 else if (g_strcmp0("3.0", version
) == 0)
286 vv
= VCARD_VERSION_3_0
;
287 else if (g_strcmp0("4.0", version
) == 0)
288 vv
= VCARD_VERSION_4_0
;
290 vv
= VCARD_VERSION_UNSUPPORTED
;
296 gchar
* vcard_version_2_str(VCardVersion version
) {
299 if (VCARD_VERSION_2_1
== version
)
300 vv
= g_strdup("2.1");
301 else if (VCARD_VERSION_3_0
== version
)
302 vv
= g_strdup("3.0");
303 else if (VCARD_VERSION_4_0
== version
)
304 vv
= g_strdup("4.0");
309 VCardParserResponse
vcard_parse_text(const gchar
* text
, VCardVersion
* version
, GHashTable
** vcard
) {
310 VCardParserResponse r
= VCARD_PARSER_ERROR
;
312 if (text
== NULL
|| strlen(text
) < 1) return VCARD_PARSER_OK
;
314 gchar
** b
= g_strsplit(text
, "\n", 0);
315 gchar
** buf
= vcard_text_normalize((const gchar
**)b
);
317 guint l
= g_strv_length(buf
);
318 //begin, end and version line is required
319 r
= VCARD_PARSER_BAD_FORMAT
;
321 if (g_strcmp0(g_strchomp(buf
[0]), "BEGIN:VCARD") == 0 && g_strcmp0(g_strchomp(buf
[l
-1]), "END:VCARD") == 0) {
322 r
= has_required_version((const gchar
**)buf
, version
);
323 if (r
== VCARD_PARSER_OK
) {
324 init_g_hash_table(vcard
, *version
);
325 if (vcard
&& *vcard
) {
326 r
= vcard_parse(buf
, *version
, vcard
);
328 r
= VCARD_PARSER_ERROR
;