3 * Copyright (C) 2003-2009 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);
70 if ( string != NULL ) {
71 val = g_strdup ((gchar *)string);
77 return g_strdup (default_val);
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
90 * Return value of a translatable property as a string.
92 * Returns: the property as a pointer to a gchar string. This string should
93 * be freed with g_free().
97 lgl_xml_get_prop_i18n_string (xmlNodePtr node,
98 const gchar *property,
99 const gchar *default_val)
105 _property = g_strdup_printf ("_%s", property);
106 string = xmlGetProp (node, (xmlChar *)_property);
109 if ( string != NULL ) {
111 val = g_strdup (gettext ((char *)string));
117 string = xmlGetProp (node, (xmlChar *)property);
118 if ( string != NULL ) {
119 val = g_strdup ((gchar *)string);
125 return g_strdup (default_val);
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
138 * Return value of property as a double.
140 * Returns: the property as a double.
144 lgl_xml_get_prop_double (xmlNodePtr node,
145 const gchar *property,
151 string = xmlGetProp (node, (xmlChar *)property);
152 if ( string != NULL ) {
153 val = g_strtod ((gchar *)string, NULL);
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
168 * Return value of property as a boolean.
170 * Returns: the property as a boolean.
174 lgl_xml_get_prop_boolean (xmlNodePtr node,
175 const gchar *property,
176 gboolean default_val)
181 string = xmlGetProp (node, (xmlChar *)property);
182 if ( string != NULL ) {
183 val = !((xmlStrcasecmp (string, (xmlChar *)"false") == 0) ||
184 xmlStrEqual (string, (xmlChar *)"0"));;
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
199 * Return value of property as an integer.
201 * Returns: the property as an integer.
205 lgl_xml_get_prop_int (xmlNodePtr node,
206 const gchar *property,
212 string = xmlGetProp (node, (xmlChar *)property);
213 if ( string != NULL ) {
214 val = strtol ((char *)string, NULL, 0);
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
229 * Return value of property (usually formatted in hex) as an unsigned integer.
231 * Returns: the property as an unsigned integer.
235 lgl_xml_get_prop_uint (xmlNodePtr node,
236 const gchar *property,
242 string = xmlGetProp (node, (xmlChar *)property);
243 if ( string != NULL ) {
244 val = strtoul ((char *)string, NULL, 0);
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
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
266 * Returns: the length in points.
270 lgl_xml_get_prop_length (xmlNodePtr node,
271 const gchar *property,
279 string = xmlGetProp (node, (xmlChar *)property);
280 if ( string != NULL ) {
282 val = g_strtod ((gchar *)string, (gchar **)&unit_id);
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)
289 val *= lgl_units_get_points_per_unit (units);
293 g_message ("Line %ld, Node \"%s\", Property \"%s\": Unknown unit \"%s\", assuming points",
294 xmlGetLineNo (node), node->name, property, unit_id);
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
315 * Set a property from a string.
319 lgl_xml_set_prop_string (xmlNodePtr node,
320 const gchar *property,
324 xmlSetProp (node, (xmlChar *)property, (xmlChar *)val);
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
335 * Set a property from a double.
339 lgl_xml_set_prop_double (xmlNodePtr node,
340 const gchar *property,
343 gchar *string, buffer[G_ASCII_DTOSTR_BUF_SIZE];
345 /* Guarantee "C" locale by use of g_ascii_formatd */
346 string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val);
348 xmlSetProp (node, (xmlChar *)property, (xmlChar *)string);
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
358 * Set a property from a boolean.
362 lgl_xml_set_prop_boolean (xmlNodePtr node,
363 const gchar *property,
366 xmlSetProp (node, (xmlChar *)property, (xmlChar *)(val ? "True" : "False"));
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
375 * Set a property from an integer.
379 lgl_xml_set_prop_int (xmlNodePtr node,
380 const gchar *property,
385 string = g_strdup_printf ("%d", val);
386 xmlSetProp (node, (xmlChar *)property, (xmlChar *)string);
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
396 * Set a property from an unsigned integer and format in hex.
400 lgl_xml_set_prop_uint_hex (xmlNodePtr node,
401 const gchar *property,
406 string = g_strdup_printf ("0x%08x", val);
407 xmlSetProp (node, (xmlChar *)property, (xmlChar *)string);
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)
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().
424 lgl_xml_set_prop_length (xmlNodePtr node,
425 const gchar *property,
428 gchar *string, buffer[G_ASCII_DTOSTR_BUF_SIZE];
431 /* Convert to default units */
432 val *= lgl_units_get_units_per_point (default_units);
434 /* Guarantee "C" locale by use of g_ascii_formatd */
435 string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val);
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);
444 * @node: the libxml2 #xmlNodePtr of the node
445 * @name : the node name
447 * Test if a node name matches given name.
449 * Returns: TRUE if the name of the node matches. Otherwise FALSE.
453 lgl_xml_is_node (xmlNodePtr node,
456 return xmlStrEqual (node->name, (xmlChar *)name);
461 * lgl_xml_get_node_content
462 * @node: the libxml2 #xmlNodePtr of the node
464 * Get the content of a node.
466 * Returns: the property as a pointer to a gchar string. This string should
467 * be freed with g_free().
470 lgl_xml_get_node_content (xmlNodePtr node)
472 xmlChar *xml_content;
475 xml_content = xmlNodeGetContent (node);
477 if (xml_content != NULL) {
479 g_content = g_strdup ((gchar *)xml_content);
480 xmlFree (xml_content);
490 * lgl_xml_set_default_units:
491 * @units: default units selection (#lglUnits)
493 * Set the default units when formatting lengths. See
494 * lgl_xml_set_prop_length().
498 lgl_xml_set_default_units (lglUnits units)
500 g_return_if_fail ((units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST));
502 default_units = units;
508 * Local Variables: -- emacs
510 * c-basic-offset: 8 -- emacs
511 * tab-width: 8 -- emacs
512 * indent-tabs-mode: nil -- emacs