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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
31 #include "msvcrt/locale.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38 /* FIXME: Need to hold locale for each LC_* type and aggregate
39 * string to produce lc_all.
41 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
42 #define MAX_LOCALE_LENGTH 256
43 char MSVCRT_current_lc_all[MAX_LOCALE_LENGTH];
44 LCID MSVCRT_current_lc_all_lcid;
45 int MSVCRT_current_lc_all_cp;
48 #define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
49 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
51 /* ctype data modified when the locale changes */
52 extern WORD MSVCRT__ctype [257];
53 extern WORD MSVCRT_current_ctype[257];
54 extern WORD* MSVCRT__pctype;
56 /* mbctype data modified when the locale changes */
57 extern int MSVCRT___mb_cur_max;
58 extern unsigned char MSVCRT_mbctype[257];
60 #define MSVCRT_LEADBYTE 0x8000
62 /* Friendly country strings & iso codes for synonym support.
63 * Based on MS documentation for setlocale().
65 static const char* _country_synonyms[] =
73 "United Kingdom","GB",
74 "United-Kingdom","GB",
83 /* INTERNAL: Map a synonym to an ISO code */
84 static void remap_synonym(char *name)
87 for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
89 if (!strcasecmp(_country_synonyms[i],name))
91 TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
92 name[0] = _country_synonyms[i+1][0];
93 name[1] = _country_synonyms[i+1][1];
100 /* Note: Flags are weighted in order of matching importance */
101 #define FOUND_LANGUAGE 0x4
102 #define FOUND_COUNTRY 0x2
103 #define FOUND_CODEPAGE 0x1
106 char search_language[MAX_ELEM_LEN];
107 char search_country[MAX_ELEM_LEN];
108 char search_codepage[MAX_ELEM_LEN];
109 char found_language[MAX_ELEM_LEN];
110 char found_country[MAX_ELEM_LEN];
111 char found_codepage[MAX_ELEM_LEN];
112 unsigned int match_flags;
113 LANGID found_lang_id;
116 #define CONTINUE_LOOKING TRUE
117 #define STOP_LOOKING FALSE
119 /* INTERNAL: Get and compare locale info with a given string */
120 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp)
123 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
124 if (!buff[0] || !cmp[0])
126 /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
127 return !strncasecmp(cmp, buff, strlen(cmp));
131 find_best_locale_proc(HMODULE hModule WINE_UNUSED, LPCSTR type WINE_UNUSED,
132 LPCSTR name WINE_UNUSED, WORD LangID, LONG lParam)
134 locale_search_t *res = (locale_search_t *)lParam;
135 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
136 char buff[MAX_ELEM_LEN];
137 unsigned int flags = 0;
139 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
140 return CONTINUE_LOOKING;
143 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
144 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
145 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
147 TRACE(":Found language: %s->%s\n", res->search_language, buff);
148 flags |= FOUND_LANGUAGE;
149 memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
151 else if (res->match_flags & FOUND_LANGUAGE)
153 return CONTINUE_LOOKING;
157 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
158 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
159 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
161 TRACE("Found country:%s->%s\n", res->search_country, buff);
162 flags |= FOUND_COUNTRY;
163 memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
165 else if (res->match_flags & FOUND_COUNTRY)
167 return CONTINUE_LOOKING;
171 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
172 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
174 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
175 flags |= FOUND_CODEPAGE;
176 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
178 else if (res->match_flags & FOUND_CODEPAGE)
180 return CONTINUE_LOOKING;
183 if (flags > res->match_flags)
185 /* Found a better match than previously */
186 res->match_flags = flags;
187 res->found_lang_id = LangID;
189 if (flags & (FOUND_LANGUAGE & FOUND_COUNTRY & FOUND_CODEPAGE))
191 TRACE(":found exact locale match\n");
194 return CONTINUE_LOOKING;
197 extern int atoi(const char *);
199 /* Internal: Find the LCID for a locale specification */
200 static LCID MSVCRT_locale_to_LCID(locale_search_t* locale)
203 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
204 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
207 if (!locale->match_flags)
210 /* If we were given something that didn't match, fail */
211 if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
214 lcid = MAKELCID(locale->found_lang_id, SORT_DEFAULT);
216 /* Populate partial locale, translating LCID to locale string elements */
217 if (!locale->found_codepage[0])
219 /* Even if a codepage is not enumerated for a locale
220 * it can be set if valid */
221 if (locale->search_codepage[0])
223 if (IsValidCodePage(atoi(locale->search_codepage)))
224 memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
227 /* Special codepage values: OEM & ANSI */
228 if (strcasecmp(locale->search_codepage,"OCP"))
230 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
231 locale->found_codepage, MAX_ELEM_LEN);
233 if (strcasecmp(locale->search_codepage,"ACP"))
235 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
236 locale->found_codepage, MAX_ELEM_LEN);
241 if (!atoi(locale->found_codepage))
247 /* Prefer ANSI codepages if present */
248 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
249 locale->found_codepage, MAX_ELEM_LEN);
250 if (!locale->found_codepage[0] || !atoi(locale->found_codepage))
251 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
252 locale->found_codepage, MAX_ELEM_LEN);
255 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
256 locale->found_language, MAX_ELEM_LEN);
257 GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
258 locale->found_country, MAX_ELEM_LEN);
262 /* INTERNAL: Set ctype behaviour for a codepage */
263 static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
267 memset(&cp, 0, sizeof(CPINFO));
269 if (GetCPInfo(codepage, &cp))
273 unsigned char *traverse = (unsigned char *)cp.LeadByte;
275 memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
276 MSVCRT_current_lc_all_cp = codepage;
278 /* Switch ctype macros to MBCS if needed */
279 MSVCRT___mb_cur_max = cp.MaxCharSize;
281 /* Set remaining ctype flags: FIXME: faster way to do this? */
283 for (i = 0; i < 256; i++)
285 if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
288 GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
292 /* Set leadbyte flags */
293 while (traverse[0] || traverse[1])
295 for( i = traverse[0]; i <= traverse[1]; i++ )
296 MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
303 /*********************************************************************
304 * setlocale (MSVCRT.@)
306 char* MSVCRT_setlocale(int category, const char* locale)
310 int haveLang, haveCountry, haveCP;
314 TRACE("(%d %s)\n",category,locale);
316 if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
321 /* Report the current Locale */
322 return MSVCRT_current_lc_all;
327 if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
329 FIXME(":restore previous locale not implemented!\n");
330 /* FIXME: Easiest way to do this is parse the string and
331 * call this function recursively with its elements,
332 * Where they differ for each lc_ type.
335 return MSVCRT_current_lc_all;
338 /* Default Locale: Special case handling */
339 if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
341 MSVCRT_current_lc_all[0] = 'C';
342 MSVCRT_current_lc_all[1] = '\0';
343 MSVCRT_current_lc_all_cp = GetACP();
347 lc_all = 1; /* Fall through all cases ... */
348 case MSVCRT_LC_COLLATE:
350 case MSVCRT_LC_CTYPE:
351 /* Restore C locale ctype info */
352 MSVCRT___mb_cur_max = 1;
353 memcpy(MSVCRT_current_ctype, MSVCRT__ctype, sizeof(MSVCRT__ctype));
354 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
356 case MSVCRT_LC_MONETARY:
358 case MSVCRT_LC_NUMERIC:
364 return MSVCRT_current_lc_all;
367 /* Get locale elements */
368 haveLang = haveCountry = haveCP = 0;
369 memset(&lc,0,sizeof(lc));
371 next = strchr(locale,'_');
372 if (next && next != locale)
375 strncpy(lc.search_language,locale,next-locale);
376 locale += next-locale+1;
379 next = strchr(locale,'.');
386 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
393 strncpy(lc.search_country,locale,next-locale);
394 locale += next-locale+1;
399 strncpy(lc.search_language,locale,next-locale);
400 locale += next-locale+1;
402 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
410 strncpy(lc.search_country, locale, MAX_ELEM_LEN);
415 strncpy(lc.search_language, locale, MAX_ELEM_LEN);
420 remap_synonym(lc.search_country);
422 if (haveCP && !haveCountry && !haveLang)
424 FIXME(":Codepage only locale not implemented\n");
425 /* FIXME: Use default lang/country and skip locale_to_LCID()
432 lcid = MSVCRT_locale_to_LCID(&lc);
434 TRACE(":found LCID %ld\n",lcid);
442 MSVCRT_current_lc_all_lcid = lcid;
444 snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
445 lc.found_language,lc.found_country,lc.found_codepage);
449 lc_all = 1; /* Fall through all cases ... */
450 case MSVCRT_LC_COLLATE:
452 case MSVCRT_LC_CTYPE:
453 msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
455 case MSVCRT_LC_MONETARY:
457 case MSVCRT_LC_NUMERIC:
463 return MSVCRT_current_lc_all;
467 /*********************************************************************
468 * _Getdays (MSVCRT.@)
470 const char* _Getdays(void)
472 static const char *MSVCRT_days = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
473 "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
474 /* FIXME: Use locale */
475 TRACE("(void) semi-stub\n");
479 /*********************************************************************
480 * _Getmonths (MSVCRT.@)
482 const char* _Getmonths(void)
484 static const char *MSVCRT_months = ":Jan:January:Feb:February:Mar:March:Apr:"
485 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
486 "October:Nov:November:Dec:December";
487 /* FIXME: Use locale */
488 TRACE("(void) semi-stub\n");
489 return MSVCRT_months;
492 /*********************************************************************
493 * _Getnames (MSVCRT.@)
495 const char* _Getnames(void)
498 TRACE("(void) stub\n");
502 /*********************************************************************
503 * _Strftime (MSVCRT.@)
505 const char* _Strftime(char *out, unsigned int len, const char *fmt,
506 const void *tm, void *foo)
509 TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
513 /* FIXME: MBCP probably belongs in mbcs.c */
515 /*********************************************************************
516 * _setmbcp (MSVCRT.@)
518 void _setmbcp(int cp)
521 if (MSVCRT_current_lc_all_cp != cp)
523 /* FIXME: set ctype behaviour for this cp */
524 MSVCRT_current_lc_all_cp = cp;
529 /*********************************************************************
530 * _getmbcp (MSVCRT.@)
534 return MSVCRT_current_lc_all_cp;
537 /*********************************************************************
538 * __crtLCMapStringA (MSVCRT.@)
540 int __crtLCMapStringA(
541 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
542 int dstlen, unsigned int codepage, int xflag
544 FIXME("(lcid %lx, flags %lx, %s(%d), %p(%d), %x, %d), partial stub!\n",
545 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
546 /* FIXME: A bit incorrect. But msvcrt itself just converts its
547 * arguments to wide strings and then calls LCMapStringW
549 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
552 /*********************************************************************
553 * localeconv (MSVCRT.@)
555 struct MSVCRT_lconv *MSVCRT_localeconv(void) {
557 struct lconv *ylconv;
558 static struct MSVCRT_lconv xlconv;
560 ylconv = localeconv();
562 #define X(x) xlconv.x = ylconv->x;
568 X(mon_decimal_point);
569 X(mon_thousands_sep);
584 /*********************************************************************
585 * _Gettnames (MSVCRT.@)
587 void *_Gettnames(void)
589 FIXME("(void), stub!\n");