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 /* 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 char MSVCRT_current_lc_all[MAX_LOCALE_LENGTH] = { 0 };
47 LCID MSVCRT_current_lc_all_lcid = 0;
48 int MSVCRT___lc_codepage = 0;
49 int MSVCRT___lc_collate_cp = 0;
50 HANDLE MSVCRT___lc_handle[MSVCRT_LC_MAX - MSVCRT_LC_MIN + 1] = { 0 };
51 unsigned char charmax = CHAR_MAX;
54 #define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
55 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
57 #define MSVCRT_LEADBYTE 0x8000
59 /* Friendly country strings & iso codes for synonym support.
60 * Based on MS documentation for setlocale().
62 static const char * const _country_synonyms[] =
70 "United Kingdom","GB",
71 "United-Kingdom","GB",
80 /* INTERNAL: Map a synonym to an ISO code */
81 static void remap_synonym(char *name)
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];
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
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;
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)
120 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
121 if (!buff[0] || !cmp[0])
123 /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
124 return !strncasecmp(cmp, buff, strlen(cmp));
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;
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;
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;
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");
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(locale_search_t* locale)
200 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
201 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
204 if (!locale->match_flags)
207 /* If we were given something that didn't match, fail */
208 if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
211 lcid = MAKELCID(locale->found_lang_id, SORT_DEFAULT);
213 /* Populate partial locale, translating LCID to locale string elements */
214 if (!locale->found_codepage[0])
216 /* Even if a codepage is not enumerated for a locale
217 * it can be set if valid */
218 if (locale->search_codepage[0])
220 if (IsValidCodePage(atoi(locale->search_codepage)))
221 memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
224 /* Special codepage values: OEM & ANSI */
225 if (strcasecmp(locale->search_codepage,"OCP"))
227 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
228 locale->found_codepage, MAX_ELEM_LEN);
230 else if (strcasecmp(locale->search_codepage,"ACP"))
232 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
233 locale->found_codepage, MAX_ELEM_LEN);
238 if (!atoi(locale->found_codepage))
244 /* Prefer ANSI codepages if present */
245 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
246 locale->found_codepage, MAX_ELEM_LEN);
247 if (!locale->found_codepage[0] || !atoi(locale->found_codepage))
248 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
249 locale->found_codepage, MAX_ELEM_LEN);
252 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
253 locale->found_language, MAX_ELEM_LEN);
254 GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
255 locale->found_country, MAX_ELEM_LEN);
259 /* INTERNAL: Set ctype behaviour for a codepage */
260 static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
264 memset(&cp, 0, sizeof(CPINFO));
266 if (GetCPInfo(codepage, &cp))
270 unsigned char *traverse = cp.LeadByte;
272 memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
273 MSVCRT___lc_codepage = codepage;
274 MSVCRT___lc_collate_cp = codepage;
276 /* Switch ctype macros to MBCS if needed */
277 MSVCRT___mb_cur_max = cp.MaxCharSize;
279 /* Set remaining ctype flags: FIXME: faster way to do this? */
281 for (i = 0; i < 256; i++)
283 if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
286 GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
290 /* Set leadbyte flags */
291 while (traverse[0] || traverse[1])
293 for( i = traverse[0]; i <= traverse[1]; i++ )
294 MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
300 /* INTERNAL: Set lc_handle, lc_id and lc_category in threadlocinfo struct */
301 BOOL update_threadlocinfo_category(LCID lcid, MSVCRT__locale_t loc, int category)
306 if(GetLocaleInfoA(lcid, LOCALE_ILANGUAGE, buf, 256)) {
309 loc->locinfo->lc_id[category].wLanguage = 0;
311 loc->locinfo->lc_id[category].wLanguage *= 16;
314 loc->locinfo->lc_id[category].wLanguage += *p-'0';
316 loc->locinfo->lc_id[category].wLanguage += *p-'a'+10;
321 loc->locinfo->lc_id[category].wCountry =
322 loc->locinfo->lc_id[category].wLanguage;
325 if(GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE, buf, 256))
326 loc->locinfo->lc_id[category].wCodePage = atoi(buf);
328 loc->locinfo->lc_handle[category] = lcid;
331 len += GetLocaleInfoA(lcid, LOCALE_SLANGUAGE, buf, 256);
333 len += GetLocaleInfoA(lcid, LOCALE_SCOUNTRY, &buf[len], 256-len);
335 len += GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE, &buf[len], 256-len);
337 loc->locinfo->lc_category[category].locale = MSVCRT_malloc(sizeof(char[len]));
338 loc->locinfo->lc_category[category].refcount = MSVCRT_malloc(sizeof(int));
339 if(!loc->locinfo->lc_category[category].locale
340 || !loc->locinfo->lc_category[category].refcount) {
341 MSVCRT_free(loc->locinfo->lc_category[category].locale);
342 MSVCRT_free(loc->locinfo->lc_category[category].refcount);
343 loc->locinfo->lc_category[category].locale = NULL;
344 loc->locinfo->lc_category[category].refcount = NULL;
347 memcpy(loc->locinfo->lc_category[category].locale, buf, sizeof(char[len]));
348 *loc->locinfo->lc_category[category].refcount = 1;
354 /*********************************************************************
355 * setlocale (MSVCRT.@)
357 char* CDECL MSVCRT_setlocale(int category, const char* locale)
361 int haveLang, haveCountry, haveCP;
365 TRACE("(%d %s)\n",category,locale);
367 if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
372 /* Report the current Locale */
373 return MSVCRT_current_lc_all;
378 if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
380 FIXME(":restore previous locale not implemented!\n");
381 /* FIXME: Easiest way to do this is parse the string and
382 * call this function recursively with its elements,
383 * Where they differ for each lc_ type.
386 return MSVCRT_current_lc_all;
389 /* Default Locale: Special case handling */
390 if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
392 MSVCRT_current_lc_all[0] = 'C';
393 MSVCRT_current_lc_all[1] = '\0';
394 MSVCRT___lc_codepage = GetACP();
395 MSVCRT___lc_collate_cp = GetACP();
399 lc_all = 1; /* Fall through all cases ... */
400 case MSVCRT_LC_COLLATE:
402 case MSVCRT_LC_CTYPE:
403 /* Restore C locale ctype info */
404 MSVCRT___mb_cur_max = 1;
405 memcpy(MSVCRT_current_ctype, MSVCRT__ctype, sizeof(MSVCRT__ctype));
407 case MSVCRT_LC_MONETARY:
409 case MSVCRT_LC_NUMERIC:
415 return MSVCRT_current_lc_all;
418 /* Get locale elements */
419 haveLang = haveCountry = haveCP = 0;
420 memset(&lc,0,sizeof(lc));
422 next = strchr(locale,'_');
423 if (next && next != locale)
426 memcpy(lc.search_language,locale,next-locale);
427 locale += next-locale+1;
430 next = strchr(locale,'.');
437 lstrcpynA(lc.search_codepage, locale, MAX_ELEM_LEN);
444 memcpy(lc.search_country,locale,next-locale);
445 locale += next-locale+1;
450 memcpy(lc.search_language,locale,next-locale);
451 locale += next-locale+1;
453 lstrcpynA(lc.search_codepage, locale, MAX_ELEM_LEN);
461 lstrcpynA(lc.search_country, locale, MAX_ELEM_LEN);
466 lstrcpynA(lc.search_language, locale, MAX_ELEM_LEN);
471 remap_synonym(lc.search_country);
473 if (haveCP && !haveCountry && !haveLang)
475 FIXME(":Codepage only locale not implemented\n");
476 /* FIXME: Use default lang/country and skip locale_to_LCID()
483 lcid = MSVCRT_locale_to_LCID(&lc);
485 TRACE(":found LCID %d\n",lcid);
493 MSVCRT_current_lc_all_lcid = lcid;
495 snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
496 lc.found_language,lc.found_country,lc.found_codepage);
500 lc_all = 1; /* Fall through all cases ... */
501 case MSVCRT_LC_COLLATE:
503 case MSVCRT_LC_CTYPE:
504 msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
506 case MSVCRT_LC_MONETARY:
508 case MSVCRT_LC_NUMERIC:
514 return MSVCRT_current_lc_all;
517 /*********************************************************************
518 * wsetlocale (MSVCRT.@)
520 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
522 static MSVCRT_wchar_t fake[] = {
523 'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
524 'S','t','a','t','e','s','.','1','2','5','2',0 };
526 FIXME("%d %s\n", category, debugstr_w(locale));
531 /*********************************************************************
532 * _Getdays (MSVCRT.@)
534 const char* CDECL _Getdays(void)
536 static const char MSVCRT_days[] = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
537 "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
538 /* FIXME: Use locale */
539 TRACE("(void) semi-stub\n");
543 /*********************************************************************
544 * _Getmonths (MSVCRT.@)
546 const char* CDECL _Getmonths(void)
548 static const char MSVCRT_months[] = ":Jan:January:Feb:February:Mar:March:Apr:"
549 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
550 "October:Nov:November:Dec:December";
551 /* FIXME: Use locale */
552 TRACE("(void) semi-stub\n");
553 return MSVCRT_months;
556 /*********************************************************************
557 * _Gettnames (MSVCRT.@)
559 const char* CDECL _Gettnames(void)
562 TRACE("(void) stub\n");
566 /*********************************************************************
567 * _Strftime (MSVCRT.@)
569 const char* CDECL _Strftime(char *out, unsigned int len, const char *fmt,
570 const void *tm, void *foo)
573 TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
577 /*********************************************************************
578 * __crtLCMapStringA (MSVCRT.@)
580 int CDECL __crtLCMapStringA(
581 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
582 int dstlen, unsigned int codepage, int xflag
584 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
585 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
586 /* FIXME: A bit incorrect. But msvcrt itself just converts its
587 * arguments to wide strings and then calls LCMapStringW
589 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
592 /*********************************************************************
593 * __crtCompareStringA (MSVCRT.@)
595 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
596 const char *src2, int len2 )
598 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
599 lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
600 /* FIXME: probably not entirely right */
601 return CompareStringA( lcid, flags, src1, len1, src2, len2 );
604 /*********************************************************************
605 * __crtCompareStringW (MSVCRT.@)
607 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
608 const MSVCRT_wchar_t *src2, int len2 )
610 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
611 lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
612 /* FIXME: probably not entirely right */
613 return CompareStringW( lcid, flags, src1, len1, src2, len2 );
616 /*********************************************************************
617 * __crtGetLocaleInfoW (MSVCRT.@)
619 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
621 FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
622 /* FIXME: probably not entirely right */
623 return GetLocaleInfoW( lcid, type, buffer, len );
626 /*********************************************************************
627 * localeconv (MSVCRT.@)
629 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
631 static struct MSVCRT_lconv xlconv;
632 struct lconv *ylconv = localeconv();
634 xlconv.decimal_point = ylconv->decimal_point;
635 xlconv.thousands_sep = ylconv->thousands_sep;
636 xlconv.grouping = ylconv->grouping; /* FIXME: fixup charmax here too */
637 xlconv.int_curr_symbol = ylconv->int_curr_symbol;
638 xlconv.currency_symbol = ylconv->currency_symbol;
639 xlconv.mon_decimal_point = ylconv->mon_decimal_point;
640 xlconv.mon_thousands_sep = ylconv->mon_thousands_sep;
641 xlconv.mon_grouping = ylconv->mon_grouping;
642 xlconv.positive_sign = ylconv->positive_sign;
643 xlconv.negative_sign = ylconv->negative_sign;
644 xlconv.int_frac_digits = ylconv->int_frac_digits;
645 xlconv.frac_digits = ylconv->frac_digits;
646 xlconv.p_cs_precedes = ylconv->p_cs_precedes;
647 xlconv.p_sep_by_space = ylconv->p_sep_by_space;
648 xlconv.n_cs_precedes = ylconv->n_cs_precedes;
649 xlconv.n_sep_by_space = ylconv->n_sep_by_space;
650 xlconv.p_sign_posn = ylconv->p_sign_posn;
651 xlconv.n_sign_posn = ylconv->n_sign_posn;
653 if (ylconv->int_frac_digits == CHAR_MAX) xlconv.int_frac_digits = charmax;
654 if (ylconv->frac_digits == CHAR_MAX) xlconv.frac_digits = charmax;
655 if (ylconv->p_cs_precedes == CHAR_MAX) xlconv.p_cs_precedes = charmax;
656 if (ylconv->p_sep_by_space == CHAR_MAX) xlconv.p_sep_by_space = charmax;
657 if (ylconv->n_cs_precedes == CHAR_MAX) xlconv.n_cs_precedes = charmax;
658 if (ylconv->n_sep_by_space == CHAR_MAX) xlconv.n_sep_by_space = charmax;
659 if (ylconv->p_sign_posn == CHAR_MAX) xlconv.p_sign_posn = charmax;
660 if (ylconv->n_sign_posn == CHAR_MAX) xlconv.n_sign_posn = charmax;
665 /*********************************************************************
666 * __lconv_init (MSVCRT.@)
668 void CDECL __lconv_init(void)
670 /* this is used to make chars unsigned */
674 /*********************************************************************
675 * ___lc_handle_func (MSVCRT.@)
677 HANDLE * CDECL ___lc_handle_func(void)
679 return MSVCRT___lc_handle;
682 /*********************************************************************
683 * ___lc_codepage_func (MSVCRT.@)
685 int CDECL ___lc_codepage_func(void)
687 return MSVCRT___lc_codepage;
690 /*********************************************************************
691 * ___lc_collate_cp_func (MSVCRT.@)
693 int CDECL ___lc_collate_cp_func(void)
695 return MSVCRT___lc_collate_cp;
698 /* _free_locale - not exported in native msvcrt */
699 void CDECL _free_locale(MSVCRT__locale_t locale)
704 for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
705 if(locale->locinfo->lc_category[i].refcount) {
706 MSVCRT_free(locale->locinfo->lc_category[i].locale);
707 MSVCRT_free(locale->locinfo->lc_category[i].refcount);
709 MSVCRT_free(locale->locinfo->lc_category[i].locale);
714 if(locale->locinfo->lconv) {
715 MSVCRT_free(locale->locinfo->lconv->decimal_point);
716 MSVCRT_free(locale->locinfo->lconv->thousands_sep);
717 MSVCRT_free(locale->locinfo->lconv->grouping);
718 MSVCRT_free(locale->locinfo->lconv->int_curr_symbol);
719 MSVCRT_free(locale->locinfo->lconv->currency_symbol);
720 MSVCRT_free(locale->locinfo->lconv->mon_decimal_point);
721 MSVCRT_free(locale->locinfo->lconv->mon_thousands_sep);
722 MSVCRT_free(locale->locinfo->lconv->mon_grouping);
723 MSVCRT_free(locale->locinfo->lconv->positive_sign);
724 MSVCRT_free(locale->locinfo->lconv->negative_sign);
726 MSVCRT_free(locale->locinfo->lconv_intl_refcount);
727 MSVCRT_free(locale->locinfo->lconv_num_refcount);
728 MSVCRT_free(locale->locinfo->lconv_mon_refcount);
729 MSVCRT_free(locale->locinfo->lconv);
731 MSVCRT_free(locale->locinfo->ctype1_refcount);
732 MSVCRT_free(locale->locinfo->ctype1);
734 MSVCRT_free(locale->locinfo->pclmap);
735 MSVCRT_free(locale->locinfo->pcumap);
737 MSVCRT_free(locale->locinfo);
738 MSVCRT_free(locale->mbcinfo);
742 /* _create_locale - not exported in native msvcrt */
743 MSVCRT__locale_t _create_locale(int category, const char *locale)
745 MSVCRT__locale_t loc;
747 char buf[256], *localeC;
750 TRACE("(%d %s)\n", category, locale);
752 if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
755 if(locale[0]=='C' && !locale[1])
758 lcid = GetSystemDefaultLCID();
760 locale_search_t search;
763 memset(&search, 0, sizeof(locale_search_t));
765 cp = strchr(locale, '.');
766 region = strchr(locale, '_');
768 lstrcpynA(search.search_language, locale, MAX_ELEM_LEN);
770 lstrcpynA(search.search_country, region+1, MAX_ELEM_LEN);
771 if(region-locale < MAX_ELEM_LEN)
772 search.search_language[region-locale] = '\0';
774 search.search_country[0] = '\0';
777 lstrcpynA(search.search_codepage, cp+1, MAX_ELEM_LEN);
778 if(cp-region < MAX_ELEM_LEN)
779 search.search_country[cp-region] = '\0';
780 if(cp-locale < MAX_ELEM_LEN)
781 search.search_language[cp-locale] = '\0';
783 search.search_codepage[0] = '\0';
785 lcid = MSVCRT_locale_to_LCID(&search);
790 loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
794 loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
800 loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
802 MSVCRT_free(loc->locinfo);
807 memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
808 memset(loc->mbcinfo, 0, sizeof(MSVCRT_threadmbcinfo));
810 if(category!=MSVCRT_LC_ALL || !lcid) {
811 localeC = MSVCRT_malloc(sizeof(char[2]));
817 for(i=MSVCRT_LC_ALL+1; i<=MSVCRT_LC_MAX; i++)
818 loc->locinfo->lc_category[i].locale = localeC;
825 loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
826 if(!loc->locinfo->lconv) {
830 memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
832 loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
833 loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
834 if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
839 loc->locinfo->refcount = 1;
841 if(lcid && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
842 if(update_threadlocinfo_category(lcid, loc, MSVCRT_LC_COLLATE)) {
848 if(lcid && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
851 if(update_threadlocinfo_category(lcid, loc, MSVCRT_LC_CTYPE)) {
856 loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
857 loc->locinfo->lc_clike = 1;
858 if(!GetCPInfo(loc->locinfo->lc_codepage, &cp)) {
862 loc->locinfo->mb_cur_max = cp.MaxCharSize;
864 loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
865 loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
866 if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
871 *loc->locinfo->ctype1_refcount = 1;
872 loc->locinfo->ctype1[0] = 0;
873 loc->locinfo->pctype = loc->locinfo->ctype1+1;
875 buf[1] = buf[2] = '\0';
876 for(i=1; i<257; i++) {
879 GetStringTypeA(lcid, CT_CTYPE1, buf, 1, loc->locinfo->ctype1+i);
880 loc->locinfo->ctype1[i] |= 0x200;
883 loc->locinfo->lc_clike = 1;
884 loc->locinfo->mb_cur_max = 1;
885 loc->locinfo->pctype = MSVCRT__ctype+1;
891 LCMapStringA(lcid, LCMAP_LOWERCASE, buf, 256, (char*)loc->locinfo->pclmap, 256);
892 LCMapStringA(lcid, LCMAP_UPPERCASE, buf, 256, (char*)loc->locinfo->pcumap, 256);
894 loc->mbcinfo->refcount = 1;
895 loc->mbcinfo->mbcodepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
897 for(i=0; i<256; i++) {
898 if(loc->locinfo->pclmap[i] != i) {
899 loc->mbcinfo->mbctype[i+1] |= 0x10;
900 loc->mbcinfo->mbcasemap[i] = loc->locinfo->pclmap[i];
901 } else if(loc->locinfo->pcumap[i] != i) {
902 loc->mbcinfo->mbctype[i+1] |= 0x20;
903 loc->mbcinfo->mbcasemap[i] = loc->locinfo->pcumap[i];
907 if(lcid && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
908 if(update_threadlocinfo_category(lcid, loc, MSVCRT_LC_MONETARY)) {
913 loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
914 loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
915 if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
920 *loc->locinfo->lconv_intl_refcount = 1;
921 *loc->locinfo->lconv_mon_refcount = 1;
923 i = GetLocaleInfoA(lcid, LOCALE_SINTLSYMBOL, buf, 256);
924 if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char[i]))))
925 memcpy(loc->locinfo->lconv->int_curr_symbol, buf, sizeof(char[i]));
931 i = GetLocaleInfoA(lcid, LOCALE_SCURRENCY, buf, 256);
932 if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char[i]))))
933 memcpy(loc->locinfo->lconv->currency_symbol, buf, sizeof(char[i]));
939 i = GetLocaleInfoA(lcid, LOCALE_SMONDECIMALSEP, buf, 256);
940 if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char[i]))))
941 memcpy(loc->locinfo->lconv->mon_decimal_point, buf, sizeof(char[i]));
947 i = GetLocaleInfoA(lcid, LOCALE_SMONTHOUSANDSEP, buf, 256);
948 if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
949 memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, sizeof(char[i]));
955 i = GetLocaleInfoA(lcid, LOCALE_SMONGROUPING, buf, 256);
957 i = i/2 + (buf[i-2]=='0'?0:1);
958 if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char[i])))) {
959 for(i=0; buf[i+1]==';'; i+=2)
960 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
961 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
963 loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
969 i = GetLocaleInfoA(lcid, LOCALE_SPOSITIVESIGN, buf, 256);
970 if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char[i]))))
971 memcpy(loc->locinfo->lconv->positive_sign, buf, sizeof(char[i]));
977 i = GetLocaleInfoA(lcid, LOCALE_SNEGATIVESIGN, buf, 256);
978 if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char[i]))))
979 memcpy(loc->locinfo->lconv->negative_sign, buf, sizeof(char[i]));
985 if(GetLocaleInfoA(lcid, LOCALE_IINTLCURRDIGITS, buf, 256))
986 loc->locinfo->lconv->int_frac_digits = atoi(buf);
992 if(GetLocaleInfoA(lcid, LOCALE_ICURRDIGITS, buf, 256))
993 loc->locinfo->lconv->frac_digits = atoi(buf);
999 if(GetLocaleInfoA(lcid, LOCALE_IPOSSYMPRECEDES, buf, 256))
1000 loc->locinfo->lconv->p_cs_precedes = atoi(buf);
1006 if(GetLocaleInfoA(lcid, LOCALE_IPOSSEPBYSPACE, buf, 256))
1007 loc->locinfo->lconv->p_sep_by_space = atoi(buf);
1013 if(GetLocaleInfoA(lcid, LOCALE_INEGSYMPRECEDES, buf, 256))
1014 loc->locinfo->lconv->n_cs_precedes = atoi(buf);
1020 if(GetLocaleInfoA(lcid, LOCALE_INEGSEPBYSPACE, buf, 256))
1021 loc->locinfo->lconv->n_sep_by_space = atoi(buf);
1027 if(GetLocaleInfoA(lcid, LOCALE_IPOSSIGNPOSN, buf, 256))
1028 loc->locinfo->lconv->p_sign_posn = atoi(buf);
1034 if(GetLocaleInfoA(lcid, LOCALE_INEGSIGNPOSN, buf, 256))
1035 loc->locinfo->lconv->n_sign_posn = atoi(buf);
1041 loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
1042 loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
1043 loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
1044 loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
1045 loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
1046 loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
1047 loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
1049 if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
1050 || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
1051 || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
1052 || !loc->locinfo->lconv->negative_sign) {
1057 loc->locinfo->lconv->int_curr_symbol[0] = '\0';
1058 loc->locinfo->lconv->currency_symbol[0] = '\0';
1059 loc->locinfo->lconv->mon_decimal_point[0] = '\0';
1060 loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
1061 loc->locinfo->lconv->mon_grouping[0] = '\0';
1062 loc->locinfo->lconv->positive_sign[0] = '\0';
1063 loc->locinfo->lconv->negative_sign[0] = '\0';
1064 loc->locinfo->lconv->int_frac_digits = 127;
1065 loc->locinfo->lconv->frac_digits = 127;
1066 loc->locinfo->lconv->p_cs_precedes = 127;
1067 loc->locinfo->lconv->p_sep_by_space = 127;
1068 loc->locinfo->lconv->n_cs_precedes = 127;
1069 loc->locinfo->lconv->n_sep_by_space = 127;
1070 loc->locinfo->lconv->p_sign_posn = 127;
1071 loc->locinfo->lconv->n_sign_posn = 127;
1074 if(lcid && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
1075 if(update_threadlocinfo_category(lcid, loc, MSVCRT_LC_NUMERIC)) {
1080 if(!loc->locinfo->lconv_intl_refcount)
1081 loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1082 loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1083 if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
1088 *loc->locinfo->lconv_intl_refcount = 1;
1089 *loc->locinfo->lconv_num_refcount = 1;
1091 i = GetLocaleInfoA(lcid, LOCALE_SDECIMAL, buf, 256);
1092 if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[i]))))
1093 memcpy(loc->locinfo->lconv->decimal_point, buf, sizeof(char[i]));
1099 i = GetLocaleInfoA(lcid, LOCALE_STHOUSAND, buf, 256);
1100 if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
1101 memcpy(loc->locinfo->lconv->thousands_sep, buf, sizeof(char[i]));
1107 i = GetLocaleInfoA(lcid, LOCALE_SGROUPING, buf, 256);
1109 i = i/2 + (buf[i-2]=='0'?0:1);
1110 if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char[i])))) {
1111 for(i=0; buf[i+1]==';'; i+=2)
1112 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1113 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1115 loc->locinfo->lconv->grouping[i/2+1] = 127;
1121 loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1122 loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1123 loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1124 if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
1125 || !loc->locinfo->lconv->grouping) {
1130 loc->locinfo->lconv->decimal_point[0] = '.';
1131 loc->locinfo->lconv->decimal_point[1] = '\0';
1132 loc->locinfo->lconv->thousands_sep[0] = '\0';
1133 loc->locinfo->lconv->grouping[0] = '\0';
1136 if(lcid && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1137 if(update_threadlocinfo_category(lcid, loc, MSVCRT_LC_TIME)) {