From 206b483f611441d18db5360800975f2f0d35014b Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Wed, 5 May 2010 20:16:26 -0400 Subject: [PATCH] Added sanity tests to template parser When parsing a template node, if there is no recognized frame node (e.g. Label-rectangle), a default full-page frame with a 1x1 layout is created. Likewise, if a frame has no layout, a default 1x1 layout is created. This prevents potential crashes due to typos in the case of typos in hand generated templates. --- libglabels/str.c | 2 +- libglabels/xml-template.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/libglabels/str.c b/libglabels/str.c index b0b71452..f206b2f4 100644 --- a/libglabels/str.c +++ b/libglabels/str.c @@ -249,7 +249,7 @@ lgl_str_format_fraction (gdouble x) } if ( denom[i] == 1.0 ) { /* Simple integer. */ - return g_strdup_printf ("%.0g", x); + return g_strdup_printf ("%.0f", x); } n = (gint)( x * denom[i] + 0.5 ); d = (gint)denom[i]; diff --git a/libglabels/xml-template.c b/libglabels/xml-template.c index 50ae1329..9a2c3c0d 100644 --- a/libglabels/xml-template.c +++ b/libglabels/xml-template.c @@ -201,6 +201,8 @@ lgl_xml_template_parse_template_node (const xmlNodePtr template_node) lglTemplate *template; xmlNodePtr node; gchar **v; + lglTemplateFrame *frame; + brand = lgl_xml_get_prop_string (template_node, "brand", NULL); part = lgl_xml_get_prop_string (template_node, "part", NULL); @@ -305,6 +307,27 @@ lgl_xml_template_parse_template_node (const xmlNodePtr template_node) g_free (description); g_free (paper_id); + /* + * Create a default full-page frame, if a known frame type was not found. + */ + if ( template->frames == NULL ) + { + g_message ("%s %s: missing valid frame node", template->brand, template->part); + frame = lgl_template_frame_rect_new ("0", page_width, page_height, 0, 0, 0); + lgl_template_frame_add_layout (frame, lgl_template_layout_new (1, 1, 0, 0, 0, 0)); + lgl_template_add_frame (template, frame); + } + + /* + * Create a default 1x1 layout, if layout is missing. + */ + frame = (lglTemplateFrame *)template->frames->data; + if ( frame->all.layouts == NULL ) + { + g_message ("%s %s: missing layout node", template->brand, template->part); + lgl_template_frame_add_layout (frame, lgl_template_layout_new (1, 1, 0, 0, 0, 0)); + } + return template; } -- 2.39.5