Return NULL in case of error.
[mmediamanager.git] / src / mm-string-utils.c
blobd48bc1d210b49cbef1f817e56916cb60b28df05f
1 /* MManager - a Desktop wide manager for multimedia applications.
3 * Copyright (C) 2008 Cosimo Cecchi <cosimoc@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include <glib.h>
22 #include <libxml/tree.h>
23 #include <libxml/xmlwriter.h>
24 #include "mm-string-utils.h"
26 typedef struct {
27 MMComparisionOperator op;
28 const char *string;
29 } OperatorGrid;
31 static OperatorGrid operator_grid[] =
33 { MM_COMP_EQUAL, "EQ" },
34 { MM_COMP_GREATER, "GR" },
35 { MM_COMP_GREATER_EQUAL, "GRQ" },
36 { MM_COMP_LESS, "LS" },
37 { MM_COMP_LESS_EQUAL, "LSQ" },
38 { MM_COMP_NONE, "" }
41 static xmlChar *
42 value_to_string (GValue *v)
44 xmlChar *safe;
45 char *ret = NULL;
46 GType type = G_VALUE_TYPE (v);
48 /* guess the most used cases and handle those first */
49 if (type == G_TYPE_STRING) {
50 ret = g_strdup (g_value_get_string (v));
51 } else if (type == G_TYPE_BOOLEAN) {
52 ret = g_strdup_printf ("%d", g_value_get_boolean (v));
53 } else if (type == G_TYPE_INT) {
54 ret = g_strdup_printf ("%d", g_value_get_int (v));
55 } else if (type == G_TYPE_FLOAT) {
56 ret = g_strdup_printf ("%f", g_value_get_float (v));
57 } else if (type == G_TYPE_DOUBLE) {
58 char double_buff[G_ASCII_DTOSTR_BUF_SIZE];
59 ret = g_ascii_dtostr (double_buff, sizeof (double_buff),
60 g_value_get_double (v));
61 } else if (type == G_TYPE_LONG) {
62 ret = g_strdup_printf ("%ld", g_value_get_long (v));
63 } else if (type == G_TYPE_INT64) {
64 ret = g_strdup_printf ("%lld", g_value_get_int64 (v));
65 } else if (type == G_TYPE_UINT) {
66 ret = g_strdup_printf ("%u", g_value_get_uint (v));
67 } else if (type == G_TYPE_ULONG) {
68 ret = g_strdup_printf ("%lu", g_value_get_ulong (v));
69 } else if (type == G_TYPE_UINT64) {
70 ret = g_strdup_printf ("%llu", g_value_get_uint64 (v));
71 } else if (G_VALUE_HOLDS_CHAR (v)) {
72 ret = g_strdup_printf ("%c", g_value_get_char (v));
73 } else if (G_VALUE_HOLDS_UCHAR (v)) {
74 ret = g_strdup_printf ("%c", g_value_get_uchar (v));
75 } else {
76 g_warning ("Can't convert the value to string: unhandled type");
77 return NULL;
80 safe = xmlCharStrdup (ret);
81 g_free (ret);
82 return safe;
85 static xmlChar *
86 op_to_string (MMComparisionOperator op)
88 int idx;
90 for (idx = 0; idx < G_N_ELEMENTS (operator_grid); idx++) {
91 if (op == operator_grid[idx].op) {
92 return xmlCharStrdup (operator_grid[idx].string);
96 return NULL;
99 static void
100 add_filter_param_to_xml (MMFilterParam *fp,
101 xmlTextWriterPtr writer)
103 MMAttribute *attribute;
104 xmlChar *safe_str;
106 attribute = mm_filter_param_get_attribute (fp);
108 xmlTextWriterStartElement (writer, BAD_CAST ("filter-param"));
110 xmlTextWriterStartElement (writer, BAD_CAST ("attribute"));
111 safe_str = xmlCharStrdup (mm_attribute_get_id (attribute));
112 xmlTextWriterWriteAttribute (writer, BAD_CAST ("id"), safe_str);
113 g_free (safe_str);
114 safe_str = xmlCharStrdup (mm_attribute_get_name (attribute));
115 xmlTextWriterWriteAttribute (writer, BAD_CAST ("name"), safe_str);
116 g_free (safe_str);
117 safe_str = xmlCharStrdup (mm_attribute_get_description (attribute));
118 xmlTextWriterWriteAttribute (writer, BAD_CAST ("description"), safe_str);
119 g_free (safe_str);
120 xmlTextWriterWriteAttribute (writer, BAD_CAST ("type"), BAD_CAST (g_type_name (mm_attribute_get_value_type (attribute))));
121 /* close "attribute" */
122 xmlTextWriterEndElement (writer);
124 safe_str = value_to_string (mm_filter_param_get_value (fp));
125 xmlTextWriterWriteElement (writer, BAD_CAST ("value"), safe_str);
126 g_free (safe_str);
128 safe_str = op_to_string (mm_filter_param_get_operator (fp));
129 xmlTextWriterWriteElement (writer, BAD_CAST ("op"), safe_str);
130 g_free (safe_str);
132 /* close "filter-param" */
133 xmlTextWriterEndElement (writer);
136 /* public functions */
137 char *
138 mm_filter_serialize (MMFilter *filter)
140 char *serialized;
141 xmlBufferPtr buffer;
142 xmlTextWriterPtr writer;
143 GList *filter_params;
145 buffer = xmlBufferCreate ();
146 writer = xmlNewTextWriterMemory (buffer, 0);
148 xmlTextWriterStartDocument (writer, NULL, NULL, NULL);
150 xmlTextWriterStartElement (writer, BAD_CAST ("filter"));
151 filter_params = mm_filter_get_filtering_params (filter);
152 g_list_foreach (filter_params, (GFunc) add_filter_param_to_xml, writer);
153 /* close "filter" */
154 xmlTextWriterEndElement (writer);
156 xmlTextWriterEndDocument (writer);
158 xmlFreeTextWriter (writer);
159 serialized = g_strdup ((char *) xmlBufferContent (buffer));
160 xmlBufferFree (buffer);
162 return serialized;
165 MMFilter *
166 mm_filter_unserialize (const char *s)
168 return NULL;