comctl32: Store style bits provided with WM_CREATE.
[wine/multimedia.git] / dlls / msvcrt / locale.c
blob699b02813c0c0e0ca1c486fbc8ba584d1ba8981c
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <limits.h>
25 #include <locale.h>
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winnls.h"
34 #include "msvcrt.h"
35 #include "mtdll.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;
51 /* MT */
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[] =
61 "american", "enu",
62 "american english", "enu",
63 "american-english", "enu",
64 "english-american", "enu",
65 "english-us", "enu",
66 "english-usa", "enu",
67 "us", "enu",
68 "usa", "enu",
69 "australian", "ena",
70 "english-aus", "ena",
71 "belgian", "nlb",
72 "french-belgian", "frb",
73 "canadian", "enc",
74 "english-can", "enc",
75 "french-canadian", "frc",
76 "chinese", "chs",
77 "chinese-simplified", "chs",
78 "chinese-traditional", "cht",
79 "dutch-belgian", "nlb",
80 "english-nz", "enz",
81 "uk", "eng",
82 "english-uk", "eng",
83 "french-swiss", "frs",
84 "swiss", "des",
85 "german-swiss", "des",
86 "italian-swiss", "its",
87 "german-austrian", "dea",
88 "portugese", "ptb",
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)
99 unsigned int i;
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]);
106 return;
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
116 typedef struct {
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;
123 } locale_search_t;
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)
131 int len;
133 buff[0] = 0;
134 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE, buff, MAX_ELEM_LEN);
135 if (!buff[0] || !cmp[0])
136 return 0;
138 /* Partial matches are only allowed on language/country names */
139 len = strlen(cmp);
140 if(exact || len<=3)
141 return !strcasecmp(cmp, buff);
142 else
143 return !strncasecmp(cmp, buff, len);
146 static BOOL CALLBACK
147 find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
149 locale_search_t *res = (locale_search_t *)lParam;
150 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
151 char buff[MAX_ELEM_LEN];
152 unsigned int flags = 0;
154 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
155 return CONTINUE_LOOKING;
157 /* Check Language */
158 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language, TRUE) ||
159 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language, TRUE) ||
160 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language, FALSE))
162 TRACE(":Found language: %s->%s\n", res->search_language, buff);
163 flags |= FOUND_LANGUAGE;
165 else if (res->match_flags & FOUND_LANGUAGE)
167 return CONTINUE_LOOKING;
170 /* Check Country */
171 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country, TRUE) ||
172 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country, TRUE) ||
173 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country, FALSE))
175 TRACE("Found country:%s->%s\n", res->search_country, buff);
176 flags |= FOUND_COUNTRY;
178 else if (res->match_flags & FOUND_COUNTRY)
180 return CONTINUE_LOOKING;
183 /* Check codepage */
184 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage, TRUE) ||
185 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage, TRUE)))
187 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
188 flags |= FOUND_CODEPAGE;
189 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
191 else if (res->match_flags & FOUND_CODEPAGE)
193 return CONTINUE_LOOKING;
196 if (flags > res->match_flags)
198 /* Found a better match than previously */
199 res->match_flags = flags;
200 res->found_lang_id = LangID;
202 if ((flags & (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE)) ==
203 (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE))
205 TRACE(":found exact locale match\n");
206 return STOP_LOOKING;
208 return CONTINUE_LOOKING;
211 extern int atoi(const char *);
213 /* Internal: Find the LCID for a locale specification */
214 LCID MSVCRT_locale_to_LCID(const char *locale)
216 LCID lcid;
217 locale_search_t search;
218 const char *cp, *region;
220 memset(&search, 0, sizeof(locale_search_t));
222 cp = strchr(locale, '.');
223 region = strchr(locale, '_');
225 lstrcpynA(search.search_language, locale, MAX_ELEM_LEN);
226 if(region) {
227 lstrcpynA(search.search_country, region+1, MAX_ELEM_LEN);
228 if(region-locale < MAX_ELEM_LEN)
229 search.search_language[region-locale] = '\0';
230 } else
231 search.search_country[0] = '\0';
233 if(cp) {
234 lstrcpynA(search.search_codepage, cp+1, MAX_ELEM_LEN);
235 if(region && cp-region-1<MAX_ELEM_LEN)
236 search.search_country[cp-region-1] = '\0';
237 if(cp-locale < MAX_ELEM_LEN)
238 search.search_language[cp-locale] = '\0';
239 } else
240 search.search_codepage[0] = '\0';
242 if(!search.search_country[0] && !search.search_codepage[0])
243 remap_synonym(search.search_language);
245 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
246 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
247 (LONG_PTR)&search);
249 if (!search.match_flags)
250 return -1;
252 /* If we were given something that didn't match, fail */
253 if (search.search_country[0] && !(search.match_flags & FOUND_COUNTRY))
254 return -1;
256 lcid = MAKELCID(search.found_lang_id, SORT_DEFAULT);
258 /* Populate partial locale, translating LCID to locale string elements */
259 if (!search.found_codepage[0]) {
260 /* Even if a codepage is not enumerated for a locale
261 * it can be set if valid */
262 if (search.search_codepage[0]) {
263 if (IsValidCodePage(atoi(search.search_codepage)))
264 memcpy(search.found_codepage,search.search_codepage,MAX_ELEM_LEN);
265 else {
266 /* Special codepage values: OEM & ANSI */
267 if (strcasecmp(search.search_codepage,"OCP")) {
268 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
269 search.found_codepage, MAX_ELEM_LEN);
270 } else if (strcasecmp(search.search_codepage,"ACP")) {
271 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
272 search.found_codepage, MAX_ELEM_LEN);
273 } else
274 return -1;
276 if (!atoi(search.found_codepage))
277 return -1;
279 } else {
280 /* Prefer ANSI codepages if present */
281 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
282 search.found_codepage, MAX_ELEM_LEN);
283 if (!search.found_codepage[0] || !atoi(search.found_codepage))
284 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
285 search.found_codepage, MAX_ELEM_LEN);
289 return lcid;
292 /* INTERNAL: Set lc_handle, lc_id and lc_category in threadlocinfo struct */
293 static BOOL update_threadlocinfo_category(LCID lcid, MSVCRT__locale_t loc, int category)
295 char buf[256], *p;
296 int len;
298 if(GetLocaleInfoA(lcid, LOCALE_ILANGUAGE|LOCALE_NOUSEROVERRIDE, buf, 256)) {
299 p = buf;
301 loc->locinfo->lc_id[category].wLanguage = 0;
302 while(*p) {
303 loc->locinfo->lc_id[category].wLanguage *= 16;
305 if(*p <= '9')
306 loc->locinfo->lc_id[category].wLanguage += *p-'0';
307 else
308 loc->locinfo->lc_id[category].wLanguage += *p-'a'+10;
310 p++;
313 loc->locinfo->lc_id[category].wCountry =
314 loc->locinfo->lc_id[category].wLanguage;
317 if(GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE
318 |LOCALE_NOUSEROVERRIDE, buf, 256))
319 loc->locinfo->lc_id[category].wCodePage = atoi(buf);
321 loc->locinfo->lc_handle[category] = lcid;
323 len = 0;
324 len += GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE
325 |LOCALE_NOUSEROVERRIDE, buf, 256);
326 buf[len-1] = '_';
327 len += GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY
328 |LOCALE_NOUSEROVERRIDE, &buf[len], 256-len);
329 buf[len-1] = '.';
330 len += GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE
331 |LOCALE_NOUSEROVERRIDE, &buf[len], 256-len);
333 loc->locinfo->lc_category[category].locale = MSVCRT_malloc(len);
334 loc->locinfo->lc_category[category].refcount = MSVCRT_malloc(sizeof(int));
335 if(!loc->locinfo->lc_category[category].locale
336 || !loc->locinfo->lc_category[category].refcount) {
337 MSVCRT_free(loc->locinfo->lc_category[category].locale);
338 MSVCRT_free(loc->locinfo->lc_category[category].refcount);
339 loc->locinfo->lc_category[category].locale = NULL;
340 loc->locinfo->lc_category[category].refcount = NULL;
341 return TRUE;
343 memcpy(loc->locinfo->lc_category[category].locale, buf, len);
344 *loc->locinfo->lc_category[category].refcount = 1;
346 return FALSE;
349 /* INTERNAL: swap pointers values */
350 static inline void swap_pointers(void **p1, void **p2) {
351 void *hlp;
353 hlp = *p1;
354 *p1 = *p2;
355 *p2 = hlp;
358 /* INTERNAL: returns pthreadlocinfo struct */
359 MSVCRT_pthreadlocinfo get_locinfo(void) {
360 thread_data_t *data = msvcrt_get_thread_data();
362 if(!data || !data->have_locale)
363 return MSVCRT_locale->locinfo;
365 return data->locinfo;
368 /* INTERNAL: returns pthreadlocinfo struct */
369 MSVCRT_pthreadmbcinfo get_mbcinfo(void) {
370 thread_data_t *data = msvcrt_get_thread_data();
372 if(!data || !data->have_locale)
373 return MSVCRT_locale->mbcinfo;
375 return data->mbcinfo;
378 /* INTERNAL: constructs string returned by setlocale */
379 static inline char* construct_lc_all(MSVCRT_pthreadlocinfo locinfo) {
380 static char current_lc_all[MAX_LOCALE_LENGTH];
382 int i;
384 for(i=MSVCRT_LC_MIN+1; i<MSVCRT_LC_MAX; i++) {
385 if(strcmp(locinfo->lc_category[i].locale,
386 locinfo->lc_category[i+1].locale))
387 break;
390 if(i==MSVCRT_LC_MAX)
391 return locinfo->lc_category[MSVCRT_LC_COLLATE].locale;
393 sprintf(current_lc_all,
394 "LC_COLLATE=%s;LC_CTYPE=%s;LC_MONETARY=%s;LC_NUMERIC=%s;LC_TIME=%s",
395 locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
396 locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
397 locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
398 locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
399 locinfo->lc_category[MSVCRT_LC_TIME].locale);
401 return current_lc_all;
405 /*********************************************************************
406 * wsetlocale (MSVCRT.@)
408 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
410 static MSVCRT_wchar_t fake[] = {
411 'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
412 'S','t','a','t','e','s','.','1','2','5','2',0 };
414 FIXME("%d %s\n", category, debugstr_w(locale));
416 return fake;
419 /*********************************************************************
420 * _Getdays (MSVCRT.@)
422 char* CDECL _Getdays(void)
424 MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
425 int i, len, size;
426 char *out;
428 TRACE("\n");
430 size = cur->str[2*7]-cur->str[0];
431 out = MSVCRT_malloc(size+1);
432 if(!out)
433 return NULL;
435 size = 0;
436 for(i=0; i<7; i++) {
437 out[size++] = ':';
438 len = strlen(cur->str[i]);
439 memcpy(&out[size], cur->str[i], len);
440 size += len;
442 out[size++] = ':';
443 len = strlen(cur->str[7+i]);
444 memcpy(&out[size], cur->str[7+i], len);
445 size += len;
447 out[size] = '\0';
449 return out;
452 /*********************************************************************
453 * _Getmonths (MSVCRT.@)
455 char* CDECL _Getmonths(void)
457 static const int months_offset = 14;
459 MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
460 int i, len, size;
461 char *out;
463 TRACE("\n");
465 size = cur->str[months_offset+2*12]-cur->str[months_offset];
466 out = MSVCRT_malloc(size+1);
467 if(!out)
468 return NULL;
470 size = 0;
471 for(i=0; i<12; i++) {
472 out[size++] = ':';
473 len = strlen(cur->str[months_offset+i]);
474 memcpy(&out[size], cur->str[months_offset+i], len);
475 size += len;
477 out[size++] = ':';
478 len = strlen(cur->str[months_offset+12+i]);
479 memcpy(&out[size], cur->str[months_offset+12+i], len);
480 size += len;
482 out[size] = '\0';
484 return out;
487 /*********************************************************************
488 * _Gettnames (MSVCRT.@)
490 void* CDECL _Gettnames(void)
492 MSVCRT___lc_time_data *ret, *cur = get_locinfo()->lc_time_curr;
493 int i, size = sizeof(MSVCRT___lc_time_data);
495 TRACE("\n");
497 for(i=0; i<sizeof(cur->str)/sizeof(cur->str[0]); i++)
498 size += strlen(cur->str[i])+1;
500 ret = MSVCRT_malloc(size);
501 if(!ret)
502 return NULL;
503 memcpy(ret, cur, size);
505 size = 0;
506 for(i=0; i<sizeof(cur->str)/sizeof(cur->str[0]); i++) {
507 ret->str[i] = &ret->data[size];
508 size += strlen(&ret->data[size])+1;
511 return ret;
514 /*********************************************************************
515 * _Strftime (MSVCRT.@)
517 const char* CDECL _Strftime(char *out, unsigned int len, const char *fmt,
518 const void *tm, void *foo)
520 /* FIXME: */
521 TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
522 return "";
525 /*********************************************************************
526 * __crtLCMapStringA (MSVCRT.@)
528 int CDECL __crtLCMapStringA(
529 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
530 int dstlen, unsigned int codepage, int xflag
532 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
533 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
534 /* FIXME: A bit incorrect. But msvcrt itself just converts its
535 * arguments to wide strings and then calls LCMapStringW
537 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
540 /*********************************************************************
541 * __crtLCMapStringW (MSVCRT.@)
543 int CDECL __crtLCMapStringW(LCID lcid, DWORD mapflags, const MSVCRT_wchar_t *src,
544 int srclen, MSVCRT_wchar_t *dst, int dstlen, unsigned int codepage, int xflag)
546 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
547 lcid, mapflags, debugstr_w(src), srclen, dst, dstlen, codepage, xflag);
549 return LCMapStringW(lcid, mapflags, src, srclen, dst, dstlen);
552 /*********************************************************************
553 * __crtCompareStringA (MSVCRT.@)
555 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
556 const char *src2, int len2 )
558 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
559 lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
560 /* FIXME: probably not entirely right */
561 return CompareStringA( lcid, flags, src1, len1, src2, len2 );
564 /*********************************************************************
565 * __crtCompareStringW (MSVCRT.@)
567 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
568 const MSVCRT_wchar_t *src2, int len2 )
570 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
571 lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
572 /* FIXME: probably not entirely right */
573 return CompareStringW( lcid, flags, src1, len1, src2, len2 );
576 /*********************************************************************
577 * __crtGetLocaleInfoW (MSVCRT.@)
579 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
581 FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
582 /* FIXME: probably not entirely right */
583 return GetLocaleInfoW( lcid, type, buffer, len );
586 /*********************************************************************
587 * btowc(MSVCRT.@)
589 MSVCRT_wint_t CDECL MSVCRT_btowc(int c)
591 unsigned char letter = c;
592 MSVCRT_wchar_t ret;
594 if(!MultiByteToWideChar(get_locinfo()->lc_handle[MSVCRT_LC_CTYPE],
595 0, (LPCSTR)&letter, 1, &ret, 1))
596 return 0;
598 return ret;
601 /*********************************************************************
602 * __crtGetStringTypeW(MSVCRT.@)
604 * This function was accepting different number of arguments in older
605 * versions of msvcrt.
607 BOOL CDECL __crtGetStringTypeW(DWORD unk, DWORD type,
608 MSVCRT_wchar_t *buffer, int len, WORD *out)
610 FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
611 unk, type, buffer, len, out);
613 return GetStringTypeW(type, buffer, len, out);
616 /*********************************************************************
617 * localeconv (MSVCRT.@)
619 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
621 return get_locinfo()->lconv;
624 /*********************************************************************
625 * __lconv_init (MSVCRT.@)
627 int CDECL __lconv_init(void)
629 /* this is used to make chars unsigned */
630 charmax = 255;
631 return 0;
634 /*********************************************************************
635 * ___lc_handle_func (MSVCRT.@)
637 LCID* CDECL ___lc_handle_func(void)
639 return MSVCRT___lc_handle;
642 /*********************************************************************
643 * ___lc_codepage_func (MSVCRT.@)
645 unsigned int CDECL ___lc_codepage_func(void)
647 return MSVCRT___lc_codepage;
650 /*********************************************************************
651 * ___lc_collate_cp_func (MSVCRT.@)
653 int CDECL ___lc_collate_cp_func(void)
655 return get_locinfo()->lc_collate_cp;
658 /* INTERNAL: frees MSVCRT_pthreadlocinfo struct */
659 void free_locinfo(MSVCRT_pthreadlocinfo locinfo)
661 int i;
663 if(!locinfo)
664 return;
666 if(InterlockedDecrement(&locinfo->refcount))
667 return;
669 for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
670 MSVCRT_free(locinfo->lc_category[i].locale);
671 MSVCRT_free(locinfo->lc_category[i].refcount);
674 if(locinfo->lconv) {
675 MSVCRT_free(locinfo->lconv->decimal_point);
676 MSVCRT_free(locinfo->lconv->thousands_sep);
677 MSVCRT_free(locinfo->lconv->grouping);
678 MSVCRT_free(locinfo->lconv->int_curr_symbol);
679 MSVCRT_free(locinfo->lconv->currency_symbol);
680 MSVCRT_free(locinfo->lconv->mon_decimal_point);
681 MSVCRT_free(locinfo->lconv->mon_thousands_sep);
682 MSVCRT_free(locinfo->lconv->mon_grouping);
683 MSVCRT_free(locinfo->lconv->positive_sign);
684 MSVCRT_free(locinfo->lconv->negative_sign);
686 MSVCRT_free(locinfo->lconv_intl_refcount);
687 MSVCRT_free(locinfo->lconv_num_refcount);
688 MSVCRT_free(locinfo->lconv_mon_refcount);
689 MSVCRT_free(locinfo->lconv);
691 MSVCRT_free(locinfo->ctype1_refcount);
692 MSVCRT_free(locinfo->ctype1);
694 MSVCRT_free(locinfo->pclmap);
695 MSVCRT_free(locinfo->pcumap);
697 MSVCRT_free(locinfo->lc_time_curr);
699 MSVCRT_free(locinfo);
702 /* INTERNAL: frees MSVCRT_pthreadmbcinfo struct */
703 void free_mbcinfo(MSVCRT_pthreadmbcinfo mbcinfo)
705 if(!mbcinfo)
706 return;
708 if(InterlockedDecrement(&mbcinfo->refcount))
709 return;
711 MSVCRT_free(mbcinfo);
714 /* _get_current_locale - not exported in native msvcrt */
715 MSVCRT__locale_t CDECL MSVCRT__get_current_locale(void)
717 MSVCRT__locale_t loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
718 if(!loc)
719 return NULL;
721 loc->locinfo = get_locinfo();
722 loc->mbcinfo = get_mbcinfo();
723 InterlockedIncrement(&loc->locinfo->refcount);
724 InterlockedIncrement(&loc->mbcinfo->refcount);
725 return loc;
728 /* _free_locale - not exported in native msvcrt */
729 void CDECL MSVCRT__free_locale(MSVCRT__locale_t locale)
731 if (!locale)
732 return;
734 free_locinfo(locale->locinfo);
735 free_mbcinfo(locale->mbcinfo);
736 MSVCRT_free(locale);
739 /* _create_locale - not exported in native msvcrt */
740 MSVCRT__locale_t CDECL MSVCRT__create_locale(int category, const char *locale)
742 static const DWORD time_data[] = {
743 LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1, LOCALE_SABBREVDAYNAME2,
744 LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4, LOCALE_SABBREVDAYNAME5,
745 LOCALE_SABBREVDAYNAME6,
746 LOCALE_SDAYNAME7, LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3,
747 LOCALE_SDAYNAME4, LOCALE_SDAYNAME5, LOCALE_SDAYNAME6,
748 LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2, LOCALE_SABBREVMONTHNAME3,
749 LOCALE_SABBREVMONTHNAME4, LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
750 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8, LOCALE_SABBREVMONTHNAME9,
751 LOCALE_SABBREVMONTHNAME10, LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12,
752 LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME4,
753 LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8,
754 LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12,
755 LOCALE_S1159, LOCALE_S2359,
756 LOCALE_SSHORTDATE, LOCALE_SLONGDATE,
757 LOCALE_STIMEFORMAT
759 static const char collate[] = "COLLATE=";
760 static const char ctype[] = "CTYPE=";
761 static const char monetary[] = "MONETARY=";
762 static const char numeric[] = "NUMERIC=";
763 static const char time[] = "TIME=";
765 MSVCRT__locale_t loc;
766 LCID lcid[6] = { 0 };
767 char buf[256];
768 int i, ret, size;
770 TRACE("(%d %s)\n", category, locale);
772 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
773 return NULL;
775 if(locale[0]=='C' && !locale[1])
776 lcid[0] = CP_ACP;
777 else if(!locale[0])
778 lcid[0] = GetSystemDefaultLCID();
779 else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
780 const char *p;
782 while(1) {
783 locale += 3; /* LC_ */
784 if(!memcmp(locale, collate, sizeof(collate)-1)) {
785 i = MSVCRT_LC_COLLATE;
786 locale += sizeof(collate)-1;
787 } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
788 i = MSVCRT_LC_CTYPE;
789 locale += sizeof(ctype)-1;
790 } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
791 i = MSVCRT_LC_MONETARY;
792 locale += sizeof(monetary)-1;
793 } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
794 i = MSVCRT_LC_NUMERIC;
795 locale += sizeof(numeric)-1;
796 } else if(!memcmp(locale, time, sizeof(time)-1)) {
797 i = MSVCRT_LC_TIME;
798 locale += sizeof(time)-1;
799 } else
800 return NULL;
802 p = strchr(locale, ';');
803 if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0'))
804 lcid[i] = 0;
805 else if(p) {
806 memcpy(buf, locale, p-locale);
807 buf[p-locale] = '\0';
808 lcid[i] = MSVCRT_locale_to_LCID(buf);
809 } else
810 lcid[i] = MSVCRT_locale_to_LCID(locale);
812 if(lcid[i] == -1)
813 return NULL;
815 if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
816 break;
818 locale = p+1;
820 } else {
821 lcid[0] = MSVCRT_locale_to_LCID(locale);
822 if(lcid[0] == -1)
823 return NULL;
826 for(i=1; i<6; i++) {
827 if(!lcid[i])
828 lcid[i] = lcid[0];
831 loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
832 if(!loc)
833 return NULL;
835 loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
836 if(!loc->locinfo) {
837 MSVCRT_free(loc);
838 return NULL;
841 loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
842 if(!loc->mbcinfo) {
843 MSVCRT_free(loc->locinfo);
844 MSVCRT_free(loc);
845 return NULL;
848 memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
849 loc->locinfo->refcount = 1;
850 loc->mbcinfo->refcount = 1;
852 loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
853 if(!loc->locinfo->lconv) {
854 MSVCRT__free_locale(loc);
855 return NULL;
857 memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
859 loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
860 loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
861 if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
862 MSVCRT__free_locale(loc);
863 return NULL;
866 if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
867 if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE], loc, MSVCRT_LC_COLLATE)) {
868 MSVCRT__free_locale(loc);
869 return NULL;
872 loc->locinfo->lc_collate_cp = loc->locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage;
873 } else
874 loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale = MSVCRT__strdup("C");
876 if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
877 CPINFO cp;
878 int j;
880 if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE], loc, MSVCRT_LC_CTYPE)) {
881 MSVCRT__free_locale(loc);
882 return NULL;
885 loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
886 loc->locinfo->lc_clike = 1;
887 if(!GetCPInfo(loc->locinfo->lc_codepage, &cp)) {
888 MSVCRT__free_locale(loc);
889 return NULL;
891 loc->locinfo->mb_cur_max = cp.MaxCharSize;
893 loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
894 loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
895 if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
896 MSVCRT__free_locale(loc);
897 return NULL;
900 *loc->locinfo->ctype1_refcount = 1;
901 loc->locinfo->ctype1[0] = 0;
902 loc->locinfo->pctype = loc->locinfo->ctype1+1;
904 buf[1] = buf[2] = '\0';
905 for(i=1; i<257; i++) {
906 buf[0] = i-1;
908 /* builtin GetStringTypeA doesn't set output to 0 on invalid input */
909 loc->locinfo->ctype1[i] = 0;
911 GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
912 1, loc->locinfo->ctype1+i);
915 for(i=0; cp.LeadByte[i+1]!=0; i+=2)
916 for(j=cp.LeadByte[i]; j<=cp.LeadByte[i+1]; j++)
917 loc->locinfo->ctype1[j+1] |= MSVCRT__LEADBYTE;
918 } else {
919 loc->locinfo->lc_clike = 1;
920 loc->locinfo->mb_cur_max = 1;
921 loc->locinfo->pctype = MSVCRT__ctype+1;
922 loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale = MSVCRT__strdup("C");
925 for(i=0; i<256; i++) {
926 if(loc->locinfo->pctype[i] & MSVCRT__LEADBYTE)
927 buf[i] = ' ';
928 else
929 buf[i] = i;
933 if(lcid[MSVCRT_LC_CTYPE]) {
934 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
935 (char*)loc->locinfo->pclmap, 256);
936 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
937 (char*)loc->locinfo->pcumap, 256);
938 } else {
939 for(i=0; i<256; i++) {
940 loc->locinfo->pclmap[i] = (i>='A' && i<='Z' ? i-'A'+'a' : i);
941 loc->locinfo->pcumap[i] = (i>='a' && i<='z' ? i-'a'+'A' : i);
945 _setmbcp_l(loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage, lcid[MSVCRT_LC_CTYPE], loc->mbcinfo);
947 if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
948 if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY], loc, MSVCRT_LC_MONETARY)) {
949 MSVCRT__free_locale(loc);
950 return NULL;
953 loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
954 loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
955 if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
956 MSVCRT__free_locale(loc);
957 return NULL;
960 *loc->locinfo->lconv_intl_refcount = 1;
961 *loc->locinfo->lconv_mon_refcount = 1;
963 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
964 |LOCALE_NOUSEROVERRIDE, buf, 256);
965 if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(i)))
966 memcpy(loc->locinfo->lconv->int_curr_symbol, buf, i);
967 else {
968 MSVCRT__free_locale(loc);
969 return NULL;
972 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
973 |LOCALE_NOUSEROVERRIDE, buf, 256);
974 if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(i)))
975 memcpy(loc->locinfo->lconv->currency_symbol, buf, i);
976 else {
977 MSVCRT__free_locale(loc);
978 return NULL;
981 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
982 |LOCALE_NOUSEROVERRIDE, buf, 256);
983 if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(i)))
984 memcpy(loc->locinfo->lconv->mon_decimal_point, buf, i);
985 else {
986 MSVCRT__free_locale(loc);
987 return NULL;
990 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
991 |LOCALE_NOUSEROVERRIDE, buf, 256);
992 if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(i)))
993 memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, i);
994 else {
995 MSVCRT__free_locale(loc);
996 return NULL;
999 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING
1000 |LOCALE_NOUSEROVERRIDE, buf, 256);
1001 if(i>1)
1002 i = i/2 + (buf[i-2]=='0'?0:1);
1003 if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(i))) {
1004 for(i=0; buf[i+1]==';'; i+=2)
1005 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1006 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1007 if(buf[i] != '0')
1008 loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
1009 } else {
1010 MSVCRT__free_locale(loc);
1011 return NULL;
1014 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
1015 |LOCALE_NOUSEROVERRIDE, buf, 256);
1016 if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(i)))
1017 memcpy(loc->locinfo->lconv->positive_sign, buf, i);
1018 else {
1019 MSVCRT__free_locale(loc);
1020 return NULL;
1023 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
1024 |LOCALE_NOUSEROVERRIDE, buf, 256);
1025 if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(i)))
1026 memcpy(loc->locinfo->lconv->negative_sign, buf, i);
1027 else {
1028 MSVCRT__free_locale(loc);
1029 return NULL;
1032 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS
1033 |LOCALE_NOUSEROVERRIDE, buf, 256))
1034 loc->locinfo->lconv->int_frac_digits = atoi(buf);
1035 else {
1036 MSVCRT__free_locale(loc);
1037 return NULL;
1040 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS
1041 |LOCALE_NOUSEROVERRIDE, buf, 256))
1042 loc->locinfo->lconv->frac_digits = atoi(buf);
1043 else {
1044 MSVCRT__free_locale(loc);
1045 return NULL;
1048 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES
1049 |LOCALE_NOUSEROVERRIDE, buf, 256))
1050 loc->locinfo->lconv->p_cs_precedes = atoi(buf);
1051 else {
1052 MSVCRT__free_locale(loc);
1053 return NULL;
1056 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE
1057 |LOCALE_NOUSEROVERRIDE, buf, 256))
1058 loc->locinfo->lconv->p_sep_by_space = atoi(buf);
1059 else {
1060 MSVCRT__free_locale(loc);
1061 return NULL;
1064 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES
1065 |LOCALE_NOUSEROVERRIDE, buf, 256))
1066 loc->locinfo->lconv->n_cs_precedes = atoi(buf);
1067 else {
1068 MSVCRT__free_locale(loc);
1069 return NULL;
1072 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE
1073 |LOCALE_NOUSEROVERRIDE, buf, 256))
1074 loc->locinfo->lconv->n_sep_by_space = atoi(buf);
1075 else {
1076 MSVCRT__free_locale(loc);
1077 return NULL;
1080 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN
1081 |LOCALE_NOUSEROVERRIDE, buf, 256))
1082 loc->locinfo->lconv->p_sign_posn = atoi(buf);
1083 else {
1084 MSVCRT__free_locale(loc);
1085 return NULL;
1088 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN
1089 |LOCALE_NOUSEROVERRIDE, buf, 256))
1090 loc->locinfo->lconv->n_sign_posn = atoi(buf);
1091 else {
1092 MSVCRT__free_locale(loc);
1093 return NULL;
1095 } else {
1096 loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
1097 loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
1098 loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
1099 loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
1100 loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
1101 loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
1102 loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
1104 if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
1105 || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
1106 || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
1107 || !loc->locinfo->lconv->negative_sign) {
1108 MSVCRT__free_locale(loc);
1109 return NULL;
1112 loc->locinfo->lconv->int_curr_symbol[0] = '\0';
1113 loc->locinfo->lconv->currency_symbol[0] = '\0';
1114 loc->locinfo->lconv->mon_decimal_point[0] = '\0';
1115 loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
1116 loc->locinfo->lconv->mon_grouping[0] = '\0';
1117 loc->locinfo->lconv->positive_sign[0] = '\0';
1118 loc->locinfo->lconv->negative_sign[0] = '\0';
1119 loc->locinfo->lconv->int_frac_digits = 127;
1120 loc->locinfo->lconv->frac_digits = 127;
1121 loc->locinfo->lconv->p_cs_precedes = 127;
1122 loc->locinfo->lconv->p_sep_by_space = 127;
1123 loc->locinfo->lconv->n_cs_precedes = 127;
1124 loc->locinfo->lconv->n_sep_by_space = 127;
1125 loc->locinfo->lconv->p_sign_posn = 127;
1126 loc->locinfo->lconv->n_sign_posn = 127;
1128 loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale = MSVCRT__strdup("C");
1131 if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
1132 if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC], loc, MSVCRT_LC_NUMERIC)) {
1133 MSVCRT__free_locale(loc);
1134 return NULL;
1137 if(!loc->locinfo->lconv_intl_refcount)
1138 loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1139 loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1140 if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
1141 MSVCRT__free_locale(loc);
1142 return NULL;
1145 *loc->locinfo->lconv_intl_refcount = 1;
1146 *loc->locinfo->lconv_num_refcount = 1;
1148 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1149 |LOCALE_NOUSEROVERRIDE, buf, 256);
1150 if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(i)))
1151 memcpy(loc->locinfo->lconv->decimal_point, buf, i);
1152 else {
1153 MSVCRT__free_locale(loc);
1154 return NULL;
1157 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1158 |LOCALE_NOUSEROVERRIDE, buf, 256);
1159 if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(i)))
1160 memcpy(loc->locinfo->lconv->thousands_sep, buf, i);
1161 else {
1162 MSVCRT__free_locale(loc);
1163 return NULL;
1166 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING
1167 |LOCALE_NOUSEROVERRIDE, buf, 256);
1168 if(i>1)
1169 i = i/2 + (buf[i-2]=='0'?0:1);
1170 if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(i))) {
1171 for(i=0; buf[i+1]==';'; i+=2)
1172 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1173 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1174 if(buf[i] != '0')
1175 loc->locinfo->lconv->grouping[i/2+1] = 127;
1176 } else {
1177 MSVCRT__free_locale(loc);
1178 return NULL;
1180 } else {
1181 loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1182 loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1183 loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1184 if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
1185 || !loc->locinfo->lconv->grouping) {
1186 MSVCRT__free_locale(loc);
1187 return NULL;
1190 loc->locinfo->lconv->decimal_point[0] = '.';
1191 loc->locinfo->lconv->decimal_point[1] = '\0';
1192 loc->locinfo->lconv->thousands_sep[0] = '\0';
1193 loc->locinfo->lconv->grouping[0] = '\0';
1195 loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale = MSVCRT__strdup("C");
1198 if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1199 if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME], loc, MSVCRT_LC_TIME)) {
1200 MSVCRT__free_locale(loc);
1201 return NULL;
1203 } else
1204 loc->locinfo->lc_category[MSVCRT_LC_TIME].locale = MSVCRT__strdup("C");
1206 size = sizeof(MSVCRT___lc_time_data);
1207 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1208 ret = GetLocaleInfoA(lcid[MSVCRT_LC_TIME], time_data[i]
1209 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1210 if(!ret) {
1211 MSVCRT__free_locale(loc);
1212 return NULL;
1214 size += ret;
1216 ret = GetLocaleInfoW(lcid[MSVCRT_LC_TIME], time_data[i]
1217 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1218 if(!ret) {
1219 MSVCRT__free_locale(loc);
1220 return NULL;
1222 size += ret*sizeof(MSVCRT_wchar_t);
1225 loc->locinfo->lc_time_curr = MSVCRT_malloc(size);
1226 if(!loc->locinfo->lc_time_curr) {
1227 MSVCRT__free_locale(loc);
1228 return NULL;
1231 ret = 0;
1232 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1233 loc->locinfo->lc_time_curr->str[i] = &loc->locinfo->lc_time_curr->data[ret];
1234 ret += GetLocaleInfoA(lcid[MSVCRT_LC_TIME], time_data[i]|LOCALE_NOUSEROVERRIDE,
1235 &loc->locinfo->lc_time_curr->data[ret], size-ret);
1237 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1238 loc->locinfo->lc_time_curr->wstr[i] = (MSVCRT_wchar_t*)&loc->locinfo->lc_time_curr->data[ret];
1239 ret += GetLocaleInfoW(lcid[MSVCRT_LC_TIME], time_data[i]|LOCALE_NOUSEROVERRIDE,
1240 (MSVCRT_wchar_t*)&loc->locinfo->lc_time_curr->data[ret], size-ret)*sizeof(MSVCRT_wchar_t);
1242 loc->locinfo->lc_time_curr->lcid = lcid[MSVCRT_LC_TIME];
1244 return loc;
1247 /*********************************************************************
1248 * setlocale (MSVCRT.@)
1250 char* CDECL MSVCRT_setlocale(int category, const char* locale)
1252 MSVCRT__locale_t loc;
1253 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
1255 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1256 return NULL;
1258 if(!locale) {
1259 if(category == MSVCRT_LC_ALL)
1260 return construct_lc_all(locinfo);
1262 return locinfo->lc_category[category].locale;
1265 loc = MSVCRT__create_locale(category, locale);
1266 if(!loc) {
1267 WARN("%d %s failed\n", category, locale);
1268 return NULL;
1271 LOCK_LOCALE;
1273 switch(category) {
1274 case MSVCRT_LC_ALL:
1275 case MSVCRT_LC_COLLATE:
1276 locinfo->lc_collate_cp = loc->locinfo->lc_collate_cp;
1277 locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1278 loc->locinfo->lc_handle[MSVCRT_LC_COLLATE];
1279 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1280 (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1281 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1282 (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1284 if(category != MSVCRT_LC_ALL)
1285 break;
1286 /* fall through */
1287 case MSVCRT_LC_CTYPE:
1288 locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1289 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE];
1290 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1291 (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1292 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1293 (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1295 locinfo->lc_codepage = loc->locinfo->lc_codepage;
1296 locinfo->lc_clike = loc->locinfo->lc_clike;
1297 locinfo->mb_cur_max = loc->locinfo->mb_cur_max;
1299 swap_pointers((void**)&locinfo->ctype1_refcount,
1300 (void**)&loc->locinfo->ctype1_refcount);
1301 swap_pointers((void**)&locinfo->ctype1, (void**)&loc->locinfo->ctype1);
1302 swap_pointers((void**)&locinfo->pctype, (void**)&loc->locinfo->pctype);
1303 swap_pointers((void**)&locinfo->pclmap, (void**)&loc->locinfo->pclmap);
1304 swap_pointers((void**)&locinfo->pcumap, (void**)&loc->locinfo->pcumap);
1306 if(category != MSVCRT_LC_ALL)
1307 break;
1308 /* fall through */
1309 case MSVCRT_LC_MONETARY:
1310 locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1311 loc->locinfo->lc_handle[MSVCRT_LC_MONETARY];
1312 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1313 (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1314 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1315 (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1317 swap_pointers((void**)&locinfo->lconv->int_curr_symbol,
1318 (void**)&loc->locinfo->lconv->int_curr_symbol);
1319 swap_pointers((void**)&locinfo->lconv->currency_symbol,
1320 (void**)&loc->locinfo->lconv->currency_symbol);
1321 swap_pointers((void**)&locinfo->lconv->mon_decimal_point,
1322 (void**)&loc->locinfo->lconv->mon_decimal_point);
1323 swap_pointers((void**)&locinfo->lconv->mon_thousands_sep,
1324 (void**)&loc->locinfo->lconv->mon_thousands_sep);
1325 swap_pointers((void**)&locinfo->lconv->mon_grouping,
1326 (void**)&loc->locinfo->lconv->mon_grouping);
1327 swap_pointers((void**)&locinfo->lconv->positive_sign,
1328 (void**)&loc->locinfo->lconv->positive_sign);
1329 swap_pointers((void**)&locinfo->lconv->negative_sign,
1330 (void**)&loc->locinfo->lconv->negative_sign);
1331 locinfo->lconv->int_frac_digits = loc->locinfo->lconv->int_frac_digits;
1332 locinfo->lconv->frac_digits = loc->locinfo->lconv->frac_digits;
1333 locinfo->lconv->p_cs_precedes = loc->locinfo->lconv->p_cs_precedes;
1334 locinfo->lconv->p_sep_by_space = loc->locinfo->lconv->p_sep_by_space;
1335 locinfo->lconv->n_cs_precedes = loc->locinfo->lconv->n_cs_precedes;
1336 locinfo->lconv->n_sep_by_space = loc->locinfo->lconv->n_sep_by_space;
1337 locinfo->lconv->p_sign_posn = loc->locinfo->lconv->p_sign_posn;
1338 locinfo->lconv->n_sign_posn = loc->locinfo->lconv->n_sign_posn;
1340 if(category != MSVCRT_LC_ALL)
1341 break;
1342 /* fall through */
1343 case MSVCRT_LC_NUMERIC:
1344 locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1345 loc->locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1346 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1347 (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1348 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1349 (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1351 swap_pointers((void**)&locinfo->lconv->decimal_point,
1352 (void**)&loc->locinfo->lconv->decimal_point);
1353 swap_pointers((void**)&locinfo->lconv->thousands_sep,
1354 (void**)&loc->locinfo->lconv->thousands_sep);
1355 swap_pointers((void**)&locinfo->lconv->grouping,
1356 (void**)&loc->locinfo->lconv->grouping);
1358 if(category != MSVCRT_LC_ALL)
1359 break;
1360 /* fall through */
1361 case MSVCRT_LC_TIME:
1362 locinfo->lc_handle[MSVCRT_LC_TIME] =
1363 loc->locinfo->lc_handle[MSVCRT_LC_TIME];
1364 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].locale,
1365 (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].locale);
1366 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1367 (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].refcount);
1368 swap_pointers((void**)&locinfo->lc_time_curr,
1369 (void**)&loc->locinfo->lc_time_curr);
1371 if(category != MSVCRT_LC_ALL)
1372 break;
1375 MSVCRT__free_locale(loc);
1376 UNLOCK_LOCALE;
1378 if(locinfo == MSVCRT_locale->locinfo) {
1379 int i;
1381 MSVCRT___lc_codepage = locinfo->lc_codepage;
1382 MSVCRT___lc_collate_cp = locinfo->lc_collate_cp;
1383 MSVCRT___mb_cur_max = locinfo->mb_cur_max;
1384 MSVCRT__pctype = locinfo->pctype;
1385 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1386 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1389 if(category == MSVCRT_LC_ALL)
1390 return construct_lc_all(locinfo);
1392 return locinfo->lc_category[category].locale;
1395 /* _configthreadlocale - not exported in native msvcrt */
1396 int CDECL _configthreadlocale(int type)
1398 thread_data_t *data = msvcrt_get_thread_data();
1399 MSVCRT__locale_t locale;
1400 int ret;
1402 if(!data)
1403 return -1;
1405 ret = (data->have_locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1407 if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1408 if(!data->have_locale) {
1409 /* Copy current global locale */
1410 locale = MSVCRT__create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
1411 if(!locale)
1412 return -1;
1414 data->locinfo = locale->locinfo;
1415 data->mbcinfo = locale->mbcinfo;
1416 data->have_locale = TRUE;
1417 MSVCRT_free(locale);
1420 return ret;
1423 if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1424 if(data->have_locale) {
1425 free_locinfo(data->locinfo);
1426 free_mbcinfo(data->mbcinfo);
1427 data->locinfo = MSVCRT_locale->locinfo;
1428 data->mbcinfo = MSVCRT_locale->mbcinfo;
1429 data->have_locale = FALSE;
1432 return ret;
1435 if(!type)
1436 return ret;
1438 return -1;
1441 BOOL msvcrt_init_locale(void)
1443 int i;
1445 LOCK_LOCALE;
1446 MSVCRT_locale = MSVCRT__create_locale(0, "C");
1447 UNLOCK_LOCALE;
1448 if(!MSVCRT_locale)
1449 return FALSE;
1451 MSVCRT___lc_codepage = MSVCRT_locale->locinfo->lc_codepage;
1452 MSVCRT___lc_collate_cp = MSVCRT_locale->locinfo->lc_collate_cp;
1453 MSVCRT___mb_cur_max = MSVCRT_locale->locinfo->mb_cur_max;
1454 MSVCRT__pctype = MSVCRT_locale->locinfo->pctype;
1455 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1456 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1457 _setmbcp(_MB_CP_ANSI);
1458 return TRUE;