Faster serial speed cases for non Linux systems.
[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 #include "winnt.h"
21 #include "winbase.h"
22 #include "winuser.h"
23
24 #include "msvcrt.h"
25 #include "msvcrt/locale.h"
26 #include "mtdll.h"
27
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
31
32 /* FIXME: Need to hold locale for each LC_* type and aggregate
33  * string to produce lc_all.
34  */
35 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
36 #define MAX_LOCALE_LENGTH 256
37 char MSVCRT_current_lc_all[MAX_LOCALE_LENGTH];
38 LCID MSVCRT_current_lc_all_lcid;
39 int MSVCRT_current_lc_all_cp;
40
41 /* MT */
42 #define LOCK_LOCALE   _mlock(_SETLOCALE_LOCK);
43 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
44
45 /* ctype data modified when the locale changes */
46 extern WORD MSVCRT__ctype [257];
47 extern WORD MSVCRT_current_ctype[257];
48 extern WORD* MSVCRT__pctype;
49
50 /* mbctype data modified when the locale changes */
51 extern int MSVCRT___mb_cur_max;
52 extern unsigned char MSVCRT_mbctype[257];
53
54 #define MSVCRT_LEADBYTE  0x8000
55
56 /* Friendly country strings & iso codes for synonym support.
57  * Based on MS documentation for setlocale().
58  */
59 static const char* _country_synonyms[] =
60 {
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"
75 };
76
77 /* INTERNAL: Map a synonym to an ISO code */
78 static void remap_synonym(char *name)
79 {
80   size_t i;
81   for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
82   {
83     if (!strcasecmp(_country_synonyms[i],name))
84     {
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;
90     }
91   }
92 }
93
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
98
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;
109
110 #define CONTINUE_LOOKING TRUE
111 #define STOP_LOOKING     FALSE
112
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)
115 {
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));
122 }
123
124 static BOOL CALLBACK
125 find_best_locale_proc(HMODULE hModule WINE_UNUSED, LPCSTR type WINE_UNUSED,
126                       LPCSTR name WINE_UNUSED, WORD LangID, LONG lParam)
127 {
128   locale_search_t *res = (locale_search_t *)lParam;
129   const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
130   char buff[MAX_ELEM_LEN];
131   unsigned int flags = 0;
132
133   if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
134     return CONTINUE_LOOKING;
135
136   /* Check Language */
137   if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
138       compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
139       compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
140   {
141     TRACE(":Found language: %s->%s\n", res->search_language, buff);
142     flags |= FOUND_LANGUAGE;
143     memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
144   }
145   else if (res->match_flags & FOUND_LANGUAGE)
146   {
147     return CONTINUE_LOOKING;
148   }
149
150   /* Check Country */
151   if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
152       compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
153       compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
154   {
155     TRACE("Found country:%s->%s\n", res->search_country, buff);
156     flags |= FOUND_COUNTRY;
157     memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
158   }
159   else if (res->match_flags & FOUND_COUNTRY)
160   {
161     return CONTINUE_LOOKING;
162   }
163
164   /* Check codepage */
165   if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
166       (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
167   {
168     TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
169     flags |= FOUND_CODEPAGE;
170     memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
171   }
172   else if (res->match_flags & FOUND_CODEPAGE)
173   {
174     return CONTINUE_LOOKING;
175   }
176
177   if (flags > res->match_flags)
178   {
179     /* Found a better match than previously */
180     res->match_flags = flags;
181     res->found_lang_id = LangID;
182   }
183   if (flags & (FOUND_LANGUAGE & FOUND_COUNTRY & FOUND_CODEPAGE))
184   {
185     TRACE(":found exact locale match\n");
186     return STOP_LOOKING;
187   }
188   return CONTINUE_LOOKING;
189 }
190
191 extern int atoi(const char *);
192
193 /* Internal: Find the LCID for a locale specification */
194 static LCID MSVCRT_locale_to_LCID(locale_search_t* locale)
195 {
196   LCID lcid;
197   EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
198                          (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
199                          (LONG)locale);
200
201   if (!locale->match_flags)
202     return 0;
203
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;
207
208   lcid =  MAKELCID(locale->found_lang_id, SORT_DEFAULT);
209
210   /* Populate partial locale, translating LCID to locale string elements */
211   if (!locale->found_codepage[0])
212   {
213     /* Even if a codepage is not enumerated for a locale
214      * it can be set if valid */
215     if (locale->search_codepage[0])
216     {
217       if (IsValidCodePage(atoi(locale->search_codepage)))
218         memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
219       else
220       {
221         /* Special codepage values: OEM & ANSI */
222         if (strcasecmp(locale->search_codepage,"OCP"))
223         {
224           GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
225                          locale->found_codepage, MAX_ELEM_LEN);
226         }
227         if (strcasecmp(locale->search_codepage,"ACP"))
228         {
229           GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
230                          locale->found_codepage, MAX_ELEM_LEN);
231         }
232         else
233           return 0;
234
235         if (!atoi(locale->found_codepage))
236            return 0;
237       }
238     }
239     else
240     {
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);
247     }
248   }
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;
254 }
255
256 extern int snprintf(char *, int, const char *, ...);
257
258 /* INTERNAL: Set ctype behaviour for a codepage */
259 static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
260 {
261   CPINFO cp;
262
263   memset(&cp, 0, sizeof(CPINFO));
264
265   if (GetCPInfo(codepage, &cp))
266   {
267     int i;
268     char str[3];
269     unsigned char *traverse = (unsigned char *)cp.LeadByte;
270
271     memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
272     MSVCRT_current_lc_all_cp = codepage;
273
274     /* Switch ctype macros to MBCS if needed */
275     MSVCRT___mb_cur_max = cp.MaxCharSize;
276
277     /* Set remaining ctype flags: FIXME: faster way to do this? */
278     str[1] = str[2] = 0;
279     for (i = 0; i < 256; i++)
280     {
281       if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
282       {
283         str[0] = i;
284         GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
285       }
286     }
287
288     /* Set leadbyte flags */
289     while (traverse[0] || traverse[1])
290     {
291       for( i = traverse[0]; i <= traverse[1]; i++ )
292         MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
293       traverse += 2;
294     };
295   }
296 }
297
298
299 /*********************************************************************
300  *              setlocale (MSVCRT.@)
301  */
302 char* MSVCRT_setlocale(int category, const char* locale)
303 {
304   LCID lcid = 0;
305   locale_search_t lc;
306   int haveLang, haveCountry, haveCP;
307   char* next;
308   int lc_all = 0;
309
310   TRACE("(%d %s)\n",category,locale);
311
312   if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
313     return NULL;
314
315   if (locale == NULL)
316   {
317     /* Report the current Locale */
318     return MSVCRT_current_lc_all;
319   }
320
321   LOCK_LOCALE;
322
323   if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
324   {
325     FIXME(":restore previous locale not implemented!\n");
326     /* FIXME: Easiest way to do this is parse the string and
327      * call this function recursively with its elements,
328      * Where they differ for each lc_ type.
329      */
330     UNLOCK_LOCALE;
331     return MSVCRT_current_lc_all;
332   }
333
334   /* Default Locale: Special case handling */
335   if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
336   {
337     MSVCRT_current_lc_all[0] = 'C';
338     MSVCRT_current_lc_all[1] = '\0';
339     MSVCRT_current_lc_all_cp = GetACP();
340
341     switch (category) {
342     case MSVCRT_LC_ALL:
343       lc_all = 1; /* Fall through all cases ... */
344     case MSVCRT_LC_COLLATE:
345       if (!lc_all) break;
346     case MSVCRT_LC_CTYPE:
347       /* Restore C locale ctype info */
348       MSVCRT___mb_cur_max = 1;
349       memcpy(MSVCRT_current_ctype, MSVCRT__ctype, sizeof(MSVCRT__ctype));
350       memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
351       if (!lc_all) break;
352     case MSVCRT_LC_MONETARY:
353       if (!lc_all) break;
354     case MSVCRT_LC_NUMERIC:
355       if (!lc_all) break;
356     case MSVCRT_LC_TIME:
357       break;
358     }
359     UNLOCK_LOCALE;
360     return MSVCRT_current_lc_all;
361   }
362
363   /* Get locale elements */
364   haveLang = haveCountry = haveCP = 0;
365   memset(&lc,0,sizeof(lc));
366
367   next = strchr(locale,'_');
368   if (next && next != locale)
369   {
370     haveLang = 1;
371     strncpy(lc.search_language,locale,next-locale);
372     locale += next-locale+1;
373   }
374
375   next = strchr(locale,'.');
376   if (next)
377   {
378     haveCP = 1;
379     if (next == locale)
380     {
381       locale++;
382       strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
383     }
384     else
385     {
386       if (haveLang)
387       {
388         haveCountry = 1;
389         strncpy(lc.search_country,locale,next-locale);
390         locale += next-locale+1;
391       }
392       else
393       {
394         haveLang = 1;
395         strncpy(lc.search_language,locale,next-locale);
396         locale += next-locale+1;
397       }
398       strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
399     }
400   }
401   else
402   {
403     if (haveLang)
404     {
405       haveCountry = 1;
406       strncpy(lc.search_country, locale, MAX_ELEM_LEN);
407     }
408     else
409     {
410       haveLang = 1;
411       strncpy(lc.search_language, locale, MAX_ELEM_LEN);
412     }
413   }
414
415   if (haveCountry)
416     remap_synonym(lc.search_country);
417
418   if (haveCP && !haveCountry && !haveLang)
419   {
420     FIXME(":Codepage only locale not implemented\n");
421     /* FIXME: Use default lang/country and skip locale_to_LCID()
422      * call below...
423      */
424     UNLOCK_LOCALE;
425     return NULL;
426   }
427
428   lcid = MSVCRT_locale_to_LCID(&lc);
429
430   TRACE(":found LCID %ld\n",lcid);
431
432   if (lcid == 0)
433   {
434     UNLOCK_LOCALE;
435     return NULL;
436   }
437
438   MSVCRT_current_lc_all_lcid = lcid;
439
440   snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
441            lc.found_language,lc.found_country,lc.found_codepage);
442
443   switch (category) {
444   case MSVCRT_LC_ALL:
445     lc_all = 1; /* Fall through all cases ... */
446   case MSVCRT_LC_COLLATE:
447     if (!lc_all) break;
448   case MSVCRT_LC_CTYPE:
449     msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
450     if (!lc_all) break;
451   case MSVCRT_LC_MONETARY:
452     if (!lc_all) break;
453   case MSVCRT_LC_NUMERIC:
454     if (!lc_all) break;
455   case MSVCRT_LC_TIME:
456     break;
457   }
458   UNLOCK_LOCALE;
459   return MSVCRT_current_lc_all;
460 }
461
462
463 /*********************************************************************
464  *              _Getdays (MSVCRT.@)
465  */
466 const char* _Getdays(void)
467 {
468   static const char *MSVCRT_days = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
469                             "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
470   /* FIXME: Use locale */
471   TRACE("(void) semi-stub\n");
472   return MSVCRT_days;
473 }
474
475 /*********************************************************************
476  *              _Getmonths (MSVCRT.@)
477  */
478 const char* _Getmonths(void)
479 {
480   static const char *MSVCRT_months = ":Jan:January:Feb:February:Mar:March:Apr:"
481                 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
482                 "October:Nov:November:Dec:December";
483   /* FIXME: Use locale */
484   TRACE("(void) semi-stub\n");
485   return MSVCRT_months;
486 }
487
488 /*********************************************************************
489  *              _Getnames (MSVCRT.@)
490  */
491 const char* _Getnames(void)
492 {
493   /* FIXME: */
494   TRACE("(void) stub\n");
495   return "";
496 }
497
498 /*********************************************************************
499  *              _Strftime (MSVCRT.@)
500  */
501 const char* _Strftime(char *out, unsigned int len, const char *fmt,
502                                      const void *tm, void *foo)
503 {
504   /* FIXME: */
505   TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
506   return "";
507 }
508
509 /* FIXME: MBCP probably belongs in mbcs.c */
510
511 /*********************************************************************
512  *              _setmbcp (MSVCRT.@)
513  */
514 void _setmbcp(int cp)
515 {
516   LOCK_LOCALE;
517   if (MSVCRT_current_lc_all_cp != cp)
518   {
519     /* FIXME: set ctype behaviour for this cp */
520     MSVCRT_current_lc_all_cp = cp;
521   }
522   UNLOCK_LOCALE;
523 }
524
525 /*********************************************************************
526  *              _getmbcp (MSVCRT.@)
527  */
528 int _getmbcp(void)
529 {
530   return MSVCRT_current_lc_all_cp;
531 }