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
22 #include "wine/port.h"
33 #include "msvcrt/locale.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
40 /* FIXME: Need to hold locale for each LC_* type and aggregate
41 * string to produce lc_all.
43 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
44 #define MAX_LOCALE_LENGTH 256
45 char MSVCRT_current_lc_all[MAX_LOCALE_LENGTH];
46 LCID MSVCRT_current_lc_all_lcid;
47 int MSVCRT_current_lc_all_cp;
50 #define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
51 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
53 /* ctype data modified when the locale changes */
54 extern WORD MSVCRT__ctype [257];
55 extern WORD MSVCRT_current_ctype[257];
56 extern WORD* MSVCRT__pctype;
58 /* mbctype data modified when the locale changes */
59 extern int MSVCRT___mb_cur_max;
60 extern unsigned char MSVCRT_mbctype[257];
62 #define MSVCRT_LEADBYTE 0x8000
64 /* Friendly country strings & iso codes for synonym support.
65 * Based on MS documentation for setlocale().
67 static const char* _country_synonyms[] =
75 "United Kingdom","GB",
76 "United-Kingdom","GB",
85 /* INTERNAL: Map a synonym to an ISO code */
86 static void remap_synonym(char *name)
89 for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
91 if (!strcasecmp(_country_synonyms[i],name))
93 TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
94 name[0] = _country_synonyms[i+1][0];
95 name[1] = _country_synonyms[i+1][1];
102 /* Note: Flags are weighted in order of matching importance */
103 #define FOUND_LANGUAGE 0x4
104 #define FOUND_COUNTRY 0x2
105 #define FOUND_CODEPAGE 0x1
108 char search_language[MAX_ELEM_LEN];
109 char search_country[MAX_ELEM_LEN];
110 char search_codepage[MAX_ELEM_LEN];
111 char found_language[MAX_ELEM_LEN];
112 char found_country[MAX_ELEM_LEN];
113 char found_codepage[MAX_ELEM_LEN];
114 unsigned int match_flags;
115 LANGID found_lang_id;
118 #define CONTINUE_LOOKING TRUE
119 #define STOP_LOOKING FALSE
121 /* INTERNAL: Get and compare locale info with a given string */
122 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp)
125 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
126 if (!buff[0] || !cmp[0])
128 /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
129 return !strncasecmp(cmp, buff, strlen(cmp));
133 find_best_locale_proc(HMODULE hModule WINE_UNUSED, LPCSTR type WINE_UNUSED,
134 LPCSTR name WINE_UNUSED, WORD LangID, LONG lParam)
136 locale_search_t *res = (locale_search_t *)lParam;
137 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
138 char buff[MAX_ELEM_LEN];
139 unsigned int flags = 0;
141 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
142 return CONTINUE_LOOKING;
145 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
146 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
147 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
149 TRACE(":Found language: %s->%s\n", res->search_language, buff);
150 flags |= FOUND_LANGUAGE;
151 memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
153 else if (res->match_flags & FOUND_LANGUAGE)
155 return CONTINUE_LOOKING;
159 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
160 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
161 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
163 TRACE("Found country:%s->%s\n", res->search_country, buff);
164 flags |= FOUND_COUNTRY;
165 memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
167 else if (res->match_flags & FOUND_COUNTRY)
169 return CONTINUE_LOOKING;
173 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
174 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
176 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
177 flags |= FOUND_CODEPAGE;
178 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
180 else if (res->match_flags & FOUND_CODEPAGE)
182 return CONTINUE_LOOKING;
185 if (flags > res->match_flags)
187 /* Found a better match than previously */
188 res->match_flags = flags;
189 res->found_lang_id = LangID;
191 if (flags & (FOUND_LANGUAGE & FOUND_COUNTRY & FOUND_CODEPAGE))
193 TRACE(":found exact locale match\n");
196 return CONTINUE_LOOKING;
199 extern int atoi(const char *);
201 /* Internal: Find the LCID for a locale specification */
202 static LCID MSVCRT_locale_to_LCID(locale_search_t* locale)
205 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
206 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
209 if (!locale->match_flags)
212 /* If we were given something that didn't match, fail */
213 if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
216 lcid = MAKELCID(locale->found_lang_id, SORT_DEFAULT);
218 /* Populate partial locale, translating LCID to locale string elements */
219 if (!locale->found_codepage[0])
221 /* Even if a codepage is not enumerated for a locale
222 * it can be set if valid */
223 if (locale->search_codepage[0])
225 if (IsValidCodePage(atoi(locale->search_codepage)))
226 memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
229 /* Special codepage values: OEM & ANSI */
230 if (strcasecmp(locale->search_codepage,"OCP"))
232 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
233 locale->found_codepage, MAX_ELEM_LEN);
235 if (strcasecmp(locale->search_codepage,"ACP"))
237 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
238 locale->found_codepage, MAX_ELEM_LEN);
243 if (!atoi(locale->found_codepage))
249 /* Prefer ANSI codepages if present */
250 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
251 locale->found_codepage, MAX_ELEM_LEN);
252 if (!locale->found_codepage[0] || !atoi(locale->found_codepage))
253 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
254 locale->found_codepage, MAX_ELEM_LEN);
257 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
258 locale->found_language, MAX_ELEM_LEN);
259 GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
260 locale->found_country, MAX_ELEM_LEN);
264 /* INTERNAL: Set ctype behaviour for a codepage */
265 static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
269 memset(&cp, 0, sizeof(CPINFO));
271 if (GetCPInfo(codepage, &cp))
275 unsigned char *traverse = (unsigned char *)cp.LeadByte;
277 memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
278 MSVCRT_current_lc_all_cp = codepage;
280 /* Switch ctype macros to MBCS if needed */
281 MSVCRT___mb_cur_max = cp.MaxCharSize;
283 /* Set remaining ctype flags: FIXME: faster way to do this? */
285 for (i = 0; i < 256; i++)
287 if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
290 GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
294 /* Set leadbyte flags */
295 while (traverse[0] || traverse[1])
297 for( i = traverse[0]; i <= traverse[1]; i++ )
298 MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
305 /*********************************************************************
306 * setlocale (MSVCRT.@)
308 char* MSVCRT_setlocale(int category, const char* locale)
312 int haveLang, haveCountry, haveCP;
316 TRACE("(%d %s)\n",category,locale);
318 if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
323 /* Report the current Locale */
324 return MSVCRT_current_lc_all;
329 if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
331 FIXME(":restore previous locale not implemented!\n");
332 /* FIXME: Easiest way to do this is parse the string and
333 * call this function recursively with its elements,
334 * Where they differ for each lc_ type.
337 return MSVCRT_current_lc_all;
340 /* Default Locale: Special case handling */
341 if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
343 MSVCRT_current_lc_all[0] = 'C';
344 MSVCRT_current_lc_all[1] = '\0';
345 MSVCRT_current_lc_all_cp = GetACP();
349 lc_all = 1; /* Fall through all cases ... */
350 case MSVCRT_LC_COLLATE:
352 case MSVCRT_LC_CTYPE:
353 /* Restore C locale ctype info */
354 MSVCRT___mb_cur_max = 1;
355 memcpy(MSVCRT_current_ctype, MSVCRT__ctype, sizeof(MSVCRT__ctype));
356 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
358 case MSVCRT_LC_MONETARY:
360 case MSVCRT_LC_NUMERIC:
366 return MSVCRT_current_lc_all;
369 /* Get locale elements */
370 haveLang = haveCountry = haveCP = 0;
371 memset(&lc,0,sizeof(lc));
373 next = strchr(locale,'_');
374 if (next && next != locale)
377 strncpy(lc.search_language,locale,next-locale);
378 locale += next-locale+1;
381 next = strchr(locale,'.');
388 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
395 strncpy(lc.search_country,locale,next-locale);
396 locale += next-locale+1;
401 strncpy(lc.search_language,locale,next-locale);
402 locale += next-locale+1;
404 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
412 strncpy(lc.search_country, locale, MAX_ELEM_LEN);
417 strncpy(lc.search_language, locale, MAX_ELEM_LEN);
422 remap_synonym(lc.search_country);
424 if (haveCP && !haveCountry && !haveLang)
426 FIXME(":Codepage only locale not implemented\n");
427 /* FIXME: Use default lang/country and skip locale_to_LCID()
434 lcid = MSVCRT_locale_to_LCID(&lc);
436 TRACE(":found LCID %ld\n",lcid);
444 MSVCRT_current_lc_all_lcid = lcid;
446 snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
447 lc.found_language,lc.found_country,lc.found_codepage);
451 lc_all = 1; /* Fall through all cases ... */
452 case MSVCRT_LC_COLLATE:
454 case MSVCRT_LC_CTYPE:
455 msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
457 case MSVCRT_LC_MONETARY:
459 case MSVCRT_LC_NUMERIC:
465 return MSVCRT_current_lc_all;
469 /*********************************************************************
470 * _Getdays (MSVCRT.@)
472 const char* _Getdays(void)
474 static const char *MSVCRT_days = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
475 "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
476 /* FIXME: Use locale */
477 TRACE("(void) semi-stub\n");
481 /*********************************************************************
482 * _Getmonths (MSVCRT.@)
484 const char* _Getmonths(void)
486 static const char *MSVCRT_months = ":Jan:January:Feb:February:Mar:March:Apr:"
487 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
488 "October:Nov:November:Dec:December";
489 /* FIXME: Use locale */
490 TRACE("(void) semi-stub\n");
491 return MSVCRT_months;
494 /*********************************************************************
495 * _Getnames (MSVCRT.@)
497 const char* _Getnames(void)
500 TRACE("(void) stub\n");
504 /*********************************************************************
505 * _Strftime (MSVCRT.@)
507 const char* _Strftime(char *out, unsigned int len, const char *fmt,
508 const void *tm, void *foo)
511 TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
515 /* FIXME: MBCP probably belongs in mbcs.c */
517 /*********************************************************************
518 * _setmbcp (MSVCRT.@)
520 void _setmbcp(int cp)
523 if (MSVCRT_current_lc_all_cp != cp)
525 /* FIXME: set ctype behaviour for this cp */
526 MSVCRT_current_lc_all_cp = cp;
531 /*********************************************************************
532 * _getmbcp (MSVCRT.@)
536 return MSVCRT_current_lc_all_cp;
539 /*********************************************************************
540 * __crtLCMapStringA (MSVCRT.@)
542 int __crtLCMapStringA(
543 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
544 int dstlen, unsigned int codepage, int xflag
546 FIXME("(lcid %lx, flags %lx, %s(%d), %p(%d), %x, %d), partial stub!\n",
547 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
548 /* FIXME: A bit incorrect. But msvcrt itself just converts its
549 * arguments to wide strings and then calls LCMapStringW
551 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
554 /*********************************************************************
555 * localeconv (MSVCRT.@)
557 struct MSVCRT_lconv *MSVCRT_localeconv(void) {
559 struct lconv *ylconv;
560 static struct MSVCRT_lconv xlconv;
562 ylconv = localeconv();
564 #define X(x) xlconv.x = ylconv->x;
570 X(mon_decimal_point);
571 X(mon_thousands_sep);
586 /*********************************************************************
587 * _Gettnames (MSVCRT.@)
589 void *_Gettnames(void)
591 FIXME("(void), stub!\n");