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