4 * Copyright 2000 Jon Griffiths
7 * Currently only LC_CTYPE behaviour is actually implemented.
8 * Passing a code page only is not yet supported.
10 * The code maps a (potentially incomplete) locale description to
11 * an LCID. The algorithm enumerates supported locales and
12 * compares the locale strings to the locale information given.
13 * Fully qualified locales should be completely compatable.
14 * Some countries (e.g. US) have synonyms that can be used in
15 * setlocale() calls - these are mapped to ISO codes before
16 * searching begins, but I may have missed some out of the list.
18 * It should be noted that the algorithm may locate a valid
19 * locale from a 2 letter ISO code, while the real DLL won't
20 * (it requires 3 letter codes or synonyms at a minimum).
21 * e.g. setlocale(LC_ALL,"de") will return "German_Germany.1252"
22 * with this implementation, while this fails in win32.
24 * It should also be noted that this implementation follows
25 * the MSVCRT behaviour, and not the CRTDLL behaviour.
26 * This is because MSVCRT provides a superset of the CRTDLL
27 * allowed locales, so this code can be used for both. Also
28 * The CRTDLL implementation can be considered broken.
30 * The code currently works for isleadbyte() but will fail
31 * (produce potentially incorrect values) for other locales
32 * with isalpha() etc. This is because the current Wine
33 * implementation of GetStringTypeA() is not locale aware.
34 * Fixing this requires a table of which characters in the
35 * code page are upper/lower/digit etc. If you locate such
36 * a table for a supported Wine locale, mail it to me and
37 * I will add the needed support (jon_p_griffiths@yahoo.com).
48 DEFAULT_DEBUG_CHANNEL(crtdll);
50 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
51 #define MAX_LOCALE_LENGTH 256
53 /* FIXME: Need to hold locale for each LC_* type and aggregate
54 * string to produce lc_all.
56 char __CRTDLL_current_lc_all[MAX_LOCALE_LENGTH];
57 LCID __CRTDLL_current_lc_all_lcid;
59 /* Friendly country strings & iso codes for synonym support.
60 * Based on MS documentation for setlocale().
62 static const char* _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 /* INTERNAL: Callback for enumerated languages */
130 find_best_locale_proc(HMODULE hModule, LPCSTR type,
131 LPCSTR name, WORD LangID, LONG lParam)
133 locale_search_t *res = (locale_search_t *)lParam;
134 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
135 char buff[MAX_ELEM_LEN];
136 unsigned int flags = 0;
138 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
139 return CONTINUE_LOOKING;
142 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
143 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
144 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
146 TRACE(":Found language: %s->%s\n", res->search_language, buff);
147 flags |= FOUND_LANGUAGE;
148 memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
150 else if (res->match_flags & FOUND_LANGUAGE)
152 return CONTINUE_LOOKING;
156 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
157 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
158 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
160 TRACE("Found country:%s->%s\n", res->search_country, buff);
161 flags |= FOUND_COUNTRY;
162 memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
164 else if (res->match_flags & FOUND_COUNTRY)
166 return CONTINUE_LOOKING;
170 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
171 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
173 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
174 flags |= FOUND_CODEPAGE;
175 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
177 else if (res->match_flags & FOUND_CODEPAGE)
179 return CONTINUE_LOOKING;
182 if (flags > res->match_flags)
184 /* Found a better match than previously */
185 res->match_flags = flags;
186 res->found_lang_id = LangID;
188 if (flags & (FOUND_LANGUAGE & FOUND_COUNTRY & FOUND_CODEPAGE))
190 TRACE(":found exact locale match\n");
193 return CONTINUE_LOOKING;
196 /* Internal: Find the LCID for a locale specification */
197 static LCID __CRTDLL_locale_to_LCID(locale_search_t* locale)
200 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
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 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);
260 /* INTERNAL: Set ctype behaviour for a codepage */
261 static void __CRTDLL_set_ctype(UINT codepage, LCID lcid)
265 memset(&cp, 0, sizeof(CPINFO));
267 if (GetCPInfo(codepage, &cp))
271 unsigned char *traverse = (unsigned char *)cp.LeadByte;
273 memset(__CRTDLL_current_ctype, 0, sizeof(CRTDLL_ctype));
275 /* Switch ctype macros to MBCS if needed */
276 CRTDLL__mb_cur_max_dll = cp.MaxCharSize;
278 /* Set remaining ctype flags: FIXME: faster way to do this? */
280 for (i = 0; i < 256; i++)
282 if (!(CRTDLL_pctype_dll[i] & CRTDLL_LEADBYTE))
285 GetStringTypeA(lcid, CT_CTYPE1, str, 1, CRTDLL_pctype_dll + i);
289 /* Set leadbyte flags */
290 while (traverse[0] || traverse[1])
292 for( i = traverse[0]; i <= traverse[1]; i++ )
293 __CRTDLL_current_ctype[i+1] |= CRTDLL_LEADBYTE;
300 /*********************************************************************
301 * setlocale (CRTDLL.453)
303 LPSTR __cdecl CRTDLL_setlocale(INT category, LPCSTR locale)
307 int haveLang, haveCountry, haveCP;
311 if (category < CRTDLL_LC_MIN || category > CRTDLL_LC_MAX)
316 /* Report the current Locale */
317 return __CRTDLL_current_lc_all;
320 if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
322 FIXME(":restore previous locale not implemented!\n");
323 /* FIXME: Easiest way to do this is parse the string and
324 * call this function recursively with its elements,
325 * Where they differ for each lc_ type.
327 return __CRTDLL_current_lc_all;
330 /* Default Locale: Special case handling */
331 if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
333 if ((toupper(__CRTDLL_current_lc_all[0]) != 'C')
334 || __CRTDLL_current_lc_all[1])
336 __CRTDLL_current_lc_all[0] = 'C';
337 __CRTDLL_current_lc_all[1] = 0;
340 lc_all = 1; /* Fall through all cases ... */
341 case CRTDLL_LC_COLLATE:
343 case CRTDLL_LC_CTYPE:
344 /* Restore C locale ctype info */
345 CRTDLL__mb_cur_max_dll = 1;
346 memcpy(__CRTDLL_current_ctype, CRTDLL_ctype, sizeof(CRTDLL_ctype));
348 case CRTDLL_LC_MONETARY:
350 case CRTDLL_LC_NUMERIC:
354 return __CRTDLL_current_lc_all;
358 /* Get locale elements */
359 haveLang = haveCountry = haveCP = 0;
360 memset(&lc,0,sizeof(lc));
362 next = strchr(locale,'_');
363 if (next && next != locale)
366 strncpy(lc.search_language,locale,next-locale);
367 locale += next-locale+1;
370 next = strchr(locale,'.');
377 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
384 strncpy(lc.search_country,locale,next-locale);
385 locale += next-locale+1;
390 strncpy(lc.search_language,locale,next-locale);
391 locale += next-locale+1;
393 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
401 strncpy(lc.search_country, locale, MAX_ELEM_LEN);
406 strncpy(lc.search_language, locale, MAX_ELEM_LEN);
411 remap_synonym(lc.search_country);
413 if (haveCP && !haveCountry && !haveLang)
415 FIXME(":Codepage only locale not implemented");
416 /* FIXME: Use default lang/country and skip locale_to_LCID()
422 lcid = __CRTDLL_locale_to_LCID(&lc);
424 TRACE(":found LCID %ld\n",lcid);
429 __CRTDLL_current_lc_all_lcid = lcid;
431 snprintf(__CRTDLL_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
432 lc.found_language,lc.found_country,lc.found_codepage);
436 lc_all = 1; /* Fall through all cases ... */
437 case CRTDLL_LC_COLLATE:
439 case CRTDLL_LC_CTYPE:
440 __CRTDLL_set_ctype(atoi(lc.found_codepage),lcid);
443 case CRTDLL_LC_MONETARY:
445 case CRTDLL_LC_NUMERIC:
449 return __CRTDLL_current_lc_all;