msvcrt: Add support for VC7 and VC8 extensions to the C++ exception handler.
[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 <limits.h>
25 #include <locale.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winnls.h"
33
34 #include "msvcrt.h"
35 #include "mtdll.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 unsigned char charmax = CHAR_MAX;
52
53 /* MT */
54 #define LOCK_LOCALE   _mlock(_SETLOCALE_LOCK);
55 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
56
57 #define MSVCRT_LEADBYTE  0x8000
58
59 /* Friendly country strings & iso codes for synonym support.
60  * Based on MS documentation for setlocale().
61  */
62 static const char * const _country_synonyms[] =
63 {
64   "Hong Kong","HK",
65   "Hong-Kong","HK",
66   "New Zealand","NZ",
67   "New-Zealand","NZ",
68   "PR China","CN",
69   "PR-China","CN",
70   "United Kingdom","GB",
71   "United-Kingdom","GB",
72   "Britain","GB",
73   "England","GB",
74   "Great Britain","GB",
75   "United States","US",
76   "United-States","US",
77   "America","US"
78 };
79
80 /* INTERNAL: Map a synonym to an ISO code */
81 static void remap_synonym(char *name)
82 {
83   unsigned int i;
84   for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
85   {
86     if (!strcasecmp(_country_synonyms[i],name))
87     {
88       TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
89       name[0] = _country_synonyms[i+1][0];
90       name[1] = _country_synonyms[i+1][1];
91       name[2] = '\0';
92       return;
93     }
94   }
95 }
96
97 /* Note: Flags are weighted in order of matching importance */
98 #define FOUND_LANGUAGE         0x4
99 #define FOUND_COUNTRY          0x2
100 #define FOUND_CODEPAGE         0x1
101
102 typedef struct {
103   char search_language[MAX_ELEM_LEN];
104   char search_country[MAX_ELEM_LEN];
105   char search_codepage[MAX_ELEM_LEN];
106   char found_language[MAX_ELEM_LEN];
107   char found_country[MAX_ELEM_LEN];
108   char found_codepage[MAX_ELEM_LEN];
109   unsigned int match_flags;
110   LANGID found_lang_id;
111 } locale_search_t;
112
113 #define CONTINUE_LOOKING TRUE
114 #define STOP_LOOKING     FALSE
115
116 /* INTERNAL: Get and compare locale info with a given string */
117 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp)
118 {
119   buff[0] = 0;
120   GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
121   if (!buff[0] || !cmp[0])
122     return 0;
123   /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
124   return !strncasecmp(cmp, buff, strlen(cmp));
125 }
126
127 static BOOL CALLBACK
128 find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
129 {
130   locale_search_t *res = (locale_search_t *)lParam;
131   const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
132   char buff[MAX_ELEM_LEN];
133   unsigned int flags = 0;
134
135   if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
136     return CONTINUE_LOOKING;
137
138   /* Check Language */
139   if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
140       compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
141       compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
142   {
143     TRACE(":Found language: %s->%s\n", res->search_language, buff);
144     flags |= FOUND_LANGUAGE;
145     memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
146   }
147   else if (res->match_flags & FOUND_LANGUAGE)
148   {
149     return CONTINUE_LOOKING;
150   }
151
152   /* Check Country */
153   if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
154       compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
155       compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
156   {
157     TRACE("Found country:%s->%s\n", res->search_country, buff);
158     flags |= FOUND_COUNTRY;
159     memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
160   }
161   else if (res->match_flags & FOUND_COUNTRY)
162   {
163     return CONTINUE_LOOKING;
164   }
165
166   /* Check codepage */
167   if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
168       (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
169   {
170     TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
171     flags |= FOUND_CODEPAGE;
172     memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
173   }
174   else if (res->match_flags & FOUND_CODEPAGE)
175   {
176     return CONTINUE_LOOKING;
177   }
178
179   if (flags > res->match_flags)
180   {
181     /* Found a better match than previously */
182     res->match_flags = flags;
183     res->found_lang_id = LangID;
184   }
185   if ((flags & (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE)) ==
186         (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE))
187   {
188     TRACE(":found exact locale match\n");
189     return STOP_LOOKING;
190   }
191   return CONTINUE_LOOKING;
192 }
193
194 extern int atoi(const char *);
195
196 /* Internal: Find the LCID for a locale specification */
197 static LCID MSVCRT_locale_to_LCID(locale_search_t* locale)
198 {
199   LCID lcid;
200   EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
201                          (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
202                          (LONG_PTR)locale);
203
204   if (!locale->match_flags)
205     return 0;
206
207   /* If we were given something that didn't match, fail */
208   if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
209     return 0;
210
211   lcid =  MAKELCID(locale->found_lang_id, SORT_DEFAULT);
212
213   /* Populate partial locale, translating LCID to locale string elements */
214   if (!locale->found_codepage[0])
215   {
216     /* Even if a codepage is not enumerated for a locale
217      * it can be set if valid */
218     if (locale->search_codepage[0])
219     {
220       if (IsValidCodePage(atoi(locale->search_codepage)))
221         memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
222       else
223       {
224         /* Special codepage values: OEM & ANSI */
225         if (strcasecmp(locale->search_codepage,"OCP"))
226         {
227           GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
228                          locale->found_codepage, MAX_ELEM_LEN);
229         }
230         else if (strcasecmp(locale->search_codepage,"ACP"))
231         {
232           GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
233                          locale->found_codepage, MAX_ELEM_LEN);
234         }
235         else
236           return 0;
237
238         if (!atoi(locale->found_codepage))
239            return 0;
240       }
241     }
242     else
243     {
244       /* Prefer ANSI codepages if present */
245       GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
246                      locale->found_codepage, MAX_ELEM_LEN);
247       if (!locale->found_codepage[0] || !atoi(locale->found_codepage))
248           GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
249                          locale->found_codepage, MAX_ELEM_LEN);
250     }
251   }
252   GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
253                  locale->found_language, MAX_ELEM_LEN);
254   GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
255                  locale->found_country, MAX_ELEM_LEN);
256   return lcid;
257 }
258
259 /* INTERNAL: Set ctype behaviour for a codepage */
260 static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
261 {
262   CPINFO cp;
263
264   memset(&cp, 0, sizeof(CPINFO));
265
266   if (GetCPInfo(codepage, &cp))
267   {
268     int i;
269     char str[3];
270     unsigned char *traverse = cp.LeadByte;
271
272     memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
273     MSVCRT___lc_codepage = codepage;
274     MSVCRT___lc_collate_cp = codepage;
275
276     /* Switch ctype macros to MBCS if needed */
277     MSVCRT___mb_cur_max = cp.MaxCharSize;
278
279     /* Set remaining ctype flags: FIXME: faster way to do this? */
280     str[1] = str[2] = 0;
281     for (i = 0; i < 256; i++)
282     {
283       if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
284       {
285         str[0] = i;
286         GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
287       }
288     }
289
290     /* Set leadbyte flags */
291     while (traverse[0] || traverse[1])
292     {
293       for( i = traverse[0]; i <= traverse[1]; i++ )
294         MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
295       traverse += 2;
296     };
297   }
298 }
299
300 /* INTERNAL: Set lc_handle, lc_id and lc_category in threadlocinfo struct */
301 BOOL update_threadlocinfo_category(LCID lcid, MSVCRT__locale_t loc, int category)
302 {
303     char buf[256], *p;
304     int len;
305
306     if(GetLocaleInfoA(lcid, LOCALE_ILANGUAGE, buf, 256)) {
307         p = buf;
308
309         loc->locinfo->lc_id[category].wLanguage = 0;
310         while(*p) {
311             loc->locinfo->lc_id[category].wLanguage *= 16;
312
313             if(*p <= '9')
314                 loc->locinfo->lc_id[category].wLanguage += *p-'0';
315             else
316                 loc->locinfo->lc_id[category].wLanguage += *p-'a'+10;
317
318             p++;
319         }
320
321         loc->locinfo->lc_id[category].wCountry =
322             loc->locinfo->lc_id[category].wLanguage;
323     }
324
325     if(GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE, buf, 256))
326         loc->locinfo->lc_id[category].wCodePage = atoi(buf);
327
328     loc->locinfo->lc_handle[category] = lcid;
329
330     len = 0;
331     len += GetLocaleInfoA(lcid, LOCALE_SLANGUAGE, buf, 256);
332     buf[len-1] = '_';
333     len += GetLocaleInfoA(lcid, LOCALE_SCOUNTRY, &buf[len], 256-len);
334     buf[len-1] = '.';
335     len += GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE, &buf[len], 256-len);
336
337     loc->locinfo->lc_category[category].locale = MSVCRT_malloc(sizeof(char[len]));
338     loc->locinfo->lc_category[category].refcount = MSVCRT_malloc(sizeof(int));
339     if(!loc->locinfo->lc_category[category].locale
340             || !loc->locinfo->lc_category[category].refcount) {
341         MSVCRT_free(loc->locinfo->lc_category[category].locale);
342         MSVCRT_free(loc->locinfo->lc_category[category].refcount);
343         loc->locinfo->lc_category[category].locale = NULL;
344         loc->locinfo->lc_category[category].refcount = NULL;
345         return TRUE;
346     }
347     memcpy(loc->locinfo->lc_category[category].locale, buf, sizeof(char[len]));
348     *loc->locinfo->lc_category[category].refcount = 1;
349
350     return FALSE;
351 }
352
353
354 /*********************************************************************
355  *              setlocale (MSVCRT.@)
356  */
357 char* CDECL MSVCRT_setlocale(int category, const char* locale)
358 {
359   LCID lcid = 0;
360   locale_search_t lc;
361   int haveLang, haveCountry, haveCP;
362   char* next;
363   int lc_all = 0;
364
365   TRACE("(%d %s)\n",category,locale);
366
367   if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
368     return NULL;
369
370   if (locale == NULL)
371   {
372     /* Report the current Locale */
373     return MSVCRT_current_lc_all;
374   }
375
376   LOCK_LOCALE;
377
378   if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
379   {
380     FIXME(":restore previous locale not implemented!\n");
381     /* FIXME: Easiest way to do this is parse the string and
382      * call this function recursively with its elements,
383      * Where they differ for each lc_ type.
384      */
385     UNLOCK_LOCALE;
386     return MSVCRT_current_lc_all;
387   }
388
389   /* Default Locale: Special case handling */
390   if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
391   {
392     MSVCRT_current_lc_all[0] = 'C';
393     MSVCRT_current_lc_all[1] = '\0';
394     MSVCRT___lc_codepage = GetACP();
395     MSVCRT___lc_collate_cp = GetACP();
396
397     switch (category) {
398     case MSVCRT_LC_ALL:
399       lc_all = 1; /* Fall through all cases ... */
400     case MSVCRT_LC_COLLATE:
401       if (!lc_all) break;
402     case MSVCRT_LC_CTYPE:
403       /* Restore C locale ctype info */
404       MSVCRT___mb_cur_max = 1;
405       memcpy(MSVCRT_current_ctype, MSVCRT__ctype, sizeof(MSVCRT__ctype));
406       if (!lc_all) break;
407     case MSVCRT_LC_MONETARY:
408       if (!lc_all) break;
409     case MSVCRT_LC_NUMERIC:
410       if (!lc_all) break;
411     case MSVCRT_LC_TIME:
412       break;
413     }
414     UNLOCK_LOCALE;
415     return MSVCRT_current_lc_all;
416   }
417
418   /* Get locale elements */
419   haveLang = haveCountry = haveCP = 0;
420   memset(&lc,0,sizeof(lc));
421
422   next = strchr(locale,'_');
423   if (next && next != locale)
424   {
425     haveLang = 1;
426     memcpy(lc.search_language,locale,next-locale);
427     locale += next-locale+1;
428   }
429
430   next = strchr(locale,'.');
431   if (next)
432   {
433     haveCP = 1;
434     if (next == locale)
435     {
436       locale++;
437       lstrcpynA(lc.search_codepage, locale, MAX_ELEM_LEN);
438     }
439     else
440     {
441       if (haveLang)
442       {
443         haveCountry = 1;
444         memcpy(lc.search_country,locale,next-locale);
445         locale += next-locale+1;
446       }
447       else
448       {
449         haveLang = 1;
450         memcpy(lc.search_language,locale,next-locale);
451         locale += next-locale+1;
452       }
453       lstrcpynA(lc.search_codepage, locale, MAX_ELEM_LEN);
454     }
455   }
456   else
457   {
458     if (haveLang)
459     {
460       haveCountry = 1;
461       lstrcpynA(lc.search_country, locale, MAX_ELEM_LEN);
462     }
463     else
464     {
465       haveLang = 1;
466       lstrcpynA(lc.search_language, locale, MAX_ELEM_LEN);
467     }
468   }
469
470   if (haveCountry)
471     remap_synonym(lc.search_country);
472
473   if (haveCP && !haveCountry && !haveLang)
474   {
475     FIXME(":Codepage only locale not implemented\n");
476     /* FIXME: Use default lang/country and skip locale_to_LCID()
477      * call below...
478      */
479     UNLOCK_LOCALE;
480     return NULL;
481   }
482
483   lcid = MSVCRT_locale_to_LCID(&lc);
484
485   TRACE(":found LCID %d\n",lcid);
486
487   if (lcid == 0)
488   {
489     UNLOCK_LOCALE;
490     return NULL;
491   }
492
493   MSVCRT_current_lc_all_lcid = lcid;
494
495   snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
496            lc.found_language,lc.found_country,lc.found_codepage);
497
498   switch (category) {
499   case MSVCRT_LC_ALL:
500     lc_all = 1; /* Fall through all cases ... */
501   case MSVCRT_LC_COLLATE:
502     if (!lc_all) break;
503   case MSVCRT_LC_CTYPE:
504     msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
505     if (!lc_all) break;
506   case MSVCRT_LC_MONETARY:
507     if (!lc_all) break;
508   case MSVCRT_LC_NUMERIC:
509     if (!lc_all) break;
510   case MSVCRT_LC_TIME:
511     break;
512   }
513   UNLOCK_LOCALE;
514   return MSVCRT_current_lc_all;
515 }
516
517 /*********************************************************************
518  *              wsetlocale (MSVCRT.@)
519  */
520 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
521 {
522   static MSVCRT_wchar_t fake[] = {
523     'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
524     'S','t','a','t','e','s','.','1','2','5','2',0 };
525
526   FIXME("%d %s\n", category, debugstr_w(locale));
527
528   return fake;
529 }
530
531 /*********************************************************************
532  *              _Getdays (MSVCRT.@)
533  */
534 const char* CDECL _Getdays(void)
535 {
536   static const char MSVCRT_days[] = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
537                             "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
538   /* FIXME: Use locale */
539   TRACE("(void) semi-stub\n");
540   return MSVCRT_days;
541 }
542
543 /*********************************************************************
544  *              _Getmonths (MSVCRT.@)
545  */
546 const char* CDECL _Getmonths(void)
547 {
548   static const char MSVCRT_months[] = ":Jan:January:Feb:February:Mar:March:Apr:"
549                 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
550                 "October:Nov:November:Dec:December";
551   /* FIXME: Use locale */
552   TRACE("(void) semi-stub\n");
553   return MSVCRT_months;
554 }
555
556 /*********************************************************************
557  *              _Gettnames (MSVCRT.@)
558  */
559 const char* CDECL _Gettnames(void)
560 {
561   /* FIXME: */
562   TRACE("(void) stub\n");
563   return "";
564 }
565
566 /*********************************************************************
567  *              _Strftime (MSVCRT.@)
568  */
569 const char* CDECL _Strftime(char *out, unsigned int len, const char *fmt,
570                             const void *tm, void *foo)
571 {
572   /* FIXME: */
573   TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
574   return "";
575 }
576
577 /*********************************************************************
578  *              __crtLCMapStringA (MSVCRT.@)
579  */
580 int CDECL __crtLCMapStringA(
581   LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
582   int dstlen, unsigned int codepage, int xflag
583 ) {
584   FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
585         lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
586   /* FIXME: A bit incorrect. But msvcrt itself just converts its
587    * arguments to wide strings and then calls LCMapStringW
588    */
589   return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
590 }
591
592 /*********************************************************************
593  *              __crtCompareStringA (MSVCRT.@)
594  */
595 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
596                                const char *src2, int len2 )
597 {
598     FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
599           lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
600     /* FIXME: probably not entirely right */
601     return CompareStringA( lcid, flags, src1, len1, src2, len2 );
602 }
603
604 /*********************************************************************
605  *              __crtCompareStringW (MSVCRT.@)
606  */
607 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
608                                const MSVCRT_wchar_t *src2, int len2 )
609 {
610     FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
611           lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
612     /* FIXME: probably not entirely right */
613     return CompareStringW( lcid, flags, src1, len1, src2, len2 );
614 }
615
616 /*********************************************************************
617  *              __crtGetLocaleInfoW (MSVCRT.@)
618  */
619 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
620 {
621     FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
622     /* FIXME: probably not entirely right */
623     return GetLocaleInfoW( lcid, type, buffer, len );
624 }
625
626 /*********************************************************************
627  *              localeconv (MSVCRT.@)
628  */
629 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
630 {
631     static struct MSVCRT_lconv xlconv;
632     struct lconv *ylconv = localeconv();
633
634     xlconv.decimal_point     = ylconv->decimal_point;
635     xlconv.thousands_sep     = ylconv->thousands_sep;
636     xlconv.grouping          = ylconv->grouping;  /* FIXME: fixup charmax here too */
637     xlconv.int_curr_symbol   = ylconv->int_curr_symbol;
638     xlconv.currency_symbol   = ylconv->currency_symbol;
639     xlconv.mon_decimal_point = ylconv->mon_decimal_point;
640     xlconv.mon_thousands_sep = ylconv->mon_thousands_sep;
641     xlconv.mon_grouping      = ylconv->mon_grouping;
642     xlconv.positive_sign     = ylconv->positive_sign;
643     xlconv.negative_sign     = ylconv->negative_sign;
644     xlconv.int_frac_digits   = ylconv->int_frac_digits;
645     xlconv.frac_digits       = ylconv->frac_digits;
646     xlconv.p_cs_precedes     = ylconv->p_cs_precedes;
647     xlconv.p_sep_by_space    = ylconv->p_sep_by_space;
648     xlconv.n_cs_precedes     = ylconv->n_cs_precedes;
649     xlconv.n_sep_by_space    = ylconv->n_sep_by_space;
650     xlconv.p_sign_posn       = ylconv->p_sign_posn;
651     xlconv.n_sign_posn       = ylconv->n_sign_posn;
652
653     if (ylconv->int_frac_digits == CHAR_MAX) xlconv.int_frac_digits = charmax;
654     if (ylconv->frac_digits == CHAR_MAX)     xlconv.frac_digits = charmax;
655     if (ylconv->p_cs_precedes == CHAR_MAX)   xlconv.p_cs_precedes = charmax;
656     if (ylconv->p_sep_by_space == CHAR_MAX)  xlconv.p_sep_by_space = charmax;
657     if (ylconv->n_cs_precedes == CHAR_MAX)   xlconv.n_cs_precedes = charmax;
658     if (ylconv->n_sep_by_space == CHAR_MAX)  xlconv.n_sep_by_space = charmax;
659     if (ylconv->p_sign_posn == CHAR_MAX)     xlconv.p_sign_posn = charmax;
660     if (ylconv->n_sign_posn == CHAR_MAX)     xlconv.n_sign_posn = charmax;
661
662     return &xlconv;
663 }
664
665 /*********************************************************************
666  *              __lconv_init (MSVCRT.@)
667  */
668 void CDECL __lconv_init(void)
669 {
670     /* this is used to make chars unsigned */
671     charmax = 255;
672 }
673
674 /*********************************************************************
675  *      ___lc_handle_func (MSVCRT.@)
676  */
677 HANDLE * CDECL ___lc_handle_func(void)
678 {
679     return MSVCRT___lc_handle;
680 }
681
682 /*********************************************************************
683  *      ___lc_codepage_func (MSVCRT.@)
684  */
685 int CDECL ___lc_codepage_func(void)
686 {
687     return MSVCRT___lc_codepage;
688 }
689
690 /*********************************************************************
691  *      ___lc_collate_cp_func (MSVCRT.@)
692  */
693 int CDECL ___lc_collate_cp_func(void)
694 {
695     return MSVCRT___lc_collate_cp;
696 }
697
698 /* _free_locale - not exported in native msvcrt */
699 void CDECL _free_locale(MSVCRT__locale_t locale)
700 {
701     int i;
702     BOOL localeC = TRUE;
703
704     for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
705         if(locale->locinfo->lc_category[i].refcount) {
706             MSVCRT_free(locale->locinfo->lc_category[i].locale);
707             MSVCRT_free(locale->locinfo->lc_category[i].refcount);
708         } else if(localeC) {
709             MSVCRT_free(locale->locinfo->lc_category[i].locale);
710             localeC = FALSE;
711         }
712     }
713
714     if(locale->locinfo->lconv) {
715         MSVCRT_free(locale->locinfo->lconv->decimal_point);
716         MSVCRT_free(locale->locinfo->lconv->thousands_sep);
717         MSVCRT_free(locale->locinfo->lconv->grouping);
718         MSVCRT_free(locale->locinfo->lconv->int_curr_symbol);
719         MSVCRT_free(locale->locinfo->lconv->currency_symbol);
720         MSVCRT_free(locale->locinfo->lconv->mon_decimal_point);
721         MSVCRT_free(locale->locinfo->lconv->mon_thousands_sep);
722         MSVCRT_free(locale->locinfo->lconv->mon_grouping);
723         MSVCRT_free(locale->locinfo->lconv->positive_sign);
724         MSVCRT_free(locale->locinfo->lconv->negative_sign);
725     }
726     MSVCRT_free(locale->locinfo->lconv_intl_refcount);
727     MSVCRT_free(locale->locinfo->lconv_num_refcount);
728     MSVCRT_free(locale->locinfo->lconv_mon_refcount);
729     MSVCRT_free(locale->locinfo->lconv);
730
731     MSVCRT_free(locale->locinfo->ctype1_refcount);
732     MSVCRT_free(locale->locinfo->ctype1);
733
734     MSVCRT_free(locale->locinfo->pclmap);
735     MSVCRT_free(locale->locinfo->pcumap);
736
737     MSVCRT_free(locale->locinfo);
738     MSVCRT_free(locale->mbcinfo);
739     MSVCRT_free(locale);
740 }
741
742 /* _create_locale - not exported in native msvcrt */
743 MSVCRT__locale_t _create_locale(int category, const char *locale)
744 {
745     MSVCRT__locale_t loc;
746     LCID lcid;
747     char buf[256], *localeC;
748     int i;
749
750     TRACE("(%d %s)\n", category, locale);
751
752     if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
753         return NULL;
754
755     if(locale[0]=='C' && !locale[1])
756         lcid = CP_ACP;
757     else if(!locale[0])
758         lcid = GetSystemDefaultLCID();
759     else {
760         locale_search_t search;
761         char *cp, *region;
762
763         memset(&search, 0, sizeof(locale_search_t));
764
765         cp = strchr(locale, '.');
766         region = strchr(locale, '_');
767
768         lstrcpynA(search.search_language, locale, MAX_ELEM_LEN);
769         if(region) {
770             lstrcpynA(search.search_country, region+1, MAX_ELEM_LEN);
771             if(region-locale < MAX_ELEM_LEN)
772                 search.search_language[region-locale] = '\0';
773         } else
774             search.search_country[0] = '\0';
775
776         if(cp) {
777             lstrcpynA(search.search_codepage, cp+1, MAX_ELEM_LEN);
778             if(cp-region < MAX_ELEM_LEN)
779                 search.search_country[cp-region] = '\0';
780             if(cp-locale < MAX_ELEM_LEN)
781                 search.search_language[cp-locale] = '\0';
782         } else
783             search.search_codepage[0] = '\0';
784
785         lcid = MSVCRT_locale_to_LCID(&search);
786         if(!lcid)
787             return NULL;
788     }
789
790     loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
791     if(!loc)
792         return NULL;
793
794     loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
795     if(!loc->locinfo) {
796         MSVCRT_free(loc);
797         return NULL;
798     }
799
800     loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
801     if(!loc->mbcinfo) {
802         MSVCRT_free(loc->locinfo);
803         MSVCRT_free(loc);
804         return NULL;
805     }
806
807     memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
808     memset(loc->mbcinfo, 0, sizeof(MSVCRT_threadmbcinfo));
809
810     if(category!=MSVCRT_LC_ALL || !lcid) {
811         localeC = MSVCRT_malloc(sizeof(char[2]));
812
813         if(localeC) {
814             localeC[0] = 'C';
815             localeC[1] = '\0';
816
817             for(i=MSVCRT_LC_ALL+1; i<=MSVCRT_LC_MAX; i++)
818                 loc->locinfo->lc_category[i].locale = localeC;
819         } else {
820             _free_locale(loc);
821             return NULL;
822         }
823     }
824
825     loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
826     if(!loc->locinfo->lconv) {
827         _free_locale(loc);
828         return NULL;
829     }
830     memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
831
832     loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
833     loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
834     if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
835         _free_locale(loc);
836         return NULL;
837     }
838
839     loc->locinfo->refcount = 1;
840
841     if(lcid && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
842         if(update_threadlocinfo_category(lcid, loc, MSVCRT_LC_COLLATE)) {
843             _free_locale(loc);
844             return NULL;
845         }
846     }
847
848     if(lcid && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
849         CPINFO cp;
850
851         if(update_threadlocinfo_category(lcid, loc, MSVCRT_LC_CTYPE)) {
852             _free_locale(loc);
853             return NULL;
854         }
855
856         loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
857         loc->locinfo->lc_clike = 1;
858         if(!GetCPInfo(loc->locinfo->lc_codepage, &cp)) {
859             _free_locale(loc);
860             return NULL;
861         }
862         loc->locinfo->mb_cur_max = cp.MaxCharSize;
863
864         loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
865         loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
866         if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
867             _free_locale(loc);
868             return NULL;
869         }
870
871         *loc->locinfo->ctype1_refcount = 1;
872         loc->locinfo->ctype1[0] = 0;
873         loc->locinfo->pctype = loc->locinfo->ctype1+1;
874
875         buf[1] = buf[2] = '\0';
876         for(i=1; i<257; i++) {
877             buf[0] = i-1;
878
879             GetStringTypeA(lcid, CT_CTYPE1, buf, 1, loc->locinfo->ctype1+i);
880             loc->locinfo->ctype1[i] |= 0x200;
881         }
882     } else {
883         loc->locinfo->lc_clike = 1;
884         loc->locinfo->mb_cur_max = 1;
885         loc->locinfo->pctype = MSVCRT__ctype+1;
886     }
887
888     for(i=0; i<256; i++)
889         buf[i] = i;
890
891     LCMapStringA(lcid, LCMAP_LOWERCASE, buf, 256, (char*)loc->locinfo->pclmap, 256);
892     LCMapStringA(lcid, LCMAP_UPPERCASE, buf, 256, (char*)loc->locinfo->pcumap, 256);
893
894     loc->mbcinfo->refcount = 1;
895     loc->mbcinfo->mbcodepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
896
897     for(i=0; i<256; i++) {
898         if(loc->locinfo->pclmap[i] != i) {
899             loc->mbcinfo->mbctype[i+1] |= 0x10;
900             loc->mbcinfo->mbcasemap[i] = loc->locinfo->pclmap[i];
901         } else if(loc->locinfo->pcumap[i] != i) {
902             loc->mbcinfo->mbctype[i+1] |= 0x20;
903             loc->mbcinfo->mbcasemap[i] = loc->locinfo->pcumap[i];
904         }
905     }
906
907     if(lcid && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
908         if(update_threadlocinfo_category(lcid, loc, MSVCRT_LC_MONETARY)) {
909             _free_locale(loc);
910             return NULL;
911         }
912
913         loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
914         loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
915         if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
916             _free_locale(loc);
917             return NULL;
918         }
919
920         *loc->locinfo->lconv_intl_refcount = 1;
921         *loc->locinfo->lconv_mon_refcount = 1;
922
923         i = GetLocaleInfoA(lcid, LOCALE_SINTLSYMBOL, buf, 256);
924         if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char[i]))))
925             memcpy(loc->locinfo->lconv->int_curr_symbol, buf, sizeof(char[i]));
926         else {
927             _free_locale(loc);
928             return NULL;
929         }
930
931         i = GetLocaleInfoA(lcid, LOCALE_SCURRENCY, buf, 256);
932         if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char[i]))))
933             memcpy(loc->locinfo->lconv->currency_symbol, buf, sizeof(char[i]));
934         else {
935             _free_locale(loc);
936             return NULL;
937         }
938
939         i = GetLocaleInfoA(lcid, LOCALE_SMONDECIMALSEP, buf, 256);
940         if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char[i]))))
941             memcpy(loc->locinfo->lconv->mon_decimal_point, buf, sizeof(char[i]));
942         else {
943             _free_locale(loc);
944             return NULL;
945         }
946
947         i = GetLocaleInfoA(lcid, LOCALE_SMONTHOUSANDSEP, buf, 256);
948         if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
949             memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, sizeof(char[i]));
950         else {
951             _free_locale(loc);
952             return NULL;
953         }
954
955         i = GetLocaleInfoA(lcid, LOCALE_SMONGROUPING, buf, 256);
956         if(i>1)
957             i = i/2 + (buf[i-2]=='0'?0:1);
958         if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char[i])))) {
959             for(i=0; buf[i+1]==';'; i+=2)
960                 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
961             loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
962             if(buf[i] != '0')
963                 loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
964         } else {
965             _free_locale(loc);
966             return NULL;
967         }
968
969         i = GetLocaleInfoA(lcid, LOCALE_SPOSITIVESIGN, buf, 256);
970         if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char[i]))))
971             memcpy(loc->locinfo->lconv->positive_sign, buf, sizeof(char[i]));
972         else {
973             _free_locale(loc);
974             return NULL;
975         }
976
977         i = GetLocaleInfoA(lcid, LOCALE_SNEGATIVESIGN, buf, 256);
978         if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char[i]))))
979             memcpy(loc->locinfo->lconv->negative_sign, buf, sizeof(char[i]));
980         else {
981             _free_locale(loc);
982             return NULL;
983         }
984
985         if(GetLocaleInfoA(lcid, LOCALE_IINTLCURRDIGITS, buf, 256))
986             loc->locinfo->lconv->int_frac_digits = atoi(buf);
987         else {
988             _free_locale(loc);
989             return NULL;
990         }
991
992         if(GetLocaleInfoA(lcid, LOCALE_ICURRDIGITS, buf, 256))
993             loc->locinfo->lconv->frac_digits = atoi(buf);
994         else {
995             _free_locale(loc);
996             return NULL;
997         }
998
999         if(GetLocaleInfoA(lcid, LOCALE_IPOSSYMPRECEDES, buf, 256))
1000             loc->locinfo->lconv->p_cs_precedes = atoi(buf);
1001         else {
1002             _free_locale(loc);
1003             return NULL;
1004         }
1005
1006         if(GetLocaleInfoA(lcid, LOCALE_IPOSSEPBYSPACE, buf, 256))
1007             loc->locinfo->lconv->p_sep_by_space = atoi(buf);
1008         else {
1009             _free_locale(loc);
1010             return NULL;
1011         }
1012
1013         if(GetLocaleInfoA(lcid, LOCALE_INEGSYMPRECEDES, buf, 256))
1014             loc->locinfo->lconv->n_cs_precedes = atoi(buf);
1015         else {
1016             _free_locale(loc);
1017             return NULL;
1018         }
1019
1020         if(GetLocaleInfoA(lcid, LOCALE_INEGSEPBYSPACE, buf, 256))
1021             loc->locinfo->lconv->n_sep_by_space = atoi(buf);
1022         else {
1023             _free_locale(loc);
1024             return NULL;
1025         }
1026
1027         if(GetLocaleInfoA(lcid, LOCALE_IPOSSIGNPOSN, buf, 256))
1028             loc->locinfo->lconv->p_sign_posn = atoi(buf);
1029         else {
1030             _free_locale(loc);
1031             return NULL;
1032         }
1033
1034         if(GetLocaleInfoA(lcid, LOCALE_INEGSIGNPOSN, buf, 256))
1035             loc->locinfo->lconv->n_sign_posn = atoi(buf);
1036         else {
1037             _free_locale(loc);
1038             return NULL;
1039         }
1040     } else {
1041         loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
1042         loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
1043         loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
1044         loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
1045         loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
1046         loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
1047         loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
1048
1049         if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
1050                 || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
1051                 || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
1052                 || !loc->locinfo->lconv->negative_sign) {
1053             _free_locale(loc);
1054             return NULL;
1055         }
1056
1057         loc->locinfo->lconv->int_curr_symbol[0] = '\0';
1058         loc->locinfo->lconv->currency_symbol[0] = '\0';
1059         loc->locinfo->lconv->mon_decimal_point[0] = '\0';
1060         loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
1061         loc->locinfo->lconv->mon_grouping[0] = '\0';
1062         loc->locinfo->lconv->positive_sign[0] = '\0';
1063         loc->locinfo->lconv->negative_sign[0] = '\0';
1064         loc->locinfo->lconv->int_frac_digits = 127;
1065         loc->locinfo->lconv->frac_digits = 127;
1066         loc->locinfo->lconv->p_cs_precedes = 127;
1067         loc->locinfo->lconv->p_sep_by_space = 127;
1068         loc->locinfo->lconv->n_cs_precedes = 127;
1069         loc->locinfo->lconv->n_sep_by_space = 127;
1070         loc->locinfo->lconv->p_sign_posn = 127;
1071         loc->locinfo->lconv->n_sign_posn = 127;
1072     }
1073
1074     if(lcid && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
1075         if(update_threadlocinfo_category(lcid, loc, MSVCRT_LC_NUMERIC)) {
1076             _free_locale(loc);
1077             return NULL;
1078         }
1079
1080         if(!loc->locinfo->lconv_intl_refcount)
1081             loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1082         loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1083         if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
1084             _free_locale(loc);
1085             return NULL;
1086         }
1087
1088         *loc->locinfo->lconv_intl_refcount = 1;
1089         *loc->locinfo->lconv_num_refcount = 1;
1090
1091         i = GetLocaleInfoA(lcid, LOCALE_SDECIMAL, buf, 256);
1092         if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[i]))))
1093             memcpy(loc->locinfo->lconv->decimal_point, buf, sizeof(char[i]));
1094         else {
1095             _free_locale(loc);
1096             return NULL;
1097         }
1098
1099         i = GetLocaleInfoA(lcid, LOCALE_STHOUSAND, buf, 256);
1100         if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
1101             memcpy(loc->locinfo->lconv->thousands_sep, buf, sizeof(char[i]));
1102         else {
1103             _free_locale(loc);
1104             return NULL;
1105         }
1106
1107         i = GetLocaleInfoA(lcid, LOCALE_SGROUPING, buf, 256);
1108         if(i>1)
1109             i = i/2 + (buf[i-2]=='0'?0:1);
1110         if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char[i])))) {
1111             for(i=0; buf[i+1]==';'; i+=2)
1112                 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1113             loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1114             if(buf[i] != '0')
1115                 loc->locinfo->lconv->grouping[i/2+1] = 127;
1116         } else {
1117             _free_locale(loc);
1118             return NULL;
1119         }
1120     } else {
1121         loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1122         loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1123         loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1124         if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
1125                 || !loc->locinfo->lconv->grouping) {
1126             _free_locale(loc);
1127             return NULL;
1128         }
1129
1130         loc->locinfo->lconv->decimal_point[0] = '.';
1131         loc->locinfo->lconv->decimal_point[1] = '\0';
1132         loc->locinfo->lconv->thousands_sep[0] = '\0';
1133         loc->locinfo->lconv->grouping[0] = '\0';
1134     }
1135
1136     if(lcid && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1137         if(update_threadlocinfo_category(lcid, loc, MSVCRT_LC_TIME)) {
1138             _free_locale(loc);
1139             return NULL;
1140         }
1141     }
1142
1143     return loc;
1144 }