From: Jim Evins Date: Wed, 8 Oct 2003 01:11:58 +0000 (+0000) Subject: Applied patch to correct conversion errors when reading files for many non-"C" locale... X-Git-Tag: glabels-2_3_0~585 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=72a09e53f6f8af5ed7d754c4f9ebabf4a28e919a;p=glabels Applied patch to correct conversion errors when reading files for many non-"C" locales, i.e. locales where commas are used as decimal points. Provided by Emmanuel Pacaud (emmanuelp@sourceforge.net). Additionally modified, to allow locale format for in xml files to be honored and to honor spaces between value and units. All values written by glabels are now guaranteed to be in the "C" locale. git-svn-id: https://glabels.svn.sourceforge.net/svnroot/glabels/trunk@305 f5e0f49d-192f-0410-a22d-a8d8700d0965 --- diff --git a/glabels2/src/xml.c b/glabels2/src/xml.c index 8e5591bf..b8bcd744 100644 --- a/glabels2/src/xml.c +++ b/glabels2/src/xml.c @@ -162,38 +162,35 @@ gl_xml_get_prop_length (xmlNodePtr node, gdouble default_val) { gdouble val; - gchar *string, units[65]; - gint n, i; + gchar *string, *unit; + gint i; string = xmlGetProp (node, property); if ( string != NULL ) { - n = sscanf (string, "%lf%64s", &val, units); - g_free (string); - - switch (n) { - case 1: - break; + val = g_strtod (string, &unit); - case 2: - for (i=0; unit_table[i].name != NULL; i++) { - if (xmlStrcasecmp (units, unit_table[i].name) == 0) { - val *= unit_table[i].points_per_unit; - break; + if (unit != string) { + unit = g_strchug (unit); + if (strlen (unit) > 0 ) { + for (i=0; unit_table[i].name != NULL; i++) { + if (xmlStrcasecmp (unit, unit_table[i].name) == 0) { + val *= unit_table[i].points_per_unit; + break; + } + } + if (unit_table[i].name == NULL) { + g_warning ("Line %d, Node \"%s\", Property \"%s\": Unknown unit \"%s\", assuming points", + xmlGetLineNo (node), node->name, property, + unit); } } - if (unit_table[i].name == NULL) { - g_warning ("Line %d, Node \"%s\", Property \"%s\": Unknown units \"%s\", assuming points", - xmlGetLineNo (node), node->name, property, - units); - } - break; - - default: + } + else { val = 0.0; - break; - } + + g_free (string); return val; } @@ -208,11 +205,12 @@ gl_xml_set_prop_double (xmlNodePtr node, const gchar *property, gdouble val) { - gchar *string; + gchar *string, buffer[G_ASCII_DTOSTR_BUF_SIZE]; + + /* Guarantee "C" locale by use of g_ascii_formatd */ + string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val); - string = g_strdup_printf ("%g", val); xmlSetProp (node, property, string); - g_free (string); } /****************************************************************************/ @@ -264,10 +262,14 @@ gl_xml_set_prop_length (xmlNodePtr node, const gchar *property, gdouble val) { - gchar *string; + gchar *string, buffer[G_ASCII_DTOSTR_BUF_SIZE]; + gchar *string_unit; - string = g_strdup_printf ("%gpt", val); - xmlSetProp (node, property, string); - g_free (string); + /* Guarantee "C" locale by use of g_ascii_formatd */ + string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val); + + string_unit = g_strdup_printf ("%spt", string); + xmlSetProp (node, property, string_unit); + g_free (string_unit); }