iostream changes locale to format a number
Paolo Carlini
pcarlini@unitus.it
Fri May 3 05:31:00 GMT 2002
Paolo Carlini wrote:
> Are you willing to contribute a patch against the current mainline
> libstdc++-v3 sources for public discussion (that setlocale ...
> setlocale pattern is present in quite a few different places) ?
Let me point out, however, that, assuming your analysis is correct (I
believe is) the solution cannot be as simple as you seems to assume: the
string returned by the first setlocale must be saved! See the following
excerpts from glibc2.2.5 docs:
...
To be sure you can use the returned string encoding the currently
selected locale at a later time, you must make a copy of the
string. It is not guaranteed that the returned pointer remains
valid over time.
...
Here is an example showing how you might use `setlocale' to
temporarily switch to a new locale.
#include <stddef.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
void
with_other_locale (char *new_locale,
void (*subroutine) (int),
int argument)
{
char *old_locale, *saved_locale;
/* Get the name of the current locale. */
old_locale = setlocale (LC_ALL, NULL);
/* Copy the name so it won't be clobbered by `setlocale'. */
saved_locale = strdup (old_locale);
if (saved_locale == NULL)
fatal ("Out of memory");
/* Now change the locale and do some stuff with it. */
setlocale (LC_ALL, new_locale);
(*subroutine) (argument);
/* Restore the original locale. */
setlocale (LC_ALL, saved_locale);
free (saved_locale);
}
...
Ciao,
Paolo.
More information about the Libstdc++
mailing list