2 * Copyright (C) 2007 OpenedHand Ltd.
4 * Author: Jorn Baayen <jorn@openedhand.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
25 #include "gvalue-util.h"
27 #include "gupnp-types.h"
30 gvalue_util_set_value_from_string (GValue *value,
33 GValue tmp_value = {0, };
36 g_return_val_if_fail (str != NULL, FALSE);
38 switch (G_VALUE_TYPE (value)) {
40 g_value_set_string (value, str);
45 g_value_set_char (value, *str);
50 g_value_set_uchar (value, *str);
55 g_value_set_int (value, strtol (str, NULL, 10));
60 g_value_set_uint (value, strtoul (str, NULL, 10));
65 g_value_set_int64 (value, g_ascii_strtoll (str, NULL, 10));
70 g_value_set_uint64 (value, g_ascii_strtoull (str, NULL, 10));
75 g_value_set_long (value, strtol (str, NULL, 10));
80 g_value_set_ulong (value, strtoul (str, NULL, 10));
85 g_value_set_float (value, g_ascii_strtod (str, NULL));
90 g_value_set_double (value, g_ascii_strtod (str, NULL));
95 if (g_ascii_strcasecmp (str, "true") == 0 ||
96 g_ascii_strcasecmp (str, "yes") == 0)
97 g_value_set_boolean (value, TRUE);
98 else if (g_ascii_strcasecmp (str, "false") == 0 ||
99 g_ascii_strcasecmp (str, "no") == 0)
100 g_value_set_boolean (value, FALSE);
103 g_value_set_boolean (value, i ? TRUE : FALSE);
110 if (g_value_type_transformable (G_TYPE_STRING,
111 G_VALUE_TYPE (value))) {
112 g_value_init (&tmp_value, G_TYPE_STRING);
113 g_value_set_static_string (&tmp_value, str);
115 g_value_transform (&tmp_value, value);
117 g_value_unset (&tmp_value);
119 } else if (g_value_type_transformable (G_TYPE_INT,
120 G_VALUE_TYPE (value))) {
123 g_value_init (&tmp_value, G_TYPE_INT);
124 g_value_set_int (&tmp_value, i);
126 g_value_transform (&tmp_value, value);
128 g_value_unset (&tmp_value);
131 g_warning ("Failed to transform integer "
133 G_VALUE_TYPE_NAME (value));
145 gvalue_util_set_value_from_xml_node (GValue *value,
151 str = xmlNodeGetContent (node);
153 ret = gvalue_util_set_value_from_string (value, (const char *) str);
161 gvalue_util_value_append_to_xml_string (const GValue *value,
164 GValue transformed_value = {0, };
166 char buf[G_ASCII_DTOSTR_BUF_SIZE];
168 switch (G_VALUE_TYPE (value)) {
170 /* Escape and append */
171 tmp = g_value_get_string (value);
173 xml_util_add_content (str, tmp);
178 g_string_append_c (str, g_value_get_char (value));
183 g_string_append_c (str, g_value_get_uchar (value));
188 g_string_append_printf (str, "%d", g_value_get_int (value));
193 g_string_append_printf (str, "%u", g_value_get_uint (value));
198 g_string_append_printf (str, "%"G_GINT64_FORMAT,
199 g_value_get_int64 (value));
204 g_string_append_printf (str, "%"G_GUINT64_FORMAT,
205 g_value_get_uint64 (value));
210 g_string_append_printf (str, "%ld", g_value_get_long (value));
215 g_string_append_printf (str, "%lu", g_value_get_ulong (value));
220 g_string_append (str,
223 g_value_get_float (value)));
228 g_string_append (str,
231 g_value_get_double (value)));
236 /* We don't want to convert to "TRUE"/"FALSE" */
237 if (g_value_get_boolean (value))
238 g_string_append_c (str, '1');
240 g_string_append_c (str, '0');
246 if (g_value_type_transformable (G_VALUE_TYPE (value),
248 g_value_init (&transformed_value, G_TYPE_STRING);
250 g_value_transform (value, &transformed_value);
252 tmp = g_value_get_string (&transformed_value);
254 xml_util_add_content (str, tmp);
256 g_value_unset (&transformed_value);
260 g_warning ("Failed to transform value of type %s "
261 "to a string", G_VALUE_TYPE_NAME (value));