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
22 #include "wine/port.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
41 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
42 #define MAX_LOCALE_LENGTH 256
43 MSVCRT__locale_t MSVCRT_locale
= NULL
;
44 unsigned short *MSVCRT__pctype
= NULL
;
45 unsigned int MSVCRT___lc_codepage
= 0;
46 int MSVCRT___lc_collate_cp
= 0;
47 LCID MSVCRT___lc_handle
[MSVCRT_LC_MAX
- MSVCRT_LC_MIN
+ 1] = { 0 };
48 int MSVCRT___mb_cur_max
= 1;
49 static unsigned char charmax
= CHAR_MAX
;
52 #define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
53 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
55 #define MSVCRT_LEADBYTE 0x8000
56 #define MSVCRT_C1_DEFINED 0x200
58 /* Friendly country strings & language names abbreviations. */
59 static const char * const _country_synonyms
[] =
62 "american english", "enu",
63 "american-english", "enu",
64 "english-american", "enu",
72 "french-belgian", "frb",
75 "french-canadian", "frc",
77 "chinese-simplified", "chs",
78 "chinese-traditional", "cht",
79 "dutch-belgian", "nlb",
83 "french-swiss", "frs",
85 "german-swiss", "des",
86 "italian-swiss", "its",
87 "german-austrian", "dea",
89 "portuguese-brazil", "ptb",
90 "spanish-mexican", "esm",
91 "norwegian-bokmal", "nor",
92 "norwegian-nynorsk", "non",
93 "spanish-modern", "esn"
96 /* INTERNAL: Map a synonym to an ISO code */
97 static void remap_synonym(char *name
)
100 for (i
= 0; i
< sizeof(_country_synonyms
)/sizeof(char*); i
+= 2 )
102 if (!strcasecmp(_country_synonyms
[i
],name
))
104 TRACE(":Mapping synonym %s to %s\n",name
,_country_synonyms
[i
+1]);
105 strcpy(name
, _country_synonyms
[i
+1]);
111 /* Note: Flags are weighted in order of matching importance */
112 #define FOUND_LANGUAGE 0x4
113 #define FOUND_COUNTRY 0x2
114 #define FOUND_CODEPAGE 0x1
117 char search_language
[MAX_ELEM_LEN
];
118 char search_country
[MAX_ELEM_LEN
];
119 char search_codepage
[MAX_ELEM_LEN
];
120 char found_codepage
[MAX_ELEM_LEN
];
121 unsigned int match_flags
;
122 LANGID found_lang_id
;
125 #define CONTINUE_LOOKING TRUE
126 #define STOP_LOOKING FALSE
128 /* INTERNAL: Get and compare locale info with a given string */
129 static int compare_info(LCID lcid
, DWORD flags
, char* buff
, const char* cmp
, BOOL exact
)
134 GetLocaleInfoA(lcid
, flags
|LOCALE_NOUSEROVERRIDE
, buff
, MAX_ELEM_LEN
);
135 if (!buff
[0] || !cmp
[0])
138 /* Partial matches are only allowed on language/country names */
141 return !strcasecmp(cmp
, buff
);
143 return !strncasecmp(cmp
, buff
, len
);
147 find_best_locale_proc(HMODULE hModule
, LPCSTR type
, LPCSTR name
, WORD LangID
, LONG_PTR lParam
)
149 locale_search_t
*res
= (locale_search_t
*)lParam
;
150 const LCID lcid
= MAKELCID(LangID
, SORT_DEFAULT
);
151 char buff
[MAX_ELEM_LEN
];
152 unsigned int flags
= 0;
154 if(PRIMARYLANGID(LangID
) == LANG_NEUTRAL
)
155 return CONTINUE_LOOKING
;
158 if (compare_info(lcid
,LOCALE_SISO639LANGNAME
,buff
,res
->search_language
, TRUE
) ||
159 compare_info(lcid
,LOCALE_SABBREVLANGNAME
,buff
,res
->search_language
, TRUE
) ||
160 compare_info(lcid
,LOCALE_SENGLANGUAGE
,buff
,res
->search_language
, FALSE
))
162 TRACE(":Found language: %s->%s\n", res
->search_language
, buff
);
163 flags
|= FOUND_LANGUAGE
;
165 else if (res
->match_flags
& FOUND_LANGUAGE
)
167 return CONTINUE_LOOKING
;
171 if (compare_info(lcid
,LOCALE_SISO3166CTRYNAME
,buff
,res
->search_country
, TRUE
) ||
172 compare_info(lcid
,LOCALE_SABBREVCTRYNAME
,buff
,res
->search_country
, TRUE
) ||
173 compare_info(lcid
,LOCALE_SENGCOUNTRY
,buff
,res
->search_country
, FALSE
))
175 TRACE("Found country:%s->%s\n", res
->search_country
, buff
);
176 flags
|= FOUND_COUNTRY
;
178 else if (res
->match_flags
& FOUND_COUNTRY
)
180 return CONTINUE_LOOKING
;
184 if (compare_info(lcid
,LOCALE_IDEFAULTCODEPAGE
,buff
,res
->search_codepage
, TRUE
) ||
185 (compare_info(lcid
,LOCALE_IDEFAULTANSICODEPAGE
,buff
,res
->search_codepage
, TRUE
)))
187 TRACE("Found codepage:%s->%s\n", res
->search_codepage
, buff
);
188 flags
|= FOUND_CODEPAGE
;
189 memcpy(res
->found_codepage
,res
->search_codepage
,MAX_ELEM_LEN
);
191 else if (res
->match_flags
& FOUND_CODEPAGE
)
193 return CONTINUE_LOOKING
;
196 if (flags
> res
->match_flags
)
198 /* Found a better match than previously */
199 res
->match_flags
= flags
;
200 res
->found_lang_id
= LangID
;
202 if ((flags
& (FOUND_LANGUAGE
| FOUND_COUNTRY
| FOUND_CODEPAGE
)) ==
203 (FOUND_LANGUAGE
| FOUND_COUNTRY
| FOUND_CODEPAGE
))
205 TRACE(":found exact locale match\n");
208 return CONTINUE_LOOKING
;
211 extern int atoi(const char *);
213 /* Internal: Find the LCID for a locale specification */
214 LCID
MSVCRT_locale_to_LCID(const char *locale
)
217 locale_search_t search
;
218 const char *cp
, *region
;
220 memset(&search
, 0, sizeof(locale_search_t
));
222 cp
= strchr(locale
, '.');
223 region
= strchr(locale
, '_');
225 lstrcpynA(search
.search_language
, locale
, MAX_ELEM_LEN
);
227 lstrcpynA(search
.search_country
, region
+1, MAX_ELEM_LEN
);
228 if(region
-locale
< MAX_ELEM_LEN
)
229 search
.search_language
[region
-locale
] = '\0';
231 search
.search_country
[0] = '\0';
234 lstrcpynA(search
.search_codepage
, cp
+1, MAX_ELEM_LEN
);
235 if(region
&& cp
-region
-1<MAX_ELEM_LEN
)
236 search
.search_country
[cp
-region
-1] = '\0';
237 if(cp
-locale
< MAX_ELEM_LEN
)
238 search
.search_language
[cp
-locale
] = '\0';
240 search
.search_codepage
[0] = '\0';
242 if(!search
.search_country
[0] && !search
.search_codepage
[0])
243 remap_synonym(search
.search_language
);
245 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR
)RT_STRING
,
246 (LPCSTR
)LOCALE_ILANGUAGE
,find_best_locale_proc
,
249 if (!search
.match_flags
)
252 /* If we were given something that didn't match, fail */
253 if (search
.search_country
[0] && !(search
.match_flags
& FOUND_COUNTRY
))
256 lcid
= MAKELCID(search
.found_lang_id
, SORT_DEFAULT
);
258 /* Populate partial locale, translating LCID to locale string elements */
259 if (!search
.found_codepage
[0]) {
260 /* Even if a codepage is not enumerated for a locale
261 * it can be set if valid */
262 if (search
.search_codepage
[0]) {
263 if (IsValidCodePage(atoi(search
.search_codepage
)))
264 memcpy(search
.found_codepage
,search
.search_codepage
,MAX_ELEM_LEN
);
266 /* Special codepage values: OEM & ANSI */
267 if (strcasecmp(search
.search_codepage
,"OCP")) {
268 GetLocaleInfoA(lcid
, LOCALE_IDEFAULTCODEPAGE
,
269 search
.found_codepage
, MAX_ELEM_LEN
);
270 } else if (strcasecmp(search
.search_codepage
,"ACP")) {
271 GetLocaleInfoA(lcid
, LOCALE_IDEFAULTANSICODEPAGE
,
272 search
.found_codepage
, MAX_ELEM_LEN
);
276 if (!atoi(search
.found_codepage
))
280 /* Prefer ANSI codepages if present */
281 GetLocaleInfoA(lcid
, LOCALE_IDEFAULTANSICODEPAGE
,
282 search
.found_codepage
, MAX_ELEM_LEN
);
283 if (!search
.found_codepage
[0] || !atoi(search
.found_codepage
))
284 GetLocaleInfoA(lcid
, LOCALE_IDEFAULTCODEPAGE
,
285 search
.found_codepage
, MAX_ELEM_LEN
);
292 /* INTERNAL: Set lc_handle, lc_id and lc_category in threadlocinfo struct */
293 static BOOL
update_threadlocinfo_category(LCID lcid
, MSVCRT__locale_t loc
, int category
)
298 if(GetLocaleInfoA(lcid
, LOCALE_ILANGUAGE
|LOCALE_NOUSEROVERRIDE
, buf
, 256)) {
301 loc
->locinfo
->lc_id
[category
].wLanguage
= 0;
303 loc
->locinfo
->lc_id
[category
].wLanguage
*= 16;
306 loc
->locinfo
->lc_id
[category
].wLanguage
+= *p
-'0';
308 loc
->locinfo
->lc_id
[category
].wLanguage
+= *p
-'a'+10;
313 loc
->locinfo
->lc_id
[category
].wCountry
=
314 loc
->locinfo
->lc_id
[category
].wLanguage
;
317 if(GetLocaleInfoA(lcid
, LOCALE_IDEFAULTANSICODEPAGE
318 |LOCALE_NOUSEROVERRIDE
, buf
, 256))
319 loc
->locinfo
->lc_id
[category
].wCodePage
= atoi(buf
);
321 loc
->locinfo
->lc_handle
[category
] = lcid
;
324 len
+= GetLocaleInfoA(lcid
, LOCALE_SENGLANGUAGE
325 |LOCALE_NOUSEROVERRIDE
, buf
, 256);
327 len
+= GetLocaleInfoA(lcid
, LOCALE_SENGCOUNTRY
328 |LOCALE_NOUSEROVERRIDE
, &buf
[len
], 256-len
);
330 len
+= GetLocaleInfoA(lcid
, LOCALE_IDEFAULTANSICODEPAGE
331 |LOCALE_NOUSEROVERRIDE
, &buf
[len
], 256-len
);
333 loc
->locinfo
->lc_category
[category
].locale
= MSVCRT_malloc(len
);
334 loc
->locinfo
->lc_category
[category
].refcount
= MSVCRT_malloc(sizeof(int));
335 if(!loc
->locinfo
->lc_category
[category
].locale
336 || !loc
->locinfo
->lc_category
[category
].refcount
) {
337 MSVCRT_free(loc
->locinfo
->lc_category
[category
].locale
);
338 MSVCRT_free(loc
->locinfo
->lc_category
[category
].refcount
);
339 loc
->locinfo
->lc_category
[category
].locale
= NULL
;
340 loc
->locinfo
->lc_category
[category
].refcount
= NULL
;
343 memcpy(loc
->locinfo
->lc_category
[category
].locale
, buf
, len
);
344 *loc
->locinfo
->lc_category
[category
].refcount
= 1;
349 /* INTERNAL: swap pointers values */
350 static inline void swap_pointers(void **p1
, void **p2
) {
358 /* INTERNAL: returns pthreadlocinfo struct */
359 MSVCRT_pthreadlocinfo
get_locinfo(void) {
360 thread_data_t
*data
= msvcrt_get_thread_data();
362 if(!data
|| !data
->have_locale
)
363 return MSVCRT_locale
->locinfo
;
365 return data
->locinfo
;
368 /* INTERNAL: returns pthreadlocinfo struct */
369 MSVCRT_pthreadmbcinfo
get_mbcinfo(void) {
370 thread_data_t
*data
= msvcrt_get_thread_data();
372 if(!data
|| !data
->have_locale
)
373 return MSVCRT_locale
->mbcinfo
;
375 return data
->mbcinfo
;
378 /* INTERNAL: constructs string returned by setlocale */
379 static inline char* construct_lc_all(MSVCRT_pthreadlocinfo locinfo
) {
380 static char current_lc_all
[MAX_LOCALE_LENGTH
];
384 for(i
=MSVCRT_LC_MIN
+1; i
<MSVCRT_LC_MAX
; i
++) {
385 if(strcmp(locinfo
->lc_category
[i
].locale
,
386 locinfo
->lc_category
[i
+1].locale
))
391 return locinfo
->lc_category
[MSVCRT_LC_COLLATE
].locale
;
393 sprintf(current_lc_all
,
394 "LC_COLLATE=%s;LC_CTYPE=%s;LC_MONETARY=%s;LC_NUMERIC=%s;LC_TIME=%s",
395 locinfo
->lc_category
[MSVCRT_LC_COLLATE
].locale
,
396 locinfo
->lc_category
[MSVCRT_LC_CTYPE
].locale
,
397 locinfo
->lc_category
[MSVCRT_LC_MONETARY
].locale
,
398 locinfo
->lc_category
[MSVCRT_LC_NUMERIC
].locale
,
399 locinfo
->lc_category
[MSVCRT_LC_TIME
].locale
);
401 return current_lc_all
;
405 /*********************************************************************
406 * wsetlocale (MSVCRT.@)
408 MSVCRT_wchar_t
* CDECL
MSVCRT__wsetlocale(int category
, const MSVCRT_wchar_t
* locale
)
410 static MSVCRT_wchar_t fake
[] = {
411 'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
412 'S','t','a','t','e','s','.','1','2','5','2',0 };
414 FIXME("%d %s\n", category
, debugstr_w(locale
));
419 /*********************************************************************
420 * _Getdays (MSVCRT.@)
422 char* CDECL
_Getdays(void)
424 MSVCRT___lc_time_data
*cur
= get_locinfo()->lc_time_curr
;
430 size
= cur
->str
.names
.short_mon
[0]-cur
->str
.names
.short_wday
[0];
431 out
= MSVCRT_malloc(size
+1);
438 len
= strlen(cur
->str
.names
.short_wday
[i
]);
439 memcpy(&out
[size
], cur
->str
.names
.short_wday
[i
], len
);
443 len
= strlen(cur
->str
.names
.wday
[i
]);
444 memcpy(&out
[size
], cur
->str
.names
.wday
[i
], len
);
452 /*********************************************************************
453 * _Getmonths (MSVCRT.@)
455 char* CDECL
_Getmonths(void)
457 MSVCRT___lc_time_data
*cur
= get_locinfo()->lc_time_curr
;
463 size
= cur
->str
.names
.am
-cur
->str
.names
.short_mon
[0];
464 out
= MSVCRT_malloc(size
+1);
469 for(i
=0; i
<12; i
++) {
471 len
= strlen(cur
->str
.names
.short_mon
[i
]);
472 memcpy(&out
[size
], cur
->str
.names
.short_mon
[i
], len
);
476 len
= strlen(cur
->str
.names
.mon
[i
]);
477 memcpy(&out
[size
], cur
->str
.names
.mon
[i
], len
);
485 /*********************************************************************
486 * _Gettnames (MSVCRT.@)
488 void* CDECL
_Gettnames(void)
490 MSVCRT___lc_time_data
*ret
, *cur
= get_locinfo()->lc_time_curr
;
491 int i
, size
= sizeof(MSVCRT___lc_time_data
);
495 for(i
=0; i
<sizeof(cur
->str
.str
)/sizeof(cur
->str
.str
[0]); i
++)
496 size
+= strlen(cur
->str
.str
[i
])+1;
498 ret
= MSVCRT_malloc(size
);
501 memcpy(ret
, cur
, size
);
504 for(i
=0; i
<sizeof(cur
->str
.str
)/sizeof(cur
->str
.str
[0]); i
++) {
505 ret
->str
.str
[i
] = &ret
->data
[size
];
506 size
+= strlen(&ret
->data
[size
])+1;
512 /*********************************************************************
513 * __crtLCMapStringA (MSVCRT.@)
515 int CDECL
__crtLCMapStringA(
516 LCID lcid
, DWORD mapflags
, const char* src
, int srclen
, char* dst
,
517 int dstlen
, unsigned int codepage
, int xflag
519 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
520 lcid
,mapflags
,src
,srclen
,dst
,dstlen
,codepage
,xflag
);
521 /* FIXME: A bit incorrect. But msvcrt itself just converts its
522 * arguments to wide strings and then calls LCMapStringW
524 return LCMapStringA(lcid
,mapflags
,src
,srclen
,dst
,dstlen
);
527 /*********************************************************************
528 * __crtLCMapStringW (MSVCRT.@)
530 int CDECL
__crtLCMapStringW(LCID lcid
, DWORD mapflags
, const MSVCRT_wchar_t
*src
,
531 int srclen
, MSVCRT_wchar_t
*dst
, int dstlen
, unsigned int codepage
, int xflag
)
533 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
534 lcid
, mapflags
, debugstr_w(src
), srclen
, dst
, dstlen
, codepage
, xflag
);
536 return LCMapStringW(lcid
, mapflags
, src
, srclen
, dst
, dstlen
);
539 /*********************************************************************
540 * __crtCompareStringA (MSVCRT.@)
542 int CDECL
__crtCompareStringA( LCID lcid
, DWORD flags
, const char *src1
, int len1
,
543 const char *src2
, int len2
)
545 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
546 lcid
, flags
, debugstr_a(src1
), len1
, debugstr_a(src2
), len2
);
547 /* FIXME: probably not entirely right */
548 return CompareStringA( lcid
, flags
, src1
, len1
, src2
, len2
);
551 /*********************************************************************
552 * __crtCompareStringW (MSVCRT.@)
554 int CDECL
__crtCompareStringW( LCID lcid
, DWORD flags
, const MSVCRT_wchar_t
*src1
, int len1
,
555 const MSVCRT_wchar_t
*src2
, int len2
)
557 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
558 lcid
, flags
, debugstr_w(src1
), len1
, debugstr_w(src2
), len2
);
559 /* FIXME: probably not entirely right */
560 return CompareStringW( lcid
, flags
, src1
, len1
, src2
, len2
);
563 /*********************************************************************
564 * __crtGetLocaleInfoW (MSVCRT.@)
566 int CDECL
__crtGetLocaleInfoW( LCID lcid
, LCTYPE type
, MSVCRT_wchar_t
*buffer
, int len
)
568 FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid
, type
, buffer
, len
);
569 /* FIXME: probably not entirely right */
570 return GetLocaleInfoW( lcid
, type
, buffer
, len
);
573 /*********************************************************************
576 MSVCRT_wint_t CDECL
MSVCRT_btowc(int c
)
578 unsigned char letter
= c
;
581 if(!MultiByteToWideChar(get_locinfo()->lc_handle
[MSVCRT_LC_CTYPE
],
582 0, (LPCSTR
)&letter
, 1, &ret
, 1))
588 /*********************************************************************
589 * __crtGetStringTypeW(MSVCRT.@)
591 * This function was accepting different number of arguments in older
592 * versions of msvcrt.
594 BOOL CDECL
__crtGetStringTypeW(DWORD unk
, DWORD type
,
595 MSVCRT_wchar_t
*buffer
, int len
, WORD
*out
)
597 FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
598 unk
, type
, buffer
, len
, out
);
600 return GetStringTypeW(type
, buffer
, len
, out
);
603 /*********************************************************************
604 * localeconv (MSVCRT.@)
606 struct MSVCRT_lconv
* CDECL
MSVCRT_localeconv(void)
608 return get_locinfo()->lconv
;
611 /*********************************************************************
612 * __lconv_init (MSVCRT.@)
614 int CDECL
__lconv_init(void)
616 /* this is used to make chars unsigned */
621 /*********************************************************************
622 * ___lc_handle_func (MSVCRT.@)
624 LCID
* CDECL
___lc_handle_func(void)
626 return MSVCRT___lc_handle
;
629 /*********************************************************************
630 * ___lc_codepage_func (MSVCRT.@)
632 unsigned int CDECL
___lc_codepage_func(void)
634 return MSVCRT___lc_codepage
;
637 /*********************************************************************
638 * ___lc_collate_cp_func (MSVCRT.@)
640 int CDECL
___lc_collate_cp_func(void)
642 return get_locinfo()->lc_collate_cp
;
645 /* INTERNAL: frees MSVCRT_pthreadlocinfo struct */
646 void free_locinfo(MSVCRT_pthreadlocinfo locinfo
)
653 if(InterlockedDecrement(&locinfo
->refcount
))
656 for(i
=MSVCRT_LC_MIN
+1; i
<=MSVCRT_LC_MAX
; i
++) {
657 MSVCRT_free(locinfo
->lc_category
[i
].locale
);
658 MSVCRT_free(locinfo
->lc_category
[i
].refcount
);
662 MSVCRT_free(locinfo
->lconv
->decimal_point
);
663 MSVCRT_free(locinfo
->lconv
->thousands_sep
);
664 MSVCRT_free(locinfo
->lconv
->grouping
);
665 MSVCRT_free(locinfo
->lconv
->int_curr_symbol
);
666 MSVCRT_free(locinfo
->lconv
->currency_symbol
);
667 MSVCRT_free(locinfo
->lconv
->mon_decimal_point
);
668 MSVCRT_free(locinfo
->lconv
->mon_thousands_sep
);
669 MSVCRT_free(locinfo
->lconv
->mon_grouping
);
670 MSVCRT_free(locinfo
->lconv
->positive_sign
);
671 MSVCRT_free(locinfo
->lconv
->negative_sign
);
673 MSVCRT_free(locinfo
->lconv_intl_refcount
);
674 MSVCRT_free(locinfo
->lconv_num_refcount
);
675 MSVCRT_free(locinfo
->lconv_mon_refcount
);
676 MSVCRT_free(locinfo
->lconv
);
678 MSVCRT_free(locinfo
->ctype1_refcount
);
679 MSVCRT_free(locinfo
->ctype1
);
681 MSVCRT_free(locinfo
->pclmap
);
682 MSVCRT_free(locinfo
->pcumap
);
684 MSVCRT_free(locinfo
->lc_time_curr
);
686 MSVCRT_free(locinfo
);
689 /* INTERNAL: frees MSVCRT_pthreadmbcinfo struct */
690 void free_mbcinfo(MSVCRT_pthreadmbcinfo mbcinfo
)
695 if(InterlockedDecrement(&mbcinfo
->refcount
))
698 MSVCRT_free(mbcinfo
);
701 /* _get_current_locale - not exported in native msvcrt */
702 MSVCRT__locale_t CDECL
MSVCRT__get_current_locale(void)
704 MSVCRT__locale_t loc
= MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct
));
708 loc
->locinfo
= get_locinfo();
709 loc
->mbcinfo
= get_mbcinfo();
710 InterlockedIncrement(&loc
->locinfo
->refcount
);
711 InterlockedIncrement(&loc
->mbcinfo
->refcount
);
715 /* _free_locale - not exported in native msvcrt */
716 void CDECL
MSVCRT__free_locale(MSVCRT__locale_t locale
)
721 free_locinfo(locale
->locinfo
);
722 free_mbcinfo(locale
->mbcinfo
);
726 /* _create_locale - not exported in native msvcrt */
727 MSVCRT__locale_t CDECL
MSVCRT__create_locale(int category
, const char *locale
)
729 static const DWORD time_data
[] = {
730 LOCALE_SABBREVDAYNAME7
, LOCALE_SABBREVDAYNAME1
, LOCALE_SABBREVDAYNAME2
,
731 LOCALE_SABBREVDAYNAME3
, LOCALE_SABBREVDAYNAME4
, LOCALE_SABBREVDAYNAME5
,
732 LOCALE_SABBREVDAYNAME6
,
733 LOCALE_SDAYNAME7
, LOCALE_SDAYNAME1
, LOCALE_SDAYNAME2
, LOCALE_SDAYNAME3
,
734 LOCALE_SDAYNAME4
, LOCALE_SDAYNAME5
, LOCALE_SDAYNAME6
,
735 LOCALE_SABBREVMONTHNAME1
, LOCALE_SABBREVMONTHNAME2
, LOCALE_SABBREVMONTHNAME3
,
736 LOCALE_SABBREVMONTHNAME4
, LOCALE_SABBREVMONTHNAME5
, LOCALE_SABBREVMONTHNAME6
,
737 LOCALE_SABBREVMONTHNAME7
, LOCALE_SABBREVMONTHNAME8
, LOCALE_SABBREVMONTHNAME9
,
738 LOCALE_SABBREVMONTHNAME10
, LOCALE_SABBREVMONTHNAME11
, LOCALE_SABBREVMONTHNAME12
,
739 LOCALE_SMONTHNAME1
, LOCALE_SMONTHNAME2
, LOCALE_SMONTHNAME3
, LOCALE_SMONTHNAME4
,
740 LOCALE_SMONTHNAME5
, LOCALE_SMONTHNAME6
, LOCALE_SMONTHNAME7
, LOCALE_SMONTHNAME8
,
741 LOCALE_SMONTHNAME9
, LOCALE_SMONTHNAME10
, LOCALE_SMONTHNAME11
, LOCALE_SMONTHNAME12
,
742 LOCALE_S1159
, LOCALE_S2359
,
743 LOCALE_SSHORTDATE
, LOCALE_SLONGDATE
,
746 static const char collate
[] = "COLLATE=";
747 static const char ctype
[] = "CTYPE=";
748 static const char monetary
[] = "MONETARY=";
749 static const char numeric
[] = "NUMERIC=";
750 static const char time
[] = "TIME=";
751 static const char cloc_short_date
[] = "MM/dd/yy";
752 static const MSVCRT_wchar_t cloc_short_dateW
[] = {'M','M','/','d','d','/','y','y',0};
753 static const char cloc_long_date
[] = "dddd, MMMM dd, yyyy";
754 static const MSVCRT_wchar_t cloc_long_dateW
[] = {'d','d','d','d',',',' ','M','M','M','M',' ','d','d',',',' ','y','y','y','y',0};
755 static const char cloc_time
[] = "HH:mm:ss";
756 static const MSVCRT_wchar_t cloc_timeW
[] = {'H','H',':','m','m',':','s','s',0};
758 MSVCRT__locale_t loc
;
759 LCID lcid
[6] = { 0 }, lcid_tmp
;
763 TRACE("(%d %s)\n", category
, locale
);
765 if(category
<MSVCRT_LC_MIN
|| category
>MSVCRT_LC_MAX
|| !locale
)
768 if(locale
[0]=='C' && !locale
[1])
771 lcid
[0] = GetSystemDefaultLCID();
772 else if (locale
[0] == 'L' && locale
[1] == 'C' && locale
[2] == '_') {
776 locale
+= 3; /* LC_ */
777 if(!memcmp(locale
, collate
, sizeof(collate
)-1)) {
778 i
= MSVCRT_LC_COLLATE
;
779 locale
+= sizeof(collate
)-1;
780 } else if(!memcmp(locale
, ctype
, sizeof(ctype
)-1)) {
782 locale
+= sizeof(ctype
)-1;
783 } else if(!memcmp(locale
, monetary
, sizeof(monetary
)-1)) {
784 i
= MSVCRT_LC_MONETARY
;
785 locale
+= sizeof(monetary
)-1;
786 } else if(!memcmp(locale
, numeric
, sizeof(numeric
)-1)) {
787 i
= MSVCRT_LC_NUMERIC
;
788 locale
+= sizeof(numeric
)-1;
789 } else if(!memcmp(locale
, time
, sizeof(time
)-1)) {
791 locale
+= sizeof(time
)-1;
795 p
= strchr(locale
, ';');
796 if(locale
[0]=='C' && (locale
[1]==';' || locale
[1]=='\0'))
799 memcpy(buf
, locale
, p
-locale
);
800 buf
[p
-locale
] = '\0';
801 lcid
[i
] = MSVCRT_locale_to_LCID(buf
);
803 lcid
[i
] = MSVCRT_locale_to_LCID(locale
);
808 if(!p
|| *(p
+1)!='L' || *(p
+2)!='C' || *(p
+3)!='_')
814 lcid
[0] = MSVCRT_locale_to_LCID(locale
);
824 loc
= MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct
));
828 loc
->locinfo
= MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo
));
834 loc
->mbcinfo
= MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo
));
836 MSVCRT_free(loc
->locinfo
);
841 memset(loc
->locinfo
, 0, sizeof(MSVCRT_threadlocinfo
));
842 loc
->locinfo
->refcount
= 1;
843 loc
->mbcinfo
->refcount
= 1;
845 loc
->locinfo
->lconv
= MSVCRT_malloc(sizeof(struct MSVCRT_lconv
));
846 if(!loc
->locinfo
->lconv
) {
847 MSVCRT__free_locale(loc
);
850 memset(loc
->locinfo
->lconv
, 0, sizeof(struct MSVCRT_lconv
));
852 loc
->locinfo
->pclmap
= MSVCRT_malloc(sizeof(char[256]));
853 loc
->locinfo
->pcumap
= MSVCRT_malloc(sizeof(char[256]));
854 if(!loc
->locinfo
->pclmap
|| !loc
->locinfo
->pcumap
) {
855 MSVCRT__free_locale(loc
);
859 if(lcid
[MSVCRT_LC_COLLATE
] && (category
==MSVCRT_LC_ALL
|| category
==MSVCRT_LC_COLLATE
)) {
860 if(update_threadlocinfo_category(lcid
[MSVCRT_LC_COLLATE
], loc
, MSVCRT_LC_COLLATE
)) {
861 MSVCRT__free_locale(loc
);
865 loc
->locinfo
->lc_collate_cp
= loc
->locinfo
->lc_id
[MSVCRT_LC_COLLATE
].wCodePage
;
867 loc
->locinfo
->lc_category
[MSVCRT_LC_COLLATE
].locale
= MSVCRT__strdup("C");
869 if(lcid
[MSVCRT_LC_CTYPE
] && (category
==MSVCRT_LC_ALL
|| category
==MSVCRT_LC_CTYPE
)) {
873 if(update_threadlocinfo_category(lcid
[MSVCRT_LC_CTYPE
], loc
, MSVCRT_LC_CTYPE
)) {
874 MSVCRT__free_locale(loc
);
878 loc
->locinfo
->lc_codepage
= loc
->locinfo
->lc_id
[MSVCRT_LC_CTYPE
].wCodePage
;
879 loc
->locinfo
->lc_clike
= 1;
880 if(!GetCPInfo(loc
->locinfo
->lc_codepage
, &cp
)) {
881 MSVCRT__free_locale(loc
);
884 loc
->locinfo
->mb_cur_max
= cp
.MaxCharSize
;
886 loc
->locinfo
->ctype1_refcount
= MSVCRT_malloc(sizeof(int));
887 loc
->locinfo
->ctype1
= MSVCRT_malloc(sizeof(short[257]));
888 if(!loc
->locinfo
->ctype1_refcount
|| !loc
->locinfo
->ctype1
) {
889 MSVCRT__free_locale(loc
);
893 *loc
->locinfo
->ctype1_refcount
= 1;
894 loc
->locinfo
->ctype1
[0] = 0;
895 loc
->locinfo
->pctype
= loc
->locinfo
->ctype1
+1;
897 buf
[1] = buf
[2] = '\0';
898 for(i
=1; i
<257; i
++) {
901 /* builtin GetStringTypeA doesn't set output to 0 on invalid input */
902 loc
->locinfo
->ctype1
[i
] = 0;
904 GetStringTypeA(lcid
[MSVCRT_LC_CTYPE
], CT_CTYPE1
, buf
,
905 1, loc
->locinfo
->ctype1
+i
);
908 for(i
=0; cp
.LeadByte
[i
+1]!=0; i
+=2)
909 for(j
=cp
.LeadByte
[i
]; j
<=cp
.LeadByte
[i
+1]; j
++)
910 loc
->locinfo
->ctype1
[j
+1] |= MSVCRT__LEADBYTE
;
912 loc
->locinfo
->lc_clike
= 1;
913 loc
->locinfo
->mb_cur_max
= 1;
914 loc
->locinfo
->pctype
= MSVCRT__ctype
+1;
915 loc
->locinfo
->lc_category
[MSVCRT_LC_CTYPE
].locale
= MSVCRT__strdup("C");
918 for(i
=0; i
<256; i
++) {
919 if(loc
->locinfo
->pctype
[i
] & MSVCRT__LEADBYTE
)
926 if(lcid
[MSVCRT_LC_CTYPE
]) {
927 LCMapStringA(lcid
[MSVCRT_LC_CTYPE
], LCMAP_LOWERCASE
, buf
, 256,
928 (char*)loc
->locinfo
->pclmap
, 256);
929 LCMapStringA(lcid
[MSVCRT_LC_CTYPE
], LCMAP_UPPERCASE
, buf
, 256,
930 (char*)loc
->locinfo
->pcumap
, 256);
932 for(i
=0; i
<256; i
++) {
933 loc
->locinfo
->pclmap
[i
] = (i
>='A' && i
<='Z' ? i
-'A'+'a' : i
);
934 loc
->locinfo
->pcumap
[i
] = (i
>='a' && i
<='z' ? i
-'a'+'A' : i
);
938 _setmbcp_l(loc
->locinfo
->lc_id
[MSVCRT_LC_CTYPE
].wCodePage
, lcid
[MSVCRT_LC_CTYPE
], loc
->mbcinfo
);
940 if(lcid
[MSVCRT_LC_MONETARY
] && (category
==MSVCRT_LC_ALL
|| category
==MSVCRT_LC_MONETARY
)) {
941 if(update_threadlocinfo_category(lcid
[MSVCRT_LC_MONETARY
], loc
, MSVCRT_LC_MONETARY
)) {
942 MSVCRT__free_locale(loc
);
946 loc
->locinfo
->lconv_intl_refcount
= MSVCRT_malloc(sizeof(int));
947 loc
->locinfo
->lconv_mon_refcount
= MSVCRT_malloc(sizeof(int));
948 if(!loc
->locinfo
->lconv_intl_refcount
|| !loc
->locinfo
->lconv_mon_refcount
) {
949 MSVCRT__free_locale(loc
);
953 *loc
->locinfo
->lconv_intl_refcount
= 1;
954 *loc
->locinfo
->lconv_mon_refcount
= 1;
956 i
= GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_SINTLSYMBOL
957 |LOCALE_NOUSEROVERRIDE
, buf
, 256);
958 if(i
&& (loc
->locinfo
->lconv
->int_curr_symbol
= MSVCRT_malloc(i
)))
959 memcpy(loc
->locinfo
->lconv
->int_curr_symbol
, buf
, i
);
961 MSVCRT__free_locale(loc
);
965 i
= GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_SCURRENCY
966 |LOCALE_NOUSEROVERRIDE
, buf
, 256);
967 if(i
&& (loc
->locinfo
->lconv
->currency_symbol
= MSVCRT_malloc(i
)))
968 memcpy(loc
->locinfo
->lconv
->currency_symbol
, buf
, i
);
970 MSVCRT__free_locale(loc
);
974 i
= GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_SMONDECIMALSEP
975 |LOCALE_NOUSEROVERRIDE
, buf
, 256);
976 if(i
&& (loc
->locinfo
->lconv
->mon_decimal_point
= MSVCRT_malloc(i
)))
977 memcpy(loc
->locinfo
->lconv
->mon_decimal_point
, buf
, i
);
979 MSVCRT__free_locale(loc
);
983 i
= GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_SMONTHOUSANDSEP
984 |LOCALE_NOUSEROVERRIDE
, buf
, 256);
985 if(i
&& (loc
->locinfo
->lconv
->mon_thousands_sep
= MSVCRT_malloc(i
)))
986 memcpy(loc
->locinfo
->lconv
->mon_thousands_sep
, buf
, i
);
988 MSVCRT__free_locale(loc
);
992 i
= GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_SMONGROUPING
993 |LOCALE_NOUSEROVERRIDE
, buf
, 256);
995 i
= i
/2 + (buf
[i
-2]=='0'?0:1);
996 if(i
&& (loc
->locinfo
->lconv
->mon_grouping
= MSVCRT_malloc(i
))) {
997 for(i
=0; buf
[i
+1]==';'; i
+=2)
998 loc
->locinfo
->lconv
->mon_grouping
[i
/2] = buf
[i
]-'0';
999 loc
->locinfo
->lconv
->mon_grouping
[i
/2] = buf
[i
]-'0';
1001 loc
->locinfo
->lconv
->mon_grouping
[i
/2+1] = 127;
1003 MSVCRT__free_locale(loc
);
1007 i
= GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_SPOSITIVESIGN
1008 |LOCALE_NOUSEROVERRIDE
, buf
, 256);
1009 if(i
&& (loc
->locinfo
->lconv
->positive_sign
= MSVCRT_malloc(i
)))
1010 memcpy(loc
->locinfo
->lconv
->positive_sign
, buf
, i
);
1012 MSVCRT__free_locale(loc
);
1016 i
= GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_SNEGATIVESIGN
1017 |LOCALE_NOUSEROVERRIDE
, buf
, 256);
1018 if(i
&& (loc
->locinfo
->lconv
->negative_sign
= MSVCRT_malloc(i
)))
1019 memcpy(loc
->locinfo
->lconv
->negative_sign
, buf
, i
);
1021 MSVCRT__free_locale(loc
);
1025 if(GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_IINTLCURRDIGITS
1026 |LOCALE_NOUSEROVERRIDE
, buf
, 256))
1027 loc
->locinfo
->lconv
->int_frac_digits
= atoi(buf
);
1029 MSVCRT__free_locale(loc
);
1033 if(GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_ICURRDIGITS
1034 |LOCALE_NOUSEROVERRIDE
, buf
, 256))
1035 loc
->locinfo
->lconv
->frac_digits
= atoi(buf
);
1037 MSVCRT__free_locale(loc
);
1041 if(GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_IPOSSYMPRECEDES
1042 |LOCALE_NOUSEROVERRIDE
, buf
, 256))
1043 loc
->locinfo
->lconv
->p_cs_precedes
= atoi(buf
);
1045 MSVCRT__free_locale(loc
);
1049 if(GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_IPOSSEPBYSPACE
1050 |LOCALE_NOUSEROVERRIDE
, buf
, 256))
1051 loc
->locinfo
->lconv
->p_sep_by_space
= atoi(buf
);
1053 MSVCRT__free_locale(loc
);
1057 if(GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_INEGSYMPRECEDES
1058 |LOCALE_NOUSEROVERRIDE
, buf
, 256))
1059 loc
->locinfo
->lconv
->n_cs_precedes
= atoi(buf
);
1061 MSVCRT__free_locale(loc
);
1065 if(GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_INEGSEPBYSPACE
1066 |LOCALE_NOUSEROVERRIDE
, buf
, 256))
1067 loc
->locinfo
->lconv
->n_sep_by_space
= atoi(buf
);
1069 MSVCRT__free_locale(loc
);
1073 if(GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_IPOSSIGNPOSN
1074 |LOCALE_NOUSEROVERRIDE
, buf
, 256))
1075 loc
->locinfo
->lconv
->p_sign_posn
= atoi(buf
);
1077 MSVCRT__free_locale(loc
);
1081 if(GetLocaleInfoA(lcid
[MSVCRT_LC_MONETARY
], LOCALE_INEGSIGNPOSN
1082 |LOCALE_NOUSEROVERRIDE
, buf
, 256))
1083 loc
->locinfo
->lconv
->n_sign_posn
= atoi(buf
);
1085 MSVCRT__free_locale(loc
);
1089 loc
->locinfo
->lconv
->int_curr_symbol
= MSVCRT_malloc(sizeof(char));
1090 loc
->locinfo
->lconv
->currency_symbol
= MSVCRT_malloc(sizeof(char));
1091 loc
->locinfo
->lconv
->mon_decimal_point
= MSVCRT_malloc(sizeof(char));
1092 loc
->locinfo
->lconv
->mon_thousands_sep
= MSVCRT_malloc(sizeof(char));
1093 loc
->locinfo
->lconv
->mon_grouping
= MSVCRT_malloc(sizeof(char));
1094 loc
->locinfo
->lconv
->positive_sign
= MSVCRT_malloc(sizeof(char));
1095 loc
->locinfo
->lconv
->negative_sign
= MSVCRT_malloc(sizeof(char));
1097 if(!loc
->locinfo
->lconv
->int_curr_symbol
|| !loc
->locinfo
->lconv
->currency_symbol
1098 || !loc
->locinfo
->lconv
->mon_decimal_point
|| !loc
->locinfo
->lconv
->mon_thousands_sep
1099 || !loc
->locinfo
->lconv
->mon_grouping
|| !loc
->locinfo
->lconv
->positive_sign
1100 || !loc
->locinfo
->lconv
->negative_sign
) {
1101 MSVCRT__free_locale(loc
);
1105 loc
->locinfo
->lconv
->int_curr_symbol
[0] = '\0';
1106 loc
->locinfo
->lconv
->currency_symbol
[0] = '\0';
1107 loc
->locinfo
->lconv
->mon_decimal_point
[0] = '\0';
1108 loc
->locinfo
->lconv
->mon_thousands_sep
[0] = '\0';
1109 loc
->locinfo
->lconv
->mon_grouping
[0] = '\0';
1110 loc
->locinfo
->lconv
->positive_sign
[0] = '\0';
1111 loc
->locinfo
->lconv
->negative_sign
[0] = '\0';
1112 loc
->locinfo
->lconv
->int_frac_digits
= 127;
1113 loc
->locinfo
->lconv
->frac_digits
= 127;
1114 loc
->locinfo
->lconv
->p_cs_precedes
= 127;
1115 loc
->locinfo
->lconv
->p_sep_by_space
= 127;
1116 loc
->locinfo
->lconv
->n_cs_precedes
= 127;
1117 loc
->locinfo
->lconv
->n_sep_by_space
= 127;
1118 loc
->locinfo
->lconv
->p_sign_posn
= 127;
1119 loc
->locinfo
->lconv
->n_sign_posn
= 127;
1121 loc
->locinfo
->lc_category
[MSVCRT_LC_MONETARY
].locale
= MSVCRT__strdup("C");
1124 if(lcid
[MSVCRT_LC_NUMERIC
] && (category
==MSVCRT_LC_ALL
|| category
==MSVCRT_LC_NUMERIC
)) {
1125 if(update_threadlocinfo_category(lcid
[MSVCRT_LC_NUMERIC
], loc
, MSVCRT_LC_NUMERIC
)) {
1126 MSVCRT__free_locale(loc
);
1130 if(!loc
->locinfo
->lconv_intl_refcount
)
1131 loc
->locinfo
->lconv_intl_refcount
= MSVCRT_malloc(sizeof(int));
1132 loc
->locinfo
->lconv_num_refcount
= MSVCRT_malloc(sizeof(int));
1133 if(!loc
->locinfo
->lconv_intl_refcount
|| !loc
->locinfo
->lconv_num_refcount
) {
1134 MSVCRT__free_locale(loc
);
1138 *loc
->locinfo
->lconv_intl_refcount
= 1;
1139 *loc
->locinfo
->lconv_num_refcount
= 1;
1141 i
= GetLocaleInfoA(lcid
[MSVCRT_LC_NUMERIC
], LOCALE_SDECIMAL
1142 |LOCALE_NOUSEROVERRIDE
, buf
, 256);
1143 if(i
&& (loc
->locinfo
->lconv
->decimal_point
= MSVCRT_malloc(i
)))
1144 memcpy(loc
->locinfo
->lconv
->decimal_point
, buf
, i
);
1146 MSVCRT__free_locale(loc
);
1150 i
= GetLocaleInfoA(lcid
[MSVCRT_LC_NUMERIC
], LOCALE_STHOUSAND
1151 |LOCALE_NOUSEROVERRIDE
, buf
, 256);
1152 if(i
&& (loc
->locinfo
->lconv
->thousands_sep
= MSVCRT_malloc(i
)))
1153 memcpy(loc
->locinfo
->lconv
->thousands_sep
, buf
, i
);
1155 MSVCRT__free_locale(loc
);
1159 i
= GetLocaleInfoA(lcid
[MSVCRT_LC_NUMERIC
], LOCALE_SGROUPING
1160 |LOCALE_NOUSEROVERRIDE
, buf
, 256);
1162 i
= i
/2 + (buf
[i
-2]=='0'?0:1);
1163 if(i
&& (loc
->locinfo
->lconv
->grouping
= MSVCRT_malloc(i
))) {
1164 for(i
=0; buf
[i
+1]==';'; i
+=2)
1165 loc
->locinfo
->lconv
->grouping
[i
/2] = buf
[i
]-'0';
1166 loc
->locinfo
->lconv
->grouping
[i
/2] = buf
[i
]-'0';
1168 loc
->locinfo
->lconv
->grouping
[i
/2+1] = 127;
1170 MSVCRT__free_locale(loc
);
1174 loc
->locinfo
->lconv
->decimal_point
= MSVCRT_malloc(sizeof(char[2]));
1175 loc
->locinfo
->lconv
->thousands_sep
= MSVCRT_malloc(sizeof(char));
1176 loc
->locinfo
->lconv
->grouping
= MSVCRT_malloc(sizeof(char));
1177 if(!loc
->locinfo
->lconv
->decimal_point
|| !loc
->locinfo
->lconv
->thousands_sep
1178 || !loc
->locinfo
->lconv
->grouping
) {
1179 MSVCRT__free_locale(loc
);
1183 loc
->locinfo
->lconv
->decimal_point
[0] = '.';
1184 loc
->locinfo
->lconv
->decimal_point
[1] = '\0';
1185 loc
->locinfo
->lconv
->thousands_sep
[0] = '\0';
1186 loc
->locinfo
->lconv
->grouping
[0] = '\0';
1188 loc
->locinfo
->lc_category
[MSVCRT_LC_NUMERIC
].locale
= MSVCRT__strdup("C");
1191 if(lcid
[MSVCRT_LC_TIME
] && (category
==MSVCRT_LC_ALL
|| category
==MSVCRT_LC_TIME
)) {
1192 if(update_threadlocinfo_category(lcid
[MSVCRT_LC_TIME
], loc
, MSVCRT_LC_TIME
)) {
1193 MSVCRT__free_locale(loc
);
1197 loc
->locinfo
->lc_category
[MSVCRT_LC_TIME
].locale
= MSVCRT__strdup("C");
1199 size
= sizeof(MSVCRT___lc_time_data
);
1200 lcid_tmp
= lcid
[MSVCRT_LC_TIME
] ? lcid
[MSVCRT_LC_TIME
] : MAKELCID(LANG_ENGLISH
, SORT_DEFAULT
);
1201 for(i
=0; i
<sizeof(time_data
)/sizeof(time_data
[0]); i
++) {
1202 if(time_data
[i
]==LOCALE_SSHORTDATE
&& !lcid
[MSVCRT_LC_TIME
]) {
1203 size
+= sizeof(cloc_short_date
) + sizeof(cloc_short_dateW
);
1204 }else if(time_data
[i
]==LOCALE_SSHORTDATE
&& !lcid
[MSVCRT_LC_TIME
]) {
1205 size
+= sizeof(cloc_long_date
) + sizeof(cloc_long_dateW
);
1207 ret
= GetLocaleInfoA(lcid_tmp
, time_data
[i
]
1208 |LOCALE_NOUSEROVERRIDE
, NULL
, 0);
1210 MSVCRT__free_locale(loc
);
1215 ret
= GetLocaleInfoW(lcid_tmp
, time_data
[i
]
1216 |LOCALE_NOUSEROVERRIDE
, NULL
, 0);
1218 MSVCRT__free_locale(loc
);
1221 size
+= ret
*sizeof(MSVCRT_wchar_t
);
1225 loc
->locinfo
->lc_time_curr
= MSVCRT_malloc(size
);
1226 if(!loc
->locinfo
->lc_time_curr
) {
1227 MSVCRT__free_locale(loc
);
1232 for(i
=0; i
<sizeof(time_data
)/sizeof(time_data
[0]); i
++) {
1233 loc
->locinfo
->lc_time_curr
->str
.str
[i
] = &loc
->locinfo
->lc_time_curr
->data
[ret
];
1234 if(time_data
[i
]==LOCALE_SSHORTDATE
&& !lcid
[MSVCRT_LC_TIME
]) {
1235 memcpy(&loc
->locinfo
->lc_time_curr
->data
[ret
], cloc_short_date
, sizeof(cloc_short_date
));
1236 ret
+= sizeof(cloc_short_date
);
1237 }else if(time_data
[i
]==LOCALE_SSHORTDATE
&& !lcid
[MSVCRT_LC_TIME
]) {
1238 memcpy(&loc
->locinfo
->lc_time_curr
->data
[ret
], cloc_long_date
, sizeof(cloc_long_date
));
1239 ret
+= sizeof(cloc_long_date
);
1240 }else if(time_data
[i
]==LOCALE_STIMEFORMAT
&& !lcid
[MSVCRT_LC_TIME
]) {
1241 memcpy(&loc
->locinfo
->lc_time_curr
->data
[ret
], cloc_time
, sizeof(cloc_time
));
1242 ret
+= sizeof(cloc_time
);
1244 ret
+= GetLocaleInfoA(lcid_tmp
, time_data
[i
]|LOCALE_NOUSEROVERRIDE
,
1245 &loc
->locinfo
->lc_time_curr
->data
[ret
], size
-ret
);
1248 for(i
=0; i
<sizeof(time_data
)/sizeof(time_data
[0]); i
++) {
1249 loc
->locinfo
->lc_time_curr
->wstr
[i
] = (MSVCRT_wchar_t
*)&loc
->locinfo
->lc_time_curr
->data
[ret
];
1250 if(time_data
[i
]==LOCALE_SSHORTDATE
&& !lcid
[MSVCRT_LC_TIME
]) {
1251 memcpy(&loc
->locinfo
->lc_time_curr
->data
[ret
], cloc_short_dateW
, sizeof(cloc_short_dateW
));
1252 ret
+= sizeof(cloc_short_dateW
);
1253 }else if(time_data
[i
]==LOCALE_SSHORTDATE
&& !lcid
[MSVCRT_LC_TIME
]) {
1254 memcpy(&loc
->locinfo
->lc_time_curr
->data
[ret
], cloc_long_dateW
, sizeof(cloc_long_dateW
));
1255 ret
+= sizeof(cloc_long_dateW
);
1256 }else if(time_data
[i
]==LOCALE_STIMEFORMAT
&& !lcid
[MSVCRT_LC_TIME
]) {
1257 memcpy(&loc
->locinfo
->lc_time_curr
->data
[ret
], cloc_timeW
, sizeof(cloc_timeW
));
1258 ret
+= sizeof(cloc_timeW
);
1260 ret
+= GetLocaleInfoW(lcid_tmp
, time_data
[i
]|LOCALE_NOUSEROVERRIDE
,
1261 (MSVCRT_wchar_t
*)&loc
->locinfo
->lc_time_curr
->data
[ret
], size
-ret
)*sizeof(MSVCRT_wchar_t
);
1264 loc
->locinfo
->lc_time_curr
->lcid
= lcid
[MSVCRT_LC_TIME
];
1269 /*********************************************************************
1270 * setlocale (MSVCRT.@)
1272 char* CDECL
MSVCRT_setlocale(int category
, const char* locale
)
1274 MSVCRT__locale_t loc
;
1275 MSVCRT_pthreadlocinfo locinfo
= get_locinfo();
1277 if(category
<MSVCRT_LC_MIN
|| category
>MSVCRT_LC_MAX
)
1281 if(category
== MSVCRT_LC_ALL
)
1282 return construct_lc_all(locinfo
);
1284 return locinfo
->lc_category
[category
].locale
;
1287 loc
= MSVCRT__create_locale(category
, locale
);
1289 WARN("%d %s failed\n", category
, locale
);
1297 case MSVCRT_LC_COLLATE
:
1298 locinfo
->lc_collate_cp
= loc
->locinfo
->lc_collate_cp
;
1299 locinfo
->lc_handle
[MSVCRT_LC_COLLATE
] =
1300 loc
->locinfo
->lc_handle
[MSVCRT_LC_COLLATE
];
1301 swap_pointers((void**)&locinfo
->lc_category
[MSVCRT_LC_COLLATE
].locale
,
1302 (void**)&loc
->locinfo
->lc_category
[MSVCRT_LC_COLLATE
].locale
);
1303 swap_pointers((void**)&locinfo
->lc_category
[MSVCRT_LC_COLLATE
].refcount
,
1304 (void**)&loc
->locinfo
->lc_category
[MSVCRT_LC_COLLATE
].refcount
);
1306 if(category
!= MSVCRT_LC_ALL
)
1309 case MSVCRT_LC_CTYPE
:
1310 locinfo
->lc_handle
[MSVCRT_LC_CTYPE
] =
1311 loc
->locinfo
->lc_handle
[MSVCRT_LC_CTYPE
];
1312 swap_pointers((void**)&locinfo
->lc_category
[MSVCRT_LC_CTYPE
].locale
,
1313 (void**)&loc
->locinfo
->lc_category
[MSVCRT_LC_CTYPE
].locale
);
1314 swap_pointers((void**)&locinfo
->lc_category
[MSVCRT_LC_CTYPE
].refcount
,
1315 (void**)&loc
->locinfo
->lc_category
[MSVCRT_LC_CTYPE
].refcount
);
1317 locinfo
->lc_codepage
= loc
->locinfo
->lc_codepage
;
1318 locinfo
->lc_clike
= loc
->locinfo
->lc_clike
;
1319 locinfo
->mb_cur_max
= loc
->locinfo
->mb_cur_max
;
1321 swap_pointers((void**)&locinfo
->ctype1_refcount
,
1322 (void**)&loc
->locinfo
->ctype1_refcount
);
1323 swap_pointers((void**)&locinfo
->ctype1
, (void**)&loc
->locinfo
->ctype1
);
1324 swap_pointers((void**)&locinfo
->pctype
, (void**)&loc
->locinfo
->pctype
);
1325 swap_pointers((void**)&locinfo
->pclmap
, (void**)&loc
->locinfo
->pclmap
);
1326 swap_pointers((void**)&locinfo
->pcumap
, (void**)&loc
->locinfo
->pcumap
);
1328 if(category
!= MSVCRT_LC_ALL
)
1331 case MSVCRT_LC_MONETARY
:
1332 locinfo
->lc_handle
[MSVCRT_LC_MONETARY
] =
1333 loc
->locinfo
->lc_handle
[MSVCRT_LC_MONETARY
];
1334 swap_pointers((void**)&locinfo
->lc_category
[MSVCRT_LC_MONETARY
].locale
,
1335 (void**)&loc
->locinfo
->lc_category
[MSVCRT_LC_MONETARY
].locale
);
1336 swap_pointers((void**)&locinfo
->lc_category
[MSVCRT_LC_MONETARY
].refcount
,
1337 (void**)&loc
->locinfo
->lc_category
[MSVCRT_LC_MONETARY
].refcount
);
1339 swap_pointers((void**)&locinfo
->lconv
->int_curr_symbol
,
1340 (void**)&loc
->locinfo
->lconv
->int_curr_symbol
);
1341 swap_pointers((void**)&locinfo
->lconv
->currency_symbol
,
1342 (void**)&loc
->locinfo
->lconv
->currency_symbol
);
1343 swap_pointers((void**)&locinfo
->lconv
->mon_decimal_point
,
1344 (void**)&loc
->locinfo
->lconv
->mon_decimal_point
);
1345 swap_pointers((void**)&locinfo
->lconv
->mon_thousands_sep
,
1346 (void**)&loc
->locinfo
->lconv
->mon_thousands_sep
);
1347 swap_pointers((void**)&locinfo
->lconv
->mon_grouping
,
1348 (void**)&loc
->locinfo
->lconv
->mon_grouping
);
1349 swap_pointers((void**)&locinfo
->lconv
->positive_sign
,
1350 (void**)&loc
->locinfo
->lconv
->positive_sign
);
1351 swap_pointers((void**)&locinfo
->lconv
->negative_sign
,
1352 (void**)&loc
->locinfo
->lconv
->negative_sign
);
1353 locinfo
->lconv
->int_frac_digits
= loc
->locinfo
->lconv
->int_frac_digits
;
1354 locinfo
->lconv
->frac_digits
= loc
->locinfo
->lconv
->frac_digits
;
1355 locinfo
->lconv
->p_cs_precedes
= loc
->locinfo
->lconv
->p_cs_precedes
;
1356 locinfo
->lconv
->p_sep_by_space
= loc
->locinfo
->lconv
->p_sep_by_space
;
1357 locinfo
->lconv
->n_cs_precedes
= loc
->locinfo
->lconv
->n_cs_precedes
;
1358 locinfo
->lconv
->n_sep_by_space
= loc
->locinfo
->lconv
->n_sep_by_space
;
1359 locinfo
->lconv
->p_sign_posn
= loc
->locinfo
->lconv
->p_sign_posn
;
1360 locinfo
->lconv
->n_sign_posn
= loc
->locinfo
->lconv
->n_sign_posn
;
1362 if(category
!= MSVCRT_LC_ALL
)
1365 case MSVCRT_LC_NUMERIC
:
1366 locinfo
->lc_handle
[MSVCRT_LC_NUMERIC
] =
1367 loc
->locinfo
->lc_handle
[MSVCRT_LC_NUMERIC
];
1368 swap_pointers((void**)&locinfo
->lc_category
[MSVCRT_LC_NUMERIC
].locale
,
1369 (void**)&loc
->locinfo
->lc_category
[MSVCRT_LC_NUMERIC
].locale
);
1370 swap_pointers((void**)&locinfo
->lc_category
[MSVCRT_LC_NUMERIC
].refcount
,
1371 (void**)&loc
->locinfo
->lc_category
[MSVCRT_LC_NUMERIC
].refcount
);
1373 swap_pointers((void**)&locinfo
->lconv
->decimal_point
,
1374 (void**)&loc
->locinfo
->lconv
->decimal_point
);
1375 swap_pointers((void**)&locinfo
->lconv
->thousands_sep
,
1376 (void**)&loc
->locinfo
->lconv
->thousands_sep
);
1377 swap_pointers((void**)&locinfo
->lconv
->grouping
,
1378 (void**)&loc
->locinfo
->lconv
->grouping
);
1380 if(category
!= MSVCRT_LC_ALL
)
1383 case MSVCRT_LC_TIME
:
1384 locinfo
->lc_handle
[MSVCRT_LC_TIME
] =
1385 loc
->locinfo
->lc_handle
[MSVCRT_LC_TIME
];
1386 swap_pointers((void**)&locinfo
->lc_category
[MSVCRT_LC_TIME
].locale
,
1387 (void**)&loc
->locinfo
->lc_category
[MSVCRT_LC_TIME
].locale
);
1388 swap_pointers((void**)&locinfo
->lc_category
[MSVCRT_LC_TIME
].refcount
,
1389 (void**)&loc
->locinfo
->lc_category
[MSVCRT_LC_TIME
].refcount
);
1390 swap_pointers((void**)&locinfo
->lc_time_curr
,
1391 (void**)&loc
->locinfo
->lc_time_curr
);
1393 if(category
!= MSVCRT_LC_ALL
)
1397 MSVCRT__free_locale(loc
);
1400 if(locinfo
== MSVCRT_locale
->locinfo
) {
1403 MSVCRT___lc_codepage
= locinfo
->lc_codepage
;
1404 MSVCRT___lc_collate_cp
= locinfo
->lc_collate_cp
;
1405 MSVCRT___mb_cur_max
= locinfo
->mb_cur_max
;
1406 MSVCRT__pctype
= locinfo
->pctype
;
1407 for(i
=MSVCRT_LC_MIN
; i
<=MSVCRT_LC_MAX
; i
++)
1408 MSVCRT___lc_handle
[i
] = MSVCRT_locale
->locinfo
->lc_handle
[i
];
1411 if(category
== MSVCRT_LC_ALL
)
1412 return construct_lc_all(locinfo
);
1414 return locinfo
->lc_category
[category
].locale
;
1417 /* _configthreadlocale - not exported in native msvcrt */
1418 int CDECL
_configthreadlocale(int type
)
1420 thread_data_t
*data
= msvcrt_get_thread_data();
1421 MSVCRT__locale_t locale
;
1427 ret
= (data
->have_locale
? MSVCRT__ENABLE_PER_THREAD_LOCALE
: MSVCRT__DISABLE_PER_THREAD_LOCALE
);
1429 if(type
== MSVCRT__ENABLE_PER_THREAD_LOCALE
) {
1430 if(!data
->have_locale
) {
1431 /* Copy current global locale */
1432 locale
= MSVCRT__create_locale(MSVCRT_LC_ALL
, MSVCRT_setlocale(MSVCRT_LC_ALL
, NULL
));
1436 data
->locinfo
= locale
->locinfo
;
1437 data
->mbcinfo
= locale
->mbcinfo
;
1438 data
->have_locale
= TRUE
;
1439 MSVCRT_free(locale
);
1445 if(type
== MSVCRT__DISABLE_PER_THREAD_LOCALE
) {
1446 if(data
->have_locale
) {
1447 free_locinfo(data
->locinfo
);
1448 free_mbcinfo(data
->mbcinfo
);
1449 data
->locinfo
= MSVCRT_locale
->locinfo
;
1450 data
->mbcinfo
= MSVCRT_locale
->mbcinfo
;
1451 data
->have_locale
= FALSE
;
1463 BOOL
msvcrt_init_locale(void)
1468 MSVCRT_locale
= MSVCRT__create_locale(0, "C");
1473 MSVCRT___lc_codepage
= MSVCRT_locale
->locinfo
->lc_codepage
;
1474 MSVCRT___lc_collate_cp
= MSVCRT_locale
->locinfo
->lc_collate_cp
;
1475 MSVCRT___mb_cur_max
= MSVCRT_locale
->locinfo
->mb_cur_max
;
1476 MSVCRT__pctype
= MSVCRT_locale
->locinfo
->pctype
;
1477 for(i
=MSVCRT_LC_MIN
; i
<=MSVCRT_LC_MAX
; i
++)
1478 MSVCRT___lc_handle
[i
] = MSVCRT_locale
->locinfo
->lc_handle
[i
];
1479 _setmbcp(_MB_CP_ANSI
);