comdlg32: Fix some alignment issues in the Dutch translation.
[wine/hramrach.git] / dlls / msvcrt / locale.c
blob546f20ce47b30534d69130082c9df23b5c15a87c
1 /*
2 * msvcrt.dll locale functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <limits.h>
25 #include <locale.h>
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winnls.h"
34 #include "msvcrt.h"
35 #include "mtdll.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
41 /* FIXME: Need to hold locale for each LC_* type and aggregate
42 * string to produce lc_all.
44 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
45 #define MAX_LOCALE_LENGTH 256
46 MSVCRT__locale_t MSVCRT_locale = NULL;
47 int MSVCRT___lc_codepage = 0;
48 int MSVCRT___lc_collate_cp = 0;
49 HANDLE MSVCRT___lc_handle[MSVCRT_LC_MAX - MSVCRT_LC_MIN + 1] = { 0 };
50 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 & iso codes for synonym support.
60 * Based on MS documentation for setlocale().
62 static const char * const _country_synonyms[] =
64 "Hong Kong","HK",
65 "Hong-Kong","HK",
66 "New Zealand","NZ",
67 "New-Zealand","NZ",
68 "PR China","CN",
69 "PR-China","CN",
70 "United Kingdom","GB",
71 "United-Kingdom","GB",
72 "Britain","GB",
73 "England","GB",
74 "Great Britain","GB",
75 "United States","US",
76 "United-States","US",
77 "America","US"
80 /* INTERNAL: Map a synonym to an ISO code */
81 static void remap_synonym(char *name)
83 unsigned int i;
84 for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
86 if (!strcasecmp(_country_synonyms[i],name))
88 TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
89 name[0] = _country_synonyms[i+1][0];
90 name[1] = _country_synonyms[i+1][1];
91 name[2] = '\0';
92 return;
97 /* Note: Flags are weighted in order of matching importance */
98 #define FOUND_LANGUAGE 0x4
99 #define FOUND_COUNTRY 0x2
100 #define FOUND_CODEPAGE 0x1
102 typedef struct {
103 char search_language[MAX_ELEM_LEN];
104 char search_country[MAX_ELEM_LEN];
105 char search_codepage[MAX_ELEM_LEN];
106 char found_language[MAX_ELEM_LEN];
107 char found_country[MAX_ELEM_LEN];
108 char found_codepage[MAX_ELEM_LEN];
109 unsigned int match_flags;
110 LANGID found_lang_id;
111 } locale_search_t;
113 #define CONTINUE_LOOKING TRUE
114 #define STOP_LOOKING FALSE
116 /* INTERNAL: Get and compare locale info with a given string */
117 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp)
119 buff[0] = 0;
120 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
121 if (!buff[0] || !cmp[0])
122 return 0;
123 /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
124 return !strncasecmp(cmp, buff, strlen(cmp));
127 static BOOL CALLBACK
128 find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
130 locale_search_t *res = (locale_search_t *)lParam;
131 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
132 char buff[MAX_ELEM_LEN];
133 unsigned int flags = 0;
135 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
136 return CONTINUE_LOOKING;
138 /* Check Language */
139 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
140 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
141 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
143 TRACE(":Found language: %s->%s\n", res->search_language, buff);
144 flags |= FOUND_LANGUAGE;
145 memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
147 else if (res->match_flags & FOUND_LANGUAGE)
149 return CONTINUE_LOOKING;
152 /* Check Country */
153 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
154 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
155 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
157 TRACE("Found country:%s->%s\n", res->search_country, buff);
158 flags |= FOUND_COUNTRY;
159 memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
161 else if (res->match_flags & FOUND_COUNTRY)
163 return CONTINUE_LOOKING;
166 /* Check codepage */
167 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
168 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
170 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
171 flags |= FOUND_CODEPAGE;
172 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
174 else if (res->match_flags & FOUND_CODEPAGE)
176 return CONTINUE_LOOKING;
179 if (flags > res->match_flags)
181 /* Found a better match than previously */
182 res->match_flags = flags;
183 res->found_lang_id = LangID;
185 if ((flags & (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE)) ==
186 (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE))
188 TRACE(":found exact locale match\n");
189 return STOP_LOOKING;
191 return CONTINUE_LOOKING;
194 extern int atoi(const char *);
196 /* Internal: Find the LCID for a locale specification */
197 static LCID MSVCRT_locale_to_LCID(const char *locale)
199 LCID lcid;
200 locale_search_t search;
201 char *cp, *region;
203 memset(&search, 0, sizeof(locale_search_t));
205 cp = strchr(locale, '.');
206 region = strchr(locale, '_');
208 lstrcpynA(search.search_language, locale, MAX_ELEM_LEN);
209 if(region) {
210 lstrcpynA(search.search_country, region+1, MAX_ELEM_LEN);
211 if(region-locale < MAX_ELEM_LEN)
212 search.search_language[region-locale] = '\0';
213 } else
214 search.search_country[0] = '\0';
216 if(cp) {
217 lstrcpynA(search.search_codepage, cp+1, MAX_ELEM_LEN);
218 if(cp-region-1 < MAX_ELEM_LEN)
219 search.search_country[cp-region-1] = '\0';
220 if(cp-locale < MAX_ELEM_LEN)
221 search.search_language[cp-locale] = '\0';
222 } else
223 search.search_codepage[0] = '\0';
225 /* FIXME: MSVCRT_locale_to_LCID is not finding remaped values */
226 remap_synonym(search.search_country);
228 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
229 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
230 (LONG_PTR)&search);
232 if (!search.match_flags)
233 return -1;
235 /* If we were given something that didn't match, fail */
236 if (search.search_country[0] && !(search.match_flags & FOUND_COUNTRY))
237 return -1;
239 lcid = MAKELCID(search.found_lang_id, SORT_DEFAULT);
241 /* Populate partial locale, translating LCID to locale string elements */
242 if (!search.found_codepage[0]) {
243 /* Even if a codepage is not enumerated for a locale
244 * it can be set if valid */
245 if (search.search_codepage[0]) {
246 if (IsValidCodePage(atoi(search.search_codepage)))
247 memcpy(search.found_codepage,search.search_codepage,MAX_ELEM_LEN);
248 else {
249 /* Special codepage values: OEM & ANSI */
250 if (strcasecmp(search.search_codepage,"OCP")) {
251 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
252 search.found_codepage, MAX_ELEM_LEN);
253 } else if (strcasecmp(search.search_codepage,"ACP")) {
254 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
255 search.found_codepage, MAX_ELEM_LEN);
256 } else
257 return -1;
259 if (!atoi(search.found_codepage))
260 return -1;
262 } else {
263 /* Prefer ANSI codepages if present */
264 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
265 search.found_codepage, MAX_ELEM_LEN);
266 if (!search.found_codepage[0] || !atoi(search.found_codepage))
267 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
268 search.found_codepage, MAX_ELEM_LEN);
272 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
273 search.found_language, MAX_ELEM_LEN);
274 GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
275 search.found_country, MAX_ELEM_LEN);
276 return lcid;
279 /* INTERNAL: Set lc_handle, lc_id and lc_category in threadlocinfo struct */
280 static BOOL update_threadlocinfo_category(LCID lcid, MSVCRT__locale_t loc, int category)
282 char buf[256], *p;
283 int len;
285 if(GetLocaleInfoA(lcid, LOCALE_ILANGUAGE, buf, 256)) {
286 p = buf;
288 loc->locinfo->lc_id[category].wLanguage = 0;
289 while(*p) {
290 loc->locinfo->lc_id[category].wLanguage *= 16;
292 if(*p <= '9')
293 loc->locinfo->lc_id[category].wLanguage += *p-'0';
294 else
295 loc->locinfo->lc_id[category].wLanguage += *p-'a'+10;
297 p++;
300 loc->locinfo->lc_id[category].wCountry =
301 loc->locinfo->lc_id[category].wLanguage;
304 if(GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE, buf, 256))
305 loc->locinfo->lc_id[category].wCodePage = atoi(buf);
307 loc->locinfo->lc_handle[category] = lcid;
309 len = 0;
310 len += GetLocaleInfoA(lcid, LOCALE_SLANGUAGE, buf, 256);
311 buf[len-1] = '_';
312 len += GetLocaleInfoA(lcid, LOCALE_SCOUNTRY, &buf[len], 256-len);
313 buf[len-1] = '.';
314 len += GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE, &buf[len], 256-len);
316 loc->locinfo->lc_category[category].locale = MSVCRT_malloc(sizeof(char[len]));
317 loc->locinfo->lc_category[category].refcount = MSVCRT_malloc(sizeof(int));
318 if(!loc->locinfo->lc_category[category].locale
319 || !loc->locinfo->lc_category[category].refcount) {
320 MSVCRT_free(loc->locinfo->lc_category[category].locale);
321 MSVCRT_free(loc->locinfo->lc_category[category].refcount);
322 loc->locinfo->lc_category[category].locale = NULL;
323 loc->locinfo->lc_category[category].refcount = NULL;
324 return TRUE;
326 memcpy(loc->locinfo->lc_category[category].locale, buf, sizeof(char[len]));
327 *loc->locinfo->lc_category[category].refcount = 1;
329 return FALSE;
332 /* INTERNAL: swap pointers values */
333 static inline void swap_pointers(void **p1, void **p2) {
334 void *hlp;
336 hlp = *p1;
337 *p1 = *p2;
338 *p2 = hlp;
341 /* INTERNAL: returns _locale_t struct for current locale */
342 MSVCRT__locale_t get_locale(void) {
343 thread_data_t *data = msvcrt_get_thread_data();
345 if(!data || !data->locale)
346 return MSVCRT_locale;
348 return data->locale;
351 /* INTERNAL: constructs string returned by setlocale */
352 static inline char* construct_lc_all(MSVCRT__locale_t cur) {
353 static char current_lc_all[MAX_LOCALE_LENGTH];
355 int i;
357 for(i=MSVCRT_LC_MIN+1; i<MSVCRT_LC_MAX; i++) {
358 if(strcmp(cur->locinfo->lc_category[i].locale,
359 cur->locinfo->lc_category[i+1].locale))
360 break;
363 if(i==MSVCRT_LC_MAX)
364 return cur->locinfo->lc_category[MSVCRT_LC_COLLATE].locale;
366 sprintf(current_lc_all,
367 "LC_COLLATE=%s;LC_CTYPE=%s;LC_MONETARY=%s;LC_NUMERIC=%s;LC_TIME=%s",
368 cur->locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
369 cur->locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
370 cur->locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
371 cur->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
372 cur->locinfo->lc_category[MSVCRT_LC_TIME].locale);
374 return current_lc_all;
378 /*********************************************************************
379 * wsetlocale (MSVCRT.@)
381 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
383 static MSVCRT_wchar_t fake[] = {
384 'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
385 'S','t','a','t','e','s','.','1','2','5','2',0 };
387 FIXME("%d %s\n", category, debugstr_w(locale));
389 return fake;
392 /*********************************************************************
393 * _Getdays (MSVCRT.@)
395 const char* CDECL _Getdays(void)
397 static const char MSVCRT_days[] = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
398 "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
399 /* FIXME: Use locale */
400 TRACE("(void) semi-stub\n");
401 return MSVCRT_days;
404 /*********************************************************************
405 * _Getmonths (MSVCRT.@)
407 const char* CDECL _Getmonths(void)
409 static const char MSVCRT_months[] = ":Jan:January:Feb:February:Mar:March:Apr:"
410 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
411 "October:Nov:November:Dec:December";
412 /* FIXME: Use locale */
413 TRACE("(void) semi-stub\n");
414 return MSVCRT_months;
417 /*********************************************************************
418 * _Gettnames (MSVCRT.@)
420 const char* CDECL _Gettnames(void)
422 /* FIXME: */
423 TRACE("(void) stub\n");
424 return "";
427 /*********************************************************************
428 * _Strftime (MSVCRT.@)
430 const char* CDECL _Strftime(char *out, unsigned int len, const char *fmt,
431 const void *tm, void *foo)
433 /* FIXME: */
434 TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
435 return "";
438 /*********************************************************************
439 * __crtLCMapStringA (MSVCRT.@)
441 int CDECL __crtLCMapStringA(
442 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
443 int dstlen, unsigned int codepage, int xflag
445 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
446 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
447 /* FIXME: A bit incorrect. But msvcrt itself just converts its
448 * arguments to wide strings and then calls LCMapStringW
450 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
453 /*********************************************************************
454 * __crtCompareStringA (MSVCRT.@)
456 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
457 const char *src2, int len2 )
459 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
460 lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
461 /* FIXME: probably not entirely right */
462 return CompareStringA( lcid, flags, src1, len1, src2, len2 );
465 /*********************************************************************
466 * __crtCompareStringW (MSVCRT.@)
468 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
469 const MSVCRT_wchar_t *src2, int len2 )
471 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
472 lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
473 /* FIXME: probably not entirely right */
474 return CompareStringW( lcid, flags, src1, len1, src2, len2 );
477 /*********************************************************************
478 * __crtGetLocaleInfoW (MSVCRT.@)
480 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
482 FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
483 /* FIXME: probably not entirely right */
484 return GetLocaleInfoW( lcid, type, buffer, len );
487 /*********************************************************************
488 * btowc(MSVCRT.@)
490 MSVCRT_wint_t CDECL MSVCRT_btowc(int c)
492 MSVCRT__locale_t locale = get_locale();
493 unsigned char letter = c;
494 MSVCRT_wchar_t ret;
496 if(!MultiByteToWideChar(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE],
497 0, (LPCSTR)&letter, 1, &ret, 1))
498 return 0;
500 return ret;
503 /*********************************************************************
504 * __crtGetStringTypeW(MSVCRT.@)
506 * This function was accepting different number of arguments in older
507 * versions of msvcrt.
509 BOOL CDECL __crtGetStringTypeW(DWORD unk, DWORD type,
510 MSVCRT_wchar_t *buffer, int len, WORD *out)
512 FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
513 unk, type, buffer, len, out);
515 return GetStringTypeW(type, buffer, len, out);
518 /*********************************************************************
519 * localeconv (MSVCRT.@)
521 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
523 return get_locale()->locinfo->lconv;
526 /*********************************************************************
527 * __lconv_init (MSVCRT.@)
529 void CDECL __lconv_init(void)
531 /* this is used to make chars unsigned */
532 charmax = 255;
535 /*********************************************************************
536 * ___lc_handle_func (MSVCRT.@)
538 HANDLE * CDECL ___lc_handle_func(void)
540 return MSVCRT___lc_handle;
543 /*********************************************************************
544 * ___lc_codepage_func (MSVCRT.@)
546 int CDECL ___lc_codepage_func(void)
548 return MSVCRT___lc_codepage;
551 /*********************************************************************
552 * ___lc_collate_cp_func (MSVCRT.@)
554 int CDECL ___lc_collate_cp_func(void)
556 return get_locale()->locinfo->lc_collate_cp;
559 /* _free_locale - not exported in native msvcrt */
560 void CDECL _free_locale(MSVCRT__locale_t locale)
562 int i;
564 for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
565 MSVCRT_free(locale->locinfo->lc_category[i].locale);
566 MSVCRT_free(locale->locinfo->lc_category[i].refcount);
569 if(locale->locinfo->lconv) {
570 MSVCRT_free(locale->locinfo->lconv->decimal_point);
571 MSVCRT_free(locale->locinfo->lconv->thousands_sep);
572 MSVCRT_free(locale->locinfo->lconv->grouping);
573 MSVCRT_free(locale->locinfo->lconv->int_curr_symbol);
574 MSVCRT_free(locale->locinfo->lconv->currency_symbol);
575 MSVCRT_free(locale->locinfo->lconv->mon_decimal_point);
576 MSVCRT_free(locale->locinfo->lconv->mon_thousands_sep);
577 MSVCRT_free(locale->locinfo->lconv->mon_grouping);
578 MSVCRT_free(locale->locinfo->lconv->positive_sign);
579 MSVCRT_free(locale->locinfo->lconv->negative_sign);
581 MSVCRT_free(locale->locinfo->lconv_intl_refcount);
582 MSVCRT_free(locale->locinfo->lconv_num_refcount);
583 MSVCRT_free(locale->locinfo->lconv_mon_refcount);
584 MSVCRT_free(locale->locinfo->lconv);
586 MSVCRT_free(locale->locinfo->ctype1_refcount);
587 MSVCRT_free(locale->locinfo->ctype1);
589 MSVCRT_free(locale->locinfo->pclmap);
590 MSVCRT_free(locale->locinfo->pcumap);
592 MSVCRT_free(locale->locinfo);
593 MSVCRT_free(locale->mbcinfo);
594 MSVCRT_free(locale);
597 /* _create_locale - not exported in native msvcrt */
598 MSVCRT__locale_t _create_locale(int category, const char *locale)
600 static const char collate[] = "COLLATE=";
601 static const char ctype[] = "CTYPE=";
602 static const char monetary[] = "MONETARY=";
603 static const char numeric[] = "NUMERIC=";
604 static const char time[] = "TIME=";
606 MSVCRT__locale_t loc;
607 LCID lcid[6] = { 0 };
608 char buf[256];
609 int i;
611 TRACE("(%d %s)\n", category, locale);
613 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
614 return NULL;
616 if(locale[0]=='C' && !locale[1])
617 lcid[0] = CP_ACP;
618 else if(!locale[0])
619 lcid[0] = GetSystemDefaultLCID();
620 else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
621 char *p;
623 while(1) {
624 locale += 3; /* LC_ */
625 if(!memcmp(locale, collate, sizeof(collate)-1)) {
626 i = MSVCRT_LC_COLLATE;
627 locale += sizeof(collate)-1;
628 } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
629 i = MSVCRT_LC_CTYPE;
630 locale += sizeof(ctype)-1;
631 } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
632 i = MSVCRT_LC_MONETARY;
633 locale += sizeof(monetary)-1;
634 } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
635 i = MSVCRT_LC_NUMERIC;
636 locale += sizeof(numeric)-1;
637 } else if(!memcmp(locale, time, sizeof(time)-1)) {
638 i = MSVCRT_LC_TIME;
639 locale += sizeof(time)-1;
640 } else
641 return NULL;
643 p = strchr(locale, ';');
644 if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0'))
645 lcid[i] = 0;
646 else if(p) {
647 memcpy(buf, locale, p-locale);
648 lcid[i] = MSVCRT_locale_to_LCID(buf);
649 } else
650 lcid[i] = MSVCRT_locale_to_LCID(locale);
652 if(lcid[i] == -1)
653 return NULL;
655 if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
656 break;
658 locale = p+1;
660 } else {
661 lcid[0] = MSVCRT_locale_to_LCID(locale);
662 if(lcid[0] == -1)
663 return NULL;
666 for(i=1; i<6; i++) {
667 if(!lcid[i])
668 lcid[i] = lcid[0];
671 loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
672 if(!loc)
673 return NULL;
675 loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
676 if(!loc->locinfo) {
677 MSVCRT_free(loc);
678 return NULL;
681 loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
682 if(!loc->mbcinfo) {
683 MSVCRT_free(loc->locinfo);
684 MSVCRT_free(loc);
685 return NULL;
688 memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
689 memset(loc->mbcinfo, 0, sizeof(MSVCRT_threadmbcinfo));
691 loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
692 if(!loc->locinfo->lconv) {
693 _free_locale(loc);
694 return NULL;
696 memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
698 loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
699 loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
700 if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
701 _free_locale(loc);
702 return NULL;
705 loc->locinfo->refcount = 1;
707 if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
708 if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE], loc, MSVCRT_LC_COLLATE)) {
709 _free_locale(loc);
710 return NULL;
712 } else
713 loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale = strdup("C");
715 if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
716 CPINFO cp;
718 if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE], loc, MSVCRT_LC_CTYPE)) {
719 _free_locale(loc);
720 return NULL;
723 loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
724 loc->locinfo->lc_collate_cp = loc->locinfo->lc_codepage;
725 loc->locinfo->lc_clike = 1;
726 if(!GetCPInfo(loc->locinfo->lc_codepage, &cp)) {
727 _free_locale(loc);
728 return NULL;
730 loc->locinfo->mb_cur_max = cp.MaxCharSize;
732 loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
733 loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
734 if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
735 _free_locale(loc);
736 return NULL;
739 *loc->locinfo->ctype1_refcount = 1;
740 loc->locinfo->ctype1[0] = 0;
741 loc->locinfo->pctype = loc->locinfo->ctype1+1;
743 buf[1] = buf[2] = '\0';
744 for(i=1; i<257; i++) {
745 buf[0] = i-1;
747 GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
748 1, loc->locinfo->ctype1+i);
749 loc->locinfo->ctype1[i] |= 0x200;
751 } else {
752 loc->locinfo->lc_clike = 1;
753 loc->locinfo->mb_cur_max = 1;
754 loc->locinfo->pctype = MSVCRT__ctype+1;
755 loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale = strdup("C");
758 for(i=0; i<256; i++)
759 buf[i] = i;
761 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
762 (char*)loc->locinfo->pclmap, 256);
763 LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
764 (char*)loc->locinfo->pcumap, 256);
766 loc->mbcinfo->refcount = 1;
767 loc->mbcinfo->mbcodepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
769 for(i=0; i<256; i++) {
770 if(loc->locinfo->pclmap[i] != i) {
771 loc->mbcinfo->mbctype[i+1] |= 0x10;
772 loc->mbcinfo->mbcasemap[i] = loc->locinfo->pclmap[i];
773 } else if(loc->locinfo->pcumap[i] != i) {
774 loc->mbcinfo->mbctype[i+1] |= 0x20;
775 loc->mbcinfo->mbcasemap[i] = loc->locinfo->pcumap[i];
779 if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
780 if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY], loc, MSVCRT_LC_MONETARY)) {
781 _free_locale(loc);
782 return NULL;
785 loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
786 loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
787 if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
788 _free_locale(loc);
789 return NULL;
792 *loc->locinfo->lconv_intl_refcount = 1;
793 *loc->locinfo->lconv_mon_refcount = 1;
795 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL, buf, 256);
796 if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char[i]))))
797 memcpy(loc->locinfo->lconv->int_curr_symbol, buf, sizeof(char[i]));
798 else {
799 _free_locale(loc);
800 return NULL;
803 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY, buf, 256);
804 if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char[i]))))
805 memcpy(loc->locinfo->lconv->currency_symbol, buf, sizeof(char[i]));
806 else {
807 _free_locale(loc);
808 return NULL;
811 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP, buf, 256);
812 if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char[i]))))
813 memcpy(loc->locinfo->lconv->mon_decimal_point, buf, sizeof(char[i]));
814 else {
815 _free_locale(loc);
816 return NULL;
819 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP, buf, 256);
820 if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
821 memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, sizeof(char[i]));
822 else {
823 _free_locale(loc);
824 return NULL;
827 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING, buf, 256);
828 if(i>1)
829 i = i/2 + (buf[i-2]=='0'?0:1);
830 if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char[i])))) {
831 for(i=0; buf[i+1]==';'; i+=2)
832 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
833 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
834 if(buf[i] != '0')
835 loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
836 } else {
837 _free_locale(loc);
838 return NULL;
841 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN, buf, 256);
842 if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char[i]))))
843 memcpy(loc->locinfo->lconv->positive_sign, buf, sizeof(char[i]));
844 else {
845 _free_locale(loc);
846 return NULL;
849 i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN, buf, 256);
850 if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char[i]))))
851 memcpy(loc->locinfo->lconv->negative_sign, buf, sizeof(char[i]));
852 else {
853 _free_locale(loc);
854 return NULL;
857 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS, buf, 256))
858 loc->locinfo->lconv->int_frac_digits = atoi(buf);
859 else {
860 _free_locale(loc);
861 return NULL;
864 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS, buf, 256))
865 loc->locinfo->lconv->frac_digits = atoi(buf);
866 else {
867 _free_locale(loc);
868 return NULL;
871 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES, buf, 256))
872 loc->locinfo->lconv->p_cs_precedes = atoi(buf);
873 else {
874 _free_locale(loc);
875 return NULL;
878 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE, buf, 256))
879 loc->locinfo->lconv->p_sep_by_space = atoi(buf);
880 else {
881 _free_locale(loc);
882 return NULL;
885 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES, buf, 256))
886 loc->locinfo->lconv->n_cs_precedes = atoi(buf);
887 else {
888 _free_locale(loc);
889 return NULL;
892 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE, buf, 256))
893 loc->locinfo->lconv->n_sep_by_space = atoi(buf);
894 else {
895 _free_locale(loc);
896 return NULL;
899 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN, buf, 256))
900 loc->locinfo->lconv->p_sign_posn = atoi(buf);
901 else {
902 _free_locale(loc);
903 return NULL;
906 if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN, buf, 256))
907 loc->locinfo->lconv->n_sign_posn = atoi(buf);
908 else {
909 _free_locale(loc);
910 return NULL;
912 } else {
913 loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
914 loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
915 loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
916 loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
917 loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
918 loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
919 loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
921 if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
922 || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
923 || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
924 || !loc->locinfo->lconv->negative_sign) {
925 _free_locale(loc);
926 return NULL;
929 loc->locinfo->lconv->int_curr_symbol[0] = '\0';
930 loc->locinfo->lconv->currency_symbol[0] = '\0';
931 loc->locinfo->lconv->mon_decimal_point[0] = '\0';
932 loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
933 loc->locinfo->lconv->mon_grouping[0] = '\0';
934 loc->locinfo->lconv->positive_sign[0] = '\0';
935 loc->locinfo->lconv->negative_sign[0] = '\0';
936 loc->locinfo->lconv->int_frac_digits = 127;
937 loc->locinfo->lconv->frac_digits = 127;
938 loc->locinfo->lconv->p_cs_precedes = 127;
939 loc->locinfo->lconv->p_sep_by_space = 127;
940 loc->locinfo->lconv->n_cs_precedes = 127;
941 loc->locinfo->lconv->n_sep_by_space = 127;
942 loc->locinfo->lconv->p_sign_posn = 127;
943 loc->locinfo->lconv->n_sign_posn = 127;
945 loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale = strdup("C");
948 if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
949 if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC], loc, MSVCRT_LC_NUMERIC)) {
950 _free_locale(loc);
951 return NULL;
954 if(!loc->locinfo->lconv_intl_refcount)
955 loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
956 loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
957 if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
958 _free_locale(loc);
959 return NULL;
962 *loc->locinfo->lconv_intl_refcount = 1;
963 *loc->locinfo->lconv_num_refcount = 1;
965 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL, buf, 256);
966 if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[i]))))
967 memcpy(loc->locinfo->lconv->decimal_point, buf, sizeof(char[i]));
968 else {
969 _free_locale(loc);
970 return NULL;
973 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND, buf, 256);
974 if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
975 memcpy(loc->locinfo->lconv->thousands_sep, buf, sizeof(char[i]));
976 else {
977 _free_locale(loc);
978 return NULL;
981 i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING, buf, 256);
982 if(i>1)
983 i = i/2 + (buf[i-2]=='0'?0:1);
984 if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char[i])))) {
985 for(i=0; buf[i+1]==';'; i+=2)
986 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
987 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
988 if(buf[i] != '0')
989 loc->locinfo->lconv->grouping[i/2+1] = 127;
990 } else {
991 _free_locale(loc);
992 return NULL;
994 } else {
995 loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
996 loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
997 loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
998 if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
999 || !loc->locinfo->lconv->grouping) {
1000 _free_locale(loc);
1001 return NULL;
1004 loc->locinfo->lconv->decimal_point[0] = '.';
1005 loc->locinfo->lconv->decimal_point[1] = '\0';
1006 loc->locinfo->lconv->thousands_sep[0] = '\0';
1007 loc->locinfo->lconv->grouping[0] = '\0';
1009 loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale = strdup("C");
1012 if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1013 if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME], loc, MSVCRT_LC_TIME)) {
1014 _free_locale(loc);
1015 return NULL;
1017 } else
1018 loc->locinfo->lc_category[MSVCRT_LC_TIME].locale = strdup("C");
1020 return loc;
1023 /* _configthreadlocale - not exported in native msvcrt */
1024 int CDECL _configthreadlocale(int type)
1026 thread_data_t *data = msvcrt_get_thread_data();
1027 int ret;
1029 if(!data)
1030 return -1;
1032 ret = (data->locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1034 if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1035 if(!data->locale) {
1036 /* Copy current global locale */
1037 data->locale = _create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
1038 if(!data->locale)
1039 return -1;
1042 return ret;
1045 if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1046 if(data->locale) {
1047 _free_locale(data->locale);
1048 data->locale = NULL;
1051 return ret;
1054 if(!type)
1055 return ret;
1057 return -1;
1060 /*********************************************************************
1061 * setlocale (MSVCRT.@)
1063 char* CDECL MSVCRT_setlocale(int category, const char* locale)
1065 MSVCRT__locale_t loc, cur;
1067 cur = get_locale();
1069 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1070 return NULL;
1072 if(!locale) {
1073 if(category == MSVCRT_LC_ALL)
1074 return construct_lc_all(cur);
1076 return cur->locinfo->lc_category[category].locale;
1079 loc = _create_locale(category, locale);
1080 if(!loc) {
1081 WARN("%d %s failed\n", category, locale);
1082 return NULL;
1085 LOCK_LOCALE;
1087 switch(category) {
1088 case MSVCRT_LC_ALL:
1089 if(!cur)
1090 break;
1091 case MSVCRT_LC_COLLATE:
1092 cur->locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1093 loc->locinfo->lc_handle[MSVCRT_LC_COLLATE];
1094 swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1095 (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1096 swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1097 (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1099 if(category != MSVCRT_LC_ALL)
1100 break;
1101 case MSVCRT_LC_CTYPE:
1102 cur->locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1103 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE];
1104 swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1105 (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1106 swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1107 (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1109 cur->locinfo->lc_codepage = loc->locinfo->lc_codepage;
1110 cur->locinfo->lc_collate_cp = loc->locinfo->lc_collate_cp;
1111 cur->locinfo->lc_clike = loc->locinfo->lc_clike;
1112 cur->locinfo->mb_cur_max = loc->locinfo->mb_cur_max;
1114 swap_pointers((void**)&cur->locinfo->ctype1_refcount,
1115 (void**)&loc->locinfo->ctype1_refcount);
1116 swap_pointers((void**)&cur->locinfo->ctype1, (void**)&loc->locinfo->ctype1);
1117 swap_pointers((void**)&cur->locinfo->pctype, (void**)&loc->locinfo->pctype);
1118 swap_pointers((void**)&cur->locinfo->pclmap, (void**)&loc->locinfo->pclmap);
1119 swap_pointers((void**)&cur->locinfo->pcumap, (void**)&loc->locinfo->pcumap);
1121 memcpy(cur->mbcinfo, loc->mbcinfo, sizeof(MSVCRT_threadmbcinfo));
1123 if(category != MSVCRT_LC_ALL)
1124 break;
1125 case MSVCRT_LC_MONETARY:
1126 cur->locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1127 loc->locinfo->lc_handle[MSVCRT_LC_MONETARY];
1128 swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1129 (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1130 swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1131 (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1133 swap_pointers((void**)&cur->locinfo->lconv->int_curr_symbol,
1134 (void**)&loc->locinfo->lconv->int_curr_symbol);
1135 swap_pointers((void**)&cur->locinfo->lconv->currency_symbol,
1136 (void**)&loc->locinfo->lconv->currency_symbol);
1137 swap_pointers((void**)&cur->locinfo->lconv->mon_decimal_point,
1138 (void**)&loc->locinfo->lconv->mon_decimal_point);
1139 swap_pointers((void**)&cur->locinfo->lconv->mon_thousands_sep,
1140 (void**)&loc->locinfo->lconv->mon_thousands_sep);
1141 swap_pointers((void**)&cur->locinfo->lconv->mon_grouping,
1142 (void**)&loc->locinfo->lconv->mon_grouping);
1143 swap_pointers((void**)&cur->locinfo->lconv->positive_sign,
1144 (void**)&loc->locinfo->lconv->positive_sign);
1145 swap_pointers((void**)&cur->locinfo->lconv->negative_sign,
1146 (void**)&loc->locinfo->lconv->negative_sign);
1147 cur->locinfo->lconv->int_frac_digits = loc->locinfo->lconv->int_frac_digits;
1148 cur->locinfo->lconv->frac_digits = loc->locinfo->lconv->frac_digits;
1149 cur->locinfo->lconv->p_cs_precedes = loc->locinfo->lconv->p_cs_precedes;
1150 cur->locinfo->lconv->p_sep_by_space = loc->locinfo->lconv->p_sep_by_space;
1151 cur->locinfo->lconv->n_cs_precedes = loc->locinfo->lconv->n_cs_precedes;
1152 cur->locinfo->lconv->n_sep_by_space = loc->locinfo->lconv->n_sep_by_space;
1153 cur->locinfo->lconv->p_sign_posn = loc->locinfo->lconv->p_sign_posn;
1154 cur->locinfo->lconv->n_sign_posn = loc->locinfo->lconv->n_sign_posn;
1156 if(category != MSVCRT_LC_ALL)
1157 break;
1158 case MSVCRT_LC_NUMERIC:
1159 cur->locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1160 loc->locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1161 swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1162 (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1163 swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1164 (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1166 swap_pointers((void**)&cur->locinfo->lconv->decimal_point,
1167 (void**)&loc->locinfo->lconv->decimal_point);
1168 swap_pointers((void**)&cur->locinfo->lconv->thousands_sep,
1169 (void**)&loc->locinfo->lconv->thousands_sep);
1170 swap_pointers((void**)&cur->locinfo->lconv->grouping,
1171 (void**)&loc->locinfo->lconv->grouping);
1173 if(category != MSVCRT_LC_ALL)
1174 break;
1175 case MSVCRT_LC_TIME:
1176 cur->locinfo->lc_handle[MSVCRT_LC_TIME] =
1177 loc->locinfo->lc_handle[MSVCRT_LC_TIME];
1178 swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_TIME].locale,
1179 (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].locale);
1180 swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1181 (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].refcount);
1183 if(category != MSVCRT_LC_ALL)
1184 break;
1187 if(!cur)
1188 MSVCRT_locale = cur = loc;
1189 else
1190 _free_locale(loc);
1192 UNLOCK_LOCALE;
1194 if(cur == MSVCRT_locale) {
1195 MSVCRT___lc_codepage = cur->locinfo->lc_codepage;
1196 MSVCRT___lc_collate_cp = cur->locinfo->lc_collate_cp;
1197 MSVCRT___mb_cur_max = cur->locinfo->mb_cur_max;
1198 MSVCRT__pctype = cur->locinfo->pctype;
1201 if(category == MSVCRT_LC_ALL)
1202 return construct_lc_all(cur);
1204 return cur->locinfo->lc_category[category].locale;