3 * Copyright (C) 2003-2010 Jim Evins <evins@snaught.com>.
5 * This file is part of libglabels.
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.
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.
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/>.
25 #include <glib/gi18n.h>
29 #include "libglabels-private.h"
32 /*========================================================*/
33 /* Private macros and constants. */
34 /*========================================================*/
36 /*========================================================*/
38 /*========================================================*/
40 /*========================================================*/
41 /* Private globals. */
42 /*========================================================*/
44 static lglUnits default_units = LGL_UNITS_POINT;
47 /****************************************************************************/
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
55 * Return value of property as a string.
57 * Returns: the property as a pointer to a gchar string. This string should
58 * be freed with g_free().
62 lgl_xml_get_prop_string (xmlNodePtr node,
63 const gchar *property,
64 const gchar *default_val)
69 string = xmlGetProp (node, (xmlChar *)property);
72 val = g_strdup ((gchar *)string);
79 return g_strdup (default_val);
87 * lgl_xml_get_prop_i18n_string:
88 * @node: the libxml2 #xmlNodePtr of the node
89 * @property: the property name
90 * @default_val: a default value to return if property not found
92 * Return value of a translatable property as a string.
94 * Returns: the property as a pointer to a gchar string. This string should
95 * be freed with g_free().
99 lgl_xml_get_prop_i18n_string (xmlNodePtr node,
100 const gchar *property,
101 const gchar *default_val)
107 _property = g_strdup_printf ("_%s", property);
108 string = xmlGetProp (node, (xmlChar *)_property);
111 if ( string != NULL )
113 val = g_strdup (gettext ((char *)string));
118 string = xmlGetProp (node, (xmlChar *)property);
119 if ( string != NULL )
121 val = g_strdup ((gchar *)string);
128 return g_strdup (default_val);
136 * lgl_xml_get_prop_double:
137 * @node: the libxml2 #xmlNodePtr of the node
138 * @property: the property name
139 * @default_val: a default value to return if property not found
141 * Return value of property as a double.
143 * Returns: the property as a double.
147 lgl_xml_get_prop_double (xmlNodePtr node,
148 const gchar *property,
154 string = xmlGetProp (node, (xmlChar *)property);
155 if ( string != NULL )
157 val = g_strtod ((gchar *)string, NULL);
167 * lgl_xml_get_prop_boolean:
168 * @node: the libxml2 #xmlNodePtr of the node
169 * @property: the property name
170 * @default_val: a default value to return if property not found
172 * Return value of property as a boolean.
174 * Returns: the property as a boolean.
178 lgl_xml_get_prop_boolean (xmlNodePtr node,
179 const gchar *property,
180 gboolean default_val)
185 string = xmlGetProp (node, (xmlChar *)property);
186 if ( string != NULL )
188 val = !((xmlStrcasecmp (string, (xmlChar *)"false") == 0) ||
189 xmlStrEqual (string, (xmlChar *)"0"));;
199 * lgl_xml_get_prop_int:
200 * @node: the libxml2 #xmlNodePtr of the node
201 * @property: the property name
202 * @default_val: a default value to return if property not found
204 * Return value of property as an integer.
206 * Returns: the property as an integer.
210 lgl_xml_get_prop_int (xmlNodePtr node,
211 const gchar *property,
217 string = xmlGetProp (node, (xmlChar *)property);
218 if ( string != NULL )
220 val = strtol ((char *)string, NULL, 0);
230 * lgl_xml_get_prop_uint:
231 * @node: the libxml2 #xmlNodePtr of the node
232 * @property: the property name
233 * @default_val: a default value to return if property not found
235 * Return value of property (usually formatted in hex) as an unsigned integer.
237 * Returns: the property as an unsigned integer.
241 lgl_xml_get_prop_uint (xmlNodePtr node,
242 const gchar *property,
248 string = xmlGetProp (node, (xmlChar *)property);
249 if ( string != NULL )
251 val = strtoul ((char *)string, NULL, 0);
261 * lgl_xml_get_prop_length:
262 * @node: the libxml2 #xmlNodePtr of the node
263 * @property: the property name
264 * @default_val: a default value to return if property not found
266 * Return value of a length property as a double, converting to internal
267 * units (points). The property is expected to be formatted as a number
268 * followed by a units string. If there is no units string, the length
269 * is assumed to be in points. Valid units strings are "pt" for points,
270 * "in" for inches, "mm" for millimeters, "cm" for centimeters, and
273 * Returns: the length in points.
277 lgl_xml_get_prop_length (xmlNodePtr node,
278 const gchar *property,
286 string = xmlGetProp (node, (xmlChar *)property);
287 if ( string != NULL )
290 val = g_strtod ((gchar *)string, (gchar **)&unit_id);
292 if (unit_id != string)
294 unit_id = (xmlChar *)g_strchug ((gchar *)unit_id);
295 units = lgl_units_from_id ((gchar *)unit_id);
296 if (units != LGL_UNITS_INVALID)
298 val *= lgl_units_get_points_per_unit (units);
302 g_message ("Line %ld, Node \"%s\", Property \"%s\": Unknown unit \"%s\", assuming points",
303 xmlGetLineNo (node), node->name, property, unit_id);
320 * lgl_xml_set_prop_string:
321 * @node: the libxml2 #xmlNodePtr of the node
322 * @property: the property name
323 * @val: the value to set
325 * Set a property from a string.
329 lgl_xml_set_prop_string (xmlNodePtr node,
330 const gchar *property,
335 xmlSetProp (node, (xmlChar *)property, (xmlChar *)val);
341 * lgl_xml_set_prop_double:
342 * @node: the libxml2 #xmlNodePtr of the node
343 * @property: the property name
344 * @val: the value to set
346 * Set a property from a double.
350 lgl_xml_set_prop_double (xmlNodePtr node,
351 const gchar *property,
354 gchar *string, buffer[G_ASCII_DTOSTR_BUF_SIZE];
356 /* Guarantee "C" locale by use of g_ascii_formatd */
357 string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val);
359 xmlSetProp (node, (xmlChar *)property, (xmlChar *)string);
364 * lgl_xml_set_prop_boolean:
365 * @node: the libxml2 #xmlNodePtr of the node
366 * @property: the property name
367 * @val: the value to set
369 * Set a property from a boolean.
373 lgl_xml_set_prop_boolean (xmlNodePtr node,
374 const gchar *property,
377 xmlSetProp (node, (xmlChar *)property, (xmlChar *)(val ? "True" : "False"));
381 * lgl_xml_set_prop_int:
382 * @node: the libxml2 #xmlNodePtr of the node
383 * @property: the property name
384 * @val: the value to set
386 * Set a property from an integer.
390 lgl_xml_set_prop_int (xmlNodePtr node,
391 const gchar *property,
396 string = g_strdup_printf ("%d", val);
397 xmlSetProp (node, (xmlChar *)property, (xmlChar *)string);
402 * lgl_xml_set_prop_uint_hex:
403 * @node: the libxml2 #xmlNodePtr of the node
404 * @property: the property name
405 * @val: the value to set
407 * Set a property from an unsigned integer and format in hex.
411 lgl_xml_set_prop_uint_hex (xmlNodePtr node,
412 const gchar *property,
417 string = g_strdup_printf ("0x%08x", val);
418 xmlSetProp (node, (xmlChar *)property, (xmlChar *)string);
423 * lgl_xml_set_prop_length:
424 * @node: the libxml2 #xmlNodePtr of the node
425 * @property: the property name
426 * @val: the length to set in internal units (points)
428 * Set a property from a length, performing any necessary conversion.
429 * Length properties are formatted as a number followed by a units string.
430 * The units of the formatted property is determined by the most recent call to
431 * lgl_xml_set_default_units().
435 lgl_xml_set_prop_length (xmlNodePtr node,
436 const gchar *property,
439 gchar *string, buffer[G_ASCII_DTOSTR_BUF_SIZE];
442 /* Convert to default units */
443 val *= lgl_units_get_units_per_point (default_units);
445 /* Guarantee "C" locale by use of g_ascii_formatd */
446 string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val);
448 string_unit = g_strdup_printf ("%s%s", string, lgl_units_get_id (default_units));
449 xmlSetProp (node, (xmlChar *)property, (xmlChar *)string_unit);
450 g_free (string_unit);
455 * @node: the libxml2 #xmlNodePtr of the node
456 * @name : the node name
458 * Test if a node name matches given name.
460 * Returns: TRUE if the name of the node matches. Otherwise FALSE.
464 lgl_xml_is_node (xmlNodePtr node,
467 return xmlStrEqual (node->name, (xmlChar *)name);
472 * lgl_xml_get_node_content
473 * @node: the libxml2 #xmlNodePtr of the node
475 * Get the content of a node.
477 * Returns: the property as a pointer to a gchar string. This string should
478 * be freed with g_free().
481 lgl_xml_get_node_content (xmlNodePtr node)
483 xmlChar *xml_content;
486 xml_content = xmlNodeGetContent (node);
488 if (xml_content != NULL)
490 g_content = g_strdup ((gchar *)xml_content);
491 xmlFree (xml_content);
500 * lgl_xml_set_default_units:
501 * @units: default units selection (#lglUnits)
503 * Set the default units when formatting lengths. See
504 * lgl_xml_set_prop_length().
508 lgl_xml_set_default_units (lglUnits units)
510 g_return_if_fail ((units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST));
512 default_units = units;
518 * Local Variables: -- emacs
520 * c-basic-offset: 8 -- emacs
521 * tab-width: 8 -- emacs
522 * indent-tabs-mode: nil -- emacs