xref: /reactos/sdk/lib/crt/locale/locale.c (revision 1de09c47)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * msvcrt.dll locale functions
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 2000 Jon Griffiths
5c2c66affSColin Finck  *
6c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
7c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
8c2c66affSColin Finck  * License as published by the Free Software Foundation; either
9c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
12c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c2c66affSColin Finck  * Lesser General Public License for more details.
15c2c66affSColin Finck  *
16c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
17c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
18c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19c2c66affSColin Finck  */
20c2c66affSColin Finck 
21c2c66affSColin Finck #include <precomp.h>
22c2c66affSColin Finck #include <locale.h>
23c2c66affSColin Finck 
24c2c66affSColin Finck #include "mbctype.h"
25c2c66affSColin Finck #include <internal/wine/msvcrt.h>
26c2c66affSColin Finck 
27c2c66affSColin Finck #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
28c2c66affSColin Finck #define MAX_LOCALE_LENGTH 256
29c2c66affSColin Finck 
30c2c66affSColin Finck #ifdef _pctype
31c2c66affSColin Finck #error _pctype should not be defined
32c2c66affSColin Finck #endif
33c2c66affSColin Finck 
34*1de09c47STimo Kreuzer #define strcasecmp _stricmp
35*1de09c47STimo Kreuzer #define strncasecmp _strnicmp
36c2c66affSColin Finck unsigned int __lc_codepage = 0;
37c2c66affSColin Finck int MSVCRT___lc_collate_cp = 0;
38c2c66affSColin Finck LCID MSVCRT___lc_handle[LC_MAX - LC_MIN + 1] = { 0 };
39c2c66affSColin Finck int __mb_cur_max = 1;
40c2c66affSColin Finck static unsigned char charmax = CHAR_MAX;
41c2c66affSColin Finck 
42c2c66affSColin Finck unsigned char _mbctype[257] = { 0 };
43c2c66affSColin Finck 
44c2c66affSColin Finck /* MT */
45c2c66affSColin Finck #define LOCK_LOCALE   _mlock(_SETLOCALE_LOCK);
46c2c66affSColin Finck #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
47c2c66affSColin Finck 
48c2c66affSColin Finck #define MSVCRT_LEADBYTE  0x8000
49c2c66affSColin Finck #define MSVCRT_C1_DEFINED 0x200
50c2c66affSColin Finck 
51c2c66affSColin Finck /* Friendly country strings & language names abbreviations. */
52c2c66affSColin Finck static const char * const _country_synonyms[] =
53c2c66affSColin Finck {
54c2c66affSColin Finck     "american", "enu",
55c2c66affSColin Finck     "american english", "enu",
56c2c66affSColin Finck     "american-english", "enu",
57c2c66affSColin Finck     "english-american", "enu",
58c2c66affSColin Finck     "english-us", "enu",
59c2c66affSColin Finck     "english-usa", "enu",
60c2c66affSColin Finck     "us", "enu",
61c2c66affSColin Finck     "usa", "enu",
62c2c66affSColin Finck     "australian", "ena",
63c2c66affSColin Finck     "english-aus", "ena",
64c2c66affSColin Finck     "belgian", "nlb",
65c2c66affSColin Finck     "french-belgian", "frb",
66c2c66affSColin Finck     "canadian", "enc",
67c2c66affSColin Finck     "english-can", "enc",
68c2c66affSColin Finck     "french-canadian", "frc",
69c2c66affSColin Finck     "chinese", "chs",
70c2c66affSColin Finck     "chinese-simplified", "chs",
71c2c66affSColin Finck     "chinese-traditional", "cht",
72c2c66affSColin Finck     "dutch-belgian", "nlb",
73c2c66affSColin Finck     "english-nz", "enz",
74c2c66affSColin Finck     "uk", "eng",
75c2c66affSColin Finck     "english-uk", "eng",
76c2c66affSColin Finck     "french-swiss", "frs",
77c2c66affSColin Finck     "swiss", "des",
78c2c66affSColin Finck     "german-swiss", "des",
79c2c66affSColin Finck     "italian-swiss", "its",
80c2c66affSColin Finck     "german-austrian", "dea",
81c2c66affSColin Finck     "portuguese", "ptb",
82c2c66affSColin Finck     "portuguese-brazil", "ptb",
83c2c66affSColin Finck     "spanish-mexican", "esm",
84c2c66affSColin Finck     "norwegian-bokmal", "nor",
85c2c66affSColin Finck     "norwegian-nynorsk", "non",
86c2c66affSColin Finck     "spanish-modern", "esn"
87c2c66affSColin Finck };
88c2c66affSColin Finck 
89c2c66affSColin Finck /* INTERNAL: Map a synonym to an ISO code */
remap_synonym(char * name)90c2c66affSColin Finck static void remap_synonym(char *name)
91c2c66affSColin Finck {
92c2c66affSColin Finck   unsigned int i;
93c2c66affSColin Finck   for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
94c2c66affSColin Finck   {
95c2c66affSColin Finck     if (!strcasecmp(_country_synonyms[i],name))
96c2c66affSColin Finck     {
97c2c66affSColin Finck       TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
98c2c66affSColin Finck       strcpy(name, _country_synonyms[i+1]);
99c2c66affSColin Finck       return;
100c2c66affSColin Finck     }
101c2c66affSColin Finck   }
102c2c66affSColin Finck }
103c2c66affSColin Finck 
104c2c66affSColin Finck /* Note: Flags are weighted in order of matching importance */
105c2c66affSColin Finck #define FOUND_LANGUAGE         0x4
106c2c66affSColin Finck #define FOUND_COUNTRY          0x2
107c2c66affSColin Finck #define FOUND_CODEPAGE         0x1
108c2c66affSColin Finck 
109c2c66affSColin Finck typedef struct {
110c2c66affSColin Finck   char search_language[MAX_ELEM_LEN];
111c2c66affSColin Finck   char search_country[MAX_ELEM_LEN];
112c2c66affSColin Finck   char search_codepage[MAX_ELEM_LEN];
113c2c66affSColin Finck   char found_codepage[MAX_ELEM_LEN];
114c2c66affSColin Finck   unsigned int match_flags;
115c2c66affSColin Finck   LANGID found_lang_id;
116c2c66affSColin Finck } locale_search_t;
117c2c66affSColin Finck 
118c2c66affSColin Finck #define CONTINUE_LOOKING TRUE
119c2c66affSColin Finck #define STOP_LOOKING     FALSE
120c2c66affSColin Finck 
121c2c66affSColin Finck /* INTERNAL: Get and compare locale info with a given string */
compare_info(LCID lcid,DWORD flags,char * buff,const char * cmp,BOOL exact)122c2c66affSColin Finck static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp, BOOL exact)
123c2c66affSColin Finck {
124c2c66affSColin Finck   int len;
125c2c66affSColin Finck 
126c2c66affSColin Finck   if(!cmp[0])
127c2c66affSColin Finck       return 0;
128c2c66affSColin Finck 
129c2c66affSColin Finck   buff[0] = 0;
130c2c66affSColin Finck   GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE, buff, MAX_ELEM_LEN);
131c2c66affSColin Finck   if (!buff[0])
132c2c66affSColin Finck     return 0;
133c2c66affSColin Finck 
134c2c66affSColin Finck   /* Partial matches are only allowed on language/country names */
135c2c66affSColin Finck   len = strlen(cmp);
136c2c66affSColin Finck   if(exact || len<=3)
137c2c66affSColin Finck     return !strcasecmp(cmp, buff);
138c2c66affSColin Finck   else
139c2c66affSColin Finck     return !strncasecmp(cmp, buff, len);
140c2c66affSColin Finck }
141c2c66affSColin Finck 
142c2c66affSColin Finck static BOOL CALLBACK
find_best_locale_proc(HMODULE hModule,LPCSTR type,LPCSTR name,WORD LangID,LONG_PTR lParam)143c2c66affSColin Finck find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
144c2c66affSColin Finck {
145c2c66affSColin Finck   locale_search_t *res = (locale_search_t *)lParam;
146c2c66affSColin Finck   const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
147c2c66affSColin Finck   char buff[MAX_ELEM_LEN];
148c2c66affSColin Finck   unsigned int flags = 0;
149c2c66affSColin Finck 
150c2c66affSColin Finck   if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
151c2c66affSColin Finck     return CONTINUE_LOOKING;
152c2c66affSColin Finck 
153c2c66affSColin Finck   /* Check Language */
154c2c66affSColin Finck   if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language, TRUE) ||
155c2c66affSColin Finck       compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language, TRUE) ||
156c2c66affSColin Finck       compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language, FALSE))
157c2c66affSColin Finck   {
158c2c66affSColin Finck     TRACE(":Found language: %s->%s\n", res->search_language, buff);
159c2c66affSColin Finck     flags |= FOUND_LANGUAGE;
160c2c66affSColin Finck   }
161c2c66affSColin Finck   else if (res->match_flags & FOUND_LANGUAGE)
162c2c66affSColin Finck   {
163c2c66affSColin Finck     return CONTINUE_LOOKING;
164c2c66affSColin Finck   }
165c2c66affSColin Finck 
166c2c66affSColin Finck   /* Check Country */
167c2c66affSColin Finck   if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country, TRUE) ||
168c2c66affSColin Finck       compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country, TRUE) ||
169c2c66affSColin Finck       compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country, FALSE))
170c2c66affSColin Finck   {
171c2c66affSColin Finck     TRACE("Found country:%s->%s\n", res->search_country, buff);
172c2c66affSColin Finck     flags |= FOUND_COUNTRY;
173c2c66affSColin Finck   }
174c2c66affSColin Finck   else if (!flags && (res->match_flags & FOUND_COUNTRY))
175c2c66affSColin Finck   {
176c2c66affSColin Finck     return CONTINUE_LOOKING;
177c2c66affSColin Finck   }
178c2c66affSColin Finck 
179c2c66affSColin Finck   /* Check codepage */
180c2c66affSColin Finck   if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage, TRUE) ||
181c2c66affSColin Finck       (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage, TRUE)))
182c2c66affSColin Finck   {
183c2c66affSColin Finck     TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
184c2c66affSColin Finck     flags |= FOUND_CODEPAGE;
185c2c66affSColin Finck     memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
186c2c66affSColin Finck   }
187c2c66affSColin Finck   else if (!flags && (res->match_flags & FOUND_CODEPAGE))
188c2c66affSColin Finck   {
189c2c66affSColin Finck     return CONTINUE_LOOKING;
190c2c66affSColin Finck   }
191c2c66affSColin Finck 
192c2c66affSColin Finck   if (flags > res->match_flags)
193c2c66affSColin Finck   {
194c2c66affSColin Finck     /* Found a better match than previously */
195c2c66affSColin Finck     res->match_flags = flags;
196c2c66affSColin Finck     res->found_lang_id = LangID;
197c2c66affSColin Finck   }
198c2c66affSColin Finck   if ((flags & (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE)) ==
199c2c66affSColin Finck         (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE))
200c2c66affSColin Finck   {
201c2c66affSColin Finck     TRACE(":found exact locale match\n");
202c2c66affSColin Finck     return STOP_LOOKING;
203c2c66affSColin Finck   }
204c2c66affSColin Finck   return CONTINUE_LOOKING;
205c2c66affSColin Finck }
206c2c66affSColin Finck 
207c2c66affSColin Finck extern int atoi(const char *);
208c2c66affSColin Finck 
209c2c66affSColin Finck /* Internal: Find the LCID for a locale specification */
MSVCRT_locale_to_LCID(const char * locale,unsigned short * codepage)210c2c66affSColin Finck LCID MSVCRT_locale_to_LCID(const char *locale, unsigned short *codepage)
211c2c66affSColin Finck {
212c2c66affSColin Finck     LCID lcid;
213c2c66affSColin Finck     locale_search_t search;
214c2c66affSColin Finck     const char *cp, *region;
215c2c66affSColin Finck 
216c2c66affSColin Finck     memset(&search, 0, sizeof(locale_search_t));
217c2c66affSColin Finck 
218c2c66affSColin Finck     cp = strchr(locale, '.');
219c2c66affSColin Finck     region = strchr(locale, '_');
220c2c66affSColin Finck 
221c2c66affSColin Finck     lstrcpynA(search.search_language, locale, MAX_ELEM_LEN);
222c2c66affSColin Finck     if(region) {
223c2c66affSColin Finck         lstrcpynA(search.search_country, region+1, MAX_ELEM_LEN);
224c2c66affSColin Finck         if(region-locale < MAX_ELEM_LEN)
225c2c66affSColin Finck             search.search_language[region-locale] = '\0';
226c2c66affSColin Finck     } else
227c2c66affSColin Finck         search.search_country[0] = '\0';
228c2c66affSColin Finck 
229c2c66affSColin Finck     if(cp) {
230c2c66affSColin Finck         lstrcpynA(search.search_codepage, cp+1, MAX_ELEM_LEN);
231c2c66affSColin Finck         if(region && cp-region-1<MAX_ELEM_LEN)
232c2c66affSColin Finck           search.search_country[cp-region-1] = '\0';
233c2c66affSColin Finck         if(cp-locale < MAX_ELEM_LEN)
234c2c66affSColin Finck             search.search_language[cp-locale] = '\0';
235c2c66affSColin Finck     } else
236c2c66affSColin Finck         search.search_codepage[0] = '\0';
237c2c66affSColin Finck 
238c2c66affSColin Finck     if(!search.search_country[0] && !search.search_codepage[0])
239c2c66affSColin Finck         remap_synonym(search.search_language);
240c2c66affSColin Finck 
241c2c66affSColin Finck     EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
242c2c66affSColin Finck             (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
243c2c66affSColin Finck             (LONG_PTR)&search);
244c2c66affSColin Finck 
245c2c66affSColin Finck     if (!search.match_flags)
246c2c66affSColin Finck         return -1;
247c2c66affSColin Finck 
248c2c66affSColin Finck     /* If we were given something that didn't match, fail */
249c2c66affSColin Finck     if (search.search_country[0] && !(search.match_flags & FOUND_COUNTRY))
250c2c66affSColin Finck         return -1;
251c2c66affSColin Finck 
252c2c66affSColin Finck     lcid =  MAKELCID(search.found_lang_id, SORT_DEFAULT);
253c2c66affSColin Finck 
254c2c66affSColin Finck     /* Populate partial locale, translating LCID to locale string elements */
255c2c66affSColin Finck     if (!(search.match_flags & FOUND_CODEPAGE)) {
256c2c66affSColin Finck         /* Even if a codepage is not enumerated for a locale
257c2c66affSColin Finck          * it can be set if valid */
258c2c66affSColin Finck         if (search.search_codepage[0]) {
259c2c66affSColin Finck             if (IsValidCodePage(atoi(search.search_codepage)))
260c2c66affSColin Finck                 memcpy(search.found_codepage,search.search_codepage,MAX_ELEM_LEN);
261c2c66affSColin Finck             else {
262c2c66affSColin Finck                 /* Special codepage values: OEM & ANSI */
263c2c66affSColin Finck                 if (!strcasecmp(search.search_codepage,"OCP")) {
264c2c66affSColin Finck                     GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
265c2c66affSColin Finck                             search.found_codepage, MAX_ELEM_LEN);
266c2c66affSColin Finck                 } else if (!strcasecmp(search.search_codepage,"ACP")) {
267c2c66affSColin Finck                     GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
268c2c66affSColin Finck                             search.found_codepage, MAX_ELEM_LEN);
269c2c66affSColin Finck                 } else
270c2c66affSColin Finck                     return -1;
271c2c66affSColin Finck 
272c2c66affSColin Finck                 if (!atoi(search.found_codepage))
273c2c66affSColin Finck                     return -1;
274c2c66affSColin Finck             }
275c2c66affSColin Finck         } else {
276c2c66affSColin Finck             /* Prefer ANSI codepages if present */
277c2c66affSColin Finck             GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
278c2c66affSColin Finck                     search.found_codepage, MAX_ELEM_LEN);
279c2c66affSColin Finck             if (!search.found_codepage[0] || !atoi(search.found_codepage))
280c2c66affSColin Finck                 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
281c2c66affSColin Finck                         search.found_codepage, MAX_ELEM_LEN);
282c2c66affSColin Finck         }
283c2c66affSColin Finck     }
284c2c66affSColin Finck     if (codepage)
285c2c66affSColin Finck         *codepage = atoi(search.found_codepage);
286c2c66affSColin Finck 
287c2c66affSColin Finck     return lcid;
288c2c66affSColin Finck }
289c2c66affSColin Finck 
290c2c66affSColin Finck /* INTERNAL: Set lc_handle, lc_id and lc_category in threadlocinfo struct */
update_threadlocinfo_category(LCID lcid,unsigned short cp,MSVCRT__locale_t loc,int category)291c2c66affSColin Finck static BOOL update_threadlocinfo_category(LCID lcid, unsigned short cp,
292c2c66affSColin Finck         MSVCRT__locale_t loc, int category)
293c2c66affSColin Finck {
294c2c66affSColin Finck     char buf[256], *p;
295c2c66affSColin Finck     int len;
296c2c66affSColin Finck 
297c2c66affSColin Finck     if(GetLocaleInfoA(lcid, LOCALE_ILANGUAGE|LOCALE_NOUSEROVERRIDE, buf, 256)) {
298c2c66affSColin Finck         p = buf;
299c2c66affSColin Finck 
300c2c66affSColin Finck         loc->locinfo->lc_id[category].wLanguage = 0;
301c2c66affSColin Finck         while(*p) {
302c2c66affSColin Finck             loc->locinfo->lc_id[category].wLanguage *= 16;
303c2c66affSColin Finck 
304c2c66affSColin Finck             if(*p <= '9')
305c2c66affSColin Finck                 loc->locinfo->lc_id[category].wLanguage += *p-'0';
306c2c66affSColin Finck             else
307c2c66affSColin Finck                 loc->locinfo->lc_id[category].wLanguage += *p-'a'+10;
308c2c66affSColin Finck 
309c2c66affSColin Finck             p++;
310c2c66affSColin Finck         }
311c2c66affSColin Finck 
312c2c66affSColin Finck         loc->locinfo->lc_id[category].wCountry =
313c2c66affSColin Finck             loc->locinfo->lc_id[category].wLanguage;
314c2c66affSColin Finck     }
315c2c66affSColin Finck 
316c2c66affSColin Finck     loc->locinfo->lc_id[category].wCodePage = cp;
317c2c66affSColin Finck 
318c2c66affSColin Finck     loc->locinfo->lc_handle[category] = lcid;
319c2c66affSColin Finck 
320c2c66affSColin Finck     len = 0;
321c2c66affSColin Finck     len += GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE
322c2c66affSColin Finck             |LOCALE_NOUSEROVERRIDE, buf, 256);
323c2c66affSColin Finck     buf[len-1] = '_';
324c2c66affSColin Finck     len += GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY
325c2c66affSColin Finck             |LOCALE_NOUSEROVERRIDE, &buf[len], 256-len);
326c2c66affSColin Finck     buf[len-1] = '.';
327c2c66affSColin Finck     sprintf(buf+len, "%u", cp);
328c2c66affSColin Finck     len += strlen(buf+len)+1;
329c2c66affSColin Finck 
330c2c66affSColin Finck     loc->locinfo->lc_category[category].locale = MSVCRT_malloc(len);
331c2c66affSColin Finck     loc->locinfo->lc_category[category].refcount = MSVCRT_malloc(sizeof(int));
332c2c66affSColin Finck     if(!loc->locinfo->lc_category[category].locale
333c2c66affSColin Finck             || !loc->locinfo->lc_category[category].refcount) {
334c2c66affSColin Finck         MSVCRT_free(loc->locinfo->lc_category[category].locale);
335c2c66affSColin Finck         MSVCRT_free(loc->locinfo->lc_category[category].refcount);
336c2c66affSColin Finck         loc->locinfo->lc_category[category].locale = NULL;
337c2c66affSColin Finck         loc->locinfo->lc_category[category].refcount = NULL;
338c2c66affSColin Finck         return TRUE;
339c2c66affSColin Finck     }
340c2c66affSColin Finck     memcpy(loc->locinfo->lc_category[category].locale, buf, len);
341c2c66affSColin Finck     *loc->locinfo->lc_category[category].refcount = 1;
342c2c66affSColin Finck 
343c2c66affSColin Finck     return FALSE;
344c2c66affSColin Finck }
345c2c66affSColin Finck 
346c2c66affSColin Finck /* INTERNAL: swap pointers values */
swap_pointers(void ** p1,void ** p2)347c2c66affSColin Finck static inline void swap_pointers(void **p1, void **p2) {
348c2c66affSColin Finck     void *hlp;
349c2c66affSColin Finck 
350c2c66affSColin Finck     hlp = *p1;
351c2c66affSColin Finck     *p1 = *p2;
352c2c66affSColin Finck     *p2 = hlp;
353c2c66affSColin Finck }
354c2c66affSColin Finck 
355c2c66affSColin Finck /* INTERNAL: returns pthreadlocinfo struct */
get_locinfo(void)356c2c66affSColin Finck MSVCRT_pthreadlocinfo get_locinfo(void) {
357c2c66affSColin Finck     thread_data_t *data = msvcrt_get_thread_data();
358c2c66affSColin Finck 
359c2c66affSColin Finck     if(!data || !data->have_locale)
360c2c66affSColin Finck         return MSVCRT_locale->locinfo;
361c2c66affSColin Finck 
362c2c66affSColin Finck     return data->locinfo;
363c2c66affSColin Finck }
364c2c66affSColin Finck 
365c2c66affSColin Finck /* INTERNAL: returns pthreadlocinfo struct */
get_mbcinfo(void)366c2c66affSColin Finck MSVCRT_pthreadmbcinfo get_mbcinfo(void) {
367c2c66affSColin Finck     thread_data_t *data = msvcrt_get_thread_data();
368c2c66affSColin Finck 
369c2c66affSColin Finck     if(!data || !data->have_locale)
370c2c66affSColin Finck         return MSVCRT_locale->mbcinfo;
371c2c66affSColin Finck 
372c2c66affSColin Finck     return data->mbcinfo;
373c2c66affSColin Finck }
374c2c66affSColin Finck 
375c2c66affSColin Finck /* INTERNAL: constructs string returned by setlocale */
construct_lc_all(MSVCRT_pthreadlocinfo locinfo)376c2c66affSColin Finck static inline char* construct_lc_all(MSVCRT_pthreadlocinfo locinfo) {
377c2c66affSColin Finck     static char current_lc_all[MAX_LOCALE_LENGTH];
378c2c66affSColin Finck 
379c2c66affSColin Finck     int i;
380c2c66affSColin Finck 
381c2c66affSColin Finck     for(i=MSVCRT_LC_MIN+1; i<MSVCRT_LC_MAX; i++) {
382c2c66affSColin Finck         if(strcmp(locinfo->lc_category[i].locale,
383c2c66affSColin Finck                     locinfo->lc_category[i+1].locale))
384c2c66affSColin Finck             break;
385c2c66affSColin Finck     }
386c2c66affSColin Finck 
387c2c66affSColin Finck     if(i==MSVCRT_LC_MAX)
388c2c66affSColin Finck         return locinfo->lc_category[MSVCRT_LC_COLLATE].locale;
389c2c66affSColin Finck 
390c2c66affSColin Finck     sprintf(current_lc_all,
391c2c66affSColin Finck             "LC_COLLATE=%s;LC_CTYPE=%s;LC_MONETARY=%s;LC_NUMERIC=%s;LC_TIME=%s",
392c2c66affSColin Finck             locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
393c2c66affSColin Finck             locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
394c2c66affSColin Finck             locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
395c2c66affSColin Finck             locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
396c2c66affSColin Finck             locinfo->lc_category[MSVCRT_LC_TIME].locale);
397c2c66affSColin Finck 
398c2c66affSColin Finck     return current_lc_all;
399c2c66affSColin Finck }
400c2c66affSColin Finck 
401c2c66affSColin Finck 
402c2c66affSColin Finck /*********************************************************************
403c2c66affSColin Finck  *		wsetlocale (MSVCRT.@)
404c2c66affSColin Finck  */
_wsetlocale(int category,const wchar_t * locale)405c2c66affSColin Finck wchar_t* CDECL _wsetlocale(int category, const wchar_t* locale)
406c2c66affSColin Finck {
407c2c66affSColin Finck   static wchar_t fake[] = {
408c2c66affSColin Finck     'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
409c2c66affSColin Finck     'S','t','a','t','e','s','.','1','2','5','2',0 };
410c2c66affSColin Finck 
411c2c66affSColin Finck   FIXME("%d %s\n", category, debugstr_w(locale));
412c2c66affSColin Finck 
413c2c66affSColin Finck   return fake;
414c2c66affSColin Finck }
415c2c66affSColin Finck 
416c2c66affSColin Finck /*********************************************************************
417c2c66affSColin Finck  *		_Getdays (MSVCRT.@)
418c2c66affSColin Finck  */
_Getdays(void)419c2c66affSColin Finck char* CDECL _Getdays(void)
420c2c66affSColin Finck {
421c2c66affSColin Finck     MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
422c2c66affSColin Finck     int i, len, size;
423c2c66affSColin Finck     char *out;
424c2c66affSColin Finck 
425c2c66affSColin Finck     TRACE("\n");
426c2c66affSColin Finck 
427c2c66affSColin Finck     size = cur->str.names.short_mon[0]-cur->str.names.short_wday[0];
428c2c66affSColin Finck     out = MSVCRT_malloc(size+1);
429c2c66affSColin Finck     if(!out)
430c2c66affSColin Finck         return NULL;
431c2c66affSColin Finck 
432c2c66affSColin Finck     size = 0;
433c2c66affSColin Finck     for(i=0; i<7; i++) {
434c2c66affSColin Finck         out[size++] = ':';
435c2c66affSColin Finck         len = strlen(cur->str.names.short_wday[i]);
436c2c66affSColin Finck         memcpy(&out[size], cur->str.names.short_wday[i], len);
437c2c66affSColin Finck         size += len;
438c2c66affSColin Finck 
439c2c66affSColin Finck         out[size++] = ':';
440c2c66affSColin Finck         len = strlen(cur->str.names.wday[i]);
441c2c66affSColin Finck         memcpy(&out[size], cur->str.names.wday[i], len);
442c2c66affSColin Finck         size += len;
443c2c66affSColin Finck     }
444c2c66affSColin Finck     out[size] = '\0';
445c2c66affSColin Finck 
446c2c66affSColin Finck     return out;
447c2c66affSColin Finck }
448c2c66affSColin Finck 
449c2c66affSColin Finck /*********************************************************************
450c2c66affSColin Finck  *		_Getmonths (MSVCRT.@)
451c2c66affSColin Finck  */
_Getmonths(void)452c2c66affSColin Finck char* CDECL _Getmonths(void)
453c2c66affSColin Finck {
454c2c66affSColin Finck     MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
455c2c66affSColin Finck     int i, len, size;
456c2c66affSColin Finck     char *out;
457c2c66affSColin Finck 
458c2c66affSColin Finck     TRACE("\n");
459c2c66affSColin Finck 
460c2c66affSColin Finck     size = cur->str.names.am-cur->str.names.short_mon[0];
461c2c66affSColin Finck     out = MSVCRT_malloc(size+1);
462c2c66affSColin Finck     if(!out)
463c2c66affSColin Finck         return NULL;
464c2c66affSColin Finck 
465c2c66affSColin Finck     size = 0;
466c2c66affSColin Finck     for(i=0; i<12; i++) {
467c2c66affSColin Finck         out[size++] = ':';
468c2c66affSColin Finck         len = strlen(cur->str.names.short_mon[i]);
469c2c66affSColin Finck         memcpy(&out[size], cur->str.names.short_mon[i], len);
470c2c66affSColin Finck         size += len;
471c2c66affSColin Finck 
472c2c66affSColin Finck         out[size++] = ':';
473c2c66affSColin Finck         len = strlen(cur->str.names.mon[i]);
474c2c66affSColin Finck         memcpy(&out[size], cur->str.names.mon[i], len);
475c2c66affSColin Finck         size += len;
476c2c66affSColin Finck     }
477c2c66affSColin Finck     out[size] = '\0';
478c2c66affSColin Finck 
479c2c66affSColin Finck     return out;
480c2c66affSColin Finck }
481c2c66affSColin Finck 
482c2c66affSColin Finck /*********************************************************************
483c2c66affSColin Finck  *		_Gettnames (MSVCRT.@)
484c2c66affSColin Finck  */
_Gettnames(void)485c2c66affSColin Finck void* CDECL _Gettnames(void)
486c2c66affSColin Finck {
487c2c66affSColin Finck     MSVCRT___lc_time_data *ret, *cur = get_locinfo()->lc_time_curr;
488c2c66affSColin Finck     unsigned int i, size = sizeof(MSVCRT___lc_time_data);
489c2c66affSColin Finck 
490c2c66affSColin Finck     TRACE("\n");
491c2c66affSColin Finck 
492c2c66affSColin Finck     for(i=0; i<sizeof(cur->str.str)/sizeof(cur->str.str[0]); i++)
493c2c66affSColin Finck         size += strlen(cur->str.str[i])+1;
494c2c66affSColin Finck 
495c2c66affSColin Finck     ret = MSVCRT_malloc(size);
496c2c66affSColin Finck     if(!ret)
497c2c66affSColin Finck         return NULL;
498c2c66affSColin Finck     memcpy(ret, cur, size);
499c2c66affSColin Finck 
500c2c66affSColin Finck     size = 0;
501c2c66affSColin Finck     for(i=0; i<sizeof(cur->str.str)/sizeof(cur->str.str[0]); i++) {
502c2c66affSColin Finck         ret->str.str[i] = &ret->data[size];
503c2c66affSColin Finck         size += strlen(&ret->data[size])+1;
504c2c66affSColin Finck     }
505c2c66affSColin Finck 
506c2c66affSColin Finck     return ret;
507c2c66affSColin Finck }
508c2c66affSColin Finck 
509c2c66affSColin Finck /*********************************************************************
510c2c66affSColin Finck  *		__crtLCMapStringA (MSVCRT.@)
511c2c66affSColin Finck  */
__crtLCMapStringA(LCID lcid,DWORD mapflags,const char * src,int srclen,char * dst,int dstlen,unsigned int codepage,int xflag)512c2c66affSColin Finck int CDECL __crtLCMapStringA(
513c2c66affSColin Finck   LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
514c2c66affSColin Finck   int dstlen, unsigned int codepage, int xflag
515c2c66affSColin Finck ) {
516c2c66affSColin Finck   FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
517c2c66affSColin Finck         lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
518c2c66affSColin Finck   /* FIXME: A bit incorrect. But msvcrt itself just converts its
519c2c66affSColin Finck    * arguments to wide strings and then calls LCMapStringW
520c2c66affSColin Finck    */
521c2c66affSColin Finck   return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
522c2c66affSColin Finck }
523c2c66affSColin Finck 
524c2c66affSColin Finck /*********************************************************************
525c2c66affSColin Finck  *              __crtLCMapStringW (MSVCRT.@)
526c2c66affSColin Finck  */
__crtLCMapStringW(LCID lcid,DWORD mapflags,const wchar_t * src,int srclen,wchar_t * dst,int dstlen,unsigned int codepage,int xflag)527c2c66affSColin Finck int CDECL __crtLCMapStringW(LCID lcid, DWORD mapflags, const wchar_t *src,
528c2c66affSColin Finck         int srclen, wchar_t *dst, int dstlen, unsigned int codepage, int xflag)
529c2c66affSColin Finck {
530c2c66affSColin Finck     FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
531c2c66affSColin Finck             lcid, mapflags, debugstr_w(src), srclen, dst, dstlen, codepage, xflag);
532c2c66affSColin Finck 
533c2c66affSColin Finck     return LCMapStringW(lcid, mapflags, src, srclen, dst, dstlen);
534c2c66affSColin Finck }
535c2c66affSColin Finck 
536c2c66affSColin Finck /*********************************************************************
537c2c66affSColin Finck  *		__crtCompareStringA (MSVCRT.@)
538c2c66affSColin Finck  */
__crtCompareStringA(LCID lcid,DWORD flags,const char * src1,int len1,const char * src2,int len2)539c2c66affSColin Finck int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
540c2c66affSColin Finck                                const char *src2, int len2 )
541c2c66affSColin Finck {
542c2c66affSColin Finck     FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
543c2c66affSColin Finck           lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
544c2c66affSColin Finck     /* FIXME: probably not entirely right */
545c2c66affSColin Finck     return CompareStringA( lcid, flags, src1, len1, src2, len2 );
546c2c66affSColin Finck }
547c2c66affSColin Finck 
548c2c66affSColin Finck /*********************************************************************
549c2c66affSColin Finck  *		__crtCompareStringW (MSVCRT.@)
550c2c66affSColin Finck  */
__crtCompareStringW(LCID lcid,DWORD flags,const wchar_t * src1,int len1,const wchar_t * src2,int len2)551c2c66affSColin Finck int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const wchar_t *src1, int len1,
552c2c66affSColin Finck                                const wchar_t *src2, int len2 )
553c2c66affSColin Finck {
554c2c66affSColin Finck     FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
555c2c66affSColin Finck           lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
556c2c66affSColin Finck     /* FIXME: probably not entirely right */
557c2c66affSColin Finck     return CompareStringW( lcid, flags, src1, len1, src2, len2 );
558c2c66affSColin Finck }
559c2c66affSColin Finck 
560c2c66affSColin Finck /*********************************************************************
561c2c66affSColin Finck  *		__crtGetLocaleInfoW (MSVCRT.@)
562c2c66affSColin Finck  */
__crtGetLocaleInfoW(LCID lcid,LCTYPE type,wchar_t * buffer,int len)563c2c66affSColin Finck int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, wchar_t *buffer, int len )
564c2c66affSColin Finck {
565c2c66affSColin Finck     FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
566c2c66affSColin Finck     /* FIXME: probably not entirely right */
567c2c66affSColin Finck     return GetLocaleInfoW( lcid, type, buffer, len );
568c2c66affSColin Finck }
569c2c66affSColin Finck 
570c2c66affSColin Finck /*********************************************************************
571c2c66affSColin Finck  *              btowc(MSVCRT.@)
572c2c66affSColin Finck  */
MSVCRT_btowc(int c)573c2c66affSColin Finck wint_t CDECL MSVCRT_btowc(int c)
574c2c66affSColin Finck {
575c2c66affSColin Finck     unsigned char letter = c;
576c2c66affSColin Finck     wchar_t ret;
577c2c66affSColin Finck 
578c2c66affSColin Finck     if(!MultiByteToWideChar(get_locinfo()->lc_handle[LC_CTYPE],
579c2c66affSColin Finck                 0, (LPCSTR)&letter, 1, &ret, 1))
580c2c66affSColin Finck         return 0;
581c2c66affSColin Finck 
582c2c66affSColin Finck     return ret;
583c2c66affSColin Finck }
584c2c66affSColin Finck 
585c2c66affSColin Finck /*********************************************************************
586c2c66affSColin Finck  *              __crtGetStringTypeW(MSVCRT.@)
587c2c66affSColin Finck  *
588c2c66affSColin Finck  * This function was accepting different number of arguments in older
589c2c66affSColin Finck  * versions of msvcrt.
590c2c66affSColin Finck  */
__crtGetStringTypeW(DWORD unk,DWORD type,wchar_t * buffer,int len,WORD * out)591c2c66affSColin Finck BOOL CDECL __crtGetStringTypeW(DWORD unk, DWORD type,
592c2c66affSColin Finck         wchar_t *buffer, int len, WORD *out)
593c2c66affSColin Finck {
594c2c66affSColin Finck     FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
595c2c66affSColin Finck             unk, type, buffer, len, out);
596c2c66affSColin Finck 
597c2c66affSColin Finck     return GetStringTypeW(type, buffer, len, out);
598c2c66affSColin Finck }
599c2c66affSColin Finck 
600c2c66affSColin Finck /*********************************************************************
601c2c66affSColin Finck  *		localeconv (MSVCRT.@)
602c2c66affSColin Finck  */
localeconv(void)603c2c66affSColin Finck struct lconv * CDECL localeconv(void)
604c2c66affSColin Finck {
605c2c66affSColin Finck     return (struct lconv*)get_locinfo()->lconv;
606c2c66affSColin Finck }
607c2c66affSColin Finck 
608c2c66affSColin Finck /*********************************************************************
609c2c66affSColin Finck  *		__lconv_init (MSVCRT.@)
610c2c66affSColin Finck  */
__lconv_init(void)611c2c66affSColin Finck int CDECL __lconv_init(void)
612c2c66affSColin Finck {
613c2c66affSColin Finck     /* this is used to make chars unsigned */
614c2c66affSColin Finck     charmax = 255;
615c2c66affSColin Finck     return 0;
616c2c66affSColin Finck }
617c2c66affSColin Finck 
618c2c66affSColin Finck /*********************************************************************
619c2c66affSColin Finck  *      ___lc_handle_func (MSVCRT.@)
620c2c66affSColin Finck  */
___lc_handle_func(void)621c2c66affSColin Finck LCID* CDECL ___lc_handle_func(void)
622c2c66affSColin Finck {
623c2c66affSColin Finck     return MSVCRT___lc_handle;
624c2c66affSColin Finck }
625c2c66affSColin Finck 
626c2c66affSColin Finck /*********************************************************************
627c2c66affSColin Finck  *      ___lc_codepage_func (MSVCRT.@)
628c2c66affSColin Finck  */
___lc_codepage_func(void)629c2c66affSColin Finck unsigned int CDECL ___lc_codepage_func(void)
630c2c66affSColin Finck {
631c2c66affSColin Finck     return __lc_codepage;
632c2c66affSColin Finck }
633c2c66affSColin Finck 
634c2c66affSColin Finck /*********************************************************************
635c2c66affSColin Finck  *      ___lc_collate_cp_func (MSVCRT.@)
636c2c66affSColin Finck  */
___lc_collate_cp_func(void)637b05cc362STimo Kreuzer unsigned int CDECL ___lc_collate_cp_func(void)
638c2c66affSColin Finck {
639c2c66affSColin Finck     return get_locinfo()->lc_collate_cp;
640c2c66affSColin Finck }
641c2c66affSColin Finck 
642c2c66affSColin Finck /* INTERNAL: frees MSVCRT_pthreadlocinfo struct */
free_locinfo(MSVCRT_pthreadlocinfo locinfo)643c2c66affSColin Finck void free_locinfo(MSVCRT_pthreadlocinfo locinfo)
644c2c66affSColin Finck {
645c2c66affSColin Finck     int i;
646c2c66affSColin Finck 
647c2c66affSColin Finck     if(!locinfo)
648c2c66affSColin Finck         return;
649c2c66affSColin Finck 
650c2c66affSColin Finck     if(InterlockedDecrement(&locinfo->refcount))
651c2c66affSColin Finck         return;
652c2c66affSColin Finck 
653c2c66affSColin Finck     for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
654c2c66affSColin Finck         MSVCRT_free(locinfo->lc_category[i].locale);
655c2c66affSColin Finck         MSVCRT_free(locinfo->lc_category[i].refcount);
656c2c66affSColin Finck     }
657c2c66affSColin Finck 
658c2c66affSColin Finck     if(locinfo->lconv) {
659c2c66affSColin Finck         MSVCRT_free(locinfo->lconv->decimal_point);
660c2c66affSColin Finck         MSVCRT_free(locinfo->lconv->thousands_sep);
661c2c66affSColin Finck         MSVCRT_free(locinfo->lconv->grouping);
662c2c66affSColin Finck         MSVCRT_free(locinfo->lconv->int_curr_symbol);
663c2c66affSColin Finck         MSVCRT_free(locinfo->lconv->currency_symbol);
664c2c66affSColin Finck         MSVCRT_free(locinfo->lconv->mon_decimal_point);
665c2c66affSColin Finck         MSVCRT_free(locinfo->lconv->mon_thousands_sep);
666c2c66affSColin Finck         MSVCRT_free(locinfo->lconv->mon_grouping);
667c2c66affSColin Finck         MSVCRT_free(locinfo->lconv->positive_sign);
668c2c66affSColin Finck         MSVCRT_free(locinfo->lconv->negative_sign);
669c2c66affSColin Finck     }
670c2c66affSColin Finck     MSVCRT_free(locinfo->lconv_intl_refcount);
671c2c66affSColin Finck     MSVCRT_free(locinfo->lconv_num_refcount);
672c2c66affSColin Finck     MSVCRT_free(locinfo->lconv_mon_refcount);
673c2c66affSColin Finck     MSVCRT_free(locinfo->lconv);
674c2c66affSColin Finck 
675c2c66affSColin Finck     MSVCRT_free(locinfo->ctype1_refcount);
676c2c66affSColin Finck     MSVCRT_free(locinfo->ctype1);
677c2c66affSColin Finck 
678c2c66affSColin Finck     MSVCRT_free(locinfo->pclmap);
679c2c66affSColin Finck     MSVCRT_free(locinfo->pcumap);
680c2c66affSColin Finck 
681c2c66affSColin Finck     MSVCRT_free(locinfo->lc_time_curr);
682c2c66affSColin Finck 
683c2c66affSColin Finck     MSVCRT_free(locinfo);
684c2c66affSColin Finck }
685c2c66affSColin Finck 
686c2c66affSColin Finck /* INTERNAL: frees MSVCRT_pthreadmbcinfo struct */
free_mbcinfo(MSVCRT_pthreadmbcinfo mbcinfo)687c2c66affSColin Finck void free_mbcinfo(MSVCRT_pthreadmbcinfo mbcinfo)
688c2c66affSColin Finck {
689c2c66affSColin Finck     if(!mbcinfo)
690c2c66affSColin Finck         return;
691c2c66affSColin Finck 
692c2c66affSColin Finck     if(InterlockedDecrement(&mbcinfo->refcount))
693c2c66affSColin Finck         return;
694c2c66affSColin Finck 
695c2c66affSColin Finck     MSVCRT_free(mbcinfo);
696c2c66affSColin Finck }
697c2c66affSColin Finck 
698c2c66affSColin Finck /* _get_current_locale - not exported in native msvcrt */
MSVCRT__get_current_locale(void)699c2c66affSColin Finck MSVCRT__locale_t CDECL MSVCRT__get_current_locale(void)
700c2c66affSColin Finck {
701c2c66affSColin Finck     MSVCRT__locale_t loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
702c2c66affSColin Finck     if(!loc)
703c2c66affSColin Finck         return NULL;
704c2c66affSColin Finck 
705c2c66affSColin Finck     loc->locinfo = get_locinfo();
706c2c66affSColin Finck     loc->mbcinfo = get_mbcinfo();
707c2c66affSColin Finck     InterlockedIncrement(&loc->locinfo->refcount);
708c2c66affSColin Finck     InterlockedIncrement(&loc->mbcinfo->refcount);
709c2c66affSColin Finck     return loc;
710c2c66affSColin Finck }
711c2c66affSColin Finck 
712c2c66affSColin Finck /* _free_locale - not exported in native msvcrt */
MSVCRT__free_locale(MSVCRT__locale_t locale)713c2c66affSColin Finck void CDECL MSVCRT__free_locale(MSVCRT__locale_t locale)
714c2c66affSColin Finck {
715c2c66affSColin Finck     if (!locale)
716c2c66affSColin Finck         return;
717c2c66affSColin Finck 
718c2c66affSColin Finck     free_locinfo(locale->locinfo);
719c2c66affSColin Finck     free_mbcinfo(locale->mbcinfo);
720c2c66affSColin Finck     MSVCRT_free(locale);
721c2c66affSColin Finck }
722c2c66affSColin Finck 
723c2c66affSColin Finck /* _create_locale - not exported in native msvcrt */
MSVCRT__create_locale(int category,const char * locale)724c2c66affSColin Finck MSVCRT__locale_t CDECL MSVCRT__create_locale(int category, const char *locale)
725c2c66affSColin Finck {
726c2c66affSColin Finck     static const DWORD time_data[] = {
727c2c66affSColin Finck         LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1, LOCALE_SABBREVDAYNAME2,
728c2c66affSColin Finck         LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4, LOCALE_SABBREVDAYNAME5,
729c2c66affSColin Finck         LOCALE_SABBREVDAYNAME6,
730c2c66affSColin Finck         LOCALE_SDAYNAME7, LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3,
731c2c66affSColin Finck         LOCALE_SDAYNAME4, LOCALE_SDAYNAME5, LOCALE_SDAYNAME6,
732c2c66affSColin Finck         LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2, LOCALE_SABBREVMONTHNAME3,
733c2c66affSColin Finck         LOCALE_SABBREVMONTHNAME4, LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
734c2c66affSColin Finck         LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8, LOCALE_SABBREVMONTHNAME9,
735c2c66affSColin Finck         LOCALE_SABBREVMONTHNAME10, LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12,
736c2c66affSColin Finck         LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME4,
737c2c66affSColin Finck         LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8,
738c2c66affSColin Finck         LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12,
739c2c66affSColin Finck         LOCALE_S1159, LOCALE_S2359,
740c2c66affSColin Finck         LOCALE_SSHORTDATE, LOCALE_SLONGDATE,
741c2c66affSColin Finck         LOCALE_STIMEFORMAT
742c2c66affSColin Finck     };
743c2c66affSColin Finck     static const char collate[] = "COLLATE=";
744c2c66affSColin Finck     static const char ctype[] = "CTYPE=";
745c2c66affSColin Finck     static const char monetary[] = "MONETARY=";
746c2c66affSColin Finck     static const char numeric[] = "NUMERIC=";
747c2c66affSColin Finck     static const char time[] = "TIME=";
748c2c66affSColin Finck     static const char cloc_short_date[] = "MM/dd/yy";
749c2c66affSColin Finck     static const wchar_t cloc_short_dateW[] = {'M','M','/','d','d','/','y','y',0};
750c2c66affSColin Finck     static const char cloc_long_date[] = "dddd, MMMM dd, yyyy";
751c2c66affSColin Finck     static const wchar_t cloc_long_dateW[] = {'d','d','d','d',',',' ','M','M','M','M',' ','d','d',',',' ','y','y','y','y',0};
752c2c66affSColin Finck     static const char cloc_time[] = "HH:mm:ss";
753c2c66affSColin Finck     static const wchar_t cloc_timeW[] = {'H','H',':','m','m',':','s','s',0};
754c2c66affSColin Finck 
755c2c66affSColin Finck     MSVCRT__locale_t loc;
756c2c66affSColin Finck     LCID lcid[6] = { 0 }, lcid_tmp;
757c2c66affSColin Finck     unsigned short cp[6] = { 0 };
758c2c66affSColin Finck     char buf[256];
759c2c66affSColin Finck     int i, ret, size;
760c2c66affSColin Finck 
761c2c66affSColin Finck     TRACE("(%d %s)\n", category, locale);
762c2c66affSColin Finck 
763c2c66affSColin Finck     if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
764c2c66affSColin Finck         return NULL;
765c2c66affSColin Finck 
766c2c66affSColin Finck     if(locale[0]=='C' && !locale[1]) {
767c2c66affSColin Finck         lcid[0] = 0;
768c2c66affSColin Finck         cp[0] = CP_ACP;
769c2c66affSColin Finck     } else if(!locale[0]) {
770c2c66affSColin Finck         lcid[0] = GetSystemDefaultLCID();
771c2c66affSColin Finck         GetLocaleInfoA(lcid[0], LOCALE_IDEFAULTANSICODEPAGE
772c2c66affSColin Finck                 |LOCALE_NOUSEROVERRIDE, buf, sizeof(buf));
773c2c66affSColin Finck         cp[0] = atoi(buf);
774c2c66affSColin Finck 
775c2c66affSColin Finck         for(i=1; i<6; i++) {
776c2c66affSColin Finck             lcid[i] = lcid[0];
777c2c66affSColin Finck             cp[i] = cp[0];
778c2c66affSColin Finck         }
779c2c66affSColin Finck     } else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
780c2c66affSColin Finck         const char *p;
781c2c66affSColin Finck 
782c2c66affSColin Finck         while(1) {
783c2c66affSColin Finck             locale += 3; /* LC_ */
784c2c66affSColin Finck             if(!memcmp(locale, collate, sizeof(collate)-1)) {
785c2c66affSColin Finck                 i = MSVCRT_LC_COLLATE;
786c2c66affSColin Finck                 locale += sizeof(collate)-1;
787c2c66affSColin Finck             } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
788c2c66affSColin Finck                 i = MSVCRT_LC_CTYPE;
789c2c66affSColin Finck                 locale += sizeof(ctype)-1;
790c2c66affSColin Finck             } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
791c2c66affSColin Finck                 i = MSVCRT_LC_MONETARY;
792c2c66affSColin Finck                 locale += sizeof(monetary)-1;
793c2c66affSColin Finck             } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
794c2c66affSColin Finck                 i = MSVCRT_LC_NUMERIC;
795c2c66affSColin Finck                 locale += sizeof(numeric)-1;
796c2c66affSColin Finck             } else if(!memcmp(locale, time, sizeof(time)-1)) {
797c2c66affSColin Finck                 i = MSVCRT_LC_TIME;
798c2c66affSColin Finck                 locale += sizeof(time)-1;
799c2c66affSColin Finck             } else
800c2c66affSColin Finck                 return NULL;
801c2c66affSColin Finck 
802c2c66affSColin Finck             p = strchr(locale, ';');
803c2c66affSColin Finck             if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0')) {
804c2c66affSColin Finck                 lcid[i] = 0;
805c2c66affSColin Finck                 cp[i] = CP_ACP;
806c2c66affSColin Finck             } else if(p) {
807c2c66affSColin Finck                 memcpy(buf, locale, p-locale);
808c2c66affSColin Finck                 buf[p-locale] = '\0';
809c2c66affSColin Finck                 lcid[i] = MSVCRT_locale_to_LCID(buf, &cp[i]);
810c2c66affSColin Finck             } else
811c2c66affSColin Finck                 lcid[i] = MSVCRT_locale_to_LCID(locale, &cp[i]);
812c2c66affSColin Finck 
813c2c66affSColin Finck             if(lcid[i] == -1)
814c2c66affSColin Finck                 return NULL;
815c2c66affSColin Finck 
816c2c66affSColin Finck             if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
817c2c66affSColin Finck                 break;
818c2c66affSColin Finck 
819c2c66affSColin Finck             locale = p+1;
820c2c66affSColin Finck         }
821c2c66affSColin Finck     } else {
822c2c66affSColin Finck         lcid[0] = MSVCRT_locale_to_LCID(locale, &cp[0]);
823c2c66affSColin Finck         if(lcid[0] == -1)
824c2c66affSColin Finck             return NULL;
825c2c66affSColin Finck 
826c2c66affSColin Finck         for(i=1; i<6; i++) {
827c2c66affSColin Finck             lcid[i] = lcid[0];
828c2c66affSColin Finck             cp[i] = cp[0];
829c2c66affSColin Finck         }
830c2c66affSColin Finck     }
831c2c66affSColin Finck 
832c2c66affSColin Finck     loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
833c2c66affSColin Finck     if(!loc)
834c2c66affSColin Finck         return NULL;
835c2c66affSColin Finck 
836c2c66affSColin Finck     loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
837c2c66affSColin Finck     if(!loc->locinfo) {
838c2c66affSColin Finck         MSVCRT_free(loc);
839c2c66affSColin Finck         return NULL;
840c2c66affSColin Finck     }
841c2c66affSColin Finck 
842c2c66affSColin Finck     loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
843c2c66affSColin Finck     if(!loc->mbcinfo) {
844c2c66affSColin Finck         MSVCRT_free(loc->locinfo);
845c2c66affSColin Finck         MSVCRT_free(loc);
846c2c66affSColin Finck         return NULL;
847c2c66affSColin Finck     }
848c2c66affSColin Finck 
849c2c66affSColin Finck     memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
850c2c66affSColin Finck     loc->locinfo->refcount = 1;
851c2c66affSColin Finck     loc->mbcinfo->refcount = 1;
852c2c66affSColin Finck 
853c2c66affSColin Finck     loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
854c2c66affSColin Finck     if(!loc->locinfo->lconv) {
855c2c66affSColin Finck         MSVCRT__free_locale(loc);
856c2c66affSColin Finck         return NULL;
857c2c66affSColin Finck     }
858c2c66affSColin Finck     memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
859c2c66affSColin Finck 
860c2c66affSColin Finck     loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
861c2c66affSColin Finck     loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
862c2c66affSColin Finck     if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
863c2c66affSColin Finck         MSVCRT__free_locale(loc);
864c2c66affSColin Finck         return NULL;
865c2c66affSColin Finck     }
866c2c66affSColin Finck 
867c2c66affSColin Finck     if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
868c2c66affSColin Finck         if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE], cp[MSVCRT_LC_COLLATE], loc, MSVCRT_LC_COLLATE)) {
869c2c66affSColin Finck             MSVCRT__free_locale(loc);
870c2c66affSColin Finck             return NULL;
871c2c66affSColin Finck         }
872c2c66affSColin Finck 
873c2c66affSColin Finck         loc->locinfo->lc_collate_cp = loc->locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage;
874c2c66affSColin Finck     } else
875c2c66affSColin Finck         loc->locinfo->lc_category[LC_COLLATE].locale = _strdup("C");
876c2c66affSColin Finck 
877c2c66affSColin Finck     if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
878c2c66affSColin Finck         CPINFO cp_info;
879c2c66affSColin Finck         int j;
880c2c66affSColin Finck 
881c2c66affSColin Finck         if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE], cp[MSVCRT_LC_CTYPE], loc, MSVCRT_LC_CTYPE)) {
882c2c66affSColin Finck             MSVCRT__free_locale(loc);
883c2c66affSColin Finck             return NULL;
884c2c66affSColin Finck         }
885c2c66affSColin Finck 
886c2c66affSColin Finck         loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
887c2c66affSColin Finck         loc->locinfo->lc_clike = 1;
888c2c66affSColin Finck         if(!GetCPInfo(loc->locinfo->lc_codepage, &cp_info)) {
889c2c66affSColin Finck             MSVCRT__free_locale(loc);
890c2c66affSColin Finck             return NULL;
891c2c66affSColin Finck         }
892c2c66affSColin Finck         loc->locinfo->mb_cur_max = cp_info.MaxCharSize;
893c2c66affSColin Finck 
894c2c66affSColin Finck         loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
895c2c66affSColin Finck         loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
896c2c66affSColin Finck         if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
897c2c66affSColin Finck             MSVCRT__free_locale(loc);
898c2c66affSColin Finck             return NULL;
899c2c66affSColin Finck         }
900c2c66affSColin Finck 
901c2c66affSColin Finck         *loc->locinfo->ctype1_refcount = 1;
902c2c66affSColin Finck         loc->locinfo->ctype1[0] = 0;
903c2c66affSColin Finck         loc->locinfo->pctype = loc->locinfo->ctype1+1;
904c2c66affSColin Finck 
905c2c66affSColin Finck         buf[1] = buf[2] = '\0';
906c2c66affSColin Finck         for(i=1; i<257; i++) {
907c2c66affSColin Finck             buf[0] = i-1;
908c2c66affSColin Finck 
909c2c66affSColin Finck             /* builtin GetStringTypeA doesn't set output to 0 on invalid input */
910c2c66affSColin Finck             loc->locinfo->ctype1[i] = 0;
911c2c66affSColin Finck 
912c2c66affSColin Finck             GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
913c2c66affSColin Finck                     1, loc->locinfo->ctype1+i);
914c2c66affSColin Finck         }
915c2c66affSColin Finck 
916c2c66affSColin Finck         for(i=0; cp_info.LeadByte[i+1]!=0; i+=2)
917c2c66affSColin Finck             for(j=cp_info.LeadByte[i]; j<=cp_info.LeadByte[i+1]; j++)
918c2c66affSColin Finck                 loc->locinfo->ctype1[j+1] |= _LEADBYTE;
919c2c66affSColin Finck     } else {
920c2c66affSColin Finck         loc->locinfo->lc_clike = 1;
921c2c66affSColin Finck         loc->locinfo->mb_cur_max = 1;
922c2c66affSColin Finck         loc->locinfo->pctype = _ctype+1;
923c2c66affSColin Finck         loc->locinfo->lc_category[LC_CTYPE].locale = _strdup("C");
924c2c66affSColin Finck     }
925c2c66affSColin Finck 
926c2c66affSColin Finck     for(i=0; i<256; i++) {
927c2c66affSColin Finck         if(loc->locinfo->pctype[i] & _LEADBYTE)
928c2c66affSColin Finck             buf[i] = ' ';
929c2c66affSColin Finck         else
930c2c66affSColin Finck             buf[i] = i;
931c2c66affSColin Finck 
932c2c66affSColin Finck     }
933c2c66affSColin Finck 
934c2c66affSColin Finck     if(lcid[MSVCRT_LC_CTYPE]) {
935c2c66affSColin Finck         LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
936c2c66affSColin Finck                 (char*)loc->locinfo->pclmap, 256);
937c2c66affSColin Finck         LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
938c2c66affSColin Finck                 (char*)loc->locinfo->pcumap, 256);
939c2c66affSColin Finck     } else {
940c2c66affSColin Finck         for(i=0; i<256; i++) {
941c2c66affSColin Finck             loc->locinfo->pclmap[i] = (i>='A' && i<='Z' ? i-'A'+'a' : i);
942c2c66affSColin Finck             loc->locinfo->pcumap[i] = (i>='a' && i<='z' ? i-'a'+'A' : i);
943c2c66affSColin Finck         }
944c2c66affSColin Finck     }
945c2c66affSColin Finck 
946c2c66affSColin Finck     _setmbcp_l(loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage, lcid[MSVCRT_LC_CTYPE], loc->mbcinfo);
947c2c66affSColin Finck 
948c2c66affSColin Finck     if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
949c2c66affSColin Finck         if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY], cp[MSVCRT_LC_MONETARY], loc, MSVCRT_LC_MONETARY)) {
950c2c66affSColin Finck             MSVCRT__free_locale(loc);
951c2c66affSColin Finck             return NULL;
952c2c66affSColin Finck         }
953c2c66affSColin Finck 
954c2c66affSColin Finck         loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
955c2c66affSColin Finck         loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
956c2c66affSColin Finck         if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
957c2c66affSColin Finck             MSVCRT__free_locale(loc);
958c2c66affSColin Finck             return NULL;
959c2c66affSColin Finck         }
960c2c66affSColin Finck 
961c2c66affSColin Finck         *loc->locinfo->lconv_intl_refcount = 1;
962c2c66affSColin Finck         *loc->locinfo->lconv_mon_refcount = 1;
963c2c66affSColin Finck 
964c2c66affSColin Finck         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
965c2c66affSColin Finck                 |LOCALE_NOUSEROVERRIDE, buf, 256);
966c2c66affSColin Finck         if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(i)))
967c2c66affSColin Finck             memcpy(loc->locinfo->lconv->int_curr_symbol, buf, i);
968c2c66affSColin Finck         else {
969c2c66affSColin Finck             MSVCRT__free_locale(loc);
970c2c66affSColin Finck             return NULL;
971c2c66affSColin Finck         }
972c2c66affSColin Finck 
973c2c66affSColin Finck         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
974c2c66affSColin Finck                 |LOCALE_NOUSEROVERRIDE, buf, 256);
975c2c66affSColin Finck         if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(i)))
976c2c66affSColin Finck             memcpy(loc->locinfo->lconv->currency_symbol, buf, i);
977c2c66affSColin Finck         else {
978c2c66affSColin Finck             MSVCRT__free_locale(loc);
979c2c66affSColin Finck             return NULL;
980c2c66affSColin Finck         }
981c2c66affSColin Finck 
982c2c66affSColin Finck         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
983c2c66affSColin Finck                 |LOCALE_NOUSEROVERRIDE, buf, 256);
984c2c66affSColin Finck         if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(i)))
985c2c66affSColin Finck             memcpy(loc->locinfo->lconv->mon_decimal_point, buf, i);
986c2c66affSColin Finck         else {
987c2c66affSColin Finck             MSVCRT__free_locale(loc);
988c2c66affSColin Finck             return NULL;
989c2c66affSColin Finck         }
990c2c66affSColin Finck 
991c2c66affSColin Finck         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
992c2c66affSColin Finck                 |LOCALE_NOUSEROVERRIDE, buf, 256);
993c2c66affSColin Finck         if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(i)))
994c2c66affSColin Finck             memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, i);
995c2c66affSColin Finck         else {
996c2c66affSColin Finck             MSVCRT__free_locale(loc);
997c2c66affSColin Finck             return NULL;
998c2c66affSColin Finck         }
999c2c66affSColin Finck 
1000c2c66affSColin Finck         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING
1001c2c66affSColin Finck                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1002c2c66affSColin Finck         if(i>1)
1003c2c66affSColin Finck             i = i/2 + (buf[i-2]=='0'?0:1);
1004c2c66affSColin Finck         if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(i))) {
1005c2c66affSColin Finck             for(i=0; buf[i+1]==';'; i+=2)
1006c2c66affSColin Finck                 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1007c2c66affSColin Finck             loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1008c2c66affSColin Finck             if(buf[i] != '0')
1009c2c66affSColin Finck                 loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
1010c2c66affSColin Finck         } else {
1011c2c66affSColin Finck             MSVCRT__free_locale(loc);
1012c2c66affSColin Finck             return NULL;
1013c2c66affSColin Finck         }
1014c2c66affSColin Finck 
1015c2c66affSColin Finck         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
1016c2c66affSColin Finck                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1017c2c66affSColin Finck         if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(i)))
1018c2c66affSColin Finck             memcpy(loc->locinfo->lconv->positive_sign, buf, i);
1019c2c66affSColin Finck         else {
1020c2c66affSColin Finck             MSVCRT__free_locale(loc);
1021c2c66affSColin Finck             return NULL;
1022c2c66affSColin Finck         }
1023c2c66affSColin Finck 
1024c2c66affSColin Finck         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
1025c2c66affSColin Finck                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1026c2c66affSColin Finck         if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(i)))
1027c2c66affSColin Finck             memcpy(loc->locinfo->lconv->negative_sign, buf, i);
1028c2c66affSColin Finck         else {
1029c2c66affSColin Finck             MSVCRT__free_locale(loc);
1030c2c66affSColin Finck             return NULL;
1031c2c66affSColin Finck         }
1032c2c66affSColin Finck 
1033c2c66affSColin Finck         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS
1034c2c66affSColin Finck                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1035c2c66affSColin Finck             loc->locinfo->lconv->int_frac_digits = atoi(buf);
1036c2c66affSColin Finck         else {
1037c2c66affSColin Finck             MSVCRT__free_locale(loc);
1038c2c66affSColin Finck             return NULL;
1039c2c66affSColin Finck         }
1040c2c66affSColin Finck 
1041c2c66affSColin Finck         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS
1042c2c66affSColin Finck                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1043c2c66affSColin Finck             loc->locinfo->lconv->frac_digits = atoi(buf);
1044c2c66affSColin Finck         else {
1045c2c66affSColin Finck             MSVCRT__free_locale(loc);
1046c2c66affSColin Finck             return NULL;
1047c2c66affSColin Finck         }
1048c2c66affSColin Finck 
1049c2c66affSColin Finck         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES
1050c2c66affSColin Finck                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1051c2c66affSColin Finck             loc->locinfo->lconv->p_cs_precedes = atoi(buf);
1052c2c66affSColin Finck         else {
1053c2c66affSColin Finck             MSVCRT__free_locale(loc);
1054c2c66affSColin Finck             return NULL;
1055c2c66affSColin Finck         }
1056c2c66affSColin Finck 
1057c2c66affSColin Finck         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE
1058c2c66affSColin Finck                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1059c2c66affSColin Finck             loc->locinfo->lconv->p_sep_by_space = atoi(buf);
1060c2c66affSColin Finck         else {
1061c2c66affSColin Finck             MSVCRT__free_locale(loc);
1062c2c66affSColin Finck             return NULL;
1063c2c66affSColin Finck         }
1064c2c66affSColin Finck 
1065c2c66affSColin Finck         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES
1066c2c66affSColin Finck                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1067c2c66affSColin Finck             loc->locinfo->lconv->n_cs_precedes = atoi(buf);
1068c2c66affSColin Finck         else {
1069c2c66affSColin Finck             MSVCRT__free_locale(loc);
1070c2c66affSColin Finck             return NULL;
1071c2c66affSColin Finck         }
1072c2c66affSColin Finck 
1073c2c66affSColin Finck         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE
1074c2c66affSColin Finck                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1075c2c66affSColin Finck             loc->locinfo->lconv->n_sep_by_space = atoi(buf);
1076c2c66affSColin Finck         else {
1077c2c66affSColin Finck             MSVCRT__free_locale(loc);
1078c2c66affSColin Finck             return NULL;
1079c2c66affSColin Finck         }
1080c2c66affSColin Finck 
1081c2c66affSColin Finck         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN
1082c2c66affSColin Finck                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1083c2c66affSColin Finck             loc->locinfo->lconv->p_sign_posn = atoi(buf);
1084c2c66affSColin Finck         else {
1085c2c66affSColin Finck             MSVCRT__free_locale(loc);
1086c2c66affSColin Finck             return NULL;
1087c2c66affSColin Finck         }
1088c2c66affSColin Finck 
1089c2c66affSColin Finck         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN
1090c2c66affSColin Finck                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1091c2c66affSColin Finck             loc->locinfo->lconv->n_sign_posn = atoi(buf);
1092c2c66affSColin Finck         else {
1093c2c66affSColin Finck             MSVCRT__free_locale(loc);
1094c2c66affSColin Finck             return NULL;
1095c2c66affSColin Finck         }
1096c2c66affSColin Finck     } else {
1097c2c66affSColin Finck         loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
1098c2c66affSColin Finck         loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
1099c2c66affSColin Finck         loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
1100c2c66affSColin Finck         loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
1101c2c66affSColin Finck         loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
1102c2c66affSColin Finck         loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
1103c2c66affSColin Finck         loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
1104c2c66affSColin Finck 
1105c2c66affSColin Finck         if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
1106c2c66affSColin Finck                 || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
1107c2c66affSColin Finck                 || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
1108c2c66affSColin Finck                 || !loc->locinfo->lconv->negative_sign) {
1109c2c66affSColin Finck             MSVCRT__free_locale(loc);
1110c2c66affSColin Finck             return NULL;
1111c2c66affSColin Finck         }
1112c2c66affSColin Finck 
1113c2c66affSColin Finck         loc->locinfo->lconv->int_curr_symbol[0] = '\0';
1114c2c66affSColin Finck         loc->locinfo->lconv->currency_symbol[0] = '\0';
1115c2c66affSColin Finck         loc->locinfo->lconv->mon_decimal_point[0] = '\0';
1116c2c66affSColin Finck         loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
1117c2c66affSColin Finck         loc->locinfo->lconv->mon_grouping[0] = '\0';
1118c2c66affSColin Finck         loc->locinfo->lconv->positive_sign[0] = '\0';
1119c2c66affSColin Finck         loc->locinfo->lconv->negative_sign[0] = '\0';
1120c2c66affSColin Finck         loc->locinfo->lconv->int_frac_digits = 127;
1121c2c66affSColin Finck         loc->locinfo->lconv->frac_digits = 127;
1122c2c66affSColin Finck         loc->locinfo->lconv->p_cs_precedes = 127;
1123c2c66affSColin Finck         loc->locinfo->lconv->p_sep_by_space = 127;
1124c2c66affSColin Finck         loc->locinfo->lconv->n_cs_precedes = 127;
1125c2c66affSColin Finck         loc->locinfo->lconv->n_sep_by_space = 127;
1126c2c66affSColin Finck         loc->locinfo->lconv->p_sign_posn = 127;
1127c2c66affSColin Finck         loc->locinfo->lconv->n_sign_posn = 127;
1128c2c66affSColin Finck 
1129c2c66affSColin Finck         loc->locinfo->lc_category[LC_MONETARY].locale = _strdup("C");
1130c2c66affSColin Finck     }
1131c2c66affSColin Finck 
1132c2c66affSColin Finck     if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
1133c2c66affSColin Finck         if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC], cp[MSVCRT_LC_NUMERIC], loc, MSVCRT_LC_NUMERIC)) {
1134c2c66affSColin Finck             MSVCRT__free_locale(loc);
1135c2c66affSColin Finck             return NULL;
1136c2c66affSColin Finck         }
1137c2c66affSColin Finck 
1138c2c66affSColin Finck         if(!loc->locinfo->lconv_intl_refcount)
1139c2c66affSColin Finck             loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1140c2c66affSColin Finck         loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1141c2c66affSColin Finck         if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
1142c2c66affSColin Finck             MSVCRT__free_locale(loc);
1143c2c66affSColin Finck             return NULL;
1144c2c66affSColin Finck         }
1145c2c66affSColin Finck 
1146c2c66affSColin Finck         *loc->locinfo->lconv_intl_refcount = 1;
1147c2c66affSColin Finck         *loc->locinfo->lconv_num_refcount = 1;
1148c2c66affSColin Finck 
1149c2c66affSColin Finck         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1150c2c66affSColin Finck                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1151c2c66affSColin Finck         if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(i)))
1152c2c66affSColin Finck             memcpy(loc->locinfo->lconv->decimal_point, buf, i);
1153c2c66affSColin Finck         else {
1154c2c66affSColin Finck             MSVCRT__free_locale(loc);
1155c2c66affSColin Finck             return NULL;
1156c2c66affSColin Finck         }
1157c2c66affSColin Finck 
1158c2c66affSColin Finck         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1159c2c66affSColin Finck                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1160c2c66affSColin Finck         if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(i)))
1161c2c66affSColin Finck             memcpy(loc->locinfo->lconv->thousands_sep, buf, i);
1162c2c66affSColin Finck         else {
1163c2c66affSColin Finck             MSVCRT__free_locale(loc);
1164c2c66affSColin Finck             return NULL;
1165c2c66affSColin Finck         }
1166c2c66affSColin Finck 
1167c2c66affSColin Finck         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING
1168c2c66affSColin Finck                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1169c2c66affSColin Finck         if(i>1)
1170c2c66affSColin Finck             i = i/2 + (buf[i-2]=='0'?0:1);
1171c2c66affSColin Finck         if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(i))) {
1172c2c66affSColin Finck             for(i=0; buf[i+1]==';'; i+=2)
1173c2c66affSColin Finck                 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1174c2c66affSColin Finck             loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1175c2c66affSColin Finck             if(buf[i] != '0')
1176c2c66affSColin Finck                 loc->locinfo->lconv->grouping[i/2+1] = 127;
1177c2c66affSColin Finck         } else {
1178c2c66affSColin Finck             MSVCRT__free_locale(loc);
1179c2c66affSColin Finck             return NULL;
1180c2c66affSColin Finck         }
1181c2c66affSColin Finck     } else {
1182c2c66affSColin Finck         loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1183c2c66affSColin Finck         loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1184c2c66affSColin Finck         loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1185c2c66affSColin Finck         if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
1186c2c66affSColin Finck                 || !loc->locinfo->lconv->grouping) {
1187c2c66affSColin Finck             MSVCRT__free_locale(loc);
1188c2c66affSColin Finck             return NULL;
1189c2c66affSColin Finck         }
1190c2c66affSColin Finck 
1191c2c66affSColin Finck         loc->locinfo->lconv->decimal_point[0] = '.';
1192c2c66affSColin Finck         loc->locinfo->lconv->decimal_point[1] = '\0';
1193c2c66affSColin Finck         loc->locinfo->lconv->thousands_sep[0] = '\0';
1194c2c66affSColin Finck         loc->locinfo->lconv->grouping[0] = '\0';
1195c2c66affSColin Finck 
1196c2c66affSColin Finck         loc->locinfo->lc_category[LC_NUMERIC].locale = _strdup("C");
1197c2c66affSColin Finck     }
1198c2c66affSColin Finck 
1199c2c66affSColin Finck     if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1200c2c66affSColin Finck         if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME], cp[MSVCRT_LC_TIME], loc, MSVCRT_LC_TIME)) {
1201c2c66affSColin Finck             MSVCRT__free_locale(loc);
1202c2c66affSColin Finck             return NULL;
1203c2c66affSColin Finck         }
1204c2c66affSColin Finck     } else
1205c2c66affSColin Finck         loc->locinfo->lc_category[LC_TIME].locale = _strdup("C");
1206c2c66affSColin Finck 
1207c2c66affSColin Finck     size = sizeof(MSVCRT___lc_time_data);
1208c2c66affSColin Finck     lcid_tmp = lcid[MSVCRT_LC_TIME] ? lcid[MSVCRT_LC_TIME] : MAKELCID(LANG_ENGLISH, SORT_DEFAULT);
1209c2c66affSColin Finck     for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1210c2c66affSColin Finck         if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1211c2c66affSColin Finck             size += sizeof(cloc_short_date) + sizeof(cloc_short_dateW);
1212c2c66affSColin Finck         }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1213c2c66affSColin Finck             size += sizeof(cloc_long_date) + sizeof(cloc_long_dateW);
1214c2c66affSColin Finck         }else {
1215c2c66affSColin Finck             ret = GetLocaleInfoA(lcid_tmp, time_data[i]
1216c2c66affSColin Finck                     |LOCALE_NOUSEROVERRIDE, NULL, 0);
1217c2c66affSColin Finck             if(!ret) {
1218c2c66affSColin Finck                 MSVCRT__free_locale(loc);
1219c2c66affSColin Finck                 return NULL;
1220c2c66affSColin Finck             }
1221c2c66affSColin Finck             size += ret;
1222c2c66affSColin Finck 
1223c2c66affSColin Finck             ret = GetLocaleInfoW(lcid_tmp, time_data[i]
1224c2c66affSColin Finck                     |LOCALE_NOUSEROVERRIDE, NULL, 0);
1225c2c66affSColin Finck             if(!ret) {
1226c2c66affSColin Finck                 MSVCRT__free_locale(loc);
1227c2c66affSColin Finck                 return NULL;
1228c2c66affSColin Finck             }
1229c2c66affSColin Finck             size += ret*sizeof(wchar_t);
1230c2c66affSColin Finck         }
1231c2c66affSColin Finck     }
1232c2c66affSColin Finck 
1233c2c66affSColin Finck     loc->locinfo->lc_time_curr = MSVCRT_malloc(size);
1234c2c66affSColin Finck     if(!loc->locinfo->lc_time_curr) {
1235c2c66affSColin Finck         MSVCRT__free_locale(loc);
1236c2c66affSColin Finck         return NULL;
1237c2c66affSColin Finck     }
1238c2c66affSColin Finck 
1239c2c66affSColin Finck     ret = 0;
1240c2c66affSColin Finck     for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1241c2c66affSColin Finck         loc->locinfo->lc_time_curr->str.str[i] = &loc->locinfo->lc_time_curr->data[ret];
1242c2c66affSColin Finck         if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1243c2c66affSColin Finck             memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_short_date, sizeof(cloc_short_date));
1244c2c66affSColin Finck             ret += sizeof(cloc_short_date);
1245c2c66affSColin Finck         }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1246c2c66affSColin Finck             memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_long_date, sizeof(cloc_long_date));
1247c2c66affSColin Finck             ret += sizeof(cloc_long_date);
1248c2c66affSColin Finck         }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {
1249c2c66affSColin Finck             memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_time, sizeof(cloc_time));
1250c2c66affSColin Finck             ret += sizeof(cloc_time);
1251c2c66affSColin Finck         }else {
1252c2c66affSColin Finck             ret += GetLocaleInfoA(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1253c2c66affSColin Finck                     &loc->locinfo->lc_time_curr->data[ret], size-ret);
1254c2c66affSColin Finck         }
1255c2c66affSColin Finck     }
1256c2c66affSColin Finck     for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1257c2c66affSColin Finck         loc->locinfo->lc_time_curr->wstr[i] = (wchar_t*)&loc->locinfo->lc_time_curr->data[ret];
1258c2c66affSColin Finck         if(time_data[i]==LOCALE_SSHORTDATE && !lcid[MSVCRT_LC_TIME]) {
1259c2c66affSColin Finck             memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_short_dateW, sizeof(cloc_short_dateW));
1260c2c66affSColin Finck             ret += sizeof(cloc_short_dateW);
1261c2c66affSColin Finck         }else if(time_data[i]==LOCALE_SLONGDATE && !lcid[MSVCRT_LC_TIME]) {
1262c2c66affSColin Finck             memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_long_dateW, sizeof(cloc_long_dateW));
1263c2c66affSColin Finck             ret += sizeof(cloc_long_dateW);
1264c2c66affSColin Finck         }else if(time_data[i]==LOCALE_STIMEFORMAT && !lcid[MSVCRT_LC_TIME]) {            memcpy(&loc->locinfo->lc_time_curr->data[ret], cloc_timeW, sizeof(cloc_timeW));
1265c2c66affSColin Finck             ret += sizeof(cloc_timeW);
1266c2c66affSColin Finck         }else {
1267c2c66affSColin Finck             ret += GetLocaleInfoW(lcid_tmp, time_data[i]|LOCALE_NOUSEROVERRIDE,
1268c2c66affSColin Finck                     (wchar_t*)&loc->locinfo->lc_time_curr->data[ret], size-ret)*sizeof(wchar_t);
1269c2c66affSColin Finck         }
1270c2c66affSColin Finck     }
1271c2c66affSColin Finck     loc->locinfo->lc_time_curr->lcid = lcid[MSVCRT_LC_TIME];
1272c2c66affSColin Finck 
1273c2c66affSColin Finck     return loc;
1274c2c66affSColin Finck }
1275c2c66affSColin Finck 
1276c2c66affSColin Finck /*********************************************************************
1277c2c66affSColin Finck  *             setlocale (MSVCRT.@)
1278c2c66affSColin Finck  */
setlocale(int category,const char * locale)1279c2c66affSColin Finck char* CDECL setlocale(int category, const char* locale)
1280c2c66affSColin Finck {
1281c2c66affSColin Finck     MSVCRT__locale_t loc;
1282c2c66affSColin Finck     MSVCRT_pthreadlocinfo locinfo = get_locinfo();
1283c2c66affSColin Finck 
1284c2c66affSColin Finck     if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1285c2c66affSColin Finck         return NULL;
1286c2c66affSColin Finck 
1287c2c66affSColin Finck     if(!locale) {
1288c2c66affSColin Finck         if(category == MSVCRT_LC_ALL)
1289c2c66affSColin Finck             return construct_lc_all(locinfo);
1290c2c66affSColin Finck 
1291c2c66affSColin Finck         return locinfo->lc_category[category].locale;
1292c2c66affSColin Finck     }
1293c2c66affSColin Finck 
1294c2c66affSColin Finck     loc = MSVCRT__create_locale(category, locale);
1295c2c66affSColin Finck     if(!loc) {
1296c2c66affSColin Finck         WARN("%d %s failed\n", category, locale);
1297c2c66affSColin Finck         return NULL;
1298c2c66affSColin Finck     }
1299c2c66affSColin Finck 
1300c2c66affSColin Finck     LOCK_LOCALE;
1301c2c66affSColin Finck 
1302c2c66affSColin Finck     switch(category) {
1303c2c66affSColin Finck         case MSVCRT_LC_ALL:
1304c2c66affSColin Finck         case MSVCRT_LC_COLLATE:
1305c2c66affSColin Finck             locinfo->lc_collate_cp = loc->locinfo->lc_collate_cp;
1306c2c66affSColin Finck             locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1307c2c66affSColin Finck                 loc->locinfo->lc_handle[MSVCRT_LC_COLLATE];
1308c2c66affSColin Finck             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1309c2c66affSColin Finck                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1310c2c66affSColin Finck             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1311c2c66affSColin Finck                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1312c2c66affSColin Finck 
1313c2c66affSColin Finck             if(category != MSVCRT_LC_ALL)
1314c2c66affSColin Finck                 break;
1315c2c66affSColin Finck             /* fall through */
1316c2c66affSColin Finck         case MSVCRT_LC_CTYPE:
1317c2c66affSColin Finck             locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1318c2c66affSColin Finck                 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE];
1319c2c66affSColin Finck             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1320c2c66affSColin Finck                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1321c2c66affSColin Finck             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1322c2c66affSColin Finck                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1323c2c66affSColin Finck 
1324c2c66affSColin Finck             locinfo->lc_codepage = loc->locinfo->lc_codepage;
1325c2c66affSColin Finck             locinfo->lc_clike = loc->locinfo->lc_clike;
1326c2c66affSColin Finck             locinfo->mb_cur_max = loc->locinfo->mb_cur_max;
1327c2c66affSColin Finck 
1328c2c66affSColin Finck             swap_pointers((void**)&locinfo->ctype1_refcount,
1329c2c66affSColin Finck                     (void**)&loc->locinfo->ctype1_refcount);
1330c2c66affSColin Finck             swap_pointers((void**)&locinfo->ctype1, (void**)&loc->locinfo->ctype1);
1331c2c66affSColin Finck             swap_pointers((void**)&locinfo->pctype, (void**)&loc->locinfo->pctype);
1332c2c66affSColin Finck             swap_pointers((void**)&locinfo->pclmap, (void**)&loc->locinfo->pclmap);
1333c2c66affSColin Finck             swap_pointers((void**)&locinfo->pcumap, (void**)&loc->locinfo->pcumap);
1334c2c66affSColin Finck 
1335c2c66affSColin Finck             if(category != MSVCRT_LC_ALL)
1336c2c66affSColin Finck                 break;
1337c2c66affSColin Finck             /* fall through */
1338c2c66affSColin Finck         case MSVCRT_LC_MONETARY:
1339c2c66affSColin Finck             locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1340c2c66affSColin Finck                 loc->locinfo->lc_handle[MSVCRT_LC_MONETARY];
1341c2c66affSColin Finck             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1342c2c66affSColin Finck                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1343c2c66affSColin Finck             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1344c2c66affSColin Finck                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1345c2c66affSColin Finck 
1346c2c66affSColin Finck             swap_pointers((void**)&locinfo->lconv->int_curr_symbol,
1347c2c66affSColin Finck                     (void**)&loc->locinfo->lconv->int_curr_symbol);
1348c2c66affSColin Finck             swap_pointers((void**)&locinfo->lconv->currency_symbol,
1349c2c66affSColin Finck                     (void**)&loc->locinfo->lconv->currency_symbol);
1350c2c66affSColin Finck             swap_pointers((void**)&locinfo->lconv->mon_decimal_point,
1351c2c66affSColin Finck                     (void**)&loc->locinfo->lconv->mon_decimal_point);
1352c2c66affSColin Finck             swap_pointers((void**)&locinfo->lconv->mon_thousands_sep,
1353c2c66affSColin Finck                     (void**)&loc->locinfo->lconv->mon_thousands_sep);
1354c2c66affSColin Finck             swap_pointers((void**)&locinfo->lconv->mon_grouping,
1355c2c66affSColin Finck                     (void**)&loc->locinfo->lconv->mon_grouping);
1356c2c66affSColin Finck             swap_pointers((void**)&locinfo->lconv->positive_sign,
1357c2c66affSColin Finck                     (void**)&loc->locinfo->lconv->positive_sign);
1358c2c66affSColin Finck             swap_pointers((void**)&locinfo->lconv->negative_sign,
1359c2c66affSColin Finck                     (void**)&loc->locinfo->lconv->negative_sign);
1360c2c66affSColin Finck             locinfo->lconv->int_frac_digits = loc->locinfo->lconv->int_frac_digits;
1361c2c66affSColin Finck             locinfo->lconv->frac_digits = loc->locinfo->lconv->frac_digits;
1362c2c66affSColin Finck             locinfo->lconv->p_cs_precedes = loc->locinfo->lconv->p_cs_precedes;
1363c2c66affSColin Finck             locinfo->lconv->p_sep_by_space = loc->locinfo->lconv->p_sep_by_space;
1364c2c66affSColin Finck             locinfo->lconv->n_cs_precedes = loc->locinfo->lconv->n_cs_precedes;
1365c2c66affSColin Finck             locinfo->lconv->n_sep_by_space = loc->locinfo->lconv->n_sep_by_space;
1366c2c66affSColin Finck             locinfo->lconv->p_sign_posn = loc->locinfo->lconv->p_sign_posn;
1367c2c66affSColin Finck             locinfo->lconv->n_sign_posn = loc->locinfo->lconv->n_sign_posn;
1368c2c66affSColin Finck 
1369c2c66affSColin Finck             if(category != MSVCRT_LC_ALL)
1370c2c66affSColin Finck                 break;
1371c2c66affSColin Finck             /* fall through */
1372c2c66affSColin Finck         case MSVCRT_LC_NUMERIC:
1373c2c66affSColin Finck             locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1374c2c66affSColin Finck                 loc->locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1375c2c66affSColin Finck             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1376c2c66affSColin Finck                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1377c2c66affSColin Finck             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1378c2c66affSColin Finck                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1379c2c66affSColin Finck 
1380c2c66affSColin Finck             swap_pointers((void**)&locinfo->lconv->decimal_point,
1381c2c66affSColin Finck                     (void**)&loc->locinfo->lconv->decimal_point);
1382c2c66affSColin Finck             swap_pointers((void**)&locinfo->lconv->thousands_sep,
1383c2c66affSColin Finck                     (void**)&loc->locinfo->lconv->thousands_sep);
1384c2c66affSColin Finck             swap_pointers((void**)&locinfo->lconv->grouping,
1385c2c66affSColin Finck                     (void**)&loc->locinfo->lconv->grouping);
1386c2c66affSColin Finck 
1387c2c66affSColin Finck             if(category != MSVCRT_LC_ALL)
1388c2c66affSColin Finck                 break;
1389c2c66affSColin Finck             /* fall through */
1390c2c66affSColin Finck         case MSVCRT_LC_TIME:
1391c2c66affSColin Finck             locinfo->lc_handle[MSVCRT_LC_TIME] =
1392c2c66affSColin Finck                 loc->locinfo->lc_handle[MSVCRT_LC_TIME];
1393c2c66affSColin Finck             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].locale,
1394c2c66affSColin Finck                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].locale);
1395c2c66affSColin Finck             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1396c2c66affSColin Finck                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].refcount);
1397c2c66affSColin Finck             swap_pointers((void**)&locinfo->lc_time_curr,
1398c2c66affSColin Finck                     (void**)&loc->locinfo->lc_time_curr);
1399c2c66affSColin Finck 
1400c2c66affSColin Finck             if(category != MSVCRT_LC_ALL)
1401c2c66affSColin Finck                 break;
1402c2c66affSColin Finck     }
1403c2c66affSColin Finck 
1404c2c66affSColin Finck     MSVCRT__free_locale(loc);
1405c2c66affSColin Finck     UNLOCK_LOCALE;
1406c2c66affSColin Finck 
1407c2c66affSColin Finck     if(locinfo == MSVCRT_locale->locinfo) {
1408c2c66affSColin Finck         int i;
1409c2c66affSColin Finck 
1410c2c66affSColin Finck         __lc_codepage = locinfo->lc_codepage;
1411c2c66affSColin Finck         MSVCRT___lc_collate_cp = locinfo->lc_collate_cp;
1412c2c66affSColin Finck         __mb_cur_max = locinfo->mb_cur_max;
1413c2c66affSColin Finck         _pctype = locinfo->pctype;
1414c2c66affSColin Finck         for(i=LC_MIN; i<=LC_MAX; i++)
1415c2c66affSColin Finck             MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1416c2c66affSColin Finck     }
1417c2c66affSColin Finck 
1418c2c66affSColin Finck     if(category == MSVCRT_LC_ALL)
1419c2c66affSColin Finck         return construct_lc_all(locinfo);
1420c2c66affSColin Finck 
1421c2c66affSColin Finck     _Analysis_assume_(category <= 5);
1422c2c66affSColin Finck     return locinfo->lc_category[category].locale;
1423c2c66affSColin Finck }
1424c2c66affSColin Finck 
1425c2c66affSColin Finck /* _configthreadlocale - not exported in native msvcrt */
_configthreadlocale(int type)1426c2c66affSColin Finck int CDECL _configthreadlocale(int type)
1427c2c66affSColin Finck {
1428c2c66affSColin Finck     thread_data_t *data = msvcrt_get_thread_data();
1429c2c66affSColin Finck     MSVCRT__locale_t locale;
1430c2c66affSColin Finck     int ret;
1431c2c66affSColin Finck 
1432c2c66affSColin Finck     if(!data)
1433c2c66affSColin Finck         return -1;
1434c2c66affSColin Finck 
1435c2c66affSColin Finck     ret = (data->have_locale ? _ENABLE_PER_THREAD_LOCALE : _DISABLE_PER_THREAD_LOCALE);
1436c2c66affSColin Finck 
1437c2c66affSColin Finck     if(type == _ENABLE_PER_THREAD_LOCALE) {
1438c2c66affSColin Finck         if(!data->have_locale) {
1439c2c66affSColin Finck             /* Copy current global locale */
1440c2c66affSColin Finck             locale = MSVCRT__create_locale(LC_ALL, setlocale(LC_ALL, NULL));
1441c2c66affSColin Finck             if(!locale)
1442c2c66affSColin Finck                 return -1;
1443c2c66affSColin Finck 
1444c2c66affSColin Finck             data->locinfo = locale->locinfo;
1445c2c66affSColin Finck             data->mbcinfo = locale->mbcinfo;
1446c2c66affSColin Finck             data->have_locale = TRUE;
1447c2c66affSColin Finck             MSVCRT_free(locale);
1448c2c66affSColin Finck         }
1449c2c66affSColin Finck 
1450c2c66affSColin Finck         return ret;
1451c2c66affSColin Finck     }
1452c2c66affSColin Finck 
1453c2c66affSColin Finck     if(type == _DISABLE_PER_THREAD_LOCALE) {
1454c2c66affSColin Finck         if(data->have_locale) {
1455c2c66affSColin Finck             free_locinfo(data->locinfo);
1456c2c66affSColin Finck             free_mbcinfo(data->mbcinfo);
1457c2c66affSColin Finck             data->locinfo = MSVCRT_locale->locinfo;
1458c2c66affSColin Finck             data->mbcinfo = MSVCRT_locale->mbcinfo;
1459c2c66affSColin Finck             data->have_locale = FALSE;
1460c2c66affSColin Finck         }
1461c2c66affSColin Finck 
1462c2c66affSColin Finck         return ret;
1463c2c66affSColin Finck     }
1464c2c66affSColin Finck 
1465c2c66affSColin Finck     if(!type)
1466c2c66affSColin Finck         return ret;
1467c2c66affSColin Finck 
1468c2c66affSColin Finck     return -1;
1469c2c66affSColin Finck }
1470c2c66affSColin Finck 
1471c2c66affSColin Finck /*********************************************************************
1472c2c66affSColin Finck  *         _getmbcp (MSVCRT.@)
1473c2c66affSColin Finck  */
_getmbcp(void)1474c2c66affSColin Finck int CDECL _getmbcp(void)
1475c2c66affSColin Finck {
1476c2c66affSColin Finck   return get_mbcinfo()->mbcodepage;
1477c2c66affSColin Finck }
1478c2c66affSColin Finck 
1479c2c66affSColin Finck extern unsigned int __setlc_active;
1480c2c66affSColin Finck /*********************************************************************
1481c2c66affSColin Finck  *         ___setlc_active_func (MSVCRT.@)
1482c2c66affSColin Finck  */
___setlc_active_func(void)1483c2c66affSColin Finck unsigned int CDECL ___setlc_active_func(void)
1484c2c66affSColin Finck {
1485c2c66affSColin Finck   return __setlc_active;
1486c2c66affSColin Finck }
1487c2c66affSColin Finck 
1488c2c66affSColin Finck extern unsigned int __unguarded_readlc_active;
1489c2c66affSColin Finck /*********************************************************************
1490c2c66affSColin Finck  *         ___unguarded_readlc_active_add_func (MSVCRT.@)
1491c2c66affSColin Finck  */
___unguarded_readlc_active_add_func(void)1492c2c66affSColin Finck unsigned int * CDECL ___unguarded_readlc_active_add_func(void)
1493c2c66affSColin Finck {
1494c2c66affSColin Finck   return &__unguarded_readlc_active;
1495c2c66affSColin Finck }
1496c2c66affSColin Finck 
1497c2c66affSColin Finck MSVCRT__locale_t global_locale = NULL;
__init_global_locale()1498c2c66affSColin Finck void __init_global_locale()
1499c2c66affSColin Finck {
1500c2c66affSColin Finck     unsigned i;
1501c2c66affSColin Finck 
1502c2c66affSColin Finck     LOCK_LOCALE;
1503c2c66affSColin Finck     /* Someone created it before us */
1504c2c66affSColin Finck     if(global_locale)
1505c2c66affSColin Finck         return;
1506c2c66affSColin Finck     global_locale = MSVCRT__create_locale(0, "C");
1507c2c66affSColin Finck 
1508c2c66affSColin Finck     __lc_codepage = MSVCRT_locale->locinfo->lc_codepage;
1509c2c66affSColin Finck     MSVCRT___lc_collate_cp = MSVCRT_locale->locinfo->lc_collate_cp;
1510c2c66affSColin Finck     __mb_cur_max = MSVCRT_locale->locinfo->mb_cur_max;
1511c2c66affSColin Finck     for(i=LC_MIN; i<=LC_MAX; i++)
1512c2c66affSColin Finck         MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1513c2c66affSColin Finck     _setmbcp(_MB_CP_ANSI);
1514c2c66affSColin Finck     UNLOCK_LOCALE;
1515c2c66affSColin Finck }
1516c2c66affSColin Finck 
1517c2c66affSColin Finck /*
1518c2c66affSColin Finck  * @implemented
1519c2c66affSColin Finck  */
__p__pctype(void)1520c2c66affSColin Finck const unsigned short **__p__pctype(void)
1521c2c66affSColin Finck {
1522c2c66affSColin Finck    return &get_locinfo()->pctype;
1523c2c66affSColin Finck }
1524c2c66affSColin Finck 
__pctype_func(void)1525c2c66affSColin Finck const unsigned short* __cdecl __pctype_func(void)
1526c2c66affSColin Finck {
1527c2c66affSColin Finck    return get_locinfo()->pctype;
1528c2c66affSColin Finck }
1529c2c66affSColin Finck 
1530