This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
RE: [PATCH] codecvt<wchar_t, char, mbstate_t>
- From: Pétur Runólfsson <peturr02 at ru dot is>
- To: "Benjamin Kosnik" <bkoz at redhat dot com>
- Cc: <libstdc++ at gcc dot gnu dot org>
- Date: Sat, 8 Feb 2003 16:21:37 -0000
- Subject: RE: [PATCH] codecvt<wchar_t, char, mbstate_t>
Hi,
> Perhaps we need to scan the list of installed locales to find
> something with the needed charset (these tests only depend on
> the charset used by the locale, the language doesn't matter).
This seems to work, and doesn't depend on named locales working:
bool is_locale_available(const char* s)
{
bool ret = false;
const char* orig = std::setlocale(LC_ALL, NULL);
char* saved = new char[std::strlen(orig) + 1];
std::strcpy(saved, orig);
if (std::setlocale(LC_ALL, s))
ret = true;
std::setlocale(LC_ALL, saved);
delete [] saved;
return ret;
}
std::locale create_locale_from_charset(const char* s)
{
const char* const languages[] = {
"da_DK",
"de_DE",
"en_AU", "en_GB", "en_HK", "en_IE", "en_US",
"es_AR", "es_ES", "es_MX",
"fi_FI",
"fr_BE", "fr_CA", "fr_CH", "fr_FR", "fr_LU",
"is_IS",
"it_CH", "it_IT",
"ja_JP",
"nn_NO",
"no_NO",
"pt_BR", "pt_PT",
"ru_RU", "ru_UA",
"sv_FI", "sv_SE",
"zh_CN", "zh_HK", "zh_SG", "zh_TW"
};
int buflen = std::strlen("aa_AA.") +
std::strlen(s) +
std::strlen("@euro") + 1;
char* buffer = new char[buflen];
const int n = sizeof(languages) / sizeof(languages[0]);
for (int i = 0; i < n; ++i)
{
std::strcpy(buffer, languages[i]);
std::strcat(buffer, ".");
std::strcat(buffer, s);
if (!is_locale_available(buffer))
{
std::strcat(buffer, "@euro");
if (!is_locale_available(buffer))
continue;
}
std::locale loc (buffer);
delete [] buffer;
return loc;
}
delete [] buffer;
std::printf("Could not find any locale for charset %s, bailing out\n", s);
std::exit(1);
}
The codecvt testsuite can then use:
locale loc (create_locale_from_charset("ISO-8859-15"));
instead of
locale loc ("en_US.ISO-8859-15");
And for the rest of the testsuite, this generates a useful log
message when a locale needed by a testsuite is missing:
std::locale create_named_locale(const char* s)
{
if (!is_locale_available(s))
{
std::printf("Locale %s is not available, bailing out\n", s);
std::exit(1);
}
return std::locale(s);
}
Petur