Re: setlocale


Subject: Re: setlocale
From: Mike Nordell (tamlin@algonet.se)
Date: Tue Apr 17 2001 - 15:55:09 CDT


WJCarpenter wrote:
> dom> setlocale(LC_NUMERIC, "C"); // set locale to C sprintf(buf,
> dom> "%.4f", float_foo); setlocale(LC_NUMERIC, ""); // reset locale
>
> Now *that* is my idea of a good place for a macro.

I disagree. This is a good place to add a new function along the following
lines. Avoid macros at all costs I say.

//
// From the top of my head, might not compile
//
class LocaleRAII {
public:
    LocaleRAII() { setlocale(LC_NUMERIC, "C"); }
    ~LocaleRAII() { setlocale(LC_NUMERIC, ""); }
};

void fmt_numeric(char* psz, const char* fmt, ...)
{
    va_list mark;
    va_start(marker, fmt);

    // will reset locale even if function is exited by exception
    LocaleRAII holder;

    vsprintf(psz, fmt, marker);
    va_end(mark);
}

Not that we use exceptions, but since its good design it doesn't hurt to add
the RAII class (I wouldn't name it XxxRAII, it was just to display a the
concept).

Just my $0.02

/Mike



This archive was generated by hypermail 2b25 : Tue Apr 17 2001 - 15:55:16 CDT