- Implemented setlocale parsing and LC_TYPE behavior.
[wine/wine-kai.git] / dlls / crtdll / locale.c
blobb99c80ce7ee254c101ca4488717983102dc1ea9a
1 /*
2 * CRT Locale functions
4 * Copyright 2000 Jon Griffiths
6 * NOTES:
7 * Currently only LC_CTYPE behaviour is actually implemented.
8 * Passing a code page only is not yet supported.
9 *
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).
39 #include "crtdll.h"
40 #include <winnt.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <stdio.h>
45 DEFAULT_DEBUG_CHANNEL(crtdll);
47 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
48 #define MAX_LOCALE_LENGTH 256
50 /* FIXME: Need to hold locale for each LC_* type and aggregate
51 * string to produce lc_all.
53 char __CRTDLL_current_lc_all[MAX_LOCALE_LENGTH];
54 LCID __CRTDLL_current_lc_all_lcid;
56 /* Friendly country strings & iso codes for synonym support.
57 * Based on MS documentation for setlocale().
59 static const char* _country_synonyms[] =
61 "Hong Kong","HK",
62 "Hong-Kong","HK",
63 "New Zealand","NZ",
64 "New-Zealand","NZ",
65 "PR China","CN",
66 "PR-China","CN",
67 "United Kingdom","GB",
68 "United-Kingdom","GB",
69 "Britain","GB",
70 "England","GB",
71 "Great Britain","GB",
72 "United States","US",
73 "United-States","US",
74 "America","US"
77 /* INTERNAL: Map a synonym to an ISO code */
78 static void remap_synonym(char *name)
80 int i;
81 for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
83 if (!strcasecmp(_country_synonyms[i],name))
85 TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
86 name[0] = _country_synonyms[i+1][0];
87 name[1] = _country_synonyms[i+1][1];
88 name[2] = '\0';
89 return;
94 /* Note: Flags are weighted in order of matching importance */
95 #define FOUND_LANGUAGE 0x4
96 #define FOUND_COUNTRY 0x2
97 #define FOUND_CODEPAGE 0x1
99 typedef struct {
100 char search_language[MAX_ELEM_LEN];
101 char search_country[MAX_ELEM_LEN];
102 char search_codepage[MAX_ELEM_LEN];
103 char found_language[MAX_ELEM_LEN];
104 char found_country[MAX_ELEM_LEN];
105 char found_codepage[MAX_ELEM_LEN];
106 unsigned int match_flags;
107 LANGID found_lang_id;
108 } locale_search_t;
110 #define CONTINUE_LOOKING TRUE
111 #define STOP_LOOKING FALSE
113 /* INTERNAL: Get and compare locale info with a given string */
114 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp)
116 buff[0] = 0;
117 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
118 if (!buff[0] || !cmp[0])
119 return 0;
120 /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
121 return !strncasecmp(cmp, buff, strlen(cmp));
125 /* INTERNAL: Callback for enumerated languages */
126 static BOOL CALLBACK
127 find_best_locale_proc(HMODULE hModule, LPCSTR type,
128 LPCSTR name, WORD LangID, LONG lParam)
130 locale_search_t *res = (locale_search_t *)lParam;
131 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
132 char buff[MAX_ELEM_LEN];
133 unsigned int flags = 0;
135 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
136 return CONTINUE_LOOKING;
138 /* Check Language */
139 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
140 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
141 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
143 TRACE(":Found language: %s->%s\n", res->search_language, buff);
144 flags |= FOUND_LANGUAGE;
145 memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
147 else if (res->match_flags & FOUND_LANGUAGE)
149 return CONTINUE_LOOKING;
152 /* Check Country */
153 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
154 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
155 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
157 TRACE("Found country:%s->%s\n", res->search_country, buff);
158 flags |= FOUND_COUNTRY;
159 memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
161 else if (res->match_flags & FOUND_COUNTRY)
163 return CONTINUE_LOOKING;
166 /* Check codepage */
167 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
168 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
170 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
171 flags |= FOUND_CODEPAGE;
172 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
174 else if (res->match_flags & FOUND_CODEPAGE)
176 return CONTINUE_LOOKING;
179 if (flags > res->match_flags)
181 /* Found a better match than previously */
182 res->match_flags = flags;
183 res->found_lang_id = LangID;
185 if (flags & (FOUND_LANGUAGE & FOUND_COUNTRY & FOUND_CODEPAGE))
187 TRACE(":found exact locale match\n");
188 return STOP_LOOKING;
190 return CONTINUE_LOOKING;
193 /* Internal: Find the LCID for a locale specification */
194 static LCID __CRTDLL_locale_to_LCID(locale_search_t* locale)
196 LCID lcid;
197 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
198 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
199 (LONG)locale);
201 if (!locale->match_flags)
202 return 0;
204 /* If we were given something that didn't match, fail */
205 if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
206 return 0;
208 lcid = MAKELCID(locale->found_lang_id, SORT_DEFAULT);
210 /* Populate partial locale, translating LCID to locale string elements */
211 if (!locale->found_codepage[0])
213 /* Even if a codepage is not enumerated for a locale
214 * it can be set if valid */
215 if (locale->search_codepage[0])
217 if (IsValidCodePage(atoi(locale->search_codepage)))
218 memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
219 else
221 /* Special codepage values: OEM & ANSI */
222 if (strcasecmp(locale->search_codepage,"OCP"))
224 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
225 locale->found_codepage, MAX_ELEM_LEN);
227 if (strcasecmp(locale->search_codepage,"ACP"))
229 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
230 locale->found_codepage, MAX_ELEM_LEN);
232 else
233 return 0;
235 if (!atoi(locale->found_codepage))
236 return 0;
239 else
241 /* Prefer ANSI codepages if present */
242 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
243 locale->found_codepage, MAX_ELEM_LEN);
244 if (!locale->found_codepage[0] || !atoi(locale->found_codepage))
245 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
246 locale->found_codepage, MAX_ELEM_LEN);
249 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
250 locale->found_language, MAX_ELEM_LEN);
251 GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
252 locale->found_country, MAX_ELEM_LEN);
253 return lcid;
257 /* INTERNAL: Set ctype behaviour for a codepage */
258 static void __CRTDLL_set_ctype(UINT codepage, LCID lcid)
260 CPINFO cp;
262 memset(&cp, 0, sizeof(CPINFO));
264 if (GetCPInfo(codepage, &cp))
266 int i;
267 char str[3];
268 unsigned char *traverse = (unsigned char *)cp.LeadByte;
270 memset(__CRTDLL_current_ctype, 0, sizeof(CRTDLL_ctype));
272 /* Switch ctype macros to MBCS if needed */
273 CRTDLL__mb_cur_max_dll = cp.MaxCharSize;
275 /* Set remaining ctype flags: FIXME: faster way to do this? */
276 str[1] = str[2] = 0;
277 for (i = 0; i < 256; i++)
279 if (!(CRTDLL_pctype_dll[i] & CRTDLL_LEADBYTE))
281 str[0] = i;
282 GetStringTypeA(lcid, CT_CTYPE1, str, 1, CRTDLL_pctype_dll + i);
286 /* Set leadbyte flags */
287 while (traverse[0] || traverse[1])
289 for( i = traverse[0]; i <= traverse[1]; i++ )
290 __CRTDLL_current_ctype[i+1] |= CRTDLL_LEADBYTE;
291 traverse += 2;
297 /*********************************************************************
298 * setlocale (CRTDLL.453)
300 LPSTR __cdecl CRTDLL_setlocale(INT category, LPCSTR locale)
302 LCID lcid = 0;
303 locale_search_t lc;
304 int haveLang, haveCountry, haveCP;
305 char* next;
306 int lc_all = 0;
308 if (category < CRTDLL_LC_MIN || category > CRTDLL_LC_MAX)
309 return NULL;
311 if (locale == NULL)
313 /* Report the current Locale */
314 return __CRTDLL_current_lc_all;
317 if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
319 FIXME(":restore previous locale not implemented!\n");
320 /* FIXME: Easiest way to do this is parse the string and
321 * call this function recursively with its elements,
322 * Where they differ for each lc_ type.
324 return __CRTDLL_current_lc_all;
327 /* Default Locale: Special case handling */
328 if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
330 if ((toupper(__CRTDLL_current_lc_all[0]) != 'C')
331 || __CRTDLL_current_lc_all[1])
333 __CRTDLL_current_lc_all[0] = 'C';
334 __CRTDLL_current_lc_all[1] = 0;
335 switch (category) {
336 case CRTDLL_LC_ALL:
337 lc_all = 1; /* Fall through all cases ... */
338 case CRTDLL_LC_COLLATE:
339 if (!lc_all) break;
340 case CRTDLL_LC_CTYPE:
341 /* Restore C locale ctype info */
342 CRTDLL__mb_cur_max_dll = 1;
343 memcpy(__CRTDLL_current_ctype, CRTDLL_ctype, sizeof(CRTDLL_ctype));
344 if (!lc_all) break;
345 case CRTDLL_LC_MONETARY:
346 if (!lc_all) break;
347 case CRTDLL_LC_NUMERIC:
348 if (!lc_all) break;
349 case CRTDLL_LC_TIME:
351 return __CRTDLL_current_lc_all;
355 /* Get locale elements */
356 haveLang = haveCountry = haveCP = 0;
357 memset(&lc,0,sizeof(lc));
359 next = strchr(locale,'_');
360 if (next && next != locale)
362 haveLang = 1;
363 strncpy(lc.search_language,locale,next-locale);
364 locale += next-locale+1;
367 next = strchr(locale,'.');
368 if (next)
370 haveCP = 1;
371 if (next == locale)
373 locale++;
374 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
376 else
378 if (haveLang)
380 haveCountry = 1;
381 strncpy(lc.search_country,locale,next-locale);
382 locale += next-locale+1;
384 else
386 haveLang = 1;
387 strncpy(lc.search_language,locale,next-locale);
388 locale += next-locale+1;
390 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
393 else
395 if (haveLang)
397 haveCountry = 1;
398 strncpy(lc.search_country, locale, MAX_ELEM_LEN);
400 else
402 haveLang = 1;
403 strncpy(lc.search_language, locale, MAX_ELEM_LEN);
407 if (haveCountry)
408 remap_synonym(lc.search_country);
410 if (haveCP && !haveCountry && !haveLang)
412 FIXME(":Codepage only locale not implemented");
413 /* FIXME: Use default lang/country and skip locale_to_LCID()
414 * call below...
416 return NULL;
419 lcid = __CRTDLL_locale_to_LCID(&lc);
421 TRACE(":found LCID %ld\n",lcid);
423 if (lcid == 0)
424 return NULL;
426 __CRTDLL_current_lc_all_lcid = lcid;
428 snprintf(__CRTDLL_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
429 lc.found_language,lc.found_country,lc.found_codepage);
431 switch (category) {
432 case CRTDLL_LC_ALL:
433 lc_all = 1; /* Fall through all cases ... */
434 case CRTDLL_LC_COLLATE:
435 if (!lc_all) break;
436 case CRTDLL_LC_CTYPE:
437 __CRTDLL_set_ctype(atoi(lc.found_codepage),lcid);
438 if (!lc_all) break;
439 break;
440 case CRTDLL_LC_MONETARY:
441 if (!lc_all) break;
442 case CRTDLL_LC_NUMERIC:
443 if (!lc_all) break;
444 case CRTDLL_LC_TIME:
446 return __CRTDLL_current_lc_all;