]> git.sur5r.net Git - glabels/blob - glabels2/libglabels/xml.c
2004-01-06 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / 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
26 #include "libglabels-private.h"
27
28 #include "xml.h"
29
30 /*========================================================*/
31 /* Private macros and constants.                          */
32 /*========================================================*/
33
34 #define POINTS_PER_POINT    1.0 /* internal units are points. */
35 #define POINTS_PER_INCH    72.0
36 #define POINTS_PER_MM       2.83464566929
37 #define POINTS_PER_CM       (10.0*POINTS_PER_MM)
38 #define POINTS_PER_PICA     (1.0/12.0)
39
40 /*========================================================*/
41 /* Private types.                                         */
42 /*========================================================*/
43
44 typedef struct {
45         gchar  *name;
46         gdouble points_per_unit;
47 } UnitTableEntry;
48
49 /*========================================================*/
50 /* Private globals.                                       */
51 /*========================================================*/
52
53 static UnitTableEntry unit_table[] = {
54
55         /* These names are identical to the absolute length units supported in
56            the CSS2 Specification (Section 4.3.2) */
57
58         {"pt",           POINTS_PER_POINT},
59         {"in",           POINTS_PER_INCH},
60         {"mm",           POINTS_PER_MM},
61         {"cm",           POINTS_PER_CM},
62         {"pc",           POINTS_PER_PICA},
63
64         {NULL, 0}
65 };
66
67 /*========================================================*/
68 /* Private function prototypes.                           */
69 /*========================================================*/
70
71 \f
72 /****************************************************************************/
73 /* Return value of property as a double.                                    */
74 /****************************************************************************/
75 gdouble
76 gl_xml_get_prop_double (xmlNodePtr   node,
77                         const gchar *property,
78                         gdouble      default_val)
79 {
80         gdouble  val;
81         gchar   *string;
82
83         string = xmlGetProp (node, property);
84         if ( string != NULL ) {
85                 val = g_strtod (string, NULL);
86                 g_free (string);
87                 return val;
88         }
89
90         return default_val;
91 }
92
93 /****************************************************************************/
94 /* Return value of property as a boolean.                                   */
95 /****************************************************************************/
96 gboolean
97 gl_xml_get_prop_boolean (xmlNodePtr   node,
98                          const gchar *property,
99                          gboolean     default_val)
100 {
101         gboolean  val;
102         gchar    *string;
103
104         string = xmlGetProp (node, property);
105         if ( string != NULL ) {
106                 val = !((xmlStrcasecmp (string, "false") == 0) ||
107                         xmlStrEqual (string, "0"));;
108                 g_free (string);
109                 return val;
110         }
111
112         return default_val;
113 }
114
115
116 /****************************************************************************/
117 /* Return value of property as an int. .                                    */
118 /****************************************************************************/
119 gint
120 gl_xml_get_prop_int (xmlNodePtr   node,
121                      const gchar *property,
122                      gint         default_val)
123 {
124         gint     val;
125         gchar   *string;
126
127         string = xmlGetProp (node, property);
128         if ( string != NULL ) {
129                 val = strtol (string, NULL, 0);
130                 g_free (string);
131                 return val;
132         }
133
134         return default_val;
135 }
136
137
138 /****************************************************************************/
139 /* Return value of hex property as an unsigned int.                         */
140 /****************************************************************************/
141 guint
142 gl_xml_get_prop_uint (xmlNodePtr   node,
143                       const gchar *property,
144                       guint        default_val)
145 {
146         guint    val;
147         gchar   *string;
148
149         string = xmlGetProp (node, property);
150         if ( string != NULL ) {
151                 val = strtoul (string, NULL, 0);
152                 g_free (string);
153                 return val;
154         }
155
156         return default_val;
157 }
158
159
160 /****************************************************************************/
161 /* Return value of length property as a double, converting to internal units*/
162 /****************************************************************************/
163 gdouble
164 gl_xml_get_prop_length (xmlNodePtr   node,
165                         const gchar *property,
166                         gdouble      default_val)
167 {
168         gdouble  val;
169         gchar   *string, *unit;
170         gint     i;
171
172         string = xmlGetProp (node, property);
173         if ( string != NULL ) {
174
175                 val = g_strtod (string, &unit);
176
177                 if (unit != string) {
178                         unit = g_strchug (unit);
179                         if (strlen (unit) > 0 ) {
180                                 for (i=0; unit_table[i].name != NULL; i++) {
181                                         if (xmlStrcasecmp (unit, unit_table[i].name) == 0) {
182                                                 val *= unit_table[i].points_per_unit;
183                                                 break;
184                                         }
185                                 }
186                                 if (unit_table[i].name == NULL) {
187                                         g_warning ("Line %d, Node \"%s\", Property \"%s\": Unknown unit \"%s\", assuming points",
188                                                    xmlGetLineNo (node), node->name, property,
189                                                    unit);
190                                 }
191                         }
192                 }
193                 else {
194                         val = 0.0;
195                 }
196
197                 g_free (string);
198                 return val;
199         }
200
201         return default_val;
202 }
203
204 /****************************************************************************/
205 /* Set property from double.                                                */
206 /****************************************************************************/
207 void
208 gl_xml_set_prop_double (xmlNodePtr    node,
209                         const gchar  *property,
210                         gdouble       val)
211 {
212         gchar  *string, buffer[G_ASCII_DTOSTR_BUF_SIZE];
213
214         /* Guarantee "C" locale by use of g_ascii_formatd */
215         string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val);
216
217         xmlSetProp (node, property, string);
218 }
219
220 /****************************************************************************/
221 /* Set property from boolean.                                               */
222 /****************************************************************************/
223 void
224 gl_xml_set_prop_boolean (xmlNodePtr    node,
225                          const gchar  *property,
226                          gboolean      val)
227 {
228         xmlSetProp (node, property, (val ? "True" : "False"));
229 }
230
231 /****************************************************************************/
232 /* Set property from int.                                                   */
233 /****************************************************************************/
234 void
235 gl_xml_set_prop_int (xmlNodePtr    node,
236                      const gchar  *property,
237                      gint          val)
238 {
239         gchar  *string;
240
241         string = g_strdup_printf ("%d", val);
242         xmlSetProp (node, property, string);
243         g_free (string);
244 }
245
246 /****************************************************************************/
247 /* Set property from uint in hex.                                           */
248 /****************************************************************************/
249 void
250 gl_xml_set_prop_uint_hex (xmlNodePtr    node,
251                           const gchar  *property,
252                           guint         val)
253 {
254         gchar  *string;
255
256         string = g_strdup_printf ("0x%08x", val);
257         xmlSetProp (node, property, string);
258         g_free (string);
259 }
260
261 /****************************************************************************/
262 /* Set property from length.                                                */
263 /****************************************************************************/
264 void
265 gl_xml_set_prop_length (xmlNodePtr    node,
266                         const gchar  *property,
267                         gdouble       val)
268 {
269         gchar  *string, buffer[G_ASCII_DTOSTR_BUF_SIZE];
270         gchar  *string_unit;
271
272         /* Guarantee "C" locale by use of g_ascii_formatd */
273         string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val);
274
275         string_unit = g_strdup_printf ("%spt", string);
276         xmlSetProp (node, property, string_unit);
277         g_free (string_unit);
278 }
279