mshtml.idl: Added some missing attributes.
[wine.git] / dlls / msvcrt / locale.c
blob5e08873390f1571f1d6f21cf2fb741c665659ee7
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <locale.h>
25 #include <stdarg.h>
26 #include <stdio.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "winnls.h"
33 #include "msvcrt.h"
34 #include "mtdll.h"
35 #include "msvcrt/mbctype.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
41 /* FIXME: Need to hold locale for each LC_* type and aggregate
42 * string to produce lc_all.
44 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
45 #define MAX_LOCALE_LENGTH 256
46 char MSVCRT_current_lc_all[MAX_LOCALE_LENGTH];
47 LCID MSVCRT_current_lc_all_lcid;
48 int MSVCRT___lc_codepage;
49 int MSVCRT___lc_collate_cp;
50 HANDLE MSVCRT___lc_handle[MSVCRT_LC_MAX - MSVCRT_LC_MIN + 1];
52 /* MT */
53 #define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
54 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
56 /* ctype data modified when the locale changes */
57 extern WORD MSVCRT__ctype [257];
58 extern WORD MSVCRT_current_ctype[257];
59 extern WORD* MSVCRT__pctype;
61 /* mbctype data modified when the locale changes */
62 extern int MSVCRT___mb_cur_max;
63 extern unsigned char MSVCRT_mbctype[257];
65 #define MSVCRT_LEADBYTE 0x8000
67 /* Friendly country strings & iso codes for synonym support.
68 * Based on MS documentation for setlocale().
70 static const char * const _country_synonyms[] =
72 "Hong Kong","HK",
73 "Hong-Kong","HK",
74 "New Zealand","NZ",
75 "New-Zealand","NZ",
76 "PR China","CN",
77 "PR-China","CN",
78 "United Kingdom","GB",
79 "United-Kingdom","GB",
80 "Britain","GB",
81 "England","GB",
82 "Great Britain","GB",
83 "United States","US",
84 "United-States","US",
85 "America","US"
88 /* INTERNAL: Map a synonym to an ISO code */
89 static void remap_synonym(char *name)
91 size_t i;
92 for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
94 if (!strcasecmp(_country_synonyms[i],name))
96 TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
97 name[0] = _country_synonyms[i+1][0];
98 name[1] = _country_synonyms[i+1][1];
99 name[2] = '\0';
100 return;
105 /* Note: Flags are weighted in order of matching importance */
106 #define FOUND_LANGUAGE 0x4
107 #define FOUND_COUNTRY 0x2
108 #define FOUND_CODEPAGE 0x1
110 typedef struct {
111 char search_language[MAX_ELEM_LEN];
112 char search_country[MAX_ELEM_LEN];
113 char search_codepage[MAX_ELEM_LEN];
114 char found_language[MAX_ELEM_LEN];
115 char found_country[MAX_ELEM_LEN];
116 char found_codepage[MAX_ELEM_LEN];
117 unsigned int match_flags;
118 LANGID found_lang_id;
119 } locale_search_t;
121 #define CONTINUE_LOOKING TRUE
122 #define STOP_LOOKING FALSE
124 /* INTERNAL: Get and compare locale info with a given string */
125 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp)
127 buff[0] = 0;
128 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
129 if (!buff[0] || !cmp[0])
130 return 0;
131 /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
132 return !strncasecmp(cmp, buff, strlen(cmp));
135 static BOOL CALLBACK
136 find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
138 locale_search_t *res = (locale_search_t *)lParam;
139 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
140 char buff[MAX_ELEM_LEN];
141 unsigned int flags = 0;
143 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
144 return CONTINUE_LOOKING;
146 /* Check Language */
147 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
148 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
149 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
151 TRACE(":Found language: %s->%s\n", res->search_language, buff);
152 flags |= FOUND_LANGUAGE;
153 memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
155 else if (res->match_flags & FOUND_LANGUAGE)
157 return CONTINUE_LOOKING;
160 /* Check Country */
161 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
162 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
163 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
165 TRACE("Found country:%s->%s\n", res->search_country, buff);
166 flags |= FOUND_COUNTRY;
167 memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
169 else if (res->match_flags & FOUND_COUNTRY)
171 return CONTINUE_LOOKING;
174 /* Check codepage */
175 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
176 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
178 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
179 flags |= FOUND_CODEPAGE;
180 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
182 else if (res->match_flags & FOUND_CODEPAGE)
184 return CONTINUE_LOOKING;
187 if (flags > res->match_flags)
189 /* Found a better match than previously */
190 res->match_flags = flags;
191 res->found_lang_id = LangID;
193 if (flags & (FOUND_LANGUAGE & FOUND_COUNTRY & FOUND_CODEPAGE))
195 TRACE(":found exact locale match\n");
196 return STOP_LOOKING;
198 return CONTINUE_LOOKING;
201 extern int atoi(const char *);
203 /* Internal: Find the LCID for a locale specification */
204 static LCID MSVCRT_locale_to_LCID(locale_search_t* locale)
206 LCID lcid;
207 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
208 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
209 (LONG_PTR)locale);
211 if (!locale->match_flags)
212 return 0;
214 /* If we were given something that didn't match, fail */
215 if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
216 return 0;
218 lcid = MAKELCID(locale->found_lang_id, SORT_DEFAULT);
220 /* Populate partial locale, translating LCID to locale string elements */
221 if (!locale->found_codepage[0])
223 /* Even if a codepage is not enumerated for a locale
224 * it can be set if valid */
225 if (locale->search_codepage[0])
227 if (IsValidCodePage(atoi(locale->search_codepage)))
228 memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
229 else
231 /* Special codepage values: OEM & ANSI */
232 if (strcasecmp(locale->search_codepage,"OCP"))
234 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
235 locale->found_codepage, MAX_ELEM_LEN);
237 if (strcasecmp(locale->search_codepage,"ACP"))
239 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
240 locale->found_codepage, MAX_ELEM_LEN);
242 else
243 return 0;
245 if (!atoi(locale->found_codepage))
246 return 0;
249 else
251 /* Prefer ANSI codepages if present */
252 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
253 locale->found_codepage, MAX_ELEM_LEN);
254 if (!locale->found_codepage[0] || !atoi(locale->found_codepage))
255 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
256 locale->found_codepage, MAX_ELEM_LEN);
259 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
260 locale->found_language, MAX_ELEM_LEN);
261 GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
262 locale->found_country, MAX_ELEM_LEN);
263 return lcid;
266 /* INTERNAL: Set ctype behaviour for a codepage */
267 static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
269 CPINFO cp;
271 memset(&cp, 0, sizeof(CPINFO));
273 if (GetCPInfo(codepage, &cp))
275 int i;
276 char str[3];
277 unsigned char *traverse = (unsigned char *)cp.LeadByte;
279 memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
280 MSVCRT___lc_codepage = codepage;
281 MSVCRT___lc_collate_cp = codepage;
283 /* Switch ctype macros to MBCS if needed */
284 MSVCRT___mb_cur_max = cp.MaxCharSize;
286 /* Set remaining ctype flags: FIXME: faster way to do this? */
287 str[1] = str[2] = 0;
288 for (i = 0; i < 256; i++)
290 if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
292 str[0] = i;
293 GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
297 /* Set leadbyte flags */
298 while (traverse[0] || traverse[1])
300 for( i = traverse[0]; i <= traverse[1]; i++ )
301 MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
302 traverse += 2;
308 /*********************************************************************
309 * setlocale (MSVCRT.@)
311 char* CDECL MSVCRT_setlocale(int category, const char* locale)
313 LCID lcid = 0;
314 locale_search_t lc;
315 int haveLang, haveCountry, haveCP;
316 char* next;
317 int lc_all = 0;
319 TRACE("(%d %s)\n",category,locale);
321 if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
322 return NULL;
324 if (locale == NULL)
326 /* Report the current Locale */
327 return MSVCRT_current_lc_all;
330 LOCK_LOCALE;
332 if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
334 FIXME(":restore previous locale not implemented!\n");
335 /* FIXME: Easiest way to do this is parse the string and
336 * call this function recursively with its elements,
337 * Where they differ for each lc_ type.
339 UNLOCK_LOCALE;
340 return MSVCRT_current_lc_all;
343 /* Default Locale: Special case handling */
344 if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
346 MSVCRT_current_lc_all[0] = 'C';
347 MSVCRT_current_lc_all[1] = '\0';
348 MSVCRT___lc_codepage = GetACP();
349 MSVCRT___lc_collate_cp = GetACP();
351 switch (category) {
352 case MSVCRT_LC_ALL:
353 lc_all = 1; /* Fall through all cases ... */
354 case MSVCRT_LC_COLLATE:
355 if (!lc_all) break;
356 case MSVCRT_LC_CTYPE:
357 /* Restore C locale ctype info */
358 MSVCRT___mb_cur_max = 1;
359 memcpy(MSVCRT_current_ctype, MSVCRT__ctype, sizeof(MSVCRT__ctype));
360 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
361 if (!lc_all) break;
362 case MSVCRT_LC_MONETARY:
363 if (!lc_all) break;
364 case MSVCRT_LC_NUMERIC:
365 if (!lc_all) break;
366 case MSVCRT_LC_TIME:
367 break;
369 UNLOCK_LOCALE;
370 return MSVCRT_current_lc_all;
373 /* Get locale elements */
374 haveLang = haveCountry = haveCP = 0;
375 memset(&lc,0,sizeof(lc));
377 next = strchr(locale,'_');
378 if (next && next != locale)
380 haveLang = 1;
381 memcpy(lc.search_language,locale,next-locale);
382 locale += next-locale+1;
385 next = strchr(locale,'.');
386 if (next)
388 haveCP = 1;
389 if (next == locale)
391 locale++;
392 lstrcpynA(lc.search_codepage, locale, MAX_ELEM_LEN);
394 else
396 if (haveLang)
398 haveCountry = 1;
399 memcpy(lc.search_country,locale,next-locale);
400 locale += next-locale+1;
402 else
404 haveLang = 1;
405 memcpy(lc.search_language,locale,next-locale);
406 locale += next-locale+1;
408 lstrcpynA(lc.search_codepage, locale, MAX_ELEM_LEN);
411 else
413 if (haveLang)
415 haveCountry = 1;
416 lstrcpynA(lc.search_country, locale, MAX_ELEM_LEN);
418 else
420 haveLang = 1;
421 lstrcpynA(lc.search_language, locale, MAX_ELEM_LEN);
425 if (haveCountry)
426 remap_synonym(lc.search_country);
428 if (haveCP && !haveCountry && !haveLang)
430 FIXME(":Codepage only locale not implemented\n");
431 /* FIXME: Use default lang/country and skip locale_to_LCID()
432 * call below...
434 UNLOCK_LOCALE;
435 return NULL;
438 lcid = MSVCRT_locale_to_LCID(&lc);
440 TRACE(":found LCID %d\n",lcid);
442 if (lcid == 0)
444 UNLOCK_LOCALE;
445 return NULL;
448 MSVCRT_current_lc_all_lcid = lcid;
450 snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
451 lc.found_language,lc.found_country,lc.found_codepage);
453 switch (category) {
454 case MSVCRT_LC_ALL:
455 lc_all = 1; /* Fall through all cases ... */
456 case MSVCRT_LC_COLLATE:
457 if (!lc_all) break;
458 case MSVCRT_LC_CTYPE:
459 msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
460 if (!lc_all) break;
461 case MSVCRT_LC_MONETARY:
462 if (!lc_all) break;
463 case MSVCRT_LC_NUMERIC:
464 if (!lc_all) break;
465 case MSVCRT_LC_TIME:
466 break;
468 UNLOCK_LOCALE;
469 return MSVCRT_current_lc_all;
472 /*********************************************************************
473 * setlocale (MSVCRT.@)
475 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
477 static MSVCRT_wchar_t fake[] = {
478 'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
479 'S','t','a','t','e','s','.','1','2','5','2',0 };
481 FIXME("%d %s\n", category, debugstr_w(locale));
483 return fake;
486 /*********************************************************************
487 * _Getdays (MSVCRT.@)
489 const char* CDECL _Getdays(void)
491 static const char MSVCRT_days[] = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
492 "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
493 /* FIXME: Use locale */
494 TRACE("(void) semi-stub\n");
495 return MSVCRT_days;
498 /*********************************************************************
499 * _Getmonths (MSVCRT.@)
501 const char* CDECL _Getmonths(void)
503 static const char MSVCRT_months[] = ":Jan:January:Feb:February:Mar:March:Apr:"
504 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
505 "October:Nov:November:Dec:December";
506 /* FIXME: Use locale */
507 TRACE("(void) semi-stub\n");
508 return MSVCRT_months;
511 /*********************************************************************
512 * _Gettnames (MSVCRT.@)
514 const char* CDECL _Gettnames(void)
516 /* FIXME: */
517 TRACE("(void) stub\n");
518 return "";
521 /*********************************************************************
522 * _Strftime (MSVCRT.@)
524 const char* CDECL _Strftime(char *out, unsigned int len, const char *fmt,
525 const void *tm, void *foo)
527 /* FIXME: */
528 TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
529 return "";
532 /* FIXME: MBCP probably belongs in mbcs.c */
534 /*********************************************************************
535 * _setmbcp (MSVCRT.@)
537 int CDECL _setmbcp(int cp)
539 LOCK_LOCALE;
540 if ( cp > _MB_CP_SBCS)
542 if( MSVCRT___lc_codepage != cp)
543 /* FIXME: set ctype behaviour for this cp */
544 MSVCRT___lc_codepage = cp;
546 else if(cp == _MB_CP_ANSI)
548 MSVCRT___lc_codepage = GetACP();
550 else if(cp == _MB_CP_OEM)
552 MSVCRT___lc_codepage = GetOEMCP();
554 else if(cp == _MB_CP_LOCALE)
556 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER,
557 (WCHAR *)&MSVCRT___lc_codepage, sizeof(INT)/sizeof(WCHAR) );
559 else if(cp == _MB_CP_SBCS)
561 FIXME ("SBCS codepages not implemented\n");
563 else
565 FIXME ("Unreal codepages (e.g. %d) not implemented\n", cp);
567 MSVCRT___lc_collate_cp = MSVCRT___lc_codepage;
568 UNLOCK_LOCALE;
569 TRACE("(%d) -> %d\n", cp, MSVCRT___lc_codepage);
570 return 0;
573 /*********************************************************************
574 * _getmbcp (MSVCRT.@)
576 int CDECL _getmbcp(void)
578 return MSVCRT___lc_codepage;
581 /*********************************************************************
582 * __crtLCMapStringA (MSVCRT.@)
584 int CDECL __crtLCMapStringA(
585 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
586 int dstlen, unsigned int codepage, int xflag
588 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
589 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
590 /* FIXME: A bit incorrect. But msvcrt itself just converts its
591 * arguments to wide strings and then calls LCMapStringW
593 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
596 /*********************************************************************
597 * localeconv (MSVCRT.@)
599 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void) {
601 struct lconv *ylconv;
602 static struct MSVCRT_lconv xlconv;
604 ylconv = localeconv();
606 #define X(x) xlconv.x = ylconv->x;
607 X(decimal_point);
608 X(thousands_sep);
609 X(grouping);
610 X(int_curr_symbol);
611 X(currency_symbol);
612 X(mon_decimal_point);
613 X(mon_thousands_sep);
614 X(mon_grouping);
615 X(positive_sign);
616 X(negative_sign);
617 X(int_frac_digits);
618 X(frac_digits);
619 X(p_cs_precedes);
620 X(p_sep_by_space);
621 X(n_cs_precedes);
622 X(n_sep_by_space);
623 X(p_sign_posn);
624 X(n_sign_posn);
625 return &xlconv;
628 /*********************************************************************
629 * __lconv_init (MSVCRT.@)
631 void CDECL __lconv_init(void)
633 FIXME(" stub\n");