- support DirectInput 8 interfaces.
[wine] / dlls / msvcrt / locale.c
1 /*
2  * msvcrt.dll locale functions
3  *
4  * Copyright 2000 Jon Griffiths
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include "winnt.h"
25 #include "winbase.h"
26 #include "winuser.h"
27
28 #include "msvcrt.h"
29 #include "msvcrt/locale.h"
30 #include "mtdll.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
35
36 /* FIXME: Need to hold locale for each LC_* type and aggregate
37  * string to produce lc_all.
38  */
39 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
40 #define MAX_LOCALE_LENGTH 256
41 char MSVCRT_current_lc_all[MAX_LOCALE_LENGTH];
42 LCID MSVCRT_current_lc_all_lcid;
43 int MSVCRT_current_lc_all_cp;
44
45 /* MT */
46 #define LOCK_LOCALE   _mlock(_SETLOCALE_LOCK);
47 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
48
49 /* ctype data modified when the locale changes */
50 extern WORD MSVCRT__ctype [257];
51 extern WORD MSVCRT_current_ctype[257];
52 extern WORD* MSVCRT__pctype;
53
54 /* mbctype data modified when the locale changes */
55 extern int MSVCRT___mb_cur_max;
56 extern unsigned char MSVCRT_mbctype[257];
57
58 #define MSVCRT_LEADBYTE  0x8000
59
60 /* Friendly country strings & iso codes for synonym support.
61  * Based on MS documentation for setlocale().
62  */
63 static const char* _country_synonyms[] =
64 {
65   "Hong Kong","HK",
66   "Hong-Kong","HK",
67   "New Zealand","NZ",
68   "New-Zealand","NZ",
69   "PR China","CN",
70   "PR-China","CN",
71   "United Kingdom","GB",
72   "United-Kingdom","GB",
73   "Britain","GB",
74   "England","GB",
75   "Great Britain","GB",
76   "United States","US",
77   "United-States","US",
78   "America","US"
79 };
80
81 /* INTERNAL: Map a synonym to an ISO code */
82 static void remap_synonym(char *name)
83 {
84   size_t i;
85   for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
86   {
87     if (!strcasecmp(_country_synonyms[i],name))
88     {
89       TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
90       name[0] = _country_synonyms[i+1][0];
91       name[1] = _country_synonyms[i+1][1];
92       name[2] = '\0';
93       return;
94     }
95   }
96 }
97
98 /* Note: Flags are weighted in order of matching importance */
99 #define FOUND_LANGUAGE         0x4
100 #define FOUND_COUNTRY          0x2
101 #define FOUND_CODEPAGE         0x1
102
103 typedef struct {
104   char search_language[MAX_ELEM_LEN];
105   char search_country[MAX_ELEM_LEN];
106   char search_codepage[MAX_ELEM_LEN];
107   char found_language[MAX_ELEM_LEN];
108   char found_country[MAX_ELEM_LEN];
109   char found_codepage[MAX_ELEM_LEN];
110   unsigned int match_flags;
111   LANGID found_lang_id;
112 } locale_search_t;
113
114 #define CONTINUE_LOOKING TRUE
115 #define STOP_LOOKING     FALSE
116
117 /* INTERNAL: Get and compare locale info with a given string */
118 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp)
119 {
120   buff[0] = 0;
121   GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
122   if (!buff[0] || !cmp[0])
123     return 0;
124   /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
125   return !strncasecmp(cmp, buff, strlen(cmp));
126 }
127
128 static BOOL CALLBACK
129 find_best_locale_proc(HMODULE hModule WINE_UNUSED, LPCSTR type WINE_UNUSED,
130                       LPCSTR name WINE_UNUSED, WORD LangID, LONG lParam)
131 {
132   locale_search_t *res = (locale_search_t *)lParam;
133   const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
134   char buff[MAX_ELEM_LEN];
135   unsigned int flags = 0;
136
137   if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
138     return CONTINUE_LOOKING;
139
140   /* Check Language */
141   if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
142       compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
143       compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
144   {
145     TRACE(":Found language: %s->%s\n", res->search_language, buff);
146     flags |= FOUND_LANGUAGE;
147     memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
148   }
149   else if (res->match_flags & FOUND_LANGUAGE)
150   {
151     return CONTINUE_LOOKING;
152   }
153
154   /* Check Country */
155   if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
156       compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
157       compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
158   {
159     TRACE("Found country:%s->%s\n", res->search_country, buff);
160     flags |= FOUND_COUNTRY;
161     memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
162   }
163   else if (res->match_flags & FOUND_COUNTRY)
164   {
165     return CONTINUE_LOOKING;
166   }
167
168   /* Check codepage */
169   if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
170       (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
171   {
172     TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
173     flags |= FOUND_CODEPAGE;
174     memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
175   }
176   else if (res->match_flags & FOUND_CODEPAGE)
177   {
178     return CONTINUE_LOOKING;
179   }
180
181   if (flags > res->match_flags)
182   {
183     /* Found a better match than previously */
184     res->match_flags = flags;
185     res->found_lang_id = LangID;
186   }
187   if (flags & (FOUND_LANGUAGE & FOUND_COUNTRY & FOUND_CODEPAGE))
188   {
189     TRACE(":found exact locale match\n");
190     return STOP_LOOKING;
191   }
192   return CONTINUE_LOOKING;
193 }
194
195 extern int atoi(const char *);
196
197 /* Internal: Find the LCID for a locale specification */
198 static LCID MSVCRT_locale_to_LCID(locale_search_t* locale)
199 {
200   LCID lcid;
201   EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
202                          (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
203                          (LONG)locale);
204
205   if (!locale->match_flags)
206     return 0;
207
208   /* If we were given something that didn't match, fail */
209   if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
210     return 0;
211
212   lcid =  MAKELCID(locale->found_lang_id, SORT_DEFAULT);
213
214   /* Populate partial locale, translating LCID to locale string elements */
215   if (!locale->found_codepage[0])
216   {
217     /* Even if a codepage is not enumerated for a locale
218      * it can be set if valid */
219     if (locale->search_codepage[0])
220     {
221       if (IsValidCodePage(atoi(locale->search_codepage)))
222         memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
223       else
224       {
225         /* Special codepage values: OEM & ANSI */
226         if (strcasecmp(locale->search_codepage,"OCP"))
227         {
228           GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
229                          locale->found_codepage, MAX_ELEM_LEN);
230         }
231         if (strcasecmp(locale->search_codepage,"ACP"))
232         {
233           GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
234                          locale->found_codepage, MAX_ELEM_LEN);
235         }
236         else
237           return 0;
238
239         if (!atoi(locale->found_codepage))
240            return 0;
241       }
242     }
243     else
244     {
245       /* Prefer ANSI codepages if present */
246       GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
247                      locale->found_codepage, MAX_ELEM_LEN);
248       if (!locale->found_codepage[0] || !atoi(locale->found_codepage))
249           GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
250                          locale->found_codepage, MAX_ELEM_LEN);
251     }
252   }
253   GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
254                  locale->found_language, MAX_ELEM_LEN);
255   GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
256                  locale->found_country, MAX_ELEM_LEN);
257   return lcid;
258 }
259
260 extern int snprintf(char *, int, const char *, ...);
261
262 /* INTERNAL: Set ctype behaviour for a codepage */
263 static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
264 {
265   CPINFO cp;
266
267   memset(&cp, 0, sizeof(CPINFO));
268
269   if (GetCPInfo(codepage, &cp))
270   {
271     int i;
272     char str[3];
273     unsigned char *traverse = (unsigned char *)cp.LeadByte;
274
275     memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
276     MSVCRT_current_lc_all_cp = codepage;
277
278     /* Switch ctype macros to MBCS if needed */
279     MSVCRT___mb_cur_max = cp.MaxCharSize;
280
281     /* Set remaining ctype flags: FIXME: faster way to do this? */
282     str[1] = str[2] = 0;
283     for (i = 0; i < 256; i++)
284     {
285       if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
286       {
287         str[0] = i;
288         GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
289       }
290     }
291
292     /* Set leadbyte flags */
293     while (traverse[0] || traverse[1])
294     {
295       for( i = traverse[0]; i <= traverse[1]; i++ )
296         MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
297       traverse += 2;
298     };
299   }
300 }
301
302
303 /*********************************************************************
304  *              setlocale (MSVCRT.@)
305  */
306 char* MSVCRT_setlocale(int category, const char* locale)
307 {
308   LCID lcid = 0;
309   locale_search_t lc;
310   int haveLang, haveCountry, haveCP;
311   char* next;
312   int lc_all = 0;
313
314   TRACE("(%d %s)\n",category,locale);
315
316   if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
317     return NULL;
318
319   if (locale == NULL)
320   {
321     /* Report the current Locale */
322     return MSVCRT_current_lc_all;
323   }
324
325   LOCK_LOCALE;
326
327   if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
328   {
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.
333      */
334     UNLOCK_LOCALE;
335     return MSVCRT_current_lc_all;
336   }
337
338   /* Default Locale: Special case handling */
339   if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
340   {
341     MSVCRT_current_lc_all[0] = 'C';
342     MSVCRT_current_lc_all[1] = '\0';
343     MSVCRT_current_lc_all_cp = GetACP();
344
345     switch (category) {
346     case MSVCRT_LC_ALL:
347       lc_all = 1; /* Fall through all cases ... */
348     case MSVCRT_LC_COLLATE:
349       if (!lc_all) break;
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));
355       if (!lc_all) break;
356     case MSVCRT_LC_MONETARY:
357       if (!lc_all) break;
358     case MSVCRT_LC_NUMERIC:
359       if (!lc_all) break;
360     case MSVCRT_LC_TIME:
361       break;
362     }
363     UNLOCK_LOCALE;
364     return MSVCRT_current_lc_all;
365   }
366
367   /* Get locale elements */
368   haveLang = haveCountry = haveCP = 0;
369   memset(&lc,0,sizeof(lc));
370
371   next = strchr(locale,'_');
372   if (next && next != locale)
373   {
374     haveLang = 1;
375     strncpy(lc.search_language,locale,next-locale);
376     locale += next-locale+1;
377   }
378
379   next = strchr(locale,'.');
380   if (next)
381   {
382     haveCP = 1;
383     if (next == locale)
384     {
385       locale++;
386       strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
387     }
388     else
389     {
390       if (haveLang)
391       {
392         haveCountry = 1;
393         strncpy(lc.search_country,locale,next-locale);
394         locale += next-locale+1;
395       }
396       else
397       {
398         haveLang = 1;
399         strncpy(lc.search_language,locale,next-locale);
400         locale += next-locale+1;
401       }
402       strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
403     }
404   }
405   else
406   {
407     if (haveLang)
408     {
409       haveCountry = 1;
410       strncpy(lc.search_country, locale, MAX_ELEM_LEN);
411     }
412     else
413     {
414       haveLang = 1;
415       strncpy(lc.search_language, locale, MAX_ELEM_LEN);
416     }
417   }
418
419   if (haveCountry)
420     remap_synonym(lc.search_country);
421
422   if (haveCP && !haveCountry && !haveLang)
423   {
424     FIXME(":Codepage only locale not implemented\n");
425     /* FIXME: Use default lang/country and skip locale_to_LCID()
426      * call below...
427      */
428     UNLOCK_LOCALE;
429     return NULL;
430   }
431
432   lcid = MSVCRT_locale_to_LCID(&lc);
433
434   TRACE(":found LCID %ld\n",lcid);
435
436   if (lcid == 0)
437   {
438     UNLOCK_LOCALE;
439     return NULL;
440   }
441
442   MSVCRT_current_lc_all_lcid = lcid;
443
444   snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
445            lc.found_language,lc.found_country,lc.found_codepage);
446
447   switch (category) {
448   case MSVCRT_LC_ALL:
449     lc_all = 1; /* Fall through all cases ... */
450   case MSVCRT_LC_COLLATE:
451     if (!lc_all) break;
452   case MSVCRT_LC_CTYPE:
453     msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
454     if (!lc_all) break;
455   case MSVCRT_LC_MONETARY:
456     if (!lc_all) break;
457   case MSVCRT_LC_NUMERIC:
458     if (!lc_all) break;
459   case MSVCRT_LC_TIME:
460     break;
461   }
462   UNLOCK_LOCALE;
463   return MSVCRT_current_lc_all;
464 }
465
466
467 /*********************************************************************
468  *              _Getdays (MSVCRT.@)
469  */
470 const char* _Getdays(void)
471 {
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");
476   return MSVCRT_days;
477 }
478
479 /*********************************************************************
480  *              _Getmonths (MSVCRT.@)
481  */
482 const char* _Getmonths(void)
483 {
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;
490 }
491
492 /*********************************************************************
493  *              _Getnames (MSVCRT.@)
494  */
495 const char* _Getnames(void)
496 {
497   /* FIXME: */
498   TRACE("(void) stub\n");
499   return "";
500 }
501
502 /*********************************************************************
503  *              _Strftime (MSVCRT.@)
504  */
505 const char* _Strftime(char *out, unsigned int len, const char *fmt,
506                                      const void *tm, void *foo)
507 {
508   /* FIXME: */
509   TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
510   return "";
511 }
512
513 /* FIXME: MBCP probably belongs in mbcs.c */
514
515 /*********************************************************************
516  *              _setmbcp (MSVCRT.@)
517  */
518 void _setmbcp(int cp)
519 {
520   LOCK_LOCALE;
521   if (MSVCRT_current_lc_all_cp != cp)
522   {
523     /* FIXME: set ctype behaviour for this cp */
524     MSVCRT_current_lc_all_cp = cp;
525   }
526   UNLOCK_LOCALE;
527 }
528
529 /*********************************************************************
530  *              _getmbcp (MSVCRT.@)
531  */
532 int _getmbcp(void)
533 {
534   return MSVCRT_current_lc_all_cp;
535 }