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"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
40 /* FIXME: Need to hold locale for each LC_* type and aggregate
41 * string to produce lc_all.
43 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
44 #define MAX_LOCALE_LENGTH 256
45 char MSVCRT_current_lc_all[MAX_LOCALE_LENGTH] = { 0 };
46 LCID MSVCRT_current_lc_all_lcid = 0;
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 };
52 #define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
53 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
55 #define MSVCRT_LEADBYTE 0x8000
57 /* Friendly country strings & iso codes for synonym support.
58 * Based on MS documentation for setlocale().
60 static const char * const _country_synonyms[] =
68 "United Kingdom","GB",
69 "United-Kingdom","GB",
78 /* INTERNAL: Map a synonym to an ISO code */
79 static void remap_synonym(char *name)
82 for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
84 if (!strcasecmp(_country_synonyms[i],name))
86 TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
87 name[0] = _country_synonyms[i+1][0];
88 name[1] = _country_synonyms[i+1][1];
95 /* Note: Flags are weighted in order of matching importance */
96 #define FOUND_LANGUAGE 0x4
97 #define FOUND_COUNTRY 0x2
98 #define FOUND_CODEPAGE 0x1
101 char search_language[MAX_ELEM_LEN];
102 char search_country[MAX_ELEM_LEN];
103 char search_codepage[MAX_ELEM_LEN];
104 char found_language[MAX_ELEM_LEN];
105 char found_country[MAX_ELEM_LEN];
106 char found_codepage[MAX_ELEM_LEN];
107 unsigned int match_flags;
108 LANGID found_lang_id;
111 #define CONTINUE_LOOKING TRUE
112 #define STOP_LOOKING FALSE
114 /* INTERNAL: Get and compare locale info with a given string */
115 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp)
118 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
119 if (!buff[0] || !cmp[0])
121 /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
122 return !strncasecmp(cmp, buff, strlen(cmp));
126 find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
128 locale_search_t *res = (locale_search_t *)lParam;
129 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
130 char buff[MAX_ELEM_LEN];
131 unsigned int flags = 0;
133 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
134 return CONTINUE_LOOKING;
137 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
138 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
139 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
141 TRACE(":Found language: %s->%s\n", res->search_language, buff);
142 flags |= FOUND_LANGUAGE;
143 memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
145 else if (res->match_flags & FOUND_LANGUAGE)
147 return CONTINUE_LOOKING;
151 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
152 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
153 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
155 TRACE("Found country:%s->%s\n", res->search_country, buff);
156 flags |= FOUND_COUNTRY;
157 memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
159 else if (res->match_flags & FOUND_COUNTRY)
161 return CONTINUE_LOOKING;
165 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
166 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
168 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
169 flags |= FOUND_CODEPAGE;
170 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
172 else if (res->match_flags & FOUND_CODEPAGE)
174 return CONTINUE_LOOKING;
177 if (flags > res->match_flags)
179 /* Found a better match than previously */
180 res->match_flags = flags;
181 res->found_lang_id = LangID;
183 if ((flags & (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE)) ==
184 (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE))
186 TRACE(":found exact locale match\n");
189 return CONTINUE_LOOKING;
192 extern int atoi(const char *);
194 /* Internal: Find the LCID for a locale specification */
195 static LCID MSVCRT_locale_to_LCID(locale_search_t* locale)
198 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
199 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
202 if (!locale->match_flags)
205 /* If we were given something that didn't match, fail */
206 if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
209 lcid = MAKELCID(locale->found_lang_id, SORT_DEFAULT);
211 /* Populate partial locale, translating LCID to locale string elements */
212 if (!locale->found_codepage[0])
214 /* Even if a codepage is not enumerated for a locale
215 * it can be set if valid */
216 if (locale->search_codepage[0])
218 if (IsValidCodePage(atoi(locale->search_codepage)))
219 memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
222 /* Special codepage values: OEM & ANSI */
223 if (strcasecmp(locale->search_codepage,"OCP"))
225 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
226 locale->found_codepage, MAX_ELEM_LEN);
228 if (strcasecmp(locale->search_codepage,"ACP"))
230 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
231 locale->found_codepage, MAX_ELEM_LEN);
236 if (!atoi(locale->found_codepage))
242 /* Prefer ANSI codepages if present */
243 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
244 locale->found_codepage, MAX_ELEM_LEN);
245 if (!locale->found_codepage[0] || !atoi(locale->found_codepage))
246 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
247 locale->found_codepage, MAX_ELEM_LEN);
250 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
251 locale->found_language, MAX_ELEM_LEN);
252 GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
253 locale->found_country, MAX_ELEM_LEN);
257 /* INTERNAL: Set ctype behaviour for a codepage */
258 static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
262 memset(&cp, 0, sizeof(CPINFO));
264 if (GetCPInfo(codepage, &cp))
268 unsigned char *traverse = (unsigned char *)cp.LeadByte;
270 memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
271 MSVCRT___lc_codepage = codepage;
272 MSVCRT___lc_collate_cp = codepage;
274 /* Switch ctype macros to MBCS if needed */
275 MSVCRT___mb_cur_max = cp.MaxCharSize;
277 /* Set remaining ctype flags: FIXME: faster way to do this? */
279 for (i = 0; i < 256; i++)
281 if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
284 GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
288 /* Set leadbyte flags */
289 while (traverse[0] || traverse[1])
291 for( i = traverse[0]; i <= traverse[1]; i++ )
292 MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
299 /*********************************************************************
300 * setlocale (MSVCRT.@)
302 char* CDECL MSVCRT_setlocale(int category, const char* locale)
306 int haveLang, haveCountry, haveCP;
310 TRACE("(%d %s)\n",category,locale);
312 if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
317 /* Report the current Locale */
318 return MSVCRT_current_lc_all;
323 if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
325 FIXME(":restore previous locale not implemented!\n");
326 /* FIXME: Easiest way to do this is parse the string and
327 * call this function recursively with its elements,
328 * Where they differ for each lc_ type.
331 return MSVCRT_current_lc_all;
334 /* Default Locale: Special case handling */
335 if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
337 MSVCRT_current_lc_all[0] = 'C';
338 MSVCRT_current_lc_all[1] = '\0';
339 MSVCRT___lc_codepage = GetACP();
340 MSVCRT___lc_collate_cp = GetACP();
344 lc_all = 1; /* Fall through all cases ... */
345 case MSVCRT_LC_COLLATE:
347 case MSVCRT_LC_CTYPE:
348 /* Restore C locale ctype info */
349 MSVCRT___mb_cur_max = 1;
350 memcpy(MSVCRT_current_ctype, MSVCRT__ctype, sizeof(MSVCRT__ctype));
352 case MSVCRT_LC_MONETARY:
354 case MSVCRT_LC_NUMERIC:
360 return MSVCRT_current_lc_all;
363 /* Get locale elements */
364 haveLang = haveCountry = haveCP = 0;
365 memset(&lc,0,sizeof(lc));
367 next = strchr(locale,'_');
368 if (next && next != locale)
371 memcpy(lc.search_language,locale,next-locale);
372 locale += next-locale+1;
375 next = strchr(locale,'.');
382 lstrcpynA(lc.search_codepage, locale, MAX_ELEM_LEN);
389 memcpy(lc.search_country,locale,next-locale);
390 locale += next-locale+1;
395 memcpy(lc.search_language,locale,next-locale);
396 locale += next-locale+1;
398 lstrcpynA(lc.search_codepage, locale, MAX_ELEM_LEN);
406 lstrcpynA(lc.search_country, locale, MAX_ELEM_LEN);
411 lstrcpynA(lc.search_language, locale, MAX_ELEM_LEN);
416 remap_synonym(lc.search_country);
418 if (haveCP && !haveCountry && !haveLang)
420 FIXME(":Codepage only locale not implemented\n");
421 /* FIXME: Use default lang/country and skip locale_to_LCID()
428 lcid = MSVCRT_locale_to_LCID(&lc);
430 TRACE(":found LCID %d\n",lcid);
438 MSVCRT_current_lc_all_lcid = lcid;
440 snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
441 lc.found_language,lc.found_country,lc.found_codepage);
445 lc_all = 1; /* Fall through all cases ... */
446 case MSVCRT_LC_COLLATE:
448 case MSVCRT_LC_CTYPE:
449 msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
451 case MSVCRT_LC_MONETARY:
453 case MSVCRT_LC_NUMERIC:
459 return MSVCRT_current_lc_all;
462 /*********************************************************************
463 * setlocale (MSVCRT.@)
465 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
467 static MSVCRT_wchar_t fake[] = {
468 'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
469 'S','t','a','t','e','s','.','1','2','5','2',0 };
471 FIXME("%d %s\n", category, debugstr_w(locale));
476 /*********************************************************************
477 * _Getdays (MSVCRT.@)
479 const char* CDECL _Getdays(void)
481 static const char MSVCRT_days[] = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
482 "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
483 /* FIXME: Use locale */
484 TRACE("(void) semi-stub\n");
488 /*********************************************************************
489 * _Getmonths (MSVCRT.@)
491 const char* CDECL _Getmonths(void)
493 static const char MSVCRT_months[] = ":Jan:January:Feb:February:Mar:March:Apr:"
494 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
495 "October:Nov:November:Dec:December";
496 /* FIXME: Use locale */
497 TRACE("(void) semi-stub\n");
498 return MSVCRT_months;
501 /*********************************************************************
502 * _Gettnames (MSVCRT.@)
504 const char* CDECL _Gettnames(void)
507 TRACE("(void) stub\n");
511 /*********************************************************************
512 * _Strftime (MSVCRT.@)
514 const char* CDECL _Strftime(char *out, unsigned int len, const char *fmt,
515 const void *tm, void *foo)
518 TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
522 /*********************************************************************
523 * __crtLCMapStringA (MSVCRT.@)
525 int CDECL __crtLCMapStringA(
526 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
527 int dstlen, unsigned int codepage, int xflag
529 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
530 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
531 /* FIXME: A bit incorrect. But msvcrt itself just converts its
532 * arguments to wide strings and then calls LCMapStringW
534 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
537 /*********************************************************************
538 * __crtCompareStringA (MSVCRT.@)
540 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
541 const char *src2, int len2 )
543 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
544 lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
545 /* FIXME: probably not entirely right */
546 return CompareStringA( lcid, flags, src1, len1, src2, len2 );
549 /*********************************************************************
550 * __crtCompareStringW (MSVCRT.@)
552 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
553 const MSVCRT_wchar_t *src2, int len2 )
555 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
556 lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
557 /* FIXME: probably not entirely right */
558 return CompareStringW( lcid, flags, src1, len1, src2, len2 );
561 /*********************************************************************
562 * __crtGetLocaleInfoW (MSVCRT.@)
564 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
566 FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
567 /* FIXME: probably not entirely right */
568 return GetLocaleInfoW( lcid, type, buffer, len );
571 /*********************************************************************
572 * localeconv (MSVCRT.@)
574 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void) {
576 struct lconv *ylconv;
577 static struct MSVCRT_lconv xlconv;
579 ylconv = localeconv();
581 #define X(x) xlconv.x = ylconv->x;
587 X(mon_decimal_point);
588 X(mon_thousands_sep);
603 /*********************************************************************
604 * __lconv_init (MSVCRT.@)
606 void CDECL __lconv_init(void)