vbscript: Fixed locale-related test failures.
[wine/multimedia.git] / dlls / msvcrt / locale.c
blobb26a9c8ef4bd1ca8c2a39c13a9206602bb2b86c7
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 "portuguese", "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.names.short_mon[0]-cur->str.names.short_wday[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.names.short_wday[i]);
439 memcpy(&out[size], cur->str.names.short_wday[i], len);
440 size += len;
442 out[size++] = ':';
443 len = strlen(cur->str.names.wday[i]);
444 memcpy(&out[size], cur->str.names.wday[i], len);
445 size += len;
447 out[size] = '\0';
449 return out;
452 /*********************************************************************
453 * _Getmonths (MSVCRT.@)
455 char* CDECL _Getmonths(void)
457 MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
458 int i, len, size;
459 char *out;
461 TRACE("\n");
463 size = cur->str.names.am-cur->str.names.short_mon[0];
464 out = MSVCRT_malloc(size+1);
465 if(!out)
466 return NULL;
468 size = 0;
469 for(i=0; i<12; i++) {
470 out[size++] = ':';
471 len = strlen(cur->str.names.short_mon[i]);
472 memcpy(&out[size], cur->str.names.short_mon[i], len);
473 size += len;
475 out[size++] = ':';
476 len = strlen(cur->str.names.mon[i]);
477 memcpy(&out[size], cur->str.names.mon[i], len);
478 size += len;
480 out[size] = '\0';
482 return out;
485 /*********************************************************************
486 * _Gettnames (MSVCRT.@)
488 void* CDECL _Gettnames(void)
490 MSVCRT___lc_time_data *ret, *cur = get_locinfo()->lc_time_curr;
491 int i, size = sizeof(MSVCRT___lc_time_data);
493 TRACE("\n");
495 for(i=0; i<sizeof(cur->str.str)/sizeof(cur->str.str[0]); i++)
496 size += strlen(cur->str.str[i])+1;
498 ret = MSVCRT_malloc(size);
499 if(!ret)
500 return NULL;
501 memcpy(ret, cur, size);
503 size = 0;
504 for(i=0; i<sizeof(cur->str.str)/sizeof(cur->str.str[0]); i++) {
505 ret->str.str[i] = &ret->data[size];
506 size += strlen(&ret->data[size])+1;
509 return ret;
512 /*********************************************************************
513 * __crtLCMapStringA (MSVCRT.@)
515 int CDECL __crtLCMapStringA(
516 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
517 int dstlen, unsigned int codepage, int xflag
519 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
520 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
521 /* FIXME: A bit incorrect. But msvcrt itself just converts its
522 * arguments to wide strings and then calls LCMapStringW
524 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
527 /*********************************************************************
528 * __crtLCMapStringW (MSVCRT.@)
530 int CDECL __crtLCMapStringW(LCID lcid, DWORD mapflags, const MSVCRT_wchar_t *src,
531 int srclen, MSVCRT_wchar_t *dst, int dstlen, unsigned int codepage, int xflag)
533 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
534 lcid, mapflags, debugstr_w(src), srclen, dst, dstlen, codepage, xflag);
536 return LCMapStringW(lcid, mapflags, src, srclen, dst, dstlen);
539 /*********************************************************************
540 * __crtCompareStringA (MSVCRT.@)
542 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
543 const char *src2, int len2 )
545 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
546 lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
547 /* FIXME: probably not entirely right */
548 return CompareStringA( lcid, flags, src1, len1, src2, len2 );
551 /*********************************************************************
552 * __crtCompareStringW (MSVCRT.@)
554 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
555 const MSVCRT_wchar_t *src2, int len2 )
557 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
558 lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
559 /* FIXME: probably not entirely right */
560 return CompareStringW( lcid, flags, src1, len1, src2, len2 );
563 /*********************************************************************
564 * __crtGetLocaleInfoW (MSVCRT.@)
566 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
568 FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
569 /* FIXME: probably not entirely right */
570 return GetLocaleInfoW( lcid, type, buffer, len );
573 /*********************************************************************
574 * btowc(MSVCRT.@)
576 MSVCRT_wint_t CDECL MSVCRT_btowc(int c)
578 unsigned char letter = c;
579 MSVCRT_wchar_t ret;
581 if(!MultiByteToWideChar(get_locinfo()->lc_handle[MSVCRT_LC_CTYPE],
582 0, (LPCSTR)&letter, 1, &ret, 1))
583 return 0;
585 return ret;
588 /*********************************************************************
589 * __crtGetStringTypeW(MSVCRT.@)
591 * This function was accepting different number of arguments in older
592 * versions of msvcrt.
594 BOOL CDECL __crtGetStringTypeW(DWORD unk, DWORD type,
595 MSVCRT_wchar_t *buffer, int len, WORD *out)
597 FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
598 unk, type, buffer, len, out);
600 return GetStringTypeW(type, buffer, len, out);
603 /*********************************************************************
604 * localeconv (MSVCRT.@)
606 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
608 return get_locinfo()->lconv;
611 /*********************************************************************
612 * __lconv_init (MSVCRT.@)
614 int CDECL __lconv_init(void)
616 /* this is used to make chars unsigned */
617 charmax = 255;
618 return 0;
621 /*********************************************************************
622 * ___lc_handle_func (MSVCRT.@)
624 LCID* CDECL ___lc_handle_func(void)
626 return MSVCRT___lc_handle;
629 /*********************************************************************
630 * ___lc_codepage_func (MSVCRT.@)
632 unsigned int CDECL ___lc_codepage_func(void)
634 return MSVCRT___lc_codepage;
637 /*********************************************************************
638 * ___lc_collate_cp_func (MSVCRT.@)
640 int CDECL ___lc_collate_cp_func(void)
642 return get_locinfo()->lc_collate_cp;
645 /* INTERNAL: frees MSVCRT_pthreadlocinfo struct */
646 void free_locinfo(MSVCRT_pthreadlocinfo locinfo)
648 int i;
650 if(!locinfo)
651 return;
653 if(InterlockedDecrement(&locinfo->refcount))
654 return;
656 for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
657 MSVCRT_free(locinfo->lc_category[i].locale);
658 MSVCRT_free(locinfo->lc_category[i].refcount);
661 if(locinfo->lconv) {
662 MSVCRT_free(locinfo->lconv->decimal_point);
663 MSVCRT_free(locinfo->lconv->thousands_sep);
664 MSVCRT_free(locinfo->lconv->grouping);
665 MSVCRT_free(locinfo->lconv->int_curr_symbol);
666 MSVCRT_free(locinfo->lconv->currency_symbol);
667 MSVCRT_free(locinfo->lconv->mon_decimal_point);
668 MSVCRT_free(locinfo->lconv->mon_thousands_sep);
669 MSVCRT_free(locinfo->lconv->mon_grouping);
670 MSVCRT_free(locinfo->lconv->positive_sign);
671 MSVCRT_free(locinfo->lconv->negative_sign);
673 MSVCRT_free(locinfo->lconv_intl_refcount);
674 MSVCRT_free(locinfo->lconv_num_refcount);
675 MSVCRT_free(locinfo->lconv_mon_refcount);
676 MSVCRT_free(locinfo->lconv);
678 MSVCRT_free(locinfo->ctype1_refcount);
679 MSVCRT_free(locinfo->ctype1);
681 MSVCRT_free(locinfo->pclmap);
682 MSVCRT_free(locinfo->pcumap);
684 MSVCRT_free(locinfo->lc_time_curr);
686 MSVCRT_free(locinfo);
689 /* INTERNAL: frees MSVCRT_pthreadmbcinfo struct */
690 void free_mbcinfo(MSVCRT_pthreadmbcinfo mbcinfo)
692 if(!mbcinfo)
693 return;
695 if(InterlockedDecrement(&mbcinfo->refcount))
696 return;
698 MSVCRT_free(mbcinfo);
701 /* _get_current_locale - not exported in native msvcrt */
702 MSVCRT__locale_t CDECL MSVCRT__get_current_locale(void)
704 MSVCRT__locale_t loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
705 if(!loc)
706 return NULL;
708 loc->locinfo = get_locinfo();
709 loc->mbcinfo = get_mbcinfo();
710 InterlockedIncrement(&loc->locinfo->refcount);
711 InterlockedIncrement(&loc->mbcinfo->refcount);
712 return loc;
715 /* _free_locale - not exported in native msvcrt */
716 void CDECL MSVCRT__free_locale(MSVCRT__locale_t locale)
718 if (!locale)
719 return;
721 free_locinfo(locale->locinfo);
722 free_mbcinfo(locale->mbcinfo);
723 MSVCRT_free(locale);
726 /* _create_locale - not exported in native msvcrt */
727 MSVCRT__locale_t CDECL MSVCRT__create_locale(int category, const char *locale)
729 static const DWORD time_data[] = {
730 LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1, LOCALE_SABBREVDAYNAME2,
731 LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4, LOCALE_SABBREVDAYNAME5,
732 LOCALE_SABBREVDAYNAME6,
733 LOCALE_SDAYNAME7, LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3,
734 LOCALE_SDAYNAME4, LOCALE_SDAYNAME5, LOCALE_SDAYNAME6,
735 LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2, LOCALE_SABBREVMONTHNAME3,
736 LOCALE_SABBREVMONTHNAME4, LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
737 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8, LOCALE_SABBREVMONTHNAME9,
738 LOCALE_SABBREVMONTHNAME10, LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12,
739 LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME4,
740 LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8,
741 LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12,
742 LOCALE_S1159, LOCALE_S2359,
743 LOCALE_SSHORTDATE, LOCALE_SLONGDATE,
744 LOCALE_STIMEFORMAT
746 static const char collate[] = "COLLATE=";
747 static const char ctype[] = "CTYPE=";
748 static const char monetary[] = "MONETARY=";
749 static const char numeric[] = "NUMERIC=";
750 static const char time[] = "TIME=";
751 static const char cloc_short_date[] = "MM/dd/yy";
752 static const MSVCRT_wchar_t cloc_short_dateW[] = {'M','M','/','d','d','/','y','y',0};
753 static const char cloc_long_date[] = "dddd, MMMM dd, yyyy";
754 static const MSVCRT_wchar_t cloc_long_dateW[] = {'d','d','d','d',',',' ','M','M','M','M',' ','d','d',',',' ','y','y','y','y',0};
755 static const char cloc_time[] = "HH:mm:ss";
756 static const MSVCRT_wchar_t cloc_timeW[] = {'H','H',':','m','m',':','s','s',0};
758 MSVCRT__locale_t loc;
759 LCID lcid[6] = { 0 }, lcid_tmp;
760 char buf[256];
761 int i, ret, size;
763 TRACE("(%d %s)\n", category, locale);
765 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
766 return NULL;
768 if(locale[0]=='C' && !locale[1])
769 lcid[0] = CP_ACP;
770 else if(!locale[0])
771 lcid[0] = GetSystemDefaultLCID();
772 else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
773 const char *p;
775 while(1) {
776 locale += 3; /* LC_ */
777 if(!memcmp(locale, collate, sizeof(collate)-1)) {
778 i = MSVCRT_LC_COLLATE;
779 locale += sizeof(collate)-1;
780 } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
781 i = MSVCRT_LC_CTYPE;
782 locale += sizeof(ctype)-1;
783 } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
784 i = MSVCRT_LC_MONETARY;
785 locale += sizeof(monetary)-1;
786 } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
787 i = MSVCRT_LC_NUMERIC;
788 locale += sizeof(numeric)-1;
789 } else if(!memcmp(locale, time, sizeof(time)-1)) {
790 i = MSVCRT_LC_TIME;
791 locale += sizeof(time)-1;
792 } else
793 return NULL;
795 p = strchr(locale, ';');
796 if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0'))
797 lcid[i] = 0;
798 else if(p) {
799 memcpy(buf, locale, p-locale);
800 buf[p-locale] = '\0';
801 lcid[i] = MSVCRT_locale_to_LCID(buf);
802 } else
803 lcid[i] = MSVCRT_locale_to_LCID(locale);
805 if(lcid[i] == -1)
806 return NULL;
808 if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
809 break;
811 locale = p+1;
813 } else {
814 lcid[0] = MSVCRT_locale_to_LCID(locale);
815 if(lcid[0] == -1)
816 return NULL;
819 for(i=1; i<6; i++) {
820 if(!lcid[i])
821 lcid[i] = lcid[0];
824 loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
825 if(!loc)
826 return NULL;
828 loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
829 if(!loc->locinfo) {
830 MSVCRT_free(loc);
831 return NULL;
834 loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
835 if(!loc->mbcinfo) {
836 MSVCRT_free(loc->locinfo);
837 MSVCRT_free(loc);
838 return NULL;
841 memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
842 loc->locinfo->refcount = 1;
843 loc->mbcinfo->refcount = 1;
845 loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
846 if(!loc->locinfo->lconv) {
847 MSVCRT__free_locale(loc);
848 return NULL;
850 memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
852 loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
853 loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
854 if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
855 MSVCRT__free_locale(loc);
856 return NULL;
859 if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
860 if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE], loc, MSVCRT_LC_COLLATE)) {
861 MSVCRT__free_locale(loc);
862 return NULL;
865 loc->locinfo->lc_collate_cp = loc->locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage;
866 } else
867 loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale = MSVCRT__strdup("C");
869 if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
870 CPINFO cp;
871 int j;
873 if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE], loc, MSVCRT_LC_CTYPE)) {
874 MSVCRT__free_locale(loc);
875 return NULL;
878 loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
879 loc->locinfo->lc_clike = 1;
880 if(!GetCPInfo(loc->locinfo->lc_codepage, &cp)) {
881 MSVCRT__free_locale(loc);
882 return NULL;
884 loc->locinfo->mb_cur_max = cp.MaxCharSize;
886 loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
887 loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
888 if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
889 MSVCRT__free_locale(loc);
890 return NULL;
893 *loc->locinfo->ctype1_refcount = 1;
894 loc->locinfo->ctype1[0] = 0;
895 loc->locinfo->pctype = loc->locinfo->ctype1+1;
897 buf[1] = buf[2] = '\0';
898 for(i=1; i<257; i++) {
899 buf[0] = i-1;
901 /* builtin GetStringTypeA doesn't set output to 0 on invalid input */
902 loc->locinfo->ctype1[i] = 0;
904 GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
905 1, loc->locinfo->ctype1+i);
908 for(i=0; cp.LeadByte[i+1]!=0; i+=2)
909 for(j=cp.LeadByte[i]; j<=cp.LeadByte[i+1]; j++)
910 loc->locinfo->ctype1[j+1] |= MSVCRT__LEADBYTE;
911 } else {
912 loc->locinfo->lc_clike = 1;
913 loc->locinfo->mb_cur_max = 1;
914 loc->locinfo->pctype = MSVCRT__ctype+1;
915 loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale = MSVCRT__strdup("C");
918 for(i=0; i<256; i++) {
919 if(loc->locinfo->pctype[i] & MSVCRT__LEADBYTE)
920 buf[i] = ' ';
921 else
922 buf[i] = i;
926 if(lcid[MSVCRT_LC_CTYPE]) {
927 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
928 (char*)loc->locinfo->pclmap, 256);
929 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
930 (char*)loc->locinfo->pcumap, 256);
931 } else {
932 for(i=0; i<256; i++) {
933 loc->locinfo->pclmap[i] = (i>='A' && i<='Z' ? i-'A'+'a' : i);
934 loc->locinfo->pcumap[i] = (i>='a' && i<='z' ? i-'a'+'A' : i);
938 _setmbcp_l(loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage, lcid[MSVCRT_LC_CTYPE], loc->mbcinfo);
940 if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
941 if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY], loc, MSVCRT_LC_MONETARY)) {
942 MSVCRT__free_locale(loc);
943 return NULL;
946 loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
947 loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
948 if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
949 MSVCRT__free_locale(loc);
950 return NULL;
953 *loc->locinfo->lconv_intl_refcount = 1;
954 *loc->locinfo->lconv_mon_refcount = 1;
956 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
957 |LOCALE_NOUSEROVERRIDE, buf, 256);
958 if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(i)))
959 memcpy(loc->locinfo->lconv->int_curr_symbol, buf, i);
960 else {
961 MSVCRT__free_locale(loc);
962 return NULL;
965 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
966 |LOCALE_NOUSEROVERRIDE, buf, 256);
967 if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(i)))
968 memcpy(loc->locinfo->lconv->currency_symbol, buf, i);
969 else {
970 MSVCRT__free_locale(loc);
971 return NULL;
974 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
975 |LOCALE_NOUSEROVERRIDE, buf, 256);
976 if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(i)))
977 memcpy(loc->locinfo->lconv->mon_decimal_point, buf, i);
978 else {
979 MSVCRT__free_locale(loc);
980 return NULL;
983 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
984 |LOCALE_NOUSEROVERRIDE, buf, 256);
985 if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(i)))
986 memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, i);
987 else {
988 MSVCRT__free_locale(loc);
989 return NULL;
992 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING
993 |LOCALE_NOUSEROVERRIDE, buf, 256);
994 if(i>1)
995 i = i/2 + (buf[i-2]=='0'?0:1);
996 if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(i))) {
997 for(i=0; buf[i+1]==';'; i+=2)
998 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
999 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1000 if(buf[i] != '0')
1001 loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
1002 } else {
1003 MSVCRT__free_locale(loc);
1004 return NULL;
1007 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
1008 |LOCALE_NOUSEROVERRIDE, buf, 256);
1009 if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(i)))
1010 memcpy(loc->locinfo->lconv->positive_sign, buf, i);
1011 else {
1012 MSVCRT__free_locale(loc);
1013 return NULL;
1016 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
1017 |LOCALE_NOUSEROVERRIDE, buf, 256);
1018 if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(i)))
1019 memcpy(loc->locinfo->lconv->negative_sign, buf, i);
1020 else {
1021 MSVCRT__free_locale(loc);
1022 return NULL;
1025 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS
1026 |LOCALE_NOUSEROVERRIDE, buf, 256))
1027 loc->locinfo->lconv->int_frac_digits = atoi(buf);
1028 else {
1029 MSVCRT__free_locale(loc);
1030 return NULL;
1033 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS
1034 |LOCALE_NOUSEROVERRIDE, buf, 256))
1035 loc->locinfo->lconv->frac_digits = atoi(buf);
1036 else {
1037 MSVCRT__free_locale(loc);
1038 return NULL;
1041 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES
1042 |LOCALE_NOUSEROVERRIDE, buf, 256))
1043 loc->locinfo->lconv->p_cs_precedes = atoi(buf);
1044 else {
1045 MSVCRT__free_locale(loc);
1046 return NULL;
1049 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE
1050 |LOCALE_NOUSEROVERRIDE, buf, 256))
1051 loc->locinfo->lconv->p_sep_by_space = atoi(buf);
1052 else {
1053 MSVCRT__free_locale(loc);
1054 return NULL;
1057 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES
1058 |LOCALE_NOUSEROVERRIDE, buf, 256))
1059 loc->locinfo->lconv->n_cs_precedes = atoi(buf);
1060 else {
1061 MSVCRT__free_locale(loc);
1062 return NULL;
1065 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE
1066 |LOCALE_NOUSEROVERRIDE, buf, 256))
1067 loc->locinfo->lconv->n_sep_by_space = atoi(buf);
1068 else {
1069 MSVCRT__free_locale(loc);
1070 return NULL;
1073 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN
1074 |LOCALE_NOUSEROVERRIDE, buf, 256))
1075 loc->locinfo->lconv->p_sign_posn = atoi(buf);
1076 else {
1077 MSVCRT__free_locale(loc);
1078 return NULL;
1081 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN
1082 |LOCALE_NOUSEROVERRIDE, buf, 256))
1083 loc->locinfo->lconv->n_sign_posn = atoi(buf);
1084 else {
1085 MSVCRT__free_locale(loc);
1086 return NULL;
1088 } else {
1089 loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
1090 loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
1091 loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
1092 loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
1093 loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
1094 loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
1095 loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
1097 if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
1098 || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
1099 || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
1100 || !loc->locinfo->lconv->negative_sign) {
1101 MSVCRT__free_locale(loc);
1102 return NULL;
1105 loc->locinfo->lconv->int_curr_symbol[0] = '\0';
1106 loc->locinfo->lconv->currency_symbol[0] = '\0';
1107 loc->locinfo->lconv->mon_decimal_point[0] = '\0';
1108 loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
1109 loc->locinfo->lconv->mon_grouping[0] = '\0';
1110 loc->locinfo->lconv->positive_sign[0] = '\0';
1111 loc->locinfo->lconv->negative_sign[0] = '\0';
1112 loc->locinfo->lconv->int_frac_digits = 127;
1113 loc->locinfo->lconv->frac_digits = 127;
1114 loc->locinfo->lconv->p_cs_precedes = 127;
1115 loc->locinfo->lconv->p_sep_by_space = 127;
1116 loc->locinfo->lconv->n_cs_precedes = 127;
1117 loc->locinfo->lconv->n_sep_by_space = 127;
1118 loc->locinfo->lconv->p_sign_posn = 127;
1119 loc->locinfo->lconv->n_sign_posn = 127;
1121 loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale = MSVCRT__strdup("C");
1124 if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
1125 if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC], loc, MSVCRT_LC_NUMERIC)) {
1126 MSVCRT__free_locale(loc);
1127 return NULL;
1130 if(!loc->locinfo->lconv_intl_refcount)
1131 loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1132 loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1133 if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
1134 MSVCRT__free_locale(loc);
1135 return NULL;
1138 *loc->locinfo->lconv_intl_refcount = 1;
1139 *loc->locinfo->lconv_num_refcount = 1;
1141 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1142 |LOCALE_NOUSEROVERRIDE, buf, 256);
1143 if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(i)))
1144 memcpy(loc->locinfo->lconv->decimal_point, buf, i);
1145 else {
1146 MSVCRT__free_locale(loc);
1147 return NULL;
1150 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1151 |LOCALE_NOUSEROVERRIDE, buf, 256);
1152 if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(i)))
1153 memcpy(loc->locinfo->lconv->thousands_sep, buf, i);
1154 else {
1155 MSVCRT__free_locale(loc);
1156 return NULL;
1159 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING
1160 |LOCALE_NOUSEROVERRIDE, buf, 256);
1161 if(i>1)
1162 i = i/2 + (buf[i-2]=='0'?0:1);
1163 if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(i))) {
1164 for(i=0; buf[i+1]==';'; i+=2)
1165 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1166 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1167 if(buf[i] != '0')
1168 loc->locinfo->lconv->grouping[i/2+1] = 127;
1169 } else {
1170 MSVCRT__free_locale(loc);
1171 return NULL;
1173 } else {
1174 loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1175 loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1176 loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1177 if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
1178 || !loc->locinfo->lconv->grouping) {
1179 MSVCRT__free_locale(loc);
1180 return NULL;
1183 loc->locinfo->lconv->decimal_point[0] = '.';
1184 loc->locinfo->lconv->decimal_point[1] = '\0';
1185 loc->locinfo->lconv->thousands_sep[0] = '\0';
1186 loc->locinfo->lconv->grouping[0] = '\0';
1188 loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale = MSVCRT__strdup("C");
1191 if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1192 if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME], loc, MSVCRT_LC_TIME)) {
1193 MSVCRT__free_locale(loc);
1194 return NULL;
1196 } else
1197 loc->locinfo->lc_category[MSVCRT_LC_TIME].locale = MSVCRT__strdup("C");
1199 size = sizeof(MSVCRT___lc_time_data);
1200 lcid_tmp = lcid[MSVCRT_LC_TIME] ? lcid[MSVCRT_LC_TIME] : MAKELCID(LANG_ENGLISH, SORT_DEFAULT);
1201 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1202 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1203 size += sizeof(cloc_short_date) + sizeof(cloc_short_dateW);
1204 }else if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1205 size += sizeof(cloc_long_date) + sizeof(cloc_long_dateW);
1206 }else {
1207 ret = GetLocaleInfoA(lcid_tmp, time_data[i]
1208 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1209 if(!ret) {
1210 MSVCRT__free_locale(loc);
1211 return NULL;
1213 size += ret;
1215 ret = GetLocaleInfoW(lcid_tmp, time_data[i]
1216 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1217 if(!ret) {
1218 MSVCRT__free_locale(loc);
1219 return NULL;
1221 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.str[i] = &loc->locinfo->lc_time_curr->data[ret];
1234 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1235 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_short_date, sizeof(cloc_short_date));
1236 ret += sizeof(cloc_short_date);
1237 }else if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1238 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_long_date, sizeof(cloc_long_date));
1239 ret += sizeof(cloc_long_date);
1240 }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1241 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_time, sizeof(cloc_time));
1242 ret += sizeof(cloc_time);
1243 }else {
1244 ret += GetLocaleInfoA(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1245 &loc->locinfo->lc_time_curr->data[ret], size-ret);
1248 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1249 loc->locinfo->lc_time_curr->wstr[i] = (MSVCRT_wchar_t*)&loc->locinfo->lc_time_curr->data[ret];
1250 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1251 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_short_dateW, sizeof(cloc_short_dateW));
1252 ret += sizeof(cloc_short_dateW);
1253 }else if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1254 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_long_dateW, sizeof(cloc_long_dateW));
1255 ret += sizeof(cloc_long_dateW);
1256 }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1257 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_timeW, sizeof(cloc_timeW));
1258 ret += sizeof(cloc_timeW);
1259 }else {
1260 ret += GetLocaleInfoW(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1261 (MSVCRT_wchar_t*)&loc->locinfo->lc_time_curr->data[ret], size-ret)*sizeof(MSVCRT_wchar_t);
1264 loc->locinfo->lc_time_curr->lcid = lcid[MSVCRT_LC_TIME];
1266 return loc;
1269 /*********************************************************************
1270 * setlocale (MSVCRT.@)
1272 char* CDECL MSVCRT_setlocale(int category, const char* locale)
1274 MSVCRT__locale_t loc;
1275 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
1277 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1278 return NULL;
1280 if(!locale) {
1281 if(category == MSVCRT_LC_ALL)
1282 return construct_lc_all(locinfo);
1284 return locinfo->lc_category[category].locale;
1287 loc = MSVCRT__create_locale(category, locale);
1288 if(!loc) {
1289 WARN("%d %s failed\n", category, locale);
1290 return NULL;
1293 LOCK_LOCALE;
1295 switch(category) {
1296 case MSVCRT_LC_ALL:
1297 case MSVCRT_LC_COLLATE:
1298 locinfo->lc_collate_cp = loc->locinfo->lc_collate_cp;
1299 locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1300 loc->locinfo->lc_handle[MSVCRT_LC_COLLATE];
1301 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1302 (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1303 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1304 (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1306 if(category != MSVCRT_LC_ALL)
1307 break;
1308 /* fall through */
1309 case MSVCRT_LC_CTYPE:
1310 locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1311 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE];
1312 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1313 (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1314 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1315 (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1317 locinfo->lc_codepage = loc->locinfo->lc_codepage;
1318 locinfo->lc_clike = loc->locinfo->lc_clike;
1319 locinfo->mb_cur_max = loc->locinfo->mb_cur_max;
1321 swap_pointers((void**)&locinfo->ctype1_refcount,
1322 (void**)&loc->locinfo->ctype1_refcount);
1323 swap_pointers((void**)&locinfo->ctype1, (void**)&loc->locinfo->ctype1);
1324 swap_pointers((void**)&locinfo->pctype, (void**)&loc->locinfo->pctype);
1325 swap_pointers((void**)&locinfo->pclmap, (void**)&loc->locinfo->pclmap);
1326 swap_pointers((void**)&locinfo->pcumap, (void**)&loc->locinfo->pcumap);
1328 if(category != MSVCRT_LC_ALL)
1329 break;
1330 /* fall through */
1331 case MSVCRT_LC_MONETARY:
1332 locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1333 loc->locinfo->lc_handle[MSVCRT_LC_MONETARY];
1334 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1335 (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1336 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1337 (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1339 swap_pointers((void**)&locinfo->lconv->int_curr_symbol,
1340 (void**)&loc->locinfo->lconv->int_curr_symbol);
1341 swap_pointers((void**)&locinfo->lconv->currency_symbol,
1342 (void**)&loc->locinfo->lconv->currency_symbol);
1343 swap_pointers((void**)&locinfo->lconv->mon_decimal_point,
1344 (void**)&loc->locinfo->lconv->mon_decimal_point);
1345 swap_pointers((void**)&locinfo->lconv->mon_thousands_sep,
1346 (void**)&loc->locinfo->lconv->mon_thousands_sep);
1347 swap_pointers((void**)&locinfo->lconv->mon_grouping,
1348 (void**)&loc->locinfo->lconv->mon_grouping);
1349 swap_pointers((void**)&locinfo->lconv->positive_sign,
1350 (void**)&loc->locinfo->lconv->positive_sign);
1351 swap_pointers((void**)&locinfo->lconv->negative_sign,
1352 (void**)&loc->locinfo->lconv->negative_sign);
1353 locinfo->lconv->int_frac_digits = loc->locinfo->lconv->int_frac_digits;
1354 locinfo->lconv->frac_digits = loc->locinfo->lconv->frac_digits;
1355 locinfo->lconv->p_cs_precedes = loc->locinfo->lconv->p_cs_precedes;
1356 locinfo->lconv->p_sep_by_space = loc->locinfo->lconv->p_sep_by_space;
1357 locinfo->lconv->n_cs_precedes = loc->locinfo->lconv->n_cs_precedes;
1358 locinfo->lconv->n_sep_by_space = loc->locinfo->lconv->n_sep_by_space;
1359 locinfo->lconv->p_sign_posn = loc->locinfo->lconv->p_sign_posn;
1360 locinfo->lconv->n_sign_posn = loc->locinfo->lconv->n_sign_posn;
1362 if(category != MSVCRT_LC_ALL)
1363 break;
1364 /* fall through */
1365 case MSVCRT_LC_NUMERIC:
1366 locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1367 loc->locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1368 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1369 (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1370 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1371 (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1373 swap_pointers((void**)&locinfo->lconv->decimal_point,
1374 (void**)&loc->locinfo->lconv->decimal_point);
1375 swap_pointers((void**)&locinfo->lconv->thousands_sep,
1376 (void**)&loc->locinfo->lconv->thousands_sep);
1377 swap_pointers((void**)&locinfo->lconv->grouping,
1378 (void**)&loc->locinfo->lconv->grouping);
1380 if(category != MSVCRT_LC_ALL)
1381 break;
1382 /* fall through */
1383 case MSVCRT_LC_TIME:
1384 locinfo->lc_handle[MSVCRT_LC_TIME] =
1385 loc->locinfo->lc_handle[MSVCRT_LC_TIME];
1386 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].locale,
1387 (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].locale);
1388 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1389 (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].refcount);
1390 swap_pointers((void**)&locinfo->lc_time_curr,
1391 (void**)&loc->locinfo->lc_time_curr);
1393 if(category != MSVCRT_LC_ALL)
1394 break;
1397 MSVCRT__free_locale(loc);
1398 UNLOCK_LOCALE;
1400 if(locinfo == MSVCRT_locale->locinfo) {
1401 int i;
1403 MSVCRT___lc_codepage = locinfo->lc_codepage;
1404 MSVCRT___lc_collate_cp = locinfo->lc_collate_cp;
1405 MSVCRT___mb_cur_max = locinfo->mb_cur_max;
1406 MSVCRT__pctype = locinfo->pctype;
1407 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1408 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1411 if(category == MSVCRT_LC_ALL)
1412 return construct_lc_all(locinfo);
1414 return locinfo->lc_category[category].locale;
1417 /* _configthreadlocale - not exported in native msvcrt */
1418 int CDECL _configthreadlocale(int type)
1420 thread_data_t *data = msvcrt_get_thread_data();
1421 MSVCRT__locale_t locale;
1422 int ret;
1424 if(!data)
1425 return -1;
1427 ret = (data->have_locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1429 if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1430 if(!data->have_locale) {
1431 /* Copy current global locale */
1432 locale = MSVCRT__create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
1433 if(!locale)
1434 return -1;
1436 data->locinfo = locale->locinfo;
1437 data->mbcinfo = locale->mbcinfo;
1438 data->have_locale = TRUE;
1439 MSVCRT_free(locale);
1442 return ret;
1445 if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1446 if(data->have_locale) {
1447 free_locinfo(data->locinfo);
1448 free_mbcinfo(data->mbcinfo);
1449 data->locinfo = MSVCRT_locale->locinfo;
1450 data->mbcinfo = MSVCRT_locale->mbcinfo;
1451 data->have_locale = FALSE;
1454 return ret;
1457 if(!type)
1458 return ret;
1460 return -1;
1463 BOOL msvcrt_init_locale(void)
1465 int i;
1467 LOCK_LOCALE;
1468 MSVCRT_locale = MSVCRT__create_locale(0, "C");
1469 UNLOCK_LOCALE;
1470 if(!MSVCRT_locale)
1471 return FALSE;
1473 MSVCRT___lc_codepage = MSVCRT_locale->locinfo->lc_codepage;
1474 MSVCRT___lc_collate_cp = MSVCRT_locale->locinfo->lc_collate_cp;
1475 MSVCRT___mb_cur_max = MSVCRT_locale->locinfo->mb_cur_max;
1476 MSVCRT__pctype = MSVCRT_locale->locinfo->pctype;
1477 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1478 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1479 _setmbcp(_MB_CP_ANSI);
1480 return TRUE;