rename accountopt.[ch] to purpleaccountoption.[ch]
[pidgin-git.git] / libpurple / protocols / gg / xml.c
blobef10dca776ca426f8a6bca05ef77ddb401397c60
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * Rewritten from scratch during Google Summer of Code 2012
8 * by Tomek Wasilczyk (http://www.wasilczyk.pl).
10 * Previously implemented by:
11 * - Arkadiusz Miskiewicz <misiek@pld.org.pl> - first implementation (2001);
12 * - Bartosz Oler <bartosz@bzimage.us> - reimplemented during GSoC 2005;
13 * - Krzysztof Klinikowski <grommasher@gmail.com> - some parts (2009-2011).
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
30 #include "xml.h"
32 #include "debug.h"
34 gboolean ggp_xml_get_string(const PurpleXmlNode *xml, gchar *childName, gchar **var)
36 char *str;
38 g_return_val_if_fail(xml != NULL, FALSE);
39 g_return_val_if_fail(var != NULL, FALSE);
41 if (childName != NULL) {
42 xml = purple_xmlnode_get_child(xml, childName);
43 if (xml == NULL)
44 return FALSE;
47 str = purple_xmlnode_get_data(xml);
48 if (str == NULL)
49 return FALSE;
51 *var = str;
52 return TRUE;
55 gboolean ggp_xml_get_bool(const PurpleXmlNode *xml, gchar *childName, gboolean *var)
57 char *str;
58 gboolean succ;
60 succ = ggp_xml_get_string(xml, childName, &str);
61 if (!succ)
62 return FALSE;
64 *var = (strcmp(str, "true") == 0 ||
65 strcmp(str, "True") == 0 ||
66 strcmp(str, "TRUE") == 0 ||
67 strcmp(str, "1") == 0);
68 g_free(str);
70 return TRUE;
73 gboolean ggp_xml_get_uint(const PurpleXmlNode *xml, gchar *childName, unsigned int *var)
75 char *str, *endptr;
76 gboolean succ;
77 unsigned int val;
79 succ = ggp_xml_get_string(xml, childName, &str);
80 if (!succ)
81 return FALSE;
83 errno = 0;
84 val = strtoul(str, &endptr, 10);
86 succ = (errno != ERANGE && endptr[0] == '\0');
87 g_free(str);
89 if (succ)
90 *var = val;
91 return succ;
94 gboolean ggp_xml_set_string(PurpleXmlNode *xml, gchar *childName, const gchar *val)
96 g_return_val_if_fail(xml != NULL, FALSE);
97 g_return_val_if_fail(val != NULL, FALSE);
99 if (childName != NULL) {
100 PurpleXmlNode *child = purple_xmlnode_get_child(xml, childName);
101 if (child == NULL)
102 child = purple_xmlnode_new_child(xml, childName);
103 xml = child;
106 ggp_xmlnode_remove_children(xml);
107 purple_xmlnode_insert_data(xml, val, -1);
109 return TRUE;
112 gboolean ggp_xml_set_bool(PurpleXmlNode *xml, gchar *childName, gboolean val)
114 return ggp_xml_set_string(xml, childName, val ? "true" : "false");
117 gboolean ggp_xml_set_uint(PurpleXmlNode *xml, gchar *childName, unsigned int val)
119 gchar buff[20];
120 g_snprintf(buff, sizeof(buff), "%u", val);
121 return ggp_xml_set_string(xml, childName, buff);
124 void ggp_xmlnode_remove_children(PurpleXmlNode *xml)
126 PurpleXmlNode *child = xml->child;
127 while (child) {
128 PurpleXmlNode *next = child->next;
129 if (child->type != PURPLE_XMLNODE_TYPE_ATTRIB)
130 purple_xmlnode_free(child);
131 child = next;
135 unsigned int ggp_xml_child_count(PurpleXmlNode *xml, const gchar *childName)
137 PurpleXmlNode *child;
138 unsigned int count = 0;
140 g_return_val_if_fail(xml != NULL, 0);
142 if (childName) {
143 child = purple_xmlnode_get_child(xml, childName);
144 while (child) {
145 child = purple_xmlnode_get_next_twin(child);
146 count++;
148 } else {
149 child = xml->child;
150 while (child) {
151 child = child->next;
152 count++;
156 return count;