From 2c35dfca3a68d17e3f264bd97b3b520ea9837bee Mon Sep 17 00:00:00 2001 From: "nhotta@netscape.com" Date: Tue, 23 Nov 1999 21:51:26 +0000 Subject: [PATCH] Changed conversion between posix and xp locale to handle extra locale info correctly, bug 18338, r=tao. --- intl/locale/src/unix/nsPosixLocale.cpp | 136 +++++++++++++++---------- intl/locale/src/unix/nsPosixLocale.h | 2 +- 2 files changed, 84 insertions(+), 54 deletions(-) diff --git a/intl/locale/src/unix/nsPosixLocale.cpp b/intl/locale/src/unix/nsPosixLocale.cpp index 348eeef382b0..43d917c3c6e1 100644 --- a/intl/locale/src/unix/nsPosixLocale.cpp +++ b/intl/locale/src/unix/nsPosixLocale.cpp @@ -50,21 +50,33 @@ nsPosixLocale::GetPlatformLocale(const nsString* locale,char* posixLocale, size_ char* xp_locale; char country_code[3]; char lang_code[3]; - char region_code[3]; - char posix_locale[9]; + char extra[65]; + char posix_locale[128]; xp_locale = locale->ToNewCString(); if (xp_locale!=nsnull) { - if (!ParseLocaleString(xp_locale,lang_code,country_code,region_code,'-')) { - strncpy(posixLocale,"C",length); + if (!ParseLocaleString(xp_locale,lang_code,country_code,extra,'-')) { +// strncpy(posixLocale,"C",length); + PL_strncpyz(posixLocale,xp_locale,length); // use xp locale if parse failed delete [] xp_locale; return NS_OK; } - if (country_code[0]==0) { - PR_snprintf(posix_locale,9,"%s%c",lang_code,0); - } else { - PR_snprintf(posix_locale,9,"%s_%s%c",lang_code,country_code,0); + if (*country_code) { + if (*extra) { + PR_snprintf(posix_locale,128,"%s_%s.%s",lang_code,country_code,extra); + } + else { + PR_snprintf(posix_locale,128,"%s_%s",lang_code,country_code); + } + } + else { + if (*extra) { + PR_snprintf(posix_locale,128,"%s.%s",lang_code,extra); + } + else { + PR_snprintf(posix_locale,128,"%s",lang_code); + } } strncpy(posixLocale,posix_locale,length); @@ -80,23 +92,35 @@ nsPosixLocale::GetXPLocale(const char* posixLocale, nsString* locale) { char country_code[3]; char lang_code[3]; - char region_code[3]; - char posix_locale[9]; + char extra[65]; + char posix_locale[128]; if (posixLocale!=nsnull) { if (strcmp(posixLocale,"C")==0 || strcmp(posixLocale,"POSIX")==0) { *locale = "en-US"; return NS_OK; } - if (!ParseLocaleString(posixLocale,lang_code,country_code,region_code,'_')) { - * locale = "x-user-defined"; + if (!ParseLocaleString(posixLocale,lang_code,country_code,extra,'_')) { +// * locale = "x-user-defined"; + locale->SetString(posixLocale); // use posix if parse failed return NS_OK; } - if (country_code[0]==0) { - PR_snprintf(posix_locale,9,"%s%c",lang_code,0); - } else { - PR_snprintf(posix_locale,9,"%s-%s%c",lang_code,country_code,0); + if (*country_code) { + if (*extra) { + PR_snprintf(posix_locale,128,"%s-%s.%s",lang_code,country_code,extra); + } + else { + PR_snprintf(posix_locale,128,"%s-%s",lang_code,country_code); + } + } + else { + if (*extra) { + PR_snprintf(posix_locale,128,"%s.%s",lang_code,extra); + } + else { + PR_snprintf(posix_locale,128,"%s",lang_code); + } } *locale = posix_locale; @@ -109,45 +133,51 @@ nsPosixLocale::GetXPLocale(const char* posixLocale, nsString* locale) } // -// returns PR_FALSE/PR_TRUE depending on if it was of the form LL-CC-RR +// returns PR_FALSE/PR_TRUE depending on if it was of the form LL-CC.Extra PRBool -nsPosixLocale::ParseLocaleString(const char* locale_string, char* language, char* country, char* region, char separator) +nsPosixLocale::ParseLocaleString(const char* locale_string, char* language, char* country, char* extra, char separator) { - size_t len; + PRUint32 len = PL_strlen(locale_string); - len = strlen(locale_string); - if (len==0 || (len!=2 && len!=5 && len!=8)) - return PR_FALSE; - - if (len==2) { - language[0]=locale_string[0]; - language[1]=locale_string[1]; - language[2]=0; - country[0]=0; - region[0]=0; - } else if (len==5) { - 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; - region[0]=0; - if (locale_string[2]!=separator) return PR_FALSE; - } else if (len==8) { - 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; - region[0]=locale_string[6]; - region[1]=locale_string[7]; - region[2]=0; - if (locale_string[2]!=separator || locale_string[5]!=separator) return PR_FALSE; - } else { - return PR_FALSE; - } + *language = '\0'; + *country = '\0'; + *extra = '\0'; - return PR_TRUE; + 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 ('.' == locale_string[2] && 4 <= len) { + PL_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 ('.' == locale_string[5] && 7 <= len) { + PL_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; + } + + return PR_TRUE; } diff --git a/intl/locale/src/unix/nsPosixLocale.h b/intl/locale/src/unix/nsPosixLocale.h index cb1baf78592a..8c2c5904236d 100644 --- a/intl/locale/src/unix/nsPosixLocale.h +++ b/intl/locale/src/unix/nsPosixLocale.h @@ -44,7 +44,7 @@ public: NS_IMETHOD GetXPLocale(const char* posixLocale, nsString* locale); protected: - inline PRBool ParseLocaleString(const char* locale_string, char* language, char* country, char* region, char separator); + inline PRBool ParseLocaleString(const char* locale_string, char* language, char* country, char* extra, char separator); };