2 * msvcrt.dll locale functions
4 * Copyright 2000 Jon Griffiths
11 #include "msvcrt/locale.h"
13 #include "wine/debug.h"
15 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
17 /* FIXME: Need to hold locale for each LC_* type and aggregate
18 * string to produce lc_all.
20 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
21 #define MAX_LOCALE_LENGTH 256
22 char MSVCRT_current_lc_all[MAX_LOCALE_LENGTH];
23 LCID MSVCRT_current_lc_all_lcid;
24 int MSVCRT_current_lc_all_cp;
27 extern CRITICAL_SECTION MSVCRT_locale_cs;
28 #define LOCK_LOCALE EnterCriticalSection(&MSVCRT_locale_cs)
29 #define UNLOCK_LOCALE LeaveCriticalSection(&MSVCRT_locale_cs)
31 /* ctype data modified when the locale changes */
32 extern WORD MSVCRT__ctype [257];
33 extern WORD MSVCRT_current_ctype[257];
34 extern WORD* MSVCRT__pctype;
36 /* mbctype data modified when the locale changes */
37 extern int MSVCRT___mb_cur_max;
38 extern unsigned char MSVCRT_mbctype[257];
40 #define MSVCRT_LEADBYTE 0x8000
42 /* Friendly country strings & iso codes for synonym support.
43 * Based on MS documentation for setlocale().
45 static const char* _country_synonyms[] =
53 "United Kingdom","GB",
54 "United-Kingdom","GB",
63 /* INTERNAL: Map a synonym to an ISO code */
64 static void remap_synonym(char *name)
67 for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
69 if (!strcasecmp(_country_synonyms[i],name))
71 TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
72 name[0] = _country_synonyms[i+1][0];
73 name[1] = _country_synonyms[i+1][1];
80 /* Note: Flags are weighted in order of matching importance */
81 #define FOUND_LANGUAGE 0x4
82 #define FOUND_COUNTRY 0x2
83 #define FOUND_CODEPAGE 0x1
86 char search_language[MAX_ELEM_LEN];
87 char search_country[MAX_ELEM_LEN];
88 char search_codepage[MAX_ELEM_LEN];
89 char found_language[MAX_ELEM_LEN];
90 char found_country[MAX_ELEM_LEN];
91 char found_codepage[MAX_ELEM_LEN];
92 unsigned int match_flags;
96 #define CONTINUE_LOOKING TRUE
97 #define STOP_LOOKING FALSE
99 /* INTERNAL: Get and compare locale info with a given string */
100 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp)
103 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
104 if (!buff[0] || !cmp[0])
106 /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
107 return !strncasecmp(cmp, buff, strlen(cmp));
111 find_best_locale_proc(HMODULE hModule WINE_UNUSED, LPCSTR type WINE_UNUSED,
112 LPCSTR name WINE_UNUSED, WORD LangID, LONG lParam)
114 locale_search_t *res = (locale_search_t *)lParam;
115 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
116 char buff[MAX_ELEM_LEN];
117 unsigned int flags = 0;
119 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
120 return CONTINUE_LOOKING;
123 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
124 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
125 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
127 TRACE(":Found language: %s->%s\n", res->search_language, buff);
128 flags |= FOUND_LANGUAGE;
129 memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
131 else if (res->match_flags & FOUND_LANGUAGE)
133 return CONTINUE_LOOKING;
137 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
138 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
139 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
141 TRACE("Found country:%s->%s\n", res->search_country, buff);
142 flags |= FOUND_COUNTRY;
143 memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
145 else if (res->match_flags & FOUND_COUNTRY)
147 return CONTINUE_LOOKING;
151 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
152 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
154 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
155 flags |= FOUND_CODEPAGE;
156 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
158 else if (res->match_flags & FOUND_CODEPAGE)
160 return CONTINUE_LOOKING;
163 if (flags > res->match_flags)
165 /* Found a better match than previously */
166 res->match_flags = flags;
167 res->found_lang_id = LangID;
169 if (flags & (FOUND_LANGUAGE & FOUND_COUNTRY & FOUND_CODEPAGE))
171 TRACE(":found exact locale match\n");
174 return CONTINUE_LOOKING;
177 extern int atoi(const char *);
179 /* Internal: Find the LCID for a locale specification */
180 static LCID MSVCRT_locale_to_LCID(locale_search_t* locale)
183 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
184 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
187 if (!locale->match_flags)
190 /* If we were given something that didn't match, fail */
191 if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
194 lcid = MAKELCID(locale->found_lang_id, SORT_DEFAULT);
196 /* Populate partial locale, translating LCID to locale string elements */
197 if (!locale->found_codepage[0])
199 /* Even if a codepage is not enumerated for a locale
200 * it can be set if valid */
201 if (locale->search_codepage[0])
203 if (IsValidCodePage(atoi(locale->search_codepage)))
204 memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
207 /* Special codepage values: OEM & ANSI */
208 if (strcasecmp(locale->search_codepage,"OCP"))
210 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
211 locale->found_codepage, MAX_ELEM_LEN);
213 if (strcasecmp(locale->search_codepage,"ACP"))
215 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
216 locale->found_codepage, MAX_ELEM_LEN);
221 if (!atoi(locale->found_codepage))
227 /* Prefer ANSI codepages if present */
228 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
229 locale->found_codepage, MAX_ELEM_LEN);
230 if (!locale->found_codepage[0] || !atoi(locale->found_codepage))
231 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
232 locale->found_codepage, MAX_ELEM_LEN);
235 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
236 locale->found_language, MAX_ELEM_LEN);
237 GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
238 locale->found_country, MAX_ELEM_LEN);
242 extern int snprintf(char *, int, const char *, ...);
244 /* INTERNAL: Set ctype behaviour for a codepage */
245 static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
249 memset(&cp, 0, sizeof(CPINFO));
251 if (GetCPInfo(codepage, &cp))
255 unsigned char *traverse = (unsigned char *)cp.LeadByte;
257 memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
258 MSVCRT_current_lc_all_cp = codepage;
260 /* Switch ctype macros to MBCS if needed */
261 MSVCRT___mb_cur_max = cp.MaxCharSize;
263 /* Set remaining ctype flags: FIXME: faster way to do this? */
265 for (i = 0; i < 256; i++)
267 if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
270 GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
274 /* Set leadbyte flags */
275 while (traverse[0] || traverse[1])
277 for( i = traverse[0]; i <= traverse[1]; i++ )
278 MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
285 /*********************************************************************
286 * setlocale (MSVCRT.@)
288 char* MSVCRT_setlocale(int category, const char* locale)
292 int haveLang, haveCountry, haveCP;
296 TRACE("(%d %s)\n",category,locale);
298 if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
303 /* Report the current Locale */
304 return MSVCRT_current_lc_all;
309 if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
311 FIXME(":restore previous locale not implemented!\n");
312 /* FIXME: Easiest way to do this is parse the string and
313 * call this function recursively with its elements,
314 * Where they differ for each lc_ type.
317 return MSVCRT_current_lc_all;
320 /* Default Locale: Special case handling */
321 if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
323 MSVCRT_current_lc_all[0] = 'C';
324 MSVCRT_current_lc_all[1] = '\0';
325 MSVCRT_current_lc_all_cp = GetACP();
329 lc_all = 1; /* Fall through all cases ... */
330 case MSVCRT_LC_COLLATE:
332 case MSVCRT_LC_CTYPE:
333 /* Restore C locale ctype info */
334 MSVCRT___mb_cur_max = 1;
335 memcpy(MSVCRT_current_ctype, MSVCRT__ctype, sizeof(MSVCRT__ctype));
336 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
338 case MSVCRT_LC_MONETARY:
340 case MSVCRT_LC_NUMERIC:
346 return MSVCRT_current_lc_all;
349 /* Get locale elements */
350 haveLang = haveCountry = haveCP = 0;
351 memset(&lc,0,sizeof(lc));
353 next = strchr(locale,'_');
354 if (next && next != locale)
357 strncpy(lc.search_language,locale,next-locale);
358 locale += next-locale+1;
361 next = strchr(locale,'.');
368 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
375 strncpy(lc.search_country,locale,next-locale);
376 locale += next-locale+1;
381 strncpy(lc.search_language,locale,next-locale);
382 locale += next-locale+1;
384 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
392 strncpy(lc.search_country, locale, MAX_ELEM_LEN);
397 strncpy(lc.search_language, locale, MAX_ELEM_LEN);
402 remap_synonym(lc.search_country);
404 if (haveCP && !haveCountry && !haveLang)
406 FIXME(":Codepage only locale not implemented\n");
407 /* FIXME: Use default lang/country and skip locale_to_LCID()
414 lcid = MSVCRT_locale_to_LCID(&lc);
416 TRACE(":found LCID %ld\n",lcid);
424 MSVCRT_current_lc_all_lcid = lcid;
426 snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
427 lc.found_language,lc.found_country,lc.found_codepage);
431 lc_all = 1; /* Fall through all cases ... */
432 case MSVCRT_LC_COLLATE:
434 case MSVCRT_LC_CTYPE:
435 msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
437 case MSVCRT_LC_MONETARY:
439 case MSVCRT_LC_NUMERIC:
445 return MSVCRT_current_lc_all;
449 /*********************************************************************
450 * _Getdays (MSVCRT.@)
452 const char* _Getdays(void)
454 static const char *MSVCRT_days = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
455 "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
456 /* FIXME: Use locale */
457 TRACE("(void) semi-stub\n");
461 /*********************************************************************
462 * _Getmonths (MSVCRT.@)
464 const char* _Getmonths(void)
466 static const char *MSVCRT_months = ":Jan:January:Feb:February:Mar:March:Apr:"
467 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
468 "October:Nov:November:Dec:December";
469 /* FIXME: Use locale */
470 TRACE("(void) semi-stub\n");
471 return MSVCRT_months;
474 /*********************************************************************
475 * _Getnames (MSVCRT.@)
477 const char* _Getnames(void)
480 TRACE("(void) stub\n");
484 /*********************************************************************
485 * _Strftime (MSVCRT.@)
487 const char* _Strftime(char *out, unsigned int len, const char *fmt,
488 const void *tm, void *foo)
491 TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
495 /* FIXME: MBCP probably belongs in mbcs.c */
497 /*********************************************************************
498 * _setmbcp (MSVCRT.@)
500 void _setmbcp(int cp)
503 if (MSVCRT_current_lc_all_cp != cp)
505 /* FIXME: set ctype behaviour for this cp */
506 MSVCRT_current_lc_all_cp = cp;
511 /*********************************************************************
512 * _getmbcp (MSVCRT.@)
516 return MSVCRT_current_lc_all_cp;