r=mkaply, sr=blizzard, a=mkaply
Patch from Peter Weilbacher - use ParseLocaleString from nsPosixLocale and use the separator that is passed in
This commit is contained in:
mkaply%us.ibm.com 2004-10-11 16:35:13 +00:00
Родитель 65033a43f8
Коммит b42a56203a
2 изменённых файлов: 143 добавлений и 48 удалений

Просмотреть файл

@ -48,6 +48,10 @@
{ 0xf25f74f0, 0xfb59, 0x11d3, \
{ 0xa9, 0xf2, 0x0, 0x20, 0x35, 0x22, 0xa0, 0x3c }}
#define MAX_LANGUAGE_CODE_LEN 3
#define MAX_COUNTRY_CODE_LEN 3
#define MAX_EXTRA_LEN 65
#define MAX_LOCALE_LEN 128
class nsIOS2Locale : public nsISupports
{

Просмотреть файл

@ -112,10 +112,10 @@ nsOS2Locale::GetPlatformLocale(const nsAString& locale, PULONG os2Codepage)
NS_IMETHODIMP
nsOS2Locale::GetXPLocale(const char* os2Locale, nsAString& locale)
{
char country_code[3];
char lang_code[3];
char extra[65];
char os2_locale[128];
char country_code[MAX_COUNTRY_CODE_LEN];
char lang_code[MAX_LANGUAGE_CODE_LEN];
char extra[MAX_EXTRA_LEN];
char os2_locale[MAX_LOCALE_LEN];
if (os2Locale!=nsnull) {
if (strcmp(os2Locale,"C")==0 || strcmp(os2Locale,"OS2")==0) {
@ -131,18 +131,18 @@ nsOS2Locale::GetXPLocale(const char* os2Locale, nsAString& locale)
if (*country_code) {
if (*extra) {
PR_snprintf(os2_locale,128,"%s-%s.%s",lang_code,country_code,extra);
PR_snprintf(os2_locale,MAX_LOCALE_LEN,"%s-%s.%s",lang_code,country_code,extra);
}
else {
PR_snprintf(os2_locale,128,"%s-%s",lang_code,country_code);
PR_snprintf(os2_locale,MAX_LOCALE_LEN,"%s-%s",lang_code,country_code);
}
}
else {
if (*extra) {
PR_snprintf(os2_locale,128,"%s.%s",lang_code,extra);
PR_snprintf(os2_locale,MAX_LOCALE_LEN,"%s.%s",lang_code,extra);
}
else {
PR_snprintf(os2_locale,128,"%s",lang_code);
PR_snprintf(os2_locale,MAX_LOCALE_LEN,"%s",lang_code);
}
}
@ -151,56 +151,147 @@ nsOS2Locale::GetXPLocale(const char* os2Locale, nsAString& locale)
}
return NS_ERROR_FAILURE;
return NS_ERROR_FAILURE;
}
//
// copied from nsPosixLocale::ParseLocaleString:
// returns PR_FALSE/PR_TRUE depending on if it was of the form LL-CC.Extra
// or possibly ll_CC_Extra (depending on the separator, which happens on OS/2
PRBool
nsOS2Locale::ParseLocaleString(const char* locale_string, char* language, char* country, char* extra, char separator)
{
PRUint32 len = strlen(locale_string);
const char *src = locale_string;
char modifier[MAX_EXTRA_LEN+1];
char *dest;
int dest_space, len;
*language = '\0';
*country = '\0';
*extra = '\0';
if (2 == len) {
language[0]=locale_string[0];
language[1]=locale_string[1];
language[2]='\0';
country[0]='\0';
}
else if (5 == len) {
language[0]=locale_string[0];
language[1]=locale_string[1];
language[2]='\0';
country[0]=locale_string[3];
country[1]=locale_string[4];
country[2]='\0';
}
else if (4 <= len && '.' == locale_string[2]) {
strcpy(extra, &locale_string[3]);
language[0]=locale_string[0];
language[1]=locale_string[1];
language[2]='\0';
country[0]=locale_string[3];
country[1]=locale_string[4];
country[2]='\0';
}
else if (7 <= len && '.' == locale_string[5]) {
strcpy(extra, &locale_string[6]);
language[0]=locale_string[0];
language[1]=locale_string[1];
language[2]='\0';
country[0]=locale_string[3];
country[1]=locale_string[4];
country[2]='\0';
}
else {
return PR_FALSE;
if (strlen(locale_string) < 2) {
return(PR_FALSE);
}
return PR_TRUE;
//
// parse the language part
//
dest = language;
dest_space = MAX_LANGUAGE_CODE_LEN;
while ((*src) && (isalpha(*src)) && (dest_space--)) {
*dest++ = tolower(*src++);
}
*dest = '\0';
len = dest - language;
if ((len != 2) && (len != 3)) {
NS_ASSERTION((len == 2) || (len == 3), "language code too short");
NS_ASSERTION(len < 3, "reminder: verify we can handle 3+ character language code in all parts of the system; eg: language packs");
*language = '\0';
return(PR_FALSE);
}
// check if all done
if (*src == '\0') {
return(PR_TRUE);
}
if ((*src != '_') && (*src != '-') && (*src != '.') && (*src != '@')) {
NS_ASSERTION(isalpha(*src), "language code too long");
NS_ASSERTION(!isalpha(*src), "unexpected language/country separator");
*language = '\0';
return(PR_FALSE);
}
//
// parse the country part
//
if ((*src == '_') || (*src == '-')) {
src++;
dest = country;
dest_space = MAX_COUNTRY_CODE_LEN;
while ((*src) && (isalpha(*src)) && (dest_space--)) {
*dest++ = toupper(*src++);
}
*dest = '\0';
len = dest - country;
if (len != 2) {
NS_ASSERTION(len == 2, "unexpected country code length");
*language = '\0';
*country = '\0';
return(PR_FALSE);
}
}
// check if all done
if (*src == '\0') {
return(PR_TRUE);
}
if ((*src != '.') && (*src != '@') && (*src != separator)) {
NS_ASSERTION(isalpha(*src), "country code too long");
NS_ASSERTION(!isalpha(*src), "unexpected country/extra separator");
*language = '\0';
*country = '\0';
return(PR_FALSE);
}
//
// handle the extra part
//
if (*src == '.') {
src++; // move past the extra part separator
dest = extra;
dest_space = MAX_EXTRA_LEN;
while ((*src) && (*src != '@') && (dest_space--)) {
*dest++ = *src++;
}
*dest = '\0';
len = dest - extra;
if (len < 1) {
NS_ASSERTION(len > 0, "found country/extra separator but no extra code");
*language = '\0';
*country = '\0';
*extra = '\0';
return(PR_FALSE);
}
}
// check if all done
if (*src == '\0') {
return(PR_TRUE);
}
//
// handle the modifier part
//
if ((*src == '@') || (*src == separator)) {
src++; // move past the modifier separator
NS_ASSERTION(strcmp("euro",src) == 0, "found non euro modifier");
dest = modifier;
dest_space = MAX_EXTRA_LEN;
while ((*src) && (dest_space--)) {
*dest++ = *src++;
}
*dest = '\0';
len = dest - modifier;
if (len < 1) {
NS_ASSERTION(len > 0, "found modifier separator but no modifier code");
*language = '\0';
*country = '\0';
*extra = '\0';
*modifier = '\0';
return(PR_FALSE);
}
}
// check if all done
if (*src == '\0') {
return(PR_TRUE);
}
NS_ASSERTION(*src == '\0', "extra/modifier code too long");
*language = '\0';
*country = '\0';
*extra = '\0';
return(PR_FALSE);
}