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., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, 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 #if GLIB_CHECK_VERSION(2,32,0)
46 g_value_set_schar (value, *str);
48 g_value_set_char (value, *str);
54 g_value_set_uchar (value, *str);
59 g_value_set_int (value, strtol (str, NULL, 10));
64 g_value_set_uint (value, strtoul (str, NULL, 10));
69 g_value_set_int64 (value, g_ascii_strtoll (str, NULL, 10));
74 g_value_set_uint64 (value, g_ascii_strtoull (str, NULL, 10));
79 g_value_set_long (value, strtol (str, NULL, 10));
84 g_value_set_ulong (value, strtoul (str, NULL, 10));
89 g_value_set_float (value, g_ascii_strtod (str, NULL));
94 g_value_set_double (value, g_ascii_strtod (str, NULL));
99 if (g_ascii_strcasecmp (str, "true") == 0 ||
100 g_ascii_strcasecmp (str, "yes") == 0)
101 g_value_set_boolean (value, TRUE);
102 else if (g_ascii_strcasecmp (str, "false") == 0 ||
103 g_ascii_strcasecmp (str, "no") == 0)
104 g_value_set_boolean (value, FALSE);
107 g_value_set_boolean (value, i ? TRUE : FALSE);
114 if (g_value_type_transformable (G_TYPE_STRING,
115 G_VALUE_TYPE (value))) {
116 g_value_init (&tmp_value, G_TYPE_STRING);
117 g_value_set_static_string (&tmp_value, str);
119 g_value_transform (&tmp_value, value);
121 g_value_unset (&tmp_value);
123 } else if (g_value_type_transformable (G_TYPE_INT,
124 G_VALUE_TYPE (value))) {
127 g_value_init (&tmp_value, G_TYPE_INT);
128 g_value_set_int (&tmp_value, i);
130 g_value_transform (&tmp_value, value);
132 g_value_unset (&tmp_value);
135 g_warning ("Failed to transform integer "
137 G_VALUE_TYPE_NAME (value));
149 gvalue_util_set_value_from_xml_node (GValue *value,
155 str = xmlNodeGetContent (node);
157 ret = gvalue_util_set_value_from_string (value, (const char *) str);
165 gvalue_util_value_append_to_xml_string (const GValue *value,
168 GValue transformed_value = {0, };
170 char buf[G_ASCII_DTOSTR_BUF_SIZE];
172 switch (G_VALUE_TYPE (value)) {
174 /* Escape and append */
175 tmp = g_value_get_string (value);
177 xml_util_add_content (str, tmp);
182 #if GLIB_CHECK_VERSION(2,32,0)
183 g_string_append_c (str, g_value_get_schar (value));
185 g_string_append_c (str, g_value_get_char (value));
191 g_string_append_c (str, g_value_get_uchar (value));
196 g_string_append_printf (str, "%d", g_value_get_int (value));
201 g_string_append_printf (str, "%u", g_value_get_uint (value));
206 g_string_append_printf (str, "%"G_GINT64_FORMAT,
207 g_value_get_int64 (value));
212 g_string_append_printf (str, "%"G_GUINT64_FORMAT,
213 g_value_get_uint64 (value));
218 g_string_append_printf (str, "%ld", g_value_get_long (value));
223 g_string_append_printf (str, "%lu", g_value_get_ulong (value));
228 g_string_append (str,
231 g_value_get_float (value)));
236 g_string_append (str,
239 g_value_get_double (value)));
244 /* We don't want to convert to "TRUE"/"FALSE" */
245 if (g_value_get_boolean (value))
246 g_string_append_c (str, '1');
248 g_string_append_c (str, '0');
254 if (g_value_type_transformable (G_VALUE_TYPE (value),
256 g_value_init (&transformed_value, G_TYPE_STRING);
258 g_value_transform (value, &transformed_value);
260 tmp = g_value_get_string (&transformed_value);
262 xml_util_add_content (str, tmp);
264 g_value_unset (&transformed_value);
268 g_warning ("Failed to transform value of type %s "
269 "to a string", G_VALUE_TYPE_NAME (value));