michael@0: /* michael@0: * Copyright (c) 1999 michael@0: * Silicon Graphics Computer Systems, Inc. michael@0: * michael@0: * Copyright (c) 1999 michael@0: * Boris Fomitchev michael@0: * michael@0: * Written 2000 michael@0: * Anton Lapach michael@0: * michael@0: * This material is provided "as is", with absolutely no warranty expressed michael@0: * or implied. Any use is at your own risk. michael@0: * michael@0: * Permission to use or copy this software for any purpose is hereby granted michael@0: * without fee, provided the above notices are retained on all copies. michael@0: * Permission to modify the code and to distribute modified code is granted, michael@0: * provided the above notices are retained, and a notice that the code was michael@0: * modified is included with the above copyright notice. michael@0: * michael@0: */ michael@0: michael@0: #include michael@0: #if defined (_STLP_MSVC) || defined (__ICL) michael@0: # include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #if defined (_STLP_USE_SAFE_STRING_FUNCTIONS) michael@0: # define _STLP_STRCPY(D, DS, S) strcpy_s(D, DS, S) michael@0: # define _STLP_STRNCPY(D, DS, S, C) strncpy_s(D, DS, S, C) michael@0: # define _STLP_STRCAT(D, DS, S) strcat_s(D, DS, S) michael@0: #else michael@0: # define _STLP_STRCPY(D, DS, S) strcpy(D, S) michael@0: # define _STLP_STRNCPY(D, DS, S, C) strncpy(D, S, C) michael@0: # define _STLP_STRCAT(D, DS, S) strcat(D, S) michael@0: #endif michael@0: michael@0: #if defined (__cplusplus) michael@0: extern "C" { michael@0: #endif michael@0: michael@0: /* Framework functions */ michael@0: /* michael@0: locale :: "lang[_country[.code_page]]" michael@0: | ".code_page" michael@0: | "" michael@0: | NULL michael@0: michael@0: */ michael@0: michael@0: typedef struct _LOCALECONV { michael@0: const char* name; michael@0: const char* abbrev; michael@0: } LOCALECONV; michael@0: michael@0: #define MAX_LANG_LEN 64 /* max language name length */ michael@0: #define MAX_CTRY_LEN 64 /* max country name length */ michael@0: #define MAX_MODIFIER_LEN 0 /* max modifier name length - n/a */ michael@0: #define MAX_LC_LEN (MAX_LANG_LEN+MAX_CTRY_LEN+MAX_MODIFIER_LEN+3) michael@0: /* max entire locale string length */ michael@0: #define MAX_CP_LEN 5 /* max code page name length */ michael@0: michael@0: #if !defined (LANG_INVARIANT) michael@0: # define LANG_INVARIANT 0x7f michael@0: # define _STLP_LANG_INVARIANT_DEFINED michael@0: #endif michael@0: michael@0: #ifndef CP_UTF7 michael@0: # define CP_UTF7 65000 michael@0: #endif michael@0: michael@0: #ifndef CP_UTF8 michael@0: # define CP_UTF8 65001 michael@0: #endif michael@0: michael@0: #define INVARIANT_LCID MAKELCID(MAKELANGID(LANG_INVARIANT, SUBLANG_NEUTRAL), SORT_DEFAULT) michael@0: michael@0: static const char *_C_name = "C"; michael@0: michael@0: /* non-NLS language string table */ michael@0: static LOCALECONV __rg_language[] = { michael@0: {"american", "ENU"}, michael@0: {"american english", "ENU"}, michael@0: {"american-english", "ENU"}, michael@0: {"australian", "ENA"}, michael@0: {"belgian", "NLB"}, michael@0: {"canadian", "ENC"}, michael@0: {"chh", "ZHH"}, michael@0: {"chi", "ZHI"}, michael@0: {"chinese", "CHS"}, michael@0: {"chinese-hongkong", "ZHH"}, michael@0: {"chinese-simplified", "CHS"}, michael@0: {"chinese-singapore", "ZHI"}, michael@0: {"chinese-traditional", "CHT"}, michael@0: {"dutch-belgian", "NLB"}, michael@0: {"english-american", "ENU"}, michael@0: {"english-aus", "ENA"}, michael@0: {"english-belize", "ENL"}, michael@0: {"english-can", "ENC"}, michael@0: {"english-caribbean", "ENB"}, michael@0: {"english-ire", "ENI"}, michael@0: {"english-jamaica", "ENJ"}, michael@0: {"english-nz", "ENZ"}, michael@0: {"english-south africa", "ENS"}, michael@0: {"english-trinidad y tobago", "ENT"}, michael@0: {"english-uk", "ENG"}, michael@0: {"english-us", "ENU"}, michael@0: {"english-usa", "ENU"}, michael@0: {"french-belgian", "FRB"}, michael@0: {"french-canadian", "FRC"}, michael@0: {"french-luxembourg", "FRL"}, michael@0: {"french-swiss", "FRS"}, michael@0: {"german-austrian", "DEA"}, michael@0: {"german-lichtenstein", "DEC"}, michael@0: {"german-luxembourg", "DEL"}, michael@0: {"german-swiss", "DES"}, michael@0: {"irish-english", "ENI"}, michael@0: {"italian-swiss", "ITS"}, michael@0: {"norwegian", "NOR"}, michael@0: {"norwegian-bokmal", "NOR"}, michael@0: {"norwegian-nynorsk", "NON"}, michael@0: {"portuguese-brazilian", "PTB"}, michael@0: {"spanish-argentina", "ESS"}, michael@0: {"spanish-bolivia", "ESB"}, michael@0: {"spanish-chile", "ESL"}, michael@0: {"spanish-colombia", "ESO"}, michael@0: {"spanish-costa rica", "ESC"}, michael@0: {"spanish-dominican republic", "ESD"}, michael@0: {"spanish-ecuador", "ESF"}, michael@0: {"spanish-el salvador", "ESE"}, michael@0: {"spanish-guatemala", "ESG"}, michael@0: {"spanish-honduras", "ESH"}, michael@0: {"spanish-mexican", "ESM"}, michael@0: {"spanish-modern", "ESN"}, michael@0: {"spanish-nicaragua", "ESI"}, michael@0: {"spanish-panama", "ESA"}, michael@0: {"spanish-paraguay", "ESZ"}, michael@0: {"spanish-peru", "ESR"}, michael@0: {"spanish-puerto rico", "ESU"}, michael@0: {"spanish-uruguay", "ESY"}, michael@0: {"spanish-venezuela", "ESV"}, michael@0: {"swedish-finland", "SVF"}, michael@0: {"swiss", "DES"}, michael@0: {"uk", "ENG"}, michael@0: {"us", "ENU"}, michael@0: {"usa", "ENU"} michael@0: }; michael@0: michael@0: /* non-NLS country string table */ michael@0: static LOCALECONV __rg_country[] = { michael@0: {"america", "USA"}, michael@0: {"britain", "GBR"}, michael@0: {"china", "CHN"}, michael@0: {"czech", "CZE"}, michael@0: {"england", "GBR"}, michael@0: {"great britain", "GBR"}, michael@0: {"holland", "NLD"}, michael@0: {"hong-kong", "HKG"}, michael@0: {"new-zealand", "NZL"}, michael@0: {"nz", "NZL"}, michael@0: {"pr china", "CHN"}, michael@0: {"pr-china", "CHN"}, michael@0: {"puerto-rico", "PRI"}, michael@0: {"slovak", "SVK"}, michael@0: {"south africa", "ZAF"}, michael@0: {"south korea", "KOR"}, michael@0: {"south-africa", "ZAF"}, michael@0: {"south-korea", "KOR"}, michael@0: {"trinidad & tobago", "TTO"}, michael@0: {"uk", "GBR"}, michael@0: {"united-kingdom", "GBR"}, michael@0: {"united-states", "USA"}, michael@0: {"us", "USA"}, michael@0: }; michael@0: michael@0: typedef struct _Locale_name_hint { michael@0: LCID id; michael@0: } _Locale_lcid_t; michael@0: michael@0: typedef struct _Locale_ctype { michael@0: _Locale_lcid_t lc; michael@0: UINT cp; michael@0: unsigned short ctable[256]; michael@0: } _Locale_ctype_t; michael@0: michael@0: typedef struct _Locale_numeric { michael@0: _Locale_lcid_t lc; michael@0: char cp[MAX_CP_LEN + 1]; michael@0: char decimal_point[4]; michael@0: char thousands_sep[4]; michael@0: char *grouping; michael@0: } _Locale_numeric_t; michael@0: michael@0: typedef struct _Locale_time { michael@0: _Locale_lcid_t lc; michael@0: char cp[MAX_CP_LEN + 1]; michael@0: char *month[12]; michael@0: char *abbrev_month[12]; michael@0: char *dayofweek[7]; michael@0: char *abbrev_dayofweek[7]; michael@0: char *date_time_format; michael@0: char *long_date_time_format; michael@0: char *date_format; michael@0: char *long_date_format; michael@0: char *time_format; michael@0: char am[9]; michael@0: char pm[9]; michael@0: } _Locale_time_t; michael@0: michael@0: typedef struct _Locale_collate { michael@0: _Locale_lcid_t lc; michael@0: char cp[MAX_CP_LEN + 1]; michael@0: } _Locale_collate_t; michael@0: michael@0: typedef struct _Locale_monetary { michael@0: _Locale_lcid_t lc; michael@0: char cp[MAX_CP_LEN + 1]; michael@0: char decimal_point[4]; michael@0: char thousands_sep[4]; michael@0: char *grouping; michael@0: char int_curr_symbol[5]; /* 3 + 1 + 1 */ michael@0: char curr_symbol[6]; michael@0: char negative_sign[5]; michael@0: char positive_sign[5]; michael@0: int frac_digits; michael@0: int int_frac_digits; michael@0: } _Locale_monetary_t; michael@0: michael@0: /* Internal function */ michael@0: static void __FixGrouping(char *grouping); michael@0: static const char* __ConvertName(const char* lname, LOCALECONV* ConvTable, int TableSize); michael@0: static int __ParseLocaleString(const char* lname, char* lang, char* ctry, char* page); michael@0: static int __GetLCID(const char* lang, const char* ctry, LCID* lcid); michael@0: static int __GetLCIDFromName(const char* lname, LCID* lcid, char *cp, _Locale_lcid_t *hint); michael@0: static char const* __GetLocaleName(LCID lcid, const char* cp, char* buf); michael@0: static char const* __Extract_locale_name(const char* loc, const char* category, char* buf); michael@0: static char const* __TranslateToSystem(const char* lname, char* buf, _Locale_lcid_t* hint, int *__err_code); michael@0: static void __GetLocaleInfoUsingACP(LCID lcid, const char* cp, LCTYPE lctype, char* buf, int buf_size, wchar_t* wbuf, int wbuf_size); michael@0: static int __intGetACP(LCID lcid); michael@0: static int __intGetOCP(LCID lcid); michael@0: static int __GetDefaultCP(LCID lcid); michael@0: static char* __ConvertToCP(int from_cp, int to_cp, const char *from, size_t size, size_t *ret_buf_size); michael@0: static void my_ltoa(long __x, char* buf); michael@0: michael@0: void my_ltoa(long __x, char* buf) { michael@0: char rbuf[64]; michael@0: char* ptr = rbuf; michael@0: michael@0: if (__x == 0) michael@0: *ptr++ = '0'; michael@0: else { michael@0: for (; __x != 0; __x /= 10) michael@0: *ptr++ = (char)(__x % 10) + '0'; michael@0: } michael@0: while(ptr > rbuf) *buf++ = *--ptr; michael@0: /* psw */ michael@0: *buf = '\0'; michael@0: } michael@0: michael@0: #if defined (__cplusplus) michael@0: _STLP_BEGIN_NAMESPACE michael@0: extern "C" { michael@0: #endif michael@0: michael@0: _Locale_lcid_t* _Locale_get_ctype_hint(_Locale_ctype_t* ltype) michael@0: { return (ltype != 0) ? <ype->lc : 0; } michael@0: _Locale_lcid_t* _Locale_get_numeric_hint(_Locale_numeric_t* lnumeric) michael@0: { return (lnumeric != 0) ? &lnumeric->lc : 0; } michael@0: _Locale_lcid_t* _Locale_get_time_hint(_Locale_time_t* ltime) michael@0: { return (ltime != 0) ? <ime->lc : 0; } michael@0: _Locale_lcid_t* _Locale_get_collate_hint(_Locale_collate_t* lcollate) michael@0: { return (lcollate != 0) ? &lcollate->lc : 0; } michael@0: _Locale_lcid_t* _Locale_get_monetary_hint(_Locale_monetary_t* lmonetary) michael@0: { return (lmonetary != 0) ? &lmonetary->lc : 0; } michael@0: _Locale_lcid_t* _Locale_get_messages_hint(struct _Locale_messages* lmessages) { michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(lmessages) michael@0: return 0; michael@0: } michael@0: michael@0: #define MAP(x, y) if ((mask & x) != 0) ret |= (y) michael@0: unsigned short MapCtypeMask(unsigned short mask) { michael@0: unsigned short ret = 0; michael@0: MAP(C1_UPPER, _Locale_UPPER | _Locale_PRINT); michael@0: MAP(C1_LOWER, _Locale_LOWER | _Locale_PRINT); michael@0: MAP(C1_DIGIT, _Locale_DIGIT | _Locale_PRINT); michael@0: MAP(C1_SPACE, _Locale_SPACE | _Locale_PRINT); michael@0: MAP(C1_PUNCT, _Locale_PUNCT | _Locale_PRINT); michael@0: /* MAP(C1_BLANK, ?); */ michael@0: MAP(C1_XDIGIT, _Locale_XDIGIT | _Locale_PRINT); michael@0: MAP(C1_ALPHA, _Locale_ALPHA | _Locale_PRINT); michael@0: if ((mask & C1_CNTRL) != 0) { ret |= _Locale_CNTRL; ret &= ~_Locale_PRINT; } michael@0: return ret; michael@0: } michael@0: michael@0: static void MapCtypeMasks(unsigned short *cur, unsigned short *end) { michael@0: for (; cur != end; ++cur) { michael@0: *cur = MapCtypeMask(*cur); michael@0: } michael@0: } michael@0: michael@0: _Locale_ctype_t* _Locale_ctype_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) { michael@0: char cp_name[MAX_CP_LEN + 1]; michael@0: int NativeCP; michael@0: unsigned char Buffer[256]; michael@0: unsigned char *ptr; michael@0: CPINFO CPInfo; michael@0: int i; michael@0: wchar_t *wbuffer; michael@0: int BufferSize; michael@0: michael@0: _Locale_ctype_t *ltype = (_Locale_ctype_t*)malloc(sizeof(_Locale_ctype_t)); michael@0: michael@0: if (!ltype) { *__err_code = _STLP_LOC_NO_MEMORY; return ltype; } michael@0: memset(ltype, 0, sizeof(_Locale_ctype_t)); michael@0: michael@0: if (__GetLCIDFromName(name, <ype->lc.id, cp_name, lc_hint) == -1) michael@0: { free(ltype); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; } michael@0: michael@0: #if defined (__BORLANDC__) michael@0: if ( ltype->lc.id == INVARIANT_LCID && name[0] == 'C' && name[1] == 0 ) michael@0: { ltype->lc.id = 0x409; } michael@0: #endif michael@0: michael@0: ltype->cp = atoi(cp_name); michael@0: michael@0: NativeCP = __GetDefaultCP(ltype->lc.id); michael@0: michael@0: /* Make table with all characters. */ michael@0: for (i = 0; i < 256; ++i) Buffer[i] = (unsigned char)i; michael@0: michael@0: if (!GetCPInfo(NativeCP, &CPInfo)) { free(ltype); return NULL; } michael@0: michael@0: if (CPInfo.MaxCharSize > 1) { michael@0: for (ptr = (unsigned char*)CPInfo.LeadByte; *ptr && *(ptr + 1); ptr+=2) michael@0: for (i = *ptr; i <= *(ptr + 1); ++i) Buffer[i] = 0; michael@0: } michael@0: michael@0: if ((UINT)NativeCP != ltype->cp) { michael@0: OSVERSIONINFO ver_info; michael@0: ver_info.dwOSVersionInfoSize = sizeof(ver_info); michael@0: GetVersionEx(&ver_info); michael@0: if (ver_info.dwPlatformId == VER_PLATFORM_WIN32_NT) { michael@0: /* Convert character sequence to Unicode. */ michael@0: BufferSize = MultiByteToWideChar(ltype->cp, MB_PRECOMPOSED, (const char*)Buffer, 256, NULL, 0); michael@0: if (!BufferSize) { free(ltype); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; } michael@0: wbuffer = (wchar_t*)malloc(BufferSize * sizeof(wchar_t)); michael@0: if (!wbuffer) { free(ltype); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: MultiByteToWideChar(ltype->cp, MB_PRECOMPOSED, (const char*)Buffer, 256, wbuffer, BufferSize); michael@0: michael@0: GetStringTypeW(CT_CTYPE1, wbuffer, 256, ltype->ctable); michael@0: MapCtypeMasks(ltype->ctable, ltype->ctable + 256); michael@0: free(wbuffer); michael@0: } michael@0: else { michael@0: unsigned short ctable[256]; michael@0: unsigned char TargetBuffer[256]; michael@0: GetStringTypeA(ltype->lc.id, CT_CTYPE1, (const char*)Buffer, 256, ctable); michael@0: michael@0: /* Convert character sequence to target code page. */ michael@0: BufferSize = MultiByteToWideChar(NativeCP, MB_PRECOMPOSED, (const char*)Buffer, 256, NULL, 0); michael@0: if (!BufferSize) { free(ltype); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; } michael@0: wbuffer = (wchar_t*)malloc(BufferSize * sizeof(wchar_t)); michael@0: if (!wbuffer) { free(ltype); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: MultiByteToWideChar(NativeCP, MB_PRECOMPOSED, (const char*)Buffer, 256, wbuffer, BufferSize); michael@0: if (!WideCharToMultiByte(ltype->cp, WC_COMPOSITECHECK | WC_SEPCHARS, wbuffer, BufferSize, (char*)TargetBuffer, 256, NULL, FALSE)) michael@0: { free(wbuffer); free(ltype); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; } michael@0: michael@0: free(wbuffer); michael@0: michael@0: /* Translate ctype table. */ michael@0: for (i = 0; i < 256; ++i) { michael@0: if (!TargetBuffer[i]) continue; michael@0: ltype->ctable[TargetBuffer[i]] = MapCtypeMask(ctable[i]); michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: GetStringTypeA(ltype->lc.id, CT_CTYPE1, (const char*)Buffer, 256, ltype->ctable); michael@0: MapCtypeMasks(ltype->ctable, ltype->ctable + 256); michael@0: } michael@0: return ltype; michael@0: } michael@0: michael@0: _Locale_numeric_t* _Locale_numeric_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) { michael@0: wchar_t wbuf[4]; michael@0: char *GroupingBuffer; michael@0: int BufferSize; michael@0: michael@0: _Locale_numeric_t *lnum = (_Locale_numeric_t*)malloc(sizeof(_Locale_numeric_t)); michael@0: if (!lnum) { *__err_code = _STLP_LOC_NO_MEMORY; return lnum; } michael@0: memset(lnum, 0, sizeof(_Locale_numeric_t)); michael@0: michael@0: if (__GetLCIDFromName(name, &lnum->lc.id, lnum->cp, lc_hint) == -1) michael@0: { free(lnum); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; } michael@0: michael@0: #if defined (__BORLANDC__) michael@0: if (lnum->lc.id != INVARIANT_LCID) { michael@0: #endif michael@0: __GetLocaleInfoUsingACP(lnum->lc.id, lnum->cp, LOCALE_SDECIMAL, lnum->decimal_point, 4, wbuf, 4); michael@0: __GetLocaleInfoUsingACP(lnum->lc.id, lnum->cp, LOCALE_STHOUSAND, lnum->thousands_sep, 4, wbuf, 4); michael@0: #if defined (__BORLANDC__) michael@0: } michael@0: else michael@0: lnum->decimal_point[0] = '.'; michael@0: #endif michael@0: michael@0: if (lnum->lc.id != INVARIANT_LCID) { michael@0: BufferSize = GetLocaleInfoA(lnum->lc.id, LOCALE_SGROUPING, NULL, 0); michael@0: GroupingBuffer = (char*)malloc(BufferSize); michael@0: if (!GroupingBuffer) { free(lnum); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: GetLocaleInfoA(lnum->lc.id, LOCALE_SGROUPING, GroupingBuffer, BufferSize); michael@0: __FixGrouping(GroupingBuffer); michael@0: lnum->grouping = GroupingBuffer; michael@0: } michael@0: else { michael@0: lnum->grouping = (char*)malloc(1); michael@0: if (!lnum->grouping) { free(lnum); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: lnum->grouping[0] = 0; michael@0: } michael@0: michael@0: return lnum; michael@0: } michael@0: michael@0: static int __ConvertDate(const char *NTDate, char *buffer, int buf_size) { michael@0: /* This function will return an incomplete buffer if buffer is not long enough */ michael@0: const char *cur_char; michael@0: char *cur_output, *end_output; michael@0: michael@0: /* Correct time format. */ michael@0: cur_char = NTDate; michael@0: cur_output = buffer; michael@0: end_output = cur_output + buf_size; michael@0: buf_size = 0; michael@0: while (*cur_char) { michael@0: if (cur_output && (cur_output == end_output)) break; michael@0: switch (*cur_char) { michael@0: case 'd': michael@0: { michael@0: if (*(cur_char + 1) == 'd') { michael@0: if (cur_output && (cur_output + 2 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (*(cur_char + 2) == 'd') { michael@0: if (*(cur_char + 3) == 'd') { michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'A'; } michael@0: buf_size += 2; michael@0: cur_char += 3; michael@0: } michael@0: else { michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'a'; } michael@0: buf_size += 2; michael@0: cur_char += 2; michael@0: } michael@0: } michael@0: else { michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'd'; } michael@0: buf_size += 2; michael@0: cur_char++; michael@0: } michael@0: } michael@0: else { michael@0: if (cur_output && (cur_output + 3 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'd'; } michael@0: buf_size += 3; michael@0: } michael@0: } michael@0: break; michael@0: case 'M': michael@0: { michael@0: if (*(cur_char + 1) == 'M') { michael@0: if (cur_output && (cur_output + 2 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (*(cur_char + 2) == 'M') { michael@0: if (*(cur_char + 3) == 'M') { michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'B'; } michael@0: buf_size += 2; michael@0: cur_char += 3; michael@0: } michael@0: else { michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'b'; } michael@0: buf_size += 2; michael@0: cur_char += 2; michael@0: } michael@0: } michael@0: else { michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'm'; } michael@0: buf_size += 2; michael@0: cur_char++; michael@0: } michael@0: } michael@0: else { michael@0: if (cur_output && (cur_output + 3 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'm'; } michael@0: buf_size += 3; michael@0: } michael@0: } michael@0: break; michael@0: case 'y': michael@0: { michael@0: if (*(cur_char + 1) == 'y') { michael@0: if (cur_output && (cur_output + 2 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (*(cur_char + 2) == 'y' && *(cur_char + 3) == 'y') { michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'Y'; } michael@0: buf_size += 2; michael@0: cur_char += 3; michael@0: } michael@0: else { michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'y'; } michael@0: buf_size += 2; michael@0: cur_char++; michael@0: } michael@0: } michael@0: else { michael@0: if (cur_output && (cur_output + 3 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'y'; } michael@0: buf_size += 3; michael@0: } michael@0: } michael@0: break; michael@0: case '%': michael@0: { michael@0: if (cur_output && (cur_output + 2 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '%'; } michael@0: buf_size += 2; michael@0: } michael@0: break; michael@0: case '\'': michael@0: { michael@0: ++cur_char; michael@0: while (*cur_char != '\'' && *cur_char != 0 && (cur_output == NULL || cur_output != end_output)) { michael@0: if (cur_output) { *cur_output++ = *cur_char; } michael@0: ++cur_char; michael@0: buf_size += 1; michael@0: } michael@0: } michael@0: break; michael@0: default: michael@0: { michael@0: if (cur_output) { *(cur_output++) = *cur_char; } michael@0: buf_size += 1; michael@0: } michael@0: break; michael@0: } michael@0: if (*cur_char == 0) break; michael@0: ++cur_char; michael@0: } michael@0: michael@0: if (!cur_output || cur_output != end_output) { michael@0: if (cur_output) *cur_output = 0; michael@0: buf_size += 1; michael@0: } michael@0: else { michael@0: /* We trunc result */ michael@0: *(--cur_output) = 0; michael@0: } michael@0: michael@0: return buf_size; michael@0: } michael@0: michael@0: static int __ConvertTime(const char *NTTime, char *buffer, int buf_size) { michael@0: const char *cur_char; michael@0: char *cur_output, *end_output; michael@0: cur_char = NTTime; michael@0: cur_output = buffer; michael@0: end_output = cur_output + buf_size; michael@0: buf_size = 0; michael@0: while (*cur_char) { michael@0: switch(*cur_char) { michael@0: case 'h': michael@0: if (*(cur_char + 1) == 'h') { michael@0: if (cur_output && (cur_output + 2 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'I'; } michael@0: buf_size += 2; michael@0: ++cur_char; michael@0: } michael@0: else { michael@0: if (cur_output && (cur_output + 3 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'I'; } michael@0: buf_size += 3; michael@0: } michael@0: break; michael@0: case 'H': michael@0: if (*(cur_char + 1) == 'H') { michael@0: if (cur_output && (cur_output + 2 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'H'; } michael@0: buf_size += 2; michael@0: ++cur_char; michael@0: } michael@0: else { michael@0: if (cur_output && (cur_output + 3 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'H'; } michael@0: buf_size += 3; michael@0: } michael@0: break; michael@0: case 'm': michael@0: if (*(cur_char + 1) == 'm') { michael@0: if (cur_output && (cur_output + 2 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'M'; } michael@0: buf_size += 2; michael@0: cur_char++; michael@0: } michael@0: else { michael@0: if (cur_output && (cur_output + 3 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'M'; } michael@0: buf_size += 3; michael@0: } michael@0: break; michael@0: case 's': michael@0: if (*(cur_char + 1) == 's') { michael@0: if (cur_output && (cur_output + 2 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'S'; } michael@0: buf_size += 2; michael@0: ++cur_char; michael@0: } michael@0: else { michael@0: if (cur_output && (cur_output + 3 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'S'; } michael@0: buf_size += 3; michael@0: } michael@0: break; michael@0: case 't': michael@0: if (*(cur_char + 1) == 't') michael@0: ++cur_char; michael@0: if (cur_output && (cur_output + 2 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'p'; } michael@0: buf_size += 2; michael@0: break; michael@0: case '%': michael@0: if (cur_output && (cur_output + 2 > end_output)) { michael@0: *cur_output = 0; michael@0: return ++buf_size; michael@0: } michael@0: if (cur_output) { *(cur_output++)='%'; *(cur_output++)='%'; } michael@0: buf_size += 2; michael@0: break; michael@0: case '\'': michael@0: ++cur_char; michael@0: while (*cur_char != '\'' && *cur_char != 0 && (!cur_output || (cur_output != end_output))) { michael@0: if (cur_output) *cur_output++ = *cur_char; michael@0: ++cur_char; michael@0: buf_size += 1; michael@0: } michael@0: break; michael@0: default: michael@0: if (cur_output) { *(cur_output++) = *cur_char; } michael@0: buf_size += 1; michael@0: break; michael@0: } michael@0: if (*cur_char == 0) break; michael@0: ++cur_char; michael@0: } michael@0: michael@0: if (!cur_output || cur_output != end_output) { michael@0: if (cur_output) *cur_output = 0; michael@0: buf_size += 1; michael@0: } michael@0: else { michael@0: /* We trunc result */ michael@0: *(--cur_output) = 0; michael@0: } michael@0: michael@0: return buf_size; michael@0: } michael@0: michael@0: _Locale_time_t* _Locale_time_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) { michael@0: int size, month, dayofweek; michael@0: size_t length; michael@0: char fmt80[80]; michael@0: wchar_t wbuf80[80]; michael@0: michael@0: _Locale_time_t *ltime = (_Locale_time_t*)malloc(sizeof(_Locale_time_t)); michael@0: michael@0: if (!ltime) { *__err_code = _STLP_LOC_NO_MEMORY; return ltime; } michael@0: memset(ltime, 0, sizeof(_Locale_time_t)); michael@0: michael@0: if (__GetLCIDFromName(name, <ime->lc.id, ltime->cp, lc_hint) == -1) michael@0: { free(ltime); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; } michael@0: michael@0: #if defined (__BORLANDC__) michael@0: if ( ltime->lc.id == INVARIANT_LCID && name[0] == 'C' && name[1] == 0 ) michael@0: { ltime->lc.id = 0x409; } michael@0: #endif michael@0: michael@0: for (month = LOCALE_SMONTHNAME1; month <= LOCALE_SMONTHNAME12; ++month) { /* Small hack :-) */ michael@0: size = GetLocaleInfoA(ltime->lc.id, month, NULL, 0); michael@0: ltime->month[month - LOCALE_SMONTHNAME1] = (char*)malloc(size); michael@0: if (!ltime->month[month - LOCALE_SMONTHNAME1]) michael@0: { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, month, ltime->month[month - LOCALE_SMONTHNAME1], size, wbuf80, 80); michael@0: } michael@0: michael@0: for (month = LOCALE_SABBREVMONTHNAME1; month <= LOCALE_SABBREVMONTHNAME12; ++month) { michael@0: size = GetLocaleInfoA(ltime->lc.id, month, NULL, 0); michael@0: ltime->abbrev_month[month - LOCALE_SABBREVMONTHNAME1] = (char*)malloc(size); michael@0: if (!ltime->abbrev_month[month - LOCALE_SABBREVMONTHNAME1]) michael@0: { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, month, ltime->abbrev_month[month - LOCALE_SABBREVMONTHNAME1], size, wbuf80, 80); michael@0: } michael@0: michael@0: for (dayofweek = LOCALE_SDAYNAME1; dayofweek <= LOCALE_SDAYNAME7; ++dayofweek) { michael@0: int dayindex = ( dayofweek != LOCALE_SDAYNAME7 ) ? dayofweek - LOCALE_SDAYNAME1 + 1 : 0; michael@0: size = GetLocaleInfoA(ltime->lc.id, dayofweek, NULL, 0); michael@0: ltime->dayofweek[dayindex] = (char*)malloc(size); michael@0: if (!ltime->dayofweek[dayindex]) michael@0: { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, dayofweek, ltime->dayofweek[dayindex], size, wbuf80, 80); michael@0: } michael@0: michael@0: for (dayofweek = LOCALE_SABBREVDAYNAME1; dayofweek <= LOCALE_SABBREVDAYNAME7; ++dayofweek) { michael@0: int dayindex = ( dayofweek != LOCALE_SABBREVDAYNAME7 ) ? dayofweek - LOCALE_SABBREVDAYNAME1 + 1 : 0; michael@0: size = GetLocaleInfoA(ltime->lc.id, dayofweek, NULL, 0); michael@0: ltime->abbrev_dayofweek[dayindex] = (char*)malloc(size); michael@0: if (!ltime->abbrev_dayofweek[dayindex]) michael@0: { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, dayofweek, ltime->abbrev_dayofweek[dayindex], size, wbuf80, 80); michael@0: } michael@0: michael@0: __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_SSHORTDATE, fmt80, 80, wbuf80, 80); michael@0: size = __ConvertDate(fmt80, NULL, 0); michael@0: ltime->date_format = (char*)malloc(size); michael@0: if (!ltime->date_format) michael@0: { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: __ConvertDate(fmt80, ltime->date_format, size); michael@0: michael@0: __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_SLONGDATE, fmt80, 80, wbuf80, 80); michael@0: size = __ConvertDate(fmt80, NULL, 0); michael@0: ltime->long_date_format = (char*)malloc(size); michael@0: if (!ltime->long_date_format) michael@0: { _Locale_time_destroy(ltime);*__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: __ConvertDate(fmt80, ltime->long_date_format, size); michael@0: michael@0: __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_STIMEFORMAT, fmt80, 80, wbuf80, 80); michael@0: size = __ConvertTime(fmt80, NULL, 0); michael@0: ltime->time_format = (char*)malloc(size); michael@0: if (!ltime->time_format) michael@0: { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: __ConvertTime(fmt80, ltime->time_format, size); michael@0: michael@0: /* NT doesn't provide this information, we must simulate. */ michael@0: length = strlen(ltime->date_format) + strlen(ltime->time_format) + 1 /* space */ + 1 /* trailing 0 */; michael@0: ltime->date_time_format = (char*)malloc(length); michael@0: if (!ltime->date_time_format) michael@0: { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: _STLP_STRCPY(ltime->date_time_format, length, ltime->date_format); michael@0: _STLP_STRCAT(ltime->date_time_format, length, " "); michael@0: _STLP_STRCAT(ltime->date_time_format, length, ltime->time_format); michael@0: michael@0: /* NT doesn't provide this information, we must simulate. */ michael@0: length = strlen(ltime->long_date_format) + strlen(ltime->time_format) + 1 /* space */ + 1 /* trailing 0 */; michael@0: ltime->long_date_time_format = (char*)malloc(length); michael@0: if (!ltime->long_date_time_format) michael@0: { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; } michael@0: _STLP_STRCPY(ltime->long_date_time_format, length, ltime->long_date_format); michael@0: _STLP_STRCAT(ltime->long_date_time_format, length, " "); michael@0: _STLP_STRCAT(ltime->long_date_time_format, length, ltime->time_format); michael@0: michael@0: __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_S1159, ltime->am, 9, wbuf80, 80); michael@0: __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_S2359, ltime->pm, 9, wbuf80, 80); michael@0: michael@0: return ltime; michael@0: } michael@0: michael@0: _Locale_collate_t* _Locale_collate_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) { michael@0: _Locale_collate_t *lcol = (_Locale_collate_t*)malloc(sizeof(_Locale_collate_t)); michael@0: if (!lcol) { *__err_code = _STLP_LOC_NO_MEMORY; return lcol; } michael@0: memset(lcol, 0, sizeof(_Locale_collate_t)); michael@0: michael@0: if (__GetLCIDFromName(name, &lcol->lc.id, lcol->cp, lc_hint) == -1) michael@0: { free(lcol); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; } michael@0: michael@0: #if defined (__BORLANDC__) michael@0: if ( lcol->lc.id == INVARIANT_LCID && name[0] == 'C' && name[1] == 0 ) michael@0: { lcol->lc.id = 0x409; } michael@0: #endif michael@0: michael@0: return lcol; michael@0: } michael@0: michael@0: _Locale_monetary_t* _Locale_monetary_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) { michael@0: char *GroupingBuffer; michael@0: int BufferSize; michael@0: char FracDigits[3]; michael@0: wchar_t wbuf[6]; michael@0: michael@0: _Locale_monetary_t *lmon = (_Locale_monetary_t*)malloc(sizeof(_Locale_monetary_t)); michael@0: if (!lmon) { *__err_code = _STLP_LOC_NO_MEMORY; return lmon; } michael@0: memset(lmon, 0, sizeof(_Locale_monetary_t)); michael@0: michael@0: if (__GetLCIDFromName(name, &lmon->lc.id, lmon->cp, lc_hint) == -1) michael@0: { free(lmon); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; } michael@0: michael@0: if (lmon->lc.id != INVARIANT_LCID) { michael@0: /* Extract information about monetary system */ michael@0: __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SDECIMAL, lmon->decimal_point, 4, wbuf, 6); michael@0: __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_STHOUSAND, lmon->thousands_sep, 4, wbuf, 6); michael@0: michael@0: BufferSize = GetLocaleInfoA(lmon->lc.id, LOCALE_SGROUPING, NULL, 0); michael@0: GroupingBuffer = (char*)malloc(BufferSize); michael@0: if (!GroupingBuffer) michael@0: { lmon->grouping = NULL; *__err_code = _STLP_LOC_NO_MEMORY; return lmon; } michael@0: GetLocaleInfoA(lmon->lc.id, LOCALE_SGROUPING, GroupingBuffer, BufferSize); michael@0: __FixGrouping(GroupingBuffer); michael@0: lmon->grouping = GroupingBuffer; michael@0: michael@0: __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SCURRENCY, lmon->curr_symbol, 6, wbuf, 6); michael@0: __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SNEGATIVESIGN, lmon->negative_sign, 5, wbuf, 6); michael@0: __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SPOSITIVESIGN, lmon->positive_sign, 5, wbuf, 6); michael@0: michael@0: GetLocaleInfoA(lmon->lc.id, LOCALE_ICURRDIGITS, FracDigits, 3); michael@0: lmon->frac_digits = atoi(FracDigits); michael@0: michael@0: GetLocaleInfoA(lmon->lc.id, LOCALE_IINTLCURRDIGITS, FracDigits, 3); michael@0: lmon->int_frac_digits = atoi(FracDigits); michael@0: michael@0: __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SINTLSYMBOL, lmon->int_curr_symbol, 5, wbuf, 6); michael@0: /* Even if Platform SDK documentation says that the returned symbol should michael@0: * be a 3 letters symbol followed by a seperation character, experimentation michael@0: * has shown that no seperation character is ever appended. We are adding it michael@0: * ourself to conform to the POSIX specification. michael@0: */ michael@0: if (lmon->int_curr_symbol[3] == 0) { michael@0: lmon->int_curr_symbol[3] = ' '; michael@0: lmon->int_curr_symbol[4] = 0; michael@0: } michael@0: } michael@0: /* else it is already ok */ michael@0: michael@0: return lmon; michael@0: } michael@0: michael@0: struct _Locale_messages* _Locale_messages_create(const char *name, _Locale_lcid_t* lc_hint, int *__err_code) { michael@0: /* The Win32 API has no support for messages facet */ michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(name) michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(lc_hint) michael@0: *__err_code = _STLP_LOC_UNSUPPORTED_FACET_CATEGORY; michael@0: return NULL; michael@0: } michael@0: michael@0: static const char* _Locale_common_default(char* buf) { michael@0: char cp[MAX_CP_LEN + 1]; michael@0: int CodePage = __GetDefaultCP(LOCALE_USER_DEFAULT); michael@0: my_ltoa(CodePage, cp); michael@0: return __GetLocaleName(LOCALE_USER_DEFAULT, cp, buf); michael@0: } michael@0: michael@0: const char* _Locale_ctype_default(char* buf) michael@0: { return _Locale_common_default(buf); } michael@0: michael@0: const char* _Locale_numeric_default(char * buf) michael@0: { return _Locale_common_default(buf); } michael@0: michael@0: const char* _Locale_time_default(char* buf) michael@0: { return _Locale_common_default(buf); } michael@0: michael@0: const char* _Locale_collate_default(char* buf) michael@0: { return _Locale_common_default(buf); } michael@0: michael@0: const char* _Locale_monetary_default(char* buf) michael@0: { return _Locale_common_default(buf); } michael@0: michael@0: const char* _Locale_messages_default(char* buf) michael@0: { return _Locale_common_default(buf); } michael@0: michael@0: char const* _Locale_ctype_name(const _Locale_ctype_t* ltype, char* buf) { michael@0: char cp_buf[MAX_CP_LEN + 1]; michael@0: my_ltoa(ltype->cp, cp_buf); michael@0: return __GetLocaleName(ltype->lc.id, cp_buf, buf); michael@0: } michael@0: michael@0: char const* _Locale_numeric_name(const _Locale_numeric_t* lnum, char* buf) michael@0: { return __GetLocaleName(lnum->lc.id, lnum->cp, buf); } michael@0: michael@0: char const* _Locale_time_name(const _Locale_time_t* ltime, char* buf) michael@0: { return __GetLocaleName(ltime->lc.id, ltime->cp, buf); } michael@0: michael@0: char const* _Locale_collate_name(const _Locale_collate_t* lcol, char* buf) michael@0: { return __GetLocaleName(lcol->lc.id, lcol->cp, buf); } michael@0: michael@0: char const* _Locale_monetary_name(const _Locale_monetary_t* lmon, char* buf) michael@0: { return __GetLocaleName(lmon->lc.id, lmon->cp, buf); } michael@0: michael@0: char const* _Locale_messages_name(const struct _Locale_messages* lmes, char* buf) { michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(lmes) michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(buf) michael@0: return NULL; michael@0: } michael@0: michael@0: void _Locale_ctype_destroy(_Locale_ctype_t* ltype) { michael@0: if (!ltype) return; michael@0: michael@0: free(ltype); michael@0: } michael@0: michael@0: void _Locale_numeric_destroy(_Locale_numeric_t* lnum) { michael@0: if (!lnum) return; michael@0: michael@0: if (lnum->grouping) free(lnum->grouping); michael@0: free(lnum); michael@0: } michael@0: michael@0: void _Locale_time_destroy(_Locale_time_t* ltime) { michael@0: int i; michael@0: if (!ltime) return; michael@0: michael@0: for (i = 0; i < 12; ++i) { michael@0: if (ltime->month[i]) free(ltime->month[i]); michael@0: if (ltime->abbrev_month[i]) free(ltime->abbrev_month[i]); michael@0: } michael@0: michael@0: for (i = 0; i < 7; ++i) { michael@0: if (ltime->dayofweek[i]) free(ltime->dayofweek[i]); michael@0: if (ltime->abbrev_dayofweek[i]) free(ltime->abbrev_dayofweek[i]); michael@0: } michael@0: michael@0: if (ltime->date_format) free(ltime->date_format); michael@0: if (ltime->long_date_format) free(ltime->long_date_format); michael@0: if (ltime->time_format) free(ltime->time_format); michael@0: if (ltime->date_time_format) free(ltime->date_time_format); michael@0: if (ltime->long_date_time_format) free(ltime->long_date_time_format); michael@0: michael@0: free(ltime); michael@0: } michael@0: michael@0: void _Locale_collate_destroy(_Locale_collate_t* lcol) { michael@0: if (!lcol) return; michael@0: michael@0: free(lcol); michael@0: } michael@0: michael@0: void _Locale_monetary_destroy(_Locale_monetary_t* lmon) { michael@0: if (!lmon) return; michael@0: michael@0: if (lmon->grouping) free(lmon->grouping); michael@0: free(lmon); michael@0: } michael@0: michael@0: void _Locale_messages_destroy(struct _Locale_messages* lmes) michael@0: { _STLP_MARK_PARAMETER_AS_UNUSED(lmes) } michael@0: michael@0: static char const* _Locale_extract_category_name(const char* name, const char* category, char* buf, michael@0: _Locale_lcid_t* hint, int *__err_code) { michael@0: const char* cname = __Extract_locale_name(name, category, buf); michael@0: if (cname == 0 || (cname[0] == 'C' && cname[1] == 0)) { michael@0: return cname; michael@0: } michael@0: return __TranslateToSystem(cname, buf, hint, __err_code); michael@0: } michael@0: michael@0: char const* _Locale_extract_ctype_name(const char* cname, char* buf, michael@0: _Locale_lcid_t* hint, int *__err_code) michael@0: { return _Locale_extract_category_name(cname, "LC_CTYPE", buf, hint, __err_code); } michael@0: michael@0: char const* _Locale_extract_numeric_name(const char* cname, char* buf, michael@0: _Locale_lcid_t* hint, int *__err_code) michael@0: { return _Locale_extract_category_name(cname, "LC_NUMERIC", buf, hint, __err_code); } michael@0: michael@0: char const* _Locale_extract_time_name(const char* cname, char* buf, michael@0: _Locale_lcid_t* hint, int *__err_code) michael@0: { return _Locale_extract_category_name(cname, "LC_TIME", buf, hint, __err_code); } michael@0: michael@0: char const* _Locale_extract_collate_name(const char* cname, char* buf, michael@0: _Locale_lcid_t* hint, int *__err_code) michael@0: { return _Locale_extract_category_name(cname, "LC_COLLATE", buf, hint, __err_code); } michael@0: michael@0: char const* _Locale_extract_monetary_name(const char* cname, char* buf, michael@0: _Locale_lcid_t* hint, int *__err_code) michael@0: { return _Locale_extract_category_name(cname, "LC_MONETARY", buf, hint, __err_code); } michael@0: michael@0: char const* _Locale_extract_messages_name(const char* cname, char* buf, michael@0: _Locale_lcid_t* hint, int *__err_code) { michael@0: if (cname[0] == 'L' && cname[1] == 'C' && cname[2] == '_') { michael@0: return _C_name; michael@0: } michael@0: if (cname[0] == 'C' && cname[1] == 0) { michael@0: return _C_name; michael@0: } michael@0: return __TranslateToSystem(cname, buf, hint, __err_code); michael@0: } michael@0: michael@0: /* ctype */ michael@0: michael@0: const _Locale_mask_t* _Locale_ctype_table(_Locale_ctype_t* ltype) { michael@0: _STLP_STATIC_ASSERT(sizeof(_Locale_mask_t) == sizeof(ltype->ctable[0])) michael@0: return (const _Locale_mask_t*)ltype->ctable; michael@0: } michael@0: michael@0: int _Locale_toupper(_Locale_ctype_t* ltype, int c) { michael@0: char buf[2], out_buf[2]; michael@0: buf[0] = (char)c; buf[1] = 0; michael@0: if ((UINT)__GetDefaultCP(ltype->lc.id) == ltype->cp) { michael@0: LCMapStringA(ltype->lc.id, LCMAP_LINGUISTIC_CASING | LCMAP_UPPERCASE, buf, 2, out_buf, 2); michael@0: return out_buf[0]; michael@0: } michael@0: else { michael@0: wchar_t wbuf[2]; michael@0: MultiByteToWideChar(ltype->cp, MB_PRECOMPOSED, buf, 2, wbuf, 2); michael@0: WideCharToMultiByte(__GetDefaultCP(ltype->lc.id), WC_COMPOSITECHECK | WC_SEPCHARS, wbuf, 2, buf, 2, NULL, FALSE); michael@0: michael@0: LCMapStringA(ltype->lc.id, LCMAP_LINGUISTIC_CASING | LCMAP_UPPERCASE, buf, 2, out_buf, 2); michael@0: michael@0: MultiByteToWideChar(__GetDefaultCP(ltype->lc.id), MB_PRECOMPOSED, out_buf, 2, wbuf, 2); michael@0: WideCharToMultiByte(ltype->cp, WC_COMPOSITECHECK | WC_SEPCHARS, wbuf, 2, out_buf, 2, NULL, FALSE); michael@0: return out_buf[0]; michael@0: } michael@0: } michael@0: michael@0: int _Locale_tolower(_Locale_ctype_t* ltype, int c) { michael@0: char buf[2], out_buf[2]; michael@0: buf[0] = (char)c; buf[1] = 0; michael@0: if ((UINT)__GetDefaultCP(ltype->lc.id) == ltype->cp) { michael@0: LCMapStringA(ltype->lc.id, LCMAP_LINGUISTIC_CASING | LCMAP_LOWERCASE, buf, 2, out_buf, 2); michael@0: return out_buf[0]; michael@0: } michael@0: else { michael@0: wchar_t wbuf[2]; michael@0: MultiByteToWideChar(ltype->cp, MB_PRECOMPOSED, buf, 2, wbuf, 2); michael@0: WideCharToMultiByte(__GetDefaultCP(ltype->lc.id), WC_COMPOSITECHECK | WC_SEPCHARS, wbuf, 2, buf, 2, NULL, FALSE); michael@0: michael@0: LCMapStringA(ltype->lc.id, LCMAP_LINGUISTIC_CASING | LCMAP_LOWERCASE, buf, 2, out_buf, 2); michael@0: michael@0: MultiByteToWideChar(__GetDefaultCP(ltype->lc.id), MB_PRECOMPOSED, out_buf, 2, wbuf, 2); michael@0: WideCharToMultiByte(ltype->cp, WC_COMPOSITECHECK | WC_SEPCHARS, wbuf, 2, out_buf, 2, NULL, FALSE); michael@0: return out_buf[0]; michael@0: } michael@0: } michael@0: michael@0: #ifndef CSTR_EQUAL /* VC5SP3*/ michael@0: # define CSTR_EQUAL 2 michael@0: #endif michael@0: #ifndef CSTR_LESS_THAN /* VC5SP3 */ michael@0: # define CSTR_LESS_THAN 1 michael@0: #endif michael@0: michael@0: static DWORD max_DWORD = 0xffffffff; michael@0: static DWORD trim_size_t_to_DWORD(size_t n) { return n < (size_t)max_DWORD ? (DWORD)n : max_DWORD; } michael@0: michael@0: /* Collate */ michael@0: /* This function takes care of the potential size_t DWORD different size. */ michael@0: static int _Locale_strcmp_auxA(_Locale_collate_t* lcol, michael@0: const char* s1, size_t n1, michael@0: const char* s2, size_t n2) { michael@0: int result = CSTR_EQUAL; michael@0: while (n1 > 0 || n2 > 0) { michael@0: DWORD size1 = trim_size_t_to_DWORD(n1); michael@0: DWORD size2 = trim_size_t_to_DWORD(n2); michael@0: result = CompareStringA(lcol->lc.id, 0, s1, size1, s2, size2); michael@0: if (result != CSTR_EQUAL) michael@0: break; michael@0: n1 -= size1; michael@0: n2 -= size2; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: int _Locale_strcmp(_Locale_collate_t* lcol, michael@0: const char* s1, size_t n1, michael@0: const char* s2, size_t n2) { michael@0: int result; michael@0: if (__GetDefaultCP(lcol->lc.id) == atoi(lcol->cp)) { michael@0: result = _Locale_strcmp_auxA(lcol, s1, n1, s2, n2); michael@0: } michael@0: else { michael@0: char *buf1, *buf2; michael@0: size_t size1, size2; michael@0: buf1 = __ConvertToCP(atoi(lcol->cp), __GetDefaultCP(lcol->lc.id), s1, n1, &size1); michael@0: buf2 = __ConvertToCP(atoi(lcol->cp), __GetDefaultCP(lcol->lc.id), s2, n2, &size2); michael@0: michael@0: result = _Locale_strcmp_auxA(lcol, buf1, size1, buf2, size2); michael@0: free(buf1); free(buf2); michael@0: } michael@0: return (result == CSTR_EQUAL) ? 0 : (result == CSTR_LESS_THAN) ? -1 : 1; michael@0: } michael@0: michael@0: size_t _Locale_strxfrm(_Locale_collate_t* lcol, michael@0: char* dst, size_t dst_size, michael@0: const char* src, size_t src_size) { michael@0: int result; michael@0: michael@0: /* The Windows API do not support transformation of very long strings (src_size > INT_MAX) michael@0: * In this case the result will just be the input string: michael@0: */ michael@0: if (src_size > INT_MAX) { michael@0: if (dst != 0) { michael@0: _STLP_STRNCPY(dst, dst_size, src, src_size); michael@0: } michael@0: return src_size; michael@0: } michael@0: if (dst_size > INT_MAX) { michael@0: /* now that we know that src_size <= INT_MAX we can safely decrease dst_size to INT_MAX. */ michael@0: dst_size = INT_MAX; michael@0: } michael@0: michael@0: if (__GetDefaultCP(lcol->lc.id) == atoi(lcol->cp)) michael@0: result = LCMapStringA(lcol->lc.id, LCMAP_SORTKEY, src, (int)src_size, dst, (int)dst_size); michael@0: else { michael@0: char *buf; michael@0: size_t size; michael@0: buf = __ConvertToCP(atoi(lcol->cp), __GetDefaultCP(lcol->lc.id), src, src_size, &size); michael@0: michael@0: result = LCMapStringA(lcol->lc.id, LCMAP_SORTKEY, buf, (int)size, dst, (int)dst_size); michael@0: free(buf); michael@0: } michael@0: return result != 0 ? result - 1 : 0; michael@0: } michael@0: michael@0: /* Numeric */ michael@0: static const char* __true_name = "true"; michael@0: static const char* __false_name = "false"; michael@0: michael@0: char _Locale_decimal_point(_Locale_numeric_t* lnum) michael@0: { return lnum->decimal_point[0]; } michael@0: michael@0: char _Locale_thousands_sep(_Locale_numeric_t* lnum) michael@0: { return lnum->thousands_sep[0]; } michael@0: michael@0: const char* _Locale_grouping(_Locale_numeric_t * lnum) { michael@0: if (!lnum->grouping) return ""; michael@0: else return lnum->grouping; michael@0: } michael@0: michael@0: const char * _Locale_true(_Locale_numeric_t * lnum) { michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(lnum) michael@0: return __true_name; /* NT does't provide information about this */ michael@0: } michael@0: michael@0: const char * _Locale_false(_Locale_numeric_t * lnum) { michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(lnum) michael@0: return __false_name; /* NT does't provide information about this */ michael@0: } michael@0: michael@0: /* Monetary */ michael@0: const char* _Locale_int_curr_symbol(_Locale_monetary_t * lmon) michael@0: { return lmon->int_curr_symbol; } michael@0: michael@0: const char* _Locale_currency_symbol(_Locale_monetary_t * lmon) michael@0: { return lmon->curr_symbol; } michael@0: michael@0: char _Locale_mon_decimal_point(_Locale_monetary_t * lmon) michael@0: { return lmon->decimal_point[0]; } michael@0: michael@0: char _Locale_mon_thousands_sep(_Locale_monetary_t * lmon) michael@0: { return lmon->thousands_sep[0]; } michael@0: michael@0: const char* _Locale_mon_grouping(_Locale_monetary_t * lmon) { michael@0: if (!lmon->grouping) return ""; michael@0: else return lmon->grouping; michael@0: } michael@0: michael@0: const char* _Locale_positive_sign(_Locale_monetary_t * lmon) michael@0: { return lmon->positive_sign; } michael@0: michael@0: const char* _Locale_negative_sign(_Locale_monetary_t * lmon) michael@0: { return lmon->negative_sign; } michael@0: michael@0: char _Locale_int_frac_digits(_Locale_monetary_t * lmon) michael@0: { return (char)lmon->int_frac_digits; } michael@0: michael@0: char _Locale_frac_digits(_Locale_monetary_t * lmon) michael@0: { return (char)lmon->frac_digits; } michael@0: michael@0: int _Locale_p_cs_precedes(_Locale_monetary_t * lmon) { michael@0: char loc_data[2]; michael@0: GetLocaleInfoA(lmon->lc.id, LOCALE_IPOSSYMPRECEDES, loc_data, 2); michael@0: if (loc_data[0] == '0') return 0; michael@0: else if (loc_data[0] == '1') return 1; michael@0: else return -1; michael@0: } michael@0: michael@0: int _Locale_p_sep_by_space(_Locale_monetary_t * lmon) { michael@0: char loc_data[2]; michael@0: GetLocaleInfoA(lmon->lc.id, LOCALE_IPOSSEPBYSPACE, loc_data, 2); michael@0: if (loc_data[0] == '0') return 0; michael@0: else if (loc_data[0] == '1') return 1; michael@0: else return -1; michael@0: } michael@0: michael@0: int _Locale_p_sign_posn(_Locale_monetary_t * lmon) { michael@0: char loc_data[2]; michael@0: if (lmon->lc.id != INVARIANT_LCID) { michael@0: GetLocaleInfoA(lmon->lc.id, LOCALE_IPOSSIGNPOSN, loc_data, 2); michael@0: return atoi(loc_data); michael@0: } michael@0: else { michael@0: return CHAR_MAX; michael@0: } michael@0: } michael@0: michael@0: int _Locale_n_cs_precedes(_Locale_monetary_t * lmon) { michael@0: char loc_data[2]; michael@0: GetLocaleInfoA(lmon->lc.id, LOCALE_INEGSYMPRECEDES, loc_data, 2); michael@0: if (loc_data[0] == '0') return 0; michael@0: else if (loc_data[0] == '1') return 1; michael@0: else return -1; michael@0: } michael@0: michael@0: int _Locale_n_sep_by_space(_Locale_monetary_t * lmon) { michael@0: char loc_data[2]; michael@0: GetLocaleInfoA(lmon->lc.id, LOCALE_INEGSEPBYSPACE, loc_data, 2); michael@0: if (loc_data[0] == '0') return 0; michael@0: else if (loc_data[0] == '1') return 1; michael@0: else return -1; michael@0: } michael@0: michael@0: int _Locale_n_sign_posn(_Locale_monetary_t * lmon) { michael@0: char loc_data[2]; michael@0: if (lmon->lc.id != INVARIANT_LCID) { michael@0: GetLocaleInfoA(lmon->lc.id, LOCALE_INEGSIGNPOSN, loc_data, 2); michael@0: return atoi(loc_data); michael@0: } michael@0: else { michael@0: return CHAR_MAX; michael@0: } michael@0: } michael@0: michael@0: /* Time */ michael@0: const char * _Locale_full_monthname(_Locale_time_t * ltime, int month) { michael@0: const char **names = (const char**)ltime->month; michael@0: return names[month]; michael@0: } michael@0: michael@0: const char * _Locale_abbrev_monthname(_Locale_time_t * ltime, int month) { michael@0: const char **names = (const char**)ltime->abbrev_month; michael@0: return names[month]; michael@0: } michael@0: michael@0: const char * _Locale_full_dayofweek(_Locale_time_t * ltime, int day) { michael@0: const char **names = (const char**)ltime->dayofweek; michael@0: return names[day]; michael@0: } michael@0: michael@0: const char * _Locale_abbrev_dayofweek(_Locale_time_t * ltime, int day) { michael@0: const char **names = (const char**)ltime->abbrev_dayofweek; michael@0: return names[day]; michael@0: } michael@0: michael@0: const char* _Locale_d_t_fmt(_Locale_time_t* ltime) michael@0: { return ltime->date_time_format; } michael@0: michael@0: const char* _Locale_long_d_t_fmt(_Locale_time_t* ltime) michael@0: { return ltime->long_date_time_format; } michael@0: michael@0: const char* _Locale_d_fmt(_Locale_time_t* ltime) michael@0: { return ltime->date_format; } michael@0: michael@0: const char* _Locale_long_d_fmt(_Locale_time_t* ltime) michael@0: { return ltime->long_date_format; } michael@0: michael@0: const char* _Locale_t_fmt(_Locale_time_t* ltime) michael@0: { return ltime->time_format; } michael@0: michael@0: const char* _Locale_am_str(_Locale_time_t* ltime) michael@0: { return ltime->am; } michael@0: michael@0: const char* _Locale_pm_str(_Locale_time_t* ltime) michael@0: { return ltime->pm; } michael@0: michael@0: /* Messages */ michael@0: nl_catd_type _Locale_catopen(struct _Locale_messages* lmes, const char* cat_name) { michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(lmes) michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(cat_name) michael@0: return -1; michael@0: } michael@0: void _Locale_catclose(struct _Locale_messages* lmes, nl_catd_type cat) { michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(lmes) michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(&cat) michael@0: } michael@0: const char* _Locale_catgets(struct _Locale_messages* lmes, nl_catd_type cat, michael@0: int setid, int msgid, const char *dfault) { michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(lmes) michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(&cat) michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(&setid) michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(&msgid) michael@0: return dfault; michael@0: } michael@0: michael@0: #ifdef __cplusplus michael@0: } /* extern C */ michael@0: _STLP_END_NAMESPACE michael@0: #endif michael@0: michael@0: void __FixGrouping(char *grouping) { michael@0: /* This converts NT version which uses '0' instead of 0, etc ; to ANSI */ michael@0: char *g = grouping; michael@0: char building_group = 0; michael@0: char repeat_last = 0; michael@0: /* Check there is a grouping info otherwise we would add a useless CHAR_MAX */ michael@0: if (*g) { michael@0: for (; *g; ++g) { michael@0: if (*g > '0' && *g <= '9') { michael@0: if (!building_group) { michael@0: *grouping = *g - '0'; michael@0: building_group = 1; michael@0: } michael@0: else { michael@0: /* Known issue: grouping might roll. */ michael@0: *grouping = *grouping * 10 + *g - '0'; michael@0: } michael@0: } michael@0: else if (*g == '0') { michael@0: if (!building_group) { michael@0: repeat_last = 1; michael@0: } michael@0: else michael@0: /* Known issue: grouping might roll. */ michael@0: *grouping *= 10; michael@0: } michael@0: else if (*g == ';') { michael@0: /* Stop adding to the current group */ michael@0: building_group = 0; michael@0: ++grouping; michael@0: } michael@0: /* else we ignore the character */ michael@0: } michael@0: michael@0: if (!repeat_last) michael@0: *grouping++ = CHAR_MAX; michael@0: *grouping = 0; michael@0: } michael@0: } michael@0: michael@0: const char* __ConvertName(const char* lname, LOCALECONV* ConvTable, int TableSize) { michael@0: int i; michael@0: int cmp; michael@0: int low = 0; michael@0: int high = TableSize - 1; michael@0: michael@0: /* typical binary search - do until no more to search or match */ michael@0: while (low <= high) { michael@0: i = (low + high) / 2; michael@0: michael@0: if ((cmp = lstrcmpiA(lname, (*(ConvTable + i)).name)) == 0) michael@0: return (*(ConvTable + i)).abbrev; michael@0: else if (cmp < 0) michael@0: high = i - 1; michael@0: else michael@0: low = i + 1; michael@0: } michael@0: return lname; michael@0: } michael@0: michael@0: int __ParseLocaleString(const char* lname, michael@0: char* lang, char* ctry, char* page) { michael@0: int param = 0; michael@0: size_t len; michael@0: size_t tmpLen; michael@0: michael@0: if (lname[0] == 0) michael@0: return 0; michael@0: michael@0: /* We look for the first country separator '_' */ michael@0: len = strcspn(lname, "_"); michael@0: if (lname[len] == '_') { michael@0: if (len == 0 || len > MAX_LANG_LEN) return -1; /* empty lang is invalid*/ michael@0: _STLP_STRNCPY(lang, MAX_LANG_LEN + 1, lname, len); michael@0: lname += len + 1; michael@0: ++param; michael@0: } michael@0: michael@0: /* We look for the last code page separator '.' */ michael@0: len = -1; michael@0: tmpLen = strcspn(lname, "."); michael@0: while (lname[tmpLen] == '.') { michael@0: len = tmpLen; ++tmpLen; michael@0: tmpLen += strcspn(lname + tmpLen, "."); michael@0: } michael@0: if (len != -1) { /* Means that we found a '.' */ michael@0: if (param == 0) { michael@0: /* We have no lang yet so we have to fill it first, no country */ michael@0: if (len > MAX_LANG_LEN) return -1; michael@0: if (len == 0) { michael@0: /* No language nor country, only code page */ michael@0: ++param; michael@0: } michael@0: else michael@0: { _STLP_STRNCPY(lang, MAX_LANG_LEN + 1, lname, len); } michael@0: ++param; michael@0: } michael@0: else { michael@0: /* We already have a lang so we are now looking for the country: */ michael@0: if (len == 0) return -1; /* We forbid locale name with the "_." motif in it */ michael@0: if (len > MAX_CTRY_LEN) return -1; michael@0: _STLP_STRNCPY(ctry, MAX_CTRY_LEN + 1, lname, len); michael@0: } michael@0: ++param; michael@0: lname += len + 1; michael@0: } michael@0: michael@0: /* We look for ',' for compatibility with POSIX */ michael@0: len = strcspn(lname, ","); michael@0: switch (param) { michael@0: case 0: michael@0: if (len > MAX_LANG_LEN) return -1; michael@0: _STLP_STRNCPY(lang, MAX_LANG_LEN + 1, lname, len); michael@0: break; michael@0: case 1: michael@0: if (len > MAX_CTRY_LEN) return -1; michael@0: _STLP_STRNCPY(ctry, MAX_CTRY_LEN + 1, lname, len); michael@0: break; michael@0: default: michael@0: if (len > MAX_CP_LEN) return -1; michael@0: _STLP_STRNCPY(page, MAX_CP_LEN + 1, lname, len); michael@0: break; michael@0: } michael@0: michael@0: /* ',' POSIX modifier is not used in NT */ michael@0: return 0; michael@0: } michael@0: michael@0: /* Data necessary for find LCID*/ michael@0: static CRITICAL_SECTION __criticalSection; michael@0: static int __FindFlag; michael@0: static LCID __FndLCID; michael@0: static const char* __FndLang; michael@0: static const char* __FndCtry; michael@0: michael@0: void _Locale_init() michael@0: { InitializeCriticalSection(&__criticalSection); } michael@0: michael@0: void _Locale_final() michael@0: { DeleteCriticalSection(&__criticalSection); } michael@0: michael@0: static LCID LocaleFromHex(const char* locale) { michael@0: unsigned long result = 0; michael@0: int digit; michael@0: while (*locale) { michael@0: result <<= 4; michael@0: digit = (*locale >= '0' && *locale <= '9') ? *locale - '0': michael@0: (*locale >= 'A' && *locale <= 'F') ? (*locale - 'A') + 10 michael@0: : (*locale - 'a') + 10; michael@0: result += digit; michael@0: ++locale; michael@0: } michael@0: return (LCID)result; michael@0: } michael@0: michael@0: static BOOL CALLBACK EnumLocalesProcA(LPSTR locale) { michael@0: LCID lcid = LocaleFromHex(locale); michael@0: int LangFlag = 0, CtryFlag = !__FndCtry; michael@0: static char Lang[MAX_LANG_LEN], Ctry[MAX_CTRY_LEN]; michael@0: michael@0: GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE, Lang, MAX_LANG_LEN); michael@0: if (lstrcmpiA(Lang, __FndLang) != 0) { michael@0: GetLocaleInfoA(lcid, LOCALE_SABBREVLANGNAME, Lang, MAX_LANG_LEN); michael@0: if (lstrcmpiA(Lang, __FndLang) != 0) { michael@0: GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME, Lang, MAX_LANG_LEN); michael@0: if (lstrcmpiA(Lang, __FndLang) == 0) LangFlag = 1; michael@0: } michael@0: else LangFlag = 1; michael@0: } michael@0: else LangFlag = 1; michael@0: michael@0: if (__FndCtry) { michael@0: GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY, Ctry, MAX_CTRY_LEN); michael@0: if (lstrcmpiA(Ctry, __FndCtry) != 0) { michael@0: GetLocaleInfoA(lcid, LOCALE_SABBREVCTRYNAME, Ctry, MAX_CTRY_LEN); michael@0: if (lstrcmpiA(Ctry, __FndCtry) != 0) { michael@0: GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME, Ctry, MAX_CTRY_LEN); michael@0: if (lstrcmpiA(Ctry, __FndCtry) == 0) CtryFlag = 1; michael@0: } michael@0: else CtryFlag = 1; michael@0: } michael@0: else michael@0: CtryFlag = 1; michael@0: } michael@0: michael@0: if (LangFlag && CtryFlag) { michael@0: __FindFlag = 1; michael@0: __FndLCID = lcid; michael@0: return FALSE; michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: int __GetLCID(const char* lang, const char* ctry, LCID* lcid) { michael@0: int ret; michael@0: EnterCriticalSection(&__criticalSection); michael@0: michael@0: __FindFlag = 0; michael@0: __FndLang = lang; michael@0: __FndCtry = ctry; michael@0: EnumSystemLocalesA(EnumLocalesProcA, LCID_INSTALLED); michael@0: michael@0: if (__FindFlag != 0) *lcid = __FndLCID; michael@0: ret = __FindFlag != 0 ? 0 : -1; michael@0: michael@0: LeaveCriticalSection(&__criticalSection); michael@0: return ret; michael@0: } michael@0: michael@0: int __GetLCIDFromName(const char* lname, LCID* lcid, char* cp, _Locale_lcid_t *hint) { michael@0: char lang[MAX_LANG_LEN + 1], ctry[MAX_CTRY_LEN + 1], page[MAX_CP_LEN + 1]; michael@0: int result = 0; michael@0: if (lname == NULL || lname[0] == 0) { michael@0: *lcid = LOCALE_USER_DEFAULT; michael@0: return 0; michael@0: } michael@0: michael@0: memset(lang, 0, MAX_LANG_LEN + 1); michael@0: memset(ctry, 0, MAX_CTRY_LEN + 1); michael@0: memset(page, 0, MAX_CP_LEN + 1); michael@0: if (__ParseLocaleString(lname, lang, ctry, page) == -1) return -1; michael@0: michael@0: if (hint != 0) { michael@0: *lcid = hint->id; michael@0: } michael@0: else { michael@0: if (lang[0] == 0 && ctry[0] == 0) michael@0: *lcid = LOCALE_USER_DEFAULT; /* Only code page given. */ michael@0: else { michael@0: if (ctry[0] == 0) { michael@0: result = __GetLCID(__ConvertName(lang, __rg_language, sizeof(__rg_language) / sizeof(LOCALECONV)), NULL, lcid); michael@0: if (result != 0) { michael@0: /* Check 'C' special case. Check is done after call to __GetLCID because normal programs do not michael@0: * generate facet from 'C' name, they use the locale::classic() facets. */ michael@0: if (lang[0] == 'C' && lang[1] == 0) { michael@0: *lcid = INVARIANT_LCID; michael@0: result = 0; michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: result = __GetLCID(__ConvertName(lang, __rg_language, sizeof(__rg_language) / sizeof(LOCALECONV)), michael@0: __ConvertName(ctry, __rg_country, sizeof(__rg_country) / sizeof(LOCALECONV)), michael@0: lcid); michael@0: if (result != 0) { michael@0: /* Non NLS mapping might introduce problem with some locales when only one entry is mapped, michael@0: * the lang or the country (example: chinese locales like 'chinese_taiwan' gives 'CHS_taiwan' michael@0: * that do not exists in system). This is why we are giving this locale an other chance by michael@0: * calling __GetLCID without the mapping. */ michael@0: result = __GetLCID(lang, ctry, lcid); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (result == 0) { michael@0: /* Handling code page */ michael@0: if (lstrcmpiA(page, "ACP") == 0 || page[0] == 0) michael@0: my_ltoa(__intGetACP(*lcid), cp); michael@0: else if (lstrcmpiA(page, "OCP") == 0) michael@0: my_ltoa(__intGetOCP(*lcid), cp); michael@0: else if (lstrcmpiA(page, "UTF7") == 0) michael@0: my_ltoa(CP_UTF7, cp); michael@0: else if (lstrcmpiA(page, "UTF8") == 0) michael@0: my_ltoa(CP_UTF8, cp); michael@0: else michael@0: _STLP_STRNCPY(cp, MAX_CP_LEN + 1, page, 5); michael@0: michael@0: /* Code page must be an integer value, michael@0: * 0 returned by __intGetACP and 1 returned by __intGetOCP are invalid michael@0: * values. michael@0: */ michael@0: if (cp[1] == 0 && (cp[0] == '0' || cp[1] == '1')) michael@0: return -1; michael@0: else if (atoi(cp) == 0) michael@0: return -1; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: char const* __GetLocaleName(LCID lcid, const char* cp, char* buf) { michael@0: if (lcid == INVARIANT_LCID) { michael@0: return _C_name; michael@0: } michael@0: else { michael@0: char lang[MAX_LANG_LEN + 1], ctry[MAX_CTRY_LEN + 1]; michael@0: GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE, lang, MAX_LANG_LEN); michael@0: GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY, ctry, MAX_CTRY_LEN); michael@0: _STLP_STRCPY(buf, _Locale_MAX_SIMPLE_NAME, lang); michael@0: _STLP_STRCAT(buf, _Locale_MAX_SIMPLE_NAME, "_"); michael@0: _STLP_STRCAT(buf, _Locale_MAX_SIMPLE_NAME, ctry); michael@0: _STLP_STRCAT(buf, _Locale_MAX_SIMPLE_NAME, "."); michael@0: _STLP_STRCAT(buf, _Locale_MAX_SIMPLE_NAME, cp); michael@0: return buf; michael@0: } michael@0: } michael@0: michael@0: char const* __Extract_locale_name(const char* loc, const char* category, char* buf) { michael@0: char *expr; michael@0: size_t len_name; michael@0: michael@0: if (loc[0] == 'L' && loc[1] == 'C' && loc[2] == '_') { michael@0: expr = strstr((char*)loc, category); michael@0: if (expr == NULL) return NULL; /* Category not found. */ michael@0: expr = strchr(expr, '='); michael@0: if (expr == NULL) return NULL; michael@0: ++expr; michael@0: len_name = strcspn(expr, ";"); michael@0: len_name = len_name >= _Locale_MAX_SIMPLE_NAME ? _Locale_MAX_SIMPLE_NAME - 1 michael@0: : len_name; michael@0: _STLP_STRNCPY(buf, _Locale_MAX_SIMPLE_NAME, expr, len_name); buf[len_name] = 0; michael@0: return buf; michael@0: } michael@0: else { michael@0: return loc; michael@0: } michael@0: } michael@0: michael@0: char const* __TranslateToSystem(const char* lname, char* buf, _Locale_lcid_t* hint, michael@0: int *__err_code) { michael@0: LCID lcid; michael@0: char cp[MAX_CP_LEN + 1]; michael@0: if (__GetLCIDFromName(lname, &lcid, cp, hint) != 0) michael@0: { *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; } michael@0: michael@0: return __GetLocaleName(lcid, cp, buf); michael@0: } michael@0: michael@0: void __GetLocaleInfoUsingACP(LCID lcid, const char* cp, LCTYPE lctype, char* buf, int buf_size, wchar_t* wbuf, int wbuf_size) { michael@0: wchar_t *Buffer; michael@0: int BufferSize; michael@0: int icp; michael@0: michael@0: GetLocaleInfoA(lcid, lctype, buf, buf_size); michael@0: michael@0: icp = atoi(cp); michael@0: if (icp != CP_ACP && buf[0] != 0) { michael@0: BufferSize = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0); michael@0: if (BufferSize > wbuf_size) michael@0: { michael@0: Buffer = (wchar_t*)malloc(sizeof(wchar_t) * BufferSize); michael@0: } michael@0: else michael@0: { michael@0: Buffer = wbuf; michael@0: } michael@0: MultiByteToWideChar(CP_ACP, 0, buf, -1, Buffer, BufferSize); michael@0: WideCharToMultiByte(icp, 0, Buffer, -1, buf, buf_size, NULL, NULL); michael@0: if (Buffer != wbuf) michael@0: { michael@0: free(Buffer); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Return 0 if ANSI code page not used */ michael@0: int __intGetACP(LCID lcid) { michael@0: char cp[6]; michael@0: if (!GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE, cp, 6)) { michael@0: #if defined (_STLP_LANG_INVARIANT_DEFINED) michael@0: if (lcid == INVARIANT_LCID) { michael@0: /* We are using a limited PSDK, we rely on the most common code page */ michael@0: return 1252; michael@0: } michael@0: #endif michael@0: return 0; michael@0: } michael@0: return atoi(cp); michael@0: } michael@0: michael@0: /* Return 1 if OEM code page not used */ michael@0: int __intGetOCP(LCID lcid) { michael@0: char cp[6]; michael@0: if (!GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE, cp, 6)) michael@0: return 0; michael@0: return atoi(cp); michael@0: } michael@0: michael@0: int __GetDefaultCP(LCID lcid) { michael@0: int cp = __intGetACP(lcid); michael@0: if (cp == 0) return __intGetOCP(lcid); michael@0: else return cp; michael@0: } michael@0: michael@0: static int trim_size_t_to_int(size_t n) { return n < (size_t)INT_MAX ? (int)n : INT_MAX; } michael@0: michael@0: char* __ConvertToCP(int from_cp, int to_cp, const char *from, size_t size, size_t *ret_buf_size) { michael@0: size_t wbuffer_size, buffer_size, from_offset, wbuf_offset; michael@0: int from_size, to_size, wbuf_size; michael@0: wchar_t *wbuffer; michael@0: char* buffer; michael@0: michael@0: size_t orig_size = size; michael@0: michael@0: wbuffer_size = 0; michael@0: from_offset = 0; michael@0: while (size > 0) { michael@0: from_size = trim_size_t_to_int(size); michael@0: wbuffer_size += MultiByteToWideChar(from_cp, MB_PRECOMPOSED, michael@0: from + from_offset, from_size, NULL, 0); michael@0: from_offset += from_size; michael@0: size -= from_size; michael@0: } michael@0: michael@0: wbuffer = (wchar_t*)malloc(sizeof(wchar_t)*wbuffer_size); michael@0: michael@0: size = orig_size; michael@0: wbuf_offset = 0; michael@0: from_offset = 0; michael@0: while (size > 0) { michael@0: from_size = trim_size_t_to_int(size); michael@0: wbuf_size = trim_size_t_to_int(wbuffer_size - wbuf_offset); michael@0: wbuf_offset += MultiByteToWideChar(from_cp, MB_PRECOMPOSED, michael@0: from + from_offset, from_size, wbuffer + wbuf_offset, wbuf_size); michael@0: from_offset += from_size; michael@0: size -= from_size; michael@0: } michael@0: michael@0: buffer_size = 0; michael@0: wbuf_offset = 0; michael@0: size = wbuffer_size; michael@0: while (size > 0) { michael@0: wbuf_size = trim_size_t_to_int(size); michael@0: buffer_size += WideCharToMultiByte(to_cp, WC_COMPOSITECHECK | WC_SEPCHARS, michael@0: wbuffer + wbuf_offset, wbuf_size, michael@0: NULL, 0, NULL, FALSE); michael@0: wbuf_offset += wbuf_size; michael@0: size -= wbuf_size; michael@0: } michael@0: michael@0: buffer = (char*)malloc(buffer_size); michael@0: *ret_buf_size = buffer_size; michael@0: michael@0: size = wbuffer_size; michael@0: wbuf_offset = 0; michael@0: while (size > 0) { michael@0: wbuf_size = trim_size_t_to_int(size); michael@0: to_size = trim_size_t_to_int(buffer_size); michael@0: buffer_size -= WideCharToMultiByte(to_cp, WC_COMPOSITECHECK | WC_SEPCHARS, michael@0: wbuffer + wbuf_offset, wbuf_size, michael@0: buffer, to_size, NULL, FALSE); michael@0: wbuf_offset += wbuf_size; michael@0: size -= wbuf_size; michael@0: } michael@0: michael@0: free(wbuffer); michael@0: return buffer; michael@0: } michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: #ifndef _STLP_NO_WCHAR_T michael@0: # include "c_wlocale_win32.c" michael@0: #endif