From 5a7b691ea17308062acf0d62f92ee8811aaeee5e Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Tue, 7 Jan 2003 04:14:03 +0000 Subject: [PATCH] Added xml utility module. Parsing of label and template xml files should no longer strand pointers returned from xmGetProp(). git-svn-id: https://glabels.svn.sourceforge.net/svnroot/glabels/trunk@241 f5e0f49d-192f-0410-a22d-a8d8700d0965 --- glabels2/po/POTFILES.in | 2 + glabels2/src/Makefile.am | 4 ++ glabels2/src/template.c | 85 +++++++++++------------ glabels2/src/xml-label-04.c | 81 ++++++++++++---------- glabels2/src/xml-label.c | 131 ++++++++++++++++++++---------------- glabels2/src/xml.c | 129 +++++++++++++++++++++++++++++++++++ glabels2/src/xml.h | 49 ++++++++++++++ 7 files changed, 345 insertions(+), 136 deletions(-) create mode 100644 glabels2/src/xml.c create mode 100644 glabels2/src/xml.h diff --git a/glabels2/po/POTFILES.in b/glabels2/po/POTFILES.in index c7838212..e5c4af76 100644 --- a/glabels2/po/POTFILES.in +++ b/glabels2/po/POTFILES.in @@ -77,6 +77,8 @@ src/xml-label.c src/xml-label.h src/xml-label-04.c src/xml-label-04.h +src/xml.c +src/xml.h src/pixbuf-cache.c src/pixbuf-cache.h src/base64.c diff --git a/glabels2/src/Makefile.am b/glabels2/src/Makefile.am index 4f06b4de..244ff6b7 100644 --- a/glabels2/src/Makefile.am +++ b/glabels2/src/Makefile.am @@ -118,6 +118,8 @@ glabels_SOURCES = \ xml-label.h \ xml-label-04.c \ xml-label-04.h \ + xml.c \ + xml.h \ pixbuf-cache.c \ pixbuf-cache.h \ base64.c \ @@ -212,6 +214,8 @@ glabels_batch_SOURCES = \ xml-label.h \ xml-label-04.c \ xml-label-04.h \ + xml.c \ + xml.h \ pixbuf-cache.c \ pixbuf-cache.h \ base64.c \ diff --git a/glabels2/src/template.c b/glabels2/src/template.c index 8a784c2d..6b323b99 100644 --- a/glabels2/src/template.c +++ b/glabels2/src/template.c @@ -27,6 +27,7 @@ #include "prefs.h" #include "util.h" +#include "xml.h" #include "template.h" #include "debug.h" @@ -571,10 +572,10 @@ gl_template_xml_parse_sheet (xmlNodePtr sheet_node) template->page_size = "US Letter"; } if (g_strcasecmp (template->page_size, "Other") == 0) { - template->page_width = - g_strtod (xmlGetProp (sheet_node, "width"), NULL); - template->page_height = - g_strtod (xmlGetProp (sheet_node, "height"), NULL); + + template->page_width = gl_xml_get_prop_double (sheet_node, "width", 0); + template->page_height = gl_xml_get_prop_double (sheet_node, "height", 0); + } else { paper = gnome_print_paper_get_by_name (template->page_size); if (paper != NULL) { @@ -614,7 +615,6 @@ xml_parse_label (xmlNodePtr label_node, { xmlNodePtr node; gchar *style; - gchar *string; gl_debug (DEBUG_TEMPLATE, "START"); @@ -632,40 +632,27 @@ xml_parse_label (xmlNodePtr label_node, g_free (style); switch (template->label.style) { + case GL_TEMPLATE_STYLE_RECT: - template->label.rect.w = - g_strtod (xmlGetProp (label_node, "width"), NULL); - template->label.rect.h = - g_strtod (xmlGetProp (label_node, "height"), NULL); - template->label.rect.r = - g_strtod (xmlGetProp (label_node, "round"), NULL); + template->label.rect.w = gl_xml_get_prop_double (label_node, "width", 0); + template->label.rect.h = gl_xml_get_prop_double (label_node, "height", 0); + template->label.rect.r = gl_xml_get_prop_double (label_node, "round", 0); break; + case GL_TEMPLATE_STYLE_ROUND: - template->label.round.r = - g_strtod (xmlGetProp (label_node, "radius"), NULL); + template->label.round.r = gl_xml_get_prop_double (label_node, "radius", 0); break; + case GL_TEMPLATE_STYLE_CD: - template->label.cd.r1 = - g_strtod (xmlGetProp (label_node, "radius"), NULL); - template->label.cd.r2 = - g_strtod (xmlGetProp (label_node, "hole"), NULL); - string = xmlGetProp (label_node, "width"); - if (string != NULL) { - template->label.cd.w = g_strtod (string, NULL); - g_free (string); - } else { - template->label.cd.w = 0.0; - } - string = xmlGetProp (label_node, "height"); - if (string != NULL) { - template->label.cd.h = g_strtod (string, NULL); - g_free (string); - } else { - template->label.cd.h = 0.0; - } + template->label.cd.r1 = gl_xml_get_prop_double (label_node, "radius", 0); + template->label.cd.r2 = gl_xml_get_prop_double (label_node, "hole", 0); + template->label.cd.w = gl_xml_get_prop_double (label_node, "width", 0); + template->label.cd.h = gl_xml_get_prop_double (label_node, "height", 0); break; + default: break; + } for (node = label_node->xmlChildrenNode; node != NULL; @@ -689,18 +676,20 @@ static void xml_parse_layout (xmlNodePtr layout_node, glTemplate *template) { - gint nx, ny; - gdouble x0, y0, dx, dy; - xmlNodePtr node; + gint nx, ny; + gdouble x0, y0, dx, dy; + xmlNodePtr node; gl_debug (DEBUG_TEMPLATE, "START"); - sscanf (xmlGetProp (layout_node, "nx"), "%d", &nx); - sscanf (xmlGetProp (layout_node, "ny"), "%d", &ny); - x0 = g_strtod (xmlGetProp (layout_node, "x0"), NULL); - y0 = g_strtod (xmlGetProp (layout_node, "y0"), NULL); - dx = g_strtod (xmlGetProp (layout_node, "dx"), NULL); - dy = g_strtod (xmlGetProp (layout_node, "dy"), NULL); + nx = gl_xml_get_prop_int (layout_node, "nx", 1); + ny = gl_xml_get_prop_int (layout_node, "ny", 1); + + x0 = gl_xml_get_prop_double (layout_node, "x0", 0); + y0 = gl_xml_get_prop_double (layout_node, "y0", 0); + + dx = gl_xml_get_prop_double (layout_node, "dx", 0); + dy = gl_xml_get_prop_double (layout_node, "dy", 0); for (node = layout_node->xmlChildrenNode; node != NULL; node = node->next) { @@ -732,19 +721,25 @@ xml_parse_markup (xmlNodePtr markup_node, type = xmlGetProp (markup_node, "type"); if (g_strcasecmp (type, "margin") == 0) { - size = g_strtod (xmlGetProp (markup_node, "size"), NULL); + + size = gl_xml_get_prop_double (markup_node, "size", 0); + template->label.any.markups = g_list_append (template->label.any.markups, markup_margin_new (size)); + } else if (g_strcasecmp (type, "line") == 0) { - x1 = g_strtod (xmlGetProp (markup_node, "x1"), NULL); - y1 = g_strtod (xmlGetProp (markup_node, "y1"), NULL); - x2 = g_strtod (xmlGetProp (markup_node, "x2"), NULL); - y2 = g_strtod (xmlGetProp (markup_node, "y2"), NULL); + + x1 = gl_xml_get_prop_double (markup_node, "x1", 0); + y1 = gl_xml_get_prop_double (markup_node, "y1", 0); + x2 = gl_xml_get_prop_double (markup_node, "x2", 0); + y2 = gl_xml_get_prop_double (markup_node, "y2", 0); + template->label.any.markups = g_list_append (template->label.any.markups, markup_line_new (x1, y1, x2, y2)); } + g_free (type); for (node = markup_node->xmlChildrenNode; node != NULL; node = node->next) { diff --git a/glabels2/src/xml-label-04.c b/glabels2/src/xml-label-04.c index 3f685abd..edf9ceef 100644 --- a/glabels2/src/xml-label-04.c +++ b/glabels2/src/xml-label-04.c @@ -31,6 +31,7 @@ #include "label-ellipse.h" #include "label-image.h" #include "label-barcode.h" +#include "xml.h" #include "debug.h" @@ -78,8 +79,7 @@ glLabel *gl_xml_label_04_parse (xmlNodePtr root, label = GL_LABEL (gl_label_new ()); - rotate_flag = - !(g_strcasecmp (xmlGetProp (root, "rotate"), "false") == 0); + rotate_flag = gl_xml_get_prop_boolean (root, "rotate", FALSE); gl_label_set_rotate_flag (label, rotate_flag); for (node = root->xmlChildrenNode; node != NULL; node = node->next) { @@ -176,8 +176,8 @@ xml04_parse_object (xmlNodePtr object_node, gl_debug (DEBUG_XML, "START"); - x = g_strtod (xmlGetProp (object_node, "x"), NULL); - y = g_strtod (xmlGetProp (object_node, "y"), NULL); + x = gl_xml_get_prop_double (object_node, "x", 0); + y = gl_xml_get_prop_double (object_node, "y", 0); gl_label_object_set_position (object, x, y); @@ -201,20 +201,22 @@ xml04_parse_text_props (xmlNodePtr object_node, glTextNode *node_text; GList *nodes, *lines; gdouble w, h, x, y; + gchar *string; gl_debug (DEBUG_XML, "START"); font_family = xmlGetProp (object_node, "font_family"); - font_size = g_strtod (xmlGetProp (object_node, "font_size"), NULL); - font_weight = - gl_util_string_to_weight (xmlGetProp (object_node, "font_weight")); - font_italic_flag = - !(g_strcasecmp (xmlGetProp (object_node, "font_italic"), "false") == - 0); + font_size = gl_xml_get_prop_double (object_node, "font_size", 0); + string = xmlGetProp (object_node, "font_weight"); + font_weight = gl_util_string_to_weight (string); + g_free (string); + font_italic_flag = gl_xml_get_prop_boolean (object_node, "font_italic", FALSE); - just = gl_util_string_to_just (xmlGetProp (object_node, "justify")); + string = xmlGetProp (object_node, "justify"); + just = gl_util_string_to_just (string); + g_free (string); - sscanf (xmlGetProp (object_node, "color"), "%x", &color); + color = gl_xml_get_prop_uint_hex (object_node, "color", 0); gl_label_text_set_props (object, font_family, @@ -304,13 +306,13 @@ xml04_parse_box_props (xmlNodePtr node, gl_debug (DEBUG_XML, "START"); - w = g_strtod (xmlGetProp (node, "w"), NULL); - h = g_strtod (xmlGetProp (node, "h"), NULL); + w = gl_xml_get_prop_double (node, "w", 0); + h = gl_xml_get_prop_double (node, "h", 0); - line_width = g_strtod (xmlGetProp (node, "line_width"), NULL); + line_width = gl_xml_get_prop_double (node, "line_width", 0); - sscanf (xmlGetProp (node, "line_color"), "%x", &line_color); - sscanf (xmlGetProp (node, "fill_color"), "%x", &fill_color); + line_color = gl_xml_get_prop_uint_hex (node, "line_color", 0); + fill_color = gl_xml_get_prop_uint_hex (node, "fill_color", 0); gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h); gl_label_box_set_line_width (object, line_width); @@ -332,12 +334,12 @@ xml04_parse_line_props (xmlNodePtr node, gl_debug (DEBUG_XML, "START"); - w = g_strtod (xmlGetProp (node, "dx"), NULL); - h = g_strtod (xmlGetProp (node, "dy"), NULL); + w = gl_xml_get_prop_double (node, "dx", 0); + h = gl_xml_get_prop_double (node, "dy", 0); - line_width = g_strtod (xmlGetProp (node, "line_width"), NULL); + line_width = gl_xml_get_prop_double (node, "line_width", 0); - sscanf (xmlGetProp (node, "line_color"), "%x", &line_color); + line_color = gl_xml_get_prop_uint_hex (node, "line_color", 0); gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h); gl_label_line_set_line_width (object, line_width); @@ -358,13 +360,13 @@ xml04_parse_ellipse_props (xmlNodePtr node, gl_debug (DEBUG_XML, "START"); - w = g_strtod (xmlGetProp (node, "w"), NULL); - h = g_strtod (xmlGetProp (node, "h"), NULL); + w = gl_xml_get_prop_double (node, "w", 0); + h = gl_xml_get_prop_double (node, "h", 0); - line_width = g_strtod (xmlGetProp (node, "line_width"), NULL); + line_width = gl_xml_get_prop_double (node, "line_width", 0); - sscanf (xmlGetProp (node, "line_color"), "%x", &line_color); - sscanf (xmlGetProp (node, "fill_color"), "%x", &fill_color); + line_color = gl_xml_get_prop_uint_hex (node, "line_color", 0); + fill_color = gl_xml_get_prop_uint_hex (node, "fill_color", 0); gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h); gl_label_ellipse_set_line_width (object, line_width); @@ -386,8 +388,8 @@ xml04_parse_image_props (xmlNodePtr node, gl_debug (DEBUG_XML, "START"); - w = g_strtod (xmlGetProp (node, "w"), NULL); - h = g_strtod (xmlGetProp (node, "h"), NULL); + w = gl_xml_get_prop_double (node, "w", 0); + h = gl_xml_get_prop_double (node, "h", 0); gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h); filename = g_new0 (glTextNode, 1); @@ -412,13 +414,18 @@ xml04_parse_barcode_props (xmlNodePtr node, gdouble scale; xmlNodePtr child; glTextNode *text_node; + gchar *string; gl_debug (DEBUG_XML, "START"); - sscanf (xmlGetProp (node, "color"), "%x", &color); - style = gl_barcode_text_to_style (xmlGetProp (node, "style")); - text_flag = !(g_strcasecmp (xmlGetProp (node, "text"), "false") == 0); - scale = g_strtod (xmlGetProp (node, "scale"), NULL); + color = gl_xml_get_prop_uint_hex (node, "color", 0); + + string = xmlGetProp (node, "style"); + style = gl_barcode_text_to_style (string); + g_free (string); + + text_flag = gl_xml_get_prop_boolean (node, "text", FALSE); + scale = gl_xml_get_prop_double (node, "scale", 1.0); if (scale == 0.0) { scale = 0.5; /* Set to a valid value */ } @@ -451,11 +458,17 @@ xml04_parse_merge_properties (xmlNodePtr node, { glMerge *merge; xmlNodePtr child; + gchar *string; gl_debug (DEBUG_XML, "START"); - merge = gl_merge_new (xmlGetProp (node, "type")); - gl_merge_set_src (merge, xmlGetProp (node, "src")); + string = xmlGetProp (node, "type"); + merge = gl_merge_new (string); + g_free (string); + + string = xmlGetProp (node, "src"); + gl_merge_set_src (merge, string); + g_free (string); gl_label_set_merge (label, merge); diff --git a/glabels2/src/xml-label.c b/glabels2/src/xml-label.c index 293425d8..e7cb46a0 100644 --- a/glabels2/src/xml-label.c +++ b/glabels2/src/xml-label.c @@ -40,6 +40,7 @@ #include "base64.h" #include "xml-label.h" #include "xml-label-04.h" +#include "xml.h" #include "util.h" #include "debug.h" @@ -334,13 +335,12 @@ static void xml_parse_objects (xmlNodePtr objects_node, glLabel *label) { - gboolean rotate_flag; - xmlNodePtr node; + gboolean rotate_flag; + xmlNodePtr node; gl_debug (DEBUG_XML, "START"); - rotate_flag = - !(g_strcasecmp (xmlGetProp (objects_node, "rotate"), "false") == 0); + rotate_flag = gl_xml_get_prop_boolean (objects_node, "rotate", FALSE); gl_label_set_rotate_flag (label, rotate_flag); for (node = objects_node->xmlChildrenNode; node != NULL; node = node->next) { @@ -366,44 +366,49 @@ xml_parse_object (xmlNodePtr object_node, { glLabelObject *object; gdouble x, y; - gchar *type_string; + gchar *string; gdouble affine[6]; gl_debug (DEBUG_XML, "START"); - type_string = xmlGetProp (object_node, "type"); + string = xmlGetProp (object_node, "type"); - if ( g_strcasecmp (type_string, "text") == 0 ) { + if ( g_strcasecmp (string, "text") == 0 ) { object = xml_parse_text_props (object_node, label); - } else if ( g_strcasecmp (type_string, "box") == 0 ) { + } else if ( g_strcasecmp (string, "box") == 0 ) { object = xml_parse_box_props (object_node, label); - } else if ( g_strcasecmp (type_string, "line") == 0 ) { + } else if ( g_strcasecmp (string, "line") == 0 ) { object = xml_parse_line_props (object_node, label); - } else if ( g_strcasecmp (type_string, "ellipse") == 0 ) { + } else if ( g_strcasecmp (string, "ellipse") == 0 ) { object = xml_parse_ellipse_props (object_node, label); - } else if ( g_strcasecmp (type_string, "image") == 0 ) { + } else if ( g_strcasecmp (string, "image") == 0 ) { object = xml_parse_image_props (object_node, label); - } else if ( g_strcasecmp (type_string, "barcode") == 0 ) { + } else if ( g_strcasecmp (string, "barcode") == 0 ) { object = xml_parse_barcode_props (object_node, label); } else { - g_warning ("Unknown label object type \"%s\"", type_string); + g_warning ("Unknown label object type \"%s\"", string); + g_free (string); return; } + g_free (string); - x = g_strtod (xmlGetProp (object_node, "x"), NULL); - y = g_strtod (xmlGetProp (object_node, "y"), NULL); + + x = gl_xml_get_prop_double (object_node, "x", 0.0); + y = gl_xml_get_prop_double (object_node, "y", 0.0); gl_label_object_set_position (object, x, y); - affine[0] = g_strtod (xmlGetProp (object_node, "a0"), NULL); - affine[1] = g_strtod (xmlGetProp (object_node, "a1"), NULL); - affine[2] = g_strtod (xmlGetProp (object_node, "a2"), NULL); - affine[3] = g_strtod (xmlGetProp (object_node, "a3"), NULL); - affine[4] = g_strtod (xmlGetProp (object_node, "a4"), NULL); - affine[5] = g_strtod (xmlGetProp (object_node, "a5"), NULL); + + affine[0] = gl_xml_get_prop_double (object_node, "a0", 0.0); + affine[1] = gl_xml_get_prop_double (object_node, "a1", 0.0); + affine[2] = gl_xml_get_prop_double (object_node, "a2", 0.0); + affine[3] = gl_xml_get_prop_double (object_node, "a3", 0.0); + affine[4] = gl_xml_get_prop_double (object_node, "a4", 0.0); + affine[5] = gl_xml_get_prop_double (object_node, "a5", 0.0); gl_label_object_set_affine (object, affine); + gl_debug (DEBUG_XML, "END"); } @@ -425,22 +430,27 @@ xml_parse_text_props (xmlNodePtr object_node, xmlNodePtr line_node, text_node; glTextNode *node_text; GList *nodes; + gchar *string; gl_debug (DEBUG_XML, "START"); object = gl_label_text_new (label); font_family = xmlGetProp (object_node, "font_family"); - font_size = g_strtod (xmlGetProp (object_node, "font_size"), NULL); - font_weight = gl_util_string_to_weight (xmlGetProp (object_node, - "font_weight")); - font_italic_flag = - !(g_strcasecmp (xmlGetProp (object_node, "font_italic"), "false") == - 0); - just = gl_util_string_to_just (xmlGetProp (object_node, "justify")); + font_size = gl_xml_get_prop_double (object_node, "font_size", 0.0); + + string = xmlGetProp (object_node, "font_weight"); + font_weight = gl_util_string_to_weight (string); + g_free (string); + + font_italic_flag = gl_xml_get_prop_boolean (object_node, "font_italic", FALSE); + + string = xmlGetProp (object_node, "justify"); + just = gl_util_string_to_just (string); + g_free (string); - sscanf (xmlGetProp (object_node, "color"), "%x", &color); + color = gl_xml_get_prop_uint_hex (object_node, "color", 0); lines = NULL; for (line_node = object_node->xmlChildrenNode; @@ -456,10 +466,8 @@ xml_parse_text_props (xmlNodePtr object_node, if (g_strcasecmp (text_node->name, "Field") == 0) { node_text = g_new0 (glTextNode, 1); node_text->field_flag = TRUE; - node_text->data = - xmlGetProp (text_node, "name"); - nodes = - g_list_append (nodes, node_text); + node_text->data = xmlGetProp (text_node, "name"); + nodes = g_list_append (nodes, node_text); } else if (g_strcasecmp (text_node->name, "Literal") == 0) { node_text = g_new0 (glTextNode, 1); node_text->field_flag = FALSE; @@ -512,13 +520,13 @@ xml_parse_box_props (xmlNodePtr node, object = gl_label_box_new (label); - w = g_strtod (xmlGetProp (node, "w"), NULL); - h = g_strtod (xmlGetProp (node, "h"), NULL); + w = gl_xml_get_prop_double (node, "w", 0); + h = gl_xml_get_prop_double (node, "h", 0); - line_width = g_strtod (xmlGetProp (node, "line_width"), NULL); + line_width = gl_xml_get_prop_double (node, "line_width", 1.0); - sscanf (xmlGetProp (node, "line_color"), "%x", &line_color); - sscanf (xmlGetProp (node, "fill_color"), "%x", &fill_color); + line_color = gl_xml_get_prop_uint_hex (node, "line_color", 0); + fill_color = gl_xml_get_prop_uint_hex (node, "fill_color", 0); gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h); gl_label_box_set_line_width (GL_LABEL_BOX(object), line_width); @@ -546,12 +554,12 @@ xml_parse_line_props (xmlNodePtr node, object = gl_label_line_new (label); - w = g_strtod (xmlGetProp (node, "dx"), NULL); - h = g_strtod (xmlGetProp (node, "dy"), NULL); + w = gl_xml_get_prop_double (node, "dx", 0); + h = gl_xml_get_prop_double (node, "dy", 0); - line_width = g_strtod (xmlGetProp (node, "line_width"), NULL); + line_width = gl_xml_get_prop_double (node, "line_width", 1.0); - sscanf (xmlGetProp (node, "line_color"), "%x", &line_color); + line_color = gl_xml_get_prop_uint_hex (node, "line_color", 0); gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h); gl_label_line_set_line_width (GL_LABEL_LINE(object), line_width); @@ -578,13 +586,13 @@ xml_parse_ellipse_props (xmlNodePtr node, object = gl_label_ellipse_new (label); - w = g_strtod (xmlGetProp (node, "w"), NULL); - h = g_strtod (xmlGetProp (node, "h"), NULL); + w = gl_xml_get_prop_double (node, "w", 0); + h = gl_xml_get_prop_double (node, "h", 0); - line_width = g_strtod (xmlGetProp (node, "line_width"), NULL); + line_width = gl_xml_get_prop_double (node, "line_width", 1.0); - sscanf (xmlGetProp (node, "line_color"), "%x", &line_color); - sscanf (xmlGetProp (node, "fill_color"), "%x", &fill_color); + line_color = gl_xml_get_prop_uint_hex (node, "line_color", 0); + fill_color = gl_xml_get_prop_uint_hex (node, "fill_color", 0); gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h); gl_label_ellipse_set_line_width (GL_LABEL_ELLIPSE(object), line_width); @@ -612,8 +620,8 @@ xml_parse_image_props (xmlNodePtr node, object = gl_label_image_new (label); - w = g_strtod (xmlGetProp (node, "w"), NULL); - h = g_strtod (xmlGetProp (node, "h"), NULL); + w = gl_xml_get_prop_double (node, "w", 0); + h = gl_xml_get_prop_double (node, "h", 0); filename = g_new0 (glTextNode, 1); for (child = node->xmlChildrenNode; child != NULL; child = child->next) { @@ -647,6 +655,7 @@ xml_parse_barcode_props (xmlNodePtr node, { GObject *object; xmlNodePtr child; + gchar *string; glTextNode *text_node; glBarcodeStyle style; gboolean text_flag; @@ -657,11 +666,15 @@ xml_parse_barcode_props (xmlNodePtr node, object = gl_label_barcode_new (label); - sscanf (xmlGetProp (node, "color"), "%x", &color); + color = gl_xml_get_prop_uint_hex (node, "color", 0); + + string = xmlGetProp (node, "style"); + style = gl_barcode_text_to_style (string); + g_free (string); - style = gl_barcode_text_to_style (xmlGetProp (node, "style")); - text_flag = !(g_strcasecmp (xmlGetProp (node, "text"), "false") == 0); - scale = g_strtod (xmlGetProp (node, "scale"), NULL); + text_flag = gl_xml_get_prop_boolean (node, "text", FALSE); + + scale = gl_xml_get_prop_double (node, "scale", 1.0); text_node = g_new0 (glTextNode, 1); for (child = node->xmlChildrenNode; child != NULL; child = child->next) { @@ -695,14 +708,18 @@ xml_parse_merge_fields (xmlNodePtr node, glLabel *label) { xmlNodePtr child; + gchar *string; glMerge *merge; - gchar *src; gl_debug (DEBUG_XML, "START"); - merge = gl_merge_new (xmlGetProp (node, "type")); - src = xmlGetProp (node, "src"); - gl_merge_set_src (merge, src); + string = xmlGetProp (node, "type"); + merge = gl_merge_new (string); + g_free (string); + + string = xmlGetProp (node, "src"); + gl_merge_set_src (merge, string); + g_free (string); gl_label_set_merge (label, merge); diff --git a/glabels2/src/xml.c b/glabels2/src/xml.c new file mode 100644 index 00000000..e6b7d53d --- /dev/null +++ b/glabels2/src/xml.c @@ -0,0 +1,129 @@ +/* + * (GLABELS) Label and Business Card Creation program for GNOME + * + * xml.c: GLabels xml utilities module + * + * Copyright (C) 2003 Jim Evins . + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include + +#include "xml.h" + +/*========================================================*/ +/* Private macros and constants. */ +/*========================================================*/ + +/*========================================================*/ +/* Private types. */ +/*========================================================*/ + +/*========================================================*/ +/* Private globals. */ +/*========================================================*/ + +/*========================================================*/ +/* Private function prototypes. */ +/*========================================================*/ + + +/****************************************************************************/ +/* Return value of property as a double. */ +/****************************************************************************/ +gdouble +gl_xml_get_prop_double (xmlNodePtr node, + const gchar *property, + gdouble default_val) +{ + gdouble val; + gchar *string; + + string = xmlGetProp (node, property); + if ( string != NULL ) { + val = g_strtod (string, NULL); + g_free (string); + return val; + } + + return default_val; +} + +/****************************************************************************/ +/* Return value of property as a boolean. */ +/****************************************************************************/ +gboolean +gl_xml_get_prop_boolean (xmlNodePtr node, + const gchar *property, + gboolean default_val) +{ + gboolean val; + gchar *string; + + string = xmlGetProp (node, property); + if ( string != NULL ) { + val = !(g_strcasecmp (string, "false") == 0); + g_free (string); + return val; + } + + return default_val; +} + + +/****************************************************************************/ +/* Return value of property as an int. . */ +/****************************************************************************/ +gint +gl_xml_get_prop_int (xmlNodePtr node, + const gchar *property, + gint default_val) +{ + gint val; + gchar *string; + + string = xmlGetProp (node, property); + if ( string != NULL ) { + sscanf (string, "%d", &val); + g_free (string); + return val; + } + + return default_val; +} + + +/****************************************************************************/ +/* Return value of hex property as an unsigned int. */ +/****************************************************************************/ +guint +gl_xml_get_prop_uint_hex (xmlNodePtr node, + const gchar *property, + guint default_val) +{ + guint val; + gchar *string; + + string = xmlGetProp (node, property); + if ( string != NULL ) { + sscanf (string, "%x", &val); + g_free (string); + return val; + } + + return default_val; +} + + diff --git a/glabels2/src/xml.h b/glabels2/src/xml.h new file mode 100644 index 00000000..c20581b6 --- /dev/null +++ b/glabels2/src/xml.h @@ -0,0 +1,49 @@ +/* + * (GLABELS) Label and Business Card Creation program for GNOME + * + * xml.h: GLabels xml utilities header file + * + * Copyright (C) 2003 Jim Evins . + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef __XML_H__ +#define __XML_H__ + +#include +#include + +G_BEGIN_DECLS + +gdouble gl_xml_get_prop_double (xmlNodePtr node, + const gchar *property, + gdouble default_val); + +gboolean gl_xml_get_prop_boolean (xmlNodePtr node, + const gchar *property, + gboolean default_val); + +gint gl_xml_get_prop_int (xmlNodePtr node, + const gchar *property, + gint default_val); + +guint gl_xml_get_prop_uint_hex (xmlNodePtr node, + const gchar *property, + guint default_val); + +G_END_DECLS + + +#endif /* __XML_H__ */ -- 2.39.5