- Fix _fullpath & splitpath, winapi_check fixes
[wine] / dlls / crtdll / locale.c
1 /*
2  * CRT Locale functions
3  *
4  * Copyright 2000 Jon Griffiths
5  *
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.
17  *
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.
23  *
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.
29  *
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).
38  */
39
40 #include "crtdll.h"
41
42 #include <string.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45
46 #include "winnt.h"
47
48 DEFAULT_DEBUG_CHANNEL(crtdll);
49
50 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
51 #define MAX_LOCALE_LENGTH 256
52
53 /* FIXME: Need to hold locale for each LC_* type and aggregate
54  * string to produce lc_all.
55  */
56 char __CRTDLL_current_lc_all[MAX_LOCALE_LENGTH];
57 LCID __CRTDLL_current_lc_all_lcid;
58
59 /* Friendly country strings & iso codes for synonym support.
60  * Based on MS documentation for setlocale().
61  */
62 static const char* _country_synonyms[] = 
63 {
64   "Hong Kong","HK",
65   "Hong-Kong","HK",
66   "New Zealand","NZ",
67   "New-Zealand","NZ",
68   "PR China","CN",
69   "PR-China","CN",
70   "United Kingdom","GB",
71   "United-Kingdom","GB",
72   "Britain","GB",
73   "England","GB",
74   "Great Britain","GB",
75   "United States","US",
76   "United-States","US",
77   "America","US"
78 };
79
80 /* INTERNAL: Map a synonym to an ISO code */
81 static void remap_synonym(char *name)
82 {
83   int i;
84   for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
85   {
86     if (!strcasecmp(_country_synonyms[i],name))
87     {
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];
91       name[2] = '\0';
92       return;
93     }
94   }
95 }
96
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
101
102 typedef struct {
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;
111 } locale_search_t;
112
113 #define CONTINUE_LOOKING TRUE
114 #define STOP_LOOKING     FALSE
115
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)
118 {
119   buff[0] = 0;
120   GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
121   if (!buff[0] || !cmp[0])
122     return 0;
123   /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
124   return !strncasecmp(cmp, buff, strlen(cmp));
125 }
126
127
128 /* INTERNAL: Callback for enumerated languages */
129 static BOOL CALLBACK
130 find_best_locale_proc(HMODULE hModule, LPCSTR type,
131                       LPCSTR name, WORD LangID, LONG lParam)
132 {
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;
137
138   if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
139     return CONTINUE_LOOKING;
140   
141   /* Check Language */
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))
145   {
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);
149   }
150   else if (res->match_flags & FOUND_LANGUAGE)
151   {
152     return CONTINUE_LOOKING;
153   }
154
155   /* Check Country */
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))
159   {
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);
163   }
164   else if (res->match_flags & FOUND_COUNTRY)
165   {
166     return CONTINUE_LOOKING;
167   }
168
169   /* Check codepage */
170   if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
171       (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
172   {
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);
176   }
177   else if (res->match_flags & FOUND_CODEPAGE)
178   {
179     return CONTINUE_LOOKING;
180   }
181
182   if (flags > res->match_flags)
183   {
184     /* Found a better match than previously */
185     res->match_flags = flags;
186     res->found_lang_id = LangID;
187   }
188   if (flags & (FOUND_LANGUAGE & FOUND_COUNTRY & FOUND_CODEPAGE))
189   {
190     TRACE(":found exact locale match\n");
191     return STOP_LOOKING;
192   }
193   return CONTINUE_LOOKING;
194 }
195
196 /* Internal: Find the LCID for a locale specification */
197 static LCID __CRTDLL_locale_to_LCID(locale_search_t* locale)
198 {
199   LCID lcid;
200   EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
201                          (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
202                          (LONG)locale);
203
204   if (!locale->match_flags)
205     return 0;
206
207   /* If we were given something that didn't match, fail */
208   if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
209     return 0;
210
211   lcid =  MAKELCID(locale->found_lang_id, SORT_DEFAULT);
212
213   /* Populate partial locale, translating LCID to locale string elements */
214   if (!locale->found_codepage[0])
215   {
216     /* Even if a codepage is not enumerated for a locale
217      * it can be set if valid */
218     if (locale->search_codepage[0])
219     {
220       if (IsValidCodePage(atoi(locale->search_codepage)))
221         memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
222       else
223       {
224         /* Special codepage values: OEM & ANSI */
225         if (strcasecmp(locale->search_codepage,"OCP"))
226         {
227           GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
228                          locale->found_codepage, MAX_ELEM_LEN);
229         }
230         if (strcasecmp(locale->search_codepage,"ACP"))
231         {
232           GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
233                          locale->found_codepage, MAX_ELEM_LEN);
234         }
235         else
236           return 0;
237
238         if (!atoi(locale->found_codepage))
239            return 0;
240       }
241     }
242     else
243     {
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);
250     }
251   }
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);
256   return lcid;
257 }
258
259
260 /* INTERNAL: Set ctype behaviour for a codepage */
261 static void __CRTDLL_set_ctype(UINT codepage, LCID lcid)
262 {
263   CPINFO cp;
264
265   memset(&cp, 0, sizeof(CPINFO));
266
267   if (GetCPInfo(codepage, &cp))
268   {
269     int i;
270     char str[3];
271     unsigned char *traverse = (unsigned char *)cp.LeadByte;
272
273     memset(__CRTDLL_current_ctype, 0, sizeof(CRTDLL_ctype));
274
275     /* Switch ctype macros to MBCS if needed */
276     CRTDLL__mb_cur_max_dll = cp.MaxCharSize;
277
278     /* Set remaining ctype flags: FIXME: faster way to do this? */
279     str[1] = str[2] = 0;
280     for (i = 0; i < 256; i++)
281     {
282       if (!(CRTDLL_pctype_dll[i] & CRTDLL_LEADBYTE))
283       {
284         str[0] = i;
285         GetStringTypeA(lcid, CT_CTYPE1, str, 1, CRTDLL_pctype_dll + i);
286       }
287     }
288
289     /* Set leadbyte flags */
290     while (traverse[0] || traverse[1])
291     {
292       for( i = traverse[0]; i <= traverse[1]; i++ )
293         __CRTDLL_current_ctype[i+1] |= CRTDLL_LEADBYTE;
294       traverse += 2;
295     };
296   }
297 }
298
299
300 /*********************************************************************
301  *                  setlocale           (CRTDLL.453)
302  */
303 LPSTR __cdecl CRTDLL_setlocale(INT category, LPCSTR locale)
304 {
305   LCID lcid = 0;
306   locale_search_t lc;
307   int haveLang, haveCountry, haveCP;
308   char* next;
309   int lc_all = 0;
310
311   if (category < CRTDLL_LC_MIN || category > CRTDLL_LC_MAX)
312     return NULL;
313
314   if (locale == NULL)
315   {
316     /* Report the current Locale */
317     return __CRTDLL_current_lc_all;
318   }
319
320   if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
321   {
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.
326      */
327     return __CRTDLL_current_lc_all;
328   }
329
330   /* Default Locale: Special case handling */
331   if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
332   {
333     if ((toupper(__CRTDLL_current_lc_all[0]) != 'C')
334         || __CRTDLL_current_lc_all[1])
335     {
336       __CRTDLL_current_lc_all[0] = 'C';
337       __CRTDLL_current_lc_all[1] = 0;
338       switch (category) {
339       case CRTDLL_LC_ALL:
340         lc_all = 1; /* Fall through all cases ... */
341       case CRTDLL_LC_COLLATE:
342         if (!lc_all) break;
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));
347         if (!lc_all) break;
348       case CRTDLL_LC_MONETARY:
349         if (!lc_all) break;
350       case CRTDLL_LC_NUMERIC:
351         if (!lc_all) break;
352       case CRTDLL_LC_TIME:
353       }
354       return __CRTDLL_current_lc_all;
355     }
356   }
357
358   /* Get locale elements */
359   haveLang = haveCountry = haveCP = 0;
360   memset(&lc,0,sizeof(lc));
361
362   next = strchr(locale,'_');
363   if (next && next != locale)
364   {
365     haveLang = 1;
366     strncpy(lc.search_language,locale,next-locale);
367     locale += next-locale+1;
368   }
369
370   next = strchr(locale,'.');
371   if (next)
372   {
373     haveCP = 1;
374     if (next == locale)
375     {
376       locale++;
377       strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
378     }
379     else
380     {
381       if (haveLang)
382       {
383         haveCountry = 1;
384         strncpy(lc.search_country,locale,next-locale);
385         locale += next-locale+1;
386       }
387       else
388       {
389         haveLang = 1;
390         strncpy(lc.search_language,locale,next-locale);
391         locale += next-locale+1;
392       }
393       strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
394     }
395   }
396   else
397   {
398     if (haveLang)
399     {
400       haveCountry = 1;
401       strncpy(lc.search_country, locale, MAX_ELEM_LEN);
402     }
403     else
404     {
405       haveLang = 1;
406       strncpy(lc.search_language, locale, MAX_ELEM_LEN);
407     }
408   }
409
410   if (haveCountry)
411     remap_synonym(lc.search_country);
412
413   if (haveCP && !haveCountry && !haveLang)
414   {
415     FIXME(":Codepage only locale not implemented");
416     /* FIXME: Use default lang/country and skip locale_to_LCID()
417      * call below...
418      */
419     return NULL;
420   }
421
422   lcid = __CRTDLL_locale_to_LCID(&lc);
423
424   TRACE(":found LCID %ld\n",lcid);
425
426   if (lcid == 0)
427     return NULL;
428
429   __CRTDLL_current_lc_all_lcid = lcid;
430
431   snprintf(__CRTDLL_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
432            lc.found_language,lc.found_country,lc.found_codepage);
433
434     switch (category) {
435     case CRTDLL_LC_ALL: 
436       lc_all = 1; /* Fall through all cases ... */
437     case CRTDLL_LC_COLLATE:
438       if (!lc_all) break;
439     case CRTDLL_LC_CTYPE:
440       __CRTDLL_set_ctype(atoi(lc.found_codepage),lcid);
441       if (!lc_all) break;
442       break;
443     case CRTDLL_LC_MONETARY:
444       if (!lc_all) break;
445     case CRTDLL_LC_NUMERIC:
446       if (!lc_all) break;
447     case CRTDLL_LC_TIME:
448     }
449     return __CRTDLL_current_lc_all;
450 }