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