2 * msvcrt.dll locale functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
41 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
42 #define MAX_LOCALE_LENGTH 256
43 MSVCRT__locale_t MSVCRT_locale = NULL;
44 unsigned short *MSVCRT__pctype = NULL;
45 unsigned int MSVCRT___lc_codepage = 0;
46 int MSVCRT___lc_collate_cp = 0;
47 LCID MSVCRT___lc_handle[MSVCRT_LC_MAX - MSVCRT_LC_MIN + 1] = { 0 };
48 int MSVCRT___mb_cur_max = 1;
49 static unsigned char charmax = CHAR_MAX;
52 #define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
53 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
55 #define MSVCRT_LEADBYTE 0x8000
56 #define MSVCRT_C1_DEFINED 0x200
58 /* Friendly country strings & language names abbreviations. */
59 static const char * const _country_synonyms[] =
62 "american english", "enu",
63 "american-english", "enu",
64 "english-american", "enu",
72 "french-belgian", "frb",
75 "french-canadian", "frc",
77 "chinese-simplified", "chs",
78 "chinese-traditional", "cht",
79 "dutch-belgian", "nlb",
83 "french-swiss", "frs",
85 "german-swiss", "des",
86 "italian-swiss", "its",
87 "german-austrian", "dea",
89 "portuguese-brazil", "ptb",
90 "spanish-mexican", "esm",
91 "norwegian-bokmal", "nor",
92 "norwegian-nynorsk", "non",
93 "spanish-modern", "esn"
96 /* INTERNAL: Map a synonym to an ISO code */
97 static void remap_synonym(char *name)
100 for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
102 if (!strcasecmp(_country_synonyms[i],name))
104 TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
105 strcpy(name, _country_synonyms[i+1]);
111 /* Note: Flags are weighted in order of matching importance */
112 #define FOUND_LANGUAGE 0x4
113 #define FOUND_COUNTRY 0x2
114 #define FOUND_CODEPAGE 0x1
117 char search_language[MAX_ELEM_LEN];
118 char search_country[MAX_ELEM_LEN];
119 char search_codepage[MAX_ELEM_LEN];
120 char found_codepage[MAX_ELEM_LEN];
121 unsigned int match_flags;
122 LANGID found_lang_id;
125 #define CONTINUE_LOOKING TRUE
126 #define STOP_LOOKING FALSE
128 /* INTERNAL: Get and compare locale info with a given string */
129 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp, BOOL exact)
137 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE, buff, MAX_ELEM_LEN);
141 /* Partial matches are only allowed on language/country names */
144 return !strcasecmp(cmp, buff);
146 return !strncasecmp(cmp, buff, len);
150 find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
152 locale_search_t *res = (locale_search_t *)lParam;
153 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
154 char buff[MAX_ELEM_LEN];
155 unsigned int flags = 0;
157 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
158 return CONTINUE_LOOKING;
161 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language, TRUE) ||
162 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language, TRUE) ||
163 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language, FALSE))
165 TRACE(":Found language: %s->%s\n", res->search_language, buff);
166 flags |= FOUND_LANGUAGE;
168 else if (res->match_flags & FOUND_LANGUAGE)
170 return CONTINUE_LOOKING;
174 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country, TRUE) ||
175 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country, TRUE) ||
176 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country, FALSE))
178 TRACE("Found country:%s->%s\n", res->search_country, buff);
179 flags |= FOUND_COUNTRY;
181 else if (!flags && (res->match_flags & FOUND_COUNTRY))
183 return CONTINUE_LOOKING;
187 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage, TRUE) ||
188 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage, TRUE)))
190 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
191 flags |= FOUND_CODEPAGE;
192 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
194 else if (!flags && (res->match_flags & FOUND_CODEPAGE))
196 return CONTINUE_LOOKING;
199 if (flags > res->match_flags)
201 /* Found a better match than previously */
202 res->match_flags = flags;
203 res->found_lang_id = LangID;
205 if ((flags & (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE)) ==
206 (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE))
208 TRACE(":found exact locale match\n");
211 return CONTINUE_LOOKING;
214 extern int atoi(const char *);
216 /* Internal: Find the LCID for a locale specification */
217 LCID MSVCRT_locale_to_LCID(const char *locale, unsigned short *codepage)
220 locale_search_t search;
221 const char *cp, *region;
223 memset(&search, 0, sizeof(locale_search_t));
225 cp = strchr(locale, '.');
226 region = strchr(locale, '_');
228 lstrcpynA(search.search_language, locale, MAX_ELEM_LEN);
230 lstrcpynA(search.search_country, region+1, MAX_ELEM_LEN);
231 if(region-locale < MAX_ELEM_LEN)
232 search.search_language[region-locale] = '\0';
234 search.search_country[0] = '\0';
237 lstrcpynA(search.search_codepage, cp+1, MAX_ELEM_LEN);
238 if(region && cp-region-1<MAX_ELEM_LEN)
239 search.search_country[cp-region-1] = '\0';
240 if(cp-locale < MAX_ELEM_LEN)
241 search.search_language[cp-locale] = '\0';
243 search.search_codepage[0] = '\0';
245 if(!search.search_country[0] && !search.search_codepage[0])
246 remap_synonym(search.search_language);
248 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
249 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
252 if (!search.match_flags)
255 /* If we were given something that didn't match, fail */
256 if (search.search_country[0] && !(search.match_flags & FOUND_COUNTRY))
259 lcid = MAKELCID(search.found_lang_id, SORT_DEFAULT);
261 /* Populate partial locale, translating LCID to locale string elements */
262 if (!(search.match_flags & FOUND_CODEPAGE)) {
263 /* Even if a codepage is not enumerated for a locale
264 * it can be set if valid */
265 if (search.search_codepage[0]) {
266 if (IsValidCodePage(atoi(search.search_codepage)))
267 memcpy(search.found_codepage,search.search_codepage,MAX_ELEM_LEN);
269 /* Special codepage values: OEM & ANSI */
270 if (!strcasecmp(search.search_codepage,"OCP")) {
271 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
272 search.found_codepage, MAX_ELEM_LEN);
273 } else if (!strcasecmp(search.search_codepage,"ACP")) {
274 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
275 search.found_codepage, MAX_ELEM_LEN);
279 if (!atoi(search.found_codepage))
283 /* Prefer ANSI codepages if present */
284 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
285 search.found_codepage, MAX_ELEM_LEN);
286 if (!search.found_codepage[0] || !atoi(search.found_codepage))
287 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
288 search.found_codepage, MAX_ELEM_LEN);
292 *codepage = atoi(search.found_codepage);
297 /* INTERNAL: Set lc_handle, lc_id and lc_category in threadlocinfo struct */
298 static BOOL update_threadlocinfo_category(LCID lcid, unsigned short cp,
299 MSVCRT__locale_t loc, int category)
304 if(GetLocaleInfoA(lcid, LOCALE_ILANGUAGE|LOCALE_NOUSEROVERRIDE, buf, 256)) {
307 loc->locinfo->lc_id[category].wLanguage = 0;
309 loc->locinfo->lc_id[category].wLanguage *= 16;
312 loc->locinfo->lc_id[category].wLanguage += *p-'0';
314 loc->locinfo->lc_id[category].wLanguage += *p-'a'+10;
319 loc->locinfo->lc_id[category].wCountry =
320 loc->locinfo->lc_id[category].wLanguage;
323 loc->locinfo->lc_id[category].wCodePage = cp;
325 loc->locinfo->lc_handle[category] = lcid;
328 len += GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE
329 |LOCALE_NOUSEROVERRIDE, buf, 256);
331 len += GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY
332 |LOCALE_NOUSEROVERRIDE, &buf[len], 256-len);
334 sprintf(buf+len, "%d", cp);
335 len += strlen(buf+len)+1;
337 loc->locinfo->lc_category[category].locale = MSVCRT_malloc(len);
338 loc->locinfo->lc_category[category].refcount = MSVCRT_malloc(sizeof(int));
339 if(!loc->locinfo->lc_category[category].locale
340 || !loc->locinfo->lc_category[category].refcount) {
341 MSVCRT_free(loc->locinfo->lc_category[category].locale);
342 MSVCRT_free(loc->locinfo->lc_category[category].refcount);
343 loc->locinfo->lc_category[category].locale = NULL;
344 loc->locinfo->lc_category[category].refcount = NULL;
347 memcpy(loc->locinfo->lc_category[category].locale, buf, len);
348 *loc->locinfo->lc_category[category].refcount = 1;
353 /* INTERNAL: swap pointers values */
354 static inline void swap_pointers(void **p1, void **p2) {
362 /* INTERNAL: returns pthreadlocinfo struct */
363 MSVCRT_pthreadlocinfo get_locinfo(void) {
364 thread_data_t *data = msvcrt_get_thread_data();
366 if(!data || !data->have_locale)
367 return MSVCRT_locale->locinfo;
369 return data->locinfo;
372 /* INTERNAL: returns pthreadlocinfo struct */
373 MSVCRT_pthreadmbcinfo get_mbcinfo(void) {
374 thread_data_t *data = msvcrt_get_thread_data();
376 if(!data || !data->have_locale)
377 return MSVCRT_locale->mbcinfo;
379 return data->mbcinfo;
382 /* INTERNAL: constructs string returned by setlocale */
383 static inline char* construct_lc_all(MSVCRT_pthreadlocinfo locinfo) {
384 static char current_lc_all[MAX_LOCALE_LENGTH];
388 for(i=MSVCRT_LC_MIN+1; i<MSVCRT_LC_MAX; i++) {
389 if(strcmp(locinfo->lc_category[i].locale,
390 locinfo->lc_category[i+1].locale))
395 return locinfo->lc_category[MSVCRT_LC_COLLATE].locale;
397 sprintf(current_lc_all,
398 "LC_COLLATE=%s;LC_CTYPE=%s;LC_MONETARY=%s;LC_NUMERIC=%s;LC_TIME=%s",
399 locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
400 locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
401 locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
402 locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
403 locinfo->lc_category[MSVCRT_LC_TIME].locale);
405 return current_lc_all;
409 /*********************************************************************
410 * wsetlocale (MSVCRT.@)
412 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
414 static MSVCRT_wchar_t fake[] = {
415 'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
416 'S','t','a','t','e','s','.','1','2','5','2',0 };
418 FIXME("%d %s\n", category, debugstr_w(locale));
423 /*********************************************************************
424 * _Getdays (MSVCRT.@)
426 char* CDECL _Getdays(void)
428 MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
434 size = cur->str.names.short_mon[0]-cur->str.names.short_wday[0];
435 out = MSVCRT_malloc(size+1);
442 len = strlen(cur->str.names.short_wday[i]);
443 memcpy(&out[size], cur->str.names.short_wday[i], len);
447 len = strlen(cur->str.names.wday[i]);
448 memcpy(&out[size], cur->str.names.wday[i], len);
456 /*********************************************************************
457 * _Getmonths (MSVCRT.@)
459 char* CDECL _Getmonths(void)
461 MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
467 size = cur->str.names.am-cur->str.names.short_mon[0];
468 out = MSVCRT_malloc(size+1);
473 for(i=0; i<12; i++) {
475 len = strlen(cur->str.names.short_mon[i]);
476 memcpy(&out[size], cur->str.names.short_mon[i], len);
480 len = strlen(cur->str.names.mon[i]);
481 memcpy(&out[size], cur->str.names.mon[i], len);
489 /*********************************************************************
490 * _Gettnames (MSVCRT.@)
492 void* CDECL _Gettnames(void)
494 MSVCRT___lc_time_data *ret, *cur = get_locinfo()->lc_time_curr;
495 int i, size = sizeof(MSVCRT___lc_time_data);
499 for(i=0; i<sizeof(cur->str.str)/sizeof(cur->str.str[0]); i++)
500 size += strlen(cur->str.str[i])+1;
502 ret = MSVCRT_malloc(size);
505 memcpy(ret, cur, size);
508 for(i=0; i<sizeof(cur->str.str)/sizeof(cur->str.str[0]); i++) {
509 ret->str.str[i] = &ret->data[size];
510 size += strlen(&ret->data[size])+1;
516 /*********************************************************************
517 * __crtLCMapStringA (MSVCRT.@)
519 int CDECL __crtLCMapStringA(
520 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
521 int dstlen, unsigned int codepage, int xflag
523 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
524 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
525 /* FIXME: A bit incorrect. But msvcrt itself just converts its
526 * arguments to wide strings and then calls LCMapStringW
528 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
531 /*********************************************************************
532 * __crtLCMapStringW (MSVCRT.@)
534 int CDECL __crtLCMapStringW(LCID lcid, DWORD mapflags, const MSVCRT_wchar_t *src,
535 int srclen, MSVCRT_wchar_t *dst, int dstlen, unsigned int codepage, int xflag)
537 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
538 lcid, mapflags, debugstr_w(src), srclen, dst, dstlen, codepage, xflag);
540 return LCMapStringW(lcid, mapflags, src, srclen, dst, dstlen);
543 /*********************************************************************
544 * __crtCompareStringA (MSVCRT.@)
546 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
547 const char *src2, int len2 )
549 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
550 lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
551 /* FIXME: probably not entirely right */
552 return CompareStringA( lcid, flags, src1, len1, src2, len2 );
555 /*********************************************************************
556 * __crtCompareStringW (MSVCRT.@)
558 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
559 const MSVCRT_wchar_t *src2, int len2 )
561 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
562 lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
563 /* FIXME: probably not entirely right */
564 return CompareStringW( lcid, flags, src1, len1, src2, len2 );
567 /*********************************************************************
568 * __crtGetLocaleInfoW (MSVCRT.@)
570 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
572 FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
573 /* FIXME: probably not entirely right */
574 return GetLocaleInfoW( lcid, type, buffer, len );
577 /*********************************************************************
580 MSVCRT_wint_t CDECL MSVCRT_btowc(int c)
582 unsigned char letter = c;
585 if(!MultiByteToWideChar(get_locinfo()->lc_handle[MSVCRT_LC_CTYPE],
586 0, (LPCSTR)&letter, 1, &ret, 1))
592 /*********************************************************************
593 * __crtGetStringTypeW(MSVCRT.@)
595 * This function was accepting different number of arguments in older
596 * versions of msvcrt.
598 BOOL CDECL __crtGetStringTypeW(DWORD unk, DWORD type,
599 MSVCRT_wchar_t *buffer, int len, WORD *out)
601 FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
602 unk, type, buffer, len, out);
604 return GetStringTypeW(type, buffer, len, out);
607 /*********************************************************************
608 * localeconv (MSVCRT.@)
610 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
612 return get_locinfo()->lconv;
615 /*********************************************************************
616 * __lconv_init (MSVCRT.@)
618 int CDECL __lconv_init(void)
620 /* this is used to make chars unsigned */
625 /*********************************************************************
626 * ___lc_handle_func (MSVCRT.@)
628 LCID* CDECL ___lc_handle_func(void)
630 return MSVCRT___lc_handle;
633 /*********************************************************************
634 * ___lc_codepage_func (MSVCRT.@)
636 unsigned int CDECL ___lc_codepage_func(void)
638 return MSVCRT___lc_codepage;
641 /*********************************************************************
642 * ___lc_collate_cp_func (MSVCRT.@)
644 int CDECL ___lc_collate_cp_func(void)
646 return get_locinfo()->lc_collate_cp;
649 /* INTERNAL: frees MSVCRT_pthreadlocinfo struct */
650 void free_locinfo(MSVCRT_pthreadlocinfo locinfo)
657 if(InterlockedDecrement(&locinfo->refcount))
660 for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
661 MSVCRT_free(locinfo->lc_category[i].locale);
662 MSVCRT_free(locinfo->lc_category[i].refcount);
666 MSVCRT_free(locinfo->lconv->decimal_point);
667 MSVCRT_free(locinfo->lconv->thousands_sep);
668 MSVCRT_free(locinfo->lconv->grouping);
669 MSVCRT_free(locinfo->lconv->int_curr_symbol);
670 MSVCRT_free(locinfo->lconv->currency_symbol);
671 MSVCRT_free(locinfo->lconv->mon_decimal_point);
672 MSVCRT_free(locinfo->lconv->mon_thousands_sep);
673 MSVCRT_free(locinfo->lconv->mon_grouping);
674 MSVCRT_free(locinfo->lconv->positive_sign);
675 MSVCRT_free(locinfo->lconv->negative_sign);
677 MSVCRT_free(locinfo->lconv_intl_refcount);
678 MSVCRT_free(locinfo->lconv_num_refcount);
679 MSVCRT_free(locinfo->lconv_mon_refcount);
680 MSVCRT_free(locinfo->lconv);
682 MSVCRT_free(locinfo->ctype1_refcount);
683 MSVCRT_free(locinfo->ctype1);
685 MSVCRT_free(locinfo->pclmap);
686 MSVCRT_free(locinfo->pcumap);
688 MSVCRT_free(locinfo->lc_time_curr);
690 MSVCRT_free(locinfo);
693 /* INTERNAL: frees MSVCRT_pthreadmbcinfo struct */
694 void free_mbcinfo(MSVCRT_pthreadmbcinfo mbcinfo)
699 if(InterlockedDecrement(&mbcinfo->refcount))
702 MSVCRT_free(mbcinfo);
705 /* _get_current_locale - not exported in native msvcrt */
706 MSVCRT__locale_t CDECL MSVCRT__get_current_locale(void)
708 MSVCRT__locale_t loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
712 loc->locinfo = get_locinfo();
713 loc->mbcinfo = get_mbcinfo();
714 InterlockedIncrement(&loc->locinfo->refcount);
715 InterlockedIncrement(&loc->mbcinfo->refcount);
719 /* _free_locale - not exported in native msvcrt */
720 void CDECL MSVCRT__free_locale(MSVCRT__locale_t locale)
725 free_locinfo(locale->locinfo);
726 free_mbcinfo(locale->mbcinfo);
730 /* _create_locale - not exported in native msvcrt */
731 MSVCRT__locale_t CDECL MSVCRT__create_locale(int category, const char *locale)
733 static const DWORD time_data[] = {
734 LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1, LOCALE_SABBREVDAYNAME2,
735 LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4, LOCALE_SABBREVDAYNAME5,
736 LOCALE_SABBREVDAYNAME6,
737 LOCALE_SDAYNAME7, LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3,
738 LOCALE_SDAYNAME4, LOCALE_SDAYNAME5, LOCALE_SDAYNAME6,
739 LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2, LOCALE_SABBREVMONTHNAME3,
740 LOCALE_SABBREVMONTHNAME4, LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
741 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8, LOCALE_SABBREVMONTHNAME9,
742 LOCALE_SABBREVMONTHNAME10, LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12,
743 LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME4,
744 LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8,
745 LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12,
746 LOCALE_S1159, LOCALE_S2359,
747 LOCALE_SSHORTDATE, LOCALE_SLONGDATE,
750 static const char collate[] = "COLLATE=";
751 static const char ctype[] = "CTYPE=";
752 static const char monetary[] = "MONETARY=";
753 static const char numeric[] = "NUMERIC=";
754 static const char time[] = "TIME=";
755 static const char cloc_short_date[] = "MM/dd/yy";
756 static const MSVCRT_wchar_t cloc_short_dateW[] = {'M','M','/','d','d','/','y','y',0};
757 static const char cloc_long_date[] = "dddd, MMMM dd, yyyy";
758 static const MSVCRT_wchar_t cloc_long_dateW[] = {'d','d','d','d',',',' ','M','M','M','M',' ','d','d',',',' ','y','y','y','y',0};
759 static const char cloc_time[] = "HH:mm:ss";
760 static const MSVCRT_wchar_t cloc_timeW[] = {'H','H',':','m','m',':','s','s',0};
762 MSVCRT__locale_t loc;
763 LCID lcid[6] = { 0 }, lcid_tmp;
764 unsigned short cp[6] = { 0 };
768 TRACE("(%d %s)\n", category, locale);
770 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
773 if(locale[0]=='C' && !locale[1]) {
776 } else if(!locale[0]) {
777 lcid[0] = GetSystemDefaultLCID();
778 GetLocaleInfoA(lcid[0], LOCALE_IDEFAULTANSICODEPAGE
779 |LOCALE_NOUSEROVERRIDE, buf, sizeof(buf));
786 } else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
790 locale += 3; /* LC_ */
791 if(!memcmp(locale, collate, sizeof(collate)-1)) {
792 i = MSVCRT_LC_COLLATE;
793 locale += sizeof(collate)-1;
794 } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
796 locale += sizeof(ctype)-1;
797 } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
798 i = MSVCRT_LC_MONETARY;
799 locale += sizeof(monetary)-1;
800 } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
801 i = MSVCRT_LC_NUMERIC;
802 locale += sizeof(numeric)-1;
803 } else if(!memcmp(locale, time, sizeof(time)-1)) {
805 locale += sizeof(time)-1;
809 p = strchr(locale, ';');
810 if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0')) {
814 memcpy(buf, locale, p-locale);
815 buf[p-locale] = '\0';
816 lcid[i] = MSVCRT_locale_to_LCID(buf, &cp[i]);
818 lcid[i] = MSVCRT_locale_to_LCID(locale, &cp[i]);
823 if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
829 lcid[0] = MSVCRT_locale_to_LCID(locale, &cp[0]);
839 loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
843 loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
849 loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
851 MSVCRT_free(loc->locinfo);
856 memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
857 loc->locinfo->refcount = 1;
858 loc->mbcinfo->refcount = 1;
860 loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
861 if(!loc->locinfo->lconv) {
862 MSVCRT__free_locale(loc);
865 memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
867 loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
868 loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
869 if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
870 MSVCRT__free_locale(loc);
874 if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
875 if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE], cp[MSVCRT_LC_COLLATE], loc, MSVCRT_LC_COLLATE)) {
876 MSVCRT__free_locale(loc);
880 loc->locinfo->lc_collate_cp = loc->locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage;
882 loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale = MSVCRT__strdup("C");
884 if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
888 if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE], cp[MSVCRT_LC_CTYPE], loc, MSVCRT_LC_CTYPE)) {
889 MSVCRT__free_locale(loc);
893 loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
894 loc->locinfo->lc_clike = 1;
895 if(!GetCPInfo(loc->locinfo->lc_codepage, &cp_info)) {
896 MSVCRT__free_locale(loc);
899 loc->locinfo->mb_cur_max = cp_info.MaxCharSize;
901 loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
902 loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
903 if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
904 MSVCRT__free_locale(loc);
908 *loc->locinfo->ctype1_refcount = 1;
909 loc->locinfo->ctype1[0] = 0;
910 loc->locinfo->pctype = loc->locinfo->ctype1+1;
912 buf[1] = buf[2] = '\0';
913 for(i=1; i<257; i++) {
916 /* builtin GetStringTypeA doesn't set output to 0 on invalid input */
917 loc->locinfo->ctype1[i] = 0;
919 GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
920 1, loc->locinfo->ctype1+i);
923 for(i=0; cp_info.LeadByte[i+1]!=0; i+=2)
924 for(j=cp_info.LeadByte[i]; j<=cp_info.LeadByte[i+1]; j++)
925 loc->locinfo->ctype1[j+1] |= MSVCRT__LEADBYTE;
927 loc->locinfo->lc_clike = 1;
928 loc->locinfo->mb_cur_max = 1;
929 loc->locinfo->pctype = MSVCRT__ctype+1;
930 loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale = MSVCRT__strdup("C");
933 for(i=0; i<256; i++) {
934 if(loc->locinfo->pctype[i] & MSVCRT__LEADBYTE)
941 if(lcid[MSVCRT_LC_CTYPE]) {
942 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
943 (char*)loc->locinfo->pclmap, 256);
944 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
945 (char*)loc->locinfo->pcumap, 256);
947 for(i=0; i<256; i++) {
948 loc->locinfo->pclmap[i] = (i>='A' && i<='Z' ? i-'A'+'a' : i);
949 loc->locinfo->pcumap[i] = (i>='a' && i<='z' ? i-'a'+'A' : i);
953 _setmbcp_l(loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage, lcid[MSVCRT_LC_CTYPE], loc->mbcinfo);
955 if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
956 if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY], cp[MSVCRT_LC_MONETARY], loc, MSVCRT_LC_MONETARY)) {
957 MSVCRT__free_locale(loc);
961 loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
962 loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
963 if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
964 MSVCRT__free_locale(loc);
968 *loc->locinfo->lconv_intl_refcount = 1;
969 *loc->locinfo->lconv_mon_refcount = 1;
971 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
972 |LOCALE_NOUSEROVERRIDE, buf, 256);
973 if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(i)))
974 memcpy(loc->locinfo->lconv->int_curr_symbol, buf, i);
976 MSVCRT__free_locale(loc);
980 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
981 |LOCALE_NOUSEROVERRIDE, buf, 256);
982 if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(i)))
983 memcpy(loc->locinfo->lconv->currency_symbol, buf, i);
985 MSVCRT__free_locale(loc);
989 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
990 |LOCALE_NOUSEROVERRIDE, buf, 256);
991 if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(i)))
992 memcpy(loc->locinfo->lconv->mon_decimal_point, buf, i);
994 MSVCRT__free_locale(loc);
998 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
999 |LOCALE_NOUSEROVERRIDE, buf, 256);
1000 if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(i)))
1001 memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, i);
1003 MSVCRT__free_locale(loc);
1007 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING
1008 |LOCALE_NOUSEROVERRIDE, buf, 256);
1010 i = i/2 + (buf[i-2]=='0'?0:1);
1011 if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(i))) {
1012 for(i=0; buf[i+1]==';'; i+=2)
1013 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1014 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1016 loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
1018 MSVCRT__free_locale(loc);
1022 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
1023 |LOCALE_NOUSEROVERRIDE, buf, 256);
1024 if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(i)))
1025 memcpy(loc->locinfo->lconv->positive_sign, buf, i);
1027 MSVCRT__free_locale(loc);
1031 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
1032 |LOCALE_NOUSEROVERRIDE, buf, 256);
1033 if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(i)))
1034 memcpy(loc->locinfo->lconv->negative_sign, buf, i);
1036 MSVCRT__free_locale(loc);
1040 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS
1041 |LOCALE_NOUSEROVERRIDE, buf, 256))
1042 loc->locinfo->lconv->int_frac_digits = atoi(buf);
1044 MSVCRT__free_locale(loc);
1048 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS
1049 |LOCALE_NOUSEROVERRIDE, buf, 256))
1050 loc->locinfo->lconv->frac_digits = atoi(buf);
1052 MSVCRT__free_locale(loc);
1056 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES
1057 |LOCALE_NOUSEROVERRIDE, buf, 256))
1058 loc->locinfo->lconv->p_cs_precedes = atoi(buf);
1060 MSVCRT__free_locale(loc);
1064 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE
1065 |LOCALE_NOUSEROVERRIDE, buf, 256))
1066 loc->locinfo->lconv->p_sep_by_space = atoi(buf);
1068 MSVCRT__free_locale(loc);
1072 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES
1073 |LOCALE_NOUSEROVERRIDE, buf, 256))
1074 loc->locinfo->lconv->n_cs_precedes = atoi(buf);
1076 MSVCRT__free_locale(loc);
1080 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE
1081 |LOCALE_NOUSEROVERRIDE, buf, 256))
1082 loc->locinfo->lconv->n_sep_by_space = atoi(buf);
1084 MSVCRT__free_locale(loc);
1088 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN
1089 |LOCALE_NOUSEROVERRIDE, buf, 256))
1090 loc->locinfo->lconv->p_sign_posn = atoi(buf);
1092 MSVCRT__free_locale(loc);
1096 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN
1097 |LOCALE_NOUSEROVERRIDE, buf, 256))
1098 loc->locinfo->lconv->n_sign_posn = atoi(buf);
1100 MSVCRT__free_locale(loc);
1104 loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
1105 loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
1106 loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
1107 loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
1108 loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
1109 loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
1110 loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
1112 if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
1113 || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
1114 || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
1115 || !loc->locinfo->lconv->negative_sign) {
1116 MSVCRT__free_locale(loc);
1120 loc->locinfo->lconv->int_curr_symbol[0] = '\0';
1121 loc->locinfo->lconv->currency_symbol[0] = '\0';
1122 loc->locinfo->lconv->mon_decimal_point[0] = '\0';
1123 loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
1124 loc->locinfo->lconv->mon_grouping[0] = '\0';
1125 loc->locinfo->lconv->positive_sign[0] = '\0';
1126 loc->locinfo->lconv->negative_sign[0] = '\0';
1127 loc->locinfo->lconv->int_frac_digits = 127;
1128 loc->locinfo->lconv->frac_digits = 127;
1129 loc->locinfo->lconv->p_cs_precedes = 127;
1130 loc->locinfo->lconv->p_sep_by_space = 127;
1131 loc->locinfo->lconv->n_cs_precedes = 127;
1132 loc->locinfo->lconv->n_sep_by_space = 127;
1133 loc->locinfo->lconv->p_sign_posn = 127;
1134 loc->locinfo->lconv->n_sign_posn = 127;
1136 loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale = MSVCRT__strdup("C");
1139 if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
1140 if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC], cp[MSVCRT_LC_NUMERIC], loc, MSVCRT_LC_NUMERIC)) {
1141 MSVCRT__free_locale(loc);
1145 if(!loc->locinfo->lconv_intl_refcount)
1146 loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1147 loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1148 if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
1149 MSVCRT__free_locale(loc);
1153 *loc->locinfo->lconv_intl_refcount = 1;
1154 *loc->locinfo->lconv_num_refcount = 1;
1156 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1157 |LOCALE_NOUSEROVERRIDE, buf, 256);
1158 if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(i)))
1159 memcpy(loc->locinfo->lconv->decimal_point, buf, i);
1161 MSVCRT__free_locale(loc);
1165 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1166 |LOCALE_NOUSEROVERRIDE, buf, 256);
1167 if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(i)))
1168 memcpy(loc->locinfo->lconv->thousands_sep, buf, i);
1170 MSVCRT__free_locale(loc);
1174 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING
1175 |LOCALE_NOUSEROVERRIDE, buf, 256);
1177 i = i/2 + (buf[i-2]=='0'?0:1);
1178 if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(i))) {
1179 for(i=0; buf[i+1]==';'; i+=2)
1180 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1181 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1183 loc->locinfo->lconv->grouping[i/2+1] = 127;
1185 MSVCRT__free_locale(loc);
1189 loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1190 loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1191 loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1192 if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
1193 || !loc->locinfo->lconv->grouping) {
1194 MSVCRT__free_locale(loc);
1198 loc->locinfo->lconv->decimal_point[0] = '.';
1199 loc->locinfo->lconv->decimal_point[1] = '\0';
1200 loc->locinfo->lconv->thousands_sep[0] = '\0';
1201 loc->locinfo->lconv->grouping[0] = '\0';
1203 loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale = MSVCRT__strdup("C");
1206 if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1207 if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME], cp[MSVCRT_LC_TIME], loc, MSVCRT_LC_TIME)) {
1208 MSVCRT__free_locale(loc);
1212 loc->locinfo->lc_category[MSVCRT_LC_TIME].locale = MSVCRT__strdup("C");
1214 size = sizeof(MSVCRT___lc_time_data);
1215 lcid_tmp = lcid[MSVCRT_LC_TIME] ? lcid[MSVCRT_LC_TIME] : MAKELCID(LANG_ENGLISH, SORT_DEFAULT);
1216 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1217 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1218 size += sizeof(cloc_short_date) + sizeof(cloc_short_dateW);
1219 }else if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1220 size += sizeof(cloc_long_date) + sizeof(cloc_long_dateW);
1222 ret = GetLocaleInfoA(lcid_tmp, time_data[i]
1223 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1225 MSVCRT__free_locale(loc);
1230 ret = GetLocaleInfoW(lcid_tmp, time_data[i]
1231 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1233 MSVCRT__free_locale(loc);
1236 size += ret*sizeof(MSVCRT_wchar_t);
1240 loc->locinfo->lc_time_curr = MSVCRT_malloc(size);
1241 if(!loc->locinfo->lc_time_curr) {
1242 MSVCRT__free_locale(loc);
1247 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1248 loc->locinfo->lc_time_curr->str.str[i] = &loc->locinfo->lc_time_curr->data[ret];
1249 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1250 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_short_date, sizeof(cloc_short_date));
1251 ret += sizeof(cloc_short_date);
1252 }else if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1253 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_long_date, sizeof(cloc_long_date));
1254 ret += sizeof(cloc_long_date);
1255 }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1256 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_time, sizeof(cloc_time));
1257 ret += sizeof(cloc_time);
1259 ret += GetLocaleInfoA(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1260 &loc->locinfo->lc_time_curr->data[ret], size-ret);
1263 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1264 loc->locinfo->lc_time_curr->wstr[i] = (MSVCRT_wchar_t*)&loc->locinfo->lc_time_curr->data[ret];
1265 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1266 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_short_dateW, sizeof(cloc_short_dateW));
1267 ret += sizeof(cloc_short_dateW);
1268 }else if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1269 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_long_dateW, sizeof(cloc_long_dateW));
1270 ret += sizeof(cloc_long_dateW);
1271 }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1272 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_timeW, sizeof(cloc_timeW));
1273 ret += sizeof(cloc_timeW);
1275 ret += GetLocaleInfoW(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1276 (MSVCRT_wchar_t*)&loc->locinfo->lc_time_curr->data[ret], size-ret)*sizeof(MSVCRT_wchar_t);
1279 loc->locinfo->lc_time_curr->lcid = lcid[MSVCRT_LC_TIME];
1284 /*********************************************************************
1285 * setlocale (MSVCRT.@)
1287 char* CDECL MSVCRT_setlocale(int category, const char* locale)
1289 MSVCRT__locale_t loc;
1290 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
1292 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1296 if(category == MSVCRT_LC_ALL)
1297 return construct_lc_all(locinfo);
1299 return locinfo->lc_category[category].locale;
1302 loc = MSVCRT__create_locale(category, locale);
1304 WARN("%d %s failed\n", category, locale);
1312 case MSVCRT_LC_COLLATE:
1313 locinfo->lc_collate_cp = loc->locinfo->lc_collate_cp;
1314 locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1315 loc->locinfo->lc_handle[MSVCRT_LC_COLLATE];
1316 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1317 (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1318 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1319 (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1321 if(category != MSVCRT_LC_ALL)
1324 case MSVCRT_LC_CTYPE:
1325 locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1326 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE];
1327 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1328 (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1329 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1330 (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1332 locinfo->lc_codepage = loc->locinfo->lc_codepage;
1333 locinfo->lc_clike = loc->locinfo->lc_clike;
1334 locinfo->mb_cur_max = loc->locinfo->mb_cur_max;
1336 swap_pointers((void**)&locinfo->ctype1_refcount,
1337 (void**)&loc->locinfo->ctype1_refcount);
1338 swap_pointers((void**)&locinfo->ctype1, (void**)&loc->locinfo->ctype1);
1339 swap_pointers((void**)&locinfo->pctype, (void**)&loc->locinfo->pctype);
1340 swap_pointers((void**)&locinfo->pclmap, (void**)&loc->locinfo->pclmap);
1341 swap_pointers((void**)&locinfo->pcumap, (void**)&loc->locinfo->pcumap);
1343 if(category != MSVCRT_LC_ALL)
1346 case MSVCRT_LC_MONETARY:
1347 locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1348 loc->locinfo->lc_handle[MSVCRT_LC_MONETARY];
1349 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1350 (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1351 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1352 (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1354 swap_pointers((void**)&locinfo->lconv->int_curr_symbol,
1355 (void**)&loc->locinfo->lconv->int_curr_symbol);
1356 swap_pointers((void**)&locinfo->lconv->currency_symbol,
1357 (void**)&loc->locinfo->lconv->currency_symbol);
1358 swap_pointers((void**)&locinfo->lconv->mon_decimal_point,
1359 (void**)&loc->locinfo->lconv->mon_decimal_point);
1360 swap_pointers((void**)&locinfo->lconv->mon_thousands_sep,
1361 (void**)&loc->locinfo->lconv->mon_thousands_sep);
1362 swap_pointers((void**)&locinfo->lconv->mon_grouping,
1363 (void**)&loc->locinfo->lconv->mon_grouping);
1364 swap_pointers((void**)&locinfo->lconv->positive_sign,
1365 (void**)&loc->locinfo->lconv->positive_sign);
1366 swap_pointers((void**)&locinfo->lconv->negative_sign,
1367 (void**)&loc->locinfo->lconv->negative_sign);
1368 locinfo->lconv->int_frac_digits = loc->locinfo->lconv->int_frac_digits;
1369 locinfo->lconv->frac_digits = loc->locinfo->lconv->frac_digits;
1370 locinfo->lconv->p_cs_precedes = loc->locinfo->lconv->p_cs_precedes;
1371 locinfo->lconv->p_sep_by_space = loc->locinfo->lconv->p_sep_by_space;
1372 locinfo->lconv->n_cs_precedes = loc->locinfo->lconv->n_cs_precedes;
1373 locinfo->lconv->n_sep_by_space = loc->locinfo->lconv->n_sep_by_space;
1374 locinfo->lconv->p_sign_posn = loc->locinfo->lconv->p_sign_posn;
1375 locinfo->lconv->n_sign_posn = loc->locinfo->lconv->n_sign_posn;
1377 if(category != MSVCRT_LC_ALL)
1380 case MSVCRT_LC_NUMERIC:
1381 locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1382 loc->locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1383 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1384 (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1385 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1386 (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1388 swap_pointers((void**)&locinfo->lconv->decimal_point,
1389 (void**)&loc->locinfo->lconv->decimal_point);
1390 swap_pointers((void**)&locinfo->lconv->thousands_sep,
1391 (void**)&loc->locinfo->lconv->thousands_sep);
1392 swap_pointers((void**)&locinfo->lconv->grouping,
1393 (void**)&loc->locinfo->lconv->grouping);
1395 if(category != MSVCRT_LC_ALL)
1398 case MSVCRT_LC_TIME:
1399 locinfo->lc_handle[MSVCRT_LC_TIME] =
1400 loc->locinfo->lc_handle[MSVCRT_LC_TIME];
1401 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].locale,
1402 (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].locale);
1403 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1404 (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].refcount);
1405 swap_pointers((void**)&locinfo->lc_time_curr,
1406 (void**)&loc->locinfo->lc_time_curr);
1408 if(category != MSVCRT_LC_ALL)
1412 MSVCRT__free_locale(loc);
1415 if(locinfo == MSVCRT_locale->locinfo) {
1418 MSVCRT___lc_codepage = locinfo->lc_codepage;
1419 MSVCRT___lc_collate_cp = locinfo->lc_collate_cp;
1420 MSVCRT___mb_cur_max = locinfo->mb_cur_max;
1421 MSVCRT__pctype = locinfo->pctype;
1422 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1423 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1426 if(category == MSVCRT_LC_ALL)
1427 return construct_lc_all(locinfo);
1429 return locinfo->lc_category[category].locale;
1432 /* _configthreadlocale - not exported in native msvcrt */
1433 int CDECL _configthreadlocale(int type)
1435 thread_data_t *data = msvcrt_get_thread_data();
1436 MSVCRT__locale_t locale;
1442 ret = (data->have_locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1444 if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1445 if(!data->have_locale) {
1446 /* Copy current global locale */
1447 locale = MSVCRT__create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
1451 data->locinfo = locale->locinfo;
1452 data->mbcinfo = locale->mbcinfo;
1453 data->have_locale = TRUE;
1454 MSVCRT_free(locale);
1460 if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1461 if(data->have_locale) {
1462 free_locinfo(data->locinfo);
1463 free_mbcinfo(data->mbcinfo);
1464 data->locinfo = MSVCRT_locale->locinfo;
1465 data->mbcinfo = MSVCRT_locale->mbcinfo;
1466 data->have_locale = FALSE;
1478 BOOL msvcrt_init_locale(void)
1483 MSVCRT_locale = MSVCRT__create_locale(0, "C");
1488 MSVCRT___lc_codepage = MSVCRT_locale->locinfo->lc_codepage;
1489 MSVCRT___lc_collate_cp = MSVCRT_locale->locinfo->lc_collate_cp;
1490 MSVCRT___mb_cur_max = MSVCRT_locale->locinfo->mb_cur_max;
1491 MSVCRT__pctype = MSVCRT_locale->locinfo->pctype;
1492 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1493 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1494 _setmbcp(_MB_CP_ANSI);