From: Jim Evins Date: Wed, 8 Jan 2003 00:13:45 +0000 (+0000) Subject: Added xml_set_prop_*() utilities to xml.c. X-Git-Tag: glabels-2_3_0~645 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=8658f7ffd7739571743c8827b994a0c09d2713ab;p=glabels Added xml_set_prop_*() utilities to xml.c. git-svn-id: https://glabels.svn.sourceforge.net/svnroot/glabels/trunk@243 f5e0f49d-192f-0410-a22d-a8d8700d0965 --- diff --git a/glabels2/src/template.c b/glabels2/src/template.c index 6b323b99..374d6fd0 100644 --- a/glabels2/src/template.c +++ b/glabels2/src/template.c @@ -776,7 +776,6 @@ gl_template_xml_add_sheet (const glTemplate *template, { xmlNodePtr node; GList *p; - gchar *string; gl_debug (DEBUG_TEMPLATE, "START"); @@ -787,13 +786,9 @@ gl_template_xml_add_sheet (const glTemplate *template, xmlSetProp (node, "size", template->page_size); if (g_strcasecmp (template->page_size, "Other") == 0) { - string = g_strdup_printf ("%g", template->page_width); - xmlSetProp (node, "width", string); - g_free (string); + gl_xml_set_prop_double (node, "width", template->page_width); + gl_xml_set_prop_double (node, "height", template->page_height); - string = g_strdup_printf ("%g", template->page_height); - xmlSetProp (node, "height", string); - g_free (string); } xmlSetProp (node, "description", template->description); @@ -816,7 +811,6 @@ xml_add_label (const glTemplate *template, xmlNsPtr ns) { xmlNodePtr node; - gchar *string; GList *p; glTemplateMarkup *markup; glTemplateLayout *layout; @@ -828,46 +822,35 @@ xml_add_label (const glTemplate *template, xmlSetProp (node, "id", "0"); switch (template->label.style) { + case GL_TEMPLATE_STYLE_RECT: xmlSetProp (node, "style", "rectangle"); - string = g_strdup_printf ("%g", template->label.rect.w); - xmlSetProp (node, "width", string); - g_free (string); - string = g_strdup_printf ("%g", template->label.rect.h); - xmlSetProp (node, "height", string); - g_free (string); - string = g_strdup_printf ("%g", template->label.rect.r); - xmlSetProp (node, "round", string); - g_free (string); + gl_xml_set_prop_double (node, "width", template->label.rect.w); + gl_xml_set_prop_double (node, "height", template->label.rect.h); + gl_xml_set_prop_double (node, "round", template->label.rect.r); break; + case GL_TEMPLATE_STYLE_ROUND: xmlSetProp (node, "style", "round"); - string = g_strdup_printf ("%g", template->label.round.r); - xmlSetProp (node, "radius", string); - g_free (string); + gl_xml_set_prop_double (node, "radius", template->label.round.r); break; + case GL_TEMPLATE_STYLE_CD: xmlSetProp (node, "style", "cd"); - string = g_strdup_printf ("%g", template->label.cd.r1); - xmlSetProp (node, "radius", string); - g_free (string); - string = g_strdup_printf ("%g", template->label.cd.r2); - xmlSetProp (node, "hole", string); - g_free (string); - if (template->label.cd.h != 0.0) { - string = g_strdup_printf ("%g", template->label.cd.h); - xmlSetProp (node, "width", string); - g_free (string); - } + gl_xml_set_prop_double (node, "radius", template->label.cd.r1); + gl_xml_set_prop_double (node, "hole", template->label.cd.r2); if (template->label.cd.w != 0.0) { - string = g_strdup_printf ("%g", template->label.cd.w); - xmlSetProp (node, "height", string); - g_free (string); + gl_xml_set_prop_double (node, "width", template->label.cd.w); + } + if (template->label.cd.h != 0.0) { + gl_xml_set_prop_double (node, "height", template->label.cd.h); } break; + default: g_warning ("Unknown label style"); break; + } for ( p=template->label.any.markups; p != NULL; p=p->next ) { @@ -904,29 +887,16 @@ xml_add_layout (glTemplateLayout *layout, xmlNsPtr ns) { xmlNodePtr node; - gchar *string; gl_debug (DEBUG_TEMPLATE, "START"); node = xmlNewChild(root, ns, "Layout", NULL); - string = g_strdup_printf ("%d", layout->nx); - xmlSetProp (node, "nx", string); - g_free (string); - string = g_strdup_printf ("%d", layout->ny); - xmlSetProp (node, "ny", string); - g_free (string); - string = g_strdup_printf ("%g", layout->x0); - xmlSetProp (node, "x0", string); - g_free (string); - string = g_strdup_printf ("%g", layout->y0); - xmlSetProp (node, "y0", string); - g_free (string); - string = g_strdup_printf ("%g", layout->dx); - xmlSetProp (node, "dx", string); - g_free (string); - string = g_strdup_printf ("%g", layout->dy); - xmlSetProp (node, "dy", string); - g_free (string); + gl_xml_set_prop_int (node, "nx", layout->nx); + gl_xml_set_prop_int (node, "ny", layout->ny); + gl_xml_set_prop_double (node, "x0", layout->x0); + gl_xml_set_prop_double (node, "y0", layout->y0); + gl_xml_set_prop_double (node, "dx", layout->dx); + gl_xml_set_prop_double (node, "dy", layout->dy); gl_debug (DEBUG_TEMPLATE, "END"); } @@ -940,16 +910,13 @@ xml_add_markup_margin (glTemplateMarkupMargin *margin, xmlNsPtr ns) { xmlNodePtr node; - gchar *string; gl_debug (DEBUG_TEMPLATE, "START"); node = xmlNewChild(root, ns, "Markup", NULL); xmlSetProp (node, "type", "margin"); - string = g_strdup_printf ("%g", margin->size); - xmlSetProp (node, "size", string); - g_free (string); + gl_xml_set_prop_double (node, "size", margin->size); gl_debug (DEBUG_TEMPLATE, "END"); } @@ -963,25 +930,16 @@ xml_add_markup_line (glTemplateMarkupLine *line, xmlNsPtr ns) { xmlNodePtr node; - gchar *string; gl_debug (DEBUG_TEMPLATE, "START"); node = xmlNewChild(root, ns, "Markup", NULL); xmlSetProp (node, "type", "line"); - string = g_strdup_printf ("%g", line->x1); - xmlSetProp (node, "x1", string); - g_free (string); - string = g_strdup_printf ("%g", line->y1); - xmlSetProp (node, "y1", string); - g_free (string); - string = g_strdup_printf ("%g", line->x2); - xmlSetProp (node, "x2", string); - g_free (string); - string = g_strdup_printf ("%g", line->y2); - xmlSetProp (node, "y2", string); - g_free (string); + gl_xml_set_prop_double (node, "x1", line->x1); + gl_xml_set_prop_double (node, "y1", line->y1); + gl_xml_set_prop_double (node, "x2", line->x2); + gl_xml_set_prop_double (node, "y2", line->y2); gl_debug (DEBUG_TEMPLATE, "END"); } diff --git a/glabels2/src/xml-label.c b/glabels2/src/xml-label.c index 5f1edce7..c1f05a7e 100644 --- a/glabels2/src/xml-label.c +++ b/glabels2/src/xml-label.c @@ -916,7 +916,7 @@ xml_create_objects (xmlNodePtr root, node = xmlNewChild (root, ns, "Objects", NULL); xmlSetProp (node, "id", "0"); - xmlSetProp (node, "rotate", rotate_flag ? "True" : "False"); + gl_xml_set_prop_boolean (node, "rotate", rotate_flag); for (p = label->objects; p != NULL; p = p->next) { xml_create_object (node, ns, GL_LABEL_OBJECT(p->data)); @@ -935,7 +935,6 @@ xml_create_object (xmlNodePtr root, { xmlNodePtr object_node; gdouble x, y; - gchar *string; gdouble affine[6]; gl_debug (DEBUG_XML, "START"); @@ -943,32 +942,16 @@ xml_create_object (xmlNodePtr root, object_node = xmlNewChild (root, ns, "Object", NULL); gl_label_object_get_position (object, &x, &y); - string = g_strdup_printf ("%g", x); - xmlSetProp (object_node, "x", string); - g_free (string); - string = g_strdup_printf ("%g", y); - xmlSetProp (object_node, "y", string); - g_free (string); + gl_xml_set_prop_double (object_node, "x", x); + gl_xml_set_prop_double (object_node, "y", y); gl_label_object_get_affine (object, affine); - string = g_strdup_printf ("%g", affine[0]); - xmlSetProp (object_node, "a0", string); - g_free (string); - string = g_strdup_printf ("%g", affine[1]); - xmlSetProp (object_node, "a1", string); - g_free (string); - string = g_strdup_printf ("%g", affine[2]); - xmlSetProp (object_node, "a2", string); - g_free (string); - string = g_strdup_printf ("%g", affine[3]); - xmlSetProp (object_node, "a3", string); - g_free (string); - string = g_strdup_printf ("%g", affine[4]); - xmlSetProp (object_node, "a4", string); - g_free (string); - string = g_strdup_printf ("%g", affine[5]); - xmlSetProp (object_node, "a5", string); - g_free (string); + gl_xml_set_prop_double (object_node, "a0", affine[0]); + gl_xml_set_prop_double (object_node, "a1", affine[1]); + gl_xml_set_prop_double (object_node, "a2", affine[2]); + gl_xml_set_prop_double (object_node, "a3", affine[3]); + gl_xml_set_prop_double (object_node, "a4", affine[4]); + gl_xml_set_prop_double (object_node, "a5", affine[5]); if ( GL_IS_LABEL_TEXT(object) ) { xml_create_text_props (object_node, ns, object); @@ -1006,7 +989,6 @@ xml_create_text_props (xmlNodePtr object_node, gboolean font_italic_flag; guint color; GtkJustification just; - gchar *string; GList *p_line, *p_node; glTextNode *node_text; @@ -1021,27 +1003,18 @@ xml_create_text_props (xmlNodePtr object_node, &font_italic_flag, &color, &just); - string = g_strdup_printf ("%g", w); - xmlSetProp (object_node, "w", string); - g_free (string); - string = g_strdup_printf ("%g", h); - xmlSetProp (object_node, "h", string); - g_free (string); + gl_xml_set_prop_double (object_node, "w", w); + gl_xml_set_prop_double (object_node, "h", h); xmlSetProp (object_node, "font_family", font_family); - string = g_strdup_printf ("%g", font_size); - xmlSetProp (object_node, "font_size", string); - g_free (string); + gl_xml_set_prop_double (object_node, "font_size", font_size); xmlSetProp (object_node, "font_weight", gl_util_weight_to_string (font_weight)); - xmlSetProp (object_node, "font_italic", - font_italic_flag?"True":"False"); + gl_xml_set_prop_boolean (object_node, "font_italic", font_italic_flag); xmlSetProp (object_node, "justify", gl_util_just_to_string (just)); - string = g_strdup_printf ("0x%08x", color); - xmlSetProp (object_node, "color", string); - g_free (string); + gl_xml_set_prop_uint_hex (object_node, "color", color); for (p_line = lines; p_line != NULL; p_line = p_line->next) { line_node = xmlNewChild (object_node, ns, "Line", NULL); @@ -1079,7 +1052,6 @@ xml_create_box_props (xmlNodePtr object_node, xmlNsPtr ns, glLabelObject *object) { - gchar *string; gdouble line_width; guint line_color, fill_color; gdouble w, h; @@ -1093,24 +1065,13 @@ xml_create_box_props (xmlNodePtr object_node, line_color = gl_label_box_get_line_color (GL_LABEL_BOX(object)); fill_color = gl_label_box_get_fill_color (GL_LABEL_BOX(object)); - string = g_strdup_printf ("%g", w); - xmlSetProp (object_node, "w", string); - g_free (string); - string = g_strdup_printf ("%g", h); - xmlSetProp (object_node, "h", string); - g_free (string); - - string = g_strdup_printf ("%g", line_width); - xmlSetProp (object_node, "line_width", string); - g_free (string); + gl_xml_set_prop_double (object_node, "w", w); + gl_xml_set_prop_double (object_node, "h", h); - string = g_strdup_printf ("0x%08x", line_color); - xmlSetProp (object_node, "line_color", string); - g_free (string); + gl_xml_set_prop_double (object_node, "line_width", line_width); - string = g_strdup_printf ("0x%08x", fill_color); - xmlSetProp (object_node, "fill_color", string); - g_free (string); + gl_xml_set_prop_uint_hex (object_node, "line_color", line_color); + gl_xml_set_prop_uint_hex (object_node, "fill_color", fill_color); gl_debug (DEBUG_XML, "END"); } @@ -1123,7 +1084,6 @@ xml_create_line_props (xmlNodePtr object_node, xmlNsPtr ns, glLabelObject *object) { - gchar *string; gdouble line_width; guint line_color; gdouble w, h; @@ -1136,20 +1096,12 @@ xml_create_line_props (xmlNodePtr object_node, line_width = gl_label_line_get_line_width (GL_LABEL_LINE(object)); line_color = gl_label_line_get_line_color (GL_LABEL_LINE(object)); - string = g_strdup_printf ("%g", w); - xmlSetProp (object_node, "dx", string); - g_free (string); - string = g_strdup_printf ("%g", h); - xmlSetProp (object_node, "dy", string); - g_free (string); + gl_xml_set_prop_double (object_node, "dx", w); + gl_xml_set_prop_double (object_node, "dy", h); - string = g_strdup_printf ("%g", line_width); - xmlSetProp (object_node, "line_width", string); - g_free (string); + gl_xml_set_prop_double (object_node, "line_width", line_width); - string = g_strdup_printf ("0x%08x", line_color); - xmlSetProp (object_node, "line_color", string); - g_free (string); + gl_xml_set_prop_uint_hex (object_node, "line_color", line_color); gl_debug (DEBUG_XML, "END"); } @@ -1162,7 +1114,6 @@ xml_create_ellipse_props (xmlNodePtr object_node, xmlNsPtr ns, glLabelObject *object) { - gchar *string; gdouble line_width; guint line_color, fill_color; gdouble w, h; @@ -1176,24 +1127,13 @@ xml_create_ellipse_props (xmlNodePtr object_node, line_color = gl_label_ellipse_get_line_color (GL_LABEL_ELLIPSE(object)); fill_color = gl_label_ellipse_get_fill_color (GL_LABEL_ELLIPSE(object)); - string = g_strdup_printf ("%g", w); - xmlSetProp (object_node, "w", string); - g_free (string); - string = g_strdup_printf ("%g", h); - xmlSetProp (object_node, "h", string); - g_free (string); - - string = g_strdup_printf ("%g", line_width); - xmlSetProp (object_node, "line_width", string); - g_free (string); + gl_xml_set_prop_double (object_node, "w", w); + gl_xml_set_prop_double (object_node, "h", h); - string = g_strdup_printf ("0x%08x", line_color); - xmlSetProp (object_node, "line_color", string); - g_free (string); + gl_xml_set_prop_double (object_node, "line_width", line_width); - string = g_strdup_printf ("0x%08x", fill_color); - xmlSetProp (object_node, "fill_color", string); - g_free (string); + gl_xml_set_prop_uint_hex (object_node, "line_color", line_color); + gl_xml_set_prop_uint_hex (object_node, "fill_color", fill_color); gl_debug (DEBUG_XML, "END"); } @@ -1206,7 +1146,6 @@ xml_create_image_props (xmlNodePtr object_node, xmlNsPtr ns, glLabelObject *object) { - gchar *string; gdouble w, h; glTextNode *filename; xmlNodePtr child; @@ -1218,12 +1157,8 @@ xml_create_image_props (xmlNodePtr object_node, gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h); filename = gl_label_image_get_filename (GL_LABEL_IMAGE(object)); - string = g_strdup_printf ("%g", w); - xmlSetProp (object_node, "w", string); - g_free (string); - string = g_strdup_printf ("%g", h); - xmlSetProp (object_node, "h", string); - g_free (string); + gl_xml_set_prop_double (object_node, "w", w); + gl_xml_set_prop_double (object_node, "h", h); if (filename->field_flag) { child = xmlNewChild (object_node, ns, "Field", NULL); @@ -1252,7 +1187,6 @@ xml_create_barcode_props (xmlNodePtr object_node, guint color; gdouble scale; xmlNodePtr child; - gchar *string; gl_debug (DEBUG_XML, "START"); @@ -1262,15 +1196,11 @@ xml_create_barcode_props (xmlNodePtr object_node, gl_label_barcode_get_props (GL_LABEL_BARCODE(object), &style, &text_flag, &color, &scale); - string = g_strdup_printf ("0x%08x", color); - xmlSetProp (object_node, "color", string); - g_free (string); + gl_xml_set_prop_uint_hex (object_node, "color", color); xmlSetProp (object_node, "style", gl_barcode_style_to_text (style)); - xmlSetProp (object_node, "text", text_flag?"True":"False"); - string = g_strdup_printf ("%g", scale); - xmlSetProp (object_node, "scale", string); - g_free (string); + gl_xml_set_prop_boolean (object_node, "text", text_flag); + gl_xml_set_prop_double (object_node, "scale", scale); if (text_node->field_flag) { child = xmlNewChild (object_node, ns, "Field", NULL); diff --git a/glabels2/src/xml.c b/glabels2/src/xml.c index e6b7d53d..3dd99748 100644 --- a/glabels2/src/xml.c +++ b/glabels2/src/xml.c @@ -127,3 +127,55 @@ gl_xml_get_prop_uint_hex (xmlNodePtr node, } +/****************************************************************************/ +/* Set property from double. */ +/****************************************************************************/ +void gl_xml_set_prop_double (xmlNodePtr node, + const gchar *property, + gdouble val) +{ + gchar *string; + + string = g_strdup_printf ("%g", val); + xmlSetProp (node, property, string); + g_free (string); +} + +/****************************************************************************/ +/* Set property from boolean. */ +/****************************************************************************/ +void gl_xml_set_prop_boolean (xmlNodePtr node, + const gchar *property, + gboolean val) +{ + xmlSetProp (node, property, (val ? "True" : "False")); +} + +/****************************************************************************/ +/* Set property from int. */ +/****************************************************************************/ +void gl_xml_set_prop_int (xmlNodePtr node, + const gchar *property, + gint val) +{ + gchar *string; + + string = g_strdup_printf ("%d", val); + xmlSetProp (node, property, string); + g_free (string); +} + +/****************************************************************************/ +/* Set property from uint in hex. */ +/****************************************************************************/ +void gl_xml_set_prop_uint_hex (xmlNodePtr node, + const gchar *property, + guint val) +{ + gchar *string; + + string = g_strdup_printf ("0x%08x", val); + xmlSetProp (node, property, string); + g_free (string); +} + diff --git a/glabels2/src/xml.h b/glabels2/src/xml.h index c20581b6..ce69f796 100644 --- a/glabels2/src/xml.h +++ b/glabels2/src/xml.h @@ -43,6 +43,23 @@ guint gl_xml_get_prop_uint_hex (xmlNodePtr node, const gchar *property, guint default_val); + +void gl_xml_set_prop_double (xmlNodePtr node, + const gchar *property, + gdouble val); + +void gl_xml_set_prop_boolean (xmlNodePtr node, + const gchar *property, + gboolean val); + +void gl_xml_set_prop_int (xmlNodePtr node, + const gchar *property, + gint val); + +void gl_xml_set_prop_uint_hex (xmlNodePtr node, + const gchar *property, + guint val); + G_END_DECLS