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"
35 #include "msvcrt/mbctype.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];
47 LCID MSVCRT_current_lc_all_lcid;
48 int MSVCRT___lc_codepage;
49 int MSVCRT___lc_collate_cp;
50 HANDLE MSVCRT___lc_handle[MSVCRT_LC_MAX - MSVCRT_LC_MIN + 1];
53 #define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
54 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
56 /* ctype data modified when the locale changes */
57 extern WORD MSVCRT__ctype [257];
58 extern WORD MSVCRT_current_ctype[257];
59 extern WORD* MSVCRT__pctype;
61 /* mbctype data modified when the locale changes */
62 extern int MSVCRT___mb_cur_max;
64 #define MSVCRT_LEADBYTE 0x8000
66 /* Friendly country strings & iso codes for synonym support.
67 * Based on MS documentation for setlocale().
69 static const char * const _country_synonyms[] =
77 "United Kingdom","GB",
78 "United-Kingdom","GB",
87 /* INTERNAL: Map a synonym to an ISO code */
88 static void remap_synonym(char *name)
91 for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
93 if (!strcasecmp(_country_synonyms[i],name))
95 TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
96 name[0] = _country_synonyms[i+1][0];
97 name[1] = _country_synonyms[i+1][1];
104 /* Note: Flags are weighted in order of matching importance */
105 #define FOUND_LANGUAGE 0x4
106 #define FOUND_COUNTRY 0x2
107 #define FOUND_CODEPAGE 0x1
110 char search_language[MAX_ELEM_LEN];
111 char search_country[MAX_ELEM_LEN];
112 char search_codepage[MAX_ELEM_LEN];
113 char found_language[MAX_ELEM_LEN];
114 char found_country[MAX_ELEM_LEN];
115 char found_codepage[MAX_ELEM_LEN];
116 unsigned int match_flags;
117 LANGID found_lang_id;
120 #define CONTINUE_LOOKING TRUE
121 #define STOP_LOOKING FALSE
123 /* INTERNAL: Get and compare locale info with a given string */
124 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp)
127 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
128 if (!buff[0] || !cmp[0])
130 /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
131 return !strncasecmp(cmp, buff, strlen(cmp));
135 find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
137 locale_search_t *res = (locale_search_t *)lParam;
138 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
139 char buff[MAX_ELEM_LEN];
140 unsigned int flags = 0;
142 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
143 return CONTINUE_LOOKING;
146 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
147 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
148 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
150 TRACE(":Found language: %s->%s\n", res->search_language, buff);
151 flags |= FOUND_LANGUAGE;
152 memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
154 else if (res->match_flags & FOUND_LANGUAGE)
156 return CONTINUE_LOOKING;
160 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
161 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
162 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
164 TRACE("Found country:%s->%s\n", res->search_country, buff);
165 flags |= FOUND_COUNTRY;
166 memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
168 else if (res->match_flags & FOUND_COUNTRY)
170 return CONTINUE_LOOKING;
174 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
175 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
177 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
178 flags |= FOUND_CODEPAGE;
179 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
181 else if (res->match_flags & FOUND_CODEPAGE)
183 return CONTINUE_LOOKING;
186 if (flags > res->match_flags)
188 /* Found a better match than previously */
189 res->match_flags = flags;
190 res->found_lang_id = LangID;
192 if (flags & (FOUND_LANGUAGE & FOUND_COUNTRY & FOUND_CODEPAGE))
194 TRACE(":found exact locale match\n");
197 return CONTINUE_LOOKING;
200 extern int atoi(const char *);
202 /* Internal: Find the LCID for a locale specification */
203 static LCID MSVCRT_locale_to_LCID(locale_search_t* locale)
206 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
207 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
210 if (!locale->match_flags)
213 /* If we were given something that didn't match, fail */
214 if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
217 lcid = MAKELCID(locale->found_lang_id, SORT_DEFAULT);
219 /* Populate partial locale, translating LCID to locale string elements */
220 if (!locale->found_codepage[0])
222 /* Even if a codepage is not enumerated for a locale
223 * it can be set if valid */
224 if (locale->search_codepage[0])
226 if (IsValidCodePage(atoi(locale->search_codepage)))
227 memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
230 /* Special codepage values: OEM & ANSI */
231 if (strcasecmp(locale->search_codepage,"OCP"))
233 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
234 locale->found_codepage, MAX_ELEM_LEN);
236 if (strcasecmp(locale->search_codepage,"ACP"))
238 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
239 locale->found_codepage, MAX_ELEM_LEN);
244 if (!atoi(locale->found_codepage))
250 /* Prefer ANSI codepages if present */
251 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
252 locale->found_codepage, MAX_ELEM_LEN);
253 if (!locale->found_codepage[0] || !atoi(locale->found_codepage))
254 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
255 locale->found_codepage, MAX_ELEM_LEN);
258 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
259 locale->found_language, MAX_ELEM_LEN);
260 GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
261 locale->found_country, MAX_ELEM_LEN);
265 /* INTERNAL: Set ctype behaviour for a codepage */
266 static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
270 memset(&cp, 0, sizeof(CPINFO));
272 if (GetCPInfo(codepage, &cp))
276 unsigned char *traverse = (unsigned char *)cp.LeadByte;
278 memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
279 MSVCRT___lc_codepage = codepage;
280 MSVCRT___lc_collate_cp = codepage;
282 /* Switch ctype macros to MBCS if needed */
283 MSVCRT___mb_cur_max = cp.MaxCharSize;
285 /* Set remaining ctype flags: FIXME: faster way to do this? */
287 for (i = 0; i < 256; i++)
289 if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
292 GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
296 /* Set leadbyte flags */
297 while (traverse[0] || traverse[1])
299 for( i = traverse[0]; i <= traverse[1]; i++ )
300 MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
307 /*********************************************************************
308 * setlocale (MSVCRT.@)
310 char* CDECL MSVCRT_setlocale(int category, const char* locale)
314 int haveLang, haveCountry, haveCP;
318 TRACE("(%d %s)\n",category,locale);
320 if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
325 /* Report the current Locale */
326 return MSVCRT_current_lc_all;
331 if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
333 FIXME(":restore previous locale not implemented!\n");
334 /* FIXME: Easiest way to do this is parse the string and
335 * call this function recursively with its elements,
336 * Where they differ for each lc_ type.
339 return MSVCRT_current_lc_all;
342 /* Default Locale: Special case handling */
343 if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
345 MSVCRT_current_lc_all[0] = 'C';
346 MSVCRT_current_lc_all[1] = '\0';
347 MSVCRT___lc_codepage = GetACP();
348 MSVCRT___lc_collate_cp = GetACP();
352 lc_all = 1; /* Fall through all cases ... */
353 case MSVCRT_LC_COLLATE:
355 case MSVCRT_LC_CTYPE:
356 /* Restore C locale ctype info */
357 MSVCRT___mb_cur_max = 1;
358 memcpy(MSVCRT_current_ctype, MSVCRT__ctype, sizeof(MSVCRT__ctype));
360 case MSVCRT_LC_MONETARY:
362 case MSVCRT_LC_NUMERIC:
368 return MSVCRT_current_lc_all;
371 /* Get locale elements */
372 haveLang = haveCountry = haveCP = 0;
373 memset(&lc,0,sizeof(lc));
375 next = strchr(locale,'_');
376 if (next && next != locale)
379 memcpy(lc.search_language,locale,next-locale);
380 locale += next-locale+1;
383 next = strchr(locale,'.');
390 lstrcpynA(lc.search_codepage, locale, MAX_ELEM_LEN);
397 memcpy(lc.search_country,locale,next-locale);
398 locale += next-locale+1;
403 memcpy(lc.search_language,locale,next-locale);
404 locale += next-locale+1;
406 lstrcpynA(lc.search_codepage, locale, MAX_ELEM_LEN);
414 lstrcpynA(lc.search_country, locale, MAX_ELEM_LEN);
419 lstrcpynA(lc.search_language, locale, MAX_ELEM_LEN);
424 remap_synonym(lc.search_country);
426 if (haveCP && !haveCountry && !haveLang)
428 FIXME(":Codepage only locale not implemented\n");
429 /* FIXME: Use default lang/country and skip locale_to_LCID()
436 lcid = MSVCRT_locale_to_LCID(&lc);
438 TRACE(":found LCID %d\n",lcid);
446 MSVCRT_current_lc_all_lcid = lcid;
448 snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
449 lc.found_language,lc.found_country,lc.found_codepage);
453 lc_all = 1; /* Fall through all cases ... */
454 case MSVCRT_LC_COLLATE:
456 case MSVCRT_LC_CTYPE:
457 msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
459 case MSVCRT_LC_MONETARY:
461 case MSVCRT_LC_NUMERIC:
467 return MSVCRT_current_lc_all;
470 /*********************************************************************
471 * setlocale (MSVCRT.@)
473 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
475 static MSVCRT_wchar_t fake[] = {
476 'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
477 'S','t','a','t','e','s','.','1','2','5','2',0 };
479 FIXME("%d %s\n", category, debugstr_w(locale));
484 /*********************************************************************
485 * _Getdays (MSVCRT.@)
487 const char* CDECL _Getdays(void)
489 static const char MSVCRT_days[] = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
490 "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
491 /* FIXME: Use locale */
492 TRACE("(void) semi-stub\n");
496 /*********************************************************************
497 * _Getmonths (MSVCRT.@)
499 const char* CDECL _Getmonths(void)
501 static const char MSVCRT_months[] = ":Jan:January:Feb:February:Mar:March:Apr:"
502 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
503 "October:Nov:November:Dec:December";
504 /* FIXME: Use locale */
505 TRACE("(void) semi-stub\n");
506 return MSVCRT_months;
509 /*********************************************************************
510 * _Gettnames (MSVCRT.@)
512 const char* CDECL _Gettnames(void)
515 TRACE("(void) stub\n");
519 /*********************************************************************
520 * _Strftime (MSVCRT.@)
522 const char* CDECL _Strftime(char *out, unsigned int len, const char *fmt,
523 const void *tm, void *foo)
526 TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
530 /*********************************************************************
531 * __crtLCMapStringA (MSVCRT.@)
533 int CDECL __crtLCMapStringA(
534 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
535 int dstlen, unsigned int codepage, int xflag
537 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
538 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
539 /* FIXME: A bit incorrect. But msvcrt itself just converts its
540 * arguments to wide strings and then calls LCMapStringW
542 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
545 /*********************************************************************
546 * __crtCompareStringA (MSVCRT.@)
548 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
549 const char *src2, int len2 )
551 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
552 lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
553 /* FIXME: probably not entirely right */
554 return CompareStringA( lcid, flags, src1, len1, src2, len2 );
557 /*********************************************************************
558 * __crtCompareStringW (MSVCRT.@)
560 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
561 const MSVCRT_wchar_t *src2, int len2 )
563 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
564 lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
565 /* FIXME: probably not entirely right */
566 return CompareStringW( lcid, flags, src1, len1, src2, len2 );
569 /*********************************************************************
570 * __crtGetLocaleInfoW (MSVCRT.@)
572 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
574 FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
575 /* FIXME: probably not entirely right */
576 return GetLocaleInfoW( lcid, type, buffer, len );
579 /*********************************************************************
580 * localeconv (MSVCRT.@)
582 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void) {
584 struct lconv *ylconv;
585 static struct MSVCRT_lconv xlconv;
587 ylconv = localeconv();
589 #define X(x) xlconv.x = ylconv->x;
595 X(mon_decimal_point);
596 X(mon_thousands_sep);
611 /*********************************************************************
612 * __lconv_init (MSVCRT.@)
614 void CDECL __lconv_init(void)