]> git.sur5r.net Git - glabels/blob - libglabels/xml.c
Reference glabels.org website
[glabels] / libglabels / xml.c
1 /*
2  *  xml.c
3  *  Copyright (C) 2003-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of libglabels.
6  *
7  *  libglabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  libglabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with libglabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "xml.h"
24
25 #include <glib/gi18n.h>
26 #include <glib.h>
27 #include <string.h>
28
29 #include "libglabels-private.h"
30
31
32 /*========================================================*/
33 /* Private macros and constants.                          */
34 /*========================================================*/
35
36 /*========================================================*/
37 /* Private types.                                         */
38 /*========================================================*/
39
40 /*========================================================*/
41 /* Private globals.                                       */
42 /*========================================================*/
43
44 static lglUnits  default_units        = LGL_UNITS_POINT;
45
46
47 /****************************************************************************/
48
49 /**
50  * lgl_xml_get_prop_string:
51  * @node:        the libxml2 #xmlNodePtr of the node
52  * @property:    the property name
53  * @default_val: a default value to return if property not found
54  *
55  * Return value of property as a string.
56  *
57  * Returns: the property as a pointer to a gchar string.  This string should
58  *          be freed with g_free().
59  *
60  */
61 gchar *
62 lgl_xml_get_prop_string (xmlNodePtr   node,
63                          const gchar *property,
64                          const gchar *default_val)
65 {
66         gchar   *val;
67         xmlChar *string;
68
69         string = xmlGetProp (node, (xmlChar *)property);
70         if ( string != NULL ) {
71                 val = g_strdup ((gchar *)string);
72                 xmlFree (string);
73                 return val;
74         }
75
76         if (default_val) {
77                 return g_strdup (default_val);
78         }
79
80         return NULL;
81 }
82
83
84 /**
85  * lgl_xml_get_prop_i18n_string:
86  * @node:        the libxml2 #xmlNodePtr of the node
87  * @property:    the property name
88  * @default_val: a default value to return if property not found
89  *
90  * Return value of a translatable property as a string.
91  *
92  * Returns: the property as a pointer to a gchar string.  This string should
93  *          be freed with g_free().
94  *
95  */
96 gchar *
97 lgl_xml_get_prop_i18n_string (xmlNodePtr   node,
98                               const gchar *property,
99                               const gchar *default_val)
100 {
101         gchar   *_property;
102         gchar   *val;
103         xmlChar *string;
104
105         _property = g_strdup_printf ("_%s", property);
106         string = xmlGetProp (node, (xmlChar *)_property);
107         g_free (_property);
108
109         if ( string != NULL ) {
110
111                 val = g_strdup (gettext ((char *)string));
112                 xmlFree (string);
113                 return val;
114
115         }
116
117         string = xmlGetProp (node, (xmlChar *)property);
118         if ( string != NULL ) {
119                 val = g_strdup ((gchar *)string);
120                 xmlFree (string);
121                 return val;
122         }
123
124         if (default_val) {
125                 return g_strdup (default_val);
126         }
127
128         return NULL;
129 }
130
131
132 /**
133  * lgl_xml_get_prop_double:
134  * @node:        the libxml2 #xmlNodePtr of the node
135  * @property:    the property name
136  * @default_val: a default value to return if property not found
137  *
138  * Return value of property as a double.
139  *
140  * Returns: the property as a double.
141  *
142  */
143 gdouble
144 lgl_xml_get_prop_double (xmlNodePtr   node,
145                          const gchar *property,
146                          gdouble      default_val)
147 {
148         gdouble  val;
149         xmlChar *string;
150
151         string = xmlGetProp (node, (xmlChar *)property);
152         if ( string != NULL ) {
153                 val = g_strtod ((gchar *)string, NULL);
154                 xmlFree (string);
155                 return val;
156         }
157
158         return default_val;
159 }
160
161
162 /**
163  * lgl_xml_get_prop_boolean:
164  * @node:        the libxml2 #xmlNodePtr of the node
165  * @property:    the property name
166  * @default_val: a default value to return if property not found
167  *
168  * Return value of property as a boolean.
169  *
170  * Returns: the property as a boolean.
171  *
172  */
173 gboolean
174 lgl_xml_get_prop_boolean (xmlNodePtr   node,
175                          const gchar *property,
176                          gboolean     default_val)
177 {
178         gboolean  val;
179         xmlChar  *string;
180
181         string = xmlGetProp (node, (xmlChar *)property);
182         if ( string != NULL ) {
183                 val = !((xmlStrcasecmp (string, (xmlChar *)"false") == 0) ||
184                         xmlStrEqual (string, (xmlChar *)"0"));;
185                 xmlFree (string);
186                 return val;
187         }
188
189         return default_val;
190 }
191
192
193 /**
194  * lgl_xml_get_prop_int:
195  * @node:        the libxml2 #xmlNodePtr of the node
196  * @property:    the property name
197  * @default_val: a default value to return if property not found
198  *
199  * Return value of property as an integer.
200  *
201  * Returns: the property as an integer.
202  *
203  */
204 gint
205 lgl_xml_get_prop_int (xmlNodePtr   node,
206                       const gchar *property,
207                       gint         default_val)
208 {
209         gint     val;
210         xmlChar *string;
211
212         string = xmlGetProp (node, (xmlChar *)property);
213         if ( string != NULL ) {
214                 val = strtol ((char *)string, NULL, 0);
215                 xmlFree (string);
216                 return val;
217         }
218
219         return default_val;
220 }
221
222
223 /**
224  * lgl_xml_get_prop_uint:
225  * @node:        the libxml2 #xmlNodePtr of the node
226  * @property:    the property name
227  * @default_val: a default value to return if property not found
228  *
229  * Return value of property (usually formatted in hex) as an unsigned integer.
230  *
231  * Returns: the property as an unsigned integer.
232  *
233  */
234 guint
235 lgl_xml_get_prop_uint (xmlNodePtr   node,
236                        const gchar *property,
237                        guint        default_val)
238 {
239         guint    val;
240         xmlChar *string;
241
242         string = xmlGetProp (node, (xmlChar *)property);
243         if ( string != NULL ) {
244                 val = strtoul ((char *)string, NULL, 0);
245                 xmlFree (string);
246                 return val;
247         }
248
249         return default_val;
250 }
251
252
253 /**
254  * lgl_xml_get_prop_length:
255  * @node:        the libxml2 #xmlNodePtr of the node
256  * @property:    the property name
257  * @default_val: a default value to return if property not found
258  *
259  * Return value of a length property as a double, converting to internal
260  * units (points).  The property is expected to be formatted as a number
261  * followed by a units string.  If there is no units string, the length
262  * is assumed to be in points.  Valid units strings are "pt" for points,
263  * "in" for inches, "mm" for millimeters, "cm" for centimeters, and
264  * "pc" for picas.
265  *
266  * Returns: the length in points.
267  *
268  */
269 gdouble
270 lgl_xml_get_prop_length (xmlNodePtr   node,
271                          const gchar *property,
272                          gdouble      default_val)
273 {
274         gdouble  val;
275         xmlChar *string;
276         xmlChar *unit_id;
277         lglUnits units;
278
279         string = xmlGetProp (node, (xmlChar *)property);
280         if ( string != NULL ) {
281
282                 val = g_strtod ((gchar *)string, (gchar **)&unit_id);
283
284                 if (unit_id != string) {
285                         unit_id = (xmlChar *)g_strchug ((gchar *)unit_id);
286                         units = lgl_units_from_id ((gchar *)unit_id);
287                         if (units != LGL_UNITS_INVALID)
288                         {
289                                 val *= lgl_units_get_points_per_unit (units);
290                         }
291                         else
292                         {
293                                 g_message ("Line %ld, Node \"%s\", Property \"%s\": Unknown unit \"%s\", assuming points",
294                                            xmlGetLineNo (node), node->name, property, unit_id);
295                         }
296                 }
297                 else {
298                         val = 0.0;
299                 }
300
301                 xmlFree (string);
302                 return val;
303         }
304
305         return default_val;
306 }
307
308
309 /**
310  * lgl_xml_set_prop_string:
311  * @node:        the libxml2 #xmlNodePtr of the node
312  * @property:    the property name
313  * @val:         the value to set
314  *
315  * Set a property from a string.
316  *
317  */
318 void
319 lgl_xml_set_prop_string (xmlNodePtr    node,
320                          const gchar  *property,
321                          const gchar  *val)
322 {
323         if (val != NULL) {
324                 xmlSetProp (node, (xmlChar *)property, (xmlChar *)val);
325         }
326 }
327
328
329 /**
330  * lgl_xml_set_prop_double:
331  * @node:        the libxml2 #xmlNodePtr of the node
332  * @property:    the property name
333  * @val:         the value to set
334  *
335  * Set a property from a double.
336  *
337  */
338 void
339 lgl_xml_set_prop_double (xmlNodePtr    node,
340                          const gchar  *property,
341                          gdouble       val)
342 {
343         gchar  *string, buffer[G_ASCII_DTOSTR_BUF_SIZE];
344
345         /* Guarantee "C" locale by use of g_ascii_formatd */
346         string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val);
347
348         xmlSetProp (node, (xmlChar *)property, (xmlChar *)string);
349 }
350
351
352 /**
353  * lgl_xml_set_prop_boolean:
354  * @node:        the libxml2 #xmlNodePtr of the node
355  * @property:    the property name
356  * @val:         the value to set
357  *
358  * Set a property from a boolean.
359  *
360  */
361 void
362 lgl_xml_set_prop_boolean (xmlNodePtr    node,
363                           const gchar  *property,
364                           gboolean      val)
365 {
366         xmlSetProp (node, (xmlChar *)property, (xmlChar *)(val ? "True" : "False"));
367 }
368
369 /**
370  * lgl_xml_set_prop_int:
371  * @node:        the libxml2 #xmlNodePtr of the node
372  * @property:    the property name
373  * @val:         the value to set
374  *
375  * Set a property from an integer.
376  *
377  */
378 void
379 lgl_xml_set_prop_int (xmlNodePtr    node,
380                       const gchar  *property,
381                       gint          val)
382 {
383         gchar  *string;
384
385         string = g_strdup_printf ("%d", val);
386         xmlSetProp (node, (xmlChar *)property, (xmlChar *)string);
387         g_free (string);
388 }
389
390 /**
391  * lgl_xml_set_prop_uint_hex:
392  * @node:        the libxml2 #xmlNodePtr of the node
393  * @property:    the property name
394  * @val:         the value to set
395  *
396  * Set a property from an unsigned integer and format in hex.
397  *
398  */
399 void
400 lgl_xml_set_prop_uint_hex (xmlNodePtr    node,
401                            const gchar  *property,
402                            guint         val)
403 {
404         gchar  *string;
405
406         string = g_strdup_printf ("0x%08x", val);
407         xmlSetProp (node, (xmlChar *)property, (xmlChar *)string);
408         g_free (string);
409 }
410
411 /**
412  * lgl_xml_set_prop_length:
413  * @node:        the libxml2 #xmlNodePtr of the node
414  * @property:    the property name
415  * @val:         the length to set in internal units (points)
416  *
417  * Set a property from a length, performing any necessary conversion.
418  * Length properties are formatted as a number followed by a units string.
419  * The units of the formatted property is determined by the most recent call to
420  * lgl_xml_set_default_units().
421  *
422  */
423 void
424 lgl_xml_set_prop_length (xmlNodePtr    node,
425                          const gchar  *property,
426                          gdouble       val)
427 {
428         gchar  *string, buffer[G_ASCII_DTOSTR_BUF_SIZE];
429         gchar  *string_unit;
430
431         /* Convert to default units */
432         val *= lgl_units_get_units_per_point (default_units);
433
434         /* Guarantee "C" locale by use of g_ascii_formatd */
435         string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val);
436
437         string_unit = g_strdup_printf ("%s%s", string, lgl_units_get_id (default_units));
438         xmlSetProp (node, (xmlChar *)property, (xmlChar *)string_unit);
439         g_free (string_unit);
440 }
441
442 /**
443  * lgl_xml_is_node
444  * @node:        the libxml2 #xmlNodePtr of the node
445  * @name    :    the node name
446  *
447  * Test if a node name matches given name.
448  *
449  * Returns: TRUE if the name of the node matches.  Otherwise FALSE.
450  *
451  */
452 gboolean
453 lgl_xml_is_node (xmlNodePtr   node,
454                  const gchar *name)
455 {
456         return xmlStrEqual (node->name, (xmlChar *)name);
457 }
458
459
460 /**
461  * lgl_xml_get_node_content
462  * @node:        the libxml2 #xmlNodePtr of the node
463  *
464  * Get the content of a node.
465  *
466  * Returns: the property as a pointer to a gchar string.  This string should
467  *          be freed with g_free().
468  */
469 gchar *
470 lgl_xml_get_node_content (xmlNodePtr   node)
471 {
472         xmlChar *xml_content;
473         gchar   *g_content;
474
475         xml_content = xmlNodeGetContent (node);
476
477         if (xml_content != NULL) {
478
479                 g_content = g_strdup ((gchar *)xml_content);
480                 xmlFree (xml_content);
481                 return g_content;
482
483         }
484
485         return NULL;
486 }
487
488
489 /**
490  * lgl_xml_set_default_units:
491  * @units:       default units selection (#lglUnits)
492  *
493  * Set the default units when formatting lengths.  See
494  * lgl_xml_set_prop_length().
495  *
496  */
497 void
498 lgl_xml_set_default_units (lglUnits   units)
499 {
500         g_return_if_fail ((units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST));
501
502         default_units = units;
503 }
504
505
506
507 /*
508  * Local Variables:       -- emacs
509  * mode: C                -- emacs
510  * c-basic-offset: 8      -- emacs
511  * tab-width: 8           -- emacs
512  * indent-tabs-mode: nil  -- emacs
513  * End:                   -- emacs
514  */