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 Property
get_property(const gchar
* property
) {
84 for (Property i
= N
; i
< VCARD_PROPERTIES
; i
++) {
85 if (g_strcmp0(Properties
[i
], property
) == 0) return i
;
88 return VCARD_PROPERTIES
;
91 static gboolean
check_support(Property property
, VCardVersion version
) {
92 gboolean support
= FALSE
;
94 if (version
== VCARD_VERSION_2_1
) {
95 support
= property
<= TZ
? TRUE
: FALSE
;
96 } else if (version
== VCARD_VERSION_3_0
) {
97 support
= property
<= IMPP
? TRUE
: FALSE
;
98 } else if (version
== VCARD_VERSION_4_0
) {
99 support
= property
<= VCARD_PROPERTIES
? TRUE
: FALSE
;
105 static void remove_cr(gchar
**str
) {
108 for (src
= dst
= *str
; *src
!= '\0'; src
++) {
110 if (*dst
!= '\r') dst
++;
115 static VCardParserResponse
has_required_version(const gchar
** buf
, VCardVersion
* version
) {
116 const gchar
** tmp
= buf
;
117 gboolean found
= FALSE
;
118 VCardParserResponse result
= VCARD_PARSER_VERSION_MISSING
;
120 while (*tmp
&& !found
) {
121 if (!g_str_has_prefix(*tmp
, "VERSION")) {
125 gchar
** b
= g_strsplit(*tmp
, ":", 0);
126 if (g_strv_length(b
) == 2) {
127 if (*version
== VCARD_VERSION_DETECT
) {
128 *version
= str_2_vcard_version(b
[1]);
129 result
= VCARD_PARSER_OK
;
131 if (*version
== str_2_vcard_version(b
[1])) {
132 result
= VCARD_PARSER_OK
;
134 result
= VCARD_PARSER_VERSION_MISMATCH
;
144 static gchar
** vcard_text_normalize(const gchar
** text
) {
146 const gchar
** tmp
= text
;
150 if (text
== NULL
) return NULL
;
152 new = g_new0(gchar
*, g_strv_length((gchar
**) text
) + 1);
153 while (*tmp
&& g_strcmp0(*tmp
, "") != 0) {
154 if (*tmp
[0] == ' ') {
157 gchar
* s
= g_strdup(str
);
159 str
= g_strconcat(s
, "\n", *tmp
+1, NULL
);
161 str
= g_strdup(*tmp
+1);
165 new[count
++] = g_strdup(str
);
169 str
= g_strdup(*tmp
);
175 new[count
++] = g_strdup(str
);
182 static VCardParserResponse
vcard_parse(gchar
** text
, VCardVersion version
, GHashTable
** vcard
) {
183 VCardParserResponse r
= VCARD_PARSER_ERROR
;
184 guint l
= g_strv_length(text
);
187 for (guint i
= 0; i
< l
; i
++) {
188 gchar
** b
= g_strsplit(text
[i
], ":", 2);
189 if (g_strv_length(b
) != 2 || !b
[0] || !b
[1]) {
190 destroy_hash_table(*vcard
);
193 return VCARD_PARSER_ERROR
;
195 gchar
** c
= g_strsplit(b
[0], ";", 0);
196 if (g_strcmp0(c
[0], "BEGIN") == 0 ||
197 g_strcmp0(c
[0], "VERSION") == 0 ||
198 g_strcmp0(c
[0], "END") == 0) {
203 if (g_str_has_prefix(c
[0], "X-")) {
206 Property property
= get_property(c
[0]);
207 support
= check_support(property
, version
);
210 GString
* val
= g_string_new(NULL
);
212 val
= g_string_append(val
, c
[1]);
213 guint l
= g_strv_length(c
);
215 for (guint j
= 2; j
< l
; j
++) {
216 val
= g_string_append(val
, ";");
217 val
= g_string_append(val
, c
[j
]);
221 val
= g_string_append(val
, ":");
222 val
= g_string_append(val
, b
[1]);
225 val
= g_string_append(val
, b
[1]);
227 GSList
* value
= g_hash_table_lookup(*vcard
, c
[0]);
228 VCardProperty
* vp
= g_new0(VCardProperty
, 1);
229 vp
->name
= g_strdup(c
[0]); //b[0]
230 vp
->value
= g_string_free_and_steal(val
); //b[1]
231 value
= g_slist_append(value
, vp
);
232 g_hash_table_insert(*vcard
, g_strdup(c
[0]), value
);
234 destroy_hash_table(*vcard
);
238 return VCARD_PARSER_ATTRIBUTE_VERSION_MISMATCH
;
248 Property
vcard_max_property(VCardVersion version
) {
249 Property p
= VCARD_PROPERTIES
;
251 if (version
== VCARD_VERSION_2_1
) {
253 } else if (version
== VCARD_VERSION_3_0
) {
255 } else if (version
== VCARD_VERSION_4_0
) {
256 p
= VCARD_PROPERTIES
- 1;
262 void destroy_hash_table(GHashTable
* ht
) {
263 g_hash_table_destroy(ht
);
266 GHashTable
* init_g_hash_table(VCardVersion version
) {
267 GHashTable
* ht
= NULL
;
269 Property property
= vcard_max_property(version
);
270 if (property
< VCARD_PROPERTIES
) {
272 ht
= g_hash_table_new_full(
273 g_str_hash
, g_str_equal
, (GDestroyNotify
) g_free
,
274 (GDestroyNotify
) vcard_slist_property_free
);
276 for (Property i
= N
; i
<= property
; i
++) {
277 g_hash_table_insert(ht
, g_strdup(Properties
[i
]), NULL
);
284 VCardVersion
str_2_vcard_version(const gchar
* version
) {
285 VCardVersion vv
= VCARD_VERSION_DETECT
;
288 if (g_strcmp0("2.1", version
) == 0)
289 vv
= VCARD_VERSION_2_1
;
290 else if (g_strcmp0("3.0", version
) == 0)
291 vv
= VCARD_VERSION_3_0
;
292 else if (g_strcmp0("4.0", version
) == 0)
293 vv
= VCARD_VERSION_4_0
;
295 vv
= VCARD_VERSION_UNSUPPORTED
;
301 gchar
* vcard_version_2_str(VCardVersion version
) {
304 if (VCARD_VERSION_2_1
== version
)
305 vv
= g_strdup("2.1");
306 else if (VCARD_VERSION_3_0
== version
)
307 vv
= g_strdup("3.0");
308 else if (VCARD_VERSION_4_0
== version
)
309 vv
= g_strdup("4.0");
314 VCardParserResponse
vcard_parse_text(const gchar
* text
, VCardVersion
* version
, GHashTable
** vcard
) {
315 VCardParserResponse r
= VCARD_PARSER_ERROR
;
317 if (text
== NULL
|| strlen(text
) < 1) return VCARD_PARSER_OK
;
319 gchar
** b
= g_strsplit(text
, "\n", 0);
320 gchar
** buf
= vcard_text_normalize((const gchar
**)b
);
322 guint l
= g_strv_length(buf
);
323 //begin, end and version line is required
324 r
= VCARD_PARSER_BAD_FORMAT
;
326 if (g_strcmp0(g_strchomp(buf
[0]), "BEGIN:VCARD") == 0 && g_strcmp0(g_strchomp(buf
[l
-1]), "END:VCARD") == 0) {
327 r
= has_required_version((const gchar
**)buf
, version
);
328 if (r
== VCARD_PARSER_OK
) {
329 *vcard
=init_g_hash_table(*version
);
330 if (vcard
&& *vcard
) {
331 r
= vcard_parse(buf
, *version
, vcard
);
333 r
= VCARD_PARSER_ERROR
;