Const correctness, pointer cast correctness, removed extraneous ';'.
[wine/wine-kai.git] / dlls / kernel / locale.c
blobc6f765bc0587986f0338800c1a7677b6535c197c
1 /*
2 * Locale support
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 David Lee Lambert
6 * Copyright 2000 Julio César Gázquez
7 * Copyright 2002 Alexandre Julliard for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <assert.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <stdlib.h>
34 #include "ntstatus.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winuser.h" /* for RT_STRINGW */
38 #include "winreg.h"
39 #include "winternl.h"
40 #include "wine/unicode.h"
41 #include "winnls.h"
42 #include "winerror.h"
43 #include "winver.h"
44 #include "thread.h"
45 #include "kernel_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(nls);
50 #define LOCALE_LOCALEINFOFLAGSMASK (LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP|LOCALE_RETURN_NUMBER)
52 /* current code pages */
53 static const union cptable *ansi_cptable;
54 static const union cptable *oem_cptable;
55 static const union cptable *mac_cptable;
56 static const union cptable *unix_cptable; /* NULL if UTF8 */
58 /* Charset to codepage map, sorted by name. */
59 static const struct charset_entry
61 const char *charset_name;
62 UINT codepage;
63 } charset_names[] =
65 { "BIG5", 950 },
66 { "CP1250", 1250 },
67 { "CP1251", 1251 },
68 { "CP1252", 1252 },
69 { "CP1253", 1253 },
70 { "CP1254", 1254 },
71 { "CP1255", 1255 },
72 { "CP1256", 1256 },
73 { "CP1257", 1257 },
74 { "CP1258", 1258 },
75 { "CP932", 932 },
76 { "CP936", 936 },
77 { "CP949", 949 },
78 { "CP950", 950 },
79 { "EUCJP", 20932 },
80 { "GB2312", 936 },
81 { "IBM037", 37 },
82 { "IBM1026", 1026 },
83 { "IBM424", 424 },
84 { "IBM437", 437 },
85 { "IBM500", 500 },
86 { "IBM850", 850 },
87 { "IBM852", 852 },
88 { "IBM855", 855 },
89 { "IBM857", 857 },
90 { "IBM860", 860 },
91 { "IBM861", 861 },
92 { "IBM862", 862 },
93 { "IBM863", 863 },
94 { "IBM864", 864 },
95 { "IBM865", 865 },
96 { "IBM866", 866 },
97 { "IBM869", 869 },
98 { "IBM874", 874 },
99 { "IBM875", 875 },
100 { "ISO88591", 28591 },
101 { "ISO885910", 28600 },
102 { "ISO885913", 28603 },
103 { "ISO885914", 28604 },
104 { "ISO885915", 28605 },
105 { "ISO88592", 28592 },
106 { "ISO88593", 28593 },
107 { "ISO88594", 28594 },
108 { "ISO88595", 28595 },
109 { "ISO88596", 28596 },
110 { "ISO88597", 28597 },
111 { "ISO88598", 28598 },
112 { "ISO88599", 28599 },
113 { "KOI8R", 20866 },
114 { "KOI8U", 20866 },
115 { "UTF8", CP_UTF8 }
118 #define NLS_MAX_LANGUAGES 20
119 typedef struct {
120 WCHAR lang[128];
121 WCHAR country[4];
122 LANGID found_lang_id[NLS_MAX_LANGUAGES];
123 WCHAR found_language[NLS_MAX_LANGUAGES][3];
124 WCHAR found_country[NLS_MAX_LANGUAGES][3];
125 int n_found;
126 } LANG_FIND_DATA;
129 /* copy Unicode string to Ascii without using codepages */
130 static inline void strcpyWtoA( char *dst, const WCHAR *src )
132 while ((*dst++ = *src++));
135 /* Copy Ascii string to Unicode without using codepages */
136 static inline void strcpynAtoW( WCHAR *dst, const char *src, size_t n )
138 while (n > 1 && *src)
140 *dst++ = (unsigned char)*src++;
141 n--;
143 if (n) *dst = 0;
146 /* return a printable string for a language id */
147 static const char *debugstr_lang( LANGID lang )
149 WCHAR langW[4], countryW[4];
150 char buffer[8];
151 LCID lcid = MAKELCID( lang, SORT_DEFAULT );
153 GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE, langW, sizeof(langW)/sizeof(WCHAR));
154 GetLocaleInfoW(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE, countryW, sizeof(countryW)/sizeof(WCHAR));
155 strcpyWtoA( buffer, langW );
156 strcat( buffer, "_" );
157 strcpyWtoA( buffer + strlen(buffer), countryW );
158 return wine_dbg_sprintf( "%s", buffer );
161 /***********************************************************************
162 * get_lcid_codepage
164 * Retrieve the ANSI codepage for a given locale.
166 inline static UINT get_lcid_codepage( LCID lcid )
168 UINT ret;
169 if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
170 sizeof(ret)/sizeof(WCHAR) )) ret = 0;
171 return ret;
175 /***********************************************************************
176 * get_codepage_table
178 * Find the table for a given codepage, handling CP_ACP etc. pseudo-codepages
180 static const union cptable *get_codepage_table( unsigned int codepage )
182 const union cptable *ret = NULL;
184 assert( ansi_cptable ); /* init must have been done already */
186 switch(codepage)
188 case CP_ACP:
189 return ansi_cptable;
190 case CP_OEMCP:
191 return oem_cptable;
192 case CP_MACCP:
193 return mac_cptable;
194 case CP_UTF7:
195 case CP_UTF8:
196 break;
197 case CP_THREAD_ACP:
198 if (!(codepage = NtCurrentTeb()->code_page)) return ansi_cptable;
199 /* fall through */
200 default:
201 if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
202 if (codepage == oem_cptable->info.codepage) return oem_cptable;
203 if (codepage == mac_cptable->info.codepage) return mac_cptable;
204 ret = wine_cp_get_table( codepage );
205 break;
207 return ret;
210 /***********************************************************************
211 * create_registry_key
213 * Create the Control Panel\\International registry key.
215 inline static HKEY create_registry_key(void)
217 static const WCHAR intlW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
218 'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
219 OBJECT_ATTRIBUTES attr;
220 UNICODE_STRING nameW;
221 HKEY hkey;
223 if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
225 attr.Length = sizeof(attr);
226 attr.RootDirectory = hkey;
227 attr.ObjectName = &nameW;
228 attr.Attributes = 0;
229 attr.SecurityDescriptor = NULL;
230 attr.SecurityQualityOfService = NULL;
231 RtlInitUnicodeString( &nameW, intlW );
233 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS) hkey = 0;
234 NtClose( attr.RootDirectory );
235 return hkey;
239 /***********************************************************************
240 * LOCALE_InitRegistry
242 * Update registry contents on startup if the user locale has changed.
243 * This simulates the action of the Windows control panel.
245 void LOCALE_InitRegistry(void)
247 static const USHORT updateValues[] = {
248 LOCALE_SLANGUAGE,
249 LOCALE_SCOUNTRY, LOCALE_ICOUNTRY,
250 LOCALE_S1159, LOCALE_S2359,
251 LOCALE_STIME, LOCALE_ITIME,
252 LOCALE_ITLZERO,
253 LOCALE_SSHORTDATE,
254 LOCALE_IDATE,
255 LOCALE_SLONGDATE,
256 LOCALE_SDATE,
257 LOCALE_SCURRENCY, LOCALE_ICURRENCY,
258 LOCALE_INEGCURR,
259 LOCALE_ICURRDIGITS,
260 LOCALE_SDECIMAL,
261 LOCALE_SLIST,
262 LOCALE_STHOUSAND,
263 LOCALE_IDIGITS,
264 LOCALE_IDIGITSUBSTITUTION,
265 LOCALE_SNATIVEDIGITS,
266 LOCALE_ITIMEMARKPOSN,
267 LOCALE_ICALENDARTYPE,
268 LOCALE_ILZERO,
269 LOCALE_IMEASURE
271 static const WCHAR LocaleW[] = {'L','o','c','a','l','e',0};
272 UNICODE_STRING nameW;
273 char buffer[20];
274 WCHAR bufferW[80];
275 DWORD count, i;
276 HKEY hkey;
277 LCID lcid = GetUserDefaultLCID();
279 if (!(hkey = create_registry_key()))
280 return; /* don't do anything if we can't create the registry key */
282 RtlInitUnicodeString( &nameW, LocaleW );
283 count = sizeof(bufferW);
284 if (!NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation, (LPBYTE)bufferW, count, &count))
286 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
287 LPCWSTR szValueText = (LPCWSTR)info->Data;
289 if (strtoulW( szValueText, NULL, 16 ) == lcid) /* already set correctly */
291 NtClose( hkey );
292 return;
294 TRACE( "updating registry, locale changed %s -> %08lx\n", debugstr_w(szValueText), lcid );
296 else TRACE( "updating registry, locale changed none -> %08lx\n", lcid );
298 sprintf( buffer, "%08lx", lcid );
299 /* Note: '9' constant below is strlen(buffer) + 1 */
300 RtlMultiByteToUnicodeN( bufferW, sizeof(bufferW), NULL, buffer, 9 );
301 NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, 9 * sizeof(WCHAR) );
302 NtClose( hkey );
304 for (i = 0; i < sizeof(updateValues)/sizeof(updateValues[0]); i++)
306 GetLocaleInfoW( lcid, updateValues[i] | LOCALE_NOUSEROVERRIDE, bufferW,
307 sizeof(bufferW)/sizeof(WCHAR) );
308 SetLocaleInfoW( lcid, updateValues[i], bufferW );
313 /***********************************************************************
314 * find_language_id_proc
316 static BOOL CALLBACK find_language_id_proc( HMODULE hModule, LPCWSTR type,
317 LPCWSTR name, WORD LangID, LPARAM lParam )
319 LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
320 LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
321 WCHAR buf_language[128];
322 WCHAR buf_country[128];
323 WCHAR buf_en_language[128];
325 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
326 return TRUE; /* continue search */
328 buf_language[0] = 0;
329 buf_country[0] = 0;
331 GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE,
332 buf_language, sizeof(buf_language)/sizeof(WCHAR));
333 GetLocaleInfoW(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
334 buf_country, sizeof(buf_country)/sizeof(WCHAR));
336 if(l_data->lang[0] && !strcmpiW(l_data->lang, buf_language))
338 if(l_data->country[0])
340 if(!strcmpiW(l_data->country, buf_country))
342 l_data->found_lang_id[0] = LangID;
343 l_data->n_found = 1;
344 TRACE("Found id %04X for lang %s country %s\n",
345 LangID, debugstr_w(l_data->lang), debugstr_w(l_data->country));
346 return FALSE; /* stop enumeration */
349 else goto found; /* l_data->country not specified */
352 /* Just in case, check LOCALE_SENGLANGUAGE too,
353 * in hope that possible alias name might have that value.
355 buf_en_language[0] = 0;
356 GetLocaleInfoW(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
357 buf_en_language, sizeof(buf_en_language)/sizeof(WCHAR));
359 if(l_data->lang[0] && !strcmpiW(l_data->lang, buf_en_language)) goto found;
360 return TRUE; /* not found, continue search */
362 found:
363 l_data->found_lang_id[l_data->n_found] = LangID;
364 strncpyW(l_data->found_country[l_data->n_found], buf_country, 3);
365 strncpyW(l_data->found_language[l_data->n_found], buf_language, 3);
366 l_data->n_found++;
367 TRACE("Found id %04X for lang %s\n", LangID, debugstr_w(l_data->lang));
368 return (l_data->n_found < NLS_MAX_LANGUAGES); /* continue search, unless we have enough */
372 /***********************************************************************
373 * get_language_id
375 * INPUT:
376 * Lang: a string whose two first chars are the iso name of a language.
377 * Country: a string whose two first chars are the iso name of country
378 * Charset: a string defining the chosen charset encoding
379 * Dialect: a string defining a variation of the locale
381 * all those values are from the standardized format of locale
382 * name in unix which is: Lang[_Country][.Charset][@Dialect]
384 * RETURNS:
385 * the numeric code of the language used by Windows
387 * FIXME: Charset and Dialect are not handled
389 static LANGID get_language_id(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
391 LANG_FIND_DATA l_data;
393 if(!Lang)
395 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
396 goto END;
399 l_data.n_found = 0;
400 strcpynAtoW(l_data.lang, Lang, sizeof(l_data.lang));
402 if (Country) strcpynAtoW(l_data.country, Country, sizeof(l_data.country));
403 else l_data.country[0] = 0;
405 EnumResourceLanguagesW(kernel32_handle, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
406 find_language_id_proc, (LPARAM)&l_data);
408 if (l_data.n_found == 1) goto END;
410 if(!l_data.n_found)
412 if(l_data.country[0])
414 /* retry without country name */
415 l_data.country[0] = 0;
416 EnumResourceLanguagesW(kernel32_handle, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
417 find_language_id_proc, (LONG)&l_data);
418 if (!l_data.n_found)
420 MESSAGE("Warning: Language '%s_%s' was not recognized, defaulting to English.\n",
421 Lang, Country);
422 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
424 else MESSAGE("Warning: Language '%s_%s' was not recognized, defaulting to '%s'.\n",
425 Lang, Country, debugstr_lang(l_data.found_lang_id[0]) );
427 else
429 MESSAGE("Warning: Language '%s' was not recognized, defaulting to English.\n", Lang);
430 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
433 else
435 int i;
437 if (Country && Country[0])
438 MESSAGE("For language '%s_%s' several language ids were found:\n", Lang, Country);
439 else
440 MESSAGE("For language '%s' several language ids were found:\n", Lang);
442 /* print a list of languages with their description */
443 for (i = 0; i < l_data.n_found; i++)
445 WCHAR buffW[128];
446 char buffA[128];
447 GetLocaleInfoW( MAKELCID( l_data.found_lang_id[i], SORT_DEFAULT ),
448 LOCALE_SLANGUAGE|LOCALE_NOUSEROVERRIDE, buffW, sizeof(buffW)/sizeof(WCHAR));
449 strcpyWtoA( buffA, buffW );
450 MESSAGE( " %s (%04X) - %s\n", debugstr_lang(l_data.found_lang_id[i]),
451 l_data.found_lang_id[i], buffA );
453 MESSAGE("Defaulting to '%s'. You should specify the exact language you want\n"
454 "by defining your LANG environment variable like this: LANG=%s\n",
455 debugstr_lang(l_data.found_lang_id[0]), debugstr_lang(l_data.found_lang_id[0]) );
457 END:
458 TRACE("Returning %04X (%s)\n", l_data.found_lang_id[0], debugstr_lang(l_data.found_lang_id[0]));
459 return l_data.found_lang_id[0];
463 /***********************************************************************
464 * charset_cmp (internal)
466 static int charset_cmp( const void *name, const void *entry )
468 const struct charset_entry *charset = (const struct charset_entry *)entry;
469 return strcasecmp( (const char *)name, charset->charset_name );
472 /***********************************************************************
473 * init_default_lcid
475 static LCID init_default_lcid( UINT *unix_cp )
477 char *buf, *lang,*country,*charset,*dialect,*next;
478 LCID ret = 0;
480 if ((lang = getenv( "LC_ALL" )) ||
481 (lang = getenv( "LANGUAGE" )) ||
482 (lang = getenv( "LANG" )))
484 if (!strcmp(lang,"POSIX") || !strcmp(lang,"C")) goto done;
486 buf = RtlAllocateHeap( GetProcessHeap(), 0, strlen(lang) + 1 );
487 strcpy( buf, lang );
488 lang=buf;
490 do {
491 next=strchr(lang,':'); if (next) *next++='\0';
492 dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
493 charset=strchr(lang,'.'); if (charset) *charset++='\0';
494 country=strchr(lang,'_'); if (country) *country++='\0';
496 ret = get_language_id(lang, country, charset, dialect);
497 if (ret && charset)
499 const struct charset_entry *entry;
500 char charset_name[16];
501 size_t i, j;
503 /* remove punctuation characters from charset name */
504 for (i = j = 0; charset[i] && j < sizeof(charset_name)-1; i++)
505 if (isalnum(charset[i])) charset_name[j++] = charset[i];
506 charset_name[j] = 0;
508 entry = bsearch( charset_name, charset_names,
509 sizeof(charset_names)/sizeof(charset_names[0]),
510 sizeof(charset_names[0]), charset_cmp );
511 if (entry)
513 *unix_cp = entry->codepage;
514 TRACE("charset %s was mapped to cp %u\n", charset, *unix_cp);
516 else
517 FIXME("charset %s was not recognized\n", charset);
520 lang=next;
521 } while (lang && !ret);
523 if (!ret) MESSAGE("Warning: language '%s' not recognized, defaulting to English\n", buf);
524 RtlFreeHeap( GetProcessHeap(), 0, buf );
527 done:
528 if (!ret) ret = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT) ;
529 return ret;
533 /***********************************************************************
534 * GetUserDefaultLangID (KERNEL32.@)
536 * Get the default language Id for the current user.
538 * PARAMS
539 * None.
541 * RETURNS
542 * The current LANGID of the default language for the current user.
544 LANGID WINAPI GetUserDefaultLangID(void)
546 return LANGIDFROMLCID(GetUserDefaultLCID());
550 /***********************************************************************
551 * GetSystemDefaultLangID (KERNEL32.@)
553 * Get the default language Id for the system.
555 * PARAMS
556 * None.
558 * RETURNS
559 * The current LANGID of the default language for the system.
561 LANGID WINAPI GetSystemDefaultLangID(void)
563 return GetUserDefaultLangID();
567 /***********************************************************************
568 * GetUserDefaultLCID (KERNEL32.@)
570 * Get the default locale Id for the current user.
572 * PARAMS
573 * None.
575 * RETURNS
576 * The current LCID of the default locale for the current user.
578 LCID WINAPI GetUserDefaultLCID(void)
580 LCID lcid;
581 NtQueryDefaultLocale( TRUE, &lcid );
582 return lcid;
586 /***********************************************************************
587 * GetSystemDefaultLCID (KERNEL32.@)
589 * Get the default locale Id for the system.
591 * PARAMS
592 * None.
594 * RETURNS
595 * The current LCID of the default locale for the system.
597 LCID WINAPI GetSystemDefaultLCID(void)
599 LCID lcid;
600 NtQueryDefaultLocale( FALSE, &lcid );
601 return lcid;
605 /***********************************************************************
606 * GetUserDefaultUILanguage (KERNEL32.@)
608 * Get the default user interface language Id for the current user.
610 * PARAMS
611 * None.
613 * RETURNS
614 * The current LANGID of the default UI language for the current user.
616 LANGID WINAPI GetUserDefaultUILanguage(void)
618 return GetUserDefaultLangID();
622 /***********************************************************************
623 * GetSystemDefaultUILanguage (KERNEL32.@)
625 * Get the default user interface language Id for the system.
627 * PARAMS
628 * None.
630 * RETURNS
631 * The current LANGID of the default UI language for the system. This is
632 * typically the same language used during the installation process.
634 LANGID WINAPI GetSystemDefaultUILanguage(void)
636 return GetSystemDefaultLangID();
640 /******************************************************************************
641 * get_locale_value_name
643 * Gets the registry value name for a given lctype.
645 static const WCHAR *get_locale_value_name( DWORD lctype )
647 static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
648 static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
649 static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
650 static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
651 static const WCHAR iDateW[] = {'i','D','a','t','e',0};
652 static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
653 static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
654 static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
655 static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
656 static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
657 static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
658 static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
659 static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
660 static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
661 static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
662 static const WCHAR iTimePrefixW[] = {'i','T','i','m','e','P','r','e','f','i','x',0};
663 static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
664 static const WCHAR s1159W[] = {'s','1','1','5','9',0};
665 static const WCHAR s2359W[] = {'s','2','3','5','9',0};
666 static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
667 static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
668 static const WCHAR sDateW[] = {'s','D','a','t','e',0};
669 static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
670 static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
671 static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
672 static const WCHAR sListW[] = {'s','L','i','s','t',0};
673 static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
674 static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
675 static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
676 static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
677 static const WCHAR sNativeDigitsW[] = {'s','N','a','t','i','v','e','D','i','g','i','t','s',0};
678 static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
679 static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
680 static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
681 static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
682 static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
683 static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
684 static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
685 static const WCHAR NumShapeW[] = {'N','u','m','s','h','a','p','e',0};
687 switch (lctype)
689 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
690 * the values are stored in the registry, confirmed under Windows.
692 case LOCALE_ICALENDARTYPE: return iCalendarTypeW;
693 case LOCALE_ICURRDIGITS: return iCurrDigitsW;
694 case LOCALE_ICURRENCY: return iCurrencyW;
695 case LOCALE_IDIGITS: return iDigitsW;
696 case LOCALE_IFIRSTDAYOFWEEK: return iFirstDayOfWeekW;
697 case LOCALE_IFIRSTWEEKOFYEAR: return iFirstWeekOfYearW;
698 case LOCALE_ILZERO: return iLZeroW;
699 case LOCALE_IMEASURE: return iMeasureW;
700 case LOCALE_INEGCURR: return iNegCurrW;
701 case LOCALE_INEGNUMBER: return iNegNumberW;
702 case LOCALE_IPAPERSIZE: return iPaperSizeW;
703 case LOCALE_ITIME: return iTimeW;
704 case LOCALE_S1159: return s1159W;
705 case LOCALE_S2359: return s2359W;
706 case LOCALE_SCURRENCY: return sCurrencyW;
707 case LOCALE_SDATE: return sDateW;
708 case LOCALE_SDECIMAL: return sDecimalW;
709 case LOCALE_SGROUPING: return sGroupingW;
710 case LOCALE_SLIST: return sListW;
711 case LOCALE_SLONGDATE: return sLongDateW;
712 case LOCALE_SMONDECIMALSEP: return sMonDecimalSepW;
713 case LOCALE_SMONGROUPING: return sMonGroupingW;
714 case LOCALE_SMONTHOUSANDSEP: return sMonThousandSepW;
715 case LOCALE_SNEGATIVESIGN: return sNegativeSignW;
716 case LOCALE_SPOSITIVESIGN: return sPositiveSignW;
717 case LOCALE_SSHORTDATE: return sShortDateW;
718 case LOCALE_STHOUSAND: return sThousandW;
719 case LOCALE_STIME: return sTimeW;
720 case LOCALE_STIMEFORMAT: return sTimeFormatW;
721 case LOCALE_SYEARMONTH: return sYearMonthW;
723 /* The following are not listed under MSDN as supported,
724 * but seem to be used and also stored in the registry.
726 case LOCALE_ICOUNTRY: return iCountryW;
727 case LOCALE_IDATE: return iDateW;
728 case LOCALE_ILDATE: return iLDateW;
729 case LOCALE_ITLZERO: return iTLZeroW;
730 case LOCALE_SCOUNTRY: return sCountryW;
731 case LOCALE_SLANGUAGE: return sLanguageW;
733 /* The following are used in XP and later */
734 case LOCALE_IDIGITSUBSTITUTION: return NumShapeW;
735 case LOCALE_SNATIVEDIGITS: return sNativeDigitsW;
736 case LOCALE_ITIMEMARKPOSN: return iTimePrefixW;
738 return NULL;
742 /******************************************************************************
743 * get_registry_locale_info
745 * Retrieve user-modified locale info from the registry.
746 * Return length, 0 on error, -1 if not found.
748 static INT get_registry_locale_info( LPCWSTR value, LPWSTR buffer, INT len )
750 DWORD size;
751 INT ret;
752 HKEY hkey;
753 NTSTATUS status;
754 UNICODE_STRING nameW;
755 KEY_VALUE_PARTIAL_INFORMATION *info;
756 static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
758 if (!(hkey = create_registry_key())) return -1;
760 RtlInitUnicodeString( &nameW, value );
761 size = info_size + len * sizeof(WCHAR);
763 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
765 NtClose( hkey );
766 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
767 return 0;
770 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
771 if (status == STATUS_BUFFER_OVERFLOW && !buffer) status = 0;
773 if (!status)
775 ret = (size - info_size) / sizeof(WCHAR);
776 /* append terminating null if needed */
777 if (!ret || ((WCHAR *)info->Data)[ret-1])
779 if (ret < len || !buffer) ret++;
780 else
782 SetLastError( ERROR_INSUFFICIENT_BUFFER );
783 ret = 0;
786 if (ret && buffer)
788 memcpy( buffer, info->Data, (ret-1) * sizeof(WCHAR) );
789 buffer[ret-1] = 0;
792 else
794 if (status == STATUS_OBJECT_NAME_NOT_FOUND) ret = -1;
795 else
797 SetLastError( RtlNtStatusToDosError(status) );
798 ret = 0;
801 NtClose( hkey );
802 HeapFree( GetProcessHeap(), 0, info );
803 return ret;
807 /******************************************************************************
808 * GetLocaleInfoA (KERNEL32.@)
810 * Get information about an aspect of a locale.
812 * PARAMS
813 * lcid [I] LCID of the locale
814 * lctype [I] LCTYPE_ flags from "winnls.h"
815 * buffer [O] Destination for the information
816 * len [I] Length of buffer in characters
818 * RETURNS
819 * Success: The size of the data requested. If buffer is non-NULL, it is filled
820 * with the information.
821 * Failure: 0. Use GetLastError() to determine the cause.
823 * NOTES
824 * - LOCALE_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
825 * - The string returned is NUL terminated, except for LOCALE_FONTSIGNATURE,
826 * which is a bit string.
828 INT WINAPI GetLocaleInfoA( LCID lcid, LCTYPE lctype, LPSTR buffer, INT len )
830 WCHAR *bufferW;
831 INT lenW, ret;
833 if (len < 0 || (len && !buffer))
835 SetLastError( ERROR_INVALID_PARAMETER );
836 return 0;
838 if (!len) buffer = NULL;
840 if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
842 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
844 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
845 return 0;
847 if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
849 if ((lctype & LOCALE_RETURN_NUMBER) ||
850 ((lctype & ~LOCALE_LOCALEINFOFLAGSMASK) == LOCALE_FONTSIGNATURE))
852 /* it's not an ASCII string, just bytes */
853 ret *= sizeof(WCHAR);
854 if (buffer)
856 if (ret <= len) memcpy( buffer, bufferW, ret );
857 else
859 SetLastError( ERROR_INSUFFICIENT_BUFFER );
860 ret = 0;
864 else
866 UINT codepage = CP_ACP;
867 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
868 ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
871 HeapFree( GetProcessHeap(), 0, bufferW );
872 return ret;
876 /******************************************************************************
877 * GetLocaleInfoW (KERNEL32.@)
879 * See GetLocaleInfoA.
881 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
883 LANGID lang_id;
884 HRSRC hrsrc;
885 HGLOBAL hmem;
886 INT ret;
887 UINT lcflags;
888 const WCHAR *p;
889 unsigned int i;
891 if (len < 0 || (len && !buffer))
893 SetLastError( ERROR_INVALID_PARAMETER );
894 return 0;
896 if (!len) buffer = NULL;
898 lcid = ConvertDefaultLocale(lcid);
900 lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
901 lctype &= 0xffff;
903 /* first check for overrides in the registry */
905 if (!(lcflags & LOCALE_NOUSEROVERRIDE) && lcid == GetUserDefaultLCID())
907 const WCHAR *value = get_locale_value_name(lctype);
909 if (value)
911 if (lcflags & LOCALE_RETURN_NUMBER)
913 WCHAR tmp[16];
914 ret = get_registry_locale_info( value, tmp, sizeof(tmp)/sizeof(WCHAR) );
915 if (ret > 0)
917 WCHAR *end;
918 UINT number = strtolW( tmp, &end, 10 );
919 if (*end) /* invalid number */
921 SetLastError( ERROR_INVALID_FLAGS );
922 return 0;
924 ret = sizeof(UINT)/sizeof(WCHAR);
925 if (!buffer) return ret;
926 if (ret > len)
928 SetLastError( ERROR_INSUFFICIENT_BUFFER );
929 return 0;
931 memcpy( buffer, &number, sizeof(number) );
934 else ret = get_registry_locale_info( value, buffer, len );
936 if (ret != -1) return ret;
940 /* now load it from kernel resources */
942 lang_id = LANGIDFROMLCID( lcid );
944 /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
945 if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
946 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
948 if (!(hrsrc = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
949 (LPCWSTR)((lctype >> 4) + 1), lang_id )))
951 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
952 return 0;
954 if (!(hmem = LoadResource( kernel32_handle, hrsrc )))
955 return 0;
957 p = LockResource( hmem );
958 for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
960 if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
961 else ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
963 if (!buffer) return ret;
965 if (ret > len)
967 SetLastError( ERROR_INSUFFICIENT_BUFFER );
968 return 0;
971 if (lcflags & LOCALE_RETURN_NUMBER)
973 UINT number;
974 WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
975 if (!tmp) return 0;
976 memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
977 tmp[*p] = 0;
978 number = strtolW( tmp, &end, 10 );
979 if (!*end)
980 memcpy( buffer, &number, sizeof(number) );
981 else /* invalid number */
983 SetLastError( ERROR_INVALID_FLAGS );
984 ret = 0;
986 HeapFree( GetProcessHeap(), 0, tmp );
988 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning number %d\n",
989 lcid, lctype, buffer, len, number );
991 else
993 memcpy( buffer, p + 1, *p * sizeof(WCHAR) );
994 if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
996 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning %d %s\n",
997 lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
999 return ret;
1003 /******************************************************************************
1004 * SetLocaleInfoA [KERNEL32.@]
1006 * Set information about an aspect of a locale.
1008 * PARAMS
1009 * lcid [I] LCID of the locale
1010 * lctype [I] LCTYPE_ flags from "winnls.h"
1011 * data [I] Information to set
1013 * RETURNS
1014 * Success: TRUE. The information given will be returned by GetLocaleInfoA()
1015 * whenever it is called without LOCALE_NOUSEROVERRIDE.
1016 * Failure: FALSE. Use GetLastError() to determine the cause.
1018 * NOTES
1019 * - Values are only be set for the current user locale; the system locale
1020 * settings cannot be changed.
1021 * - Any settings changed by this call are lost when the locale is changed by
1022 * the control panel (in Wine, this happens every time you change LANG).
1023 * - The native implementation of this function does not check that lcid matches
1024 * the current user locale, and simply sets the new values. Wine warns you in
1025 * this case, but behaves the same.
1027 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
1029 UINT codepage = CP_ACP;
1030 WCHAR *strW;
1031 DWORD len;
1032 BOOL ret;
1034 lcid = ConvertDefaultLocale(lcid);
1036 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
1038 if (!data)
1040 SetLastError( ERROR_INVALID_PARAMETER );
1041 return FALSE;
1043 len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
1044 if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1046 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1047 return FALSE;
1049 MultiByteToWideChar( codepage, 0, data, -1, strW, len );
1050 ret = SetLocaleInfoW( lcid, lctype, strW );
1051 HeapFree( GetProcessHeap(), 0, strW );
1052 return ret;
1056 /******************************************************************************
1057 * SetLocaleInfoW (KERNEL32.@)
1059 * See SetLocaleInfoA.
1061 BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
1063 const WCHAR *value;
1064 static const WCHAR intlW[] = {'i','n','t','l',0 };
1065 UNICODE_STRING valueW;
1066 NTSTATUS status;
1067 HKEY hkey;
1069 lcid = ConvertDefaultLocale(lcid);
1071 lctype &= 0xffff;
1072 value = get_locale_value_name( lctype );
1074 if (!data || !value)
1076 SetLastError( ERROR_INVALID_PARAMETER );
1077 return FALSE;
1080 if (lctype == LOCALE_IDATE || lctype == LOCALE_ILDATE)
1082 SetLastError( ERROR_INVALID_FLAGS );
1083 return FALSE;
1086 if (lcid != GetUserDefaultLCID())
1088 /* Windows does not check that the lcid matches the current lcid */
1089 WARN("locale 0x%08lx isn't the current locale (0x%08lx), setting anyway!\n",
1090 lcid, GetUserDefaultLCID());
1093 TRACE("setting %lx to %s\n", lctype, debugstr_w(data) );
1095 /* FIXME: should check that data to set is sane */
1097 /* FIXME: profile functions should map to registry */
1098 WriteProfileStringW( intlW, value, data );
1100 if (!(hkey = create_registry_key())) return FALSE;
1101 RtlInitUnicodeString( &valueW, value );
1102 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
1104 if (lctype == LOCALE_SDATE || lctype == LOCALE_SLONGDATE)
1106 /* Set I-value from S value */
1107 WCHAR *lpD, *lpM, *lpY;
1108 WCHAR szBuff[2];
1110 lpD = strchrW(data, 'd');
1111 lpM = strchrW(data, 'M');
1112 lpY = strchrW(data, 'y');
1114 if (lpD <= lpM)
1116 szBuff[0] = '1'; /* D-M-Y */
1118 else
1120 if (lpY <= lpM)
1121 szBuff[0] = '2'; /* Y-M-D */
1122 else
1123 szBuff[0] = '0'; /* M-D-Y */
1126 szBuff[1] = '\0';
1128 if (lctype == LOCALE_SDATE)
1129 lctype = LOCALE_IDATE;
1130 else
1131 lctype = LOCALE_ILDATE;
1133 value = get_locale_value_name( lctype );
1135 WriteProfileStringW( intlW, value, szBuff );
1137 RtlInitUnicodeString( &valueW, value );
1138 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, szBuff, sizeof(szBuff) );
1141 NtClose( hkey );
1143 if (status) SetLastError( RtlNtStatusToDosError(status) );
1144 return !status;
1148 /******************************************************************************
1149 * GetACP (KERNEL32.@)
1151 * Get the current Ansi code page Id for the system.
1153 * PARAMS
1154 * None.
1156 * RETURNS
1157 * The current Ansi code page identifier for the system.
1159 UINT WINAPI GetACP(void)
1161 assert( ansi_cptable );
1162 return ansi_cptable->info.codepage;
1166 /***********************************************************************
1167 * GetOEMCP (KERNEL32.@)
1169 * Get the current OEM code page Id for the system.
1171 * PARAMS
1172 * None.
1174 * RETURNS
1175 * The current OEM code page identifier for the system.
1177 UINT WINAPI GetOEMCP(void)
1179 assert( oem_cptable );
1180 return oem_cptable->info.codepage;
1184 /***********************************************************************
1185 * IsValidCodePage (KERNEL32.@)
1187 * Determine if a given code page identifier is valid.
1189 * PARAMS
1190 * codepage [I] Code page Id to verify.
1192 * RETURNS
1193 * TRUE, If codepage is valid and available on the system,
1194 * FALSE otherwise.
1196 BOOL WINAPI IsValidCodePage( UINT codepage )
1198 switch(codepage) {
1199 case CP_UTF7:
1200 case CP_UTF8:
1201 return TRUE;
1202 default:
1203 return wine_cp_get_table( codepage ) != NULL;
1208 /***********************************************************************
1209 * IsDBCSLeadByteEx (KERNEL32.@)
1211 * Determine if a character is a lead byte in a given code page.
1213 * PARAMS
1214 * codepage [I] Code page for the test.
1215 * testchar [I] Character to test
1217 * RETURNS
1218 * TRUE, if testchar is a lead byte in codepage,
1219 * FALSE otherwise.
1221 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
1223 const union cptable *table = get_codepage_table( codepage );
1224 return table && is_dbcs_leadbyte( table, testchar );
1228 /***********************************************************************
1229 * IsDBCSLeadByte (KERNEL32.@)
1230 * IsDBCSLeadByte (KERNEL.207)
1232 * Determine if a character is a lead byte.
1234 * PARAMS
1235 * testchar [I] Character to test
1237 * RETURNS
1238 * TRUE, if testchar is a lead byte in the Ansii code page,
1239 * FALSE otherwise.
1241 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
1243 if (!ansi_cptable) return FALSE;
1244 return is_dbcs_leadbyte( ansi_cptable, testchar );
1248 /***********************************************************************
1249 * GetCPInfo (KERNEL32.@)
1251 * Get information about a code page.
1253 * PARAMS
1254 * codepage [I] Code page number
1255 * cpinfo [O] Destination for code page information
1257 * RETURNS
1258 * Success: TRUE. cpinfo is updated with the information about codepage.
1259 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1261 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
1263 const union cptable *table = get_codepage_table( codepage );
1265 if (!table)
1267 SetLastError( ERROR_INVALID_PARAMETER );
1268 return FALSE;
1270 if (table->info.def_char & 0xff00)
1272 cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
1273 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
1275 else
1277 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
1278 cpinfo->DefaultChar[1] = 0;
1280 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
1281 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
1282 else
1283 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1285 return TRUE;
1288 /***********************************************************************
1289 * GetCPInfoExA (KERNEL32.@)
1291 * Get extended information about a code page.
1293 * PARAMS
1294 * codepage [I] Code page number
1295 * dwFlags [I] Reserved, must to 0.
1296 * cpinfo [O] Destination for code page information
1298 * RETURNS
1299 * Success: TRUE. cpinfo is updated with the information about codepage.
1300 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1302 BOOL WINAPI GetCPInfoExA( UINT codepage, DWORD dwFlags, LPCPINFOEXA cpinfo )
1304 const union cptable *table = get_codepage_table( codepage );
1306 if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
1307 return FALSE;
1309 cpinfo->CodePage = codepage;
1310 cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
1311 strcpy(cpinfo->CodePageName, table->info.name);
1312 return TRUE;
1315 /***********************************************************************
1316 * GetCPInfoExW (KERNEL32.@)
1318 * Unicode version of GetCPInfoExA.
1320 BOOL WINAPI GetCPInfoExW( UINT codepage, DWORD dwFlags, LPCPINFOEXW cpinfo )
1322 const union cptable *table = get_codepage_table( codepage );
1324 if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
1325 return FALSE;
1327 cpinfo->CodePage = codepage;
1328 cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
1329 MultiByteToWideChar( CP_ACP, 0, table->info.name, -1, cpinfo->CodePageName,
1330 sizeof(cpinfo->CodePageName)/sizeof(WCHAR));
1331 return TRUE;
1334 /***********************************************************************
1335 * EnumSystemCodePagesA (KERNEL32.@)
1337 * Call a user defined function for every code page installed on the system.
1339 * PARAMS
1340 * lpfnCodePageEnum [I] User CODEPAGE_ENUMPROC to call with each found code page
1341 * flags [I] Reserved, set to 0.
1343 * RETURNS
1344 * TRUE, If all code pages have been enumerated, or
1345 * FALSE if lpfnCodePageEnum returned FALSE to stop the enumeration.
1347 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
1349 const union cptable *table;
1350 char buffer[10];
1351 int index = 0;
1353 for (;;)
1355 if (!(table = wine_cp_enum_table( index++ ))) break;
1356 sprintf( buffer, "%d", table->info.codepage );
1357 if (!lpfnCodePageEnum( buffer )) break;
1359 return TRUE;
1363 /***********************************************************************
1364 * EnumSystemCodePagesW (KERNEL32.@)
1366 * See EnumSystemCodePagesA.
1368 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
1370 const union cptable *table;
1371 WCHAR buffer[10], *p;
1372 int page, index = 0;
1374 for (;;)
1376 if (!(table = wine_cp_enum_table( index++ ))) break;
1377 p = buffer + sizeof(buffer)/sizeof(WCHAR);
1378 *--p = 0;
1379 page = table->info.codepage;
1382 *--p = '0' + (page % 10);
1383 page /= 10;
1384 } while( page );
1385 if (!lpfnCodePageEnum( p )) break;
1387 return TRUE;
1391 /***********************************************************************
1392 * MultiByteToWideChar (KERNEL32.@)
1394 * Convert a multibyte character string into a Unicode string.
1396 * PARAMS
1397 * page [I] Codepage character set to convert from
1398 * flags [I] Character mapping flags
1399 * src [I] Source string buffer
1400 * srclen [I] Length of src, or -1 if src is NUL terminated
1401 * dst [O] Destination buffer
1402 * dstlen [I] Length of dst, or 0 to compute the required length
1404 * RETURNS
1405 * Success: If dstlen > 0, the number of characters written to dst.
1406 * If dstlen == 0, the number of characters needed to perform the
1407 * conversion. In both cases the count includes the terminating NUL.
1408 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1409 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1410 * and dstlen != 0; ERROR_INVALID_PARAMETER, if an invalid parameter
1411 * is passed, and ERROR_NO_UNICODE_TRANSLATION if no translation is
1412 * possible for src.
1414 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
1415 LPWSTR dst, INT dstlen )
1417 const union cptable *table;
1418 int ret;
1420 if (!src || (!dst && dstlen))
1422 SetLastError( ERROR_INVALID_PARAMETER );
1423 return 0;
1426 if (srclen < 0) srclen = strlen(src) + 1;
1428 if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
1430 switch(page)
1432 case CP_SYMBOL:
1433 if( flags)
1435 SetLastError( ERROR_INVALID_PARAMETER );
1436 return 0;
1438 ret = wine_cpsymbol_mbstowcs( src, srclen, dst, dstlen );
1439 break;
1440 case CP_UTF7:
1441 FIXME("UTF-7 not supported\n");
1442 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1443 return 0;
1444 case CP_UNIXCP:
1445 if (unix_cptable)
1447 ret = wine_cp_mbstowcs( unix_cptable, flags, src, srclen, dst, dstlen );
1448 break;
1450 /* fall through */
1451 case CP_UTF8:
1452 ret = wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
1453 break;
1454 default:
1455 if (!(table = get_codepage_table( page )))
1457 SetLastError( ERROR_INVALID_PARAMETER );
1458 return 0;
1460 ret = wine_cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
1461 break;
1464 if (ret < 0)
1466 switch(ret)
1468 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1469 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1471 ret = 0;
1473 return ret;
1477 /***********************************************************************
1478 * WideCharToMultiByte (KERNEL32.@)
1480 * Convert a Unicode character string into a multibyte string.
1482 * PARAMS
1483 * page [I] Code page character set to convert to
1484 * flags [I] Mapping Flags (MB_ constants from "winnls.h").
1485 * src [I] Source string buffer
1486 * srclen [I] Length of src, or -1 if src is NUL terminated
1487 * dst [O] Destination buffer
1488 * dstlen [I] Length of dst, or 0 to compute the required length
1489 * defchar [I] Default character to use for conversion if no exact
1490 * conversion can be made
1491 * used [O] Set if default character was used in the conversion
1493 * RETURNS
1494 * Success: If dstlen > 0, the number of characters written to dst.
1495 * If dstlen == 0, number of characters needed to perform the
1496 * conversion. In both cases the count includes the terminating NUL.
1497 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1498 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1499 * and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
1500 * parameter was given.
1502 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
1503 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
1505 const union cptable *table;
1506 int ret, used_tmp;
1508 if (!src || (!dst && dstlen))
1510 SetLastError( ERROR_INVALID_PARAMETER );
1511 return 0;
1514 if (srclen < 0) srclen = strlenW(src) + 1;
1516 switch(page)
1518 case CP_SYMBOL:
1519 if( flags || defchar || used)
1521 SetLastError( ERROR_INVALID_PARAMETER );
1522 return 0;
1524 ret = wine_cpsymbol_wcstombs( src, srclen, dst, dstlen );
1525 break;
1526 case CP_UTF7:
1527 FIXME("UTF-7 not supported\n");
1528 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1529 return 0;
1530 case CP_UNIXCP:
1531 if (unix_cptable)
1533 ret = wine_cp_wcstombs( unix_cptable, flags, src, srclen, dst, dstlen,
1534 defchar, used ? &used_tmp : NULL );
1535 break;
1537 /* fall through */
1538 case CP_UTF8:
1539 if (used) *used = FALSE; /* all chars are valid for UTF-8 */
1540 ret = wine_utf8_wcstombs( src, srclen, dst, dstlen );
1541 break;
1542 default:
1543 if (!(table = get_codepage_table( page )))
1545 SetLastError( ERROR_INVALID_PARAMETER );
1546 return 0;
1548 ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
1549 defchar, used ? &used_tmp : NULL );
1550 if (used) *used = used_tmp;
1551 break;
1554 if (ret < 0)
1556 switch(ret)
1558 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1559 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1561 ret = 0;
1563 TRACE("cp %d %s -> %s\n", page, debugstr_w(src), debugstr_a(dst));
1564 return ret;
1568 /***********************************************************************
1569 * GetThreadLocale (KERNEL32.@)
1571 * Get the current threads locale.
1573 * PARAMS
1574 * None.
1576 * RETURNS
1577 * The LCID currently assocated with the calling thread.
1579 LCID WINAPI GetThreadLocale(void)
1581 LCID ret = NtCurrentTeb()->CurrentLocale;
1582 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
1583 return ret;
1586 /**********************************************************************
1587 * SetThreadLocale (KERNEL32.@)
1589 * Set the current threads locale.
1591 * PARAMS
1592 * lcid [I] LCID of the locale to set
1594 * RETURNS
1595 * Success: TRUE. The threads locale is set to lcid.
1596 * Failure: FALSE. Use GetLastError() to determine the cause.
1598 BOOL WINAPI SetThreadLocale( LCID lcid )
1600 TRACE("(0x%04lX)\n", lcid);
1602 lcid = ConvertDefaultLocale(lcid);
1604 if (lcid != GetThreadLocale())
1606 if (!IsValidLocale(lcid, LCID_SUPPORTED))
1608 SetLastError(ERROR_INVALID_PARAMETER);
1609 return FALSE;
1612 NtCurrentTeb()->CurrentLocale = lcid;
1613 NtCurrentTeb()->code_page = get_lcid_codepage( lcid );
1615 return TRUE;
1618 /******************************************************************************
1619 * ConvertDefaultLocale (KERNEL32.@)
1621 * Convert a default locale identifier into a real identifier.
1623 * PARAMS
1624 * lcid [I] LCID identifier of the locale to convert
1626 * RETURNS
1627 * lcid unchanged, if not a default locale or its sublanguage is
1628 * not SUBLANG_NEUTRAL.
1629 * GetSystemDefaultLCID(), if lcid == LOCALE_SYSTEM_DEFAULT.
1630 * GetUserDefaultLCID(), if lcid == LOCALE_USER_DEFAULT or LOCALE_NEUTRAL.
1631 * Otherwise, lcid with sublanguage changed to SUBLANG_DEFAULT.
1633 LCID WINAPI ConvertDefaultLocale( LCID lcid )
1635 LANGID langid;
1637 switch (lcid)
1639 case LOCALE_SYSTEM_DEFAULT:
1640 lcid = GetSystemDefaultLCID();
1641 break;
1642 case LOCALE_USER_DEFAULT:
1643 case LOCALE_NEUTRAL:
1644 lcid = GetUserDefaultLCID();
1645 break;
1646 default:
1647 /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
1648 langid = LANGIDFROMLCID(lcid);
1649 if (SUBLANGID(langid) == SUBLANG_NEUTRAL)
1651 langid = MAKELANGID(PRIMARYLANGID(langid), SUBLANG_DEFAULT);
1652 lcid = MAKELCID(langid, SORTIDFROMLCID(lcid));
1655 return lcid;
1659 /******************************************************************************
1660 * IsValidLocale (KERNEL32.@)
1662 * Determine if a locale is valid.
1664 * PARAMS
1665 * lcid [I] LCID of the locale to check
1666 * flags [I] LCID_SUPPORTED = Valid, LCID_INSTALLED = Valid and installed on the system
1668 * RETURN
1669 * TRUE, if lcid is valid,
1670 * FALSE, otherwise.
1672 * NOTES
1673 * Wine does not currently make the distinction between supported and installed. All
1674 * languages supported are installed by default.
1676 BOOL WINAPI IsValidLocale( LCID lcid, DWORD flags )
1678 /* check if language is registered in the kernel32 resources */
1679 return FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
1680 (LPCWSTR)LOCALE_ILANGUAGE, LANGIDFROMLCID(lcid)) != 0;
1684 static BOOL CALLBACK enum_lang_proc_a( HMODULE hModule, LPCSTR type,
1685 LPCSTR name, WORD LangID, LONG lParam )
1687 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
1688 char buf[20];
1690 sprintf(buf, "%08x", (UINT)LangID);
1691 return lpfnLocaleEnum( buf );
1694 static BOOL CALLBACK enum_lang_proc_w( HMODULE hModule, LPCWSTR type,
1695 LPCWSTR name, WORD LangID, LONG lParam )
1697 static const WCHAR formatW[] = {'%','0','8','x',0};
1698 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
1699 WCHAR buf[20];
1700 sprintfW( buf, formatW, (UINT)LangID );
1701 return lpfnLocaleEnum( buf );
1704 /******************************************************************************
1705 * EnumSystemLocalesA (KERNEL32.@)
1707 * Call a users function for each locale available on the system.
1709 * PARAMS
1710 * lpfnLocaleEnum [I] Callback function to call for each locale
1711 * dwFlags [I] LOCALE_SUPPORTED=All supported, LOCALE_INSTALLED=Installed only
1713 * RETURNS
1714 * Success: TRUE.
1715 * Failure: FALSE. Use GetLastError() to determine the cause.
1717 BOOL WINAPI EnumSystemLocalesA( LOCALE_ENUMPROCA lpfnLocaleEnum, DWORD dwFlags )
1719 TRACE("(%p,%08lx)\n", lpfnLocaleEnum, dwFlags);
1720 EnumResourceLanguagesA( kernel32_handle, (LPSTR)RT_STRING,
1721 (LPCSTR)LOCALE_ILANGUAGE, enum_lang_proc_a,
1722 (LONG)lpfnLocaleEnum);
1723 return TRUE;
1727 /******************************************************************************
1728 * EnumSystemLocalesW (KERNEL32.@)
1730 * See EnumSystemLocalesA.
1732 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum, DWORD dwFlags )
1734 TRACE("(%p,%08lx)\n", lpfnLocaleEnum, dwFlags);
1735 EnumResourceLanguagesW( kernel32_handle, (LPWSTR)RT_STRING,
1736 (LPCWSTR)LOCALE_ILANGUAGE, enum_lang_proc_w,
1737 (LONG)lpfnLocaleEnum);
1738 return TRUE;
1742 /***********************************************************************
1743 * VerLanguageNameA (KERNEL32.@)
1745 * Get the name of a language.
1747 * PARAMS
1748 * wLang [I] LANGID of the language
1749 * szLang [O] Destination for the language name
1751 * RETURNS
1752 * Success: The size of the language name. If szLang is non-NULL, it is filled
1753 * with the name.
1754 * Failure: 0. Use GetLastError() to determine the cause.
1757 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
1759 return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
1763 /***********************************************************************
1764 * VerLanguageNameW (KERNEL32.@)
1766 * See VerLanguageNameA.
1768 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
1770 return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
1774 /******************************************************************************
1775 * GetStringTypeW (KERNEL32.@)
1777 * See GetStringTypeA.
1779 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1781 if (count == -1) count = strlenW(src) + 1;
1782 switch(type)
1784 case CT_CTYPE1:
1785 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
1786 break;
1787 case CT_CTYPE2:
1788 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
1789 break;
1790 case CT_CTYPE3:
1792 WARN("CT_CTYPE3: semi-stub.\n");
1793 while (count--)
1795 int c = *src;
1796 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
1798 type1 = get_char_typeW( *src++ ) & 0xfff;
1799 /* try to construct type3 from type1 */
1800 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
1801 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
1802 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
1803 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
1804 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
1805 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
1806 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
1808 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
1809 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
1810 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
1811 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
1812 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
1813 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
1814 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
1815 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
1817 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
1818 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
1819 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
1820 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
1821 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
1822 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
1823 *chartype++ = type3;
1825 break;
1827 default:
1828 SetLastError( ERROR_INVALID_PARAMETER );
1829 return FALSE;
1831 return TRUE;
1835 /******************************************************************************
1836 * GetStringTypeExW (KERNEL32.@)
1838 * See GetStringTypeExA.
1840 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1842 /* locale is ignored for Unicode */
1843 return GetStringTypeW( type, src, count, chartype );
1847 /******************************************************************************
1848 * GetStringTypeA (KERNEL32.@)
1850 * Get characteristics of the characters making up a string.
1852 * PARAMS
1853 * locale [I] Locale Id for the string
1854 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
1855 * src [I] String to analyse
1856 * count [I] Length of src in chars, or -1 if src is NUL terminated
1857 * chartype [O] Destination for the calculated characteristics
1859 * RETURNS
1860 * Success: TRUE. chartype is filled with the requested characteristics of each char
1861 * in src.
1862 * Failure: FALSE. Use GetLastError() to determine the cause.
1864 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1866 UINT cp;
1867 INT countW;
1868 LPWSTR srcW;
1869 BOOL ret = FALSE;
1871 if(count == -1) count = strlen(src) + 1;
1873 if (!(cp = get_lcid_codepage( locale )))
1875 FIXME("For locale %04lx using current ANSI code page\n", locale);
1876 cp = GetACP();
1879 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
1880 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
1882 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
1884 * NOTE: the target buffer has 1 word for each CHARACTER in the source
1885 * string, with multibyte characters there maybe be more bytes in count
1886 * than character space in the buffer!
1888 ret = GetStringTypeW(type, srcW, countW, chartype);
1889 HeapFree(GetProcessHeap(), 0, srcW);
1891 return ret;
1894 /******************************************************************************
1895 * GetStringTypeExA (KERNEL32.@)
1897 * Get characteristics of the characters making up a string.
1899 * PARAMS
1900 * locale [I] Locale Id for the string
1901 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
1902 * src [I] String to analyse
1903 * count [I] Length of src in chars, or -1 if src is NUL terminated
1904 * chartype [O] Destination for the calculated characteristics
1906 * RETURNS
1907 * Success: TRUE. chartype is filled with the requested characteristics of each char
1908 * in src.
1909 * Failure: FALSE. Use GetLastError() to determine the cause.
1911 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1913 return GetStringTypeA(locale, type, src, count, chartype);
1917 /*************************************************************************
1918 * LCMapStringW (KERNEL32.@)
1920 * See LCMapStringA.
1922 INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
1923 LPWSTR dst, INT dstlen)
1925 LPWSTR dst_ptr;
1927 if (!src || !srclen || dstlen < 0)
1929 SetLastError(ERROR_INVALID_PARAMETER);
1930 return 0;
1933 /* mutually exclusive flags */
1934 if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
1935 (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
1936 (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
1937 (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE))
1939 SetLastError(ERROR_INVALID_FLAGS);
1940 return 0;
1943 if (!dstlen) dst = NULL;
1945 lcid = ConvertDefaultLocale(lcid);
1947 if (flags & LCMAP_SORTKEY)
1949 if (src == dst)
1951 SetLastError(ERROR_INVALID_FLAGS);
1952 return 0;
1955 if (srclen < 0) srclen = strlenW(src);
1957 TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1958 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
1960 return wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
1963 /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
1964 if (flags & SORT_STRINGSORT)
1966 SetLastError(ERROR_INVALID_FLAGS);
1967 return 0;
1970 if (srclen < 0) srclen = strlenW(src) + 1;
1972 TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1973 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
1975 if (!dst) /* return required string length */
1977 INT len;
1979 for (len = 0; srclen; src++, srclen--)
1981 WCHAR wch = *src;
1982 /* tests show that win2k just ignores NORM_IGNORENONSPACE,
1983 * and skips white space and punctuation characters for
1984 * NORM_IGNORESYMBOLS.
1986 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1987 continue;
1988 len++;
1990 return len;
1993 if (flags & LCMAP_UPPERCASE)
1995 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
1997 WCHAR wch = *src;
1998 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1999 continue;
2000 *dst_ptr++ = toupperW(wch);
2001 dstlen--;
2004 else if (flags & LCMAP_LOWERCASE)
2006 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2008 WCHAR wch = *src;
2009 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2010 continue;
2011 *dst_ptr++ = tolowerW(wch);
2012 dstlen--;
2015 else
2017 if (src == dst)
2019 SetLastError(ERROR_INVALID_FLAGS);
2020 return 0;
2022 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2024 WCHAR wch = *src;
2025 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2026 continue;
2027 *dst_ptr++ = wch;
2028 dstlen--;
2032 if (srclen)
2034 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2035 return 0;
2038 return dst_ptr - dst;
2041 /*************************************************************************
2042 * LCMapStringA (KERNEL32.@)
2044 * Map characters in a locale sensitive string.
2046 * PARAMS
2047 * lcid [I] LCID for the conversion.
2048 * flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
2049 * src [I] String to map
2050 * srclen [I] Length of src in chars, or -1 if src is NUL terminated
2051 * dst [O] Destination for mapped string
2052 * dstlen [I] Length of dst in characters
2054 * RETURNS
2055 * Success: The length of the mapped string in dst, including the NUL terminator.
2056 * Failure: 0. Use GetLastError() to determine the cause.
2058 INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
2059 LPSTR dst, INT dstlen)
2061 WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
2062 LPWSTR srcW, dstW;
2063 INT ret = 0, srclenW, dstlenW;
2064 UINT locale_cp;
2066 if (!src || !srclen || dstlen < 0)
2068 SetLastError(ERROR_INVALID_PARAMETER);
2069 return 0;
2072 locale_cp = get_lcid_codepage(lcid);
2074 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
2075 if (srclenW)
2076 srcW = bufW;
2077 else
2079 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
2080 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2081 if (!srcW)
2083 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2084 return 0;
2086 MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
2089 if (flags & LCMAP_SORTKEY)
2091 if (src == dst)
2093 SetLastError(ERROR_INVALID_FLAGS);
2094 goto map_string_exit;
2096 ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
2097 goto map_string_exit;
2100 if (flags & SORT_STRINGSORT)
2102 SetLastError(ERROR_INVALID_FLAGS);
2103 goto map_string_exit;
2106 dstlenW = LCMapStringW(lcid, flags, srcW, srclenW, NULL, 0);
2107 if (!dstlenW)
2108 goto map_string_exit;
2110 dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
2111 if (!dstW)
2113 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2114 goto map_string_exit;
2117 LCMapStringW(lcid, flags, srcW, srclenW, dstW, dstlenW);
2118 ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
2119 HeapFree(GetProcessHeap(), 0, dstW);
2121 map_string_exit:
2122 if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
2123 return ret;
2126 /*************************************************************************
2127 * FoldStringA (KERNEL32.@)
2129 * Map characters in a string.
2131 * PARAMS
2132 * dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
2133 * src [I] String to map
2134 * srclen [I] Length of src, or -1 if src is NUL terminated
2135 * dst [O] Destination for mapped string
2136 * dstlen [I] Length of dst, or 0 to find the required length for the mapped string
2138 * RETURNS
2139 * Success: The length of the string written to dst, including the terminating NUL. If
2140 * dstlen is 0, the value returned is the same, but nothing is written to dst,
2141 * and dst may be NULL.
2142 * Failure: 0. Use GetLastError() to determine the cause.
2144 INT WINAPI FoldStringA(DWORD dwFlags, LPCSTR src, INT srclen,
2145 LPSTR dst, INT dstlen)
2147 INT ret = 0, srclenW = 0;
2148 WCHAR *srcW = NULL, *dstW = NULL;
2150 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2152 SetLastError(ERROR_INVALID_PARAMETER);
2153 return 0;
2156 srclenW = MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2157 src, srclen, NULL, 0);
2158 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2160 if (!srcW)
2162 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2163 goto FoldStringA_exit;
2166 MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2167 src, srclen, srcW, srclenW);
2169 dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
2171 ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
2172 if (ret && dstlen)
2174 dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
2176 if (!dstW)
2178 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2179 goto FoldStringA_exit;
2182 ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
2183 if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
2185 ret = 0;
2186 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2190 if (dstW)
2191 HeapFree(GetProcessHeap(), 0, dstW);
2193 FoldStringA_exit:
2194 if (srcW)
2195 HeapFree(GetProcessHeap(), 0, srcW);
2196 return ret;
2199 /*************************************************************************
2200 * FoldStringW (KERNEL32.@)
2202 * See FoldStringA.
2204 INT WINAPI FoldStringW(DWORD dwFlags, LPCWSTR src, INT srclen,
2205 LPWSTR dst, INT dstlen)
2207 int ret;
2209 switch (dwFlags & (MAP_COMPOSITE|MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES))
2211 case 0:
2212 if (dwFlags)
2213 break;
2214 /* Fall through for dwFlags == 0 */
2215 case MAP_PRECOMPOSED|MAP_COMPOSITE:
2216 case MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES:
2217 case MAP_COMPOSITE|MAP_EXPAND_LIGATURES:
2218 SetLastError(ERROR_INVALID_FLAGS);
2219 return 0;
2222 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2224 SetLastError(ERROR_INVALID_PARAMETER);
2225 return 0;
2228 ret = wine_fold_string(dwFlags, src, srclen, dst, dstlen);
2229 if (!ret)
2230 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2231 return ret;
2234 /******************************************************************************
2235 * CompareStringW (KERNEL32.@)
2237 * See CompareStringA.
2239 INT WINAPI CompareStringW(LCID lcid, DWORD style,
2240 LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
2242 INT ret;
2244 if (!str1 || !str2)
2246 SetLastError(ERROR_INVALID_PARAMETER);
2247 return 0;
2250 if( style & ~(NORM_IGNORECASE|NORM_IGNORENONSPACE|NORM_IGNORESYMBOLS|
2251 SORT_STRINGSORT|NORM_IGNOREKANATYPE|NORM_IGNOREWIDTH|0x10000000) )
2253 SetLastError(ERROR_INVALID_FLAGS);
2254 return 0;
2257 if (style & 0x10000000)
2258 FIXME("Ignoring unknown style 0x10000000\n");
2260 if (len1 < 0) len1 = strlenW(str1);
2261 if (len2 < 0) len2 = strlenW(str2);
2263 ret = wine_compare_string(style, str1, len1, str2, len2);
2265 if (ret) /* need to translate result */
2266 return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
2267 return CSTR_EQUAL;
2270 /******************************************************************************
2271 * CompareStringA (KERNEL32.@)
2273 * Compare two locale sensitive strings.
2275 * PARAMS
2276 * lcid [I] LCID for the comparison
2277 * style [I] Flags for the comparison (NORM_ constants from "winnls.h").
2278 * str1 [I] First string to compare
2279 * len1 [I] Length of str1, or -1 if str1 is NUL terminated
2280 * str2 [I] Second string to compare
2281 * len2 [I] Length of str2, or -1 if str2 is NUL terminated
2283 * RETURNS
2284 * Success: CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN depending on whether
2285 * str2 is less than, equal to or greater than str1 respectively.
2286 * Failure: FALSE. Use GetLastError() to determine the cause.
2288 INT WINAPI CompareStringA(LCID lcid, DWORD style,
2289 LPCSTR str1, INT len1, LPCSTR str2, INT len2)
2291 WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
2292 WCHAR *buf2W = buf1W + 130;
2293 LPWSTR str1W, str2W;
2294 INT len1W, len2W, ret;
2295 UINT locale_cp;
2297 if (!str1 || !str2)
2299 SetLastError(ERROR_INVALID_PARAMETER);
2300 return 0;
2302 if (len1 < 0) len1 = strlen(str1);
2303 if (len2 < 0) len2 = strlen(str2);
2305 locale_cp = get_lcid_codepage(lcid);
2307 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 130);
2308 if (len1W)
2309 str1W = buf1W;
2310 else
2312 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
2313 str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
2314 if (!str1W)
2316 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2317 return 0;
2319 MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
2321 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 130);
2322 if (len2W)
2323 str2W = buf2W;
2324 else
2326 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
2327 str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
2328 if (!str2W)
2330 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2331 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2332 return 0;
2334 MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
2337 ret = CompareStringW(lcid, style, str1W, len1W, str2W, len2W);
2339 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2340 if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
2341 return ret;
2344 /*************************************************************************
2345 * lstrcmp (KERNEL32.@)
2346 * lstrcmpA (KERNEL32.@)
2348 * Compare two strings using the current thread locale.
2350 * PARAMS
2351 * str1 [I] First string to compare
2352 * str2 [I] Second string to compare
2354 * RETURNS
2355 * Success: A number less than, equal to or greater than 0 depending on whether
2356 * str2 is less than, equal to or greater than str1 respectively.
2357 * Failure: FALSE. Use GetLastError() to determine the cause.
2359 int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
2361 int ret;
2363 if ((str1 == NULL) && (str2 == NULL)) return 0;
2364 if (str1 == NULL) return -1;
2365 if (str2 == NULL) return 1;
2367 ret = CompareStringA(GetThreadLocale(), 0, str1, -1, str2, -1);
2368 if (ret) ret -= 2;
2370 return ret;
2373 /*************************************************************************
2374 * lstrcmpi (KERNEL32.@)
2375 * lstrcmpiA (KERNEL32.@)
2377 * Compare two strings using the current thread locale, ignoring case.
2379 * PARAMS
2380 * str1 [I] First string to compare
2381 * str2 [I] Second string to compare
2383 * RETURNS
2384 * Success: A number less than, equal to or greater than 0 depending on whether
2385 * str2 is less than, equal to or greater than str1 respectively.
2386 * Failure: FALSE. Use GetLastError() to determine the cause.
2388 int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
2390 int ret;
2392 if ((str1 == NULL) && (str2 == NULL)) return 0;
2393 if (str1 == NULL) return -1;
2394 if (str2 == NULL) return 1;
2396 ret = CompareStringA(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2397 if (ret) ret -= 2;
2399 return ret;
2402 /*************************************************************************
2403 * lstrcmpW (KERNEL32.@)
2405 * See lstrcmpA.
2407 int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
2409 int ret;
2411 if ((str1 == NULL) && (str2 == NULL)) return 0;
2412 if (str1 == NULL) return -1;
2413 if (str2 == NULL) return 1;
2415 ret = CompareStringW(GetThreadLocale(), 0, str1, -1, str2, -1);
2416 if (ret) ret -= 2;
2418 return ret;
2421 /*************************************************************************
2422 * lstrcmpiW (KERNEL32.@)
2424 * See lstrcmpiA.
2426 int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
2428 int ret;
2430 if ((str1 == NULL) && (str2 == NULL)) return 0;
2431 if (str1 == NULL) return -1;
2432 if (str2 == NULL) return 1;
2434 ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2435 if (ret) ret -= 2;
2437 return ret;
2440 /******************************************************************************
2441 * LOCALE_Init
2443 void LOCALE_Init(void)
2445 extern void __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp,
2446 const union cptable *unix_cp );
2448 UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp = ~0U;
2449 LCID lcid = init_default_lcid( &unix_cp );
2451 NtSetDefaultLocale( FALSE, lcid );
2452 NtSetDefaultLocale( TRUE, lcid );
2454 ansi_cp = get_lcid_codepage(lcid);
2455 GetLocaleInfoW( lcid, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
2456 (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
2457 GetLocaleInfoW( lcid, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
2458 (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
2459 if (unix_cp == ~0U)
2460 GetLocaleInfoW( lcid, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
2461 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) );
2463 if (!(ansi_cptable = wine_cp_get_table( ansi_cp )))
2464 ansi_cptable = wine_cp_get_table( 1252 );
2465 if (!(oem_cptable = wine_cp_get_table( oem_cp )))
2466 oem_cptable = wine_cp_get_table( 437 );
2467 if (!(mac_cptable = wine_cp_get_table( mac_cp )))
2468 mac_cptable = wine_cp_get_table( 10000 );
2469 if (unix_cp != CP_UTF8)
2471 if (!(unix_cptable = wine_cp_get_table( unix_cp )))
2472 unix_cptable = wine_cp_get_table( 28591 );
2475 __wine_init_codepages( ansi_cptable, oem_cptable, unix_cptable );
2477 TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
2478 ansi_cptable->info.codepage, oem_cptable->info.codepage,
2479 mac_cptable->info.codepage, unix_cp );
2482 static HKEY NLS_RegOpenKey(HKEY hRootKey, LPCWSTR szKeyName)
2484 UNICODE_STRING keyName;
2485 OBJECT_ATTRIBUTES attr;
2486 HKEY hkey;
2488 RtlInitUnicodeString( &keyName, szKeyName );
2489 InitializeObjectAttributes(&attr, &keyName, 0, hRootKey, NULL);
2491 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS)
2492 hkey = 0;
2494 return hkey;
2497 static HKEY NLS_RegOpenSubKey(HKEY hRootKey, LPCWSTR szKeyName)
2499 HKEY hKey = NLS_RegOpenKey(hRootKey, szKeyName);
2501 if (hRootKey)
2502 NtClose( hRootKey );
2504 return hKey;
2507 static BOOL NLS_RegEnumSubKey(HKEY hKey, UINT ulIndex, LPWSTR szKeyName,
2508 ULONG keyNameSize)
2510 BYTE buffer[80];
2511 KEY_BASIC_INFORMATION *info = (KEY_BASIC_INFORMATION *)buffer;
2512 DWORD dwLen;
2514 if (NtEnumerateKey( hKey, ulIndex, KeyBasicInformation, buffer,
2515 sizeof(buffer), &dwLen) != STATUS_SUCCESS ||
2516 info->NameLength > keyNameSize)
2518 return FALSE;
2521 TRACE("info->Name %s info->NameLength %ld\n", debugstr_w(info->Name), info->NameLength);
2523 memcpy( szKeyName, info->Name, info->NameLength);
2524 szKeyName[info->NameLength / sizeof(WCHAR)] = '\0';
2526 TRACE("returning %s\n", debugstr_w(szKeyName));
2527 return TRUE;
2530 static BOOL NLS_RegEnumValue(HKEY hKey, UINT ulIndex,
2531 LPWSTR szValueName, ULONG valueNameSize,
2532 LPWSTR szValueData, ULONG valueDataSize)
2534 BYTE buffer[80];
2535 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2536 DWORD dwLen;
2538 if (NtEnumerateValueKey( hKey, ulIndex, KeyValueFullInformation,
2539 buffer, sizeof(buffer), &dwLen ) != STATUS_SUCCESS ||
2540 info->NameLength > valueNameSize ||
2541 info->DataLength > valueDataSize)
2543 return FALSE;
2546 TRACE("info->Name %s info->DataLength %ld\n", debugstr_w(info->Name), info->DataLength);
2548 memcpy( szValueName, info->Name, info->NameLength);
2549 szValueName[info->NameLength / sizeof(WCHAR)] = '\0';
2550 memcpy( szValueData, buffer + info->DataOffset, info->DataLength );
2551 szValueData[info->DataLength / sizeof(WCHAR)] = '\0';
2553 TRACE("returning %s %s\n", debugstr_w(szValueName), debugstr_w(szValueData));
2554 return TRUE;
2557 static BOOL NLS_RegGetDword(HKEY hKey, LPCWSTR szValueName, DWORD *lpVal)
2559 BYTE buffer[128];
2560 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
2561 DWORD dwSize = sizeof(buffer);
2562 UNICODE_STRING valueName;
2564 RtlInitUnicodeString( &valueName, szValueName );
2566 TRACE("%p, %s\n", hKey, debugstr_w(szValueName));
2567 if (NtQueryValueKey( hKey, &valueName, KeyValuePartialInformation,
2568 buffer, dwSize, &dwSize ) == STATUS_SUCCESS &&
2569 info->DataLength == sizeof(DWORD))
2571 memcpy(lpVal, info->Data, sizeof(DWORD));
2572 return TRUE;
2575 return FALSE;
2578 static BOOL NLS_GetLanguageGroupName(LGRPID lgrpid, LPWSTR szName, ULONG nameSize)
2580 LANGID langId;
2581 LPCWSTR szResourceName = (LPCWSTR)(((lgrpid + 0x2000) >> 4) + 1);
2582 HRSRC hResource;
2583 BOOL bRet = FALSE;
2585 /* FIXME: Is it correct to use the system default langid? */
2586 langId = GetSystemDefaultLangID();
2588 if (SUBLANGID(langId) == SUBLANG_NEUTRAL)
2589 langId = MAKELANGID( PRIMARYLANGID(langId), SUBLANG_DEFAULT );
2591 hResource = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING, szResourceName, langId );
2593 if (hResource)
2595 HGLOBAL hResDir = LoadResource( kernel32_handle, hResource );
2597 if (hResDir)
2599 ULONG iResourceIndex = lgrpid & 0xf;
2600 LPCWSTR lpResEntry = LockResource( hResDir );
2601 ULONG i;
2603 for (i = 0; i < iResourceIndex; i++)
2604 lpResEntry += *lpResEntry + 1;
2606 if (*lpResEntry < nameSize)
2608 memcpy( szName, lpResEntry + 1, *lpResEntry * sizeof(WCHAR) );
2609 szName[*lpResEntry] = '\0';
2610 bRet = TRUE;
2614 FreeResource( hResource );
2616 return bRet;
2619 /* Registry keys for NLS related information */
2620 static const WCHAR szNlsKeyName[] = {
2621 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
2622 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
2623 'C','o','n','t','r','o','l','\\','N','l','s','\0'
2626 static const WCHAR szLangGroupsKeyName[] = {
2627 'L','a','n','g','u','a','g','e',' ','G','r','o','u','p','s','\0'
2630 static const WCHAR szCountryListName[] = {
2631 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
2632 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
2633 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2634 'T','e','l','e','p','h','o','n','y','\\',
2635 'C','o','u','n','t','r','y',' ','L','i','s','t','\0'
2639 /* Callback function ptrs for EnumSystemLanguageGroupsA/W */
2640 typedef struct
2642 LANGUAGEGROUP_ENUMPROCA procA;
2643 LANGUAGEGROUP_ENUMPROCW procW;
2644 DWORD dwFlags;
2645 LONG_PTR lParam;
2646 } ENUMLANGUAGEGROUP_CALLBACKS;
2648 /* Internal implementation of EnumSystemLanguageGroupsA/W */
2649 static BOOL NLS_EnumSystemLanguageGroups(ENUMLANGUAGEGROUP_CALLBACKS *lpProcs)
2651 WCHAR szNumber[10], szValue[4];
2652 HKEY hKey;
2653 BOOL bContinue = TRUE;
2654 ULONG ulIndex = 0;
2656 if (!lpProcs)
2658 SetLastError(ERROR_INVALID_PARAMETER);
2659 return FALSE;
2662 switch (lpProcs->dwFlags)
2664 case 0:
2665 /* Default to LGRPID_INSTALLED */
2666 lpProcs->dwFlags = LGRPID_INSTALLED;
2667 /* Fall through... */
2668 case LGRPID_INSTALLED:
2669 case LGRPID_SUPPORTED:
2670 break;
2671 default:
2672 SetLastError(ERROR_INVALID_FLAGS);
2673 return FALSE;
2676 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLangGroupsKeyName );
2678 if (!hKey)
2679 WARN("NLS registry key not found. Please apply the default registry file 'winedefault.reg'\n");
2681 while (bContinue)
2683 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
2684 szValue, sizeof(szValue) ))
2686 BOOL bInstalled = szValue[0] == '1' ? TRUE : FALSE;
2687 LGRPID lgrpid = strtoulW( szNumber, NULL, 16 );
2689 TRACE("grpid %s (%sinstalled)\n", debugstr_w(szNumber),
2690 bInstalled ? "" : "not ");
2692 if (lpProcs->dwFlags == LGRPID_SUPPORTED || bInstalled)
2694 WCHAR szGrpName[48];
2696 if (!NLS_GetLanguageGroupName( lgrpid, szGrpName, sizeof(szGrpName) / sizeof(WCHAR) ))
2697 szGrpName[0] = '\0';
2699 if (lpProcs->procW)
2700 bContinue = lpProcs->procW( lgrpid, szNumber, szGrpName, lpProcs->dwFlags,
2701 lpProcs->lParam );
2702 else
2704 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2705 char szGrpNameA[48];
2707 /* FIXME: MSDN doesn't say which code page the W->A translation uses,
2708 * or whether the language names are ever localised. Assume CP_ACP.
2711 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2712 WideCharToMultiByte(CP_ACP, 0, szGrpName, -1, szGrpNameA, sizeof(szGrpNameA), 0, 0);
2714 bContinue = lpProcs->procA( lgrpid, szNumberA, szGrpNameA, lpProcs->dwFlags,
2715 lpProcs->lParam );
2719 ulIndex++;
2721 else
2722 bContinue = FALSE;
2724 if (!bContinue)
2725 break;
2728 if (hKey)
2729 NtClose( hKey );
2731 return TRUE;
2734 /******************************************************************************
2735 * EnumSystemLanguageGroupsA (KERNEL32.@)
2737 * Call a users function for each language group available on the system.
2739 * PARAMS
2740 * pLangGrpEnumProc [I] Callback function to call for each language group
2741 * dwFlags [I] LGRPID_SUPPORTED=All Supported, LGRPID_INSTALLED=Installed only
2742 * lParam [I] User parameter to pass to pLangGrpEnumProc
2744 * RETURNS
2745 * Success: TRUE.
2746 * Failure: FALSE. Use GetLastError() to determine the cause.
2748 BOOL WINAPI EnumSystemLanguageGroupsA(LANGUAGEGROUP_ENUMPROCA pLangGrpEnumProc,
2749 DWORD dwFlags, LONG_PTR lParam)
2751 ENUMLANGUAGEGROUP_CALLBACKS procs;
2753 TRACE("(%p,0x%08lX,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
2755 procs.procA = pLangGrpEnumProc;
2756 procs.procW = NULL;
2757 procs.dwFlags = dwFlags;
2758 procs.lParam = lParam;
2760 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
2763 /******************************************************************************
2764 * EnumSystemLanguageGroupsW (KERNEL32.@)
2766 * See EnumSystemLanguageGroupsA.
2768 BOOL WINAPI EnumSystemLanguageGroupsW(LANGUAGEGROUP_ENUMPROCW pLangGrpEnumProc,
2769 DWORD dwFlags, LONG_PTR lParam)
2771 ENUMLANGUAGEGROUP_CALLBACKS procs;
2773 TRACE("(%p,0x%08lX,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
2775 procs.procA = NULL;
2776 procs.procW = pLangGrpEnumProc;
2777 procs.dwFlags = dwFlags;
2778 procs.lParam = lParam;
2780 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
2783 /******************************************************************************
2784 * IsValidLanguageGroup (KERNEL32.@)
2786 * Determine if a language group is supported and/or installed.
2788 * PARAMS
2789 * lgrpid [I] Language Group Id (LGRPID_ values from "winnls.h")
2790 * dwFlags [I] LGRPID_SUPPORTED=Supported, LGRPID_INSTALLED=Installed
2792 * RETURNS
2793 * TRUE, if lgrpid is supported and/or installed, according to dwFlags.
2794 * FALSE otherwise.
2796 BOOL WINAPI IsValidLanguageGroup(LGRPID lgrpid, DWORD dwFlags)
2798 static const WCHAR szFormat[] = { '%','x','\0' };
2799 WCHAR szValueName[16], szValue[2];
2800 BOOL bSupported = FALSE, bInstalled = FALSE;
2801 HKEY hKey;
2804 switch (dwFlags)
2806 case LGRPID_INSTALLED:
2807 case LGRPID_SUPPORTED:
2809 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLangGroupsKeyName );
2811 sprintfW( szValueName, szFormat, lgrpid );
2813 if (NLS_RegGetDword( hKey, szValueName, (LPDWORD)&szValue ))
2815 bSupported = TRUE;
2817 if (szValue[0] == '1')
2818 bInstalled = TRUE;
2821 if (hKey)
2822 NtClose( hKey );
2824 break;
2827 if ((dwFlags == LGRPID_SUPPORTED && bSupported) ||
2828 (dwFlags == LGRPID_INSTALLED && bInstalled))
2829 return TRUE;
2831 return FALSE;
2834 /* Callback function ptrs for EnumLanguageGrouplocalesA/W */
2835 typedef struct
2837 LANGGROUPLOCALE_ENUMPROCA procA;
2838 LANGGROUPLOCALE_ENUMPROCW procW;
2839 DWORD dwFlags;
2840 LGRPID lgrpid;
2841 LONG_PTR lParam;
2842 } ENUMLANGUAGEGROUPLOCALE_CALLBACKS;
2844 /* Internal implementation of EnumLanguageGrouplocalesA/W */
2845 static BOOL NLS_EnumLanguageGroupLocales(ENUMLANGUAGEGROUPLOCALE_CALLBACKS *lpProcs)
2847 static const WCHAR szLocaleKeyName[] = {
2848 'L','o','c','a','l','e','\0'
2850 static const WCHAR szAlternateSortsKeyName[] = {
2851 'A','l','t','e','r','n','a','t','e',' ','S','o','r','t','s','\0'
2853 WCHAR szNumber[10], szValue[4];
2854 HKEY hKey;
2855 BOOL bContinue = TRUE, bAlternate = FALSE;
2856 LGRPID lgrpid;
2857 ULONG ulIndex = 1; /* Ignore default entry of 1st key */
2859 if (!lpProcs || !lpProcs->lgrpid || lpProcs->lgrpid > LGRPID_ARMENIAN)
2861 SetLastError(ERROR_INVALID_PARAMETER);
2862 return FALSE;
2865 if (lpProcs->dwFlags)
2867 SetLastError(ERROR_INVALID_FLAGS);
2868 return FALSE;
2871 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLocaleKeyName );
2873 if (!hKey)
2874 WARN("NLS registry key not found. Please apply the default registry file 'winedefault.reg'\n");
2876 while (bContinue)
2878 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
2879 szValue, sizeof(szValue) ))
2881 lgrpid = strtoulW( szValue, NULL, 16 );
2883 TRACE("lcid %s, grpid %ld (%smatched)\n", debugstr_w(szNumber),
2884 lgrpid, lgrpid == lpProcs->lgrpid ? "" : "not ");
2886 if (lgrpid == lpProcs->lgrpid)
2888 LCID lcid;
2890 lcid = strtoulW( szNumber, NULL, 16 );
2892 /* FIXME: native returns extra text for a few (17/150) locales, e.g:
2893 * '00000437 ;Georgian'
2894 * At present we only pass the LCID string.
2897 if (lpProcs->procW)
2898 bContinue = lpProcs->procW( lgrpid, lcid, szNumber, lpProcs->lParam );
2899 else
2901 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2903 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2905 bContinue = lpProcs->procA( lgrpid, lcid, szNumberA, lpProcs->lParam );
2909 ulIndex++;
2911 else
2913 /* Finished enumerating this key */
2914 if (!bAlternate)
2916 /* Enumerate alternate sorts also */
2917 hKey = NLS_RegOpenKey( hKey, szAlternateSortsKeyName );
2918 bAlternate = TRUE;
2919 ulIndex = 0;
2921 else
2922 bContinue = FALSE; /* Finished both keys */
2925 if (!bContinue)
2926 break;
2929 if (hKey)
2930 NtClose( hKey );
2932 return TRUE;
2935 /******************************************************************************
2936 * EnumLanguageGroupLocalesA (KERNEL32.@)
2938 * Call a users function for every locale in a language group available on the system.
2940 * PARAMS
2941 * pLangGrpLcEnumProc [I] Callback function to call for each locale
2942 * lgrpid [I] Language group (LGRPID_ values from "winnls.h")
2943 * dwFlags [I] Reserved, set to 0
2944 * lParam [I] User parameter to pass to pLangGrpLcEnumProc
2946 * RETURNS
2947 * Success: TRUE.
2948 * Failure: FALSE. Use GetLastError() to determine the cause.
2950 BOOL WINAPI EnumLanguageGroupLocalesA(LANGGROUPLOCALE_ENUMPROCA pLangGrpLcEnumProc,
2951 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
2953 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
2955 TRACE("(%p,0x%08lX,0x%08lX,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
2957 callbacks.procA = pLangGrpLcEnumProc;
2958 callbacks.procW = NULL;
2959 callbacks.dwFlags = dwFlags;
2960 callbacks.lgrpid = lgrpid;
2961 callbacks.lParam = lParam;
2963 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
2966 /******************************************************************************
2967 * EnumLanguageGroupLocalesW (KERNEL32.@)
2969 * See EnumLanguageGroupLocalesA.
2971 BOOL WINAPI EnumLanguageGroupLocalesW(LANGGROUPLOCALE_ENUMPROCW pLangGrpLcEnumProc,
2972 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
2974 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
2976 TRACE("(%p,0x%08lX,0x%08lX,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
2978 callbacks.procA = NULL;
2979 callbacks.procW = pLangGrpLcEnumProc;
2980 callbacks.dwFlags = dwFlags;
2981 callbacks.lgrpid = lgrpid;
2982 callbacks.lParam = lParam;
2984 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
2987 /******************************************************************************
2988 * EnumSystemGeoID (KERNEL32.@)
2990 * Call a users function for every location available on the system.
2992 * PARAMS
2993 * geoclass [I] Type of information desired (SYSGEOTYPE enum from "winnls.h")
2994 * reserved [I] Reserved, set to 0
2995 * pGeoEnumProc [I] Callback function to call for each location
2997 * RETURNS
2998 * Success: TRUE.
2999 * Failure: FALSE. Use GetLastError() to determine the cause.
3001 BOOL WINAPI EnumSystemGeoID(GEOCLASS geoclass, GEOID reserved, GEO_ENUMPROC pGeoEnumProc)
3003 static const WCHAR szCountryCodeValueName[] = {
3004 'C','o','u','n','t','r','y','C','o','d','e','\0'
3006 WCHAR szNumber[10];
3007 HKEY hKey;
3008 ULONG ulIndex = 0;
3010 TRACE("(0x%08lX,0x%08lX,%p)\n", geoclass, reserved, pGeoEnumProc);
3012 if (geoclass != GEOCLASS_NATION || reserved || !pGeoEnumProc)
3014 SetLastError(ERROR_INVALID_PARAMETER);
3015 return FALSE;
3018 hKey = NLS_RegOpenKey( 0, szCountryListName );
3020 while (NLS_RegEnumSubKey( hKey, ulIndex, szNumber, sizeof(szNumber) ))
3022 BOOL bContinue = TRUE;
3023 DWORD dwGeoId;
3024 HKEY hSubKey = NLS_RegOpenKey( hKey, szNumber );
3026 if (hSubKey)
3028 if (NLS_RegGetDword( hSubKey, szCountryCodeValueName, &dwGeoId ))
3030 TRACE("Got geoid %ld\n", dwGeoId);
3032 if (!pGeoEnumProc( dwGeoId ))
3033 bContinue = FALSE;
3036 NtClose( hSubKey );
3039 if (!bContinue)
3040 break;
3042 ulIndex++;
3045 if (hKey)
3046 NtClose( hKey );
3048 return TRUE;
3051 /******************************************************************************
3052 * InvalidateNLSCache (KERNEL32.@)
3054 * Invalidate the cache of NLS values.
3056 * PARAMS
3057 * None.
3059 * RETURNS
3060 * Success: TRUE.
3061 * Failure: FALSE.
3063 BOOL WINAPI InvalidateNLSCache(void)
3065 FIXME("() stub\n");
3066 return FALSE;
3069 /******************************************************************************
3070 * GetUserGeoID (KERNEL32.@)
3072 GEOID WINAPI GetUserGeoID( GEOCLASS GeoClass )
3074 FIXME("%ld\n",GeoClass);
3075 return GEOID_NOT_AVAILABLE;
3078 /******************************************************************************
3079 * SetUserGeoID (KERNEL32.@)
3081 BOOL WINAPI SetUserGeoID( GEOID GeoID )
3083 FIXME("%ld\n",GeoID);
3084 return FALSE;