source: trunk/src/msvcrt/locale.c@ 9633

Last change on this file since 9633 was 9633, checked in by sandervl, 23 years ago

PF: Msvcrt Wine port with GCC

File size: 14.2 KB
Line 
1/*
2 * msvcrt.dll locale functions
3 *
4 * Copyright 2000 Jon Griffiths
5 *
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.
10 *
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.
15 *
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
19 */
20#ifndef __WIN32OS2__
21#include "config.h"
22#else
23#include <emxheader.h>
24#endif
25
26#include "wine/port.h"
27
28#include "winbase.h"
29#include "winuser.h"
30
31#include "msvcrt.h"
32#include "msvcrt/locale.h"
33#include "mtdll.h"
34
35#include "wine/debug.h"
36
37WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38
39/* FIXME: Need to hold locale for each LC_* type and aggregate
40 * string to produce lc_all.
41 */
42#define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
43#define MAX_LOCALE_LENGTH 256
44char MSVCRT_current_lc_all[MAX_LOCALE_LENGTH];
45LCID MSVCRT_current_lc_all_lcid;
46int MSVCRT_current_lc_all_cp;
47
48/* MT */
49#define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
50#define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
51
52/* ctype data modified when the locale changes */
53extern WORD MSVCRT__ctype [257];
54extern WORD MSVCRT_current_ctype[257];
55extern WORD* MSVCRT__pctype;
56
57/* mbctype data modified when the locale changes */
58extern int MSVCRT___mb_cur_max;
59extern unsigned char MSVCRT_mbctype[257];
60
61#define MSVCRT_LEADBYTE 0x8000
62
63/* Friendly country strings & iso codes for synonym support.
64 * Based on MS documentation for setlocale().
65 */
66static const char* _country_synonyms[] =
67{
68 "Hong Kong","HK",
69 "Hong-Kong","HK",
70 "New Zealand","NZ",
71 "New-Zealand","NZ",
72 "PR China","CN",
73 "PR-China","CN",
74 "United Kingdom","GB",
75 "United-Kingdom","GB",
76 "Britain","GB",
77 "England","GB",
78 "Great Britain","GB",
79 "United States","US",
80 "United-States","US",
81 "America","US"
82};
83
84/* INTERNAL: Map a synonym to an ISO code */
85static void remap_synonym(char *name)
86{
87 size_t i;
88 for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
89 {
90 if (!strcasecmp(_country_synonyms[i],name))
91 {
92 TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
93 name[0] = _country_synonyms[i+1][0];
94 name[1] = _country_synonyms[i+1][1];
95 name[2] = '\0';
96 return;
97 }
98 }
99}
100
101/* Note: Flags are weighted in order of matching importance */
102#define FOUND_LANGUAGE 0x4
103#define FOUND_COUNTRY 0x2
104#define FOUND_CODEPAGE 0x1
105
106typedef struct {
107 char search_language[MAX_ELEM_LEN];
108 char search_country[MAX_ELEM_LEN];
109 char search_codepage[MAX_ELEM_LEN];
110 char found_language[MAX_ELEM_LEN];
111 char found_country[MAX_ELEM_LEN];
112 char found_codepage[MAX_ELEM_LEN];
113 unsigned int match_flags;
114 LANGID found_lang_id;
115} locale_search_t;
116
117#define CONTINUE_LOOKING TRUE
118#define STOP_LOOKING FALSE
119
120/* INTERNAL: Get and compare locale info with a given string */
121static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp)
122{
123 buff[0] = 0;
124 GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE,buff, MAX_ELEM_LEN);
125 if (!buff[0] || !cmp[0])
126 return 0;
127 /* Partial matches are allowed, e.g. "Germ" matches "Germany" */
128 return !strncasecmp(cmp, buff, strlen(cmp));
129}
130
131static BOOL CALLBACK
132find_best_locale_proc(HMODULE hModule WINE_UNUSED, LPCSTR type WINE_UNUSED,
133 LPCSTR name WINE_UNUSED, WORD LangID, LONG lParam)
134{
135 locale_search_t *res = (locale_search_t *)lParam;
136 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
137 char buff[MAX_ELEM_LEN];
138 unsigned int flags = 0;
139
140 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
141 return CONTINUE_LOOKING;
142
143 /* Check Language */
144 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
145 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
146 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
147 {
148 TRACE(":Found language: %s->%s\n", res->search_language, buff);
149 flags |= FOUND_LANGUAGE;
150 memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
151 }
152 else if (res->match_flags & FOUND_LANGUAGE)
153 {
154 return CONTINUE_LOOKING;
155 }
156
157 /* Check Country */
158 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
159 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
160 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
161 {
162 TRACE("Found country:%s->%s\n", res->search_country, buff);
163 flags |= FOUND_COUNTRY;
164 memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
165 }
166 else if (res->match_flags & FOUND_COUNTRY)
167 {
168 return CONTINUE_LOOKING;
169 }
170
171 /* Check codepage */
172 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
173 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
174 {
175 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
176 flags |= FOUND_CODEPAGE;
177 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
178 }
179 else if (res->match_flags & FOUND_CODEPAGE)
180 {
181 return CONTINUE_LOOKING;
182 }
183
184 if (flags > res->match_flags)
185 {
186 /* Found a better match than previously */
187 res->match_flags = flags;
188 res->found_lang_id = LangID;
189 }
190 if (flags & (FOUND_LANGUAGE & FOUND_COUNTRY & FOUND_CODEPAGE))
191 {
192 TRACE(":found exact locale match\n");
193 return STOP_LOOKING;
194 }
195 return CONTINUE_LOOKING;
196}
197
198extern int atoi(const char *);
199
200/* Internal: Find the LCID for a locale specification */
201static LCID MSVCRT_locale_to_LCID(locale_search_t* locale)
202{
203 LCID lcid;
204 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
205 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
206 (LONG)locale);
207
208 if (!locale->match_flags)
209 return 0;
210
211 /* If we were given something that didn't match, fail */
212 if (locale->search_country[0] && !(locale->match_flags & FOUND_COUNTRY))
213 return 0;
214
215 lcid = MAKELCID(locale->found_lang_id, SORT_DEFAULT);
216
217 /* Populate partial locale, translating LCID to locale string elements */
218 if (!locale->found_codepage[0])
219 {
220 /* Even if a codepage is not enumerated for a locale
221 * it can be set if valid */
222 if (locale->search_codepage[0])
223 {
224 if (IsValidCodePage(atoi(locale->search_codepage)))
225 memcpy(locale->found_codepage,locale->search_codepage,MAX_ELEM_LEN);
226 else
227 {
228 /* Special codepage values: OEM & ANSI */
229 if (strcasecmp(locale->search_codepage,"OCP"))
230 {
231 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
232 locale->found_codepage, MAX_ELEM_LEN);
233 }
234 if (strcasecmp(locale->search_codepage,"ACP"))
235 {
236 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
237 locale->found_codepage, MAX_ELEM_LEN);
238 }
239 else
240 return 0;
241
242 if (!atoi(locale->found_codepage))
243 return 0;
244 }
245 }
246 else
247 {
248 /* Prefer ANSI codepages if present */
249 GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
250 locale->found_codepage, MAX_ELEM_LEN);
251 if (!locale->found_codepage[0] || !atoi(locale->found_codepage))
252 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
253 locale->found_codepage, MAX_ELEM_LEN);
254 }
255 }
256 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
257 locale->found_language, MAX_ELEM_LEN);
258 GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY|LOCALE_NOUSEROVERRIDE,
259 locale->found_country, MAX_ELEM_LEN);
260 return lcid;
261}
262
263extern int snprintf(char *, int, const char *, ...);
264
265/* INTERNAL: Set ctype behaviour for a codepage */
266static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
267{
268 CPINFO cp;
269
270 memset(&cp, 0, sizeof(CPINFO));
271
272 if (GetCPInfo(codepage, &cp))
273 {
274 int i;
275 char str[3];
276 unsigned char *traverse = (unsigned char *)cp.LeadByte;
277
278 memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
279 MSVCRT_current_lc_all_cp = codepage;
280
281 /* Switch ctype macros to MBCS if needed */
282 MSVCRT___mb_cur_max = cp.MaxCharSize;
283
284 /* Set remaining ctype flags: FIXME: faster way to do this? */
285 str[1] = str[2] = 0;
286 for (i = 0; i < 256; i++)
287 {
288 if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
289 {
290 str[0] = i;
291 GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
292 }
293 }
294
295 /* Set leadbyte flags */
296 while (traverse[0] || traverse[1])
297 {
298 for( i = traverse[0]; i <= traverse[1]; i++ )
299 MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
300 traverse += 2;
301 };
302 }
303}
304
305
306/*********************************************************************
307 * setlocale (MSVCRT.@)
308 */
309char* MSVCRT_setlocale(int category, const char* locale)
310{
311 LCID lcid = 0;
312 locale_search_t lc;
313 int haveLang, haveCountry, haveCP;
314 char* next;
315 int lc_all = 0;
316
317 TRACE("(%d %s)\n",category,locale);
318
319 if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
320 return NULL;
321
322 if (locale == NULL)
323 {
324 /* Report the current Locale */
325 return MSVCRT_current_lc_all;
326 }
327
328 LOCK_LOCALE;
329
330 if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
331 {
332 FIXME(":restore previous locale not implemented!\n");
333 /* FIXME: Easiest way to do this is parse the string and
334 * call this function recursively with its elements,
335 * Where they differ for each lc_ type.
336 */
337 UNLOCK_LOCALE;
338 return MSVCRT_current_lc_all;
339 }
340
341 /* Default Locale: Special case handling */
342 if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
343 {
344 MSVCRT_current_lc_all[0] = 'C';
345 MSVCRT_current_lc_all[1] = '\0';
346 MSVCRT_current_lc_all_cp = GetACP();
347
348 switch (category) {
349 case MSVCRT_LC_ALL:
350 lc_all = 1; /* Fall through all cases ... */
351 case MSVCRT_LC_COLLATE:
352 if (!lc_all) break;
353 case MSVCRT_LC_CTYPE:
354 /* Restore C locale ctype info */
355 MSVCRT___mb_cur_max = 1;
356 memcpy(MSVCRT_current_ctype, MSVCRT__ctype, sizeof(MSVCRT__ctype));
357 memset(MSVCRT_mbctype, 0, sizeof(MSVCRT_mbctype));
358 if (!lc_all) break;
359 case MSVCRT_LC_MONETARY:
360 if (!lc_all) break;
361 case MSVCRT_LC_NUMERIC:
362 if (!lc_all) break;
363 case MSVCRT_LC_TIME:
364 break;
365 }
366 UNLOCK_LOCALE;
367 return MSVCRT_current_lc_all;
368 }
369
370 /* Get locale elements */
371 haveLang = haveCountry = haveCP = 0;
372 memset(&lc,0,sizeof(lc));
373
374 next = strchr(locale,'_');
375 if (next && next != locale)
376 {
377 haveLang = 1;
378 strncpy(lc.search_language,locale,next-locale);
379 locale += next-locale+1;
380 }
381
382 next = strchr(locale,'.');
383 if (next)
384 {
385 haveCP = 1;
386 if (next == locale)
387 {
388 locale++;
389 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
390 }
391 else
392 {
393 if (haveLang)
394 {
395 haveCountry = 1;
396 strncpy(lc.search_country,locale,next-locale);
397 locale += next-locale+1;
398 }
399 else
400 {
401 haveLang = 1;
402 strncpy(lc.search_language,locale,next-locale);
403 locale += next-locale+1;
404 }
405 strncpy(lc.search_codepage, locale, MAX_ELEM_LEN);
406 }
407 }
408 else
409 {
410 if (haveLang)
411 {
412 haveCountry = 1;
413 strncpy(lc.search_country, locale, MAX_ELEM_LEN);
414 }
415 else
416 {
417 haveLang = 1;
418 strncpy(lc.search_language, locale, MAX_ELEM_LEN);
419 }
420 }
421
422 if (haveCountry)
423 remap_synonym(lc.search_country);
424
425 if (haveCP && !haveCountry && !haveLang)
426 {
427 FIXME(":Codepage only locale not implemented\n");
428 /* FIXME: Use default lang/country and skip locale_to_LCID()
429 * call below...
430 */
431 UNLOCK_LOCALE;
432 return NULL;
433 }
434
435 lcid = MSVCRT_locale_to_LCID(&lc);
436
437 TRACE(":found LCID %ld\n",lcid);
438
439 if (lcid == 0)
440 {
441 UNLOCK_LOCALE;
442 return NULL;
443 }
444
445 MSVCRT_current_lc_all_lcid = lcid;
446
447 snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
448 lc.found_language,lc.found_country,lc.found_codepage);
449
450 switch (category) {
451 case MSVCRT_LC_ALL:
452 lc_all = 1; /* Fall through all cases ... */
453 case MSVCRT_LC_COLLATE:
454 if (!lc_all) break;
455 case MSVCRT_LC_CTYPE:
456 msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
457 if (!lc_all) break;
458 case MSVCRT_LC_MONETARY:
459 if (!lc_all) break;
460 case MSVCRT_LC_NUMERIC:
461 if (!lc_all) break;
462 case MSVCRT_LC_TIME:
463 break;
464 }
465 UNLOCK_LOCALE;
466 return MSVCRT_current_lc_all;
467}
468
469
470/*********************************************************************
471 * _Getdays (MSVCRT.@)
472 */
473const char* _Getdays(void)
474{
475 static const char *MSVCRT_days = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
476 "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
477 /* FIXME: Use locale */
478 TRACE("(void) semi-stub\n");
479 return MSVCRT_days;
480}
481
482/*********************************************************************
483 * _Getmonths (MSVCRT.@)
484 */
485const char* _Getmonths(void)
486{
487 static const char *MSVCRT_months = ":Jan:January:Feb:February:Mar:March:Apr:"
488 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
489 "October:Nov:November:Dec:December";
490 /* FIXME: Use locale */
491 TRACE("(void) semi-stub\n");
492 return MSVCRT_months;
493}
494
495/*********************************************************************
496 * _Getnames (MSVCRT.@)
497 */
498const char* _Getnames(void)
499{
500 /* FIXME: */
501 TRACE("(void) stub\n");
502 return "";
503}
504
505/*********************************************************************
506 * _Strftime (MSVCRT.@)
507 */
508const char* _Strftime(char *out, unsigned int len, const char *fmt,
509 const void *tm, void *foo)
510{
511 /* FIXME: */
512 TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
513 return "";
514}
515
516/* FIXME: MBCP probably belongs in mbcs.c */
517
518/*********************************************************************
519 * _setmbcp (MSVCRT.@)
520 */
521void _setmbcp(int cp)
522{
523 LOCK_LOCALE;
524 if (MSVCRT_current_lc_all_cp != cp)
525 {
526 /* FIXME: set ctype behaviour for this cp */
527 MSVCRT_current_lc_all_cp = cp;
528 }
529 UNLOCK_LOCALE;
530}
531
532/*********************************************************************
533 * _getmbcp (MSVCRT.@)
534 */
535int _getmbcp(void)
536{
537 return MSVCRT_current_lc_all_cp;
538}
Note: See TracBrowser for help on using the repository browser.