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