Release 20030408.
[wine/gsoc-2012-control.git] / dlls / msvcrt / locale.c
bloba3de0c2c3ed2171db15a36b57b10ed15a781ae7b
1 /*
2 * msvcrt.dll locale functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include "winbase.h"
25 #include "winuser.h"
27 #include "msvcrt.h"
28 #include "msvcrt/locale.h"
29 #include "mtdll.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
35 /* FIXME: Need to hold locale for each LC_* type and aggregate
36 * string to produce lc_all.
38 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
39 #define MAX_LOCALE_LENGTH 256
40 char MSVCRT_current_lc_all[MAX_LOCALE_LENGTH];
41 LCID MSVCRT_current_lc_all_lcid;
42 int MSVCRT_current_lc_all_cp;
44 /* MT */
45 #define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
46 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
48 /* ctype data modified when the locale changes */
49 extern WORD MSVCRT__ctype [257];
50 extern WORD MSVCRT_current_ctype[257];
51 extern WORD* MSVCRT__pctype;
53 /* mbctype data modified when the locale changes */
54 extern int MSVCRT___mb_cur_max;
55 extern unsigned char MSVCRT_mbctype[257];
57 #define MSVCRT_LEADBYTE 0x8000
59 /* Friendly country strings & iso codes for synonym support.
60 * Based on MS documentation for setlocale().
62 static const char* _country_synonyms[] =
64 "Hong Kong","HK",
65 "Hong-Kong","HK",
66 "New Zealand","NZ",
67 "New-Zealand","NZ",
68 "PR China","CN",
69 "PR-China","CN",
70 "United Kingdom","GB",
71 "United-Kingdom","GB",
72 "Britain","GB",
73 "England","GB",
74 "Great Britain","GB",
75 "United States","US",
76 "United-States","US",
77 "America","US"
80 /* INTERNAL: Map a synonym to an ISO code */
81 static void remap_synonym(char *name)
83 size_t i;
84 for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
86 if (!strcasecmp(_country_synonyms[i],name))
88 TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
89 name[0] = _country_synonyms[i+1][0];
90 name[1] = _country_synonyms[i+1][1];
91 name[2] = '\0';
92 return;
97 /* Note: Flags are weighted in order of matching importance */
98 #define FOUND_LANGUAGE 0x4
99 #define FOUND_COUNTRY 0x2
100 #define FOUND_CODEPAGE 0x1
102 typedef struct {
103 char search_language[MAX_ELEM_LEN];
104 char search_country[MAX_ELEM_LEN];
105 char search_codepage[MAX_ELEM_LEN];
106 char found_language[MAX_ELEM_LEN];
107 char found_country[MAX_ELEM_LEN];
108 char found_codepage[MAX_ELEM_LEN];
109 unsigned int match_flags;
110 LANGID found_lang_id;
111 } locale_search_t;
113 #define CONTINUE_LOOKING TRUE
114 #define STOP_LOOKING FALSE
116 /* INTERNAL: Get and compare locale info with a given string */
117 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp)
119 buff[0] = 0;
120 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
121 if (!buff[0] || !cmp[0])
122 return 0;
123 /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
124 return !strncasecmp(cmp, buff, strlen(cmp));
127 static BOOL CALLBACK
128 find_best_locale_proc(HMODULE hModule WINE_UNUSED, LPCSTR type WINE_UNUSED,
129 LPCSTR name WINE_UNUSED, WORD LangID, LONG lParam)
131 locale_search_t *res = (locale_search_t *)lParam;
132 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
133 char buff[MAX_ELEM_LEN];
134 unsigned int flags = 0;
136 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
137 return CONTINUE_LOOKING;
139 /* Check Language */
140 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
141 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
142 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
144 TRACE(":Found language: %s->%s\n", res->search_language, buff);
145 flags |= FOUND_LANGUAGE;
146 memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
148 else if (res->match_flags & FOUND_LANGUAGE)
150 return CONTINUE_LOOKING;
153 /* Check Country */
154 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
155 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
156 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
158 TRACE("Found country:%s->%s\n", res->search_country, buff);
159 flags |= FOUND_COUNTRY;
160 memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
162 else if (res->match_flags & FOUND_COUNTRY)
164 return CONTINUE_LOOKING;
167 /* Check codepage */
168 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
169 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
171 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
172 flags |= FOUND_CODEPAGE;
173 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
175 else if (res->match_flags & FOUND_CODEPAGE)
177 return CONTINUE_LOOKING;
180 if (flags > res->match_flags)
182 /* Found a better match than previously */
183 res->match_flags = flags;
184 res->found_lang_id = LangID;
186 if (flags & (FOUND_LANGUAGE & FOUND_COUNTRY & FOUND_CODEPAGE))
188 TRACE(":found exact locale match\n");
189 return STOP_LOOKING;
191 return CONTINUE_LOOKING;
194 extern int atoi(const char *);
196 /* Internal: Find the LCID for a locale specification */
197 static LCID MSVCRT_locale_to_LCID(locale_search_t* locale)
199 LCID lcid;
200 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
201 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
202 (LONG)locale);
204 if (!locale->match_flags)
205 return 0;
207 /* If we were given something that didn't match, fail */
208 if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
209 return 0;
211 lcid = MAKELCID(locale->found_lang_id, SORT_DEFAULT);
213 /* Populate partial locale, translating LCID to locale string elements */
214 if (!locale->found_codepage[0])
216 /* Even if a codepage is not enumerated for a locale
217 * it can be set if valid */
218 if (locale->search_codepage[0])
220 if (IsValidCodePage(atoi(locale->search_codepage)))
221 memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
222 else
224 /* Special codepage values: OEM & ANSI */
225 if (strcasecmp(locale->search_codepage,"OCP"))
227 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
228 locale->found_codepage, MAX_ELEM_LEN);
230 if (strcasecmp(locale->search_codepage,"ACP"))
232 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
233 locale->found_codepage, MAX_ELEM_LEN);
235 else
236 return 0;
238 if (!atoi(locale->found_codepage))
239 return 0;
242 else
244 /* Prefer ANSI codepages if present */
245 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
246 locale->found_codepage, MAX_ELEM_LEN);
247 if (!locale->found_codepage[0] || !atoi(locale->found_codepage))
248 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
249 locale->found_codepage, MAX_ELEM_LEN);
252 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
253 locale->found_language, MAX_ELEM_LEN);
254 GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
255 locale->found_country, MAX_ELEM_LEN);
256 return lcid;
259 extern int snprintf(char *, int, const char *, ...);
261 /* INTERNAL: Set ctype behaviour for a codepage */
262 static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
264 CPINFO cp;
266 memset(&cp, 0, sizeof(CPINFO));
268 if (GetCPInfo(codepage, &cp))
270 int i;
271 char str[3];
272 unsigned char *traverse = (unsigned char *)cp.LeadByte;
274 memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
275 MSVCRT_current_lc_all_cp = codepage;
277 /* Switch ctype macros to MBCS if needed */
278 MSVCRT___mb_cur_max = cp.MaxCharSize;
280 /* Set remaining ctype flags: FIXME: faster way to do this? */
281 str[1] = str[2] = 0;
282 for (i = 0; i < 256; i++)
284 if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
286 str[0] = i;
287 GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
291 /* Set leadbyte flags */
292 while (traverse[0] || traverse[1])
294 for( i = traverse[0]; i <= traverse[1]; i++ )
295 MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
296 traverse += 2;
302 /*********************************************************************
303 * setlocale (MSVCRT.@)
305 char* MSVCRT_setlocale(int category, const char* locale)
307 LCID lcid = 0;
308 locale_search_t lc;
309 int haveLang, haveCountry, haveCP;
310 char* next;
311 int lc_all = 0;
313 TRACE("(%d %s)\n",category,locale);
315 if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
316 return NULL;
318 if (locale == NULL)
320 /* Report the current Locale */
321 return MSVCRT_current_lc_all;
324 LOCK_LOCALE;
326 if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
328 FIXME(":restore previous locale not implemented!\n");
329 /* FIXME: Easiest way to do this is parse the string and
330 * call this function recursively with its elements,
331 * Where they differ for each lc_ type.
333 UNLOCK_LOCALE;
334 return MSVCRT_current_lc_all;
337 /* Default Locale: Special case handling */
338 if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
340 MSVCRT_current_lc_all[0] = 'C';
341 MSVCRT_current_lc_all[1] = '\0';
342 MSVCRT_current_lc_all_cp = GetACP();
344 switch (category) {
345 case MSVCRT_LC_ALL:
346 lc_all = 1; /* Fall through all cases ... */
347 case MSVCRT_LC_COLLATE:
348 if (!lc_all) break;
349 case MSVCRT_LC_CTYPE:
350 /* Restore C locale ctype info */
351 MSVCRT___mb_cur_max = 1;
352 memcpy(MSVCRT_current_ctype, MSVCRT__ctype, sizeof(MSVCRT__ctype));
353 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
354 if (!lc_all) break;
355 case MSVCRT_LC_MONETARY:
356 if (!lc_all) break;
357 case MSVCRT_LC_NUMERIC:
358 if (!lc_all) break;
359 case MSVCRT_LC_TIME:
360 break;
362 UNLOCK_LOCALE;
363 return MSVCRT_current_lc_all;
366 /* Get locale elements */
367 haveLang = haveCountry = haveCP = 0;
368 memset(&lc,0,sizeof(lc));
370 next = strchr(locale,'_');
371 if (next && next != locale)
373 haveLang = 1;
374 strncpy(lc.search_language,locale,next-locale);
375 locale += next-locale+1;
378 next = strchr(locale,'.');
379 if (next)
381 haveCP = 1;
382 if (next == locale)
384 locale++;
385 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
387 else
389 if (haveLang)
391 haveCountry = 1;
392 strncpy(lc.search_country,locale,next-locale);
393 locale += next-locale+1;
395 else
397 haveLang = 1;
398 strncpy(lc.search_language,locale,next-locale);
399 locale += next-locale+1;
401 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
404 else
406 if (haveLang)
408 haveCountry = 1;
409 strncpy(lc.search_country, locale, MAX_ELEM_LEN);
411 else
413 haveLang = 1;
414 strncpy(lc.search_language, locale, MAX_ELEM_LEN);
418 if (haveCountry)
419 remap_synonym(lc.search_country);
421 if (haveCP && !haveCountry && !haveLang)
423 FIXME(":Codepage only locale not implemented\n");
424 /* FIXME: Use default lang/country and skip locale_to_LCID()
425 * call below...
427 UNLOCK_LOCALE;
428 return NULL;
431 lcid = MSVCRT_locale_to_LCID(&lc);
433 TRACE(":found LCID %ld\n",lcid);
435 if (lcid == 0)
437 UNLOCK_LOCALE;
438 return NULL;
441 MSVCRT_current_lc_all_lcid = lcid;
443 snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
444 lc.found_language,lc.found_country,lc.found_codepage);
446 switch (category) {
447 case MSVCRT_LC_ALL:
448 lc_all = 1; /* Fall through all cases ... */
449 case MSVCRT_LC_COLLATE:
450 if (!lc_all) break;
451 case MSVCRT_LC_CTYPE:
452 msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
453 if (!lc_all) break;
454 case MSVCRT_LC_MONETARY:
455 if (!lc_all) break;
456 case MSVCRT_LC_NUMERIC:
457 if (!lc_all) break;
458 case MSVCRT_LC_TIME:
459 break;
461 UNLOCK_LOCALE;
462 return MSVCRT_current_lc_all;
466 /*********************************************************************
467 * _Getdays (MSVCRT.@)
469 const char* _Getdays(void)
471 static const char *MSVCRT_days = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
472 "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
473 /* FIXME: Use locale */
474 TRACE("(void) semi-stub\n");
475 return MSVCRT_days;
478 /*********************************************************************
479 * _Getmonths (MSVCRT.@)
481 const char* _Getmonths(void)
483 static const char *MSVCRT_months = ":Jan:January:Feb:February:Mar:March:Apr:"
484 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
485 "October:Nov:November:Dec:December";
486 /* FIXME: Use locale */
487 TRACE("(void) semi-stub\n");
488 return MSVCRT_months;
491 /*********************************************************************
492 * _Getnames (MSVCRT.@)
494 const char* _Getnames(void)
496 /* FIXME: */
497 TRACE("(void) stub\n");
498 return "";
501 /*********************************************************************
502 * _Strftime (MSVCRT.@)
504 const char* _Strftime(char *out, unsigned int len, const char *fmt,
505 const void *tm, void *foo)
507 /* FIXME: */
508 TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
509 return "";
512 /* FIXME: MBCP probably belongs in mbcs.c */
514 /*********************************************************************
515 * _setmbcp (MSVCRT.@)
517 void _setmbcp(int cp)
519 LOCK_LOCALE;
520 if (MSVCRT_current_lc_all_cp != cp)
522 /* FIXME: set ctype behaviour for this cp */
523 MSVCRT_current_lc_all_cp = cp;
525 UNLOCK_LOCALE;
528 /*********************************************************************
529 * _getmbcp (MSVCRT.@)
531 int _getmbcp(void)
533 return MSVCRT_current_lc_all_cp;
536 /*********************************************************************
537 * __crtLCMapStringA (MSVCRT.@)
539 int __crtLCMapStringA(
540 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
541 int dstlen, unsigned int codepage, int xflag
543 FIXME("(lcid %lx, flags %lx, %s(%d), %p(%d), %x, %d), partial stub!\n",
544 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
545 /* FIXME: A bit incorrect. But msvcrt itself just converts its
546 * arguments to wide strings and then calls LCMapStringW
548 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);