Added missing parameter check in SetLocaleInfoA revealed by the
[wine.git] / dlls / kernel / locale.c
blobed771e7318cc222a401b7ddcd20813e2c7a1ac08
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 "thread.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(nls);
48 #define LOCALE_LOCALEINFOFLAGSMASK (LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP|LOCALE_RETURN_NUMBER)
50 static const WCHAR kernel32W[] = { 'K','E','R','N','E','L','3','2','\0' };
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 { "CP1250", 1250 },
66 { "CP1251", 1251 },
67 { "CP1252", 1252 },
68 { "CP1253", 1253 },
69 { "CP1254", 1254 },
70 { "CP1255", 1255 },
71 { "CP1256", 1256 },
72 { "CP1257", 1257 },
73 { "CP1258", 1258 },
74 { "EUCJP", 20932 },
75 { "IBM037", 37 },
76 { "IBM1026", 1026 },
77 { "IBM424", 424 },
78 { "IBM437", 437 },
79 { "IBM500", 500 },
80 { "IBM850", 850 },
81 { "IBM852", 852 },
82 { "IBM855", 855 },
83 { "IBM857", 857 },
84 { "IBM860", 860 },
85 { "IBM861", 861 },
86 { "IBM862", 862 },
87 { "IBM863", 863 },
88 { "IBM864", 864 },
89 { "IBM865", 865 },
90 { "IBM866", 866 },
91 { "IBM869", 869 },
92 { "IBM874", 874 },
93 { "IBM875", 875 },
94 { "ISO88591", 28591 },
95 { "ISO885910", 28600 },
96 { "ISO885913", 28603 },
97 { "ISO885914", 28604 },
98 { "ISO885915", 28605 },
99 { "ISO88592", 28592 },
100 { "ISO88593", 28593 },
101 { "ISO88594", 28594 },
102 { "ISO88595", 28595 },
103 { "ISO88596", 28596 },
104 { "ISO88597", 28597 },
105 { "ISO88598", 28598 },
106 { "ISO88599", 28599 },
107 { "KOI8R", 20866 },
108 { "KOI8U", 20866 },
109 { "UTF8", CP_UTF8 }
112 #define NLS_MAX_LANGUAGES 20
113 typedef struct {
114 WCHAR lang[128];
115 WCHAR country[4];
116 LANGID found_lang_id[NLS_MAX_LANGUAGES];
117 WCHAR found_language[NLS_MAX_LANGUAGES][3];
118 WCHAR found_country[NLS_MAX_LANGUAGES][3];
119 int n_found;
120 } LANG_FIND_DATA;
123 /* copy Unicode string to Ascii without using codepages */
124 static inline void strcpyWtoA( char *dst, const WCHAR *src )
126 while ((*dst++ = *src++));
129 /* Copy Ascii string to Unicode without using codepages */
130 static inline void strcpynAtoW( WCHAR *dst, const char *src, size_t n )
132 while (n > 1 && *src)
134 *dst++ = (unsigned char)*src++;
135 n--;
137 if (n) *dst = 0;
140 /* return a printable string for a language id */
141 static const char *debugstr_lang( LANGID lang )
143 WCHAR langW[4], countryW[4];
144 char buffer[8];
145 LCID lcid = MAKELCID( lang, SORT_DEFAULT );
147 GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE, langW, sizeof(langW));
148 GetLocaleInfoW(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE, countryW, sizeof(countryW));
149 strcpyWtoA( buffer, langW );
150 strcat( buffer, "_" );
151 strcpyWtoA( buffer + strlen(buffer), countryW );
152 return wine_dbg_sprintf( "%s", buffer );
155 /***********************************************************************
156 * get_lcid_codepage
158 * Retrieve the ANSI codepage for a given locale.
160 inline static UINT get_lcid_codepage( LCID lcid )
162 UINT ret;
163 if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
164 sizeof(ret)/sizeof(WCHAR) )) ret = 0;
165 return ret;
169 /***********************************************************************
170 * get_codepage_table
172 * Find the table for a given codepage, handling CP_ACP etc. pseudo-codepages
174 static const union cptable *get_codepage_table( unsigned int codepage )
176 const union cptable *ret = NULL;
178 assert( ansi_cptable ); /* init must have been done already */
180 switch(codepage)
182 case CP_ACP:
183 return ansi_cptable;
184 case CP_OEMCP:
185 return oem_cptable;
186 case CP_MACCP:
187 return mac_cptable;
188 case CP_UTF7:
189 case CP_UTF8:
190 break;
191 case CP_THREAD_ACP:
192 if (!(codepage = NtCurrentTeb()->code_page)) return ansi_cptable;
193 /* fall through */
194 default:
195 if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
196 if (codepage == oem_cptable->info.codepage) return oem_cptable;
197 if (codepage == mac_cptable->info.codepage) return mac_cptable;
198 ret = wine_cp_get_table( codepage );
199 break;
201 return ret;
204 /***********************************************************************
205 * create_registry_key
207 * Create the Control Panel\\International registry key.
209 inline static HKEY create_registry_key(void)
211 static const WCHAR intlW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
212 'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
213 OBJECT_ATTRIBUTES attr;
214 UNICODE_STRING nameW;
215 HKEY hkey;
217 if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
219 attr.Length = sizeof(attr);
220 attr.RootDirectory = hkey;
221 attr.ObjectName = &nameW;
222 attr.Attributes = 0;
223 attr.SecurityDescriptor = NULL;
224 attr.SecurityQualityOfService = NULL;
225 RtlInitUnicodeString( &nameW, intlW );
227 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS) hkey = 0;
228 NtClose( attr.RootDirectory );
229 return hkey;
233 /***********************************************************************
234 * LOCALE_InitRegistry
236 * Update registry contents on startup if the user locale has changed.
237 * This simulates the action of the Windows control panel.
239 void LOCALE_InitRegistry(void)
241 static const USHORT updateValues[] = {
242 LOCALE_SLANGUAGE,
243 LOCALE_SCOUNTRY, LOCALE_ICOUNTRY,
244 LOCALE_S1159, LOCALE_S2359,
245 LOCALE_STIME, LOCALE_ITIME,
246 LOCALE_ITLZERO,
247 LOCALE_SSHORTDATE,
248 LOCALE_IDATE,
249 LOCALE_SLONGDATE,
250 LOCALE_SDATE,
251 LOCALE_SCURRENCY, LOCALE_ICURRENCY,
252 LOCALE_INEGCURR,
253 LOCALE_ICURRDIGITS,
254 LOCALE_SDECIMAL,
255 LOCALE_SLIST,
256 LOCALE_STHOUSAND,
257 LOCALE_IDIGITS,
258 LOCALE_IDIGITSUBSTITUTION,
259 LOCALE_SNATIVEDIGITS,
260 LOCALE_ITIMEMARKPOSN,
261 LOCALE_ICALENDARTYPE,
262 LOCALE_ILZERO,
263 LOCALE_IMEASURE
265 static const WCHAR LocaleW[] = {'L','o','c','a','l','e',0};
266 UNICODE_STRING nameW;
267 char buffer[20];
268 WCHAR bufferW[80];
269 DWORD count, i;
270 HKEY hkey;
271 LCID lcid = GetUserDefaultLCID();
273 if (!(hkey = create_registry_key()))
274 return; /* don't do anything if we can't create the registry key */
276 RtlInitUnicodeString( &nameW, LocaleW );
277 count = sizeof(bufferW);
278 if (!NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation, (LPBYTE)bufferW, count, &count))
280 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
281 LPCWSTR szValueText = (LPCWSTR)info->Data;
283 if (strtoulW( szValueText, NULL, 16 ) == lcid) /* already set correctly */
285 NtClose( hkey );
286 return;
288 TRACE( "updating registry, locale changed %s -> %08lx\n", debugstr_w(szValueText), lcid );
290 else TRACE( "updating registry, locale changed none -> %08lx\n", lcid );
292 sprintf( buffer, "%08lx", lcid );
293 /* Note: '9' constant below is strlen(buffer) + 1 */
294 RtlMultiByteToUnicodeN( bufferW, sizeof(bufferW), NULL, buffer, 9 );
295 NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, 9 * sizeof(WCHAR) );
296 NtClose( hkey );
298 for (i = 0; i < sizeof(updateValues)/sizeof(updateValues[0]); i++)
300 GetLocaleInfoW( lcid, updateValues[i] | LOCALE_NOUSEROVERRIDE, bufferW,
301 sizeof(bufferW)/sizeof(WCHAR) );
302 SetLocaleInfoW( lcid, updateValues[i], bufferW );
307 /***********************************************************************
308 * find_language_id_proc
310 static BOOL CALLBACK find_language_id_proc( HMODULE hModule, LPCWSTR type,
311 LPCWSTR name, WORD LangID, LPARAM lParam )
313 LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
314 LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
315 WCHAR buf_language[128];
316 WCHAR buf_country[128];
317 WCHAR buf_en_language[128];
319 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
320 return TRUE; /* continue search */
322 buf_language[0] = 0;
323 buf_country[0] = 0;
325 GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE,
326 buf_language, sizeof(buf_language));
327 GetLocaleInfoW(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
328 buf_country, sizeof(buf_country));
330 if(l_data->lang[0] && !strcmpiW(l_data->lang, buf_language))
332 if(l_data->country[0])
334 if(!strcmpiW(l_data->country, buf_country))
336 l_data->found_lang_id[0] = LangID;
337 l_data->n_found = 1;
338 TRACE("Found id %04X for lang %s country %s\n",
339 LangID, debugstr_w(l_data->lang), debugstr_w(l_data->country));
340 return FALSE; /* stop enumeration */
343 else goto found; /* l_data->country not specified */
346 /* Just in case, check LOCALE_SENGLANGUAGE too,
347 * in hope that possible alias name might have that value.
349 buf_en_language[0] = 0;
350 GetLocaleInfoW(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
351 buf_en_language, sizeof(buf_en_language));
353 if(l_data->lang[0] && !strcmpiW(l_data->lang, buf_en_language)) goto found;
354 return TRUE; /* not found, continue search */
356 found:
357 l_data->found_lang_id[l_data->n_found] = LangID;
358 strncpyW(l_data->found_country[l_data->n_found], buf_country, 3);
359 strncpyW(l_data->found_language[l_data->n_found], buf_language, 3);
360 l_data->n_found++;
361 TRACE("Found id %04X for lang %s\n", LangID, debugstr_w(l_data->lang));
362 return (l_data->n_found < NLS_MAX_LANGUAGES); /* continue search, unless we have enough */
366 /***********************************************************************
367 * get_language_id
369 * INPUT:
370 * Lang: a string whose two first chars are the iso name of a language.
371 * Country: a string whose two first chars are the iso name of country
372 * Charset: a string defining the chosen charset encoding
373 * Dialect: a string defining a variation of the locale
375 * all those values are from the standardized format of locale
376 * name in unix which is: Lang[_Country][.Charset][@Dialect]
378 * RETURNS:
379 * the numeric code of the language used by Windows
381 * FIXME: Charset and Dialect are not handled
383 static LANGID get_language_id(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
385 LANG_FIND_DATA l_data;
386 HMODULE hKernel32;
388 if(!Lang)
390 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
391 goto END;
394 l_data.n_found = 0;
395 strcpynAtoW(l_data.lang, Lang, sizeof(l_data.lang));
397 if (Country) strcpynAtoW(l_data.country, Country, sizeof(l_data.country));
398 else l_data.country[0] = 0;
400 hKernel32 = GetModuleHandleW(kernel32W);
402 EnumResourceLanguagesW(hKernel32, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
403 find_language_id_proc, (LPARAM)&l_data);
405 if (l_data.n_found == 1) goto END;
407 if(!l_data.n_found)
409 if(l_data.country[0])
411 /* retry without country name */
412 l_data.country[0] = 0;
413 EnumResourceLanguagesW(hKernel32, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
414 find_language_id_proc, (LONG)&l_data);
415 if (!l_data.n_found)
417 MESSAGE("Warning: Language '%s_%s' was not recognized, defaulting to English.\n",
418 Lang, Country);
419 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
421 else MESSAGE("Warning: Language '%s_%s' was not recognized, defaulting to '%s'.\n",
422 Lang, Country, debugstr_lang(l_data.found_lang_id[0]) );
424 else
426 MESSAGE("Warning: Language '%s' was not recognized, defaulting to English.\n", Lang);
427 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
430 else
432 int i;
434 if (Country && Country[0])
435 MESSAGE("For language '%s_%s' several language ids were found:\n", Lang, Country);
436 else
437 MESSAGE("For language '%s' several language ids were found:\n", Lang);
439 /* print a list of languages with their description */
440 for (i = 0; i < l_data.n_found; i++)
442 WCHAR buffW[128];
443 char buffA[128];
444 GetLocaleInfoW( MAKELCID( l_data.found_lang_id[i], SORT_DEFAULT ),
445 LOCALE_SLANGUAGE|LOCALE_NOUSEROVERRIDE, buffW, sizeof(buffW));
446 strcpyWtoA( buffA, buffW );
447 MESSAGE( " %s (%04X) - %s\n", debugstr_lang(l_data.found_lang_id[i]),
448 l_data.found_lang_id[i], buffA );
450 MESSAGE("Defaulting to '%s'. You should specify the exact language you want\n"
451 "by defining your LANG environment variable like this: LANG=%s\n",
452 debugstr_lang(l_data.found_lang_id[0]), debugstr_lang(l_data.found_lang_id[0]) );
454 END:
455 TRACE("Returning %04X (%s)\n", l_data.found_lang_id[0], debugstr_lang(l_data.found_lang_id[0]));
456 return l_data.found_lang_id[0];
460 /***********************************************************************
461 * charset_cmp (internal)
463 static int charset_cmp( const void *name, const void *entry )
465 const struct charset_entry *charset = (struct charset_entry *)entry;
466 return strcasecmp( (char *)name, charset->charset_name );
469 /***********************************************************************
470 * init_default_lcid
472 static LCID init_default_lcid( UINT *unix_cp )
474 char *buf, *lang,*country,*charset,*dialect,*next;
475 LCID ret = 0;
477 if ((lang = getenv( "LC_ALL" )) ||
478 (lang = getenv( "LC_CTYPE" )) ||
479 (lang = getenv( "LANGUAGE" )) ||
480 (lang = getenv( "LC_MESSAGES" )) ||
481 (lang = getenv( "LANG" )))
483 if (!strcmp(lang,"POSIX") || !strcmp(lang,"C")) goto done;
485 buf = RtlAllocateHeap( GetProcessHeap(), 0, strlen(lang) + 1 );
486 strcpy( buf, lang );
487 lang=buf;
489 do {
490 next=strchr(lang,':'); if (next) *next++='\0';
491 dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
492 charset=strchr(lang,'.'); if (charset) *charset++='\0';
493 country=strchr(lang,'_'); if (country) *country++='\0';
495 ret = get_language_id(lang, country, charset, dialect);
496 if (ret && charset)
498 const struct charset_entry *entry;
499 char charset_name[16];
500 size_t i, j;
502 /* remove punctuation characters from charset name */
503 for (i = j = 0; charset[i] && j < sizeof(charset_name)-1; i++)
504 if (isalnum(charset[i])) charset_name[j++] = charset[i];
505 charset_name[j] = 0;
507 entry = bsearch( charset_name, charset_names,
508 sizeof(charset_names)/sizeof(charset_names[0]),
509 sizeof(charset_names[0]), charset_cmp );
510 if (entry)
512 *unix_cp = entry->codepage;
513 TRACE("charset %s was mapped to cp %u\n", charset, *unix_cp);
515 else
516 FIXME("charset %s was not recognized\n", charset);
519 lang=next;
520 } while (lang && !ret);
522 if (!ret) MESSAGE("Warning: language '%s' not recognized, defaulting to English\n", buf);
523 RtlFreeHeap( GetProcessHeap(), 0, buf );
526 done:
527 if (!ret) ret = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT) ;
528 return ret;
532 /***********************************************************************
533 * GetUserDefaultLangID (KERNEL32.@)
535 * Get the default language Id for the current user.
537 * PARAMS
538 * None.
540 * RETURNS
541 * The current LANGID of the default language for the current user.
543 LANGID WINAPI GetUserDefaultLangID(void)
545 return LANGIDFROMLCID(GetUserDefaultLCID());
549 /***********************************************************************
550 * GetSystemDefaultLangID (KERNEL32.@)
552 * Get the default language Id for the system.
554 * PARAMS
555 * None.
557 * RETURNS
558 * The current LANGID of the default language for the system.
560 LANGID WINAPI GetSystemDefaultLangID(void)
562 return GetUserDefaultLangID();
566 /***********************************************************************
567 * GetUserDefaultLCID (KERNEL32.@)
569 * Get the default locale Id for the current user.
571 * PARAMS
572 * None.
574 * RETURNS
575 * The current LCID of the default locale for the current user.
577 LCID WINAPI GetUserDefaultLCID(void)
579 LCID lcid;
580 NtQueryDefaultLocale( TRUE, &lcid );
581 return lcid;
585 /***********************************************************************
586 * GetSystemDefaultLCID (KERNEL32.@)
588 * Get the default locale Id for the system.
590 * PARAMS
591 * None.
593 * RETURNS
594 * The current LCID of the default locale for the system.
596 LCID WINAPI GetSystemDefaultLCID(void)
598 LCID lcid;
599 NtQueryDefaultLocale( FALSE, &lcid );
600 return lcid;
604 /***********************************************************************
605 * GetUserDefaultUILanguage (KERNEL32.@)
607 * Get the default user interface language Id for the current user.
609 * PARAMS
610 * None.
612 * RETURNS
613 * The current LANGID of the default UI language for the current user.
615 LANGID WINAPI GetUserDefaultUILanguage(void)
617 return GetUserDefaultLangID();
621 /***********************************************************************
622 * GetSystemDefaultUILanguage (KERNEL32.@)
624 * Get the default user interface language Id for the system.
626 * PARAMS
627 * None.
629 * RETURNS
630 * The current LANGID of the default UI language for the system. This is
631 * typically the same language used during the installation process.
633 LANGID WINAPI GetSystemDefaultUILanguage(void)
635 return GetSystemDefaultLangID();
639 /******************************************************************************
640 * get_locale_value_name
642 * Gets the registry value name for a given lctype.
644 static const WCHAR *get_locale_value_name( DWORD lctype )
646 static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
647 static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
648 static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
649 static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
650 static const WCHAR iDateW[] = {'i','D','a','t','e',0};
651 static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
652 static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
653 static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
654 static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
655 static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
656 static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
657 static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
658 static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
659 static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
660 static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
661 static const WCHAR iTimePrefixW[] = {'i','T','i','m','e','P','r','e','f','i','x',0};
662 static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
663 static const WCHAR s1159W[] = {'s','1','1','5','9',0};
664 static const WCHAR s2359W[] = {'s','2','3','5','9',0};
665 static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
666 static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
667 static const WCHAR sDateW[] = {'s','D','a','t','e',0};
668 static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
669 static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
670 static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
671 static const WCHAR sListW[] = {'s','L','i','s','t',0};
672 static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
673 static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
674 static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
675 static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
676 static const WCHAR sNativeDigitsW[] = {'s','N','a','t','i','v','e','D','i','g','i','t','s',0};
677 static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
678 static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
679 static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
680 static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
681 static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
682 static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
683 static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
684 static const WCHAR NumShapeW[] = {'N','u','m','s','h','a','p','e',0};
686 switch (lctype)
688 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
689 * the values are stored in the registry, confirmed under Windows.
691 case LOCALE_ICALENDARTYPE: return iCalendarTypeW;
692 case LOCALE_ICURRDIGITS: return iCurrDigitsW;
693 case LOCALE_ICURRENCY: return iCurrencyW;
694 case LOCALE_IDIGITS: return iDigitsW;
695 case LOCALE_IFIRSTDAYOFWEEK: return iFirstDayOfWeekW;
696 case LOCALE_IFIRSTWEEKOFYEAR: return iFirstWeekOfYearW;
697 case LOCALE_ILZERO: return iLZeroW;
698 case LOCALE_IMEASURE: return iMeasureW;
699 case LOCALE_INEGCURR: return iNegCurrW;
700 case LOCALE_INEGNUMBER: return iNegNumberW;
701 case LOCALE_IPAPERSIZE: return iPaperSizeW;
702 case LOCALE_ITIME: return iTimeW;
703 case LOCALE_S1159: return s1159W;
704 case LOCALE_S2359: return s2359W;
705 case LOCALE_SCURRENCY: return sCurrencyW;
706 case LOCALE_SDATE: return sDateW;
707 case LOCALE_SDECIMAL: return sDecimalW;
708 case LOCALE_SGROUPING: return sGroupingW;
709 case LOCALE_SLIST: return sListW;
710 case LOCALE_SLONGDATE: return sLongDateW;
711 case LOCALE_SMONDECIMALSEP: return sMonDecimalSepW;
712 case LOCALE_SMONGROUPING: return sMonGroupingW;
713 case LOCALE_SMONTHOUSANDSEP: return sMonThousandSepW;
714 case LOCALE_SNEGATIVESIGN: return sNegativeSignW;
715 case LOCALE_SPOSITIVESIGN: return sPositiveSignW;
716 case LOCALE_SSHORTDATE: return sShortDateW;
717 case LOCALE_STHOUSAND: return sThousandW;
718 case LOCALE_STIME: return sTimeW;
719 case LOCALE_STIMEFORMAT: return sTimeFormatW;
720 case LOCALE_SYEARMONTH: return sYearMonthW;
722 /* The following are not listed under MSDN as supported,
723 * but seem to be used and also stored in the registry.
725 case LOCALE_ICOUNTRY: return iCountryW;
726 case LOCALE_IDATE: return iDateW;
727 case LOCALE_ILDATE: return iLDateW;
728 case LOCALE_ITLZERO: return iTLZeroW;
729 case LOCALE_SCOUNTRY: return sCountryW;
730 case LOCALE_SLANGUAGE: return sLanguageW;
732 /* The following are used in XP and later */
733 case LOCALE_IDIGITSUBSTITUTION: return NumShapeW;
734 case LOCALE_SNATIVEDIGITS: return sNativeDigitsW;
735 case LOCALE_ITIMEMARKPOSN: return iTimePrefixW;
737 return NULL;
741 /******************************************************************************
742 * get_registry_locale_info
744 * Retrieve user-modified locale info from the registry.
745 * Return length, 0 on error, -1 if not found.
747 static INT get_registry_locale_info( LPCWSTR value, LPWSTR buffer, INT len )
749 DWORD size;
750 INT ret;
751 HKEY hkey;
752 NTSTATUS status;
753 UNICODE_STRING nameW;
754 KEY_VALUE_PARTIAL_INFORMATION *info;
755 static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
757 if (!(hkey = create_registry_key())) return -1;
759 RtlInitUnicodeString( &nameW, value );
760 size = info_size + len * sizeof(WCHAR);
762 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
764 NtClose( hkey );
765 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
766 return 0;
769 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
770 if (status == STATUS_BUFFER_OVERFLOW && !buffer) status = 0;
772 if (!status)
774 ret = (size - info_size) / sizeof(WCHAR);
775 /* append terminating null if needed */
776 if (!ret || ((WCHAR *)info->Data)[ret-1])
778 if (ret < len || !buffer) ret++;
779 else
781 SetLastError( ERROR_INSUFFICIENT_BUFFER );
782 ret = 0;
785 if (ret && buffer)
787 memcpy( buffer, info->Data, (ret-1) * sizeof(WCHAR) );
788 buffer[ret-1] = 0;
791 else
793 if (status == STATUS_OBJECT_NAME_NOT_FOUND) ret = -1;
794 else
796 SetLastError( RtlNtStatusToDosError(status) );
797 ret = 0;
800 NtClose( hkey );
801 HeapFree( GetProcessHeap(), 0, info );
802 return ret;
806 /******************************************************************************
807 * GetLocaleInfoA (KERNEL32.@)
809 * Get information about an aspect of a locale.
811 * PARAMS
812 * lcid [I] LCID of the locale
813 * lctype [I] LCTYPE_ flags from "winnls.h"
814 * buffer [O] Destination for the information
815 * len [I] Length of buffer in characters
817 * RETURNS
818 * Success: The size of the data requested. If buffer is non-NULL, it is filled
819 * with the information.
820 * Failure: 0. Use GetLastError() to determine the cause.
822 * NOTES
823 * - LOCALE_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
824 * - The string returned is NUL terminated, except for LOCALE_FONTSIGNATURE,
825 * which is a bit string.
827 INT WINAPI GetLocaleInfoA( LCID lcid, LCTYPE lctype, LPSTR buffer, INT len )
829 WCHAR *bufferW;
830 INT lenW, ret;
832 if (len < 0 || (len && !buffer))
834 SetLastError( ERROR_INVALID_PARAMETER );
835 return 0;
837 if (!len) buffer = NULL;
839 if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
841 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
843 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
844 return 0;
846 if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
848 if ((lctype & LOCALE_RETURN_NUMBER) ||
849 ((lctype & ~LOCALE_LOCALEINFOFLAGSMASK) == LOCALE_FONTSIGNATURE))
851 /* it's not an ASCII string, just bytes */
852 ret *= sizeof(WCHAR);
853 if (buffer)
855 if (ret <= len) memcpy( buffer, bufferW, ret );
856 else
858 SetLastError( ERROR_INSUFFICIENT_BUFFER );
859 ret = 0;
863 else
865 UINT codepage = CP_ACP;
866 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
867 ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
870 HeapFree( GetProcessHeap(), 0, bufferW );
871 return ret;
875 /******************************************************************************
876 * GetLocaleInfoW (KERNEL32.@)
878 * See GetLocaleInfoA.
880 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
882 LANGID lang_id;
883 HRSRC hrsrc;
884 HGLOBAL hmem;
885 HMODULE hModule;
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 hModule = GetModuleHandleW( kernel32W );
949 if (!(hrsrc = FindResourceExW( hModule, (LPWSTR)RT_STRING, (LPCWSTR)((lctype >> 4) + 1), lang_id )))
951 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
952 return 0;
954 if (!(hmem = LoadResource( hModule, 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 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 ret = wine_utf8_wcstombs( src, srclen, dst, dstlen );
1540 break;
1541 default:
1542 if (!(table = get_codepage_table( page )))
1544 SetLastError( ERROR_INVALID_PARAMETER );
1545 return 0;
1547 ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
1548 defchar, used ? &used_tmp : NULL );
1549 if (used) *used = used_tmp;
1550 break;
1553 if (ret < 0)
1555 switch(ret)
1557 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1558 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1560 ret = 0;
1562 TRACE("cp %d %s -> %s\n", page, debugstr_w(src), debugstr_a(dst));
1563 return ret;
1567 /***********************************************************************
1568 * GetThreadLocale (KERNEL32.@)
1570 * Get the current threads locale.
1572 * PARAMS
1573 * None.
1575 * RETURNS
1576 * The LCID currently assocated with the calling thread.
1578 LCID WINAPI GetThreadLocale(void)
1580 LCID ret = NtCurrentTeb()->CurrentLocale;
1581 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
1582 return ret;
1585 /**********************************************************************
1586 * SetThreadLocale (KERNEL32.@)
1588 * Set the current threads locale.
1590 * PARAMS
1591 * lcid [I] LCID of the locale to set
1593 * RETURNS
1594 * Success: TRUE. The threads locale is set to lcid.
1595 * Failure: FALSE. Use GetLastError() to determine the cause.
1597 BOOL WINAPI SetThreadLocale( LCID lcid )
1599 TRACE("(0x%04lX)\n", lcid);
1601 lcid = ConvertDefaultLocale(lcid);
1603 if (lcid != GetThreadLocale())
1605 if (!IsValidLocale(lcid, LCID_SUPPORTED))
1607 SetLastError(ERROR_INVALID_PARAMETER);
1608 return FALSE;
1611 NtCurrentTeb()->CurrentLocale = lcid;
1612 NtCurrentTeb()->code_page = get_lcid_codepage( lcid );
1614 return TRUE;
1617 /******************************************************************************
1618 * ConvertDefaultLocale (KERNEL32.@)
1620 * Convert a default locale identifier into a real identifier.
1622 * PARAMS
1623 * lcid [I] LCID identifier of the locale to convert
1625 * RETURNS
1626 * lcid unchanged, if not a default locale or its sublanguage is
1627 * not SUBLANG_NEUTRAL.
1628 * GetSystemDefaultLCID(), if lcid == LOCALE_SYSTEM_DEFAULT.
1629 * GetUserDefaultLCID(), if lcid == LOCALE_USER_DEFAULT or LOCALE_NEUTRAL.
1630 * Otherwise, lcid with sublanguage changed to SUBLANG_DEFAULT.
1632 LCID WINAPI ConvertDefaultLocale( LCID lcid )
1634 LANGID langid;
1636 switch (lcid)
1638 case LOCALE_SYSTEM_DEFAULT:
1639 lcid = GetSystemDefaultLCID();
1640 break;
1641 case LOCALE_USER_DEFAULT:
1642 case LOCALE_NEUTRAL:
1643 lcid = GetUserDefaultLCID();
1644 break;
1645 default:
1646 /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
1647 langid = LANGIDFROMLCID(lcid);
1648 if (SUBLANGID(langid) == SUBLANG_NEUTRAL)
1650 langid = MAKELANGID(PRIMARYLANGID(langid), SUBLANG_DEFAULT);
1651 lcid = MAKELCID(langid, SORTIDFROMLCID(lcid));
1654 return lcid;
1658 /******************************************************************************
1659 * IsValidLocale (KERNEL32.@)
1661 * Determine if a locale is valid.
1663 * PARAMS
1664 * lcid [I] LCID of the locale to check
1665 * flags [I] LCID_SUPPORTED = Valid, LCID_INSTALLED = Valid and installed on the system
1667 * RETURN
1668 * TRUE, if lcid is valid,
1669 * FALSE, otherwise.
1671 * NOTES
1672 * Wine does not currently make the distinction between supported and installed. All
1673 * languages supported are installed by default.
1675 BOOL WINAPI IsValidLocale( LCID lcid, DWORD flags )
1677 /* check if language is registered in the kernel32 resources */
1678 return FindResourceExW( GetModuleHandleW(kernel32W), (LPWSTR)RT_STRING,
1679 (LPCWSTR)LOCALE_ILANGUAGE, LANGIDFROMLCID(lcid)) != 0;
1683 static BOOL CALLBACK enum_lang_proc_a( HMODULE hModule, LPCSTR type,
1684 LPCSTR name, WORD LangID, LONG lParam )
1686 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
1687 char buf[20];
1689 sprintf(buf, "%08x", (UINT)LangID);
1690 return lpfnLocaleEnum( buf );
1693 static BOOL CALLBACK enum_lang_proc_w( HMODULE hModule, LPCWSTR type,
1694 LPCWSTR name, WORD LangID, LONG lParam )
1696 static const WCHAR formatW[] = {'%','0','8','x',0};
1697 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
1698 WCHAR buf[20];
1699 sprintfW( buf, formatW, (UINT)LangID );
1700 return lpfnLocaleEnum( buf );
1703 /******************************************************************************
1704 * EnumSystemLocalesA (KERNEL32.@)
1706 * Call a users function for each locale available on the system.
1708 * PARAMS
1709 * lpfnLocaleEnum [I] Callback function to call for each locale
1710 * dwFlags [I] LOCALE_SUPPORTED=All supported, LOCALE_INSTALLED=Installed only
1712 * RETURNS
1713 * Success: TRUE.
1714 * Failure: FALSE. Use GetLastError() to determine the cause.
1716 BOOL WINAPI EnumSystemLocalesA( LOCALE_ENUMPROCA lpfnLocaleEnum, DWORD dwFlags )
1718 TRACE("(%p,%08lx)\n", lpfnLocaleEnum, dwFlags);
1719 EnumResourceLanguagesA( GetModuleHandleW(kernel32W), (LPSTR)RT_STRING,
1720 (LPCSTR)LOCALE_ILANGUAGE, enum_lang_proc_a,
1721 (LONG)lpfnLocaleEnum);
1722 return TRUE;
1726 /******************************************************************************
1727 * EnumSystemLocalesW (KERNEL32.@)
1729 * See EnumSystemLocalesA.
1731 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum, DWORD dwFlags )
1733 TRACE("(%p,%08lx)\n", lpfnLocaleEnum, dwFlags);
1734 EnumResourceLanguagesW( GetModuleHandleW(kernel32W), (LPWSTR)RT_STRING,
1735 (LPCWSTR)LOCALE_ILANGUAGE, enum_lang_proc_w,
1736 (LONG)lpfnLocaleEnum);
1737 return TRUE;
1741 /***********************************************************************
1742 * VerLanguageNameA (KERNEL32.@)
1744 * Get the name of a language.
1746 * PARAMS
1747 * wLang [I] LANGID of the language
1748 * szLang [O] Destination for the language name
1750 * RETURNS
1751 * Success: The size of the language name. If szLang is non-NULL, it is filled
1752 * with the name.
1753 * Failure: 0. Use GetLastError() to determine the cause.
1756 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
1758 return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
1762 /***********************************************************************
1763 * VerLanguageNameW (KERNEL32.@)
1765 * See VerLanguageNameA.
1767 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
1769 return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
1773 /******************************************************************************
1774 * GetStringTypeW (KERNEL32.@)
1776 * See GetStringTypeA.
1778 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1780 if (count == -1) count = strlenW(src) + 1;
1781 switch(type)
1783 case CT_CTYPE1:
1784 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
1785 break;
1786 case CT_CTYPE2:
1787 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
1788 break;
1789 case CT_CTYPE3:
1791 WARN("CT_CTYPE3: semi-stub.\n");
1792 while (count--)
1794 int c = *src;
1795 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
1797 type1 = get_char_typeW( *src++ ) & 0xfff;
1798 /* try to construct type3 from type1 */
1799 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
1800 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
1801 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
1802 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
1803 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
1804 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
1805 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
1807 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
1808 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
1809 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
1810 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
1811 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
1812 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
1813 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
1814 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
1816 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
1817 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
1818 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
1819 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
1820 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
1821 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
1822 *chartype++ = type3;
1824 break;
1826 default:
1827 SetLastError( ERROR_INVALID_PARAMETER );
1828 return FALSE;
1830 return TRUE;
1834 /******************************************************************************
1835 * GetStringTypeExW (KERNEL32.@)
1837 * See GetStringTypeExA.
1839 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1841 /* locale is ignored for Unicode */
1842 return GetStringTypeW( type, src, count, chartype );
1846 /******************************************************************************
1847 * GetStringTypeA (KERNEL32.@)
1849 * Get characteristics of the characters making up a string.
1851 * PARAMS
1852 * locale [I] Locale Id for the string
1853 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
1854 * src [I] String to analyse
1855 * count [I] Length of src in chars, or -1 if src is NUL terminated
1856 * chartype [O] Destination for the calculated characteristics
1858 * RETURNS
1859 * Success: TRUE. chartype is filled with the requested characteristics of each char
1860 * in src.
1861 * Failure: FALSE. Use GetLastError() to determine the cause.
1863 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1865 UINT cp;
1866 INT countW;
1867 LPWSTR srcW;
1868 BOOL ret = FALSE;
1870 if(count == -1) count = strlen(src) + 1;
1872 if (!(cp = get_lcid_codepage( locale )))
1874 FIXME("For locale %04lx using current ANSI code page\n", locale);
1875 cp = GetACP();
1878 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
1879 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
1881 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
1883 * NOTE: the target buffer has 1 word for each CHARACTER in the source
1884 * string, with multibyte characters there maybe be more bytes in count
1885 * than character space in the buffer!
1887 ret = GetStringTypeW(type, srcW, countW, chartype);
1888 HeapFree(GetProcessHeap(), 0, srcW);
1890 return ret;
1893 /******************************************************************************
1894 * GetStringTypeExA (KERNEL32.@)
1896 * Get characteristics of the characters making up a string.
1898 * PARAMS
1899 * locale [I] Locale Id for the string
1900 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
1901 * src [I] String to analyse
1902 * count [I] Length of src in chars, or -1 if src is NUL terminated
1903 * chartype [O] Destination for the calculated characteristics
1905 * RETURNS
1906 * Success: TRUE. chartype is filled with the requested characteristics of each char
1907 * in src.
1908 * Failure: FALSE. Use GetLastError() to determine the cause.
1910 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1912 return GetStringTypeA(locale, type, src, count, chartype);
1916 /*************************************************************************
1917 * LCMapStringW (KERNEL32.@)
1919 * See LCMapStringA.
1921 INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
1922 LPWSTR dst, INT dstlen)
1924 LPWSTR dst_ptr;
1926 if (!src || !srclen || dstlen < 0)
1928 SetLastError(ERROR_INVALID_PARAMETER);
1929 return 0;
1932 /* mutually exclusive flags */
1933 if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
1934 (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
1935 (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
1936 (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE))
1938 SetLastError(ERROR_INVALID_FLAGS);
1939 return 0;
1942 if (!dstlen) dst = NULL;
1944 lcid = ConvertDefaultLocale(lcid);
1946 if (flags & LCMAP_SORTKEY)
1948 if (src == dst)
1950 SetLastError(ERROR_INVALID_FLAGS);
1951 return 0;
1954 if (srclen < 0) srclen = strlenW(src);
1956 TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1957 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
1959 return wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
1962 /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
1963 if (flags & SORT_STRINGSORT)
1965 SetLastError(ERROR_INVALID_FLAGS);
1966 return 0;
1969 if (srclen < 0) srclen = strlenW(src) + 1;
1971 TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1972 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
1974 if (!dst) /* return required string length */
1976 INT len;
1978 for (len = 0; srclen; src++, srclen--)
1980 WCHAR wch = *src;
1981 /* tests show that win2k just ignores NORM_IGNORENONSPACE,
1982 * and skips white space and punctuation characters for
1983 * NORM_IGNORESYMBOLS.
1985 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1986 continue;
1987 len++;
1989 return len;
1992 if (flags & LCMAP_UPPERCASE)
1994 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
1996 WCHAR wch = *src;
1997 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1998 continue;
1999 *dst_ptr++ = toupperW(wch);
2000 dstlen--;
2003 else if (flags & LCMAP_LOWERCASE)
2005 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2007 WCHAR wch = *src;
2008 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2009 continue;
2010 *dst_ptr++ = tolowerW(wch);
2011 dstlen--;
2014 else
2016 if (src == dst)
2018 SetLastError(ERROR_INVALID_FLAGS);
2019 return 0;
2021 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2023 WCHAR wch = *src;
2024 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2025 continue;
2026 *dst_ptr++ = wch;
2027 dstlen--;
2031 if (srclen)
2033 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2034 return 0;
2037 return dst_ptr - dst;
2040 /*************************************************************************
2041 * LCMapStringA (KERNEL32.@)
2043 * Map characters in a locale sensitive string.
2045 * PARAMS
2046 * lcid [I] LCID for the conversion.
2047 * flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
2048 * src [I] String to map
2049 * srclen [I] Length of src in chars, or -1 if src is NUL terminated
2050 * dst [O] Destination for mapped string
2051 * dstlen [I] Length of dst in characters
2053 * RETURNS
2054 * Success: The length of the mapped string in dst, including the NUL terminator.
2055 * Failure: 0. Use GetLastError() to determine the cause.
2057 INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
2058 LPSTR dst, INT dstlen)
2060 WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
2061 LPWSTR srcW, dstW;
2062 INT ret = 0, srclenW, dstlenW;
2063 UINT locale_cp;
2065 if (!src || !srclen || dstlen < 0)
2067 SetLastError(ERROR_INVALID_PARAMETER);
2068 return 0;
2071 locale_cp = get_lcid_codepage(lcid);
2073 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
2074 if (srclenW)
2075 srcW = bufW;
2076 else
2078 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
2079 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2080 if (!srcW)
2082 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2083 return 0;
2085 MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
2088 if (flags & LCMAP_SORTKEY)
2090 if (src == dst)
2092 SetLastError(ERROR_INVALID_FLAGS);
2093 goto map_string_exit;
2095 ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
2096 goto map_string_exit;
2099 if (flags & SORT_STRINGSORT)
2101 SetLastError(ERROR_INVALID_FLAGS);
2102 goto map_string_exit;
2105 dstlenW = LCMapStringW(lcid, flags, srcW, srclenW, NULL, 0);
2106 if (!dstlenW)
2107 goto map_string_exit;
2109 dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
2110 if (!dstW)
2112 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2113 goto map_string_exit;
2116 LCMapStringW(lcid, flags, srcW, srclenW, dstW, dstlenW);
2117 ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
2118 HeapFree(GetProcessHeap(), 0, dstW);
2120 map_string_exit:
2121 if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
2122 return ret;
2125 /*************************************************************************
2126 * FoldStringA (KERNEL32.@)
2128 * Map characters in a string.
2130 * PARAMS
2131 * dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
2132 * src [I] String to map
2133 * srclen [I] Length of src, or -1 if src is NUL terminated
2134 * dst [O] Destination for mapped string
2135 * dstlen [I] Length of dst, or 0 to find the required length for the mapped string
2137 * RETURNS
2138 * Success: The length of the string written to dst, including the terminating NUL. If
2139 * dstlen is 0, the value returned is the same, but nothing is written to dst,
2140 * and dst may be NULL.
2141 * Failure: 0. Use GetLastError() to determine the cause.
2143 INT WINAPI FoldStringA(DWORD dwFlags, LPCSTR src, INT srclen,
2144 LPSTR dst, INT dstlen)
2146 INT ret = 0, srclenW = 0;
2147 WCHAR *srcW = NULL, *dstW = NULL;
2149 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2151 SetLastError(ERROR_INVALID_PARAMETER);
2152 return 0;
2155 srclenW = MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2156 src, srclen, NULL, 0);
2157 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2159 if (!srcW)
2161 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2162 goto FoldStringA_exit;
2165 MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2166 src, srclen, srcW, srclenW);
2168 dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
2170 ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
2171 if (ret && dstlen)
2173 dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
2175 if (!dstW)
2177 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2178 goto FoldStringA_exit;
2181 ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
2182 if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
2184 ret = 0;
2185 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2189 if (dstW)
2190 HeapFree(GetProcessHeap(), 0, dstW);
2192 FoldStringA_exit:
2193 if (srcW)
2194 HeapFree(GetProcessHeap(), 0, srcW);
2195 return ret;
2198 /*************************************************************************
2199 * FoldStringW (KERNEL32.@)
2201 * See FoldStringA.
2203 INT WINAPI FoldStringW(DWORD dwFlags, LPCWSTR src, INT srclen,
2204 LPWSTR dst, INT dstlen)
2206 int ret;
2208 switch (dwFlags & (MAP_COMPOSITE|MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES))
2210 case 0:
2211 if (dwFlags)
2212 break;
2213 /* Fall through for dwFlags == 0 */
2214 case MAP_PRECOMPOSED|MAP_COMPOSITE:
2215 case MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES:
2216 case MAP_COMPOSITE|MAP_EXPAND_LIGATURES:
2217 SetLastError(ERROR_INVALID_FLAGS);
2218 return 0;
2221 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2223 SetLastError(ERROR_INVALID_PARAMETER);
2224 return 0;
2227 ret = wine_fold_string(dwFlags, src, srclen, dst, dstlen);
2228 if (!ret)
2229 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2230 return ret;
2233 /******************************************************************************
2234 * CompareStringW (KERNEL32.@)
2236 * See CompareStringA.
2238 INT WINAPI CompareStringW(LCID lcid, DWORD style,
2239 LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
2241 INT ret;
2243 if (!str1 || !str2)
2245 SetLastError(ERROR_INVALID_PARAMETER);
2246 return 0;
2249 if( style & ~(NORM_IGNORECASE|NORM_IGNORENONSPACE|NORM_IGNORESYMBOLS|
2250 SORT_STRINGSORT|NORM_IGNOREKANATYPE|NORM_IGNOREWIDTH|0x10000000) )
2252 SetLastError(ERROR_INVALID_FLAGS);
2253 return 0;
2256 if (style & 0x10000000)
2257 FIXME("Ignoring unknown style 0x10000000\n");
2259 if (len1 < 0) len1 = strlenW(str1);
2260 if (len2 < 0) len2 = strlenW(str2);
2262 ret = wine_compare_string(style, str1, len1, str2, len2);
2264 if (ret) /* need to translate result */
2265 return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
2266 return CSTR_EQUAL;
2269 /******************************************************************************
2270 * CompareStringA (KERNEL32.@)
2272 * Compare two locale sensitive strings.
2274 * PARAMS
2275 * lcid [I] LCID for the comparason
2276 * style [I] Flags for the comparason (NORM_ constants from "winnls.h").
2277 * str1 [I] First string to compare
2278 * len1 [I] Length of str1, or -1 if str1 is NUL terminated
2279 * str2 [I] Second string to compare
2280 * len2 [I] Length of str2, or -1 if str2 is NUL terminated
2282 * RETURNS
2283 * Success: CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN depending on whether
2284 * str2 is less than, equal to or greater than str1 respectively.
2285 * Failure: FALSE. Use GetLastError() to determine the cause.
2287 INT WINAPI CompareStringA(LCID lcid, DWORD style,
2288 LPCSTR str1, INT len1, LPCSTR str2, INT len2)
2290 WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
2291 WCHAR *buf2W = buf1W + 130;
2292 LPWSTR str1W, str2W;
2293 INT len1W, len2W, ret;
2294 UINT locale_cp;
2296 if (!str1 || !str2)
2298 SetLastError(ERROR_INVALID_PARAMETER);
2299 return 0;
2301 if (len1 < 0) len1 = strlen(str1);
2302 if (len2 < 0) len2 = strlen(str2);
2304 locale_cp = get_lcid_codepage(lcid);
2306 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 130);
2307 if (len1W)
2308 str1W = buf1W;
2309 else
2311 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
2312 str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
2313 if (!str1W)
2315 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2316 return 0;
2318 MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
2320 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 130);
2321 if (len2W)
2322 str2W = buf2W;
2323 else
2325 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
2326 str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
2327 if (!str2W)
2329 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2330 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2331 return 0;
2333 MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
2336 ret = CompareStringW(lcid, style, str1W, len1W, str2W, len2W);
2338 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2339 if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
2340 return ret;
2343 /*************************************************************************
2344 * lstrcmp (KERNEL32.@)
2345 * lstrcmpA (KERNEL32.@)
2347 * Compare two strings using the current thread locale.
2349 * PARAMS
2350 * str1 [I] First string to compare
2351 * str2 [I] Second string to compare
2353 * RETURNS
2354 * Success: A number less than, equal to or greater than 0 depending on whether
2355 * str2 is less than, equal to or greater than str1 respectively.
2356 * Failure: FALSE. Use GetLastError() to determine the cause.
2358 int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
2360 int ret = CompareStringA(GetThreadLocale(), 0, str1, -1, str2, -1);
2361 if (ret) ret -= 2;
2362 return ret;
2365 /*************************************************************************
2366 * lstrcmpi (KERNEL32.@)
2367 * lstrcmpiA (KERNEL32.@)
2369 * Compare two strings using the current thread locale, ignoring case.
2371 * PARAMS
2372 * str1 [I] First string to compare
2373 * str2 [I] Second string to compare
2375 * RETURNS
2376 * Success: A number less than, equal to or greater than 0 depending on whether
2377 * str2 is less than, equal to or greater than str1 respectively.
2378 * Failure: FALSE. Use GetLastError() to determine the cause.
2380 int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
2382 int ret = CompareStringA(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2383 if (ret) ret -= 2;
2384 return ret;
2387 /*************************************************************************
2388 * lstrcmpW (KERNEL32.@)
2390 * See lstrcmpA.
2392 int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
2394 int ret = CompareStringW(GetThreadLocale(), 0, str1, -1, str2, -1);
2395 if (ret) ret -= 2;
2396 return ret;
2399 /*************************************************************************
2400 * lstrcmpiW (KERNEL32.@)
2402 * See lstrcmpiA.
2404 int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
2406 int ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2407 if (ret) ret -= 2;
2408 return ret;
2411 /******************************************************************************
2412 * LOCALE_Init
2414 void LOCALE_Init(void)
2416 extern void __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp,
2417 const union cptable *unix_cp );
2419 UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp = ~0U;
2420 LCID lcid = init_default_lcid( &unix_cp );
2422 NtSetDefaultLocale( FALSE, lcid );
2423 NtSetDefaultLocale( TRUE, lcid );
2425 ansi_cp = get_lcid_codepage(lcid);
2426 GetLocaleInfoW( lcid, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
2427 (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
2428 GetLocaleInfoW( lcid, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
2429 (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
2430 if (unix_cp == ~0U)
2431 GetLocaleInfoW( lcid, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
2432 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) );
2434 if (!(ansi_cptable = wine_cp_get_table( ansi_cp )))
2435 ansi_cptable = wine_cp_get_table( 1252 );
2436 if (!(oem_cptable = wine_cp_get_table( oem_cp )))
2437 oem_cptable = wine_cp_get_table( 437 );
2438 if (!(mac_cptable = wine_cp_get_table( mac_cp )))
2439 mac_cptable = wine_cp_get_table( 10000 );
2440 if (unix_cp != CP_UTF8)
2442 if (!(unix_cptable = wine_cp_get_table( unix_cp )))
2443 unix_cptable = wine_cp_get_table( 28591 );
2446 __wine_init_codepages( ansi_cptable, oem_cptable, unix_cptable );
2448 TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
2449 ansi_cptable->info.codepage, oem_cptable->info.codepage,
2450 mac_cptable->info.codepage, unix_cp );
2453 static HKEY NLS_RegOpenKey(HKEY hRootKey, LPCWSTR szKeyName)
2455 UNICODE_STRING keyName;
2456 OBJECT_ATTRIBUTES attr;
2457 HKEY hkey;
2459 RtlInitUnicodeString( &keyName, szKeyName );
2460 InitializeObjectAttributes(&attr, &keyName, 0, hRootKey, NULL);
2462 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS)
2463 hkey = 0;
2465 return hkey;
2468 static HKEY NLS_RegOpenSubKey(HKEY hRootKey, LPCWSTR szKeyName)
2470 HKEY hKey = NLS_RegOpenKey(hRootKey, szKeyName);
2472 if (hRootKey)
2473 NtClose( hRootKey );
2475 return hKey;
2478 static BOOL NLS_RegEnumSubKey(HKEY hKey, UINT ulIndex, LPWSTR szKeyName,
2479 ULONG keyNameSize)
2481 BYTE buffer[80];
2482 KEY_BASIC_INFORMATION *info = (KEY_BASIC_INFORMATION *)buffer;
2483 DWORD dwLen;
2485 if (NtEnumerateKey( hKey, ulIndex, KeyBasicInformation, buffer,
2486 sizeof(buffer), &dwLen) != STATUS_SUCCESS ||
2487 info->NameLength > keyNameSize)
2489 return FALSE;
2492 TRACE("info->Name %s info->NameLength %ld\n", debugstr_w(info->Name), info->NameLength);
2494 memcpy( szKeyName, info->Name, info->NameLength);
2495 szKeyName[info->NameLength / sizeof(WCHAR)] = '\0';
2497 TRACE("returning %s\n", debugstr_w(szKeyName));
2498 return TRUE;
2501 static BOOL NLS_RegEnumValue(HKEY hKey, UINT ulIndex,
2502 LPWSTR szValueName, ULONG valueNameSize,
2503 LPWSTR szValueData, ULONG valueDataSize)
2505 BYTE buffer[80];
2506 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2507 DWORD dwLen;
2509 if (NtEnumerateValueKey( hKey, ulIndex, KeyValueFullInformation,
2510 buffer, sizeof(buffer), &dwLen ) != STATUS_SUCCESS ||
2511 info->NameLength > valueNameSize ||
2512 info->DataLength > valueDataSize)
2514 return FALSE;
2517 TRACE("info->Name %s info->DataLength %ld\n", debugstr_w(info->Name), info->DataLength);
2519 memcpy( szValueName, info->Name, info->NameLength);
2520 szValueName[info->NameLength / sizeof(WCHAR)] = '\0';
2521 memcpy( szValueData, buffer + info->DataOffset, info->DataLength );
2522 szValueData[info->DataLength / sizeof(WCHAR)] = '\0';
2524 TRACE("returning %s %s\n", debugstr_w(szValueName), debugstr_w(szValueData));
2525 return TRUE;
2528 static BOOL NLS_RegGetDword(HKEY hKey, LPCWSTR szValueName, DWORD *lpVal)
2530 BYTE buffer[128];
2531 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
2532 DWORD dwSize = sizeof(buffer);
2533 UNICODE_STRING valueName;
2535 RtlInitUnicodeString( &valueName, szValueName );
2537 TRACE("%p, %s\n", hKey, debugstr_w(szValueName));
2538 if (NtQueryValueKey( hKey, &valueName, KeyValuePartialInformation,
2539 buffer, dwSize, &dwSize ) == STATUS_SUCCESS &&
2540 info->DataLength == sizeof(DWORD))
2542 memcpy(lpVal, info->Data, sizeof(DWORD));
2543 return TRUE;
2546 return FALSE;
2549 static BOOL NLS_GetLanguageGroupName(LGRPID lgrpid, LPWSTR szName, ULONG nameSize)
2551 HMODULE hModule = GetModuleHandleW(kernel32W);
2552 LANGID langId;
2553 LPCWSTR szResourceName = (LPCWSTR)(((lgrpid + 0x2000) >> 4) + 1);
2554 HRSRC hResource;
2555 BOOL bRet = FALSE;
2557 /* FIXME: Is it correct to use the system default langid? */
2558 langId = GetSystemDefaultLangID();
2560 if (SUBLANGID(langId) == SUBLANG_NEUTRAL)
2561 langId = MAKELANGID( PRIMARYLANGID(langId), SUBLANG_DEFAULT );
2563 hResource = FindResourceExW( hModule, (LPWSTR)RT_STRING, szResourceName, langId );
2565 if (hResource)
2567 HGLOBAL hResDir = LoadResource( hModule, hResource );
2569 if (hResDir)
2571 ULONG iResourceIndex = lgrpid & 0xf;
2572 LPCWSTR lpResEntry = LockResource( hResDir );
2573 ULONG i;
2575 for (i = 0; i < iResourceIndex; i++)
2576 lpResEntry += *lpResEntry + 1;
2578 if (*lpResEntry < nameSize)
2580 memcpy( szName, lpResEntry + 1, *lpResEntry * sizeof(WCHAR) );
2581 szName[*lpResEntry] = '\0';
2582 bRet = TRUE;
2586 FreeResource( hResource );
2588 return bRet;
2591 /* Registry keys for NLS related information */
2592 static const WCHAR szNlsKeyName[] = {
2593 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
2594 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
2595 'C','o','n','t','r','o','l','\\','N','l','s','\0'
2598 static const WCHAR szLangGroupsKeyName[] = {
2599 'L','a','n','g','u','a','g','e',' ','G','r','o','u','p','s','\0'
2602 static const WCHAR szCountryListName[] = {
2603 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
2604 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
2605 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2606 'T','e','l','e','p','h','o','n','y','\\',
2607 'C','o','u','n','t','r','y',' ','L','i','s','t','\0'
2611 /* Callback function ptrs for EnumSystemLanguageGroupsA/W */
2612 typedef struct
2614 LANGUAGEGROUP_ENUMPROCA procA;
2615 LANGUAGEGROUP_ENUMPROCW procW;
2616 DWORD dwFlags;
2617 LONG_PTR lParam;
2618 } ENUMLANGUAGEGROUP_CALLBACKS;
2620 /* Internal implementation of EnumSystemLanguageGroupsA/W */
2621 static BOOL NLS_EnumSystemLanguageGroups(ENUMLANGUAGEGROUP_CALLBACKS *lpProcs)
2623 WCHAR szNumber[10], szValue[4];
2624 HKEY hKey;
2625 BOOL bContinue = TRUE;
2626 ULONG ulIndex = 0;
2628 if (!lpProcs)
2630 SetLastError(ERROR_INVALID_PARAMETER);
2631 return FALSE;
2634 switch (lpProcs->dwFlags)
2636 case 0:
2637 /* Default to LGRPID_INSTALLED */
2638 lpProcs->dwFlags = LGRPID_INSTALLED;
2639 /* Fall through... */
2640 case LGRPID_INSTALLED:
2641 case LGRPID_SUPPORTED:
2642 break;
2643 default:
2644 SetLastError(ERROR_INVALID_FLAGS);
2645 return FALSE;
2648 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLangGroupsKeyName );
2650 if (!hKey)
2651 WARN("NLS registry key not found. Please apply the default registry file 'winedefault.reg'\n");
2653 while (bContinue)
2655 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
2656 szValue, sizeof(szValue) ))
2658 BOOL bInstalled = szValue[0] == '1' ? TRUE : FALSE;
2659 LGRPID lgrpid = strtoulW( szNumber, NULL, 16 );
2661 TRACE("grpid %s (%sinstalled)\n", debugstr_w(szNumber),
2662 bInstalled ? "" : "not ");
2664 if (lpProcs->dwFlags == LGRPID_SUPPORTED || bInstalled)
2666 WCHAR szGrpName[48];
2668 if (!NLS_GetLanguageGroupName( lgrpid, szGrpName, sizeof(szGrpName) / sizeof(WCHAR) ))
2669 szGrpName[0] = '\0';
2671 if (lpProcs->procW)
2672 bContinue = lpProcs->procW( lgrpid, szNumber, szGrpName, lpProcs->dwFlags,
2673 lpProcs->lParam );
2674 else
2676 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2677 char szGrpNameA[48];
2679 /* FIXME: MSDN doesn't say which code page the W->A translation uses,
2680 * or whether the language names are ever localised. Assume CP_ACP.
2683 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2684 WideCharToMultiByte(CP_ACP, 0, szGrpName, -1, szGrpNameA, sizeof(szGrpNameA), 0, 0);
2686 bContinue = lpProcs->procA( lgrpid, szNumberA, szGrpNameA, lpProcs->dwFlags,
2687 lpProcs->lParam );
2691 ulIndex++;
2693 else
2694 bContinue = FALSE;
2696 if (!bContinue)
2697 break;
2700 if (hKey)
2701 NtClose( hKey );
2703 return TRUE;
2706 /******************************************************************************
2707 * EnumSystemLanguageGroupsA (KERNEL32.@)
2709 * Call a users function for each language group available on the system.
2711 * PARAMS
2712 * pLangGrpEnumProc [I] Callback function to call for each language group
2713 * dwFlags [I] LGRPID_SUPPORTED=All Supported, LGRPID_INSTALLED=Installed only
2714 * lParam [I] User parameter to pass to pLangGrpEnumProc
2716 * RETURNS
2717 * Success: TRUE.
2718 * Failure: FALSE. Use GetLastError() to determine the cause.
2720 BOOL WINAPI EnumSystemLanguageGroupsA(LANGUAGEGROUP_ENUMPROCA pLangGrpEnumProc,
2721 DWORD dwFlags, LONG_PTR lParam)
2723 ENUMLANGUAGEGROUP_CALLBACKS procs;
2725 TRACE("(%p,0x%08lX,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
2727 procs.procA = pLangGrpEnumProc;
2728 procs.procW = NULL;
2729 procs.dwFlags = dwFlags;
2730 procs.lParam = lParam;
2732 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
2735 /******************************************************************************
2736 * EnumSystemLanguageGroupsW (KERNEL32.@)
2738 * See EnumSystemLanguageGroupsA.
2740 BOOL WINAPI EnumSystemLanguageGroupsW(LANGUAGEGROUP_ENUMPROCW pLangGrpEnumProc,
2741 DWORD dwFlags, LONG_PTR lParam)
2743 ENUMLANGUAGEGROUP_CALLBACKS procs;
2745 TRACE("(%p,0x%08lX,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
2747 procs.procA = NULL;
2748 procs.procW = pLangGrpEnumProc;
2749 procs.dwFlags = dwFlags;
2750 procs.lParam = lParam;
2752 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
2755 /******************************************************************************
2756 * IsValidLanguageGroup (KERNEL32.@)
2758 * Determine if a language group is supported and/or installed.
2760 * PARAMS
2761 * lgrpid [I] Language Group Id (LGRPID_ values from "winnls.h")
2762 * dwFlags [I] LGRPID_SUPPORTED=Supported, LGRPID_INSTALLED=Installed
2764 * RETURNS
2765 * TRUE, if lgrpid is supported and/or installed, according to dwFlags.
2766 * FALSE otherwise.
2768 BOOL WINAPI IsValidLanguageGroup(LGRPID lgrpid, DWORD dwFlags)
2770 static const WCHAR szFormat[] = { '%','x','\0' };
2771 WCHAR szValueName[16], szValue[2];
2772 BOOL bSupported = FALSE, bInstalled = FALSE;
2773 HKEY hKey;
2776 switch (dwFlags)
2778 case LGRPID_INSTALLED:
2779 case LGRPID_SUPPORTED:
2781 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLangGroupsKeyName );
2783 sprintfW( szValueName, szFormat, lgrpid );
2785 if (NLS_RegGetDword( hKey, szValueName, (LPDWORD)&szValue ))
2787 bSupported = TRUE;
2789 if (szValue[0] == '1')
2790 bInstalled = TRUE;
2793 if (hKey)
2794 NtClose( hKey );
2796 break;
2799 if ((dwFlags == LGRPID_SUPPORTED && bSupported) ||
2800 (dwFlags == LGRPID_INSTALLED && bInstalled))
2801 return TRUE;
2803 return FALSE;
2806 /* Callback function ptrs for EnumLanguageGrouplocalesA/W */
2807 typedef struct
2809 LANGGROUPLOCALE_ENUMPROCA procA;
2810 LANGGROUPLOCALE_ENUMPROCW procW;
2811 DWORD dwFlags;
2812 LGRPID lgrpid;
2813 LONG_PTR lParam;
2814 } ENUMLANGUAGEGROUPLOCALE_CALLBACKS;
2816 /* Internal implementation of EnumLanguageGrouplocalesA/W */
2817 static BOOL NLS_EnumLanguageGroupLocales(ENUMLANGUAGEGROUPLOCALE_CALLBACKS *lpProcs)
2819 static const WCHAR szLocaleKeyName[] = {
2820 'L','o','c','a','l','e','\0'
2822 static const WCHAR szAlternateSortsKeyName[] = {
2823 'A','l','t','e','r','n','a','t','e',' ','S','o','r','t','s','\0'
2825 WCHAR szNumber[10], szValue[4];
2826 HKEY hKey;
2827 BOOL bContinue = TRUE, bAlternate = FALSE;
2828 LGRPID lgrpid;
2829 ULONG ulIndex = 1; /* Ignore default entry of 1st key */
2831 if (!lpProcs || !lpProcs->lgrpid || lpProcs->lgrpid > LGRPID_ARMENIAN)
2833 SetLastError(ERROR_INVALID_PARAMETER);
2834 return FALSE;
2837 if (lpProcs->dwFlags)
2839 SetLastError(ERROR_INVALID_FLAGS);
2840 return FALSE;
2843 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLocaleKeyName );
2845 if (!hKey)
2846 WARN("NLS registry key not found. Please apply the default registry file 'winedefault.reg'\n");
2848 while (bContinue)
2850 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
2851 szValue, sizeof(szValue) ))
2853 lgrpid = strtoulW( szValue, NULL, 16 );
2855 TRACE("lcid %s, grpid %ld (%smatched)\n", debugstr_w(szNumber),
2856 lgrpid, lgrpid == lpProcs->lgrpid ? "" : "not ");
2858 if (lgrpid == lpProcs->lgrpid)
2860 LCID lcid;
2862 lcid = strtoulW( szNumber, NULL, 16 );
2864 /* FIXME: native returns extra text for a few (17/150) locales, e.g:
2865 * '00000437 ;Georgian'
2866 * At present we only pass the LCID string.
2869 if (lpProcs->procW)
2870 bContinue = lpProcs->procW( lgrpid, lcid, szNumber, lpProcs->lParam );
2871 else
2873 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2875 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2877 bContinue = lpProcs->procA( lgrpid, lcid, szNumberA, lpProcs->lParam );
2881 ulIndex++;
2883 else
2885 /* Finished enumerating this key */
2886 if (!bAlternate)
2888 /* Enumerate alternate sorts also */
2889 hKey = NLS_RegOpenKey( hKey, szAlternateSortsKeyName );
2890 bAlternate = TRUE;
2891 ulIndex = 0;
2893 else
2894 bContinue = FALSE; /* Finished both keys */
2897 if (!bContinue)
2898 break;
2901 if (hKey)
2902 NtClose( hKey );
2904 return TRUE;
2907 /******************************************************************************
2908 * EnumLanguageGroupLocalesA (KERNEL32.@)
2910 * Call a users function for every locale in a language group available on the system.
2912 * PARAMS
2913 * pLangGrpLcEnumProc [I] Callback function to call for each locale
2914 * lgrpid [I] Language group (LGRPID_ values from "winnls.h")
2915 * dwFlags [I] Reserved, set to 0
2916 * lParam [I] User parameter to pass to pLangGrpLcEnumProc
2918 * RETURNS
2919 * Success: TRUE.
2920 * Failure: FALSE. Use GetLastError() to determine the cause.
2922 BOOL WINAPI EnumLanguageGroupLocalesA(LANGGROUPLOCALE_ENUMPROCA pLangGrpLcEnumProc,
2923 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
2925 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
2927 TRACE("(%p,0x%08lX,0x%08lX,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
2929 callbacks.procA = pLangGrpLcEnumProc;
2930 callbacks.procW = NULL;
2931 callbacks.dwFlags = dwFlags;
2932 callbacks.lgrpid = lgrpid;
2933 callbacks.lParam = lParam;
2935 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
2938 /******************************************************************************
2939 * EnumLanguageGroupLocalesW (KERNEL32.@)
2941 * See EnumLanguageGroupLocalesA.
2943 BOOL WINAPI EnumLanguageGroupLocalesW(LANGGROUPLOCALE_ENUMPROCW pLangGrpLcEnumProc,
2944 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
2946 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
2948 TRACE("(%p,0x%08lX,0x%08lX,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
2950 callbacks.procA = NULL;
2951 callbacks.procW = pLangGrpLcEnumProc;
2952 callbacks.dwFlags = dwFlags;
2953 callbacks.lgrpid = lgrpid;
2954 callbacks.lParam = lParam;
2956 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
2959 /******************************************************************************
2960 * EnumSystemGeoID (KERNEL32.@)
2962 * Call a users function for every location available on the system.
2964 * PARAMS
2965 * geoclass [I] Type of information desired (SYSGEOTYPE enum from "winnls.h")
2966 * reserved [I] Reserved, set to 0
2967 * pGeoEnumProc [I] Callback function to call for each location
2969 * RETURNS
2970 * Success: TRUE.
2971 * Failure: FALSE. Use GetLastError() to determine the cause.
2973 BOOL WINAPI EnumSystemGeoID(GEOCLASS geoclass, GEOID reserved, GEO_ENUMPROC pGeoEnumProc)
2975 static const WCHAR szCountryCodeValueName[] = {
2976 'C','o','u','n','t','r','y','C','o','d','e','\0'
2978 WCHAR szNumber[10];
2979 HKEY hKey;
2980 ULONG ulIndex = 0;
2982 TRACE("(0x%08lX,0x%08lX,%p)\n", geoclass, reserved, pGeoEnumProc);
2984 if (geoclass != GEOCLASS_NATION || reserved || !pGeoEnumProc)
2986 SetLastError(ERROR_INVALID_PARAMETER);
2987 return FALSE;
2990 hKey = NLS_RegOpenKey( 0, szCountryListName );
2992 while (NLS_RegEnumSubKey( hKey, ulIndex, szNumber, sizeof(szNumber) ))
2994 BOOL bContinue = TRUE;
2995 DWORD dwGeoId;
2996 HKEY hSubKey = NLS_RegOpenKey( hKey, szNumber );
2998 if (hSubKey)
3000 if (NLS_RegGetDword( hSubKey, szCountryCodeValueName, &dwGeoId ))
3002 TRACE("Got geoid %ld\n", dwGeoId);
3004 if (!pGeoEnumProc( dwGeoId ))
3005 bContinue = FALSE;
3008 NtClose( hSubKey );
3011 if (!bContinue)
3012 break;
3014 ulIndex++;
3017 if (hKey)
3018 NtClose( hKey );
3020 return TRUE;
3023 /******************************************************************************
3024 * InvalidateNLSCache (KERNEL32.@)
3026 * Invalidate the cache of NLS values.
3028 * PARAMS
3029 * None.
3031 * RETURNS
3032 * Success: TRUE.
3033 * Failure: FALSE.
3035 BOOL WINAPI InvalidateNLSCache(void)
3037 FIXME("() stub\n");
3038 return FALSE;
3041 /******************************************************************************
3042 * GetUserGeoID (KERNEL32.@)
3044 GEOID WINAPI GetUserGeoID( GEOCLASS GeoClass )
3046 FIXME("%ld\n",GeoClass);
3047 return GEOID_NOT_AVAILABLE;
3050 /******************************************************************************
3051 * SetUserGeoID (KERNEL32.@)
3053 BOOL WINAPI SetUserGeoID( GEOID GeoID )
3055 FIXME("%ld\n",GeoID);
3056 return FALSE;