kernel32: Add GetConsoleFontInfo stub.
[wine.git] / dlls / msvcrt / locale.c
bloba1551c2f7271e9dd55a204e35227b6f095229802
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"
38 #include "wine/unicode.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
42 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
43 #define MAX_LOCALE_LENGTH 256
44 MSVCRT__locale_t MSVCRT_locale = NULL;
45 unsigned short *MSVCRT__pctype = NULL;
46 unsigned int MSVCRT___lc_codepage = 0;
47 int MSVCRT___lc_collate_cp = 0;
48 LCID MSVCRT___lc_handle[MSVCRT_LC_MAX - MSVCRT_LC_MIN + 1] = { 0 };
49 int MSVCRT___mb_cur_max = 1;
50 static unsigned char charmax = CHAR_MAX;
52 /* MT */
53 #define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
54 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
56 #define MSVCRT_LEADBYTE 0x8000
57 #define MSVCRT_C1_DEFINED 0x200
59 /* Friendly country strings & language names abbreviations. */
60 static const char * const _country_synonyms[] =
62 "american", "enu",
63 "american english", "enu",
64 "american-english", "enu",
65 "english-american", "enu",
66 "english-us", "enu",
67 "english-usa", "enu",
68 "us", "enu",
69 "usa", "enu",
70 "australian", "ena",
71 "english-aus", "ena",
72 "belgian", "nlb",
73 "french-belgian", "frb",
74 "canadian", "enc",
75 "english-can", "enc",
76 "french-canadian", "frc",
77 "chinese", "chs",
78 "chinese-simplified", "chs",
79 "chinese-traditional", "cht",
80 "dutch-belgian", "nlb",
81 "english-nz", "enz",
82 "uk", "eng",
83 "english-uk", "eng",
84 "french-swiss", "frs",
85 "swiss", "des",
86 "german-swiss", "des",
87 "italian-swiss", "its",
88 "german-austrian", "dea",
89 "portuguese", "ptb",
90 "portuguese-brazil", "ptb",
91 "spanish-mexican", "esm",
92 "norwegian-bokmal", "nor",
93 "norwegian-nynorsk", "non",
94 "spanish-modern", "esn"
97 /* INTERNAL: Map a synonym to an ISO code */
98 static void remap_synonym(char *name)
100 unsigned int i;
101 for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
103 if (!strcasecmp(_country_synonyms[i],name))
105 TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
106 strcpy(name, _country_synonyms[i+1]);
107 return;
112 /* Note: Flags are weighted in order of matching importance */
113 #define FOUND_LANGUAGE 0x4
114 #define FOUND_COUNTRY 0x2
115 #define FOUND_CODEPAGE 0x1
117 typedef struct {
118 char search_language[MAX_ELEM_LEN];
119 char search_country[MAX_ELEM_LEN];
120 char search_codepage[MAX_ELEM_LEN];
121 char found_codepage[MAX_ELEM_LEN];
122 unsigned int match_flags;
123 LANGID found_lang_id;
124 } locale_search_t;
126 #define CONTINUE_LOOKING TRUE
127 #define STOP_LOOKING FALSE
129 /* INTERNAL: Get and compare locale info with a given string */
130 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp, BOOL exact)
132 int len;
134 if(!cmp[0])
135 return 0;
137 buff[0] = 0;
138 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE, buff, MAX_ELEM_LEN);
139 if (!buff[0])
140 return 0;
142 /* Partial matches are only allowed on language/country names */
143 len = strlen(cmp);
144 if(exact || len<=3)
145 return !strcasecmp(cmp, buff);
146 else
147 return !strncasecmp(cmp, buff, len);
150 static BOOL CALLBACK
151 find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
153 locale_search_t *res = (locale_search_t *)lParam;
154 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
155 char buff[MAX_ELEM_LEN];
156 unsigned int flags = 0;
158 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
159 return CONTINUE_LOOKING;
161 /* Check Language */
162 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language, TRUE) ||
163 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language, TRUE) ||
164 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language, FALSE))
166 TRACE(":Found language: %s->%s\n", res->search_language, buff);
167 flags |= FOUND_LANGUAGE;
169 else if (res->match_flags & FOUND_LANGUAGE)
171 return CONTINUE_LOOKING;
174 /* Check Country */
175 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country, TRUE) ||
176 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country, TRUE) ||
177 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country, FALSE))
179 TRACE("Found country:%s->%s\n", res->search_country, buff);
180 flags |= FOUND_COUNTRY;
182 else if (!flags && (res->match_flags & FOUND_COUNTRY))
184 return CONTINUE_LOOKING;
187 /* Check codepage */
188 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage, TRUE) ||
189 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage, TRUE)))
191 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
192 flags |= FOUND_CODEPAGE;
193 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
195 else if (!flags && (res->match_flags & FOUND_CODEPAGE))
197 return CONTINUE_LOOKING;
200 if (flags > res->match_flags)
202 /* Found a better match than previously */
203 res->match_flags = flags;
204 res->found_lang_id = LangID;
206 if ((flags & (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE)) ==
207 (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE))
209 TRACE(":found exact locale match\n");
210 return STOP_LOOKING;
212 return CONTINUE_LOOKING;
215 extern int atoi(const char *);
217 /* Internal: Find the LCID for a locale specification */
218 LCID MSVCRT_locale_to_LCID(const char *locale, unsigned short *codepage)
220 thread_data_t *data = msvcrt_get_thread_data();
221 LCID lcid;
222 locale_search_t search;
223 const char *cp, *region;
225 if (!strcmp(locale, data->cached_locale)) {
226 if (codepage)
227 *codepage = data->cached_cp;
228 return data->cached_lcid;
231 memset(&search, 0, sizeof(locale_search_t));
233 cp = strchr(locale, '.');
234 region = strchr(locale, '_');
236 lstrcpynA(search.search_language, locale, MAX_ELEM_LEN);
237 if(region) {
238 lstrcpynA(search.search_country, region+1, MAX_ELEM_LEN);
239 if(region-locale < MAX_ELEM_LEN)
240 search.search_language[region-locale] = '\0';
241 } else
242 search.search_country[0] = '\0';
244 if(cp) {
245 lstrcpynA(search.search_codepage, cp+1, MAX_ELEM_LEN);
246 if(region && cp-region-1<MAX_ELEM_LEN)
247 search.search_country[cp-region-1] = '\0';
248 if(cp-locale < MAX_ELEM_LEN)
249 search.search_language[cp-locale] = '\0';
250 } else
251 search.search_codepage[0] = '\0';
253 if(!search.search_country[0] && !search.search_codepage[0])
254 remap_synonym(search.search_language);
256 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
257 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
258 (LONG_PTR)&search);
260 if (!search.match_flags)
261 return -1;
263 /* If we were given something that didn't match, fail */
264 if (search.search_country[0] && !(search.match_flags & FOUND_COUNTRY))
265 return -1;
267 lcid = MAKELCID(search.found_lang_id, SORT_DEFAULT);
269 /* Populate partial locale, translating LCID to locale string elements */
270 if (!(search.match_flags & FOUND_CODEPAGE)) {
271 /* Even if a codepage is not enumerated for a locale
272 * it can be set if valid */
273 if (search.search_codepage[0]) {
274 if (IsValidCodePage(atoi(search.search_codepage)))
275 memcpy(search.found_codepage,search.search_codepage,MAX_ELEM_LEN);
276 else {
277 /* Special codepage values: OEM & ANSI */
278 if (!strcasecmp(search.search_codepage,"OCP")) {
279 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
280 search.found_codepage, MAX_ELEM_LEN);
281 } else if (!strcasecmp(search.search_codepage,"ACP")) {
282 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
283 search.found_codepage, MAX_ELEM_LEN);
284 } else
285 return -1;
287 if (!atoi(search.found_codepage))
288 return -1;
290 } else {
291 /* Prefer ANSI codepages if present */
292 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
293 search.found_codepage, MAX_ELEM_LEN);
294 if (!search.found_codepage[0] || !atoi(search.found_codepage))
295 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
296 search.found_codepage, MAX_ELEM_LEN);
299 if (codepage)
300 *codepage = atoi(search.found_codepage);
302 if (strlen(locale) < sizeof(data->cached_locale)) {
303 strcpy(data->cached_locale, locale);
304 data->cached_lcid = lcid;
305 data->cached_cp = codepage ? *codepage : atoi(search.found_codepage);
308 return lcid;
311 /* INTERNAL: Set lc_handle, lc_id and lc_category in threadlocinfo struct */
312 static BOOL update_threadlocinfo_category(LCID lcid, unsigned short cp,
313 MSVCRT_pthreadlocinfo locinfo, int category)
315 char buf[256], *p;
316 int len;
318 if(GetLocaleInfoA(lcid, LOCALE_ILANGUAGE|LOCALE_NOUSEROVERRIDE, buf, 256)) {
319 p = buf;
321 locinfo->lc_id[category].wLanguage = 0;
322 while(*p) {
323 locinfo->lc_id[category].wLanguage *= 16;
325 if(*p <= '9')
326 locinfo->lc_id[category].wLanguage += *p-'0';
327 else
328 locinfo->lc_id[category].wLanguage += *p-'a'+10;
330 p++;
333 locinfo->lc_id[category].wCountry =
334 locinfo->lc_id[category].wLanguage;
337 locinfo->lc_id[category].wCodePage = cp;
339 locinfo->lc_handle[category] = lcid;
341 len = 0;
342 len += GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE
343 |LOCALE_NOUSEROVERRIDE, buf, 256);
344 buf[len-1] = '_';
345 len += GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY
346 |LOCALE_NOUSEROVERRIDE, &buf[len], 256-len);
347 buf[len-1] = '.';
348 sprintf(buf+len, "%d", cp);
349 len += strlen(buf+len)+1;
351 locinfo->lc_category[category].locale = MSVCRT_malloc(len);
352 locinfo->lc_category[category].refcount = MSVCRT_malloc(sizeof(int));
353 if(!locinfo->lc_category[category].locale
354 || !locinfo->lc_category[category].refcount) {
355 MSVCRT_free(locinfo->lc_category[category].locale);
356 MSVCRT_free(locinfo->lc_category[category].refcount);
357 locinfo->lc_category[category].locale = NULL;
358 locinfo->lc_category[category].refcount = NULL;
359 return TRUE;
361 memcpy(locinfo->lc_category[category].locale, buf, len);
362 *locinfo->lc_category[category].refcount = 1;
364 return FALSE;
367 /* INTERNAL: swap pointers values */
368 static inline void swap_pointers(void **p1, void **p2) {
369 void *hlp;
371 hlp = *p1;
372 *p1 = *p2;
373 *p2 = hlp;
376 /* INTERNAL: returns pthreadlocinfo struct */
377 MSVCRT_pthreadlocinfo get_locinfo(void) {
378 thread_data_t *data = msvcrt_get_thread_data();
380 if(!data || !data->have_locale)
381 return MSVCRT_locale->locinfo;
383 return data->locinfo;
386 /* INTERNAL: returns pthreadlocinfo struct */
387 MSVCRT_pthreadmbcinfo get_mbcinfo(void) {
388 thread_data_t *data = msvcrt_get_thread_data();
390 if(!data || !data->have_locale)
391 return MSVCRT_locale->mbcinfo;
393 return data->mbcinfo;
396 /* INTERNAL: constructs string returned by setlocale */
397 static inline char* construct_lc_all(MSVCRT_pthreadlocinfo locinfo) {
398 static char current_lc_all[MAX_LOCALE_LENGTH];
400 int i;
402 for(i=MSVCRT_LC_MIN+1; i<MSVCRT_LC_MAX; i++) {
403 if(strcmp(locinfo->lc_category[i].locale,
404 locinfo->lc_category[i+1].locale))
405 break;
408 if(i==MSVCRT_LC_MAX)
409 return locinfo->lc_category[MSVCRT_LC_COLLATE].locale;
411 sprintf(current_lc_all,
412 "LC_COLLATE=%s;LC_CTYPE=%s;LC_MONETARY=%s;LC_NUMERIC=%s;LC_TIME=%s",
413 locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
414 locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
415 locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
416 locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
417 locinfo->lc_category[MSVCRT_LC_TIME].locale);
419 return current_lc_all;
423 /*********************************************************************
424 * _Getdays (MSVCRT.@)
426 char* CDECL _Getdays(void)
428 MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
429 int i, len, size;
430 char *out;
432 TRACE("\n");
434 size = cur->str.names.short_mon[0]-cur->str.names.short_wday[0];
435 out = MSVCRT_malloc(size+1);
436 if(!out)
437 return NULL;
439 size = 0;
440 for(i=0; i<7; i++) {
441 out[size++] = ':';
442 len = strlen(cur->str.names.short_wday[i]);
443 memcpy(&out[size], cur->str.names.short_wday[i], len);
444 size += len;
446 out[size++] = ':';
447 len = strlen(cur->str.names.wday[i]);
448 memcpy(&out[size], cur->str.names.wday[i], len);
449 size += len;
451 out[size] = '\0';
453 return out;
456 /*********************************************************************
457 * _W_Getdays (MSVCRT.@)
459 MSVCRT_wchar_t* CDECL _W_Getdays(void)
461 MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
462 MSVCRT_wchar_t *out;
463 int i, len, size;
465 TRACE("\n");
467 size = cur->wstr.names.short_mon[0]-cur->wstr.names.short_wday[0];
468 out = MSVCRT_malloc((size+1)*sizeof(*out));
469 if(!out)
470 return NULL;
472 size = 0;
473 for(i=0; i<7; i++) {
474 out[size++] = ':';
475 len = strlenW(cur->wstr.names.short_wday[i]);
476 memcpy(&out[size], cur->wstr.names.short_wday[i], len*sizeof(*out));
477 size += len;
479 out[size++] = ':';
480 len = strlenW(cur->wstr.names.wday[i]);
481 memcpy(&out[size], cur->wstr.names.wday[i], len*sizeof(*out));
482 size += len;
484 out[size] = '\0';
486 return out;
489 /*********************************************************************
490 * _Getmonths (MSVCRT.@)
492 char* CDECL _Getmonths(void)
494 MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
495 int i, len, size;
496 char *out;
498 TRACE("\n");
500 size = cur->str.names.am-cur->str.names.short_mon[0];
501 out = MSVCRT_malloc(size+1);
502 if(!out)
503 return NULL;
505 size = 0;
506 for(i=0; i<12; i++) {
507 out[size++] = ':';
508 len = strlen(cur->str.names.short_mon[i]);
509 memcpy(&out[size], cur->str.names.short_mon[i], len);
510 size += len;
512 out[size++] = ':';
513 len = strlen(cur->str.names.mon[i]);
514 memcpy(&out[size], cur->str.names.mon[i], len);
515 size += len;
517 out[size] = '\0';
519 return out;
522 /*********************************************************************
523 * _W_Getmonths (MSVCRT.@)
525 MSVCRT_wchar_t* CDECL _W_Getmonths(void)
527 MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
528 MSVCRT_wchar_t *out;
529 int i, len, size;
531 TRACE("\n");
533 size = cur->wstr.names.am-cur->wstr.names.short_mon[0];
534 out = MSVCRT_malloc((size+1)*sizeof(*out));
535 if(!out)
536 return NULL;
538 size = 0;
539 for(i=0; i<12; i++) {
540 out[size++] = ':';
541 len = strlenW(cur->wstr.names.short_mon[i]);
542 memcpy(&out[size], cur->wstr.names.short_mon[i], len*sizeof(*out));
543 size += len;
545 out[size++] = ':';
546 len = strlenW(cur->wstr.names.mon[i]);
547 memcpy(&out[size], cur->wstr.names.mon[i], len*sizeof(*out));
548 size += len;
550 out[size] = '\0';
552 return out;
555 /*********************************************************************
556 * _Gettnames (MSVCRT.@)
558 void* CDECL _Gettnames(void)
560 MSVCRT___lc_time_data *ret, *cur = get_locinfo()->lc_time_curr;
561 unsigned int i, size = sizeof(MSVCRT___lc_time_data);
563 TRACE("\n");
565 for(i=0; i<sizeof(cur->str.str)/sizeof(cur->str.str[0]); i++)
566 size += strlen(cur->str.str[i])+1;
568 ret = MSVCRT_malloc(size);
569 if(!ret)
570 return NULL;
571 memcpy(ret, cur, size);
573 size = 0;
574 for(i=0; i<sizeof(cur->str.str)/sizeof(cur->str.str[0]); i++) {
575 ret->str.str[i] = &ret->data[size];
576 size += strlen(&ret->data[size])+1;
579 return ret;
582 /*********************************************************************
583 * _W_Gettnames (MSVCR110.@)
585 void* CDECL _W_Gettnames(void)
587 return _Gettnames();
590 /*********************************************************************
591 * __crtLCMapStringA (MSVCRT.@)
593 int CDECL __crtLCMapStringA(
594 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
595 int dstlen, unsigned int codepage, int xflag
597 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
598 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
599 /* FIXME: A bit incorrect. But msvcrt itself just converts its
600 * arguments to wide strings and then calls LCMapStringW
602 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
605 /*********************************************************************
606 * __crtLCMapStringW (MSVCRT.@)
608 int CDECL __crtLCMapStringW(LCID lcid, DWORD mapflags, const MSVCRT_wchar_t *src,
609 int srclen, MSVCRT_wchar_t *dst, int dstlen, unsigned int codepage, int xflag)
611 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
612 lcid, mapflags, debugstr_w(src), srclen, dst, dstlen, codepage, xflag);
614 return LCMapStringW(lcid, mapflags, src, srclen, dst, dstlen);
617 /*********************************************************************
618 * __crtCompareStringA (MSVCRT.@)
620 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
621 const char *src2, int len2 )
623 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
624 lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
625 /* FIXME: probably not entirely right */
626 return CompareStringA( lcid, flags, src1, len1, src2, len2 );
629 /*********************************************************************
630 * __crtCompareStringW (MSVCRT.@)
632 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
633 const MSVCRT_wchar_t *src2, int len2 )
635 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
636 lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
637 /* FIXME: probably not entirely right */
638 return CompareStringW( lcid, flags, src1, len1, src2, len2 );
641 /*********************************************************************
642 * __crtGetLocaleInfoW (MSVCRT.@)
644 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
646 FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
647 /* FIXME: probably not entirely right */
648 return GetLocaleInfoW( lcid, type, buffer, len );
651 /*********************************************************************
652 * __crtGetLocaleInfoEx (MSVC110.@)
654 int CDECL __crtGetLocaleInfoEx( const WCHAR *locale, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
656 TRACE("(%s, %x, %p, %d)\n", debugstr_w(locale), type, buffer, len);
657 return GetLocaleInfoEx(locale, type, buffer, len);
660 /*********************************************************************
661 * btowc(MSVCRT.@)
663 MSVCRT_wint_t CDECL MSVCRT_btowc(int c)
665 unsigned char letter = c;
666 MSVCRT_wchar_t ret;
668 if(!MultiByteToWideChar(get_locinfo()->lc_handle[MSVCRT_LC_CTYPE],
669 0, (LPCSTR)&letter, 1, &ret, 1))
670 return 0;
672 return ret;
675 /*********************************************************************
676 * __crtGetStringTypeW(MSVCRT.@)
678 * This function was accepting different number of arguments in older
679 * versions of msvcrt.
681 BOOL CDECL __crtGetStringTypeW(DWORD unk, DWORD type,
682 MSVCRT_wchar_t *buffer, int len, WORD *out)
684 FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
685 unk, type, buffer, len, out);
687 return GetStringTypeW(type, buffer, len, out);
690 /*********************************************************************
691 * localeconv (MSVCRT.@)
693 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
695 return get_locinfo()->lconv;
698 /*********************************************************************
699 * __lconv_init (MSVCRT.@)
701 int CDECL __lconv_init(void)
703 /* this is used to make chars unsigned */
704 charmax = 255;
705 return 0;
708 /*********************************************************************
709 * ___lc_handle_func (MSVCRT.@)
711 LCID* CDECL ___lc_handle_func(void)
713 return get_locinfo()->lc_handle;
716 /*********************************************************************
717 * ___lc_locale_name_func (MSVCR110.@)
719 #if _MSVCR_VER >= 110
720 MSVCRT_wchar_t** CDECL ___lc_locale_name_func(void)
722 return get_locinfo()->lc_name;
724 #endif
726 /*********************************************************************
727 * ___lc_codepage_func (MSVCRT.@)
729 unsigned int CDECL ___lc_codepage_func(void)
731 return get_locinfo()->lc_codepage;
734 /*********************************************************************
735 * ___lc_collate_cp_func (MSVCRT.@)
737 int CDECL ___lc_collate_cp_func(void)
739 return get_locinfo()->lc_collate_cp;
742 /* INTERNAL: frees MSVCRT_pthreadlocinfo struct */
743 void free_locinfo(MSVCRT_pthreadlocinfo locinfo)
745 int i;
747 if(!locinfo)
748 return;
750 if(InterlockedDecrement(&locinfo->refcount))
751 return;
753 for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
754 MSVCRT_free(locinfo->lc_category[i].locale);
755 MSVCRT_free(locinfo->lc_category[i].refcount);
756 #if _MSVCR_VER >= 110
757 MSVCRT_free(locinfo->lc_name[i]);
758 #endif
761 if(locinfo->lconv) {
762 MSVCRT_free(locinfo->lconv->decimal_point);
763 MSVCRT_free(locinfo->lconv->thousands_sep);
764 MSVCRT_free(locinfo->lconv->grouping);
765 MSVCRT_free(locinfo->lconv->int_curr_symbol);
766 MSVCRT_free(locinfo->lconv->currency_symbol);
767 MSVCRT_free(locinfo->lconv->mon_decimal_point);
768 MSVCRT_free(locinfo->lconv->mon_thousands_sep);
769 MSVCRT_free(locinfo->lconv->mon_grouping);
770 MSVCRT_free(locinfo->lconv->positive_sign);
771 MSVCRT_free(locinfo->lconv->negative_sign);
772 #if _MSVCR_VER >= 120
773 MSVCRT_free(locinfo->lconv->_W_decimal_point);
774 MSVCRT_free(locinfo->lconv->_W_thousands_sep);
775 MSVCRT_free(locinfo->lconv->_W_int_curr_symbol);
776 MSVCRT_free(locinfo->lconv->_W_currency_symbol);
777 MSVCRT_free(locinfo->lconv->_W_mon_decimal_point);
778 MSVCRT_free(locinfo->lconv->_W_mon_thousands_sep);
779 MSVCRT_free(locinfo->lconv->_W_positive_sign);
780 MSVCRT_free(locinfo->lconv->_W_negative_sign);
781 #endif
783 MSVCRT_free(locinfo->lconv_intl_refcount);
784 MSVCRT_free(locinfo->lconv_num_refcount);
785 MSVCRT_free(locinfo->lconv_mon_refcount);
786 MSVCRT_free(locinfo->lconv);
788 MSVCRT_free(locinfo->ctype1_refcount);
789 MSVCRT_free(locinfo->ctype1);
791 MSVCRT_free(locinfo->pclmap);
792 MSVCRT_free(locinfo->pcumap);
794 MSVCRT_free(locinfo->lc_time_curr);
796 MSVCRT_free(locinfo);
799 /* INTERNAL: frees MSVCRT_pthreadmbcinfo struct */
800 void free_mbcinfo(MSVCRT_pthreadmbcinfo mbcinfo)
802 if(!mbcinfo)
803 return;
805 if(InterlockedDecrement(&mbcinfo->refcount))
806 return;
808 MSVCRT_free(mbcinfo);
811 /*********************************************************************
812 * _get_current_locale (MSVCRT.@)
814 MSVCRT__locale_t CDECL MSVCRT__get_current_locale(void)
816 MSVCRT__locale_t loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
817 if(!loc)
818 return NULL;
820 loc->locinfo = get_locinfo();
821 loc->mbcinfo = get_mbcinfo();
822 InterlockedIncrement(&loc->locinfo->refcount);
823 InterlockedIncrement(&loc->mbcinfo->refcount);
824 return loc;
827 /*********************************************************************
828 * _free_locale (MSVCRT.@)
830 void CDECL MSVCRT__free_locale(MSVCRT__locale_t locale)
832 if (!locale)
833 return;
835 free_locinfo(locale->locinfo);
836 free_mbcinfo(locale->mbcinfo);
837 MSVCRT_free(locale);
840 #if _MSVCR_VER >= 110
841 static inline BOOL set_lc_locale_name(MSVCRT_pthreadlocinfo locinfo, int cat)
843 LCID lcid = locinfo->lc_handle[cat];
844 WCHAR buf[100];
845 int len;
847 len = GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME
848 |LOCALE_NOUSEROVERRIDE, buf, 100);
849 if(!len) return FALSE;
851 if(LocaleNameToLCID(buf, 0) != lcid)
852 len = LCIDToLocaleName(lcid, buf, 100, 0);
854 if(!len || !(locinfo->lc_name[cat] = MSVCRT_malloc(len*sizeof(MSVCRT_wchar_t))))
855 return FALSE;
857 memcpy(locinfo->lc_name[cat], buf, len*sizeof(MSVCRT_wchar_t));
858 return TRUE;
860 #else
861 static inline BOOL set_lc_locale_name(MSVCRT_pthreadlocinfo locinfo, int cat)
863 return TRUE;
865 #endif
867 static inline BOOL category_needs_update(int cat, int user_cat,
868 MSVCRT_pthreadlocinfo locinfo, LCID lcid, unsigned short cp)
870 if(!locinfo) return TRUE;
871 if(user_cat!=cat && user_cat!=MSVCRT_LC_ALL) return FALSE;
872 return lcid!=locinfo->lc_handle[cat] || cp!=locinfo->lc_id[cat].wCodePage;
875 static MSVCRT_pthreadlocinfo create_locinfo(int category,
876 const char *locale, MSVCRT_pthreadlocinfo old_locinfo)
878 static const DWORD time_data[] = {
879 LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1, LOCALE_SABBREVDAYNAME2,
880 LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4, LOCALE_SABBREVDAYNAME5,
881 LOCALE_SABBREVDAYNAME6,
882 LOCALE_SDAYNAME7, LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3,
883 LOCALE_SDAYNAME4, LOCALE_SDAYNAME5, LOCALE_SDAYNAME6,
884 LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2, LOCALE_SABBREVMONTHNAME3,
885 LOCALE_SABBREVMONTHNAME4, LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
886 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8, LOCALE_SABBREVMONTHNAME9,
887 LOCALE_SABBREVMONTHNAME10, LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12,
888 LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME4,
889 LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8,
890 LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12,
891 LOCALE_S1159, LOCALE_S2359,
892 LOCALE_SSHORTDATE, LOCALE_SLONGDATE,
893 LOCALE_STIMEFORMAT
895 static const char collate[] = "COLLATE=";
896 static const char ctype[] = "CTYPE=";
897 static const char monetary[] = "MONETARY=";
898 static const char numeric[] = "NUMERIC=";
899 static const char time[] = "TIME=";
900 static const char cloc_short_date[] = "MM/dd/yy";
901 static const MSVCRT_wchar_t cloc_short_dateW[] = {'M','M','/','d','d','/','y','y',0};
902 static const char cloc_long_date[] = "dddd, MMMM dd, yyyy";
903 static const MSVCRT_wchar_t cloc_long_dateW[] = {'d','d','d','d',',',' ','M','M','M','M',' ','d','d',',',' ','y','y','y','y',0};
904 static const char cloc_time[] = "HH:mm:ss";
905 static const MSVCRT_wchar_t cloc_timeW[] = {'H','H',':','m','m',':','s','s',0};
907 MSVCRT_pthreadlocinfo locinfo;
908 LCID lcid[6] = { 0 }, lcid_tmp;
909 unsigned short cp[6] = { 0 };
910 char buf[256];
911 #if _MSVCR_VER >= 120
912 MSVCRT_wchar_t wbuf[256];
913 #endif
914 int i, ret, size;
916 TRACE("(%d %s)\n", category, locale);
918 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
919 return NULL;
921 if(locale[0]=='C' && !locale[1]) {
922 lcid[0] = 0;
923 cp[0] = CP_ACP;
924 } else if(!locale[0]) {
925 lcid[0] = GetSystemDefaultLCID();
926 GetLocaleInfoA(lcid[0], LOCALE_IDEFAULTANSICODEPAGE
927 |LOCALE_NOUSEROVERRIDE, buf, sizeof(buf));
928 cp[0] = atoi(buf);
930 for(i=1; i<6; i++) {
931 lcid[i] = lcid[0];
932 cp[i] = cp[0];
934 } else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
935 const char *p;
937 while(1) {
938 locale += 3; /* LC_ */
939 if(!memcmp(locale, collate, sizeof(collate)-1)) {
940 i = MSVCRT_LC_COLLATE;
941 locale += sizeof(collate)-1;
942 } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
943 i = MSVCRT_LC_CTYPE;
944 locale += sizeof(ctype)-1;
945 } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
946 i = MSVCRT_LC_MONETARY;
947 locale += sizeof(monetary)-1;
948 } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
949 i = MSVCRT_LC_NUMERIC;
950 locale += sizeof(numeric)-1;
951 } else if(!memcmp(locale, time, sizeof(time)-1)) {
952 i = MSVCRT_LC_TIME;
953 locale += sizeof(time)-1;
954 } else
955 return NULL;
957 p = strchr(locale, ';');
958 if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0')) {
959 lcid[i] = 0;
960 cp[i] = CP_ACP;
961 } else if(p) {
962 memcpy(buf, locale, p-locale);
963 buf[p-locale] = '\0';
964 lcid[i] = MSVCRT_locale_to_LCID(buf, &cp[i]);
965 } else
966 lcid[i] = MSVCRT_locale_to_LCID(locale, &cp[i]);
968 if(lcid[i] == -1)
969 return NULL;
971 if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
972 break;
974 locale = p+1;
976 } else {
977 lcid[0] = MSVCRT_locale_to_LCID(locale, &cp[0]);
978 if(lcid[0] == -1)
979 return NULL;
981 for(i=1; i<6; i++) {
982 lcid[i] = lcid[0];
983 cp[i] = cp[0];
987 locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
988 if(!locinfo)
989 return NULL;
991 memset(locinfo, 0, sizeof(MSVCRT_threadlocinfo));
992 locinfo->refcount = 1;
994 locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
995 if(!locinfo->lconv) {
996 free_locinfo(locinfo);
997 return NULL;
999 memset(locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
1001 locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
1002 locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
1003 if(!locinfo->pclmap || !locinfo->pcumap) {
1004 free_locinfo(locinfo);
1005 return NULL;
1008 if(!category_needs_update(MSVCRT_LC_COLLATE, category, old_locinfo,
1009 lcid[MSVCRT_LC_COLLATE], cp[MSVCRT_LC_COLLATE])) {
1010 locinfo->lc_handle[MSVCRT_LC_COLLATE] = old_locinfo->lc_handle[MSVCRT_LC_COLLATE];
1011 locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage = old_locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage;
1012 } else if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
1013 if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE],
1014 cp[MSVCRT_LC_COLLATE], locinfo, MSVCRT_LC_COLLATE)) {
1015 free_locinfo(locinfo);
1016 return NULL;
1019 locinfo->lc_collate_cp = locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage;
1021 if(!set_lc_locale_name(locinfo, MSVCRT_LC_COLLATE)) {
1022 free_locinfo(locinfo);
1023 return NULL;
1025 } else
1026 locinfo->lc_category[MSVCRT_LC_COLLATE].locale = MSVCRT__strdup("C");
1028 if(!category_needs_update(MSVCRT_LC_CTYPE, category, old_locinfo,
1029 lcid[MSVCRT_LC_CTYPE], cp[MSVCRT_LC_CTYPE])) {
1030 locinfo->lc_handle[MSVCRT_LC_CTYPE] = old_locinfo->lc_handle[MSVCRT_LC_CTYPE];
1031 locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage = old_locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
1032 } else if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
1033 CPINFO cp_info;
1034 int j;
1036 if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE],
1037 cp[MSVCRT_LC_CTYPE], locinfo, MSVCRT_LC_CTYPE)) {
1038 free_locinfo(locinfo);
1039 return NULL;
1042 locinfo->lc_codepage = locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
1043 locinfo->lc_clike = 1;
1044 if(!GetCPInfo(locinfo->lc_codepage, &cp_info)) {
1045 free_locinfo(locinfo);
1046 return NULL;
1048 locinfo->mb_cur_max = cp_info.MaxCharSize;
1050 locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
1051 locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
1052 if(!locinfo->ctype1_refcount || !locinfo->ctype1) {
1053 free_locinfo(locinfo);
1054 return NULL;
1057 *locinfo->ctype1_refcount = 1;
1058 locinfo->ctype1[0] = 0;
1059 locinfo->pctype = locinfo->ctype1+1;
1061 buf[1] = buf[2] = '\0';
1062 for(i=1; i<257; i++) {
1063 buf[0] = i-1;
1065 /* builtin GetStringTypeA doesn't set output to 0 on invalid input */
1066 locinfo->ctype1[i] = 0;
1068 GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
1069 1, locinfo->ctype1+i);
1072 for(i=0; cp_info.LeadByte[i+1]!=0; i+=2)
1073 for(j=cp_info.LeadByte[i]; j<=cp_info.LeadByte[i+1]; j++)
1074 locinfo->ctype1[j+1] |= MSVCRT__LEADBYTE;
1076 if(!set_lc_locale_name(locinfo, MSVCRT_LC_CTYPE)) {
1077 free_locinfo(locinfo);
1078 return NULL;
1081 for(i=0; i<256; i++) {
1082 if(locinfo->pctype[i] & MSVCRT__LEADBYTE)
1083 buf[i] = ' ';
1084 else
1085 buf[i] = i;
1088 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
1089 (char*)locinfo->pclmap, 256);
1090 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
1091 (char*)locinfo->pcumap, 256);
1092 } else {
1093 locinfo->lc_clike = 1;
1094 locinfo->mb_cur_max = 1;
1095 locinfo->pctype = MSVCRT__ctype+1;
1096 locinfo->lc_category[MSVCRT_LC_CTYPE].locale = MSVCRT__strdup("C");
1098 for(i=0; i<256; i++) {
1099 if(locinfo->pctype[i] & MSVCRT__LEADBYTE)
1100 buf[i] = ' ';
1101 else
1102 buf[i] = i;
1105 for(i=0; i<256; i++) {
1106 locinfo->pclmap[i] = (i>='A' && i<='Z' ? i-'A'+'a' : i);
1107 locinfo->pcumap[i] = (i>='a' && i<='z' ? i-'a'+'A' : i);
1111 if(!category_needs_update(MSVCRT_LC_MONETARY, category, old_locinfo,
1112 lcid[MSVCRT_LC_MONETARY], cp[MSVCRT_LC_MONETARY])) {
1113 locinfo->lc_handle[MSVCRT_LC_MONETARY] = old_locinfo->lc_handle[MSVCRT_LC_MONETARY];
1114 locinfo->lc_id[MSVCRT_LC_MONETARY].wCodePage = old_locinfo->lc_id[MSVCRT_LC_MONETARY].wCodePage;
1115 } else if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
1116 if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY],
1117 cp[MSVCRT_LC_MONETARY], locinfo, MSVCRT_LC_MONETARY)) {
1118 free_locinfo(locinfo);
1119 return NULL;
1122 locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1123 locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
1124 if(!locinfo->lconv_intl_refcount || !locinfo->lconv_mon_refcount) {
1125 free_locinfo(locinfo);
1126 return NULL;
1129 *locinfo->lconv_intl_refcount = 1;
1130 *locinfo->lconv_mon_refcount = 1;
1132 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
1133 |LOCALE_NOUSEROVERRIDE, buf, 256);
1134 if(i && (locinfo->lconv->int_curr_symbol = MSVCRT_malloc(i)))
1135 memcpy(locinfo->lconv->int_curr_symbol, buf, i);
1136 else {
1137 free_locinfo(locinfo);
1138 return NULL;
1141 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
1142 |LOCALE_NOUSEROVERRIDE, buf, 256);
1143 if(i && (locinfo->lconv->currency_symbol = MSVCRT_malloc(i)))
1144 memcpy(locinfo->lconv->currency_symbol, buf, i);
1145 else {
1146 free_locinfo(locinfo);
1147 return NULL;
1150 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
1151 |LOCALE_NOUSEROVERRIDE, buf, 256);
1152 if(i && (locinfo->lconv->mon_decimal_point = MSVCRT_malloc(i)))
1153 memcpy(locinfo->lconv->mon_decimal_point, buf, i);
1154 else {
1155 free_locinfo(locinfo);
1156 return NULL;
1159 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
1160 |LOCALE_NOUSEROVERRIDE, buf, 256);
1161 if(i && (locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(i)))
1162 memcpy(locinfo->lconv->mon_thousands_sep, buf, i);
1163 else {
1164 free_locinfo(locinfo);
1165 return NULL;
1168 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING
1169 |LOCALE_NOUSEROVERRIDE, buf, 256);
1170 if(i>1)
1171 i = i/2 + (buf[i-2]=='0'?0:1);
1172 if(i && (locinfo->lconv->mon_grouping = MSVCRT_malloc(i))) {
1173 for(i=0; buf[i+1]==';'; i+=2)
1174 locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1175 locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1176 if(buf[i] != '0')
1177 locinfo->lconv->mon_grouping[i/2+1] = 127;
1178 } else {
1179 free_locinfo(locinfo);
1180 return NULL;
1183 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
1184 |LOCALE_NOUSEROVERRIDE, buf, 256);
1185 if(i && (locinfo->lconv->positive_sign = MSVCRT_malloc(i)))
1186 memcpy(locinfo->lconv->positive_sign, buf, i);
1187 else {
1188 free_locinfo(locinfo);
1189 return NULL;
1192 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
1193 |LOCALE_NOUSEROVERRIDE, buf, 256);
1194 if(i && (locinfo->lconv->negative_sign = MSVCRT_malloc(i)))
1195 memcpy(locinfo->lconv->negative_sign, buf, i);
1196 else {
1197 free_locinfo(locinfo);
1198 return NULL;
1201 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS
1202 |LOCALE_NOUSEROVERRIDE, buf, 256))
1203 locinfo->lconv->int_frac_digits = atoi(buf);
1204 else {
1205 free_locinfo(locinfo);
1206 return NULL;
1209 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS
1210 |LOCALE_NOUSEROVERRIDE, buf, 256))
1211 locinfo->lconv->frac_digits = atoi(buf);
1212 else {
1213 free_locinfo(locinfo);
1214 return NULL;
1217 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES
1218 |LOCALE_NOUSEROVERRIDE, buf, 256))
1219 locinfo->lconv->p_cs_precedes = atoi(buf);
1220 else {
1221 free_locinfo(locinfo);
1222 return NULL;
1225 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE
1226 |LOCALE_NOUSEROVERRIDE, buf, 256))
1227 locinfo->lconv->p_sep_by_space = atoi(buf);
1228 else {
1229 free_locinfo(locinfo);
1230 return NULL;
1233 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES
1234 |LOCALE_NOUSEROVERRIDE, buf, 256))
1235 locinfo->lconv->n_cs_precedes = atoi(buf);
1236 else {
1237 free_locinfo(locinfo);
1238 return NULL;
1241 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE
1242 |LOCALE_NOUSEROVERRIDE, buf, 256))
1243 locinfo->lconv->n_sep_by_space = atoi(buf);
1244 else {
1245 free_locinfo(locinfo);
1246 return NULL;
1249 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN
1250 |LOCALE_NOUSEROVERRIDE, buf, 256))
1251 locinfo->lconv->p_sign_posn = atoi(buf);
1252 else {
1253 free_locinfo(locinfo);
1254 return NULL;
1257 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN
1258 |LOCALE_NOUSEROVERRIDE, buf, 256))
1259 locinfo->lconv->n_sign_posn = atoi(buf);
1260 else {
1261 free_locinfo(locinfo);
1262 return NULL;
1265 #if _MSVCR_VER >= 120
1266 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
1267 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1268 if(i && (locinfo->lconv->_W_int_curr_symbol = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1269 memcpy(locinfo->lconv->_W_int_curr_symbol, wbuf, i * sizeof(MSVCRT_wchar_t));
1270 else {
1271 free_locinfo(locinfo);
1272 return NULL;
1275 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
1276 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1277 if(i && (locinfo->lconv->_W_currency_symbol = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1278 memcpy(locinfo->lconv->_W_currency_symbol, wbuf, i * sizeof(MSVCRT_wchar_t));
1279 else {
1280 free_locinfo(locinfo);
1281 return NULL;
1284 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
1285 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1286 if(i && (locinfo->lconv->_W_mon_decimal_point = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1287 memcpy(locinfo->lconv->_W_mon_decimal_point, wbuf, i * sizeof(MSVCRT_wchar_t));
1288 else {
1289 free_locinfo(locinfo);
1290 return NULL;
1293 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
1294 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1295 if(i && (locinfo->lconv->_W_mon_thousands_sep = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1296 memcpy(locinfo->lconv->_W_mon_thousands_sep, wbuf, i * sizeof(MSVCRT_wchar_t));
1297 else {
1298 free_locinfo(locinfo);
1299 return NULL;
1302 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
1303 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1304 if(i && (locinfo->lconv->_W_positive_sign = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1305 memcpy(locinfo->lconv->_W_positive_sign, wbuf, i * sizeof(MSVCRT_wchar_t));
1306 else {
1307 free_locinfo(locinfo);
1308 return NULL;
1311 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
1312 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1313 if(i && (locinfo->lconv->_W_negative_sign = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1314 memcpy(locinfo->lconv->_W_negative_sign, wbuf, i * sizeof(MSVCRT_wchar_t));
1315 else {
1316 free_locinfo(locinfo);
1317 return NULL;
1319 #endif
1321 if(!set_lc_locale_name(locinfo, MSVCRT_LC_MONETARY)) {
1322 free_locinfo(locinfo);
1323 return NULL;
1325 } else {
1326 locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
1327 locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
1328 locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
1329 locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
1330 locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
1331 locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
1332 locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
1334 if(!locinfo->lconv->int_curr_symbol || !locinfo->lconv->currency_symbol
1335 || !locinfo->lconv->mon_decimal_point || !locinfo->lconv->mon_thousands_sep
1336 || !locinfo->lconv->mon_grouping || !locinfo->lconv->positive_sign
1337 || !locinfo->lconv->negative_sign) {
1338 free_locinfo(locinfo);
1339 return NULL;
1342 locinfo->lconv->int_curr_symbol[0] = '\0';
1343 locinfo->lconv->currency_symbol[0] = '\0';
1344 locinfo->lconv->mon_decimal_point[0] = '\0';
1345 locinfo->lconv->mon_thousands_sep[0] = '\0';
1346 locinfo->lconv->mon_grouping[0] = '\0';
1347 locinfo->lconv->positive_sign[0] = '\0';
1348 locinfo->lconv->negative_sign[0] = '\0';
1349 locinfo->lconv->int_frac_digits = 127;
1350 locinfo->lconv->frac_digits = 127;
1351 locinfo->lconv->p_cs_precedes = 127;
1352 locinfo->lconv->p_sep_by_space = 127;
1353 locinfo->lconv->n_cs_precedes = 127;
1354 locinfo->lconv->n_sep_by_space = 127;
1355 locinfo->lconv->p_sign_posn = 127;
1356 locinfo->lconv->n_sign_posn = 127;
1358 #if _MSVCR_VER >= 120
1359 locinfo->lconv->_W_int_curr_symbol = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1360 locinfo->lconv->_W_currency_symbol = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1361 locinfo->lconv->_W_mon_decimal_point = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1362 locinfo->lconv->_W_mon_thousands_sep = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1363 locinfo->lconv->_W_positive_sign = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1364 locinfo->lconv->_W_negative_sign = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1366 if(!locinfo->lconv->_W_int_curr_symbol || !locinfo->lconv->_W_currency_symbol
1367 || !locinfo->lconv->_W_mon_decimal_point || !locinfo->lconv->_W_mon_thousands_sep
1368 || !locinfo->lconv->positive_sign || !locinfo->lconv->negative_sign) {
1369 free_locinfo(locinfo);
1370 return NULL;
1373 locinfo->lconv->_W_int_curr_symbol[0] = '\0';
1374 locinfo->lconv->_W_currency_symbol[0] = '\0';
1375 locinfo->lconv->_W_mon_decimal_point[0] = '\0';
1376 locinfo->lconv->_W_mon_thousands_sep[0] = '\0';
1377 locinfo->lconv->_W_positive_sign[0] = '\0';
1378 locinfo->lconv->_W_negative_sign[0] = '\0';
1379 #endif
1381 locinfo->lc_category[MSVCRT_LC_MONETARY].locale = MSVCRT__strdup("C");
1384 if(!category_needs_update(MSVCRT_LC_NUMERIC, category, old_locinfo,
1385 lcid[MSVCRT_LC_NUMERIC], cp[MSVCRT_LC_NUMERIC])) {
1386 locinfo->lc_handle[MSVCRT_LC_NUMERIC] = old_locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1387 locinfo->lc_id[MSVCRT_LC_NUMERIC].wCodePage = old_locinfo->lc_id[MSVCRT_LC_NUMERIC].wCodePage;
1388 } else if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
1389 if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC],
1390 cp[MSVCRT_LC_NUMERIC], locinfo, MSVCRT_LC_NUMERIC)) {
1391 free_locinfo(locinfo);
1392 return NULL;
1395 if(!locinfo->lconv_intl_refcount)
1396 locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1397 locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1398 if(!locinfo->lconv_intl_refcount || !locinfo->lconv_num_refcount) {
1399 free_locinfo(locinfo);
1400 return NULL;
1403 *locinfo->lconv_intl_refcount = 1;
1404 *locinfo->lconv_num_refcount = 1;
1406 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1407 |LOCALE_NOUSEROVERRIDE, buf, 256);
1408 if(i && (locinfo->lconv->decimal_point = MSVCRT_malloc(i)))
1409 memcpy(locinfo->lconv->decimal_point, buf, i);
1410 else {
1411 free_locinfo(locinfo);
1412 return NULL;
1415 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1416 |LOCALE_NOUSEROVERRIDE, buf, 256);
1417 if(i && (locinfo->lconv->thousands_sep = MSVCRT_malloc(i)))
1418 memcpy(locinfo->lconv->thousands_sep, buf, i);
1419 else {
1420 free_locinfo(locinfo);
1421 return NULL;
1424 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING
1425 |LOCALE_NOUSEROVERRIDE, buf, 256);
1426 if(i>1)
1427 i = i/2 + (buf[i-2]=='0'?0:1);
1428 if(i && (locinfo->lconv->grouping = MSVCRT_malloc(i))) {
1429 for(i=0; buf[i+1]==';'; i+=2)
1430 locinfo->lconv->grouping[i/2] = buf[i]-'0';
1431 locinfo->lconv->grouping[i/2] = buf[i]-'0';
1432 if(buf[i] != '0')
1433 locinfo->lconv->grouping[i/2+1] = 127;
1434 } else {
1435 free_locinfo(locinfo);
1436 return NULL;
1439 #if _MSVCR_VER >= 120
1440 i = GetLocaleInfoW(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1441 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1442 if(i && (locinfo->lconv->_W_decimal_point = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1443 memcpy(locinfo->lconv->_W_decimal_point, wbuf, i * sizeof(MSVCRT_wchar_t));
1444 else {
1445 free_locinfo(locinfo);
1446 return NULL;
1449 i = GetLocaleInfoW(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1450 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1451 if(i && (locinfo->lconv->_W_thousands_sep = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1452 memcpy(locinfo->lconv->_W_thousands_sep, wbuf, i * sizeof(MSVCRT_wchar_t));
1453 else {
1454 free_locinfo(locinfo);
1455 return NULL;
1457 #endif
1459 if(!set_lc_locale_name(locinfo, MSVCRT_LC_NUMERIC)) {
1460 free_locinfo(locinfo);
1461 return NULL;
1463 } else {
1464 locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1465 locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1466 locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1467 if(!locinfo->lconv->decimal_point || !locinfo->lconv->thousands_sep
1468 || !locinfo->lconv->grouping) {
1469 free_locinfo(locinfo);
1470 return NULL;
1473 locinfo->lconv->decimal_point[0] = '.';
1474 locinfo->lconv->decimal_point[1] = '\0';
1475 locinfo->lconv->thousands_sep[0] = '\0';
1476 locinfo->lconv->grouping[0] = '\0';
1478 #if _MSVCR_VER >= 120
1479 locinfo->lconv->_W_decimal_point = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[2]));
1480 locinfo->lconv->_W_thousands_sep = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1482 if(!locinfo->lconv->_W_decimal_point || !locinfo->lconv->_W_thousands_sep) {
1483 free_locinfo(locinfo);
1484 return NULL;
1487 locinfo->lconv->_W_decimal_point[0] = '.';
1488 locinfo->lconv->_W_decimal_point[1] = '\0';
1489 locinfo->lconv->_W_thousands_sep[0] = '\0';
1490 #endif
1492 locinfo->lc_category[MSVCRT_LC_NUMERIC].locale = MSVCRT__strdup("C");
1495 if(!category_needs_update(MSVCRT_LC_TIME, category, old_locinfo,
1496 lcid[MSVCRT_LC_TIME], cp[MSVCRT_LC_TIME])) {
1497 locinfo->lc_handle[MSVCRT_LC_TIME] = old_locinfo->lc_handle[MSVCRT_LC_TIME];
1498 locinfo->lc_id[MSVCRT_LC_TIME].wCodePage = old_locinfo->lc_id[MSVCRT_LC_TIME].wCodePage;
1499 } else {
1500 if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1501 if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME],
1502 cp[MSVCRT_LC_TIME], locinfo, MSVCRT_LC_TIME)) {
1503 free_locinfo(locinfo);
1504 return NULL;
1507 if(!set_lc_locale_name(locinfo, MSVCRT_LC_TIME)) {
1508 free_locinfo(locinfo);
1509 return NULL;
1511 } else
1512 locinfo->lc_category[MSVCRT_LC_TIME].locale = MSVCRT__strdup("C");
1514 size = sizeof(MSVCRT___lc_time_data);
1515 lcid_tmp = lcid[MSVCRT_LC_TIME] ? lcid[MSVCRT_LC_TIME] : MAKELCID(LANG_ENGLISH, SORT_DEFAULT);
1516 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1517 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1518 size += sizeof(cloc_short_date) + sizeof(cloc_short_dateW);
1519 }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1520 size += sizeof(cloc_long_date) + sizeof(cloc_long_dateW);
1521 }else {
1522 ret = GetLocaleInfoA(lcid_tmp, time_data[i]
1523 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1524 if(!ret) {
1525 free_locinfo(locinfo);
1526 return NULL;
1528 size += ret;
1530 ret = GetLocaleInfoW(lcid_tmp, time_data[i]
1531 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1532 if(!ret) {
1533 free_locinfo(locinfo);
1534 return NULL;
1536 size += ret*sizeof(MSVCRT_wchar_t);
1539 #if _MSVCR_VER >= 110
1540 size += LCIDToLocaleName(lcid[MSVCRT_LC_TIME], NULL, 0, 0)*sizeof(MSVCRT_wchar_t);
1541 #endif
1543 locinfo->lc_time_curr = MSVCRT_malloc(size);
1544 if(!locinfo->lc_time_curr) {
1545 free_locinfo(locinfo);
1546 return NULL;
1549 ret = 0;
1550 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1551 locinfo->lc_time_curr->str.str[i] = &locinfo->lc_time_curr->data[ret];
1552 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1553 memcpy(&locinfo->lc_time_curr->data[ret], cloc_short_date, sizeof(cloc_short_date));
1554 ret += sizeof(cloc_short_date);
1555 }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1556 memcpy(&locinfo->lc_time_curr->data[ret], cloc_long_date, sizeof(cloc_long_date));
1557 ret += sizeof(cloc_long_date);
1558 }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1559 memcpy(&locinfo->lc_time_curr->data[ret], cloc_time, sizeof(cloc_time));
1560 ret += sizeof(cloc_time);
1561 }else {
1562 ret += GetLocaleInfoA(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1563 &locinfo->lc_time_curr->data[ret], size-ret);
1566 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1567 locinfo->lc_time_curr->wstr.wstr[i] = (MSVCRT_wchar_t*)&locinfo->lc_time_curr->data[ret];
1568 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1569 memcpy(&locinfo->lc_time_curr->data[ret], cloc_short_dateW, sizeof(cloc_short_dateW));
1570 ret += sizeof(cloc_short_dateW);
1571 }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1572 memcpy(&locinfo->lc_time_curr->data[ret], cloc_long_dateW, sizeof(cloc_long_dateW));
1573 ret += sizeof(cloc_long_dateW);
1574 }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1575 memcpy(&locinfo->lc_time_curr->data[ret], cloc_timeW, sizeof(cloc_timeW));
1576 ret += sizeof(cloc_timeW);
1577 }else {
1578 ret += GetLocaleInfoW(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1579 (MSVCRT_wchar_t*)&locinfo->lc_time_curr->data[ret], size-ret)*sizeof(MSVCRT_wchar_t);
1582 #if _MSVCR_VER >= 110
1583 locinfo->lc_time_curr->locname = (MSVCRT_wchar_t*)&locinfo->lc_time_curr->data[ret];
1584 LCIDToLocaleName(lcid[MSVCRT_LC_TIME], locinfo->lc_time_curr->locname,
1585 (size-ret)/sizeof(MSVCRT_wchar_t), 0);
1586 #else
1587 locinfo->lc_time_curr->lcid = lcid[MSVCRT_LC_TIME];
1588 #endif
1591 return locinfo;
1594 /*********************************************************************
1595 * _create_locale (MSVCRT.@)
1597 MSVCRT__locale_t CDECL MSVCRT__create_locale(int category, const char *locale)
1599 MSVCRT__locale_t loc;
1601 loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
1602 if(!loc)
1603 return NULL;
1605 loc->locinfo = create_locinfo(category, locale, NULL);
1606 if(!loc->locinfo) {
1607 MSVCRT_free(loc);
1608 return NULL;
1611 loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
1612 if(!loc->mbcinfo) {
1613 free_locinfo(loc->locinfo);
1614 MSVCRT_free(loc);
1615 return NULL;
1618 loc->mbcinfo->refcount = 1;
1619 _setmbcp_l(loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage,
1620 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE], loc->mbcinfo);
1621 return loc;
1624 /*********************************************************************
1625 * setlocale (MSVCRT.@)
1627 char* CDECL MSVCRT_setlocale(int category, const char* locale)
1629 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
1630 MSVCRT_pthreadlocinfo newlocinfo;
1632 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1633 return NULL;
1635 if(!locale) {
1636 if(category == MSVCRT_LC_ALL)
1637 return construct_lc_all(locinfo);
1639 return locinfo->lc_category[category].locale;
1642 newlocinfo = create_locinfo(category, locale, locinfo);
1643 if(!newlocinfo) {
1644 WARN("%d %s failed\n", category, locale);
1645 return NULL;
1648 LOCK_LOCALE;
1650 if(locinfo->lc_handle[MSVCRT_LC_COLLATE]!=newlocinfo->lc_handle[MSVCRT_LC_COLLATE]
1651 || locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage!=newlocinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage) {
1652 locinfo->lc_collate_cp = newlocinfo->lc_collate_cp;
1653 locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1654 newlocinfo->lc_handle[MSVCRT_LC_COLLATE];
1655 locinfo->lc_id[MSVCRT_LC_COLLATE] =
1656 newlocinfo->lc_id[MSVCRT_LC_COLLATE];
1657 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1658 (void**)&newlocinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1659 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1660 (void**)&newlocinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1662 #if _MSVCR_VER >= 110
1663 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_COLLATE],
1664 (void**)&newlocinfo->lc_name[MSVCRT_LC_COLLATE]);
1665 #endif
1668 if(locinfo->lc_handle[MSVCRT_LC_CTYPE]!=newlocinfo->lc_handle[MSVCRT_LC_CTYPE]
1669 || locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage!=newlocinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage) {
1670 locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1671 newlocinfo->lc_handle[MSVCRT_LC_CTYPE];
1672 locinfo->lc_id[MSVCRT_LC_CTYPE] =
1673 newlocinfo->lc_id[MSVCRT_LC_CTYPE];
1674 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1675 (void**)&newlocinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1676 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1677 (void**)&newlocinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1679 locinfo->lc_codepage = newlocinfo->lc_codepage;
1680 locinfo->lc_clike = newlocinfo->lc_clike;
1681 locinfo->mb_cur_max = newlocinfo->mb_cur_max;
1683 swap_pointers((void**)&locinfo->ctype1_refcount,
1684 (void**)&newlocinfo->ctype1_refcount);
1685 swap_pointers((void**)&locinfo->ctype1, (void**)&newlocinfo->ctype1);
1686 swap_pointers((void**)&locinfo->pctype, (void**)&newlocinfo->pctype);
1687 swap_pointers((void**)&locinfo->pclmap, (void**)&newlocinfo->pclmap);
1688 swap_pointers((void**)&locinfo->pcumap, (void**)&newlocinfo->pcumap);
1690 #if _MSVCR_VER >= 110
1691 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_CTYPE],
1692 (void**)&newlocinfo->lc_name[MSVCRT_LC_CTYPE]);
1693 #endif
1696 if(locinfo->lc_handle[MSVCRT_LC_MONETARY]!=newlocinfo->lc_handle[MSVCRT_LC_MONETARY]
1697 || locinfo->lc_id[MSVCRT_LC_MONETARY].wCodePage!=newlocinfo->lc_id[MSVCRT_LC_MONETARY].wCodePage) {
1698 locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1699 newlocinfo->lc_handle[MSVCRT_LC_MONETARY];
1700 locinfo->lc_id[MSVCRT_LC_MONETARY] =
1701 newlocinfo->lc_id[MSVCRT_LC_MONETARY];
1702 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1703 (void**)&newlocinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1704 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1705 (void**)&newlocinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1707 swap_pointers((void**)&locinfo->lconv->int_curr_symbol,
1708 (void**)&newlocinfo->lconv->int_curr_symbol);
1709 swap_pointers((void**)&locinfo->lconv->currency_symbol,
1710 (void**)&newlocinfo->lconv->currency_symbol);
1711 swap_pointers((void**)&locinfo->lconv->mon_decimal_point,
1712 (void**)&newlocinfo->lconv->mon_decimal_point);
1713 swap_pointers((void**)&locinfo->lconv->mon_thousands_sep,
1714 (void**)&newlocinfo->lconv->mon_thousands_sep);
1715 swap_pointers((void**)&locinfo->lconv->mon_grouping,
1716 (void**)&newlocinfo->lconv->mon_grouping);
1717 swap_pointers((void**)&locinfo->lconv->positive_sign,
1718 (void**)&newlocinfo->lconv->positive_sign);
1719 swap_pointers((void**)&locinfo->lconv->negative_sign,
1720 (void**)&newlocinfo->lconv->negative_sign);
1722 #if _MSVCR_VER >= 120
1723 swap_pointers((void**)&locinfo->lconv->_W_int_curr_symbol,
1724 (void**)&newlocinfo->lconv->_W_int_curr_symbol);
1725 swap_pointers((void**)&locinfo->lconv->_W_currency_symbol,
1726 (void**)&newlocinfo->lconv->_W_currency_symbol);
1727 swap_pointers((void**)&locinfo->lconv->_W_mon_decimal_point,
1728 (void**)&newlocinfo->lconv->_W_mon_decimal_point);
1729 swap_pointers((void**)&locinfo->lconv->_W_mon_thousands_sep,
1730 (void**)&newlocinfo->lconv->_W_mon_thousands_sep);
1731 swap_pointers((void**)&locinfo->lconv->_W_positive_sign,
1732 (void**)&newlocinfo->lconv->_W_positive_sign);
1733 swap_pointers((void**)&locinfo->lconv->_W_negative_sign,
1734 (void**)&newlocinfo->lconv->_W_negative_sign);
1735 #endif
1737 locinfo->lconv->int_frac_digits = newlocinfo->lconv->int_frac_digits;
1738 locinfo->lconv->frac_digits = newlocinfo->lconv->frac_digits;
1739 locinfo->lconv->p_cs_precedes = newlocinfo->lconv->p_cs_precedes;
1740 locinfo->lconv->p_sep_by_space = newlocinfo->lconv->p_sep_by_space;
1741 locinfo->lconv->n_cs_precedes = newlocinfo->lconv->n_cs_precedes;
1742 locinfo->lconv->n_sep_by_space = newlocinfo->lconv->n_sep_by_space;
1743 locinfo->lconv->p_sign_posn = newlocinfo->lconv->p_sign_posn;
1744 locinfo->lconv->n_sign_posn = newlocinfo->lconv->n_sign_posn;
1746 #if _MSVCR_VER >= 110
1747 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_MONETARY],
1748 (void**)&newlocinfo->lc_name[MSVCRT_LC_MONETARY]);
1749 #endif
1752 if(locinfo->lc_handle[MSVCRT_LC_NUMERIC]!=newlocinfo->lc_handle[MSVCRT_LC_NUMERIC]
1753 || locinfo->lc_id[MSVCRT_LC_NUMERIC].wCodePage!=newlocinfo->lc_id[MSVCRT_LC_NUMERIC].wCodePage) {
1754 locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1755 newlocinfo->lc_handle[MSVCRT_LC_NUMERIC];
1756 locinfo->lc_id[MSVCRT_LC_NUMERIC] =
1757 newlocinfo->lc_id[MSVCRT_LC_NUMERIC];
1758 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1759 (void**)&newlocinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1760 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1761 (void**)&newlocinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1763 swap_pointers((void**)&locinfo->lconv->decimal_point,
1764 (void**)&newlocinfo->lconv->decimal_point);
1765 swap_pointers((void**)&locinfo->lconv->thousands_sep,
1766 (void**)&newlocinfo->lconv->thousands_sep);
1767 swap_pointers((void**)&locinfo->lconv->grouping,
1768 (void**)&newlocinfo->lconv->grouping);
1770 #if _MSVCR_VER >= 120
1771 swap_pointers((void**)&locinfo->lconv->_W_decimal_point,
1772 (void**)&newlocinfo->lconv->_W_decimal_point);
1773 swap_pointers((void**)&locinfo->lconv->_W_thousands_sep,
1774 (void**)&newlocinfo->lconv->_W_thousands_sep);
1775 #endif
1777 #if _MSVCR_VER >= 110
1778 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_NUMERIC],
1779 (void**)&newlocinfo->lc_name[MSVCRT_LC_NUMERIC]);
1780 #endif
1783 if(locinfo->lc_handle[MSVCRT_LC_TIME]!=newlocinfo->lc_handle[MSVCRT_LC_TIME]
1784 || locinfo->lc_id[MSVCRT_LC_TIME].wCodePage!=newlocinfo->lc_id[MSVCRT_LC_TIME].wCodePage) {
1785 locinfo->lc_handle[MSVCRT_LC_TIME] =
1786 newlocinfo->lc_handle[MSVCRT_LC_TIME];
1787 locinfo->lc_id[MSVCRT_LC_TIME] =
1788 newlocinfo->lc_id[MSVCRT_LC_TIME];
1789 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].locale,
1790 (void**)&newlocinfo->lc_category[MSVCRT_LC_TIME].locale);
1791 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1792 (void**)&newlocinfo->lc_category[MSVCRT_LC_TIME].refcount);
1793 swap_pointers((void**)&locinfo->lc_time_curr,
1794 (void**)&newlocinfo->lc_time_curr);
1796 #if _MSVCR_VER >= 110
1797 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_TIME],
1798 (void**)&newlocinfo->lc_name[MSVCRT_LC_TIME]);
1799 #endif
1802 free_locinfo(newlocinfo);
1803 UNLOCK_LOCALE;
1805 if(locinfo == MSVCRT_locale->locinfo) {
1806 int i;
1808 MSVCRT___lc_codepage = locinfo->lc_codepage;
1809 MSVCRT___lc_collate_cp = locinfo->lc_collate_cp;
1810 MSVCRT___mb_cur_max = locinfo->mb_cur_max;
1811 MSVCRT__pctype = locinfo->pctype;
1812 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1813 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1816 if(category == MSVCRT_LC_ALL)
1817 return construct_lc_all(locinfo);
1819 return locinfo->lc_category[category].locale;
1822 /*********************************************************************
1823 * _wsetlocale (MSVCRT.@)
1825 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* wlocale)
1827 static MSVCRT_wchar_t current_lc_all[MAX_LOCALE_LENGTH];
1829 char *locale = NULL;
1830 const char *ret;
1831 MSVCRT_size_t len;
1833 if(wlocale) {
1834 len = MSVCRT_wcstombs(NULL, wlocale, 0);
1835 if(len == -1)
1836 return NULL;
1838 locale = MSVCRT_malloc(++len);
1839 if(!locale)
1840 return NULL;
1842 MSVCRT_wcstombs(locale, wlocale, len);
1845 LOCK_LOCALE;
1846 ret = MSVCRT_setlocale(category, locale);
1847 MSVCRT_free(locale);
1849 if(ret && MSVCRT_mbstowcs(current_lc_all, ret, MAX_LOCALE_LENGTH)==-1)
1850 ret = NULL;
1852 UNLOCK_LOCALE;
1853 return ret ? current_lc_all : NULL;
1856 /*********************************************************************
1857 * _configthreadlocale (MSVCR80.@)
1859 int CDECL _configthreadlocale(int type)
1861 thread_data_t *data = msvcrt_get_thread_data();
1862 MSVCRT__locale_t locale;
1863 int ret;
1865 if(!data)
1866 return -1;
1868 ret = (data->have_locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1870 if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1871 if(!data->have_locale) {
1872 /* Copy current global locale */
1873 locale = MSVCRT__create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
1874 if(!locale)
1875 return -1;
1877 data->locinfo = locale->locinfo;
1878 data->mbcinfo = locale->mbcinfo;
1879 data->have_locale = TRUE;
1880 MSVCRT_free(locale);
1883 return ret;
1886 if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1887 if(data->have_locale) {
1888 free_locinfo(data->locinfo);
1889 free_mbcinfo(data->mbcinfo);
1890 data->locinfo = MSVCRT_locale->locinfo;
1891 data->mbcinfo = MSVCRT_locale->mbcinfo;
1892 data->have_locale = FALSE;
1895 return ret;
1898 if(!type)
1899 return ret;
1901 return -1;
1904 BOOL msvcrt_init_locale(void)
1906 int i;
1908 LOCK_LOCALE;
1909 MSVCRT_locale = MSVCRT__create_locale(0, "C");
1910 UNLOCK_LOCALE;
1911 if(!MSVCRT_locale)
1912 return FALSE;
1914 MSVCRT___lc_codepage = MSVCRT_locale->locinfo->lc_codepage;
1915 MSVCRT___lc_collate_cp = MSVCRT_locale->locinfo->lc_collate_cp;
1916 MSVCRT___mb_cur_max = MSVCRT_locale->locinfo->mb_cur_max;
1917 MSVCRT__pctype = MSVCRT_locale->locinfo->pctype;
1918 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1919 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1920 _setmbcp(_MB_CP_ANSI);
1921 return TRUE;