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