]> git.sur5r.net Git - glabels/blob - glabels2/src/xml.c
d90091b823b750a144df4e51400835dd1756e91d
[glabels] / glabels2 / src / xml.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  xml.c:  GLabels xml utilities module
5  *
6  *  Copyright (C) 2003  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22 #include <config.h>
23
24 #include "xml.h"
25
26 /*========================================================*/
27 /* Private macros and constants.                          */
28 /*========================================================*/
29
30 /*========================================================*/
31 /* Private types.                                         */
32 /*========================================================*/
33
34 /*========================================================*/
35 /* Private globals.                                       */
36 /*========================================================*/
37
38 /*========================================================*/
39 /* Private function prototypes.                           */
40 /*========================================================*/
41
42 \f
43 /****************************************************************************/
44 /* Return value of property as a double.                                    */
45 /****************************************************************************/
46 gdouble
47 gl_xml_get_prop_double (xmlNodePtr   node,
48                         const gchar *property,
49                         gdouble      default_val)
50 {
51         gdouble  val;
52         gchar   *string;
53
54         string = xmlGetProp (node, property);
55         if ( string != NULL ) {
56                 val = g_strtod (string, NULL);
57                 g_free (string);
58                 return val;
59         }
60
61         return default_val;
62 }
63
64 /****************************************************************************/
65 /* Return value of property as a boolean.                                   */
66 /****************************************************************************/
67 gboolean
68 gl_xml_get_prop_boolean (xmlNodePtr   node,
69                          const gchar *property,
70                          gboolean     default_val)
71 {
72         gboolean  val;
73         gchar    *string;
74
75         string = xmlGetProp (node, property);
76         if ( string != NULL ) {
77                 val = !(g_strcasecmp (string, "false") == 0);
78                 g_free (string);
79                 return val;
80         }
81
82         return default_val;
83 }
84
85
86 /****************************************************************************/
87 /* Return value of property as an int. .                                    */
88 /****************************************************************************/
89 gint
90 gl_xml_get_prop_int (xmlNodePtr   node,
91                      const gchar *property,
92                      gint         default_val)
93 {
94         gint     val;
95         gchar   *string;
96
97         string = xmlGetProp (node, property);
98         if ( string != NULL ) {
99                 sscanf (string, "%d", &val);
100                 g_free (string);
101                 return val;
102         }
103
104         return default_val;
105 }
106
107
108 /****************************************************************************/
109 /* Return value of hex property as an unsigned int.                         */
110 /****************************************************************************/
111 guint
112 gl_xml_get_prop_uint_hex (xmlNodePtr   node,
113                           const gchar *property,
114                           guint        default_val)
115 {
116         guint    val;
117         gchar   *string;
118
119         string = xmlGetProp (node, property);
120         if ( string != NULL ) {
121                 sscanf (string, "%x", &val);
122                 g_free (string);
123                 return val;
124         }
125
126         return default_val;
127 }
128
129
130 /****************************************************************************/
131 /* Set property from double.                                                */
132 /****************************************************************************/
133 void
134 gl_xml_set_prop_double (xmlNodePtr    node,
135                         const gchar  *property,
136                         gdouble       val)
137 {
138         gchar  *string;
139
140         string = g_strdup_printf ("%g", val);
141         xmlSetProp (node, property, string);
142         g_free (string);
143 }
144
145 /****************************************************************************/
146 /* Set property from boolean.                                               */
147 /****************************************************************************/
148 void
149 gl_xml_set_prop_boolean (xmlNodePtr    node,
150                          const gchar  *property,
151                          gboolean      val)
152 {
153         xmlSetProp (node, property, (val ? "True" : "False"));
154 }
155
156 /****************************************************************************/
157 /* Set property from int.                                                   */
158 /****************************************************************************/
159 void
160 gl_xml_set_prop_int (xmlNodePtr    node,
161                      const gchar  *property,
162                      gint          val)
163 {
164         gchar  *string;
165
166         string = g_strdup_printf ("%d", val);
167         xmlSetProp (node, property, string);
168         g_free (string);
169 }
170
171 /****************************************************************************/
172 /* Set property from uint in hex.                                           */
173 /****************************************************************************/
174 void
175 gl_xml_set_prop_uint_hex (xmlNodePtr    node,
176                           const gchar  *property,
177                           guint         val)
178 {
179         gchar  *string;
180
181         string = g_strdup_printf ("0x%08x", val);
182         xmlSetProp (node, property, string);
183         g_free (string);
184 }
185