Re: locale affects (infects! ;) strtod()



On Sat, 20 Dec 1997, Tim Janik wrote:

> 
> hi folks,
> 
> while including GScanner into glib, i ran into an obscure
> problem ;(
> the strtod() function <stdlib.h> got unusable for the
> string->float conversion within the lexer code, because it
> doesn't recognize the `.' anymore:
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <gtk/gtk.h>
> 
> main (int argc, char *argv[])
> {
> 	gchar *number = "1.0";
> 	gchar *string;
> 	gfloat x;
> 	
> 	gtk_init (&argc, &argv);
> 
> 	x = strtod ("1.1", &string);
> 	if (string && *string)
> 	  printf ("strtod(\"%s\") failed at: `%s'\n", number, string);
> 	else
> 	  printf ("strtod(\"%s\") gave: %f\n", number, x);
> 	
> 	printf ("setting locale...\n");
> 	gtk_set_locale ();
> 	
> 	x = strtod ("1.1", &string);
> 	if (string && *string)
> 	  printf ("strtod(\"%s\") failed at: `%s'\n", number, string);
> 	else
> 	  printf ("strtod(\"%s\") gave: %f\n", number, x);
> }
> 
> now this little program gave me:
> 
> birgrave:bash<~>$ gcc main.c -lgtk -lgdk -lglib -lX11 -lm && ./a.out
> strtod("1.0") gave: 1.100000
> setting locale...
> strtod("1.0") failed at: `.1'
> birgrave:bash<~>$
> 
> if that's of interest, $LANG is set to `de_DE.88591' by default on
> my system.
> 
> if strtod() can't be used to parse _international_ *rc files, which 
> function should be used instead to do string->float conversions?
> 

ok, for all of you who didn't pay much attention to my first mail, it might
be of interest, that this also affects atof() as atof() can just be thought
off as
#define atof(nptr)               strtod(nptr, (char **)NULL)

now strtod() is only used in
gtk+/gtk/gtkgamma.c:1
gimp/plug-ins/gag-0.3/eval_expr.c:2
gimp/plug-ins/gimptcl/gtcl_misc.c:4

while atof() is used in
gtk+/gtk/gtkcolorsel.c:3
gimp/app/batch.c:1
gimp/app/brightness_contrast.c:2
gimp/app/channel_ops.c:2
gimp/app/color_balance.c:3
gimp/app/commands.c:1
gimp/app/convert.c:1
gimp/app/general.c:1
gimp/app/hue_saturation.c:3
gimp/app/levels.c:6
gimp/app/posterize.c:1
gimp/app/resize.c:2
gimp/app/text_tool.c:2
gimp/app/threshold.c:2

plus about 40 else source files of plugins.

so, for all of these places the conversion will be performed just
as if atol() has been used if the locale is different from "POSIX" or "C".
i *really* don't think this would have been the original intention of
the various programmers.
so now i think we either have to come up with some g_strtod() / g_atof()
functions, or skip locale support in gtk+ (which is unacceptable also).

> 
> ---
> ciaoTJ
> 

---
ciaoTJ




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]