msvcrt: Add _wctime32_s implementation.
[wine/multimedia.git] / dlls / msvcrt / locale.c
blobad06b1ae8adb9ec03a7c9b89ca884f728480adc4
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__locale_t loc, int category)
315 char buf[256], *p;
316 int len;
318 if(GetLocaleInfoA(lcid, LOCALE_ILANGUAGE|LOCALE_NOUSEROVERRIDE, buf, 256)) {
319 p = buf;
321 loc->locinfo->lc_id[category].wLanguage = 0;
322 while(*p) {
323 loc->locinfo->lc_id[category].wLanguage *= 16;
325 if(*p <= '9')
326 loc->locinfo->lc_id[category].wLanguage += *p-'0';
327 else
328 loc->locinfo->lc_id[category].wLanguage += *p-'a'+10;
330 p++;
333 loc->locinfo->lc_id[category].wCountry =
334 loc->locinfo->lc_id[category].wLanguage;
337 loc->locinfo->lc_id[category].wCodePage = cp;
339 loc->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 loc->locinfo->lc_category[category].locale = MSVCRT_malloc(len);
352 loc->locinfo->lc_category[category].refcount = MSVCRT_malloc(sizeof(int));
353 if(!loc->locinfo->lc_category[category].locale
354 || !loc->locinfo->lc_category[category].refcount) {
355 MSVCRT_free(loc->locinfo->lc_category[category].locale);
356 MSVCRT_free(loc->locinfo->lc_category[category].refcount);
357 loc->locinfo->lc_category[category].locale = NULL;
358 loc->locinfo->lc_category[category].refcount = NULL;
359 return TRUE;
361 memcpy(loc->locinfo->lc_category[category].locale, buf, len);
362 *loc->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__locale_t loc, int cat)
835 LCID lcid = loc->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 || !(loc->locinfo->lc_name[cat] = MSVCRT_malloc(len*sizeof(MSVCRT_wchar_t))))
847 return FALSE;
849 memcpy(loc->locinfo->lc_name[cat], buf, len*sizeof(MSVCRT_wchar_t));
850 return TRUE;
852 #else
853 static inline BOOL set_lc_locale_name(MSVCRT__locale_t loc, int cat)
855 return TRUE;
857 #endif
859 /*********************************************************************
860 * _create_locale (MSVCRT.@)
862 MSVCRT__locale_t CDECL MSVCRT__create_locale(int category, const char *locale)
864 static const DWORD time_data[] = {
865 LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1, LOCALE_SABBREVDAYNAME2,
866 LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4, LOCALE_SABBREVDAYNAME5,
867 LOCALE_SABBREVDAYNAME6,
868 LOCALE_SDAYNAME7, LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3,
869 LOCALE_SDAYNAME4, LOCALE_SDAYNAME5, LOCALE_SDAYNAME6,
870 LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2, LOCALE_SABBREVMONTHNAME3,
871 LOCALE_SABBREVMONTHNAME4, LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
872 LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8, LOCALE_SABBREVMONTHNAME9,
873 LOCALE_SABBREVMONTHNAME10, LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12,
874 LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME4,
875 LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8,
876 LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12,
877 LOCALE_S1159, LOCALE_S2359,
878 LOCALE_SSHORTDATE, LOCALE_SLONGDATE,
879 LOCALE_STIMEFORMAT
881 static const char collate[] = "COLLATE=";
882 static const char ctype[] = "CTYPE=";
883 static const char monetary[] = "MONETARY=";
884 static const char numeric[] = "NUMERIC=";
885 static const char time[] = "TIME=";
886 static const char cloc_short_date[] = "MM/dd/yy";
887 static const MSVCRT_wchar_t cloc_short_dateW[] = {'M','M','/','d','d','/','y','y',0};
888 static const char cloc_long_date[] = "dddd, MMMM dd, yyyy";
889 static const MSVCRT_wchar_t cloc_long_dateW[] = {'d','d','d','d',',',' ','M','M','M','M',' ','d','d',',',' ','y','y','y','y',0};
890 static const char cloc_time[] = "HH:mm:ss";
891 static const MSVCRT_wchar_t cloc_timeW[] = {'H','H',':','m','m',':','s','s',0};
893 MSVCRT__locale_t loc;
894 LCID lcid[6] = { 0 }, lcid_tmp;
895 unsigned short cp[6] = { 0 };
896 char buf[256];
897 #if _MSVCR_VER >= 120
898 MSVCRT_wchar_t wbuf[256];
899 #endif
900 int i, ret, size;
902 TRACE("(%d %s)\n", category, locale);
904 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
905 return NULL;
907 if(locale[0]=='C' && !locale[1]) {
908 lcid[0] = 0;
909 cp[0] = CP_ACP;
910 } else if(!locale[0]) {
911 lcid[0] = GetSystemDefaultLCID();
912 GetLocaleInfoA(lcid[0], LOCALE_IDEFAULTANSICODEPAGE
913 |LOCALE_NOUSEROVERRIDE, buf, sizeof(buf));
914 cp[0] = atoi(buf);
916 for(i=1; i<6; i++) {
917 lcid[i] = lcid[0];
918 cp[i] = cp[0];
920 } else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
921 const char *p;
923 while(1) {
924 locale += 3; /* LC_ */
925 if(!memcmp(locale, collate, sizeof(collate)-1)) {
926 i = MSVCRT_LC_COLLATE;
927 locale += sizeof(collate)-1;
928 } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
929 i = MSVCRT_LC_CTYPE;
930 locale += sizeof(ctype)-1;
931 } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
932 i = MSVCRT_LC_MONETARY;
933 locale += sizeof(monetary)-1;
934 } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
935 i = MSVCRT_LC_NUMERIC;
936 locale += sizeof(numeric)-1;
937 } else if(!memcmp(locale, time, sizeof(time)-1)) {
938 i = MSVCRT_LC_TIME;
939 locale += sizeof(time)-1;
940 } else
941 return NULL;
943 p = strchr(locale, ';');
944 if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0')) {
945 lcid[i] = 0;
946 cp[i] = CP_ACP;
947 } else if(p) {
948 memcpy(buf, locale, p-locale);
949 buf[p-locale] = '\0';
950 lcid[i] = MSVCRT_locale_to_LCID(buf, &cp[i]);
951 } else
952 lcid[i] = MSVCRT_locale_to_LCID(locale, &cp[i]);
954 if(lcid[i] == -1)
955 return NULL;
957 if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
958 break;
960 locale = p+1;
962 } else {
963 lcid[0] = MSVCRT_locale_to_LCID(locale, &cp[0]);
964 if(lcid[0] == -1)
965 return NULL;
967 for(i=1; i<6; i++) {
968 lcid[i] = lcid[0];
969 cp[i] = cp[0];
973 loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
974 if(!loc)
975 return NULL;
977 loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
978 if(!loc->locinfo) {
979 MSVCRT_free(loc);
980 return NULL;
983 loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
984 if(!loc->mbcinfo) {
985 MSVCRT_free(loc->locinfo);
986 MSVCRT_free(loc);
987 return NULL;
990 memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
991 loc->locinfo->refcount = 1;
992 loc->mbcinfo->refcount = 1;
994 loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
995 if(!loc->locinfo->lconv) {
996 MSVCRT__free_locale(loc);
997 return NULL;
999 memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
1001 loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
1002 loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
1003 if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
1004 MSVCRT__free_locale(loc);
1005 return NULL;
1008 if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
1009 if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE], cp[MSVCRT_LC_COLLATE], loc, MSVCRT_LC_COLLATE)) {
1010 MSVCRT__free_locale(loc);
1011 return NULL;
1014 loc->locinfo->lc_collate_cp = loc->locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage;
1016 if(!set_lc_locale_name(loc, MSVCRT_LC_COLLATE)) {
1017 MSVCRT__free_locale(loc);
1018 return NULL;
1020 } else
1021 loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale = MSVCRT__strdup("C");
1023 if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
1024 CPINFO cp_info;
1025 int j;
1027 if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE], cp[MSVCRT_LC_CTYPE], loc, MSVCRT_LC_CTYPE)) {
1028 MSVCRT__free_locale(loc);
1029 return NULL;
1032 loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
1033 loc->locinfo->lc_clike = 1;
1034 if(!GetCPInfo(loc->locinfo->lc_codepage, &cp_info)) {
1035 MSVCRT__free_locale(loc);
1036 return NULL;
1038 loc->locinfo->mb_cur_max = cp_info.MaxCharSize;
1040 loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
1041 loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
1042 if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
1043 MSVCRT__free_locale(loc);
1044 return NULL;
1047 *loc->locinfo->ctype1_refcount = 1;
1048 loc->locinfo->ctype1[0] = 0;
1049 loc->locinfo->pctype = loc->locinfo->ctype1+1;
1051 buf[1] = buf[2] = '\0';
1052 for(i=1; i<257; i++) {
1053 buf[0] = i-1;
1055 /* builtin GetStringTypeA doesn't set output to 0 on invalid input */
1056 loc->locinfo->ctype1[i] = 0;
1058 GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
1059 1, loc->locinfo->ctype1+i);
1062 for(i=0; cp_info.LeadByte[i+1]!=0; i+=2)
1063 for(j=cp_info.LeadByte[i]; j<=cp_info.LeadByte[i+1]; j++)
1064 loc->locinfo->ctype1[j+1] |= MSVCRT__LEADBYTE;
1066 if(!set_lc_locale_name(loc, MSVCRT_LC_CTYPE)) {
1067 MSVCRT__free_locale(loc);
1068 return NULL;
1070 } else {
1071 loc->locinfo->lc_clike = 1;
1072 loc->locinfo->mb_cur_max = 1;
1073 loc->locinfo->pctype = MSVCRT__ctype+1;
1074 loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale = MSVCRT__strdup("C");
1077 for(i=0; i<256; i++) {
1078 if(loc->locinfo->pctype[i] & MSVCRT__LEADBYTE)
1079 buf[i] = ' ';
1080 else
1081 buf[i] = i;
1085 if(lcid[MSVCRT_LC_CTYPE]) {
1086 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
1087 (char*)loc->locinfo->pclmap, 256);
1088 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
1089 (char*)loc->locinfo->pcumap, 256);
1090 } else {
1091 for(i=0; i<256; i++) {
1092 loc->locinfo->pclmap[i] = (i>='A' && i<='Z' ? i-'A'+'a' : i);
1093 loc->locinfo->pcumap[i] = (i>='a' && i<='z' ? i-'a'+'A' : i);
1097 _setmbcp_l(loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage, lcid[MSVCRT_LC_CTYPE], loc->mbcinfo);
1099 if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
1100 if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY], cp[MSVCRT_LC_MONETARY], loc, MSVCRT_LC_MONETARY)) {
1101 MSVCRT__free_locale(loc);
1102 return NULL;
1105 loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1106 loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
1107 if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
1108 MSVCRT__free_locale(loc);
1109 return NULL;
1112 *loc->locinfo->lconv_intl_refcount = 1;
1113 *loc->locinfo->lconv_mon_refcount = 1;
1115 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
1116 |LOCALE_NOUSEROVERRIDE, buf, 256);
1117 if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(i)))
1118 memcpy(loc->locinfo->lconv->int_curr_symbol, buf, i);
1119 else {
1120 MSVCRT__free_locale(loc);
1121 return NULL;
1124 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
1125 |LOCALE_NOUSEROVERRIDE, buf, 256);
1126 if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(i)))
1127 memcpy(loc->locinfo->lconv->currency_symbol, buf, i);
1128 else {
1129 MSVCRT__free_locale(loc);
1130 return NULL;
1133 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
1134 |LOCALE_NOUSEROVERRIDE, buf, 256);
1135 if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(i)))
1136 memcpy(loc->locinfo->lconv->mon_decimal_point, buf, i);
1137 else {
1138 MSVCRT__free_locale(loc);
1139 return NULL;
1142 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
1143 |LOCALE_NOUSEROVERRIDE, buf, 256);
1144 if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(i)))
1145 memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, i);
1146 else {
1147 MSVCRT__free_locale(loc);
1148 return NULL;
1151 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING
1152 |LOCALE_NOUSEROVERRIDE, buf, 256);
1153 if(i>1)
1154 i = i/2 + (buf[i-2]=='0'?0:1);
1155 if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(i))) {
1156 for(i=0; buf[i+1]==';'; i+=2)
1157 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1158 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1159 if(buf[i] != '0')
1160 loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
1161 } else {
1162 MSVCRT__free_locale(loc);
1163 return NULL;
1166 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
1167 |LOCALE_NOUSEROVERRIDE, buf, 256);
1168 if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(i)))
1169 memcpy(loc->locinfo->lconv->positive_sign, buf, i);
1170 else {
1171 MSVCRT__free_locale(loc);
1172 return NULL;
1175 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
1176 |LOCALE_NOUSEROVERRIDE, buf, 256);
1177 if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(i)))
1178 memcpy(loc->locinfo->lconv->negative_sign, buf, i);
1179 else {
1180 MSVCRT__free_locale(loc);
1181 return NULL;
1184 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS
1185 |LOCALE_NOUSEROVERRIDE, buf, 256))
1186 loc->locinfo->lconv->int_frac_digits = atoi(buf);
1187 else {
1188 MSVCRT__free_locale(loc);
1189 return NULL;
1192 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS
1193 |LOCALE_NOUSEROVERRIDE, buf, 256))
1194 loc->locinfo->lconv->frac_digits = atoi(buf);
1195 else {
1196 MSVCRT__free_locale(loc);
1197 return NULL;
1200 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES
1201 |LOCALE_NOUSEROVERRIDE, buf, 256))
1202 loc->locinfo->lconv->p_cs_precedes = atoi(buf);
1203 else {
1204 MSVCRT__free_locale(loc);
1205 return NULL;
1208 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE
1209 |LOCALE_NOUSEROVERRIDE, buf, 256))
1210 loc->locinfo->lconv->p_sep_by_space = atoi(buf);
1211 else {
1212 MSVCRT__free_locale(loc);
1213 return NULL;
1216 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES
1217 |LOCALE_NOUSEROVERRIDE, buf, 256))
1218 loc->locinfo->lconv->n_cs_precedes = atoi(buf);
1219 else {
1220 MSVCRT__free_locale(loc);
1221 return NULL;
1224 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE
1225 |LOCALE_NOUSEROVERRIDE, buf, 256))
1226 loc->locinfo->lconv->n_sep_by_space = atoi(buf);
1227 else {
1228 MSVCRT__free_locale(loc);
1229 return NULL;
1232 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN
1233 |LOCALE_NOUSEROVERRIDE, buf, 256))
1234 loc->locinfo->lconv->p_sign_posn = atoi(buf);
1235 else {
1236 MSVCRT__free_locale(loc);
1237 return NULL;
1240 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN
1241 |LOCALE_NOUSEROVERRIDE, buf, 256))
1242 loc->locinfo->lconv->n_sign_posn = atoi(buf);
1243 else {
1244 MSVCRT__free_locale(loc);
1245 return NULL;
1248 #if _MSVCR_VER >= 120
1249 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
1250 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1251 if(i && (loc->locinfo->lconv->_W_int_curr_symbol = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1252 memcpy(loc->locinfo->lconv->_W_int_curr_symbol, wbuf, i * sizeof(MSVCRT_wchar_t));
1253 else {
1254 MSVCRT__free_locale(loc);
1255 return NULL;
1258 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
1259 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1260 if(i && (loc->locinfo->lconv->_W_currency_symbol = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1261 memcpy(loc->locinfo->lconv->_W_currency_symbol, wbuf, i * sizeof(MSVCRT_wchar_t));
1262 else {
1263 MSVCRT__free_locale(loc);
1264 return NULL;
1267 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
1268 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1269 if(i && (loc->locinfo->lconv->_W_mon_decimal_point = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1270 memcpy(loc->locinfo->lconv->_W_mon_decimal_point, wbuf, i * sizeof(MSVCRT_wchar_t));
1271 else {
1272 MSVCRT__free_locale(loc);
1273 return NULL;
1276 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
1277 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1278 if(i && (loc->locinfo->lconv->_W_mon_thousands_sep = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1279 memcpy(loc->locinfo->lconv->_W_mon_thousands_sep, wbuf, i * sizeof(MSVCRT_wchar_t));
1280 else {
1281 MSVCRT__free_locale(loc);
1282 return NULL;
1285 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
1286 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1287 if(i && (loc->locinfo->lconv->_W_positive_sign = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1288 memcpy(loc->locinfo->lconv->_W_positive_sign, wbuf, i * sizeof(MSVCRT_wchar_t));
1289 else {
1290 MSVCRT__free_locale(loc);
1291 return NULL;
1294 i = GetLocaleInfoW(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
1295 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1296 if(i && (loc->locinfo->lconv->_W_negative_sign = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1297 memcpy(loc->locinfo->lconv->_W_negative_sign, wbuf, i * sizeof(MSVCRT_wchar_t));
1298 else {
1299 MSVCRT__free_locale(loc);
1300 return NULL;
1302 #endif
1304 if(!set_lc_locale_name(loc, MSVCRT_LC_MONETARY)) {
1305 MSVCRT__free_locale(loc);
1306 return NULL;
1308 } else {
1309 loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
1310 loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
1311 loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
1312 loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
1313 loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
1314 loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
1315 loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
1317 if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
1318 || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
1319 || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
1320 || !loc->locinfo->lconv->negative_sign) {
1321 MSVCRT__free_locale(loc);
1322 return NULL;
1325 loc->locinfo->lconv->int_curr_symbol[0] = '\0';
1326 loc->locinfo->lconv->currency_symbol[0] = '\0';
1327 loc->locinfo->lconv->mon_decimal_point[0] = '\0';
1328 loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
1329 loc->locinfo->lconv->mon_grouping[0] = '\0';
1330 loc->locinfo->lconv->positive_sign[0] = '\0';
1331 loc->locinfo->lconv->negative_sign[0] = '\0';
1332 loc->locinfo->lconv->int_frac_digits = 127;
1333 loc->locinfo->lconv->frac_digits = 127;
1334 loc->locinfo->lconv->p_cs_precedes = 127;
1335 loc->locinfo->lconv->p_sep_by_space = 127;
1336 loc->locinfo->lconv->n_cs_precedes = 127;
1337 loc->locinfo->lconv->n_sep_by_space = 127;
1338 loc->locinfo->lconv->p_sign_posn = 127;
1339 loc->locinfo->lconv->n_sign_posn = 127;
1341 #if _MSVCR_VER >= 120
1342 loc->locinfo->lconv->_W_int_curr_symbol = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1343 loc->locinfo->lconv->_W_currency_symbol = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1344 loc->locinfo->lconv->_W_mon_decimal_point = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1345 loc->locinfo->lconv->_W_mon_thousands_sep = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1346 loc->locinfo->lconv->_W_positive_sign = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1347 loc->locinfo->lconv->_W_negative_sign = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1349 if(!loc->locinfo->lconv->_W_int_curr_symbol || !loc->locinfo->lconv->_W_currency_symbol
1350 || !loc->locinfo->lconv->_W_mon_decimal_point || !loc->locinfo->lconv->_W_mon_thousands_sep
1351 || !loc->locinfo->lconv->positive_sign || !loc->locinfo->lconv->negative_sign) {
1352 MSVCRT__free_locale(loc);
1353 return NULL;
1356 loc->locinfo->lconv->_W_int_curr_symbol[0] = '\0';
1357 loc->locinfo->lconv->_W_currency_symbol[0] = '\0';
1358 loc->locinfo->lconv->_W_mon_decimal_point[0] = '\0';
1359 loc->locinfo->lconv->_W_mon_thousands_sep[0] = '\0';
1360 loc->locinfo->lconv->_W_positive_sign[0] = '\0';
1361 loc->locinfo->lconv->_W_negative_sign[0] = '\0';
1362 #endif
1364 loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale = MSVCRT__strdup("C");
1367 if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
1368 if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC], cp[MSVCRT_LC_NUMERIC], loc, MSVCRT_LC_NUMERIC)) {
1369 MSVCRT__free_locale(loc);
1370 return NULL;
1373 if(!loc->locinfo->lconv_intl_refcount)
1374 loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1375 loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1376 if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
1377 MSVCRT__free_locale(loc);
1378 return NULL;
1381 *loc->locinfo->lconv_intl_refcount = 1;
1382 *loc->locinfo->lconv_num_refcount = 1;
1384 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1385 |LOCALE_NOUSEROVERRIDE, buf, 256);
1386 if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(i)))
1387 memcpy(loc->locinfo->lconv->decimal_point, buf, i);
1388 else {
1389 MSVCRT__free_locale(loc);
1390 return NULL;
1393 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1394 |LOCALE_NOUSEROVERRIDE, buf, 256);
1395 if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(i)))
1396 memcpy(loc->locinfo->lconv->thousands_sep, buf, i);
1397 else {
1398 MSVCRT__free_locale(loc);
1399 return NULL;
1402 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING
1403 |LOCALE_NOUSEROVERRIDE, buf, 256);
1404 if(i>1)
1405 i = i/2 + (buf[i-2]=='0'?0:1);
1406 if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(i))) {
1407 for(i=0; buf[i+1]==';'; i+=2)
1408 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1409 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1410 if(buf[i] != '0')
1411 loc->locinfo->lconv->grouping[i/2+1] = 127;
1412 } else {
1413 MSVCRT__free_locale(loc);
1414 return NULL;
1417 #if _MSVCR_VER >= 120
1418 i = GetLocaleInfoW(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1419 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1420 if(i && (loc->locinfo->lconv->_W_decimal_point = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1421 memcpy(loc->locinfo->lconv->_W_decimal_point, wbuf, i * sizeof(MSVCRT_wchar_t));
1422 else {
1423 MSVCRT__free_locale(loc);
1424 return NULL;
1427 i = GetLocaleInfoW(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1428 |LOCALE_NOUSEROVERRIDE, wbuf, 256);
1429 if(i && (loc->locinfo->lconv->_W_thousands_sep = MSVCRT_malloc(i * sizeof(MSVCRT_wchar_t))))
1430 memcpy(loc->locinfo->lconv->_W_thousands_sep, wbuf, i * sizeof(MSVCRT_wchar_t));
1431 else {
1432 MSVCRT__free_locale(loc);
1433 return NULL;
1435 #endif
1437 if(!set_lc_locale_name(loc, MSVCRT_LC_NUMERIC)) {
1438 MSVCRT__free_locale(loc);
1439 return NULL;
1441 } else {
1442 loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1443 loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1444 loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1445 if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
1446 || !loc->locinfo->lconv->grouping) {
1447 MSVCRT__free_locale(loc);
1448 return NULL;
1451 loc->locinfo->lconv->decimal_point[0] = '.';
1452 loc->locinfo->lconv->decimal_point[1] = '\0';
1453 loc->locinfo->lconv->thousands_sep[0] = '\0';
1454 loc->locinfo->lconv->grouping[0] = '\0';
1456 #if _MSVCR_VER >= 120
1457 loc->locinfo->lconv->_W_decimal_point = MSVCRT_malloc(sizeof(MSVCRT_wchar_t[2]));
1458 loc->locinfo->lconv->_W_thousands_sep = MSVCRT_malloc(sizeof(MSVCRT_wchar_t));
1460 if(!loc->locinfo->lconv->_W_decimal_point || !loc->locinfo->lconv->_W_thousands_sep) {
1461 MSVCRT__free_locale(loc);
1462 return NULL;
1465 loc->locinfo->lconv->_W_decimal_point[0] = '.';
1466 loc->locinfo->lconv->_W_decimal_point[1] = '\0';
1467 loc->locinfo->lconv->_W_thousands_sep[0] = '\0';
1468 #endif
1470 loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale = MSVCRT__strdup("C");
1473 if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1474 if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME], cp[MSVCRT_LC_TIME], loc, MSVCRT_LC_TIME)) {
1475 MSVCRT__free_locale(loc);
1476 return NULL;
1479 if(!set_lc_locale_name(loc, MSVCRT_LC_TIME)) {
1480 MSVCRT__free_locale(loc);
1481 return NULL;
1483 } else
1484 loc->locinfo->lc_category[MSVCRT_LC_TIME].locale = MSVCRT__strdup("C");
1486 size = sizeof(MSVCRT___lc_time_data);
1487 lcid_tmp = lcid[MSVCRT_LC_TIME] ? lcid[MSVCRT_LC_TIME] : MAKELCID(LANG_ENGLISH, SORT_DEFAULT);
1488 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1489 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1490 size += sizeof(cloc_short_date) + sizeof(cloc_short_dateW);
1491 }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1492 size += sizeof(cloc_long_date) + sizeof(cloc_long_dateW);
1493 }else {
1494 ret = GetLocaleInfoA(lcid_tmp, time_data[i]
1495 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1496 if(!ret) {
1497 MSVCRT__free_locale(loc);
1498 return NULL;
1500 size += ret;
1502 ret = GetLocaleInfoW(lcid_tmp, time_data[i]
1503 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1504 if(!ret) {
1505 MSVCRT__free_locale(loc);
1506 return NULL;
1508 size += ret*sizeof(MSVCRT_wchar_t);
1512 loc->locinfo->lc_time_curr = MSVCRT_malloc(size);
1513 if(!loc->locinfo->lc_time_curr) {
1514 MSVCRT__free_locale(loc);
1515 return NULL;
1518 ret = 0;
1519 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1520 loc->locinfo->lc_time_curr->str.str[i] = &loc->locinfo->lc_time_curr->data[ret];
1521 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1522 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_short_date, sizeof(cloc_short_date));
1523 ret += sizeof(cloc_short_date);
1524 }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1525 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_long_date, sizeof(cloc_long_date));
1526 ret += sizeof(cloc_long_date);
1527 }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1528 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_time, sizeof(cloc_time));
1529 ret += sizeof(cloc_time);
1530 }else {
1531 ret += GetLocaleInfoA(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1532 &loc->locinfo->lc_time_curr->data[ret], size-ret);
1535 for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1536 loc->locinfo->lc_time_curr->wstr.wstr[i] = (MSVCRT_wchar_t*)&loc->locinfo->lc_time_curr->data[ret];
1537 if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1538 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_short_dateW, sizeof(cloc_short_dateW));
1539 ret += sizeof(cloc_short_dateW);
1540 }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1541 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_long_dateW, sizeof(cloc_long_dateW));
1542 ret += sizeof(cloc_long_dateW);
1543 }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1544 memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_timeW, sizeof(cloc_timeW));
1545 ret += sizeof(cloc_timeW);
1546 }else {
1547 ret += GetLocaleInfoW(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1548 (MSVCRT_wchar_t*)&loc->locinfo->lc_time_curr->data[ret], size-ret)*sizeof(MSVCRT_wchar_t);
1551 loc->locinfo->lc_time_curr->lcid = lcid[MSVCRT_LC_TIME];
1553 return loc;
1556 /*********************************************************************
1557 * setlocale (MSVCRT.@)
1559 char* CDECL MSVCRT_setlocale(int category, const char* locale)
1561 MSVCRT__locale_t loc;
1562 MSVCRT_pthreadlocinfo locinfo = get_locinfo();
1564 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1565 return NULL;
1567 if(!locale) {
1568 if(category == MSVCRT_LC_ALL)
1569 return construct_lc_all(locinfo);
1571 return locinfo->lc_category[category].locale;
1574 loc = MSVCRT__create_locale(category, locale);
1575 if(!loc) {
1576 WARN("%d %s failed\n", category, locale);
1577 return NULL;
1580 LOCK_LOCALE;
1582 switch(category) {
1583 case MSVCRT_LC_ALL:
1584 case MSVCRT_LC_COLLATE:
1585 locinfo->lc_collate_cp = loc->locinfo->lc_collate_cp;
1586 locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1587 loc->locinfo->lc_handle[MSVCRT_LC_COLLATE];
1588 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1589 (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1590 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1591 (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1593 #if _MSVCR_VER >= 110
1594 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_COLLATE],
1595 (void**)&loc->locinfo->lc_name[MSVCRT_LC_COLLATE]);
1596 #endif
1598 if(category != MSVCRT_LC_ALL)
1599 break;
1600 /* fall through */
1601 case MSVCRT_LC_CTYPE:
1602 locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1603 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE];
1604 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1605 (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1606 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1607 (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1609 locinfo->lc_codepage = loc->locinfo->lc_codepage;
1610 locinfo->lc_clike = loc->locinfo->lc_clike;
1611 locinfo->mb_cur_max = loc->locinfo->mb_cur_max;
1613 swap_pointers((void**)&locinfo->ctype1_refcount,
1614 (void**)&loc->locinfo->ctype1_refcount);
1615 swap_pointers((void**)&locinfo->ctype1, (void**)&loc->locinfo->ctype1);
1616 swap_pointers((void**)&locinfo->pctype, (void**)&loc->locinfo->pctype);
1617 swap_pointers((void**)&locinfo->pclmap, (void**)&loc->locinfo->pclmap);
1618 swap_pointers((void**)&locinfo->pcumap, (void**)&loc->locinfo->pcumap);
1620 #if _MSVCR_VER >= 110
1621 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_CTYPE],
1622 (void**)&loc->locinfo->lc_name[MSVCRT_LC_CTYPE]);
1623 #endif
1625 if(category != MSVCRT_LC_ALL)
1626 break;
1627 /* fall through */
1628 case MSVCRT_LC_MONETARY:
1629 locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1630 loc->locinfo->lc_handle[MSVCRT_LC_MONETARY];
1631 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1632 (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1633 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1634 (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1636 swap_pointers((void**)&locinfo->lconv->int_curr_symbol,
1637 (void**)&loc->locinfo->lconv->int_curr_symbol);
1638 swap_pointers((void**)&locinfo->lconv->currency_symbol,
1639 (void**)&loc->locinfo->lconv->currency_symbol);
1640 swap_pointers((void**)&locinfo->lconv->mon_decimal_point,
1641 (void**)&loc->locinfo->lconv->mon_decimal_point);
1642 swap_pointers((void**)&locinfo->lconv->mon_thousands_sep,
1643 (void**)&loc->locinfo->lconv->mon_thousands_sep);
1644 swap_pointers((void**)&locinfo->lconv->mon_grouping,
1645 (void**)&loc->locinfo->lconv->mon_grouping);
1646 swap_pointers((void**)&locinfo->lconv->positive_sign,
1647 (void**)&loc->locinfo->lconv->positive_sign);
1648 swap_pointers((void**)&locinfo->lconv->negative_sign,
1649 (void**)&loc->locinfo->lconv->negative_sign);
1651 #if _MSVCR_VER >= 120
1652 swap_pointers((void**)&locinfo->lconv->_W_int_curr_symbol,
1653 (void**)&loc->locinfo->lconv->_W_int_curr_symbol);
1654 swap_pointers((void**)&locinfo->lconv->_W_currency_symbol,
1655 (void**)&loc->locinfo->lconv->_W_currency_symbol);
1656 swap_pointers((void**)&locinfo->lconv->_W_mon_decimal_point,
1657 (void**)&loc->locinfo->lconv->_W_mon_decimal_point);
1658 swap_pointers((void**)&locinfo->lconv->_W_mon_thousands_sep,
1659 (void**)&loc->locinfo->lconv->_W_mon_thousands_sep);
1660 swap_pointers((void**)&locinfo->lconv->_W_positive_sign,
1661 (void**)&loc->locinfo->lconv->_W_positive_sign);
1662 swap_pointers((void**)&locinfo->lconv->_W_negative_sign,
1663 (void**)&loc->locinfo->lconv->_W_negative_sign);
1664 #endif
1666 locinfo->lconv->int_frac_digits = loc->locinfo->lconv->int_frac_digits;
1667 locinfo->lconv->frac_digits = loc->locinfo->lconv->frac_digits;
1668 locinfo->lconv->p_cs_precedes = loc->locinfo->lconv->p_cs_precedes;
1669 locinfo->lconv->p_sep_by_space = loc->locinfo->lconv->p_sep_by_space;
1670 locinfo->lconv->n_cs_precedes = loc->locinfo->lconv->n_cs_precedes;
1671 locinfo->lconv->n_sep_by_space = loc->locinfo->lconv->n_sep_by_space;
1672 locinfo->lconv->p_sign_posn = loc->locinfo->lconv->p_sign_posn;
1673 locinfo->lconv->n_sign_posn = loc->locinfo->lconv->n_sign_posn;
1675 #if _MSVCR_VER >= 110
1676 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_MONETARY],
1677 (void**)&loc->locinfo->lc_name[MSVCRT_LC_MONETARY]);
1678 #endif
1680 if(category != MSVCRT_LC_ALL)
1681 break;
1682 /* fall through */
1683 case MSVCRT_LC_NUMERIC:
1684 locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1685 loc->locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1686 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1687 (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1688 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1689 (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1691 swap_pointers((void**)&locinfo->lconv->decimal_point,
1692 (void**)&loc->locinfo->lconv->decimal_point);
1693 swap_pointers((void**)&locinfo->lconv->thousands_sep,
1694 (void**)&loc->locinfo->lconv->thousands_sep);
1695 swap_pointers((void**)&locinfo->lconv->grouping,
1696 (void**)&loc->locinfo->lconv->grouping);
1698 #if _MSVCR_VER >= 120
1699 swap_pointers((void**)&locinfo->lconv->_W_decimal_point,
1700 (void**)&loc->locinfo->lconv->_W_decimal_point);
1701 swap_pointers((void**)&locinfo->lconv->_W_thousands_sep,
1702 (void**)&loc->locinfo->lconv->_W_thousands_sep);
1703 #endif
1705 #if _MSVCR_VER >= 110
1706 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_NUMERIC],
1707 (void**)&loc->locinfo->lc_name[MSVCRT_LC_NUMERIC]);
1708 #endif
1710 if(category != MSVCRT_LC_ALL)
1711 break;
1712 /* fall through */
1713 case MSVCRT_LC_TIME:
1714 locinfo->lc_handle[MSVCRT_LC_TIME] =
1715 loc->locinfo->lc_handle[MSVCRT_LC_TIME];
1716 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].locale,
1717 (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].locale);
1718 swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1719 (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].refcount);
1720 swap_pointers((void**)&locinfo->lc_time_curr,
1721 (void**)&loc->locinfo->lc_time_curr);
1723 #if _MSVCR_VER >= 110
1724 swap_pointers((void**)&locinfo->lc_name[MSVCRT_LC_TIME],
1725 (void**)&loc->locinfo->lc_name[MSVCRT_LC_TIME]);
1726 #endif
1728 if(category != MSVCRT_LC_ALL)
1729 break;
1732 MSVCRT__free_locale(loc);
1733 UNLOCK_LOCALE;
1735 if(locinfo == MSVCRT_locale->locinfo) {
1736 int i;
1738 MSVCRT___lc_codepage = locinfo->lc_codepage;
1739 MSVCRT___lc_collate_cp = locinfo->lc_collate_cp;
1740 MSVCRT___mb_cur_max = locinfo->mb_cur_max;
1741 MSVCRT__pctype = locinfo->pctype;
1742 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1743 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1746 if(category == MSVCRT_LC_ALL)
1747 return construct_lc_all(locinfo);
1749 return locinfo->lc_category[category].locale;
1752 /*********************************************************************
1753 * _wsetlocale (MSVCRT.@)
1755 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* wlocale)
1757 static MSVCRT_wchar_t current_lc_all[MAX_LOCALE_LENGTH];
1759 char *locale = NULL;
1760 const char *ret;
1761 MSVCRT_size_t len;
1763 if(wlocale) {
1764 len = MSVCRT_wcstombs(NULL, wlocale, 0);
1765 if(len == -1)
1766 return NULL;
1768 locale = MSVCRT_malloc(++len);
1769 if(!locale)
1770 return NULL;
1772 MSVCRT_wcstombs(locale, wlocale, len);
1775 LOCK_LOCALE;
1776 ret = MSVCRT_setlocale(category, locale);
1777 MSVCRT_free(locale);
1779 if(ret && MSVCRT_mbstowcs(current_lc_all, ret, MAX_LOCALE_LENGTH)==-1)
1780 ret = NULL;
1782 UNLOCK_LOCALE;
1783 return ret ? current_lc_all : NULL;
1786 /*********************************************************************
1787 * _configthreadlocale (MSVCR80.@)
1789 int CDECL _configthreadlocale(int type)
1791 thread_data_t *data = msvcrt_get_thread_data();
1792 MSVCRT__locale_t locale;
1793 int ret;
1795 if(!data)
1796 return -1;
1798 ret = (data->have_locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1800 if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1801 if(!data->have_locale) {
1802 /* Copy current global locale */
1803 locale = MSVCRT__create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
1804 if(!locale)
1805 return -1;
1807 data->locinfo = locale->locinfo;
1808 data->mbcinfo = locale->mbcinfo;
1809 data->have_locale = TRUE;
1810 MSVCRT_free(locale);
1813 return ret;
1816 if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1817 if(data->have_locale) {
1818 free_locinfo(data->locinfo);
1819 free_mbcinfo(data->mbcinfo);
1820 data->locinfo = MSVCRT_locale->locinfo;
1821 data->mbcinfo = MSVCRT_locale->mbcinfo;
1822 data->have_locale = FALSE;
1825 return ret;
1828 if(!type)
1829 return ret;
1831 return -1;
1834 BOOL msvcrt_init_locale(void)
1836 int i;
1838 LOCK_LOCALE;
1839 MSVCRT_locale = MSVCRT__create_locale(0, "C");
1840 UNLOCK_LOCALE;
1841 if(!MSVCRT_locale)
1842 return FALSE;
1844 MSVCRT___lc_codepage = MSVCRT_locale->locinfo->lc_codepage;
1845 MSVCRT___lc_collate_cp = MSVCRT_locale->locinfo->lc_collate_cp;
1846 MSVCRT___mb_cur_max = MSVCRT_locale->locinfo->mb_cur_max;
1847 MSVCRT__pctype = MSVCRT_locale->locinfo->pctype;
1848 for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1849 MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1850 _setmbcp(_MB_CP_ANSI);
1851 return TRUE;