msi/tests: Add a test for MsiInstallProduct with command line containing double quote...
[wine.git] / dlls / msvcrt / locale.c
blobb00bc78f59fa61ad46c064f8c818f307e08e7790
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 * __crtLCMapStringA (MSVCRT.@)
585 int CDECL __crtLCMapStringA(
586 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
587 int dstlen, unsigned int codepage, int xflag
589 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
590 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
591 /* FIXME: A bit incorrect. But msvcrt itself just converts its
592 * arguments to wide strings and then calls LCMapStringW
594 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
597 /*********************************************************************
598 * __crtLCMapStringW (MSVCRT.@)
600 int CDECL __crtLCMapStringW(LCID lcid, DWORD mapflags, const MSVCRT_wchar_t *src,
601 int srclen, MSVCRT_wchar_t *dst, int dstlen, unsigned int codepage, int xflag)
603 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
604 lcid, mapflags, debugstr_w(src), srclen, dst, dstlen, codepage, xflag);
606 return LCMapStringW(lcid, mapflags, src, srclen, dst, dstlen);
609 /*********************************************************************
610 * __crtCompareStringA (MSVCRT.@)
612 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
613 const char *src2, int len2 )
615 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
616 lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
617 /* FIXME: probably not entirely right */
618 return CompareStringA( lcid, flags, src1, len1, src2, len2 );
621 /*********************************************************************
622 * __crtCompareStringW (MSVCRT.@)
624 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
625 const MSVCRT_wchar_t *src2, int len2 )
627 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
628 lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
629 /* FIXME: probably not entirely right */
630 return CompareStringW( lcid, flags, src1, len1, src2, len2 );
633 /*********************************************************************
634 * __crtGetLocaleInfoW (MSVCRT.@)
636 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
638 FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
639 /* FIXME: probably not entirely right */
640 return GetLocaleInfoW( lcid, type, buffer, len );
643 /*********************************************************************
644 * __crtGetLocaleInfoEx (MSVC110.@)
646 int CDECL __crtGetLocaleInfoEx( const WCHAR *locale, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
648 TRACE("(%s, %x, %p, %d)\n", debugstr_w(locale), type, buffer, len);
649 return GetLocaleInfoEx(locale, type, buffer, len);
652 /*********************************************************************
653 * btowc(MSVCRT.@)
655 MSVCRT_wint_t CDECL MSVCRT_btowc(int c)
657 unsigned char letter = c;
658 MSVCRT_wchar_t ret;
660 if(!MultiByteToWideChar(get_locinfo()->lc_handle[MSVCRT_LC_CTYPE],
661 0, (LPCSTR)&letter, 1, &ret, 1))
662 return 0;
664 return ret;
667 /*********************************************************************
668 * __crtGetStringTypeW(MSVCRT.@)
670 * This function was accepting different number of arguments in older
671 * versions of msvcrt.
673 BOOL CDECL __crtGetStringTypeW(DWORD unk, DWORD type,
674 MSVCRT_wchar_t *buffer, int len, WORD *out)
676 FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
677 unk, type, buffer, len, out);
679 return GetStringTypeW(type, buffer, len, out);
682 /*********************************************************************
683 * localeconv (MSVCRT.@)
685 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
687 return get_locinfo()->lconv;
690 /*********************************************************************
691 * __lconv_init (MSVCRT.@)
693 int CDECL __lconv_init(void)
695 /* this is used to make chars unsigned */
696 charmax = 255;
697 return 0;
700 /*********************************************************************
701 * ___lc_handle_func (MSVCRT.@)
703 LCID* CDECL ___lc_handle_func(void)
705 return get_locinfo()->lc_handle;
708 /*********************************************************************
709 * ___lc_locale_name_func (MSVCR110.@)
711 #if _MSVCR_VER >= 110
712 MSVCRT_wchar_t** CDECL ___lc_locale_name_func(void)
714 return get_locinfo()->lc_name;
716 #endif
718 /*********************************************************************
719 * ___lc_codepage_func (MSVCRT.@)
721 unsigned int CDECL ___lc_codepage_func(void)
723 return get_locinfo()->lc_codepage;
726 /*********************************************************************
727 * ___lc_collate_cp_func (MSVCRT.@)
729 int CDECL ___lc_collate_cp_func(void)
731 return get_locinfo()->lc_collate_cp;
734 /* INTERNAL: frees MSVCRT_pthreadlocinfo struct */
735 void free_locinfo(MSVCRT_pthreadlocinfo locinfo)
737 int i;
739 if(!locinfo)
740 return;
742 if(InterlockedDecrement(&locinfo->refcount))
743 return;
745 for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
746 MSVCRT_free(locinfo->lc_category[i].locale);
747 MSVCRT_free(locinfo->lc_category[i].refcount);
748 #if _MSVCR_VER >= 110
749 MSVCRT_free(locinfo->lc_name[i]);
750 #endif
753 if(locinfo->lconv) {
754 MSVCRT_free(locinfo->lconv->decimal_point);
755 MSVCRT_free(locinfo->lconv->thousands_sep);
756 MSVCRT_free(locinfo->lconv->grouping);
757 MSVCRT_free(locinfo->lconv->int_curr_symbol);
758 MSVCRT_free(locinfo->lconv->currency_symbol);
759 MSVCRT_free(locinfo->lconv->mon_decimal_point);
760 MSVCRT_free(locinfo->lconv->mon_thousands_sep);
761 MSVCRT_free(locinfo->lconv->mon_grouping);
762 MSVCRT_free(locinfo->lconv->positive_sign);
763 MSVCRT_free(locinfo->lconv->negative_sign);
764 #if _MSVCR_VER >= 120
765 MSVCRT_free(locinfo->lconv->_W_decimal_point);
766 MSVCRT_free(locinfo->lconv->_W_thousands_sep);
767 MSVCRT_free(locinfo->lconv->_W_int_curr_symbol);
768 MSVCRT_free(locinfo->lconv->_W_currency_symbol);
769 MSVCRT_free(locinfo->lconv->_W_mon_decimal_point);
770 MSVCRT_free(locinfo->lconv->_W_mon_thousands_sep);
771 MSVCRT_free(locinfo->lconv->_W_positive_sign);
772 MSVCRT_free(locinfo->lconv->_W_negative_sign);
773 #endif
775 MSVCRT_free(locinfo->lconv_intl_refcount);
776 MSVCRT_free(locinfo->lconv_num_refcount);
777 MSVCRT_free(locinfo->lconv_mon_refcount);
778 MSVCRT_free(locinfo->lconv);
780 MSVCRT_free(locinfo->ctype1_refcount);
781 MSVCRT_free(locinfo->ctype1);
783 MSVCRT_free(locinfo->pclmap);
784 MSVCRT_free(locinfo->pcumap);
786 MSVCRT_free(locinfo->lc_time_curr);
788 MSVCRT_free(locinfo);
791 /* INTERNAL: frees MSVCRT_pthreadmbcinfo struct */
792 void free_mbcinfo(MSVCRT_pthreadmbcinfo mbcinfo)
794 if(!mbcinfo)
795 return;
797 if(InterlockedDecrement(&mbcinfo->refcount))
798 return;
800 MSVCRT_free(mbcinfo);
803 /*********************************************************************
804 * _get_current_locale (MSVCRT.@)
806 MSVCRT__locale_t CDECL MSVCRT__get_current_locale(void)
808 MSVCRT__locale_t loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
809 if(!loc)
810 return NULL;
812 loc->locinfo = get_locinfo();
813 loc->mbcinfo = get_mbcinfo();
814 InterlockedIncrement(&loc->locinfo->refcount);
815 InterlockedIncrement(&loc->mbcinfo->refcount);
816 return loc;
819 /*********************************************************************
820 * _free_locale (MSVCRT.@)
822 void CDECL MSVCRT__free_locale(MSVCRT__locale_t locale)
824 if (!locale)
825 return;
827 free_locinfo(locale->locinfo);
828 free_mbcinfo(locale->mbcinfo);
829 MSVCRT_free(locale);
832 #if _MSVCR_VER >= 110
833 static inline BOOL set_lc_locale_name(MSVCRT_pthreadlocinfo locinfo, int cat)
835 LCID lcid = locinfo->lc_handle[cat];
836 WCHAR buf[100];
837 int len;
839 len = GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME
840 |LOCALE_NOUSEROVERRIDE, buf, 100);
841 if(!len) return FALSE;
843 if(LocaleNameToLCID(buf, 0) != lcid)
844 len = LCIDToLocaleName(lcid, buf, 100, 0);
846 if(!len || !(locinfo->lc_name[cat] = MSVCRT_malloc(len*sizeof(MSVCRT_wchar_t))))
847 return FALSE;
849 memcpy(locinfo->lc_name[cat], buf, len*sizeof(MSVCRT_wchar_t));
850 return TRUE;
852 #else
853 static inline BOOL set_lc_locale_name(MSVCRT_pthreadlocinfo locinfo, int cat)
855 return TRUE;
857 #endif
859 static inline BOOL category_needs_update(int cat, int user_cat,
860 MSVCRT_pthreadlocinfo locinfo, LCID lcid, unsigned short cp)
862 if(!locinfo) return TRUE;
863 if(user_cat!=cat && user_cat!=MSVCRT_LC_ALL) return FALSE;
864 return lcid!=locinfo->lc_handle[cat] || cp!=locinfo->lc_id[cat].wCodePage;
867 static MSVCRT_pthreadlocinfo create_locinfo(int category,
868 const char *locale, MSVCRT_pthreadlocinfo old_locinfo)
870 static const DWORD time_data[] = {
871 LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1, LOCALE_SABBREVDAYNAME2,
872 LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4, LOCALE_SABBREVDAYNAME5,
873 LOCALE_SABBREVDAYNAME6,
874 LOCALE_SDAYNAME7, LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3,
875 LOCALE_SDAYNAME4, LOCALE_SDAYNAME5, LOCALE_SDAYNAME6,
876 LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2, LOCALE_SABBREVMONTHNAME3,
877 LOCALE_SABBREVMONTHNAME4, LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
878 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8, LOCALE_SABBREVMONTHNAME9,
879 LOCALE_SABBREVMONTHNAME10, LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12,
880 LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME4,
881 LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8,
882 LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12,
883 LOCALE_S1159, LOCALE_S2359,
884 LOCALE_SSHORTDATE, LOCALE_SLONGDATE,
885 LOCALE_STIMEFORMAT
887 static const char collate[] = "COLLATE=";
888 static const char ctype[] = "CTYPE=";
889 static const char monetary[] = "MONETARY=";
890 static const char numeric[] = "NUMERIC=";
891 static const char time[] = "TIME=";
892 static const char cloc_short_date[] = "MM/dd/yy";
893 static const MSVCRT_wchar_t cloc_short_dateW[] = {'M','M','/','d','d','/','y','y',0};
894 static const char cloc_long_date[] = "dddd, MMMM dd, yyyy";
895 static const MSVCRT_wchar_t cloc_long_dateW[] = {'d','d','d','d',',',' ','M','M','M','M',' ','d','d',',',' ','y','y','y','y',0};
896 static const char cloc_time[] = "HH:mm:ss";
897 static const MSVCRT_wchar_t cloc_timeW[] = {'H','H',':','m','m',':','s','s',0};
899 MSVCRT_pthreadlocinfo locinfo;
900 LCID lcid[6] = { 0 }, lcid_tmp;
901 unsigned short cp[6] = { 0 };
902 char buf[256];
903 #if _MSVCR_VER >= 120
904 MSVCRT_wchar_t wbuf[256];
905 #endif
906 int i, ret, size;
908 TRACE("(%d %s)\n", category, locale);
910 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
911 return NULL;
913 if(locale[0]=='C' && !locale[1]) {
914 lcid[0] = 0;
915 cp[0] = CP_ACP;
916 } else if(!locale[0]) {
917 lcid[0] = GetSystemDefaultLCID();
918 GetLocaleInfoA(lcid[0], LOCALE_IDEFAULTANSICODEPAGE
919 |LOCALE_NOUSEROVERRIDE, buf, sizeof(buf));
920 cp[0] = atoi(buf);
922 for(i=1; i<6; i++) {
923 lcid[i] = lcid[0];
924 cp[i] = cp[0];
926 } else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
927 const char *p;
929 while(1) {
930 locale += 3; /* LC_ */
931 if(!memcmp(locale, collate, sizeof(collate)-1)) {
932 i = MSVCRT_LC_COLLATE;
933 locale += sizeof(collate)-1;
934 } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
935 i = MSVCRT_LC_CTYPE;
936 locale += sizeof(ctype)-1;
937 } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
938 i = MSVCRT_LC_MONETARY;
939 locale += sizeof(monetary)-1;
940 } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
941 i = MSVCRT_LC_NUMERIC;
942 locale += sizeof(numeric)-1;
943 } else if(!memcmp(locale, time, sizeof(time)-1)) {
944 i = MSVCRT_LC_TIME;
945 locale += sizeof(time)-1;
946 } else
947 return NULL;
949 p = strchr(locale, ';');
950 if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0')) {
951 lcid[i] = 0;
952 cp[i] = CP_ACP;
953 } else if(p) {
954 memcpy(buf, locale, p-locale);
955 buf[p-locale] = '\0';
956 lcid[i] = MSVCRT_locale_to_LCID(buf, &cp[i]);
957 } else
958 lcid[i] = MSVCRT_locale_to_LCID(locale, &cp[i]);
960 if(lcid[i] == -1)
961 return NULL;
963 if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
964 break;
966 locale = p+1;
968 } else {
969 lcid[0] = MSVCRT_locale_to_LCID(locale, &cp[0]);
970 if(lcid[0] == -1)
971 return NULL;
973 for(i=1; i<6; i++) {
974 lcid[i] = lcid[0];
975 cp[i] = cp[0];
979 locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
980 if(!locinfo)
981 return NULL;
983 memset(locinfo, 0, sizeof(MSVCRT_threadlocinfo));
984 locinfo->refcount = 1;
986 locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
987 if(!locinfo->lconv) {
988 free_locinfo(locinfo);
989 return NULL;
991 memset(locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
993 locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
994 locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
995 if(!locinfo->pclmap || !locinfo->pcumap) {
996 free_locinfo(locinfo);
997 return NULL;
1000 if(!category_needs_update(MSVCRT_LC_COLLATE, category, old_locinfo,
1001 lcid[MSVCRT_LC_COLLATE], cp[MSVCRT_LC_COLLATE])) {
1002 locinfo->lc_handle[MSVCRT_LC_COLLATE] = old_locinfo->lc_handle[MSVCRT_LC_COLLATE];
1003 locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage = old_locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage;
1004 } else if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
1005 if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE],
1006 cp[MSVCRT_LC_COLLATE], locinfo, MSVCRT_LC_COLLATE)) {
1007 free_locinfo(locinfo);
1008 return NULL;
1011 locinfo->lc_collate_cp = locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage;
1013 if(!set_lc_locale_name(locinfo, MSVCRT_LC_COLLATE)) {
1014 free_locinfo(locinfo);
1015 return NULL;
1017 } else
1018 locinfo->lc_category[MSVCRT_LC_COLLATE].locale = MSVCRT__strdup("C");
1020 if(!category_needs_update(MSVCRT_LC_CTYPE, category, old_locinfo,
1021 lcid[MSVCRT_LC_CTYPE], cp[MSVCRT_LC_CTYPE])) {
1022 locinfo->lc_handle[MSVCRT_LC_CTYPE] = old_locinfo->lc_handle[MSVCRT_LC_CTYPE];
1023 locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage = old_locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
1024 } else if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
1025 CPINFO cp_info;
1026 int j;
1028 if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE],
1029 cp[MSVCRT_LC_CTYPE], locinfo, MSVCRT_LC_CTYPE)) {
1030 free_locinfo(locinfo);
1031 return NULL;
1034 locinfo->lc_codepage = locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
1035 locinfo->lc_clike = 1;
1036 if(!GetCPInfo(locinfo->lc_codepage, &cp_info)) {
1037 free_locinfo(locinfo);
1038 return NULL;
1040 locinfo->mb_cur_max = cp_info.MaxCharSize;
1042 locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
1043 locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
1044 if(!locinfo->ctype1_refcount || !locinfo->ctype1) {
1045 free_locinfo(locinfo);
1046 return NULL;
1049 *locinfo->ctype1_refcount = 1;
1050 locinfo->ctype1[0] = 0;
1051 locinfo->pctype = locinfo->ctype1+1;
1053 buf[1] = buf[2] = '\0';
1054 for(i=1; i<257; i++) {
1055 buf[0] = i-1;
1057 /* builtin GetStringTypeA doesn't set output to 0 on invalid input */
1058 locinfo->ctype1[i] = 0;
1060 GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
1061 1, locinfo->ctype1+i);
1064 for(i=0; cp_info.LeadByte[i+1]!=0; i+=2)
1065 for(j=cp_info.LeadByte[i]; j<=cp_info.LeadByte[i+1]; j++)
1066 locinfo->ctype1[j+1] |= MSVCRT__LEADBYTE;
1068 if(!set_lc_locale_name(locinfo, MSVCRT_LC_CTYPE)) {
1069 free_locinfo(locinfo);
1070 return NULL;
1073 for(i=0; i<256; i++) {
1074 if(locinfo->pctype[i] & MSVCRT__LEADBYTE)
1075 buf[i] = ' ';
1076 else
1077 buf[i] = i;
1080 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
1081 (char*)locinfo->pclmap, 256);
1082 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
1083 (char*)locinfo->pcumap, 256);
1084 } else {
1085 locinfo->lc_clike = 1;
1086 locinfo->mb_cur_max = 1;
1087 locinfo->pctype = MSVCRT__ctype+1;
1088 locinfo->lc_category[MSVCRT_LC_CTYPE].locale = MSVCRT__strdup("C");
1090 for(i=0; i<256; i++) {
1091 if(locinfo->pctype[i] & MSVCRT__LEADBYTE)
1092 buf[i] = ' ';
1093 else
1094 buf[i] = i;
1097 for(i=0; i<256; i++) {
1098 locinfo->pclmap[i] = (i>='A' && i<='Z' ? i-'A'+'a' : i);
1099 locinfo->pcumap[i] = (i>='a' && i<='z' ? i-'a'+'A' : i);
1103 if(!category_needs_update(MSVCRT_LC_MONETARY, category, old_locinfo,
1104 lcid[MSVCRT_LC_MONETARY], cp[MSVCRT_LC_MONETARY])) {
1105 locinfo->lc_handle[MSVCRT_LC_MONETARY] = old_locinfo->lc_handle[MSVCRT_LC_MONETARY];
1106 locinfo->lc_id[MSVCRT_LC_MONETARY].wCodePage = old_locinfo->lc_id[MSVCRT_LC_MONETARY].wCodePage;
1107 } else if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
1108 if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY],
1109 cp[MSVCRT_LC_MONETARY], locinfo, MSVCRT_LC_MONETARY)) {
1110 free_locinfo(locinfo);
1111 return NULL;
1114 locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1115 locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
1116 if(!locinfo->lconv_intl_refcount || !locinfo->lconv_mon_refcount) {
1117 free_locinfo(locinfo);
1118 return NULL;
1121 *locinfo->lconv_intl_refcount = 1;
1122 *locinfo->lconv_mon_refcount = 1;
1124 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
1125 |LOCALE_NOUSEROVERRIDE, buf, 256);
1126 if(i && (locinfo->lconv->int_curr_symbol = MSVCRT_malloc(i)))
1127 memcpy(locinfo->lconv->int_curr_symbol, buf, i);
1128 else {
1129 free_locinfo(locinfo);
1130 return NULL;
1133 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
1134 |LOCALE_NOUSEROVERRIDE, buf, 256);
1135 if(i && (locinfo->lconv->currency_symbol = MSVCRT_malloc(i)))
1136 memcpy(locinfo->lconv->currency_symbol, buf, i);
1137 else {
1138 free_locinfo(locinfo);
1139 return NULL;
1142 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
1143 |LOCALE_NOUSEROVERRIDE, buf, 256);
1144 if(i && (locinfo->lconv->mon_decimal_point = MSVCRT_malloc(i)))
1145 memcpy(locinfo->lconv->mon_decimal_point, buf, i);
1146 else {
1147 free_locinfo(locinfo);
1148 return NULL;
1151 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
1152 |LOCALE_NOUSEROVERRIDE, buf, 256);
1153 if(i && (locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(i)))
1154 memcpy(locinfo->lconv->mon_thousands_sep, buf, i);
1155 else {
1156 free_locinfo(locinfo);
1157 return NULL;
1160 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING
1161 |LOCALE_NOUSEROVERRIDE, buf, 256);
1162 if(i>1)
1163 i = i/2 + (buf[i-2]=='0'?0:1);
1164 if(i && (locinfo->lconv->mon_grouping = MSVCRT_malloc(i))) {
1165 for(i=0; buf[i+1]==';'; i+=2)
1166 locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1167 locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1168 if(buf[i] != '0')
1169 locinfo->lconv->mon_grouping[i/2+1] = 127;
1170 } else {
1171 free_locinfo(locinfo);
1172 return NULL;
1175 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
1176 |LOCALE_NOUSEROVERRIDE, buf, 256);
1177 if(i && (locinfo->lconv->positive_sign = MSVCRT_malloc(i)))
1178 memcpy(locinfo->lconv->positive_sign, buf, i);
1179 else {
1180 free_locinfo(locinfo);
1181 return NULL;
1184 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
1185 |LOCALE_NOUSEROVERRIDE, buf, 256);
1186 if(i && (locinfo->lconv->negative_sign = MSVCRT_malloc(i)))
1187 memcpy(locinfo->lconv->negative_sign, buf, i);
1188 else {
1189 free_locinfo(locinfo);
1190 return NULL;
1193 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS
1194 |LOCALE_NOUSEROVERRIDE, buf, 256))
1195 locinfo->lconv->int_frac_digits = atoi(buf);
1196 else {
1197 free_locinfo(locinfo);
1198 return NULL;
1201 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS
1202 |LOCALE_NOUSEROVERRIDE, buf, 256))
1203 locinfo->lconv->frac_digits = atoi(buf);
1204 else {
1205 free_locinfo(locinfo);
1206 return NULL;
1209 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES
1210 |LOCALE_NOUSEROVERRIDE, buf, 256))
1211 locinfo->lconv->p_cs_precedes = atoi(buf);
1212 else {
1213 free_locinfo(locinfo);
1214 return NULL;
1217 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE
1218 |LOCALE_NOUSEROVERRIDE, buf, 256))
1219 locinfo->lconv->p_sep_by_space = atoi(buf);
1220 else {
1221 free_locinfo(locinfo);
1222 return NULL;
1225 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES
1226 |LOCALE_NOUSEROVERRIDE, buf, 256))
1227 locinfo->lconv->n_cs_precedes = atoi(buf);
1228 else {
1229 free_locinfo(locinfo);
1230 return NULL;
1233 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE
1234 |LOCALE_NOUSEROVERRIDE, buf, 256))
1235 locinfo->lconv->n_sep_by_space = atoi(buf);
1236 else {
1237 free_locinfo(locinfo);
1238 return NULL;
1241 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN
1242 |LOCALE_NOUSEROVERRIDE, buf, 256))
1243 locinfo->lconv->p_sign_posn = atoi(buf);
1244 else {
1245 free_locinfo(locinfo);
1246 return NULL;
1249 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN
1250 |LOCALE_NOUSEROVERRIDE, buf, 256))
1251 locinfo->lconv->n_sign_posn = atoi(buf);
1252 else {
1253 free_locinfo(locinfo);
1254 return NULL;
1257 #if _MSVCR_VER >= 120
1258 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
1259 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1260 if(i && (locinfo->lconv->_W_int_curr_symbol = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1261 memcpy(locinfo->lconv->_W_int_curr_symbol, wbuf, i * sizeof(MSVCRT_wchar_t));
1262 else {
1263 free_locinfo(locinfo);
1264 return NULL;
1267 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
1268 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1269 if(i && (locinfo->lconv->_W_currency_symbol = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1270 memcpy(locinfo->lconv->_W_currency_symbol, wbuf, i * sizeof(MSVCRT_wchar_t));
1271 else {
1272 free_locinfo(locinfo);
1273 return NULL;
1276 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
1277 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1278 if(i && (locinfo->lconv->_W_mon_decimal_point = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1279 memcpy(locinfo->lconv->_W_mon_decimal_point, wbuf, i * sizeof(MSVCRT_wchar_t));
1280 else {
1281 free_locinfo(locinfo);
1282 return NULL;
1285 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
1286 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1287 if(i && (locinfo->lconv->_W_mon_thousands_sep = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1288 memcpy(locinfo->lconv->_W_mon_thousands_sep, wbuf, i * sizeof(MSVCRT_wchar_t));
1289 else {
1290 free_locinfo(locinfo);
1291 return NULL;
1294 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
1295 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1296 if(i && (locinfo->lconv->_W_positive_sign = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1297 memcpy(locinfo->lconv->_W_positive_sign, wbuf, i * sizeof(MSVCRT_wchar_t));
1298 else {
1299 free_locinfo(locinfo);
1300 return NULL;
1303 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
1304 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1305 if(i && (locinfo->lconv->_W_negative_sign = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1306 memcpy(locinfo->lconv->_W_negative_sign, wbuf, i * sizeof(MSVCRT_wchar_t));
1307 else {
1308 free_locinfo(locinfo);
1309 return NULL;
1311 #endif
1313 if(!set_lc_locale_name(locinfo, MSVCRT_LC_MONETARY)) {
1314 free_locinfo(locinfo);
1315 return NULL;
1317 } else {
1318 locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
1319 locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
1320 locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
1321 locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
1322 locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
1323 locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
1324 locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
1326 if(!locinfo->lconv->int_curr_symbol || !locinfo->lconv->currency_symbol
1327 || !locinfo->lconv->mon_decimal_point || !locinfo->lconv->mon_thousands_sep
1328 || !locinfo->lconv->mon_grouping || !locinfo->lconv->positive_sign
1329 || !locinfo->lconv->negative_sign) {
1330 free_locinfo(locinfo);
1331 return NULL;
1334 locinfo->lconv->int_curr_symbol[0] = '\0';
1335 locinfo->lconv->currency_symbol[0] = '\0';
1336 locinfo->lconv->mon_decimal_point[0] = '\0';
1337 locinfo->lconv->mon_thousands_sep[0] = '\0';
1338 locinfo->lconv->mon_grouping[0] = '\0';
1339 locinfo->lconv->positive_sign[0] = '\0';
1340 locinfo->lconv->negative_sign[0] = '\0';
1341 locinfo->lconv->int_frac_digits = 127;
1342 locinfo->lconv->frac_digits = 127;
1343 locinfo->lconv->p_cs_precedes = 127;
1344 locinfo->lconv->p_sep_by_space = 127;
1345 locinfo->lconv->n_cs_precedes = 127;
1346 locinfo->lconv->n_sep_by_space = 127;
1347 locinfo->lconv->p_sign_posn = 127;
1348 locinfo->lconv->n_sign_posn = 127;
1350 #if _MSVCR_VER >= 120
1351 locinfo->lconv->_W_int_curr_symbol = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1352 locinfo->lconv->_W_currency_symbol = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1353 locinfo->lconv->_W_mon_decimal_point = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1354 locinfo->lconv->_W_mon_thousands_sep = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1355 locinfo->lconv->_W_positive_sign = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1356 locinfo->lconv->_W_negative_sign = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1358 if(!locinfo->lconv->_W_int_curr_symbol || !locinfo->lconv->_W_currency_symbol
1359 || !locinfo->lconv->_W_mon_decimal_point || !locinfo->lconv->_W_mon_thousands_sep
1360 || !locinfo->lconv->positive_sign || !locinfo->lconv->negative_sign) {
1361 free_locinfo(locinfo);
1362 return NULL;
1365 locinfo->lconv->_W_int_curr_symbol[0] = '\0';
1366 locinfo->lconv->_W_currency_symbol[0] = '\0';
1367 locinfo->lconv->_W_mon_decimal_point[0] = '\0';
1368 locinfo->lconv->_W_mon_thousands_sep[0] = '\0';
1369 locinfo->lconv->_W_positive_sign[0] = '\0';
1370 locinfo->lconv->_W_negative_sign[0] = '\0';
1371 #endif
1373 locinfo->lc_category[MSVCRT_LC_MONETARY].locale = MSVCRT__strdup("C");
1376 if(!category_needs_update(MSVCRT_LC_NUMERIC, category, old_locinfo,
1377 lcid[MSVCRT_LC_NUMERIC], cp[MSVCRT_LC_NUMERIC])) {
1378 locinfo->lc_handle[MSVCRT_LC_NUMERIC] = old_locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1379 locinfo->lc_id[MSVCRT_LC_NUMERIC].wCodePage = old_locinfo->lc_id[MSVCRT_LC_NUMERIC].wCodePage;
1380 } else if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
1381 if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC],
1382 cp[MSVCRT_LC_NUMERIC], locinfo, MSVCRT_LC_NUMERIC)) {
1383 free_locinfo(locinfo);
1384 return NULL;
1387 if(!locinfo->lconv_intl_refcount)
1388 locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1389 locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1390 if(!locinfo->lconv_intl_refcount || !locinfo->lconv_num_refcount) {
1391 free_locinfo(locinfo);
1392 return NULL;
1395 *locinfo->lconv_intl_refcount = 1;
1396 *locinfo->lconv_num_refcount = 1;
1398 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1399 |LOCALE_NOUSEROVERRIDE, buf, 256);
1400 if(i && (locinfo->lconv->decimal_point = MSVCRT_malloc(i)))
1401 memcpy(locinfo->lconv->decimal_point, buf, i);
1402 else {
1403 free_locinfo(locinfo);
1404 return NULL;
1407 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1408 |LOCALE_NOUSEROVERRIDE, buf, 256);
1409 if(i && (locinfo->lconv->thousands_sep = MSVCRT_malloc(i)))
1410 memcpy(locinfo->lconv->thousands_sep, buf, i);
1411 else {
1412 free_locinfo(locinfo);
1413 return NULL;
1416 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING
1417 |LOCALE_NOUSEROVERRIDE, buf, 256);
1418 if(i>1)
1419 i = i/2 + (buf[i-2]=='0'?0:1);
1420 if(i && (locinfo->lconv->grouping = MSVCRT_malloc(i))) {
1421 for(i=0; buf[i+1]==';'; i+=2)
1422 locinfo->lconv->grouping[i/2] = buf[i]-'0';
1423 locinfo->lconv->grouping[i/2] = buf[i]-'0';
1424 if(buf[i] != '0')
1425 locinfo->lconv->grouping[i/2+1] = 127;
1426 } else {
1427 free_locinfo(locinfo);
1428 return NULL;
1431 #if _MSVCR_VER >= 120
1432 i = GetLocaleInfoW(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1433 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1434 if(i && (locinfo->lconv->_W_decimal_point = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1435 memcpy(locinfo->lconv->_W_decimal_point, wbuf, i * sizeof(MSVCRT_wchar_t));
1436 else {
1437 free_locinfo(locinfo);
1438 return NULL;
1441 i = GetLocaleInfoW(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1442 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1443 if(i && (locinfo->lconv->_W_thousands_sep = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1444 memcpy(locinfo->lconv->_W_thousands_sep, wbuf, i * sizeof(MSVCRT_wchar_t));
1445 else {
1446 free_locinfo(locinfo);
1447 return NULL;
1449 #endif
1451 if(!set_lc_locale_name(locinfo, MSVCRT_LC_NUMERIC)) {
1452 free_locinfo(locinfo);
1453 return NULL;
1455 } else {
1456 locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1457 locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1458 locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1459 if(!locinfo->lconv->decimal_point || !locinfo->lconv->thousands_sep
1460 || !locinfo->lconv->grouping) {
1461 free_locinfo(locinfo);
1462 return NULL;
1465 locinfo->lconv->decimal_point[0] = '.';
1466 locinfo->lconv->decimal_point[1] = '\0';
1467 locinfo->lconv->thousands_sep[0] = '\0';
1468 locinfo->lconv->grouping[0] = '\0';
1470 #if _MSVCR_VER >= 120
1471 locinfo->lconv->_W_decimal_point = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[2]));
1472 locinfo->lconv->_W_thousands_sep = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1474 if(!locinfo->lconv->_W_decimal_point || !locinfo->lconv->_W_thousands_sep) {
1475 free_locinfo(locinfo);
1476 return NULL;
1479 locinfo->lconv->_W_decimal_point[0] = '.';
1480 locinfo->lconv->_W_decimal_point[1] = '\0';
1481 locinfo->lconv->_W_thousands_sep[0] = '\0';
1482 #endif
1484 locinfo->lc_category[MSVCRT_LC_NUMERIC].locale = MSVCRT__strdup("C");
1487 if(!category_needs_update(MSVCRT_LC_TIME, category, old_locinfo,
1488 lcid[MSVCRT_LC_TIME], cp[MSVCRT_LC_TIME])) {
1489 locinfo->lc_handle[MSVCRT_LC_TIME] = old_locinfo->lc_handle[MSVCRT_LC_TIME];
1490 locinfo->lc_id[MSVCRT_LC_TIME].wCodePage = old_locinfo->lc_id[MSVCRT_LC_TIME].wCodePage;
1491 } else {
1492 if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1493 if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME],
1494 cp[MSVCRT_LC_TIME], locinfo, MSVCRT_LC_TIME)) {
1495 free_locinfo(locinfo);
1496 return NULL;
1499 if(!set_lc_locale_name(locinfo, MSVCRT_LC_TIME)) {
1500 free_locinfo(locinfo);
1501 return NULL;
1503 } else
1504 locinfo->lc_category[MSVCRT_LC_TIME].locale = MSVCRT__strdup("C");
1506 size = sizeof(MSVCRT___lc_time_data);
1507 lcid_tmp = lcid[MSVCRT_LC_TIME] ? lcid[MSVCRT_LC_TIME] : MAKELCID(LANG_ENGLISH, SORT_DEFAULT);
1508 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1509 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1510 size += sizeof(cloc_short_date) + sizeof(cloc_short_dateW);
1511 }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1512 size += sizeof(cloc_long_date) + sizeof(cloc_long_dateW);
1513 }else {
1514 ret = GetLocaleInfoA(lcid_tmp, time_data[i]
1515 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1516 if(!ret) {
1517 free_locinfo(locinfo);
1518 return NULL;
1520 size += ret;
1522 ret = GetLocaleInfoW(lcid_tmp, time_data[i]
1523 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1524 if(!ret) {
1525 free_locinfo(locinfo);
1526 return NULL;
1528 size += ret*sizeof(MSVCRT_wchar_t);
1532 locinfo->lc_time_curr = MSVCRT_malloc(size);
1533 if(!locinfo->lc_time_curr) {
1534 free_locinfo(locinfo);
1535 return NULL;
1538 ret = 0;
1539 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1540 locinfo->lc_time_curr->str.str[i] = &locinfo->lc_time_curr->data[ret];
1541 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1542 memcpy(&locinfo->lc_time_curr->data[ret], cloc_short_date, sizeof(cloc_short_date));
1543 ret += sizeof(cloc_short_date);
1544 }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1545 memcpy(&locinfo->lc_time_curr->data[ret], cloc_long_date, sizeof(cloc_long_date));
1546 ret += sizeof(cloc_long_date);
1547 }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1548 memcpy(&locinfo->lc_time_curr->data[ret], cloc_time, sizeof(cloc_time));
1549 ret += sizeof(cloc_time);
1550 }else {
1551 ret += GetLocaleInfoA(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1552 &locinfo->lc_time_curr->data[ret], size-ret);
1555 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1556 locinfo->lc_time_curr->wstr.wstr[i] = (MSVCRT_wchar_t*)&locinfo->lc_time_curr->data[ret];
1557 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1558 memcpy(&locinfo->lc_time_curr->data[ret], cloc_short_dateW, sizeof(cloc_short_dateW));
1559 ret += sizeof(cloc_short_dateW);
1560 }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1561 memcpy(&locinfo->lc_time_curr->data[ret], cloc_long_dateW, sizeof(cloc_long_dateW));
1562 ret += sizeof(cloc_long_dateW);
1563 }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1564 memcpy(&locinfo->lc_time_curr->data[ret], cloc_timeW, sizeof(cloc_timeW));
1565 ret += sizeof(cloc_timeW);
1566 }else {
1567 ret += GetLocaleInfoW(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1568 (MSVCRT_wchar_t*)&locinfo->lc_time_curr->data[ret], size-ret)*sizeof(MSVCRT_wchar_t);
1571 locinfo->lc_time_curr->lcid = lcid[MSVCRT_LC_TIME];
1574 return locinfo;
1577 /*********************************************************************
1578 * _create_locale (MSVCRT.@)
1580 MSVCRT__locale_t CDECL MSVCRT__create_locale(int category, const char *locale)
1582 MSVCRT__locale_t loc;
1584 loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
1585 if(!loc)
1586 return NULL;
1588 loc->locinfo = create_locinfo(category, locale, NULL);
1589 if(!loc->locinfo) {
1590 MSVCRT_free(loc);
1591 return NULL;
1594 loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
1595 if(!loc->mbcinfo) {
1596 free_locinfo(loc->locinfo);
1597 MSVCRT_free(loc);
1598 return NULL;
1601 loc->mbcinfo->refcount = 1;
1602 _setmbcp_l(loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage,
1603 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE], loc->mbcinfo);
1604 return loc;
1607 /*********************************************************************
1608 * setlocale (MSVCRT.@)
1610 char* CDECL MSVCRT_setlocale(int category, const char* locale)
1612 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
1613 MSVCRT_pthreadlocinfo newlocinfo;
1615 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1616 return NULL;
1618 if(!locale) {
1619 if(category == MSVCRT_LC_ALL)
1620 return construct_lc_all(locinfo);
1622 return locinfo->lc_category[category].locale;
1625 newlocinfo = create_locinfo(category, locale, locinfo);
1626 if(!newlocinfo) {
1627 WARN("%d %s failed\n", category, locale);
1628 return NULL;
1631 LOCK_LOCALE;
1633 if(locinfo->lc_handle[MSVCRT_LC_COLLATE]!=newlocinfo->lc_handle[MSVCRT_LC_COLLATE]
1634 || locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage!=newlocinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage) {
1635 locinfo->lc_collate_cp = newlocinfo->lc_collate_cp;
1636 locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1637 newlocinfo->lc_handle[MSVCRT_LC_COLLATE];
1638 locinfo->lc_id[MSVCRT_LC_COLLATE] =
1639 newlocinfo->lc_id[MSVCRT_LC_COLLATE];
1640 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1641 (void**)&newlocinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1642 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1643 (void**)&newlocinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1645 #if _MSVCR_VER >= 110
1646 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_COLLATE],
1647 (void**)&newlocinfo->lc_name[MSVCRT_LC_COLLATE]);
1648 #endif
1651 if(locinfo->lc_handle[MSVCRT_LC_CTYPE]!=newlocinfo->lc_handle[MSVCRT_LC_CTYPE]
1652 || locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage!=newlocinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage) {
1653 locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1654 newlocinfo->lc_handle[MSVCRT_LC_CTYPE];
1655 locinfo->lc_id[MSVCRT_LC_CTYPE] =
1656 newlocinfo->lc_id[MSVCRT_LC_CTYPE];
1657 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1658 (void**)&newlocinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1659 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1660 (void**)&newlocinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1662 locinfo->lc_codepage = newlocinfo->lc_codepage;
1663 locinfo->lc_clike = newlocinfo->lc_clike;
1664 locinfo->mb_cur_max = newlocinfo->mb_cur_max;
1666 swap_pointers((void**)&locinfo->ctype1_refcount,
1667 (void**)&newlocinfo->ctype1_refcount);
1668 swap_pointers((void**)&locinfo->ctype1, (void**)&newlocinfo->ctype1);
1669 swap_pointers((void**)&locinfo->pctype, (void**)&newlocinfo->pctype);
1670 swap_pointers((void**)&locinfo->pclmap, (void**)&newlocinfo->pclmap);
1671 swap_pointers((void**)&locinfo->pcumap, (void**)&newlocinfo->pcumap);
1673 #if _MSVCR_VER >= 110
1674 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_CTYPE],
1675 (void**)&newlocinfo->lc_name[MSVCRT_LC_CTYPE]);
1676 #endif
1679 if(locinfo->lc_handle[MSVCRT_LC_MONETARY]!=newlocinfo->lc_handle[MSVCRT_LC_MONETARY]
1680 || locinfo->lc_id[MSVCRT_LC_MONETARY].wCodePage!=newlocinfo->lc_id[MSVCRT_LC_MONETARY].wCodePage) {
1681 locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1682 newlocinfo->lc_handle[MSVCRT_LC_MONETARY];
1683 locinfo->lc_id[MSVCRT_LC_MONETARY] =
1684 newlocinfo->lc_id[MSVCRT_LC_MONETARY];
1685 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1686 (void**)&newlocinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1687 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1688 (void**)&newlocinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1690 swap_pointers((void**)&locinfo->lconv->int_curr_symbol,
1691 (void**)&newlocinfo->lconv->int_curr_symbol);
1692 swap_pointers((void**)&locinfo->lconv->currency_symbol,
1693 (void**)&newlocinfo->lconv->currency_symbol);
1694 swap_pointers((void**)&locinfo->lconv->mon_decimal_point,
1695 (void**)&newlocinfo->lconv->mon_decimal_point);
1696 swap_pointers((void**)&locinfo->lconv->mon_thousands_sep,
1697 (void**)&newlocinfo->lconv->mon_thousands_sep);
1698 swap_pointers((void**)&locinfo->lconv->mon_grouping,
1699 (void**)&newlocinfo->lconv->mon_grouping);
1700 swap_pointers((void**)&locinfo->lconv->positive_sign,
1701 (void**)&newlocinfo->lconv->positive_sign);
1702 swap_pointers((void**)&locinfo->lconv->negative_sign,
1703 (void**)&newlocinfo->lconv->negative_sign);
1705 #if _MSVCR_VER >= 120
1706 swap_pointers((void**)&locinfo->lconv->_W_int_curr_symbol,
1707 (void**)&newlocinfo->lconv->_W_int_curr_symbol);
1708 swap_pointers((void**)&locinfo->lconv->_W_currency_symbol,
1709 (void**)&newlocinfo->lconv->_W_currency_symbol);
1710 swap_pointers((void**)&locinfo->lconv->_W_mon_decimal_point,
1711 (void**)&newlocinfo->lconv->_W_mon_decimal_point);
1712 swap_pointers((void**)&locinfo->lconv->_W_mon_thousands_sep,
1713 (void**)&newlocinfo->lconv->_W_mon_thousands_sep);
1714 swap_pointers((void**)&locinfo->lconv->_W_positive_sign,
1715 (void**)&newlocinfo->lconv->_W_positive_sign);
1716 swap_pointers((void**)&locinfo->lconv->_W_negative_sign,
1717 (void**)&newlocinfo->lconv->_W_negative_sign);
1718 #endif
1720 locinfo->lconv->int_frac_digits = newlocinfo->lconv->int_frac_digits;
1721 locinfo->lconv->frac_digits = newlocinfo->lconv->frac_digits;
1722 locinfo->lconv->p_cs_precedes = newlocinfo->lconv->p_cs_precedes;
1723 locinfo->lconv->p_sep_by_space = newlocinfo->lconv->p_sep_by_space;
1724 locinfo->lconv->n_cs_precedes = newlocinfo->lconv->n_cs_precedes;
1725 locinfo->lconv->n_sep_by_space = newlocinfo->lconv->n_sep_by_space;
1726 locinfo->lconv->p_sign_posn = newlocinfo->lconv->p_sign_posn;
1727 locinfo->lconv->n_sign_posn = newlocinfo->lconv->n_sign_posn;
1729 #if _MSVCR_VER >= 110
1730 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_MONETARY],
1731 (void**)&newlocinfo->lc_name[MSVCRT_LC_MONETARY]);
1732 #endif
1735 if(locinfo->lc_handle[MSVCRT_LC_NUMERIC]!=newlocinfo->lc_handle[MSVCRT_LC_NUMERIC]
1736 || locinfo->lc_id[MSVCRT_LC_NUMERIC].wCodePage!=newlocinfo->lc_id[MSVCRT_LC_NUMERIC].wCodePage) {
1737 locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1738 newlocinfo->lc_handle[MSVCRT_LC_NUMERIC];
1739 locinfo->lc_id[MSVCRT_LC_NUMERIC] =
1740 newlocinfo->lc_id[MSVCRT_LC_NUMERIC];
1741 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1742 (void**)&newlocinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1743 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1744 (void**)&newlocinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1746 swap_pointers((void**)&locinfo->lconv->decimal_point,
1747 (void**)&newlocinfo->lconv->decimal_point);
1748 swap_pointers((void**)&locinfo->lconv->thousands_sep,
1749 (void**)&newlocinfo->lconv->thousands_sep);
1750 swap_pointers((void**)&locinfo->lconv->grouping,
1751 (void**)&newlocinfo->lconv->grouping);
1753 #if _MSVCR_VER >= 120
1754 swap_pointers((void**)&locinfo->lconv->_W_decimal_point,
1755 (void**)&newlocinfo->lconv->_W_decimal_point);
1756 swap_pointers((void**)&locinfo->lconv->_W_thousands_sep,
1757 (void**)&newlocinfo->lconv->_W_thousands_sep);
1758 #endif
1760 #if _MSVCR_VER >= 110
1761 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_NUMERIC],
1762 (void**)&newlocinfo->lc_name[MSVCRT_LC_NUMERIC]);
1763 #endif
1766 if(locinfo->lc_handle[MSVCRT_LC_TIME]!=newlocinfo->lc_handle[MSVCRT_LC_TIME]
1767 || locinfo->lc_id[MSVCRT_LC_TIME].wCodePage!=newlocinfo->lc_id[MSVCRT_LC_TIME].wCodePage) {
1768 locinfo->lc_handle[MSVCRT_LC_TIME] =
1769 newlocinfo->lc_handle[MSVCRT_LC_TIME];
1770 locinfo->lc_id[MSVCRT_LC_TIME] =
1771 newlocinfo->lc_id[MSVCRT_LC_TIME];
1772 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].locale,
1773 (void**)&newlocinfo->lc_category[MSVCRT_LC_TIME].locale);
1774 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1775 (void**)&newlocinfo->lc_category[MSVCRT_LC_TIME].refcount);
1776 swap_pointers((void**)&locinfo->lc_time_curr,
1777 (void**)&newlocinfo->lc_time_curr);
1779 #if _MSVCR_VER >= 110
1780 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_TIME],
1781 (void**)&newlocinfo->lc_name[MSVCRT_LC_TIME]);
1782 #endif
1785 free_locinfo(newlocinfo);
1786 UNLOCK_LOCALE;
1788 if(locinfo == MSVCRT_locale->locinfo) {
1789 int i;
1791 MSVCRT___lc_codepage = locinfo->lc_codepage;
1792 MSVCRT___lc_collate_cp = locinfo->lc_collate_cp;
1793 MSVCRT___mb_cur_max = locinfo->mb_cur_max;
1794 MSVCRT__pctype = locinfo->pctype;
1795 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1796 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1799 if(category == MSVCRT_LC_ALL)
1800 return construct_lc_all(locinfo);
1802 return locinfo->lc_category[category].locale;
1805 /*********************************************************************
1806 * _wsetlocale (MSVCRT.@)
1808 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* wlocale)
1810 static MSVCRT_wchar_t current_lc_all[MAX_LOCALE_LENGTH];
1812 char *locale = NULL;
1813 const char *ret;
1814 MSVCRT_size_t len;
1816 if(wlocale) {
1817 len = MSVCRT_wcstombs(NULL, wlocale, 0);
1818 if(len == -1)
1819 return NULL;
1821 locale = MSVCRT_malloc(++len);
1822 if(!locale)
1823 return NULL;
1825 MSVCRT_wcstombs(locale, wlocale, len);
1828 LOCK_LOCALE;
1829 ret = MSVCRT_setlocale(category, locale);
1830 MSVCRT_free(locale);
1832 if(ret && MSVCRT_mbstowcs(current_lc_all, ret, MAX_LOCALE_LENGTH)==-1)
1833 ret = NULL;
1835 UNLOCK_LOCALE;
1836 return ret ? current_lc_all : NULL;
1839 /*********************************************************************
1840 * _configthreadlocale (MSVCR80.@)
1842 int CDECL _configthreadlocale(int type)
1844 thread_data_t *data = msvcrt_get_thread_data();
1845 MSVCRT__locale_t locale;
1846 int ret;
1848 if(!data)
1849 return -1;
1851 ret = (data->have_locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1853 if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1854 if(!data->have_locale) {
1855 /* Copy current global locale */
1856 locale = MSVCRT__create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
1857 if(!locale)
1858 return -1;
1860 data->locinfo = locale->locinfo;
1861 data->mbcinfo = locale->mbcinfo;
1862 data->have_locale = TRUE;
1863 MSVCRT_free(locale);
1866 return ret;
1869 if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1870 if(data->have_locale) {
1871 free_locinfo(data->locinfo);
1872 free_mbcinfo(data->mbcinfo);
1873 data->locinfo = MSVCRT_locale->locinfo;
1874 data->mbcinfo = MSVCRT_locale->mbcinfo;
1875 data->have_locale = FALSE;
1878 return ret;
1881 if(!type)
1882 return ret;
1884 return -1;
1887 BOOL msvcrt_init_locale(void)
1889 int i;
1891 LOCK_LOCALE;
1892 MSVCRT_locale = MSVCRT__create_locale(0, "C");
1893 UNLOCK_LOCALE;
1894 if(!MSVCRT_locale)
1895 return FALSE;
1897 MSVCRT___lc_codepage = MSVCRT_locale->locinfo->lc_codepage;
1898 MSVCRT___lc_collate_cp = MSVCRT_locale->locinfo->lc_collate_cp;
1899 MSVCRT___mb_cur_max = MSVCRT_locale->locinfo->mb_cur_max;
1900 MSVCRT__pctype = MSVCRT_locale->locinfo->pctype;
1901 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1902 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1903 _setmbcp(_MB_CP_ANSI);
1904 return TRUE;