ole32: Add error checking to StorageBaseImpl_CreateStream.
[wine/multimedia.git] / dlls / msvcrt / locale.c
blobc60b80d4274e9cdd2d8882aeafe9ada30fa6e9df
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 <limits.h>
25 #include <locale.h>
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winnls.h"
34 #include "msvcrt.h"
35 #include "mtdll.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] = { 0 };
47 LCID MSVCRT_current_lc_all_lcid = 0;
48 int MSVCRT___lc_codepage = 0;
49 int MSVCRT___lc_collate_cp = 0;
50 HANDLE MSVCRT___lc_handle[MSVCRT_LC_MAX - MSVCRT_LC_MIN + 1] = { 0 };
51 unsigned char charmax = CHAR_MAX;
53 /* MT */
54 #define LOCK_LOCALE _mlock(_SETLOCALE_LOCK);
55 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
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 * const _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 unsigned int 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, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
130 locale_search_t *res = (locale_search_t *)lParam;
131 const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
132 char buff[MAX_ELEM_LEN];
133 unsigned int flags = 0;
135 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
136 return CONTINUE_LOOKING;
138 /* Check Language */
139 if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language) ||
140 compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language) ||
141 compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language))
143 TRACE(":Found language: %s->%s\n", res->search_language, buff);
144 flags |= FOUND_LANGUAGE;
145 memcpy(res->found_language,res->search_language,MAX_ELEM_LEN);
147 else if (res->match_flags & FOUND_LANGUAGE)
149 return CONTINUE_LOOKING;
152 /* Check Country */
153 if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country) ||
154 compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country) ||
155 compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country))
157 TRACE("Found country:%s->%s\n", res->search_country, buff);
158 flags |= FOUND_COUNTRY;
159 memcpy(res->found_country,res->search_country,MAX_ELEM_LEN);
161 else if (res->match_flags & FOUND_COUNTRY)
163 return CONTINUE_LOOKING;
166 /* Check codepage */
167 if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage) ||
168 (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage)))
170 TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
171 flags |= FOUND_CODEPAGE;
172 memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
174 else if (res->match_flags & FOUND_CODEPAGE)
176 return CONTINUE_LOOKING;
179 if (flags > res->match_flags)
181 /* Found a better match than previously */
182 res->match_flags = flags;
183 res->found_lang_id = LangID;
185 if ((flags & (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE)) ==
186 (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"), (LPSTR)RT_STRING,
201 (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
202 (LONG_PTR)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 /* INTERNAL: Set ctype behaviour for a codepage */
260 static void msvcrt_set_ctype(unsigned int codepage, LCID lcid)
262 CPINFO cp;
264 memset(&cp, 0, sizeof(CPINFO));
266 if (GetCPInfo(codepage, &cp))
268 int i;
269 char str[3];
270 unsigned char *traverse = cp.LeadByte;
272 memset(MSVCRT_current_ctype, 0, sizeof(MSVCRT__ctype));
273 MSVCRT___lc_codepage = codepage;
274 MSVCRT___lc_collate_cp = codepage;
276 /* Switch ctype macros to MBCS if needed */
277 MSVCRT___mb_cur_max = cp.MaxCharSize;
279 /* Set remaining ctype flags: FIXME: faster way to do this? */
280 str[1] = str[2] = 0;
281 for (i = 0; i < 256; i++)
283 if (!(MSVCRT__pctype[i] & MSVCRT_LEADBYTE))
285 str[0] = i;
286 GetStringTypeA(lcid, CT_CTYPE1, str, 1, MSVCRT__pctype + i);
290 /* Set leadbyte flags */
291 while (traverse[0] || traverse[1])
293 for( i = traverse[0]; i <= traverse[1]; i++ )
294 MSVCRT_current_ctype[i+1] |= MSVCRT_LEADBYTE;
295 traverse += 2;
301 /*********************************************************************
302 * setlocale (MSVCRT.@)
304 char* CDECL MSVCRT_setlocale(int category, const char* locale)
306 LCID lcid = 0;
307 locale_search_t lc;
308 int haveLang, haveCountry, haveCP;
309 char* next;
310 int lc_all = 0;
312 TRACE("(%d %s)\n",category,locale);
314 if (category < MSVCRT_LC_MIN || category > MSVCRT_LC_MAX)
315 return NULL;
317 if (locale == NULL)
319 /* Report the current Locale */
320 return MSVCRT_current_lc_all;
323 LOCK_LOCALE;
325 if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_')
327 FIXME(":restore previous locale not implemented!\n");
328 /* FIXME: Easiest way to do this is parse the string and
329 * call this function recursively with its elements,
330 * Where they differ for each lc_ type.
332 UNLOCK_LOCALE;
333 return MSVCRT_current_lc_all;
336 /* Default Locale: Special case handling */
337 if (!strlen(locale) || ((toupper(locale[0]) == 'C') && !locale[1]))
339 MSVCRT_current_lc_all[0] = 'C';
340 MSVCRT_current_lc_all[1] = '\0';
341 MSVCRT___lc_codepage = GetACP();
342 MSVCRT___lc_collate_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 if (!lc_all) break;
354 case MSVCRT_LC_MONETARY:
355 if (!lc_all) break;
356 case MSVCRT_LC_NUMERIC:
357 if (!lc_all) break;
358 case MSVCRT_LC_TIME:
359 break;
361 UNLOCK_LOCALE;
362 return MSVCRT_current_lc_all;
365 /* Get locale elements */
366 haveLang = haveCountry = haveCP = 0;
367 memset(&lc,0,sizeof(lc));
369 next = strchr(locale,'_');
370 if (next && next != locale)
372 haveLang = 1;
373 memcpy(lc.search_language,locale,next-locale);
374 locale += next-locale+1;
377 next = strchr(locale,'.');
378 if (next)
380 haveCP = 1;
381 if (next == locale)
383 locale++;
384 lstrcpynA(lc.search_codepage, locale, MAX_ELEM_LEN);
386 else
388 if (haveLang)
390 haveCountry = 1;
391 memcpy(lc.search_country,locale,next-locale);
392 locale += next-locale+1;
394 else
396 haveLang = 1;
397 memcpy(lc.search_language,locale,next-locale);
398 locale += next-locale+1;
400 lstrcpynA(lc.search_codepage, locale, MAX_ELEM_LEN);
403 else
405 if (haveLang)
407 haveCountry = 1;
408 lstrcpynA(lc.search_country, locale, MAX_ELEM_LEN);
410 else
412 haveLang = 1;
413 lstrcpynA(lc.search_language, locale, MAX_ELEM_LEN);
417 if (haveCountry)
418 remap_synonym(lc.search_country);
420 if (haveCP && !haveCountry && !haveLang)
422 FIXME(":Codepage only locale not implemented\n");
423 /* FIXME: Use default lang/country and skip locale_to_LCID()
424 * call below...
426 UNLOCK_LOCALE;
427 return NULL;
430 lcid = MSVCRT_locale_to_LCID(&lc);
432 TRACE(":found LCID %d\n",lcid);
434 if (lcid == 0)
436 UNLOCK_LOCALE;
437 return NULL;
440 MSVCRT_current_lc_all_lcid = lcid;
442 snprintf(MSVCRT_current_lc_all,MAX_LOCALE_LENGTH,"%s_%s.%s",
443 lc.found_language,lc.found_country,lc.found_codepage);
445 switch (category) {
446 case MSVCRT_LC_ALL:
447 lc_all = 1; /* Fall through all cases ... */
448 case MSVCRT_LC_COLLATE:
449 if (!lc_all) break;
450 case MSVCRT_LC_CTYPE:
451 msvcrt_set_ctype(atoi(lc.found_codepage),lcid);
452 if (!lc_all) break;
453 case MSVCRT_LC_MONETARY:
454 if (!lc_all) break;
455 case MSVCRT_LC_NUMERIC:
456 if (!lc_all) break;
457 case MSVCRT_LC_TIME:
458 break;
460 UNLOCK_LOCALE;
461 return MSVCRT_current_lc_all;
464 /*********************************************************************
465 * wsetlocale (MSVCRT.@)
467 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
469 static MSVCRT_wchar_t fake[] = {
470 'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
471 'S','t','a','t','e','s','.','1','2','5','2',0 };
473 FIXME("%d %s\n", category, debugstr_w(locale));
475 return fake;
478 /*********************************************************************
479 * _Getdays (MSVCRT.@)
481 const char* CDECL _Getdays(void)
483 static const char MSVCRT_days[] = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
484 "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
485 /* FIXME: Use locale */
486 TRACE("(void) semi-stub\n");
487 return MSVCRT_days;
490 /*********************************************************************
491 * _Getmonths (MSVCRT.@)
493 const char* CDECL _Getmonths(void)
495 static const char MSVCRT_months[] = ":Jan:January:Feb:February:Mar:March:Apr:"
496 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
497 "October:Nov:November:Dec:December";
498 /* FIXME: Use locale */
499 TRACE("(void) semi-stub\n");
500 return MSVCRT_months;
503 /*********************************************************************
504 * _Gettnames (MSVCRT.@)
506 const char* CDECL _Gettnames(void)
508 /* FIXME: */
509 TRACE("(void) stub\n");
510 return "";
513 /*********************************************************************
514 * _Strftime (MSVCRT.@)
516 const char* CDECL _Strftime(char *out, unsigned int len, const char *fmt,
517 const void *tm, void *foo)
519 /* FIXME: */
520 TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
521 return "";
524 /*********************************************************************
525 * __crtLCMapStringA (MSVCRT.@)
527 int CDECL __crtLCMapStringA(
528 LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
529 int dstlen, unsigned int codepage, int xflag
531 FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
532 lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
533 /* FIXME: A bit incorrect. But msvcrt itself just converts its
534 * arguments to wide strings and then calls LCMapStringW
536 return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
539 /*********************************************************************
540 * __crtCompareStringA (MSVCRT.@)
542 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
543 const char *src2, int len2 )
545 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
546 lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
547 /* FIXME: probably not entirely right */
548 return CompareStringA( lcid, flags, src1, len1, src2, len2 );
551 /*********************************************************************
552 * __crtCompareStringW (MSVCRT.@)
554 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
555 const MSVCRT_wchar_t *src2, int len2 )
557 FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
558 lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
559 /* FIXME: probably not entirely right */
560 return CompareStringW( lcid, flags, src1, len1, src2, len2 );
563 /*********************************************************************
564 * __crtGetLocaleInfoW (MSVCRT.@)
566 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
568 FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
569 /* FIXME: probably not entirely right */
570 return GetLocaleInfoW( lcid, type, buffer, len );
573 /*********************************************************************
574 * localeconv (MSVCRT.@)
576 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
578 static struct MSVCRT_lconv xlconv;
579 struct lconv *ylconv = localeconv();
581 xlconv.decimal_point = ylconv->decimal_point;
582 xlconv.thousands_sep = ylconv->thousands_sep;
583 xlconv.grouping = ylconv->grouping; /* FIXME: fixup charmax here too */
584 xlconv.int_curr_symbol = ylconv->int_curr_symbol;
585 xlconv.currency_symbol = ylconv->currency_symbol;
586 xlconv.mon_decimal_point = ylconv->mon_decimal_point;
587 xlconv.mon_thousands_sep = ylconv->mon_thousands_sep;
588 xlconv.mon_grouping = ylconv->mon_grouping;
589 xlconv.positive_sign = ylconv->positive_sign;
590 xlconv.negative_sign = ylconv->negative_sign;
591 xlconv.int_frac_digits = ylconv->int_frac_digits;
592 xlconv.frac_digits = ylconv->frac_digits;
593 xlconv.p_cs_precedes = ylconv->p_cs_precedes;
594 xlconv.p_sep_by_space = ylconv->p_sep_by_space;
595 xlconv.n_cs_precedes = ylconv->n_cs_precedes;
596 xlconv.n_sep_by_space = ylconv->n_sep_by_space;
597 xlconv.p_sign_posn = ylconv->p_sign_posn;
598 xlconv.n_sign_posn = ylconv->n_sign_posn;
600 if (ylconv->int_frac_digits == CHAR_MAX) xlconv.int_frac_digits = charmax;
601 if (ylconv->frac_digits == CHAR_MAX) xlconv.frac_digits = charmax;
602 if (ylconv->p_cs_precedes == CHAR_MAX) xlconv.p_cs_precedes = charmax;
603 if (ylconv->p_sep_by_space == CHAR_MAX) xlconv.p_sep_by_space = charmax;
604 if (ylconv->n_cs_precedes == CHAR_MAX) xlconv.n_cs_precedes = charmax;
605 if (ylconv->n_sep_by_space == CHAR_MAX) xlconv.n_sep_by_space = charmax;
606 if (ylconv->p_sign_posn == CHAR_MAX) xlconv.p_sign_posn = charmax;
607 if (ylconv->n_sign_posn == CHAR_MAX) xlconv.n_sign_posn = charmax;
609 return &xlconv;
612 /*********************************************************************
613 * __lconv_init (MSVCRT.@)
615 void CDECL __lconv_init(void)
617 /* this is used to make chars unsigned */
618 charmax = 255;
621 /*********************************************************************
622 * ___lc_handle_func (MSVCRT.@)
624 HANDLE * CDECL ___lc_handle_func(void)
626 return MSVCRT___lc_handle;
629 /*********************************************************************
630 * ___lc_codepage_func (MSVCRT.@)
632 int CDECL ___lc_codepage_func(void)
634 return MSVCRT___lc_codepage;
637 /*********************************************************************
638 * ___lc_collate_cp_func (MSVCRT.@)
640 int CDECL ___lc_collate_cp_func(void)
642 return MSVCRT___lc_collate_cp;