Re: Disabling localisation for an app



On Thu, 2008-10-16 at 14:21 +0200, Søren Hauberg wrote:
> Hi,
>  I'm working on a GUI in a fairly large project, that (amongst other
> things) write numeric data to a file that gnuplot will read. What this
> boils down to is that I need
> 
>  printf ("%f", 1.7);
> 
> to give me "1.70000". However, if I include
> 
>  Gtk::Main kit(argc, argv);
> 
> in my application, the formating of numbers begin to depend on the
> value of the $LANG environment variable. Specifically, if I set
> LANG=da_DK.UTF-8 (default on my system), then the above-mentioned
> printf statement gives "1,70000" (with a comma instead of a dot). How
> can I (from within my application) enforce that this doesn't happen?

you need to ensure that a portable locale is used when using stdio
calls. my preferred method is this little class. Put one of this in any
scope where you do stdio (input or output) and the locale is switched
and then automatically restored when leaving the scope.

	struct LocaleGuard {
	    LocaleGuard (const char*);
	    ~LocaleGuard ();
	    const char* old;
	};

LocaleGuard::LocaleGuard (const char* str)
{
	old = strdup (setlocale (LC_NUMERIC, NULL));
	if (strcmp (old, str)) {
		setlocale (LC_NUMERIC, str);
	}
}

LocaleGuard::~LocaleGuard ()
{
	setlocale (LC_NUMERIC, old);
	free ((char*)old);
}







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