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
24 #include <libxml/tree.h>
25 #include <telepathy-glib/telepathy-glib.h>
27 #include "empathy-plist.h"
29 static GValue *empathy_plist_parse_node (xmlNode *a_node);
32 empathy_plist_parse_integer (xmlNode *a_node)
38 str_val = (char *) xmlNodeGetContent (a_node);
39 int_val = strtol (str_val, &end_ptr, 0);
40 if (*end_ptr != '\0') {
46 return tp_g_value_slice_new_int (int_val);
50 empathy_plist_parse_string (xmlNode *a_node)
55 str_val = (char *) xmlNodeGetContent (a_node);
57 value = tp_g_value_slice_new_string (str_val);
65 empathy_plist_parse_real (xmlNode *a_node)
71 str_val = (char *) xmlNodeGetContent (a_node);
72 double_val = g_ascii_strtod (str_val, &end_ptr);
73 if (*end_ptr != '\0') {
79 return tp_g_value_slice_new_double (double_val);
83 empathy_plist_parse_boolean (xmlNode *a_node)
87 if (strcmp ((char *) a_node->name, "true") == 0) {
89 } else if (strcmp ((char *) a_node->name, "false") == 0) {
95 return tp_g_value_slice_new_boolean (bool_val);
99 empathy_plist_parse_data (xmlNode *a_node)
106 str_val = (char *) xmlNodeGetContent (a_node);
107 raw_data = g_base64_decode (str_val, &len);
110 value = tp_g_value_slice_new_bytes (len, raw_data);
118 empathy_plist_parse_one_dict_entry (xmlNode *a_node, GHashTable *dict)
120 xmlNode *cur_node = a_node;
125 (xmlStrcmp (cur_node->name, (xmlChar *) "key") != 0)) {
126 cur_node = cur_node->next;
131 key_name = xmlNodeGetContent (cur_node);
132 cur_node = cur_node->next;
133 while (cur_node && xmlIsBlankNode (cur_node)) {
134 cur_node = cur_node->next;
141 value = empathy_plist_parse_node (cur_node);
143 g_hash_table_insert (dict, g_strdup ((char *) key_name), value);
147 return cur_node->next;
151 empathy_plist_parse_dict (xmlNode *a_node)
153 xmlNode *cur_node = a_node->children;
156 dict = g_hash_table_new_full (g_str_hash, g_str_equal,
157 g_free, (GDestroyNotify) tp_g_value_slice_free);
160 if (xmlIsBlankNode (cur_node)) {
161 cur_node = cur_node->next;
163 cur_node = empathy_plist_parse_one_dict_entry (cur_node, dict);
167 return tp_g_value_slice_new_take_boxed (G_TYPE_HASH_TABLE, dict);
170 typedef GValue *(*ParseCallback) (xmlNode *);
173 const char * const type_name;
174 ParseCallback parser;
177 static const struct Parser parsers[] = { {"integer", empathy_plist_parse_integer},
178 {"real", empathy_plist_parse_real},
179 {"string", empathy_plist_parse_string},
180 {"true", empathy_plist_parse_boolean},
181 {"false", empathy_plist_parse_boolean},
182 {"data", empathy_plist_parse_data},
183 {"dict", empathy_plist_parse_dict},
187 empathy_plist_get_parser_for_type (const xmlChar *type)
191 while (parsers[i].type_name) {
192 if (xmlStrcmp (type, (xmlChar *) parsers[i].type_name) == 0) {
193 if (parsers[i].parser) {
194 return parsers[i].parser;
203 empathy_plist_parse_node (xmlNode *a_node)
205 ParseCallback parser;
207 g_return_val_if_fail (a_node != NULL, NULL);
208 parser = empathy_plist_get_parser_for_type (a_node->name);
210 return parser (a_node);
217 empathy_plist_parse (xmlNode * a_node)
224 if (xmlStrcmp (a_node->name, (xmlChar *) "plist") != 0) {
227 cur_node = a_node->xmlChildrenNode;
228 while (cur_node && (xmlIsBlankNode (cur_node))) {
229 cur_node = cur_node->next;
232 return empathy_plist_parse_node (cur_node);
239 * empathy_plist_parse_from_file:
240 * @filename: file containing XML plist data to parse
242 * Parses the XML plist file. If an error occurs during the parsing,
243 * empathy_plist_parse_from_file() will return NULL.
245 * Returns: NULL on error, a newly allocated
246 * #GValue otherwise. Free it using tp_g_value_slice_free()
249 empathy_plist_parse_from_file (const char *filename)
252 xmlNode *root_element = NULL;
255 doc = xmlReadFile (filename, NULL, 0);
261 root_element = xmlDocGetRootElement (doc);
263 parsed_doc = empathy_plist_parse (root_element);
271 * empathy_plist_parse_from_memory:
272 * @data: memory location containing XML plist data to parse
273 * @len: length in bytes of the string to parse
275 * Parses the XML plist file stored in @data which length is @len
276 * bytes. If an error occurs during the parsing,
277 * empathy_plist_parse_from_memory() will return NULL.
279 * Returns: NULL on error, a newly allocated
280 * #GValue otherwise. Free it using tp_g_value_slice_free()
283 empathy_plist_parse_from_memory (const char *data, gsize len)
286 xmlNode *root_element = NULL;
289 doc = xmlReadMemory (data, len, "noname.xml", NULL, 0);
295 root_element = xmlDocGetRootElement (doc);
297 parsed_doc = empathy_plist_parse (root_element);