1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2008 Christophe Fergeau <teuf@gnome.org>
4 * Based on itdb_plist parser from the gtkpod project.
6 * The code contained in this file is free software; you can redistribute
7 * it and/or modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either version
9 * 2.1 of the License, or (at your option) any later version.
11 * This file 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this code; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "empathy-plist.h"
25 #include <libxml/tree.h>
26 #include <telepathy-glib/telepathy-glib.h>
28 static GValue
*empathy_plist_parse_node (xmlNode
*a_node
);
31 empathy_plist_parse_integer (xmlNode
*a_node
)
37 str_val
= (char *) xmlNodeGetContent (a_node
);
38 int_val
= strtol (str_val
, &end_ptr
, 0);
39 if (*end_ptr
!= '\0') {
45 return tp_g_value_slice_new_int (int_val
);
49 empathy_plist_parse_string (xmlNode
*a_node
)
54 str_val
= (char *) xmlNodeGetContent (a_node
);
56 value
= tp_g_value_slice_new_string (str_val
);
64 empathy_plist_parse_real (xmlNode
*a_node
)
70 str_val
= (char *) xmlNodeGetContent (a_node
);
71 double_val
= g_ascii_strtod (str_val
, &end_ptr
);
72 if (*end_ptr
!= '\0') {
78 return tp_g_value_slice_new_double (double_val
);
82 empathy_plist_parse_boolean (xmlNode
*a_node
)
86 if (strcmp ((char *) a_node
->name
, "true") == 0) {
88 } else if (strcmp ((char *) a_node
->name
, "false") == 0) {
94 return tp_g_value_slice_new_boolean (bool_val
);
98 empathy_plist_parse_data (xmlNode
*a_node
)
105 str_val
= (char *) xmlNodeGetContent (a_node
);
106 raw_data
= g_base64_decode (str_val
, &len
);
109 value
= tp_g_value_slice_new_bytes (len
, raw_data
);
117 empathy_plist_parse_one_dict_entry (xmlNode
*a_node
, GHashTable
*dict
)
119 xmlNode
*cur_node
= a_node
;
124 (xmlStrcmp (cur_node
->name
, (xmlChar
*) "key") != 0)) {
125 cur_node
= cur_node
->next
;
130 key_name
= xmlNodeGetContent (cur_node
);
131 cur_node
= cur_node
->next
;
132 while (cur_node
&& xmlIsBlankNode (cur_node
)) {
133 cur_node
= cur_node
->next
;
140 value
= empathy_plist_parse_node (cur_node
);
142 g_hash_table_insert (dict
, g_strdup ((char *) key_name
), value
);
146 return cur_node
->next
;
150 empathy_plist_parse_dict (xmlNode
*a_node
)
152 xmlNode
*cur_node
= a_node
->children
;
155 dict
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
156 g_free
, (GDestroyNotify
) tp_g_value_slice_free
);
159 if (xmlIsBlankNode (cur_node
)) {
160 cur_node
= cur_node
->next
;
162 cur_node
= empathy_plist_parse_one_dict_entry (cur_node
, dict
);
166 return tp_g_value_slice_new_take_boxed (G_TYPE_HASH_TABLE
, dict
);
169 typedef GValue
*(*ParseCallback
) (xmlNode
*);
172 const char * const type_name
;
173 ParseCallback parser
;
176 static const struct Parser parsers
[] = { {"integer", empathy_plist_parse_integer
},
177 {"real", empathy_plist_parse_real
},
178 {"string", empathy_plist_parse_string
},
179 {"true", empathy_plist_parse_boolean
},
180 {"false", empathy_plist_parse_boolean
},
181 {"data", empathy_plist_parse_data
},
182 {"dict", empathy_plist_parse_dict
},
186 empathy_plist_get_parser_for_type (const xmlChar
*type
)
190 while (parsers
[i
].type_name
) {
191 if (xmlStrcmp (type
, (xmlChar
*) parsers
[i
].type_name
) == 0) {
192 if (parsers
[i
].parser
) {
193 return parsers
[i
].parser
;
202 empathy_plist_parse_node (xmlNode
*a_node
)
204 ParseCallback parser
;
206 g_return_val_if_fail (a_node
!= NULL
, NULL
);
207 parser
= empathy_plist_get_parser_for_type (a_node
->name
);
209 return parser (a_node
);
216 empathy_plist_parse (xmlNode
* a_node
)
223 if (xmlStrcmp (a_node
->name
, (xmlChar
*) "plist") != 0) {
226 cur_node
= a_node
->xmlChildrenNode
;
227 while (cur_node
&& (xmlIsBlankNode (cur_node
))) {
228 cur_node
= cur_node
->next
;
231 return empathy_plist_parse_node (cur_node
);
238 * empathy_plist_parse_from_file:
239 * @filename: file containing XML plist data to parse
241 * Parses the XML plist file. If an error occurs during the parsing,
242 * empathy_plist_parse_from_file() will return NULL.
244 * Returns: NULL on error, a newly allocated
245 * #GValue otherwise. Free it using tp_g_value_slice_free()
248 empathy_plist_parse_from_file (const char *filename
)
251 xmlNode
*root_element
= NULL
;
254 doc
= xmlReadFile (filename
, NULL
, 0);
260 root_element
= xmlDocGetRootElement (doc
);
262 parsed_doc
= empathy_plist_parse (root_element
);
270 * empathy_plist_parse_from_memory:
271 * @data: memory location containing XML plist data to parse
272 * @len: length in bytes of the string to parse
274 * Parses the XML plist file stored in @data which length is @len
275 * bytes. If an error occurs during the parsing,
276 * empathy_plist_parse_from_memory() will return NULL.
278 * Returns: NULL on error, a newly allocated
279 * #GValue otherwise. Free it using tp_g_value_slice_free()
282 empathy_plist_parse_from_memory (const char *data
, gsize len
)
285 xmlNode
*root_element
= NULL
;
288 doc
= xmlReadMemory (data
, len
, "noname.xml", NULL
, 0);
294 root_element
= xmlDocGetRootElement (doc
);
296 parsed_doc
= empathy_plist_parse (root_element
);