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