d3d10core: Implement d3d10_device_GSGetShaderResources().
[wine/multimedia.git] / dlls / msvcrt / locale.c
blob992aa3ecb7f2962630c5dc478bce58229a2a1fe0
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 if(!cmp[0])
134 return 0;
136 buff[0] = 0;
137 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE, buff, MAX_ELEM_LEN);
138 if (!buff[0])
139 return 0;
141 /* Partial matches are only allowed on language/country names */
142 len = strlen(cmp);
143 if(exact || len<=3)
144 return !strcasecmp(cmp, buff);
145 else
146 return !strncasecmp(cmp, buff, len);
149 static BOOL CALLBACK
150 find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
152 locale_search_t *res = (locale_search_t *)lParam;
153 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
154 char buff[MAX_ELEM_LEN];
155 unsigned int flags = 0;
157 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
158 return CONTINUE_LOOKING;
160 /* Check Language */
161 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language, TRUE) ||
162 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language, TRUE) ||
163 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language, FALSE))
165 TRACE(":Found language: %s->%s\n", res->search_language, buff);
166 flags |= FOUND_LANGUAGE;
168 else if (res->match_flags & FOUND_LANGUAGE)
170 return CONTINUE_LOOKING;
173 /* Check Country */
174 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country, TRUE) ||
175 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country, TRUE) ||
176 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country, FALSE))
178 TRACE("Found country:%s->%s\n", res->search_country, buff);
179 flags |= FOUND_COUNTRY;
181 else if (!flags && (res->match_flags & FOUND_COUNTRY))
183 return CONTINUE_LOOKING;
186 /* Check codepage */
187 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage, TRUE) ||
188 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage, TRUE)))
190 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
191 flags |= FOUND_CODEPAGE;
192 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
194 else if (!flags && (res->match_flags & FOUND_CODEPAGE))
196 return CONTINUE_LOOKING;
199 if (flags > res->match_flags)
201 /* Found a better match than previously */
202 res->match_flags = flags;
203 res->found_lang_id = LangID;
205 if ((flags & (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE)) ==
206 (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE))
208 TRACE(":found exact locale match\n");
209 return STOP_LOOKING;
211 return CONTINUE_LOOKING;
214 extern int atoi(const char *);
216 /* Internal: Find the LCID for a locale specification */
217 LCID MSVCRT_locale_to_LCID(const char *locale, unsigned short *codepage)
219 LCID lcid;
220 locale_search_t search;
221 const char *cp, *region;
223 memset(&search, 0, sizeof(locale_search_t));
225 cp = strchr(locale, '.');
226 region = strchr(locale, '_');
228 lstrcpynA(search.search_language, locale, MAX_ELEM_LEN);
229 if(region) {
230 lstrcpynA(search.search_country, region+1, MAX_ELEM_LEN);
231 if(region-locale < MAX_ELEM_LEN)
232 search.search_language[region-locale] = '\0';
233 } else
234 search.search_country[0] = '\0';
236 if(cp) {
237 lstrcpynA(search.search_codepage, cp+1, MAX_ELEM_LEN);
238 if(region && cp-region-1<MAX_ELEM_LEN)
239 search.search_country[cp-region-1] = '\0';
240 if(cp-locale < MAX_ELEM_LEN)
241 search.search_language[cp-locale] = '\0';
242 } else
243 search.search_codepage[0] = '\0';
245 if(!search.search_country[0] && !search.search_codepage[0])
246 remap_synonym(search.search_language);
248 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
249 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
250 (LONG_PTR)&search);
252 if (!search.match_flags)
253 return -1;
255 /* If we were given something that didn't match, fail */
256 if (search.search_country[0] && !(search.match_flags & FOUND_COUNTRY))
257 return -1;
259 lcid = MAKELCID(search.found_lang_id, SORT_DEFAULT);
261 /* Populate partial locale, translating LCID to locale string elements */
262 if (!(search.match_flags & FOUND_CODEPAGE)) {
263 /* Even if a codepage is not enumerated for a locale
264 * it can be set if valid */
265 if (search.search_codepage[0]) {
266 if (IsValidCodePage(atoi(search.search_codepage)))
267 memcpy(search.found_codepage,search.search_codepage,MAX_ELEM_LEN);
268 else {
269 /* Special codepage values: OEM & ANSI */
270 if (!strcasecmp(search.search_codepage,"OCP")) {
271 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
272 search.found_codepage, MAX_ELEM_LEN);
273 } else if (!strcasecmp(search.search_codepage,"ACP")) {
274 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
275 search.found_codepage, MAX_ELEM_LEN);
276 } else
277 return -1;
279 if (!atoi(search.found_codepage))
280 return -1;
282 } else {
283 /* Prefer ANSI codepages if present */
284 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
285 search.found_codepage, MAX_ELEM_LEN);
286 if (!search.found_codepage[0] || !atoi(search.found_codepage))
287 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
288 search.found_codepage, MAX_ELEM_LEN);
291 if (codepage)
292 *codepage = atoi(search.found_codepage);
294 return lcid;
297 /* INTERNAL: Set lc_handle, lc_id and lc_category in threadlocinfo struct */
298 static BOOL update_threadlocinfo_category(LCID lcid, unsigned short cp,
299 MSVCRT__locale_t loc, int category)
301 char buf[256], *p;
302 int len;
304 if(GetLocaleInfoA(lcid, LOCALE_ILANGUAGE|LOCALE_NOUSEROVERRIDE, buf, 256)) {
305 p = buf;
307 loc->locinfo->lc_id[category].wLanguage = 0;
308 while(*p) {
309 loc->locinfo->lc_id[category].wLanguage *= 16;
311 if(*p <= '9')
312 loc->locinfo->lc_id[category].wLanguage += *p-'0';
313 else
314 loc->locinfo->lc_id[category].wLanguage += *p-'a'+10;
316 p++;
319 loc->locinfo->lc_id[category].wCountry =
320 loc->locinfo->lc_id[category].wLanguage;
323 loc->locinfo->lc_id[category].wCodePage = cp;
325 loc->locinfo->lc_handle[category] = lcid;
327 len = 0;
328 len += GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE
329 |LOCALE_NOUSEROVERRIDE, buf, 256);
330 buf[len-1] = '_';
331 len += GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY
332 |LOCALE_NOUSEROVERRIDE, &buf[len], 256-len);
333 buf[len-1] = '.';
334 sprintf(buf+len, "%d", cp);
335 len += strlen(buf+len)+1;
337 loc->locinfo->lc_category[category].locale = MSVCRT_malloc(len);
338 loc->locinfo->lc_category[category].refcount = MSVCRT_malloc(sizeof(int));
339 if(!loc->locinfo->lc_category[category].locale
340 || !loc->locinfo->lc_category[category].refcount) {
341 MSVCRT_free(loc->locinfo->lc_category[category].locale);
342 MSVCRT_free(loc->locinfo->lc_category[category].refcount);
343 loc->locinfo->lc_category[category].locale = NULL;
344 loc->locinfo->lc_category[category].refcount = NULL;
345 return TRUE;
347 memcpy(loc->locinfo->lc_category[category].locale, buf, len);
348 *loc->locinfo->lc_category[category].refcount = 1;
350 return FALSE;
353 /* INTERNAL: swap pointers values */
354 static inline void swap_pointers(void **p1, void **p2) {
355 void *hlp;
357 hlp = *p1;
358 *p1 = *p2;
359 *p2 = hlp;
362 /* INTERNAL: returns pthreadlocinfo struct */
363 MSVCRT_pthreadlocinfo get_locinfo(void) {
364 thread_data_t *data = msvcrt_get_thread_data();
366 if(!data || !data->have_locale)
367 return MSVCRT_locale->locinfo;
369 return data->locinfo;
372 /* INTERNAL: returns pthreadlocinfo struct */
373 MSVCRT_pthreadmbcinfo get_mbcinfo(void) {
374 thread_data_t *data = msvcrt_get_thread_data();
376 if(!data || !data->have_locale)
377 return MSVCRT_locale->mbcinfo;
379 return data->mbcinfo;
382 /* INTERNAL: constructs string returned by setlocale */
383 static inline char* construct_lc_all(MSVCRT_pthreadlocinfo locinfo) {
384 static char current_lc_all[MAX_LOCALE_LENGTH];
386 int i;
388 for(i=MSVCRT_LC_MIN+1; i<MSVCRT_LC_MAX; i++) {
389 if(strcmp(locinfo->lc_category[i].locale,
390 locinfo->lc_category[i+1].locale))
391 break;
394 if(i==MSVCRT_LC_MAX)
395 return locinfo->lc_category[MSVCRT_LC_COLLATE].locale;
397 sprintf(current_lc_all,
398 "LC_COLLATE=%s;LC_CTYPE=%s;LC_MONETARY=%s;LC_NUMERIC=%s;LC_TIME=%s",
399 locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
400 locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
401 locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
402 locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
403 locinfo->lc_category[MSVCRT_LC_TIME].locale);
405 return current_lc_all;
409 /*********************************************************************
410 * _Getdays (MSVCRT.@)
412 char* CDECL _Getdays(void)
414 MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
415 int i, len, size;
416 char *out;
418 TRACE("\n");
420 size = cur->str.names.short_mon[0]-cur->str.names.short_wday[0];
421 out = MSVCRT_malloc(size+1);
422 if(!out)
423 return NULL;
425 size = 0;
426 for(i=0; i<7; i++) {
427 out[size++] = ':';
428 len = strlen(cur->str.names.short_wday[i]);
429 memcpy(&out[size], cur->str.names.short_wday[i], len);
430 size += len;
432 out[size++] = ':';
433 len = strlen(cur->str.names.wday[i]);
434 memcpy(&out[size], cur->str.names.wday[i], len);
435 size += len;
437 out[size] = '\0';
439 return out;
442 /*********************************************************************
443 * _Getmonths (MSVCRT.@)
445 char* CDECL _Getmonths(void)
447 MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
448 int i, len, size;
449 char *out;
451 TRACE("\n");
453 size = cur->str.names.am-cur->str.names.short_mon[0];
454 out = MSVCRT_malloc(size+1);
455 if(!out)
456 return NULL;
458 size = 0;
459 for(i=0; i<12; i++) {
460 out[size++] = ':';
461 len = strlen(cur->str.names.short_mon[i]);
462 memcpy(&out[size], cur->str.names.short_mon[i], len);
463 size += len;
465 out[size++] = ':';
466 len = strlen(cur->str.names.mon[i]);
467 memcpy(&out[size], cur->str.names.mon[i], len);
468 size += len;
470 out[size] = '\0';
472 return out;
475 /*********************************************************************
476 * _Gettnames (MSVCRT.@)
478 void* CDECL _Gettnames(void)
480 MSVCRT___lc_time_data *ret, *cur = get_locinfo()->lc_time_curr;
481 unsigned int i, size = sizeof(MSVCRT___lc_time_data);
483 TRACE("\n");
485 for(i=0; i<sizeof(cur->str.str)/sizeof(cur->str.str[0]); i++)
486 size += strlen(cur->str.str[i])+1;
488 ret = MSVCRT_malloc(size);
489 if(!ret)
490 return NULL;
491 memcpy(ret, cur, size);
493 size = 0;
494 for(i=0; i<sizeof(cur->str.str)/sizeof(cur->str.str[0]); i++) {
495 ret->str.str[i] = &ret->data[size];
496 size += strlen(&ret->data[size])+1;
499 return ret;
502 /*********************************************************************
503 * __crtLCMapStringA (MSVCRT.@)
505 int CDECL __crtLCMapStringA(
506 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
507 int dstlen, unsigned int codepage, int xflag
509 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
510 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
511 /* FIXME: A bit incorrect. But msvcrt itself just converts its
512 * arguments to wide strings and then calls LCMapStringW
514 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
517 /*********************************************************************
518 * __crtLCMapStringW (MSVCRT.@)
520 int CDECL __crtLCMapStringW(LCID lcid, DWORD mapflags, const MSVCRT_wchar_t *src,
521 int srclen, MSVCRT_wchar_t *dst, int dstlen, unsigned int codepage, int xflag)
523 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
524 lcid, mapflags, debugstr_w(src), srclen, dst, dstlen, codepage, xflag);
526 return LCMapStringW(lcid, mapflags, src, srclen, dst, dstlen);
529 /*********************************************************************
530 * __crtCompareStringA (MSVCRT.@)
532 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
533 const char *src2, int len2 )
535 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
536 lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
537 /* FIXME: probably not entirely right */
538 return CompareStringA( lcid, flags, src1, len1, src2, len2 );
541 /*********************************************************************
542 * __crtCompareStringW (MSVCRT.@)
544 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
545 const MSVCRT_wchar_t *src2, int len2 )
547 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
548 lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
549 /* FIXME: probably not entirely right */
550 return CompareStringW( lcid, flags, src1, len1, src2, len2 );
553 /*********************************************************************
554 * __crtGetLocaleInfoW (MSVCRT.@)
556 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
558 FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
559 /* FIXME: probably not entirely right */
560 return GetLocaleInfoW( lcid, type, buffer, len );
563 /*********************************************************************
564 * btowc(MSVCRT.@)
566 MSVCRT_wint_t CDECL MSVCRT_btowc(int c)
568 unsigned char letter = c;
569 MSVCRT_wchar_t ret;
571 if(!MultiByteToWideChar(get_locinfo()->lc_handle[MSVCRT_LC_CTYPE],
572 0, (LPCSTR)&letter, 1, &ret, 1))
573 return 0;
575 return ret;
578 /*********************************************************************
579 * __crtGetStringTypeW(MSVCRT.@)
581 * This function was accepting different number of arguments in older
582 * versions of msvcrt.
584 BOOL CDECL __crtGetStringTypeW(DWORD unk, DWORD type,
585 MSVCRT_wchar_t *buffer, int len, WORD *out)
587 FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
588 unk, type, buffer, len, out);
590 return GetStringTypeW(type, buffer, len, out);
593 /*********************************************************************
594 * localeconv (MSVCRT.@)
596 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
598 return get_locinfo()->lconv;
601 /*********************************************************************
602 * __lconv_init (MSVCRT.@)
604 int CDECL __lconv_init(void)
606 /* this is used to make chars unsigned */
607 charmax = 255;
608 return 0;
611 /*********************************************************************
612 * ___lc_handle_func (MSVCRT.@)
614 LCID* CDECL ___lc_handle_func(void)
616 return get_locinfo()->lc_handle;
619 /*********************************************************************
620 * ___lc_codepage_func (MSVCRT.@)
622 unsigned int CDECL ___lc_codepage_func(void)
624 return get_locinfo()->lc_codepage;
627 /*********************************************************************
628 * ___lc_collate_cp_func (MSVCRT.@)
630 int CDECL ___lc_collate_cp_func(void)
632 return get_locinfo()->lc_collate_cp;
635 /* INTERNAL: frees MSVCRT_pthreadlocinfo struct */
636 void free_locinfo(MSVCRT_pthreadlocinfo locinfo)
638 int i;
640 if(!locinfo)
641 return;
643 if(InterlockedDecrement(&locinfo->refcount))
644 return;
646 for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
647 MSVCRT_free(locinfo->lc_category[i].locale);
648 MSVCRT_free(locinfo->lc_category[i].refcount);
651 if(locinfo->lconv) {
652 MSVCRT_free(locinfo->lconv->decimal_point);
653 MSVCRT_free(locinfo->lconv->thousands_sep);
654 MSVCRT_free(locinfo->lconv->grouping);
655 MSVCRT_free(locinfo->lconv->int_curr_symbol);
656 MSVCRT_free(locinfo->lconv->currency_symbol);
657 MSVCRT_free(locinfo->lconv->mon_decimal_point);
658 MSVCRT_free(locinfo->lconv->mon_thousands_sep);
659 MSVCRT_free(locinfo->lconv->mon_grouping);
660 MSVCRT_free(locinfo->lconv->positive_sign);
661 MSVCRT_free(locinfo->lconv->negative_sign);
663 MSVCRT_free(locinfo->lconv_intl_refcount);
664 MSVCRT_free(locinfo->lconv_num_refcount);
665 MSVCRT_free(locinfo->lconv_mon_refcount);
666 MSVCRT_free(locinfo->lconv);
668 MSVCRT_free(locinfo->ctype1_refcount);
669 MSVCRT_free(locinfo->ctype1);
671 MSVCRT_free(locinfo->pclmap);
672 MSVCRT_free(locinfo->pcumap);
674 MSVCRT_free(locinfo->lc_time_curr);
676 MSVCRT_free(locinfo);
679 /* INTERNAL: frees MSVCRT_pthreadmbcinfo struct */
680 void free_mbcinfo(MSVCRT_pthreadmbcinfo mbcinfo)
682 if(!mbcinfo)
683 return;
685 if(InterlockedDecrement(&mbcinfo->refcount))
686 return;
688 MSVCRT_free(mbcinfo);
691 /*********************************************************************
692 * _get_current_locale (MSVCRT.@)
694 MSVCRT__locale_t CDECL MSVCRT__get_current_locale(void)
696 MSVCRT__locale_t loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
697 if(!loc)
698 return NULL;
700 loc->locinfo = get_locinfo();
701 loc->mbcinfo = get_mbcinfo();
702 InterlockedIncrement(&loc->locinfo->refcount);
703 InterlockedIncrement(&loc->mbcinfo->refcount);
704 return loc;
707 /*********************************************************************
708 * _free_locale (MSVCRT.@)
710 void CDECL MSVCRT__free_locale(MSVCRT__locale_t locale)
712 if (!locale)
713 return;
715 free_locinfo(locale->locinfo);
716 free_mbcinfo(locale->mbcinfo);
717 MSVCRT_free(locale);
720 /*********************************************************************
721 * _create_locale (MSVCRT.@)
723 MSVCRT__locale_t CDECL MSVCRT__create_locale(int category, const char *locale)
725 static const DWORD time_data[] = {
726 LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1, LOCALE_SABBREVDAYNAME2,
727 LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4, LOCALE_SABBREVDAYNAME5,
728 LOCALE_SABBREVDAYNAME6,
729 LOCALE_SDAYNAME7, LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3,
730 LOCALE_SDAYNAME4, LOCALE_SDAYNAME5, LOCALE_SDAYNAME6,
731 LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2, LOCALE_SABBREVMONTHNAME3,
732 LOCALE_SABBREVMONTHNAME4, LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
733 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8, LOCALE_SABBREVMONTHNAME9,
734 LOCALE_SABBREVMONTHNAME10, LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12,
735 LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME4,
736 LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8,
737 LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12,
738 LOCALE_S1159, LOCALE_S2359,
739 LOCALE_SSHORTDATE, LOCALE_SLONGDATE,
740 LOCALE_STIMEFORMAT
742 static const char collate[] = "COLLATE=";
743 static const char ctype[] = "CTYPE=";
744 static const char monetary[] = "MONETARY=";
745 static const char numeric[] = "NUMERIC=";
746 static const char time[] = "TIME=";
747 static const char cloc_short_date[] = "MM/dd/yy";
748 static const MSVCRT_wchar_t cloc_short_dateW[] = {'M','M','/','d','d','/','y','y',0};
749 static const char cloc_long_date[] = "dddd, MMMM dd, yyyy";
750 static const MSVCRT_wchar_t cloc_long_dateW[] = {'d','d','d','d',',',' ','M','M','M','M',' ','d','d',',',' ','y','y','y','y',0};
751 static const char cloc_time[] = "HH:mm:ss";
752 static const MSVCRT_wchar_t cloc_timeW[] = {'H','H',':','m','m',':','s','s',0};
754 MSVCRT__locale_t loc;
755 LCID lcid[6] = { 0 }, lcid_tmp;
756 unsigned short cp[6] = { 0 };
757 char buf[256];
758 int i, ret, size;
760 TRACE("(%d %s)\n", category, locale);
762 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
763 return NULL;
765 if(locale[0]=='C' && !locale[1]) {
766 lcid[0] = 0;
767 cp[0] = CP_ACP;
768 } else if(!locale[0]) {
769 lcid[0] = GetSystemDefaultLCID();
770 GetLocaleInfoA(lcid[0], LOCALE_IDEFAULTANSICODEPAGE
771 |LOCALE_NOUSEROVERRIDE, buf, sizeof(buf));
772 cp[0] = atoi(buf);
774 for(i=1; i<6; i++) {
775 lcid[i] = lcid[0];
776 cp[i] = cp[0];
778 } else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
779 const char *p;
781 while(1) {
782 locale += 3; /* LC_ */
783 if(!memcmp(locale, collate, sizeof(collate)-1)) {
784 i = MSVCRT_LC_COLLATE;
785 locale += sizeof(collate)-1;
786 } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
787 i = MSVCRT_LC_CTYPE;
788 locale += sizeof(ctype)-1;
789 } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
790 i = MSVCRT_LC_MONETARY;
791 locale += sizeof(monetary)-1;
792 } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
793 i = MSVCRT_LC_NUMERIC;
794 locale += sizeof(numeric)-1;
795 } else if(!memcmp(locale, time, sizeof(time)-1)) {
796 i = MSVCRT_LC_TIME;
797 locale += sizeof(time)-1;
798 } else
799 return NULL;
801 p = strchr(locale, ';');
802 if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0')) {
803 lcid[i] = 0;
804 cp[i] = CP_ACP;
805 } else if(p) {
806 memcpy(buf, locale, p-locale);
807 buf[p-locale] = '\0';
808 lcid[i] = MSVCRT_locale_to_LCID(buf, &cp[i]);
809 } else
810 lcid[i] = MSVCRT_locale_to_LCID(locale, &cp[i]);
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, &cp[0]);
822 if(lcid[0] == -1)
823 return NULL;
825 for(i=1; i<6; i++) {
826 lcid[i] = lcid[0];
827 cp[i] = cp[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], cp[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_info;
878 int j;
880 if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE], cp[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_info)) {
888 MSVCRT__free_locale(loc);
889 return NULL;
891 loc->locinfo->mb_cur_max = cp_info.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_info.LeadByte[i+1]!=0; i+=2)
916 for(j=cp_info.LeadByte[i]; j<=cp_info.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], cp[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], cp[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], cp[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 lcid_tmp = lcid[MSVCRT_LC_TIME] ? lcid[MSVCRT_LC_TIME] : MAKELCID(LANG_ENGLISH, SORT_DEFAULT);
1208 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1209 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1210 size += sizeof(cloc_short_date) + sizeof(cloc_short_dateW);
1211 }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1212 size += sizeof(cloc_long_date) + sizeof(cloc_long_dateW);
1213 }else {
1214 ret = GetLocaleInfoA(lcid_tmp, time_data[i]
1215 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1216 if(!ret) {
1217 MSVCRT__free_locale(loc);
1218 return NULL;
1220 size += ret;
1222 ret = GetLocaleInfoW(lcid_tmp, time_data[i]
1223 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1224 if(!ret) {
1225 MSVCRT__free_locale(loc);
1226 return NULL;
1228 size += ret*sizeof(MSVCRT_wchar_t);
1232 loc->locinfo->lc_time_curr = MSVCRT_malloc(size);
1233 if(!loc->locinfo->lc_time_curr) {
1234 MSVCRT__free_locale(loc);
1235 return NULL;
1238 ret = 0;
1239 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1240 loc->locinfo->lc_time_curr->str.str[i] = &loc->locinfo->lc_time_curr->data[ret];
1241 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1242 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_short_date, sizeof(cloc_short_date));
1243 ret += sizeof(cloc_short_date);
1244 }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1245 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_long_date, sizeof(cloc_long_date));
1246 ret += sizeof(cloc_long_date);
1247 }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1248 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_time, sizeof(cloc_time));
1249 ret += sizeof(cloc_time);
1250 }else {
1251 ret += GetLocaleInfoA(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1252 &loc->locinfo->lc_time_curr->data[ret], size-ret);
1255 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1256 loc->locinfo->lc_time_curr->wstr[i] = (MSVCRT_wchar_t*)&loc->locinfo->lc_time_curr->data[ret];
1257 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1258 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_short_dateW, sizeof(cloc_short_dateW));
1259 ret += sizeof(cloc_short_dateW);
1260 }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1261 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_long_dateW, sizeof(cloc_long_dateW));
1262 ret += sizeof(cloc_long_dateW);
1263 }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1264 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_timeW, sizeof(cloc_timeW));
1265 ret += sizeof(cloc_timeW);
1266 }else {
1267 ret += GetLocaleInfoW(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1268 (MSVCRT_wchar_t*)&loc->locinfo->lc_time_curr->data[ret], size-ret)*sizeof(MSVCRT_wchar_t);
1271 loc->locinfo->lc_time_curr->lcid = lcid[MSVCRT_LC_TIME];
1273 return loc;
1276 /*********************************************************************
1277 * setlocale (MSVCRT.@)
1279 char* CDECL MSVCRT_setlocale(int category, const char* locale)
1281 MSVCRT__locale_t loc;
1282 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
1284 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1285 return NULL;
1287 if(!locale) {
1288 if(category == MSVCRT_LC_ALL)
1289 return construct_lc_all(locinfo);
1291 return locinfo->lc_category[category].locale;
1294 loc = MSVCRT__create_locale(category, locale);
1295 if(!loc) {
1296 WARN("%d %s failed\n", category, locale);
1297 return NULL;
1300 LOCK_LOCALE;
1302 switch(category) {
1303 case MSVCRT_LC_ALL:
1304 case MSVCRT_LC_COLLATE:
1305 locinfo->lc_collate_cp = loc->locinfo->lc_collate_cp;
1306 locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1307 loc->locinfo->lc_handle[MSVCRT_LC_COLLATE];
1308 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1309 (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1310 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1311 (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1313 if(category != MSVCRT_LC_ALL)
1314 break;
1315 /* fall through */
1316 case MSVCRT_LC_CTYPE:
1317 locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1318 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE];
1319 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1320 (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1321 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1322 (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1324 locinfo->lc_codepage = loc->locinfo->lc_codepage;
1325 locinfo->lc_clike = loc->locinfo->lc_clike;
1326 locinfo->mb_cur_max = loc->locinfo->mb_cur_max;
1328 swap_pointers((void**)&locinfo->ctype1_refcount,
1329 (void**)&loc->locinfo->ctype1_refcount);
1330 swap_pointers((void**)&locinfo->ctype1, (void**)&loc->locinfo->ctype1);
1331 swap_pointers((void**)&locinfo->pctype, (void**)&loc->locinfo->pctype);
1332 swap_pointers((void**)&locinfo->pclmap, (void**)&loc->locinfo->pclmap);
1333 swap_pointers((void**)&locinfo->pcumap, (void**)&loc->locinfo->pcumap);
1335 if(category != MSVCRT_LC_ALL)
1336 break;
1337 /* fall through */
1338 case MSVCRT_LC_MONETARY:
1339 locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1340 loc->locinfo->lc_handle[MSVCRT_LC_MONETARY];
1341 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1342 (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1343 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1344 (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1346 swap_pointers((void**)&locinfo->lconv->int_curr_symbol,
1347 (void**)&loc->locinfo->lconv->int_curr_symbol);
1348 swap_pointers((void**)&locinfo->lconv->currency_symbol,
1349 (void**)&loc->locinfo->lconv->currency_symbol);
1350 swap_pointers((void**)&locinfo->lconv->mon_decimal_point,
1351 (void**)&loc->locinfo->lconv->mon_decimal_point);
1352 swap_pointers((void**)&locinfo->lconv->mon_thousands_sep,
1353 (void**)&loc->locinfo->lconv->mon_thousands_sep);
1354 swap_pointers((void**)&locinfo->lconv->mon_grouping,
1355 (void**)&loc->locinfo->lconv->mon_grouping);
1356 swap_pointers((void**)&locinfo->lconv->positive_sign,
1357 (void**)&loc->locinfo->lconv->positive_sign);
1358 swap_pointers((void**)&locinfo->lconv->negative_sign,
1359 (void**)&loc->locinfo->lconv->negative_sign);
1360 locinfo->lconv->int_frac_digits = loc->locinfo->lconv->int_frac_digits;
1361 locinfo->lconv->frac_digits = loc->locinfo->lconv->frac_digits;
1362 locinfo->lconv->p_cs_precedes = loc->locinfo->lconv->p_cs_precedes;
1363 locinfo->lconv->p_sep_by_space = loc->locinfo->lconv->p_sep_by_space;
1364 locinfo->lconv->n_cs_precedes = loc->locinfo->lconv->n_cs_precedes;
1365 locinfo->lconv->n_sep_by_space = loc->locinfo->lconv->n_sep_by_space;
1366 locinfo->lconv->p_sign_posn = loc->locinfo->lconv->p_sign_posn;
1367 locinfo->lconv->n_sign_posn = loc->locinfo->lconv->n_sign_posn;
1369 if(category != MSVCRT_LC_ALL)
1370 break;
1371 /* fall through */
1372 case MSVCRT_LC_NUMERIC:
1373 locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1374 loc->locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1375 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1376 (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1377 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1378 (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1380 swap_pointers((void**)&locinfo->lconv->decimal_point,
1381 (void**)&loc->locinfo->lconv->decimal_point);
1382 swap_pointers((void**)&locinfo->lconv->thousands_sep,
1383 (void**)&loc->locinfo->lconv->thousands_sep);
1384 swap_pointers((void**)&locinfo->lconv->grouping,
1385 (void**)&loc->locinfo->lconv->grouping);
1387 if(category != MSVCRT_LC_ALL)
1388 break;
1389 /* fall through */
1390 case MSVCRT_LC_TIME:
1391 locinfo->lc_handle[MSVCRT_LC_TIME] =
1392 loc->locinfo->lc_handle[MSVCRT_LC_TIME];
1393 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].locale,
1394 (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].locale);
1395 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1396 (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].refcount);
1397 swap_pointers((void**)&locinfo->lc_time_curr,
1398 (void**)&loc->locinfo->lc_time_curr);
1400 if(category != MSVCRT_LC_ALL)
1401 break;
1404 MSVCRT__free_locale(loc);
1405 UNLOCK_LOCALE;
1407 if(locinfo == MSVCRT_locale->locinfo) {
1408 int i;
1410 MSVCRT___lc_codepage = locinfo->lc_codepage;
1411 MSVCRT___lc_collate_cp = locinfo->lc_collate_cp;
1412 MSVCRT___mb_cur_max = locinfo->mb_cur_max;
1413 MSVCRT__pctype = locinfo->pctype;
1414 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1415 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1418 if(category == MSVCRT_LC_ALL)
1419 return construct_lc_all(locinfo);
1421 return locinfo->lc_category[category].locale;
1424 /*********************************************************************
1425 * _wsetlocale (MSVCRT.@)
1427 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* wlocale)
1429 static MSVCRT_wchar_t current_lc_all[MAX_LOCALE_LENGTH];
1431 char *locale = NULL;
1432 const char *ret;
1433 MSVCRT_size_t len;
1435 if(wlocale) {
1436 len = MSVCRT_wcstombs(NULL, wlocale, 0);
1437 if(len == -1)
1438 return NULL;
1440 locale = MSVCRT_malloc(++len);
1441 if(!locale)
1442 return NULL;
1444 MSVCRT_wcstombs(locale, wlocale, len);
1447 LOCK_LOCALE;
1448 ret = MSVCRT_setlocale(category, locale);
1449 MSVCRT_free(locale);
1451 if(ret && MSVCRT_mbstowcs(current_lc_all, ret, MAX_LOCALE_LENGTH)==-1)
1452 ret = NULL;
1454 UNLOCK_LOCALE;
1455 return ret ? current_lc_all : NULL;
1458 /*********************************************************************
1459 * _configthreadlocale (MSVCR80.@)
1461 int CDECL _configthreadlocale(int type)
1463 thread_data_t *data = msvcrt_get_thread_data();
1464 MSVCRT__locale_t locale;
1465 int ret;
1467 if(!data)
1468 return -1;
1470 ret = (data->have_locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1472 if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1473 if(!data->have_locale) {
1474 /* Copy current global locale */
1475 locale = MSVCRT__create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
1476 if(!locale)
1477 return -1;
1479 data->locinfo = locale->locinfo;
1480 data->mbcinfo = locale->mbcinfo;
1481 data->have_locale = TRUE;
1482 MSVCRT_free(locale);
1485 return ret;
1488 if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1489 if(data->have_locale) {
1490 free_locinfo(data->locinfo);
1491 free_mbcinfo(data->mbcinfo);
1492 data->locinfo = MSVCRT_locale->locinfo;
1493 data->mbcinfo = MSVCRT_locale->mbcinfo;
1494 data->have_locale = FALSE;
1497 return ret;
1500 if(!type)
1501 return ret;
1503 return -1;
1506 BOOL msvcrt_init_locale(void)
1508 int i;
1510 LOCK_LOCALE;
1511 MSVCRT_locale = MSVCRT__create_locale(0, "C");
1512 UNLOCK_LOCALE;
1513 if(!MSVCRT_locale)
1514 return FALSE;
1516 MSVCRT___lc_codepage = MSVCRT_locale->locinfo->lc_codepage;
1517 MSVCRT___lc_collate_cp = MSVCRT_locale->locinfo->lc_collate_cp;
1518 MSVCRT___mb_cur_max = MSVCRT_locale->locinfo->mb_cur_max;
1519 MSVCRT__pctype = MSVCRT_locale->locinfo->pctype;
1520 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1521 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1522 _setmbcp(_MB_CP_ANSI);
1523 return TRUE;