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
25 #include "msvcrt/locale.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
32 /* FIXME: Need to hold locale for each LC_* type and aggregate
33 * string to produce lc_all.
35 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
36 #define MAX_LOCALE_LENGTH 256
37 char MSVCRT_current_lc_all
[MAX_LOCALE_LENGTH
];
38 LCID MSVCRT_current_lc_all_lcid
;
39 int MSVCRT_current_lc_all_cp
;
42 #define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
43 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
45 /* ctype data modified when the locale changes */
46 extern WORD MSVCRT__ctype
[257];
47 extern WORD MSVCRT_current_ctype
[257];
48 extern WORD
* MSVCRT__pctype
;
50 /* mbctype data modified when the locale changes */
51 extern int MSVCRT___mb_cur_max
;
52 extern unsigned char MSVCRT_mbctype
[257];
54 #define MSVCRT_LEADBYTE 0x8000
56 /* Friendly country strings & iso codes for synonym support.
57 * Based on MS documentation for setlocale().
59 static const char* _country_synonyms
[] =
67 "United Kingdom","GB",
68 "United-Kingdom","GB",
77 /* INTERNAL: Map a synonym to an ISO code */
78 static void remap_synonym(char *name
)
81 for (i
= 0; i
< sizeof(_country_synonyms
)/sizeof(char*); i
+= 2 )
83 if (!strcasecmp(_country_synonyms
[i
],name
))
85 TRACE(":Mapping synonym %s to %s\n",name
,_country_synonyms
[i
+1]);
86 name
[0] = _country_synonyms
[i
+1][0];
87 name
[1] = _country_synonyms
[i
+1][1];
94 /* Note: Flags are weighted in order of matching importance */
95 #define FOUND_LANGUAGE 0x4
96 #define FOUND_COUNTRY 0x2
97 #define FOUND_CODEPAGE 0x1
100 char search_language
[MAX_ELEM_LEN
];
101 char search_country
[MAX_ELEM_LEN
];
102 char search_codepage
[MAX_ELEM_LEN
];
103 char found_language
[MAX_ELEM_LEN
];
104 char found_country
[MAX_ELEM_LEN
];
105 char found_codepage
[MAX_ELEM_LEN
];
106 unsigned int match_flags
;
107 LANGID found_lang_id
;
110 #define CONTINUE_LOOKING TRUE
111 #define STOP_LOOKING FALSE
113 /* INTERNAL: Get and compare locale info with a given string */
114 static int compare_info(LCID lcid
, DWORD flags
, char* buff
, const char* cmp
)
117 GetLocaleInfoA(lcid
, flags
|LOCALE_NOUSEROVERRIDE
,buff
, MAX_ELEM_LEN
);
118 if (!buff
[0] || !cmp
[0])
120 /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
121 return !strncasecmp(cmp
, buff
, strlen(cmp
));
125 find_best_locale_proc(HMODULE hModule WINE_UNUSED
, LPCSTR type WINE_UNUSED
,
126 LPCSTR name WINE_UNUSED
, WORD LangID
, LONG lParam
)
128 locale_search_t
*res
= (locale_search_t
*)lParam
;
129 const LCID lcid
= MAKELCID(LangID
, SORT_DEFAULT
);
130 char buff
[MAX_ELEM_LEN
];
131 unsigned int flags
= 0;
133 if(PRIMARYLANGID(LangID
) == LANG_NEUTRAL
)
134 return CONTINUE_LOOKING
;
137 if (compare_info(lcid
,LOCALE_SISO639LANGNAME
,buff
,res
->search_language
) ||
138 compare_info(lcid
,LOCALE_SABBREVLANGNAME
,buff
,res
->search_language
) ||
139 compare_info(lcid
,LOCALE_SENGLANGUAGE
,buff
,res
->search_language
))
141 TRACE(":Found language: %s->%s\n", res
->search_language
, buff
);
142 flags
|= FOUND_LANGUAGE
;
143 memcpy(res
->found_language
,res
->search_language
,MAX_ELEM_LEN
);
145 else if (res
->match_flags
& FOUND_LANGUAGE
)
147 return CONTINUE_LOOKING
;
151 if (compare_info(lcid
,LOCALE_SISO3166CTRYNAME
,buff
,res
->search_country
) ||
152 compare_info(lcid
,LOCALE_SABBREVCTRYNAME
,buff
,res
->search_country
) ||
153 compare_info(lcid
,LOCALE_SENGCOUNTRY
,buff
,res
->search_country
))
155 TRACE("Found country:%s->%s\n", res
->search_country
, buff
);
156 flags
|= FOUND_COUNTRY
;
157 memcpy(res
->found_country
,res
->search_country
,MAX_ELEM_LEN
);
159 else if (res
->match_flags
& FOUND_COUNTRY
)
161 return CONTINUE_LOOKING
;
165 if (compare_info(lcid
,LOCALE_IDEFAULTCODEPAGE
,buff
,res
->search_codepage
) ||
166 (compare_info(lcid
,LOCALE_IDEFAULTANSICODEPAGE
,buff
,res
->search_codepage
)))
168 TRACE("Found codepage:%s->%s\n", res
->search_codepage
, buff
);
169 flags
|= FOUND_CODEPAGE
;
170 memcpy(res
->found_codepage
,res
->search_codepage
,MAX_ELEM_LEN
);
172 else if (res
->match_flags
& FOUND_CODEPAGE
)
174 return CONTINUE_LOOKING
;
177 if (flags
> res
->match_flags
)
179 /* Found a better match than previously */
180 res
->match_flags
= flags
;
181 res
->found_lang_id
= LangID
;
183 if (flags
& (FOUND_LANGUAGE
& FOUND_COUNTRY
& FOUND_CODEPAGE
))
185 TRACE(":found exact locale match\n");
188 return CONTINUE_LOOKING
;
191 extern int atoi(const char *);
193 /* Internal: Find the LCID for a locale specification */
194 static LCID
MSVCRT_locale_to_LCID(locale_search_t
* locale
)
197 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA
,
198 (LPCSTR
)LOCALE_ILANGUAGE
,find_best_locale_proc
,
201 if (!locale
->match_flags
)
204 /* If we were given something that didn't match, fail */
205 if (locale
->search_country
[0] && !(locale
->match_flags
& FOUND_COUNTRY
))
208 lcid
= MAKELCID(locale
->found_lang_id
, SORT_DEFAULT
);
210 /* Populate partial locale, translating LCID to locale string elements */
211 if (!locale
->found_codepage
[0])
213 /* Even if a codepage is not enumerated for a locale
214 * it can be set if valid */
215 if (locale
->search_codepage
[0])
217 if (IsValidCodePage(atoi(locale
->search_codepage
)))
218 memcpy(locale
->found_codepage
,locale
->search_codepage
,MAX_ELEM_LEN
);
221 /* Special codepage values: OEM & ANSI */
222 if (strcasecmp(locale
->search_codepage
,"OCP"))
224 GetLocaleInfoA(lcid
, LOCALE_IDEFAULTCODEPAGE
,
225 locale
->found_codepage
, MAX_ELEM_LEN
);
227 if (strcasecmp(locale
->search_codepage
,"ACP"))
229 GetLocaleInfoA(lcid
, LOCALE_IDEFAULTANSICODEPAGE
,
230 locale
->found_codepage
, MAX_ELEM_LEN
);
235 if (!atoi(locale
->found_codepage
))
241 /* Prefer ANSI codepages if present */
242 GetLocaleInfoA(lcid
, LOCALE_IDEFAULTANSICODEPAGE
,
243 locale
->found_codepage
, MAX_ELEM_LEN
);
244 if (!locale
->found_codepage
[0] || !atoi(locale
->found_codepage
))
245 GetLocaleInfoA(lcid
, LOCALE_IDEFAULTCODEPAGE
,
246 locale
->found_codepage
, MAX_ELEM_LEN
);
249 GetLocaleInfoA(lcid
, LOCALE_SENGLANGUAGE
|LOCALE_NOUSEROVERRIDE
,
250 locale
->found_language
, MAX_ELEM_LEN
);
251 GetLocaleInfoA(lcid
, LOCALE_SENGCOUNTRY
|LOCALE_NOUSEROVERRIDE
,
252 locale
->found_country
, MAX_ELEM_LEN
);
256 extern int snprintf(char *, int, const char *, ...);
258 /* INTERNAL: Set ctype behaviour for a codepage */
259 static void msvcrt_set_ctype(unsigned int codepage
, LCID lcid
)
263 memset(&cp
, 0, sizeof(CPINFO
));
265 if (GetCPInfo(codepage
, &cp
))
269 unsigned char *traverse
= (unsigned char *)cp
.LeadByte
;
271 memset(MSVCRT_current_ctype
, 0, sizeof(MSVCRT__ctype
));
272 MSVCRT_current_lc_all_cp
= codepage
;
274 /* Switch ctype macros to MBCS if needed */
275 MSVCRT___mb_cur_max
= cp
.MaxCharSize
;
277 /* Set remaining ctype flags: FIXME: faster way to do this? */
279 for (i
= 0; i
< 256; i
++)
281 if (!(MSVCRT__pctype
[i
] & MSVCRT_LEADBYTE
))
284 GetStringTypeA(lcid
, CT_CTYPE1
, str
, 1, MSVCRT__pctype
+ i
);
288 /* Set leadbyte flags */
289 while (traverse
[0] || traverse
[1])
291 for( i
= traverse
[0]; i
<= traverse
[1]; i
++ )
292 MSVCRT_current_ctype
[i
+1] |= MSVCRT_LEADBYTE
;
299 /*********************************************************************
300 * setlocale (MSVCRT.@)
302 char* MSVCRT_setlocale(int category
, const char* locale
)
306 int haveLang
, haveCountry
, haveCP
;
310 TRACE("(%d %s)\n",category
,locale
);
312 if (category
< MSVCRT_LC_MIN
|| category
> MSVCRT_LC_MAX
)
317 /* Report the current Locale */
318 return MSVCRT_current_lc_all
;
323 if (locale
[0] == 'L' && locale
[1] == 'C' && locale
[2] == '_')
325 FIXME(":restore previous locale not implemented!\n");
326 /* FIXME: Easiest way to do this is parse the string and
327 * call this function recursively with its elements,
328 * Where they differ for each lc_ type.
331 return MSVCRT_current_lc_all
;
334 /* Default Locale: Special case handling */
335 if (!strlen(locale
) || ((toupper(locale
[0]) == 'C') && !locale
[1]))
337 MSVCRT_current_lc_all
[0] = 'C';
338 MSVCRT_current_lc_all
[1] = '\0';
339 MSVCRT_current_lc_all_cp
= GetACP();
343 lc_all
= 1; /* Fall through all cases ... */
344 case MSVCRT_LC_COLLATE
:
346 case MSVCRT_LC_CTYPE
:
347 /* Restore C locale ctype info */
348 MSVCRT___mb_cur_max
= 1;
349 memcpy(MSVCRT_current_ctype
, MSVCRT__ctype
, sizeof(MSVCRT__ctype
));
350 memset(MSVCRT_mbctype
, 0, sizeof(MSVCRT_mbctype
));
352 case MSVCRT_LC_MONETARY
:
354 case MSVCRT_LC_NUMERIC
:
360 return MSVCRT_current_lc_all
;
363 /* Get locale elements */
364 haveLang
= haveCountry
= haveCP
= 0;
365 memset(&lc
,0,sizeof(lc
));
367 next
= strchr(locale
,'_');
368 if (next
&& next
!= locale
)
371 strncpy(lc
.search_language
,locale
,next
-locale
);
372 locale
+= next
-locale
+1;
375 next
= strchr(locale
,'.');
382 strncpy(lc
.search_codepage
, locale
, MAX_ELEM_LEN
);
389 strncpy(lc
.search_country
,locale
,next
-locale
);
390 locale
+= next
-locale
+1;
395 strncpy(lc
.search_language
,locale
,next
-locale
);
396 locale
+= next
-locale
+1;
398 strncpy(lc
.search_codepage
, locale
, MAX_ELEM_LEN
);
406 strncpy(lc
.search_country
, locale
, MAX_ELEM_LEN
);
411 strncpy(lc
.search_language
, locale
, MAX_ELEM_LEN
);
416 remap_synonym(lc
.search_country
);
418 if (haveCP
&& !haveCountry
&& !haveLang
)
420 FIXME(":Codepage only locale not implemented\n");
421 /* FIXME: Use default lang/country and skip locale_to_LCID()
428 lcid
= MSVCRT_locale_to_LCID(&lc
);
430 TRACE(":found LCID %ld\n",lcid
);
438 MSVCRT_current_lc_all_lcid
= lcid
;
440 snprintf(MSVCRT_current_lc_all
,MAX_LOCALE_LENGTH
,"%s_%s.%s",
441 lc
.found_language
,lc
.found_country
,lc
.found_codepage
);
445 lc_all
= 1; /* Fall through all cases ... */
446 case MSVCRT_LC_COLLATE
:
448 case MSVCRT_LC_CTYPE
:
449 msvcrt_set_ctype(atoi(lc
.found_codepage
),lcid
);
451 case MSVCRT_LC_MONETARY
:
453 case MSVCRT_LC_NUMERIC
:
459 return MSVCRT_current_lc_all
;
463 /*********************************************************************
464 * _Getdays (MSVCRT.@)
466 const char* _Getdays(void)
468 static const char *MSVCRT_days
= ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
469 "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
470 /* FIXME: Use locale */
471 TRACE("(void) semi-stub\n");
475 /*********************************************************************
476 * _Getmonths (MSVCRT.@)
478 const char* _Getmonths(void)
480 static const char *MSVCRT_months
= ":Jan:January:Feb:February:Mar:March:Apr:"
481 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
482 "October:Nov:November:Dec:December";
483 /* FIXME: Use locale */
484 TRACE("(void) semi-stub\n");
485 return MSVCRT_months
;
488 /*********************************************************************
489 * _Getnames (MSVCRT.@)
491 const char* _Getnames(void)
494 TRACE("(void) stub\n");
498 /*********************************************************************
499 * _Strftime (MSVCRT.@)
501 const char* _Strftime(char *out
, unsigned int len
, const char *fmt
,
502 const void *tm
, void *foo
)
505 TRACE("(%p %d %s %p %p) stub\n", out
, len
, fmt
, tm
, foo
);
509 /* FIXME: MBCP probably belongs in mbcs.c */
511 /*********************************************************************
512 * _setmbcp (MSVCRT.@)
514 void _setmbcp(int cp
)
517 if (MSVCRT_current_lc_all_cp
!= cp
)
519 /* FIXME: set ctype behaviour for this cp */
520 MSVCRT_current_lc_all_cp
= cp
;
525 /*********************************************************************
526 * _getmbcp (MSVCRT.@)
530 return MSVCRT_current_lc_all_cp
;