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