push a18c97e3310ae68c9cf1d09430f25ca292f320eb
[wine/hacks.git] / dlls / kernel32 / locale.c
bloba13c346b2d58f8d8ceb460f9a36aa2f99e48e776
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <assert.h>
28 #include <locale.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <stdlib.h>
35 #ifdef __APPLE__
36 # include <CoreFoundation/CFBundle.h>
37 # include <CoreFoundation/CFLocale.h>
38 # include <CoreFoundation/CFString.h>
39 #endif
41 #include "ntstatus.h"
42 #define WIN32_NO_STATUS
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winuser.h" /* for RT_STRINGW */
46 #include "winternl.h"
47 #include "wine/unicode.h"
48 #include "winnls.h"
49 #include "winerror.h"
50 #include "winver.h"
51 #include "kernel_private.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(nls);
56 #define LOCALE_LOCALEINFOFLAGSMASK (LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP|LOCALE_RETURN_NUMBER)
58 /* current code pages */
59 static const union cptable *ansi_cptable;
60 static const union cptable *oem_cptable;
61 static const union cptable *mac_cptable;
62 static const union cptable *unix_cptable; /* NULL if UTF8 */
64 static HANDLE NLS_RegOpenKey(HANDLE hRootKey, LPCWSTR szKeyName);
66 static const WCHAR szNlsKeyName[] = {
67 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
68 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
69 'C','o','n','t','r','o','l','\\','N','l','s','\0'
72 static const WCHAR szLocaleKeyName[] = {
73 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
74 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
75 'C','o','n','t','r','o','l','\\','N','l','s','\\','L','o','c','a','l','e',0
78 static const WCHAR szCodepageKeyName[] = {
79 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
80 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
81 'C','o','n','t','r','o','l','\\','N','l','s','\\','C','o','d','e','p','a','g','e',0
84 static const WCHAR szLangGroupsKeyName[] = {
85 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
86 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
87 'C','o','n','t','r','o','l','\\','N','l','s','\\',
88 'L','a','n','g','u','a','g','e',' ','G','r','o','u','p','s',0
91 /* Charset to codepage map, sorted by name. */
92 static const struct charset_entry
94 const char *charset_name;
95 UINT codepage;
96 } charset_names[] =
98 { "BIG5", 950 },
99 { "CP1250", 1250 },
100 { "CP1251", 1251 },
101 { "CP1252", 1252 },
102 { "CP1253", 1253 },
103 { "CP1254", 1254 },
104 { "CP1255", 1255 },
105 { "CP1256", 1256 },
106 { "CP1257", 1257 },
107 { "CP1258", 1258 },
108 { "CP932", 932 },
109 { "CP936", 936 },
110 { "CP949", 949 },
111 { "CP950", 950 },
112 { "EUCJP", 20932 },
113 { "GB2312", 936 },
114 { "IBM037", 37 },
115 { "IBM1026", 1026 },
116 { "IBM424", 424 },
117 { "IBM437", 437 },
118 { "IBM500", 500 },
119 { "IBM850", 850 },
120 { "IBM852", 852 },
121 { "IBM855", 855 },
122 { "IBM857", 857 },
123 { "IBM860", 860 },
124 { "IBM861", 861 },
125 { "IBM862", 862 },
126 { "IBM863", 863 },
127 { "IBM864", 864 },
128 { "IBM865", 865 },
129 { "IBM866", 866 },
130 { "IBM869", 869 },
131 { "IBM874", 874 },
132 { "IBM875", 875 },
133 { "ISO88591", 28591 },
134 { "ISO885910", 28600 },
135 { "ISO885913", 28603 },
136 { "ISO885914", 28604 },
137 { "ISO885915", 28605 },
138 { "ISO885916", 28606 },
139 { "ISO88592", 28592 },
140 { "ISO88593", 28593 },
141 { "ISO88594", 28594 },
142 { "ISO88595", 28595 },
143 { "ISO88596", 28596 },
144 { "ISO88597", 28597 },
145 { "ISO88598", 28598 },
146 { "ISO88599", 28599 },
147 { "KOI8R", 20866 },
148 { "KOI8U", 21866 },
149 { "UTF8", CP_UTF8 }
153 struct locale_name
155 WCHAR win_name[128]; /* Windows name ("en-US") */
156 WCHAR lang[128]; /* language ("en") (note: buffer contains the other strings too) */
157 WCHAR *country; /* country ("US") */
158 WCHAR *charset; /* charset ("UTF-8") for Unix format only */
159 WCHAR *script; /* script ("Latn") for Windows format only */
160 WCHAR *modifier; /* modifier or sort order */
161 LCID lcid; /* corresponding LCID */
162 int matches; /* number of elements matching LCID (0..4) */
163 UINT codepage; /* codepage corresponding to charset */
166 /* locale ids corresponding to the various Unix locale parameters */
167 static LCID lcid_LC_COLLATE;
168 static LCID lcid_LC_CTYPE;
169 static LCID lcid_LC_MESSAGES;
170 static LCID lcid_LC_MONETARY;
171 static LCID lcid_LC_NUMERIC;
172 static LCID lcid_LC_TIME;
173 static LCID lcid_LC_PAPER;
174 static LCID lcid_LC_MEASUREMENT;
175 static LCID lcid_LC_TELEPHONE;
177 /* Copy Ascii string to Unicode without using codepages */
178 static inline void strcpynAtoW( WCHAR *dst, const char *src, size_t n )
180 while (n > 1 && *src)
182 *dst++ = (unsigned char)*src++;
183 n--;
185 if (n) *dst = 0;
189 /***********************************************************************
190 * get_lcid_codepage
192 * Retrieve the ANSI codepage for a given locale.
194 static inline UINT get_lcid_codepage( LCID lcid )
196 UINT ret;
197 if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
198 sizeof(ret)/sizeof(WCHAR) )) ret = 0;
199 return ret;
203 /***********************************************************************
204 * get_codepage_table
206 * Find the table for a given codepage, handling CP_ACP etc. pseudo-codepages
208 static const union cptable *get_codepage_table( unsigned int codepage )
210 const union cptable *ret = NULL;
212 assert( ansi_cptable ); /* init must have been done already */
214 switch(codepage)
216 case CP_ACP:
217 return ansi_cptable;
218 case CP_OEMCP:
219 return oem_cptable;
220 case CP_MACCP:
221 return mac_cptable;
222 case CP_UTF7:
223 case CP_UTF8:
224 break;
225 case CP_THREAD_ACP:
226 if (!(codepage = kernel_get_thread_data()->code_page)) return ansi_cptable;
227 /* fall through */
228 default:
229 if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
230 if (codepage == oem_cptable->info.codepage) return oem_cptable;
231 if (codepage == mac_cptable->info.codepage) return mac_cptable;
232 ret = wine_cp_get_table( codepage );
233 break;
235 return ret;
239 /***********************************************************************
240 * charset_cmp (internal)
242 static int charset_cmp( const void *name, const void *entry )
244 const struct charset_entry *charset = (const struct charset_entry *)entry;
245 return strcasecmp( (const char *)name, charset->charset_name );
248 /***********************************************************************
249 * find_charset
251 static UINT find_charset( const WCHAR *name )
253 const struct charset_entry *entry;
254 char charset_name[16];
255 size_t i, j;
257 /* remove punctuation characters from charset name */
258 for (i = j = 0; name[i] && j < sizeof(charset_name)-1; i++)
259 if (isalnum((unsigned char)name[i])) charset_name[j++] = name[i];
260 charset_name[j] = 0;
262 entry = bsearch( charset_name, charset_names,
263 sizeof(charset_names)/sizeof(charset_names[0]),
264 sizeof(charset_names[0]), charset_cmp );
265 if (entry) return entry->codepage;
266 return 0;
270 /***********************************************************************
271 * find_locale_id_callback
273 static BOOL CALLBACK find_locale_id_callback( HMODULE hModule, LPCWSTR type,
274 LPCWSTR name, WORD LangID, LPARAM lParam )
276 struct locale_name *data = (struct locale_name *)lParam;
277 WCHAR buffer[128];
278 int matches = 0;
279 LCID lcid = MAKELCID( LangID, SORT_DEFAULT ); /* FIXME: handle sort order */
281 if (PRIMARYLANGID(LangID) == LANG_NEUTRAL) return TRUE; /* continue search */
283 /* first check exact name */
284 if (data->win_name[0] &&
285 GetLocaleInfoW( lcid, LOCALE_SNAME | LOCALE_NOUSEROVERRIDE,
286 buffer, sizeof(buffer)/sizeof(WCHAR) ))
288 if (!strcmpW( data->win_name, buffer ))
290 matches = 4; /* everything matches */
291 goto done;
295 if (!GetLocaleInfoW( lcid, LOCALE_SISO639LANGNAME | LOCALE_NOUSEROVERRIDE,
296 buffer, sizeof(buffer)/sizeof(WCHAR) ))
297 return TRUE;
298 if (strcmpW( buffer, data->lang )) return TRUE;
299 matches++; /* language name matched */
301 if (data->country)
303 if (GetLocaleInfoW( lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
304 buffer, sizeof(buffer)/sizeof(WCHAR) ))
306 if (strcmpW( buffer, data->country )) goto done;
307 matches++; /* country name matched */
310 else /* match default language */
312 if (SUBLANGID(LangID) == SUBLANG_DEFAULT) matches++;
315 if (data->codepage)
317 UINT unix_cp;
318 if (GetLocaleInfoW( lcid, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
319 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) ))
321 if (unix_cp == data->codepage) matches++;
325 /* FIXME: check sort order */
327 done:
328 if (matches > data->matches)
330 data->lcid = lcid;
331 data->matches = matches;
333 return (data->matches < 4); /* no need to continue for perfect match */
337 /***********************************************************************
338 * parse_locale_name
340 * Parse a locale name into a struct locale_name, handling both Windows and Unix formats.
341 * Unix format is: lang[_country][.charset][@modifier]
342 * Windows format is: lang[-script][-country][_modifier]
344 static void parse_locale_name( const WCHAR *str, struct locale_name *name )
346 static const WCHAR sepW[] = {'-','_','.','@',0};
347 static const WCHAR winsepW[] = {'-','_',0};
348 static const WCHAR posixW[] = {'P','O','S','I','X',0};
349 static const WCHAR cW[] = {'C',0};
350 static const WCHAR latinW[] = {'l','a','t','i','n',0};
351 static const WCHAR latnW[] = {'-','L','a','t','n',0};
352 WCHAR *p;
354 TRACE("%s\n", debugstr_w(str));
356 name->country = name->charset = name->script = name->modifier = NULL;
357 name->lcid = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
358 name->matches = 0;
359 name->codepage = 0;
360 name->win_name[0] = 0;
361 lstrcpynW( name->lang, str, sizeof(name->lang)/sizeof(WCHAR) );
363 if (!(p = strpbrkW( name->lang, sepW )))
365 if (!strcmpW( name->lang, posixW ) || !strcmpW( name->lang, cW ))
367 name->matches = 4; /* perfect match for default English lcid */
368 return;
370 strcpyW( name->win_name, name->lang );
372 else if (*p == '-') /* Windows format */
374 strcpyW( name->win_name, name->lang );
375 *p++ = 0;
376 name->country = p;
377 if (!(p = strpbrkW( p, winsepW ))) goto done;
378 if (*p == '-')
380 *p++ = 0;
381 name->script = name->country;
382 name->country = p;
383 if (!(p = strpbrkW( p, winsepW ))) goto done;
385 *p++ = 0;
386 name->modifier = p;
388 else /* Unix format */
390 if (*p == '_')
392 *p++ = 0;
393 name->country = p;
394 p = strpbrkW( p, sepW + 2 );
396 if (p && *p == '.')
398 *p++ = 0;
399 name->charset = p;
400 p = strchrW( p, '@' );
402 if (p)
404 *p++ = 0;
405 name->modifier = p;
408 if (name->charset)
409 name->codepage = find_charset( name->charset );
411 /* rebuild a Windows name if possible */
413 if (name->charset) goto done; /* can't specify charset in Windows format */
414 if (name->modifier && strcmpW( name->modifier, latinW ))
415 goto done; /* only Latn script supported for now */
416 strcpyW( name->win_name, name->lang );
417 if (name->modifier) strcatW( name->win_name, latnW );
418 if (name->country)
420 p = name->win_name + strlenW(name->win_name);
421 *p++ = '-';
422 strcpyW( p, name->country );
425 done:
426 EnumResourceLanguagesW( kernel32_handle, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
427 find_locale_id_callback, (LPARAM)name );
431 /***********************************************************************
432 * convert_default_lcid
434 * Get the default LCID to use for a given lctype in GetLocaleInfo.
436 static LCID convert_default_lcid( LCID lcid, LCTYPE lctype )
438 if (lcid == LOCALE_SYSTEM_DEFAULT ||
439 lcid == LOCALE_USER_DEFAULT ||
440 lcid == LOCALE_NEUTRAL)
442 LCID default_id = 0;
444 switch(lctype & 0xffff)
446 case LOCALE_SSORTNAME:
447 default_id = lcid_LC_COLLATE;
448 break;
450 case LOCALE_FONTSIGNATURE:
451 case LOCALE_IDEFAULTANSICODEPAGE:
452 case LOCALE_IDEFAULTCODEPAGE:
453 case LOCALE_IDEFAULTEBCDICCODEPAGE:
454 case LOCALE_IDEFAULTMACCODEPAGE:
455 case LOCALE_IDEFAULTUNIXCODEPAGE:
456 default_id = lcid_LC_CTYPE;
457 break;
459 case LOCALE_ICURRDIGITS:
460 case LOCALE_ICURRENCY:
461 case LOCALE_IINTLCURRDIGITS:
462 case LOCALE_INEGCURR:
463 case LOCALE_INEGSEPBYSPACE:
464 case LOCALE_INEGSIGNPOSN:
465 case LOCALE_INEGSYMPRECEDES:
466 case LOCALE_IPOSSEPBYSPACE:
467 case LOCALE_IPOSSIGNPOSN:
468 case LOCALE_IPOSSYMPRECEDES:
469 case LOCALE_SCURRENCY:
470 case LOCALE_SINTLSYMBOL:
471 case LOCALE_SMONDECIMALSEP:
472 case LOCALE_SMONGROUPING:
473 case LOCALE_SMONTHOUSANDSEP:
474 case LOCALE_SNATIVECURRNAME:
475 default_id = lcid_LC_MONETARY;
476 break;
478 case LOCALE_IDIGITS:
479 case LOCALE_IDIGITSUBSTITUTION:
480 case LOCALE_ILZERO:
481 case LOCALE_INEGNUMBER:
482 case LOCALE_SDECIMAL:
483 case LOCALE_SGROUPING:
484 case LOCALE_SNAN:
485 case LOCALE_SNATIVEDIGITS:
486 case LOCALE_SNEGATIVESIGN:
487 case LOCALE_SNEGINFINITY:
488 case LOCALE_SPOSINFINITY:
489 case LOCALE_SPOSITIVESIGN:
490 case LOCALE_STHOUSAND:
491 default_id = lcid_LC_NUMERIC;
492 break;
494 case LOCALE_ICALENDARTYPE:
495 case LOCALE_ICENTURY:
496 case LOCALE_IDATE:
497 case LOCALE_IDAYLZERO:
498 case LOCALE_IFIRSTDAYOFWEEK:
499 case LOCALE_IFIRSTWEEKOFYEAR:
500 case LOCALE_ILDATE:
501 case LOCALE_IMONLZERO:
502 case LOCALE_IOPTIONALCALENDAR:
503 case LOCALE_ITIME:
504 case LOCALE_ITIMEMARKPOSN:
505 case LOCALE_ITLZERO:
506 case LOCALE_S1159:
507 case LOCALE_S2359:
508 case LOCALE_SABBREVDAYNAME1:
509 case LOCALE_SABBREVDAYNAME2:
510 case LOCALE_SABBREVDAYNAME3:
511 case LOCALE_SABBREVDAYNAME4:
512 case LOCALE_SABBREVDAYNAME5:
513 case LOCALE_SABBREVDAYNAME6:
514 case LOCALE_SABBREVDAYNAME7:
515 case LOCALE_SABBREVMONTHNAME1:
516 case LOCALE_SABBREVMONTHNAME2:
517 case LOCALE_SABBREVMONTHNAME3:
518 case LOCALE_SABBREVMONTHNAME4:
519 case LOCALE_SABBREVMONTHNAME5:
520 case LOCALE_SABBREVMONTHNAME6:
521 case LOCALE_SABBREVMONTHNAME7:
522 case LOCALE_SABBREVMONTHNAME8:
523 case LOCALE_SABBREVMONTHNAME9:
524 case LOCALE_SABBREVMONTHNAME10:
525 case LOCALE_SABBREVMONTHNAME11:
526 case LOCALE_SABBREVMONTHNAME12:
527 case LOCALE_SABBREVMONTHNAME13:
528 case LOCALE_SDATE:
529 case LOCALE_SDAYNAME1:
530 case LOCALE_SDAYNAME2:
531 case LOCALE_SDAYNAME3:
532 case LOCALE_SDAYNAME4:
533 case LOCALE_SDAYNAME5:
534 case LOCALE_SDAYNAME6:
535 case LOCALE_SDAYNAME7:
536 case LOCALE_SDURATION:
537 case LOCALE_SLONGDATE:
538 case LOCALE_SMONTHNAME1:
539 case LOCALE_SMONTHNAME2:
540 case LOCALE_SMONTHNAME3:
541 case LOCALE_SMONTHNAME4:
542 case LOCALE_SMONTHNAME5:
543 case LOCALE_SMONTHNAME6:
544 case LOCALE_SMONTHNAME7:
545 case LOCALE_SMONTHNAME8:
546 case LOCALE_SMONTHNAME9:
547 case LOCALE_SMONTHNAME10:
548 case LOCALE_SMONTHNAME11:
549 case LOCALE_SMONTHNAME12:
550 case LOCALE_SMONTHNAME13:
551 case LOCALE_SSHORTDATE:
552 case LOCALE_SSHORTESTDAYNAME1:
553 case LOCALE_SSHORTESTDAYNAME2:
554 case LOCALE_SSHORTESTDAYNAME3:
555 case LOCALE_SSHORTESTDAYNAME4:
556 case LOCALE_SSHORTESTDAYNAME5:
557 case LOCALE_SSHORTESTDAYNAME6:
558 case LOCALE_SSHORTESTDAYNAME7:
559 case LOCALE_STIME:
560 case LOCALE_STIMEFORMAT:
561 case LOCALE_SYEARMONTH:
562 default_id = lcid_LC_TIME;
563 break;
565 case LOCALE_IPAPERSIZE:
566 default_id = lcid_LC_PAPER;
567 break;
569 case LOCALE_IMEASURE:
570 default_id = lcid_LC_MEASUREMENT;
571 break;
573 case LOCALE_ICOUNTRY:
574 default_id = lcid_LC_TELEPHONE;
575 break;
577 if (default_id) lcid = default_id;
579 return ConvertDefaultLocale( lcid );
583 /***********************************************************************
584 * create_registry_key
586 * Create the Control Panel\\International registry key.
588 static inline HANDLE create_registry_key(void)
590 static const WCHAR intlW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
591 'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
592 OBJECT_ATTRIBUTES attr;
593 UNICODE_STRING nameW;
594 HANDLE hkey;
596 if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
598 attr.Length = sizeof(attr);
599 attr.RootDirectory = hkey;
600 attr.ObjectName = &nameW;
601 attr.Attributes = 0;
602 attr.SecurityDescriptor = NULL;
603 attr.SecurityQualityOfService = NULL;
604 RtlInitUnicodeString( &nameW, intlW );
606 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS) hkey = 0;
607 NtClose( attr.RootDirectory );
608 return hkey;
612 /* update the registry settings for a given locale parameter */
613 /* return TRUE if an update was needed */
614 static BOOL locale_update_registry( HKEY hkey, const WCHAR *name, LCID lcid,
615 const LCTYPE *values, UINT nb_values )
617 static const WCHAR formatW[] = { '%','0','8','x',0 };
618 WCHAR bufferW[40];
619 UNICODE_STRING nameW;
620 DWORD count, i;
622 RtlInitUnicodeString( &nameW, name );
623 count = sizeof(bufferW);
624 if (!NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation, (LPBYTE)bufferW, count, &count))
626 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
627 LPCWSTR text = (LPCWSTR)info->Data;
629 if (strtoulW( text, NULL, 16 ) == lcid) return FALSE; /* already set correctly */
630 TRACE( "updating registry, locale %s changed %s -> %08x\n",
631 debugstr_w(name), debugstr_w(text), lcid );
633 else TRACE( "updating registry, locale %s changed none -> %08x\n", debugstr_w(name), lcid );
634 sprintfW( bufferW, formatW, lcid );
635 NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, (strlenW(bufferW) + 1) * sizeof(WCHAR) );
637 for (i = 0; i < nb_values; i++)
639 GetLocaleInfoW( lcid, values[i] | LOCALE_NOUSEROVERRIDE, bufferW,
640 sizeof(bufferW)/sizeof(WCHAR) );
641 SetLocaleInfoW( lcid, values[i], bufferW );
643 return TRUE;
647 /***********************************************************************
648 * LOCALE_InitRegistry
650 * Update registry contents on startup if the user locale has changed.
651 * This simulates the action of the Windows control panel.
653 void LOCALE_InitRegistry(void)
655 static const WCHAR acpW[] = {'A','C','P',0};
656 static const WCHAR oemcpW[] = {'O','E','M','C','P',0};
657 static const WCHAR maccpW[] = {'M','A','C','C','P',0};
658 static const WCHAR localeW[] = {'L','o','c','a','l','e',0};
659 static const WCHAR lc_ctypeW[] = { 'L','C','_','C','T','Y','P','E',0 };
660 static const WCHAR lc_monetaryW[] = { 'L','C','_','M','O','N','E','T','A','R','Y',0 };
661 static const WCHAR lc_numericW[] = { 'L','C','_','N','U','M','E','R','I','C',0 };
662 static const WCHAR lc_timeW[] = { 'L','C','_','T','I','M','E',0 };
663 static const WCHAR lc_measurementW[] = { 'L','C','_','M','E','A','S','U','R','E','M','E','N','T',0 };
664 static const WCHAR lc_telephoneW[] = { 'L','C','_','T','E','L','E','P','H','O','N','E',0 };
665 static const WCHAR lc_paperW[] = { 'L','C','_','P','A','P','E','R',0};
666 static const struct
668 LPCWSTR name;
669 USHORT value;
670 } update_cp_values[] = {
671 { acpW, LOCALE_IDEFAULTANSICODEPAGE },
672 { oemcpW, LOCALE_IDEFAULTCODEPAGE },
673 { maccpW, LOCALE_IDEFAULTMACCODEPAGE }
675 static const LCTYPE lc_messages_values[] = {
676 LOCALE_SLANGUAGE,
677 LOCALE_SCOUNTRY,
678 LOCALE_SLIST };
679 static const LCTYPE lc_monetary_values[] = {
680 LOCALE_SCURRENCY,
681 LOCALE_ICURRENCY,
682 LOCALE_INEGCURR,
683 LOCALE_ICURRDIGITS,
684 LOCALE_ILZERO,
685 LOCALE_SMONDECIMALSEP,
686 LOCALE_SMONGROUPING,
687 LOCALE_SMONTHOUSANDSEP };
688 static const LCTYPE lc_numeric_values[] = {
689 LOCALE_SDECIMAL,
690 LOCALE_STHOUSAND,
691 LOCALE_IDIGITS,
692 LOCALE_IDIGITSUBSTITUTION,
693 LOCALE_SNATIVEDIGITS,
694 LOCALE_INEGNUMBER,
695 LOCALE_SNEGATIVESIGN,
696 LOCALE_SPOSITIVESIGN,
697 LOCALE_SGROUPING };
698 static const LCTYPE lc_time_values[] = {
699 LOCALE_S1159,
700 LOCALE_S2359,
701 LOCALE_STIME,
702 LOCALE_ITIME,
703 LOCALE_ITLZERO,
704 LOCALE_SSHORTDATE,
705 LOCALE_SLONGDATE,
706 LOCALE_SDATE,
707 LOCALE_ITIMEMARKPOSN,
708 LOCALE_ICALENDARTYPE,
709 LOCALE_IFIRSTDAYOFWEEK,
710 LOCALE_IFIRSTWEEKOFYEAR,
711 LOCALE_STIMEFORMAT,
712 LOCALE_SYEARMONTH,
713 LOCALE_IDATE };
714 static const LCTYPE lc_measurement_values[] = { LOCALE_IMEASURE };
715 static const LCTYPE lc_telephone_values[] = { LOCALE_ICOUNTRY };
716 static const LCTYPE lc_paper_values[] = { LOCALE_IPAPERSIZE };
718 UNICODE_STRING nameW;
719 WCHAR bufferW[80];
720 DWORD count, i;
721 HANDLE hkey;
722 LCID lcid = GetUserDefaultLCID();
724 if (!(hkey = create_registry_key()))
725 return; /* don't do anything if we can't create the registry key */
727 locale_update_registry( hkey, localeW, lcid_LC_MESSAGES, lc_messages_values,
728 sizeof(lc_messages_values)/sizeof(lc_messages_values[0]) );
729 locale_update_registry( hkey, lc_monetaryW, lcid_LC_MONETARY, lc_monetary_values,
730 sizeof(lc_monetary_values)/sizeof(lc_monetary_values[0]) );
731 locale_update_registry( hkey, lc_numericW, lcid_LC_NUMERIC, lc_numeric_values,
732 sizeof(lc_numeric_values)/sizeof(lc_numeric_values[0]) );
733 locale_update_registry( hkey, lc_timeW, lcid_LC_TIME, lc_time_values,
734 sizeof(lc_time_values)/sizeof(lc_time_values[0]) );
735 locale_update_registry( hkey, lc_measurementW, lcid_LC_MEASUREMENT, lc_measurement_values,
736 sizeof(lc_measurement_values)/sizeof(lc_measurement_values[0]) );
737 locale_update_registry( hkey, lc_telephoneW, lcid_LC_TELEPHONE, lc_telephone_values,
738 sizeof(lc_telephone_values)/sizeof(lc_telephone_values[0]) );
739 locale_update_registry( hkey, lc_paperW, lcid_LC_PAPER, lc_paper_values,
740 sizeof(lc_paper_values)/sizeof(lc_paper_values[0]) );
742 if (locale_update_registry( hkey, lc_ctypeW, lcid_LC_CTYPE, NULL, 0 ))
744 HKEY nls_key = NLS_RegOpenKey( 0, szCodepageKeyName );
746 for (i = 0; i < sizeof(update_cp_values)/sizeof(update_cp_values[0]); i++)
748 count = GetLocaleInfoW( lcid, update_cp_values[i].value | LOCALE_NOUSEROVERRIDE,
749 bufferW, sizeof(bufferW)/sizeof(WCHAR) );
750 RtlInitUnicodeString( &nameW, update_cp_values[i].name );
751 NtSetValueKey( nls_key, &nameW, 0, REG_SZ, bufferW, count * sizeof(WCHAR) );
753 NtClose( nls_key );
756 NtClose( hkey );
760 /***********************************************************************
761 * setup_unix_locales
763 static UINT setup_unix_locales(void)
765 struct locale_name locale_name;
766 WCHAR buffer[128], ctype_buff[128];
767 char *locale;
768 UINT unix_cp = 0;
770 if ((locale = setlocale( LC_CTYPE, NULL )))
772 strcpynAtoW( ctype_buff, locale, sizeof(ctype_buff)/sizeof(WCHAR) );
773 parse_locale_name( ctype_buff, &locale_name );
774 lcid_LC_CTYPE = locale_name.lcid;
775 unix_cp = locale_name.codepage;
777 if (!lcid_LC_CTYPE) /* this one needs a default value */
778 lcid_LC_CTYPE = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
780 TRACE( "got lcid %04x (%d matches) for LC_CTYPE=%s\n",
781 locale_name.lcid, locale_name.matches, debugstr_a(locale) );
783 #define GET_UNIX_LOCALE(cat) do \
784 if ((locale = setlocale( cat, NULL ))) \
786 strcpynAtoW( buffer, locale, sizeof(buffer)/sizeof(WCHAR) ); \
787 if (!strcmpW( buffer, ctype_buff )) lcid_##cat = lcid_LC_CTYPE; \
788 else { \
789 parse_locale_name( buffer, &locale_name ); \
790 lcid_##cat = locale_name.lcid; \
791 TRACE( "got lcid %04x (%d matches) for " #cat "=%s\n", \
792 locale_name.lcid, locale_name.matches, debugstr_a(locale) ); \
794 } while (0)
796 GET_UNIX_LOCALE( LC_COLLATE );
797 GET_UNIX_LOCALE( LC_MESSAGES );
798 GET_UNIX_LOCALE( LC_MONETARY );
799 GET_UNIX_LOCALE( LC_NUMERIC );
800 GET_UNIX_LOCALE( LC_TIME );
801 #ifdef LC_PAPER
802 GET_UNIX_LOCALE( LC_PAPER );
803 #endif
804 #ifdef LC_MEASUREMENT
805 GET_UNIX_LOCALE( LC_MEASUREMENT );
806 #endif
807 #ifdef LC_TELEPHONE
808 GET_UNIX_LOCALE( LC_TELEPHONE );
809 #endif
811 #undef GET_UNIX_LOCALE
813 return unix_cp;
817 /***********************************************************************
818 * GetUserDefaultLangID (KERNEL32.@)
820 * Get the default language Id for the current user.
822 * PARAMS
823 * None.
825 * RETURNS
826 * The current LANGID of the default language for the current user.
828 LANGID WINAPI GetUserDefaultLangID(void)
830 return LANGIDFROMLCID(GetUserDefaultLCID());
834 /***********************************************************************
835 * GetSystemDefaultLangID (KERNEL32.@)
837 * Get the default language Id for the system.
839 * PARAMS
840 * None.
842 * RETURNS
843 * The current LANGID of the default language for the system.
845 LANGID WINAPI GetSystemDefaultLangID(void)
847 return LANGIDFROMLCID(GetSystemDefaultLCID());
851 /***********************************************************************
852 * GetUserDefaultLCID (KERNEL32.@)
854 * Get the default locale Id for the current user.
856 * PARAMS
857 * None.
859 * RETURNS
860 * The current LCID of the default locale for the current user.
862 LCID WINAPI GetUserDefaultLCID(void)
864 LCID lcid;
865 NtQueryDefaultLocale( TRUE, &lcid );
866 return lcid;
870 /***********************************************************************
871 * GetSystemDefaultLCID (KERNEL32.@)
873 * Get the default locale Id for the system.
875 * PARAMS
876 * None.
878 * RETURNS
879 * The current LCID of the default locale for the system.
881 LCID WINAPI GetSystemDefaultLCID(void)
883 LCID lcid;
884 NtQueryDefaultLocale( FALSE, &lcid );
885 return lcid;
889 /***********************************************************************
890 * GetUserDefaultUILanguage (KERNEL32.@)
892 * Get the default user interface language Id for the current user.
894 * PARAMS
895 * None.
897 * RETURNS
898 * The current LANGID of the default UI language for the current user.
900 LANGID WINAPI GetUserDefaultUILanguage(void)
902 LANGID lang;
903 NtQueryDefaultUILanguage( &lang );
904 return lang;
908 /***********************************************************************
909 * GetSystemDefaultUILanguage (KERNEL32.@)
911 * Get the default user interface language Id for the system.
913 * PARAMS
914 * None.
916 * RETURNS
917 * The current LANGID of the default UI language for the system. This is
918 * typically the same language used during the installation process.
920 LANGID WINAPI GetSystemDefaultUILanguage(void)
922 LANGID lang;
923 NtQueryInstallUILanguage( &lang );
924 return lang;
928 /***********************************************************************
929 * LocaleNameToLCID (KERNEL32.@)
931 LCID WINAPI LocaleNameToLCID( LPCWSTR name, DWORD flags )
933 struct locale_name locale_name;
935 if (flags) FIXME( "unsupported flags %x\n", flags );
937 parse_locale_name( name, &locale_name );
939 TRACE( "found lcid %x for %s, matches %d\n",
940 locale_name.lcid, debugstr_w(name), locale_name.matches );
942 if (!locale_name.matches)
943 WARN( "locale %s not recognized, defaulting to English\n", debugstr_w(name) );
944 else if (locale_name.matches == 1)
945 WARN( "locale %s not recognized, defaulting to %s\n",
946 debugstr_w(name), debugstr_w(locale_name.lang) );
948 return locale_name.lcid;
952 /***********************************************************************
953 * LCIDToLocaleName (KERNEL32.@)
955 INT WINAPI LCIDToLocaleName( LCID lcid, LPWSTR name, INT count, DWORD flags )
957 if (flags) FIXME( "unsupported flags %x\n", flags );
959 return GetLocaleInfoW( lcid, LOCALE_SNAME | LOCALE_NOUSEROVERRIDE, name, count );
963 /******************************************************************************
964 * get_locale_value_name
966 * Gets the registry value name for a given lctype.
968 static const WCHAR *get_locale_value_name( DWORD lctype )
970 static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
971 static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
972 static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
973 static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
974 static const WCHAR iDateW[] = {'i','D','a','t','e',0};
975 static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
976 static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
977 static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
978 static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
979 static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
980 static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
981 static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
982 static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
983 static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
984 static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
985 static const WCHAR iTimePrefixW[] = {'i','T','i','m','e','P','r','e','f','i','x',0};
986 static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
987 static const WCHAR s1159W[] = {'s','1','1','5','9',0};
988 static const WCHAR s2359W[] = {'s','2','3','5','9',0};
989 static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
990 static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
991 static const WCHAR sDateW[] = {'s','D','a','t','e',0};
992 static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
993 static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
994 static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
995 static const WCHAR sListW[] = {'s','L','i','s','t',0};
996 static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
997 static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
998 static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
999 static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
1000 static const WCHAR sNativeDigitsW[] = {'s','N','a','t','i','v','e','D','i','g','i','t','s',0};
1001 static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
1002 static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
1003 static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
1004 static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
1005 static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
1006 static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
1007 static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
1008 static const WCHAR NumShapeW[] = {'N','u','m','s','h','a','p','e',0};
1010 switch (lctype)
1012 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
1013 * the values are stored in the registry, confirmed under Windows.
1015 case LOCALE_ICALENDARTYPE: return iCalendarTypeW;
1016 case LOCALE_ICURRDIGITS: return iCurrDigitsW;
1017 case LOCALE_ICURRENCY: return iCurrencyW;
1018 case LOCALE_IDIGITS: return iDigitsW;
1019 case LOCALE_IFIRSTDAYOFWEEK: return iFirstDayOfWeekW;
1020 case LOCALE_IFIRSTWEEKOFYEAR: return iFirstWeekOfYearW;
1021 case LOCALE_ILZERO: return iLZeroW;
1022 case LOCALE_IMEASURE: return iMeasureW;
1023 case LOCALE_INEGCURR: return iNegCurrW;
1024 case LOCALE_INEGNUMBER: return iNegNumberW;
1025 case LOCALE_IPAPERSIZE: return iPaperSizeW;
1026 case LOCALE_ITIME: return iTimeW;
1027 case LOCALE_S1159: return s1159W;
1028 case LOCALE_S2359: return s2359W;
1029 case LOCALE_SCURRENCY: return sCurrencyW;
1030 case LOCALE_SDATE: return sDateW;
1031 case LOCALE_SDECIMAL: return sDecimalW;
1032 case LOCALE_SGROUPING: return sGroupingW;
1033 case LOCALE_SLIST: return sListW;
1034 case LOCALE_SLONGDATE: return sLongDateW;
1035 case LOCALE_SMONDECIMALSEP: return sMonDecimalSepW;
1036 case LOCALE_SMONGROUPING: return sMonGroupingW;
1037 case LOCALE_SMONTHOUSANDSEP: return sMonThousandSepW;
1038 case LOCALE_SNEGATIVESIGN: return sNegativeSignW;
1039 case LOCALE_SPOSITIVESIGN: return sPositiveSignW;
1040 case LOCALE_SSHORTDATE: return sShortDateW;
1041 case LOCALE_STHOUSAND: return sThousandW;
1042 case LOCALE_STIME: return sTimeW;
1043 case LOCALE_STIMEFORMAT: return sTimeFormatW;
1044 case LOCALE_SYEARMONTH: return sYearMonthW;
1046 /* The following are not listed under MSDN as supported,
1047 * but seem to be used and also stored in the registry.
1049 case LOCALE_ICOUNTRY: return iCountryW;
1050 case LOCALE_IDATE: return iDateW;
1051 case LOCALE_ILDATE: return iLDateW;
1052 case LOCALE_ITLZERO: return iTLZeroW;
1053 case LOCALE_SCOUNTRY: return sCountryW;
1054 case LOCALE_SLANGUAGE: return sLanguageW;
1056 /* The following are used in XP and later */
1057 case LOCALE_IDIGITSUBSTITUTION: return NumShapeW;
1058 case LOCALE_SNATIVEDIGITS: return sNativeDigitsW;
1059 case LOCALE_ITIMEMARKPOSN: return iTimePrefixW;
1061 return NULL;
1065 /******************************************************************************
1066 * get_registry_locale_info
1068 * Retrieve user-modified locale info from the registry.
1069 * Return length, 0 on error, -1 if not found.
1071 static INT get_registry_locale_info( LPCWSTR value, LPWSTR buffer, INT len )
1073 DWORD size;
1074 INT ret;
1075 HANDLE hkey;
1076 NTSTATUS status;
1077 UNICODE_STRING nameW;
1078 KEY_VALUE_PARTIAL_INFORMATION *info;
1079 static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
1081 if (!(hkey = create_registry_key())) return -1;
1083 RtlInitUnicodeString( &nameW, value );
1084 size = info_size + len * sizeof(WCHAR);
1086 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
1088 NtClose( hkey );
1089 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1090 return 0;
1093 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
1095 if (!status)
1097 ret = (size - info_size) / sizeof(WCHAR);
1098 /* append terminating null if needed */
1099 if (!ret || ((WCHAR *)info->Data)[ret-1])
1101 if (ret < len || !buffer) ret++;
1102 else
1104 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1105 ret = 0;
1108 if (ret && buffer)
1110 memcpy( buffer, info->Data, (ret-1) * sizeof(WCHAR) );
1111 buffer[ret-1] = 0;
1114 else if (status == STATUS_BUFFER_OVERFLOW && !buffer)
1116 ret = (size - info_size) / sizeof(WCHAR) + 1;
1118 else if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1120 ret = -1;
1122 else
1124 SetLastError( RtlNtStatusToDosError(status) );
1125 ret = 0;
1127 NtClose( hkey );
1128 HeapFree( GetProcessHeap(), 0, info );
1129 return ret;
1133 /******************************************************************************
1134 * GetLocaleInfoA (KERNEL32.@)
1136 * Get information about an aspect of a locale.
1138 * PARAMS
1139 * lcid [I] LCID of the locale
1140 * lctype [I] LCTYPE_ flags from "winnls.h"
1141 * buffer [O] Destination for the information
1142 * len [I] Length of buffer in characters
1144 * RETURNS
1145 * Success: The size of the data requested. If buffer is non-NULL, it is filled
1146 * with the information.
1147 * Failure: 0. Use GetLastError() to determine the cause.
1149 * NOTES
1150 * - LOCALE_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
1151 * - The string returned is NUL terminated, except for LOCALE_FONTSIGNATURE,
1152 * which is a bit string.
1154 INT WINAPI GetLocaleInfoA( LCID lcid, LCTYPE lctype, LPSTR buffer, INT len )
1156 WCHAR *bufferW;
1157 INT lenW, ret;
1159 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
1161 if (len < 0 || (len && !buffer))
1163 SetLastError( ERROR_INVALID_PARAMETER );
1164 return 0;
1166 if (!len) buffer = NULL;
1168 if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
1170 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
1172 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1173 return 0;
1175 if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
1177 if ((lctype & LOCALE_RETURN_NUMBER) ||
1178 ((lctype & ~LOCALE_LOCALEINFOFLAGSMASK) == LOCALE_FONTSIGNATURE))
1180 /* it's not an ASCII string, just bytes */
1181 ret *= sizeof(WCHAR);
1182 if (buffer)
1184 if (ret <= len) memcpy( buffer, bufferW, ret );
1185 else
1187 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1188 ret = 0;
1192 else
1194 UINT codepage = CP_ACP;
1195 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
1196 ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
1199 HeapFree( GetProcessHeap(), 0, bufferW );
1200 return ret;
1204 /******************************************************************************
1205 * GetLocaleInfoW (KERNEL32.@)
1207 * See GetLocaleInfoA.
1209 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
1211 LANGID lang_id;
1212 HRSRC hrsrc;
1213 HGLOBAL hmem;
1214 INT ret;
1215 UINT lcflags;
1216 const WCHAR *p;
1217 unsigned int i;
1219 if (len < 0 || (len && !buffer))
1221 SetLastError( ERROR_INVALID_PARAMETER );
1222 return 0;
1224 if (!len) buffer = NULL;
1226 lcid = convert_default_lcid( lcid, lctype );
1228 lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
1229 lctype &= 0xffff;
1231 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
1233 /* first check for overrides in the registry */
1235 if (!(lcflags & LOCALE_NOUSEROVERRIDE) &&
1236 lcid == convert_default_lcid( LOCALE_USER_DEFAULT, lctype ))
1238 const WCHAR *value = get_locale_value_name(lctype);
1240 if (value)
1242 if (lcflags & LOCALE_RETURN_NUMBER)
1244 WCHAR tmp[16];
1245 ret = get_registry_locale_info( value, tmp, sizeof(tmp)/sizeof(WCHAR) );
1246 if (ret > 0)
1248 WCHAR *end;
1249 UINT number = strtolW( tmp, &end, 10 );
1250 if (*end) /* invalid number */
1252 SetLastError( ERROR_INVALID_FLAGS );
1253 return 0;
1255 ret = sizeof(UINT)/sizeof(WCHAR);
1256 if (!buffer) return ret;
1257 if (ret > len)
1259 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1260 return 0;
1262 memcpy( buffer, &number, sizeof(number) );
1265 else ret = get_registry_locale_info( value, buffer, len );
1267 if (ret != -1) return ret;
1271 /* now load it from kernel resources */
1273 lang_id = LANGIDFROMLCID( lcid );
1275 /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
1276 if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
1277 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
1279 if (!(hrsrc = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
1280 ULongToPtr((lctype >> 4) + 1), lang_id )))
1282 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
1283 return 0;
1285 if (!(hmem = LoadResource( kernel32_handle, hrsrc )))
1286 return 0;
1288 p = LockResource( hmem );
1289 for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
1291 if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
1292 else ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
1294 if (!buffer) return ret;
1296 if (ret > len)
1298 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1299 return 0;
1302 if (lcflags & LOCALE_RETURN_NUMBER)
1304 UINT number;
1305 WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
1306 if (!tmp) return 0;
1307 memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
1308 tmp[*p] = 0;
1309 number = strtolW( tmp, &end, 10 );
1310 if (!*end)
1311 memcpy( buffer, &number, sizeof(number) );
1312 else /* invalid number */
1314 SetLastError( ERROR_INVALID_FLAGS );
1315 ret = 0;
1317 HeapFree( GetProcessHeap(), 0, tmp );
1319 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning number %d\n",
1320 lcid, lctype, buffer, len, number );
1322 else
1324 memcpy( buffer, p + 1, *p * sizeof(WCHAR) );
1325 if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
1327 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning %d %s\n",
1328 lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
1330 return ret;
1334 /******************************************************************************
1335 * SetLocaleInfoA [KERNEL32.@]
1337 * Set information about an aspect of a locale.
1339 * PARAMS
1340 * lcid [I] LCID of the locale
1341 * lctype [I] LCTYPE_ flags from "winnls.h"
1342 * data [I] Information to set
1344 * RETURNS
1345 * Success: TRUE. The information given will be returned by GetLocaleInfoA()
1346 * whenever it is called without LOCALE_NOUSEROVERRIDE.
1347 * Failure: FALSE. Use GetLastError() to determine the cause.
1349 * NOTES
1350 * - Values are only be set for the current user locale; the system locale
1351 * settings cannot be changed.
1352 * - Any settings changed by this call are lost when the locale is changed by
1353 * the control panel (in Wine, this happens every time you change LANG).
1354 * - The native implementation of this function does not check that lcid matches
1355 * the current user locale, and simply sets the new values. Wine warns you in
1356 * this case, but behaves the same.
1358 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
1360 UINT codepage = CP_ACP;
1361 WCHAR *strW;
1362 DWORD len;
1363 BOOL ret;
1365 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
1367 if (!data)
1369 SetLastError( ERROR_INVALID_PARAMETER );
1370 return FALSE;
1372 len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
1373 if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1375 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1376 return FALSE;
1378 MultiByteToWideChar( codepage, 0, data, -1, strW, len );
1379 ret = SetLocaleInfoW( lcid, lctype, strW );
1380 HeapFree( GetProcessHeap(), 0, strW );
1381 return ret;
1385 /******************************************************************************
1386 * SetLocaleInfoW (KERNEL32.@)
1388 * See SetLocaleInfoA.
1390 BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
1392 const WCHAR *value;
1393 static const WCHAR intlW[] = {'i','n','t','l',0 };
1394 UNICODE_STRING valueW;
1395 NTSTATUS status;
1396 HANDLE hkey;
1398 lctype &= 0xffff;
1399 value = get_locale_value_name( lctype );
1401 if (!data || !value)
1403 SetLastError( ERROR_INVALID_PARAMETER );
1404 return FALSE;
1407 if (lctype == LOCALE_IDATE || lctype == LOCALE_ILDATE)
1409 SetLastError( ERROR_INVALID_FLAGS );
1410 return FALSE;
1413 TRACE("setting %x (%s) to %s\n", lctype, debugstr_w(value), debugstr_w(data) );
1415 /* FIXME: should check that data to set is sane */
1417 /* FIXME: profile functions should map to registry */
1418 WriteProfileStringW( intlW, value, data );
1420 if (!(hkey = create_registry_key())) return FALSE;
1421 RtlInitUnicodeString( &valueW, value );
1422 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
1424 if (lctype == LOCALE_SSHORTDATE || lctype == LOCALE_SLONGDATE)
1426 /* Set I-value from S value */
1427 WCHAR *lpD, *lpM, *lpY;
1428 WCHAR szBuff[2];
1430 lpD = strrchrW(data, 'd');
1431 lpM = strrchrW(data, 'M');
1432 lpY = strrchrW(data, 'y');
1434 if (lpD <= lpM)
1436 szBuff[0] = '1'; /* D-M-Y */
1438 else
1440 if (lpY <= lpM)
1441 szBuff[0] = '2'; /* Y-M-D */
1442 else
1443 szBuff[0] = '0'; /* M-D-Y */
1446 szBuff[1] = '\0';
1448 if (lctype == LOCALE_SSHORTDATE)
1449 lctype = LOCALE_IDATE;
1450 else
1451 lctype = LOCALE_ILDATE;
1453 value = get_locale_value_name( lctype );
1455 WriteProfileStringW( intlW, value, szBuff );
1457 RtlInitUnicodeString( &valueW, value );
1458 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, szBuff, sizeof(szBuff) );
1461 NtClose( hkey );
1463 if (status) SetLastError( RtlNtStatusToDosError(status) );
1464 return !status;
1468 /******************************************************************************
1469 * GetACP (KERNEL32.@)
1471 * Get the current Ansi code page Id for the system.
1473 * PARAMS
1474 * None.
1476 * RETURNS
1477 * The current Ansi code page identifier for the system.
1479 UINT WINAPI GetACP(void)
1481 assert( ansi_cptable );
1482 return ansi_cptable->info.codepage;
1486 /******************************************************************************
1487 * SetCPGlobal (KERNEL32.@)
1489 * Set the current Ansi code page Id for the system.
1491 * PARAMS
1492 * acp [I] code page ID to be the new ACP.
1494 * RETURNS
1495 * The previous ACP.
1497 UINT WINAPI SetCPGlobal( UINT acp )
1499 UINT ret = GetACP();
1500 const union cptable *new_cptable = wine_cp_get_table( acp );
1502 if (new_cptable) ansi_cptable = new_cptable;
1503 return ret;
1507 /***********************************************************************
1508 * GetOEMCP (KERNEL32.@)
1510 * Get the current OEM code page Id for the system.
1512 * PARAMS
1513 * None.
1515 * RETURNS
1516 * The current OEM code page identifier for the system.
1518 UINT WINAPI GetOEMCP(void)
1520 assert( oem_cptable );
1521 return oem_cptable->info.codepage;
1525 /***********************************************************************
1526 * IsValidCodePage (KERNEL32.@)
1528 * Determine if a given code page identifier is valid.
1530 * PARAMS
1531 * codepage [I] Code page Id to verify.
1533 * RETURNS
1534 * TRUE, If codepage is valid and available on the system,
1535 * FALSE otherwise.
1537 BOOL WINAPI IsValidCodePage( UINT codepage )
1539 switch(codepage) {
1540 case CP_UTF7:
1541 case CP_UTF8:
1542 return TRUE;
1543 default:
1544 return wine_cp_get_table( codepage ) != NULL;
1549 /***********************************************************************
1550 * IsDBCSLeadByteEx (KERNEL32.@)
1552 * Determine if a character is a lead byte in a given code page.
1554 * PARAMS
1555 * codepage [I] Code page for the test.
1556 * testchar [I] Character to test
1558 * RETURNS
1559 * TRUE, if testchar is a lead byte in codepage,
1560 * FALSE otherwise.
1562 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
1564 const union cptable *table = get_codepage_table( codepage );
1565 return table && wine_is_dbcs_leadbyte( table, testchar );
1569 /***********************************************************************
1570 * IsDBCSLeadByte (KERNEL32.@)
1571 * IsDBCSLeadByte (KERNEL.207)
1573 * Determine if a character is a lead byte.
1575 * PARAMS
1576 * testchar [I] Character to test
1578 * RETURNS
1579 * TRUE, if testchar is a lead byte in the Ansii code page,
1580 * FALSE otherwise.
1582 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
1584 if (!ansi_cptable) return FALSE;
1585 return wine_is_dbcs_leadbyte( ansi_cptable, testchar );
1589 /***********************************************************************
1590 * GetCPInfo (KERNEL32.@)
1592 * Get information about a code page.
1594 * PARAMS
1595 * codepage [I] Code page number
1596 * cpinfo [O] Destination for code page information
1598 * RETURNS
1599 * Success: TRUE. cpinfo is updated with the information about codepage.
1600 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1602 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
1604 const union cptable *table;
1606 if (!cpinfo)
1608 SetLastError( ERROR_INVALID_PARAMETER );
1609 return FALSE;
1612 if (!(table = get_codepage_table( codepage )))
1614 switch(codepage)
1616 case CP_UTF7:
1617 case CP_UTF8:
1618 cpinfo->DefaultChar[0] = 0x3f;
1619 cpinfo->DefaultChar[1] = 0;
1620 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1621 cpinfo->MaxCharSize = (codepage == CP_UTF7) ? 5 : 4;
1622 return TRUE;
1625 SetLastError( ERROR_INVALID_PARAMETER );
1626 return FALSE;
1628 if (table->info.def_char & 0xff00)
1630 cpinfo->DefaultChar[0] = (table->info.def_char & 0xff00) >> 8;
1631 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
1633 else
1635 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
1636 cpinfo->DefaultChar[1] = 0;
1638 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
1639 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
1640 else
1641 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1643 return TRUE;
1646 /***********************************************************************
1647 * GetCPInfoExA (KERNEL32.@)
1649 * Get extended information about a code page.
1651 * PARAMS
1652 * codepage [I] Code page number
1653 * dwFlags [I] Reserved, must to 0.
1654 * cpinfo [O] Destination for code page information
1656 * RETURNS
1657 * Success: TRUE. cpinfo is updated with the information about codepage.
1658 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1660 BOOL WINAPI GetCPInfoExA( UINT codepage, DWORD dwFlags, LPCPINFOEXA cpinfo )
1662 CPINFOEXW cpinfoW;
1664 if (!GetCPInfoExW( codepage, dwFlags, &cpinfoW ))
1665 return FALSE;
1667 /* the layout is the same except for CodePageName */
1668 memcpy(cpinfo, &cpinfoW, sizeof(CPINFOEXA));
1669 WideCharToMultiByte(CP_ACP, 0, cpinfoW.CodePageName, -1, cpinfo->CodePageName, sizeof(cpinfo->CodePageName), NULL, NULL);
1670 return TRUE;
1673 /***********************************************************************
1674 * GetCPInfoExW (KERNEL32.@)
1676 * Unicode version of GetCPInfoExA.
1678 BOOL WINAPI GetCPInfoExW( UINT codepage, DWORD dwFlags, LPCPINFOEXW cpinfo )
1680 if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
1681 return FALSE;
1683 switch(codepage)
1685 case CP_UTF7:
1687 static const WCHAR utf7[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','7',')',0};
1689 cpinfo->CodePage = CP_UTF7;
1690 cpinfo->UnicodeDefaultChar = 0x3f;
1691 strcpyW(cpinfo->CodePageName, utf7);
1692 break;
1695 case CP_UTF8:
1697 static const WCHAR utf8[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','8',')',0};
1699 cpinfo->CodePage = CP_UTF8;
1700 cpinfo->UnicodeDefaultChar = 0x3f;
1701 strcpyW(cpinfo->CodePageName, utf8);
1702 break;
1705 default:
1707 const union cptable *table = get_codepage_table( codepage );
1709 cpinfo->CodePage = table->info.codepage;
1710 cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
1711 MultiByteToWideChar( CP_ACP, 0, table->info.name, -1, cpinfo->CodePageName,
1712 sizeof(cpinfo->CodePageName)/sizeof(WCHAR));
1713 break;
1716 return TRUE;
1719 /***********************************************************************
1720 * EnumSystemCodePagesA (KERNEL32.@)
1722 * Call a user defined function for every code page installed on the system.
1724 * PARAMS
1725 * lpfnCodePageEnum [I] User CODEPAGE_ENUMPROC to call with each found code page
1726 * flags [I] Reserved, set to 0.
1728 * RETURNS
1729 * TRUE, If all code pages have been enumerated, or
1730 * FALSE if lpfnCodePageEnum returned FALSE to stop the enumeration.
1732 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
1734 const union cptable *table;
1735 char buffer[10];
1736 int index = 0;
1738 for (;;)
1740 if (!(table = wine_cp_enum_table( index++ ))) break;
1741 sprintf( buffer, "%d", table->info.codepage );
1742 if (!lpfnCodePageEnum( buffer )) break;
1744 return TRUE;
1748 /***********************************************************************
1749 * EnumSystemCodePagesW (KERNEL32.@)
1751 * See EnumSystemCodePagesA.
1753 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
1755 const union cptable *table;
1756 WCHAR buffer[10], *p;
1757 int page, index = 0;
1759 for (;;)
1761 if (!(table = wine_cp_enum_table( index++ ))) break;
1762 p = buffer + sizeof(buffer)/sizeof(WCHAR);
1763 *--p = 0;
1764 page = table->info.codepage;
1767 *--p = '0' + (page % 10);
1768 page /= 10;
1769 } while( page );
1770 if (!lpfnCodePageEnum( p )) break;
1772 return TRUE;
1776 /***********************************************************************
1777 * MultiByteToWideChar (KERNEL32.@)
1779 * Convert a multibyte character string into a Unicode string.
1781 * PARAMS
1782 * page [I] Codepage character set to convert from
1783 * flags [I] Character mapping flags
1784 * src [I] Source string buffer
1785 * srclen [I] Length of src (in bytes), or -1 if src is NUL terminated
1786 * dst [O] Destination buffer
1787 * dstlen [I] Length of dst (in WCHARs), or 0 to compute the required length
1789 * RETURNS
1790 * Success: If dstlen > 0, the number of characters written to dst.
1791 * If dstlen == 0, the number of characters needed to perform the
1792 * conversion. In both cases the count includes the terminating NUL.
1793 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1794 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1795 * and dstlen != 0; ERROR_INVALID_PARAMETER, if an invalid parameter
1796 * is passed, and ERROR_NO_UNICODE_TRANSLATION if no translation is
1797 * possible for src.
1799 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
1800 LPWSTR dst, INT dstlen )
1802 const union cptable *table;
1803 int ret;
1805 if (!src || (!dst && dstlen))
1807 SetLastError( ERROR_INVALID_PARAMETER );
1808 return 0;
1811 if (srclen < 0) srclen = strlen(src) + 1;
1813 switch(page)
1815 case CP_SYMBOL:
1816 if( flags)
1818 SetLastError( ERROR_INVALID_PARAMETER );
1819 return 0;
1821 ret = wine_cpsymbol_mbstowcs( src, srclen, dst, dstlen );
1822 break;
1823 case CP_UTF7:
1824 FIXME("UTF-7 not supported\n");
1825 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1826 return 0;
1827 case CP_UNIXCP:
1828 if (unix_cptable)
1830 ret = wine_cp_mbstowcs( unix_cptable, flags, src, srclen, dst, dstlen );
1831 break;
1833 /* fall through */
1834 case CP_UTF8:
1835 ret = wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
1836 break;
1837 default:
1838 if (!(table = get_codepage_table( page )))
1840 SetLastError( ERROR_INVALID_PARAMETER );
1841 return 0;
1843 ret = wine_cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
1844 break;
1847 if (ret < 0)
1849 switch(ret)
1851 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1852 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1854 ret = 0;
1856 TRACE("cp %d %s -> %s, ret = %d\n",
1857 page, debugstr_an(src, srclen), debugstr_wn(dst, ret), ret);
1858 return ret;
1862 /***********************************************************************
1863 * WideCharToMultiByte (KERNEL32.@)
1865 * Convert a Unicode character string into a multibyte string.
1867 * PARAMS
1868 * page [I] Code page character set to convert to
1869 * flags [I] Mapping Flags (MB_ constants from "winnls.h").
1870 * src [I] Source string buffer
1871 * srclen [I] Length of src (in WCHARs), or -1 if src is NUL terminated
1872 * dst [O] Destination buffer
1873 * dstlen [I] Length of dst (in bytes), or 0 to compute the required length
1874 * defchar [I] Default character to use for conversion if no exact
1875 * conversion can be made
1876 * used [O] Set if default character was used in the conversion
1878 * RETURNS
1879 * Success: If dstlen > 0, the number of characters written to dst.
1880 * If dstlen == 0, number of characters needed to perform the
1881 * conversion. In both cases the count includes the terminating NUL.
1882 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1883 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1884 * and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
1885 * parameter was given.
1887 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
1888 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
1890 const union cptable *table;
1891 int ret, used_tmp;
1893 if (!src || (!dst && dstlen))
1895 SetLastError( ERROR_INVALID_PARAMETER );
1896 return 0;
1899 if (srclen < 0) srclen = strlenW(src) + 1;
1901 switch(page)
1903 case CP_SYMBOL:
1904 if( flags || defchar || used)
1906 SetLastError( ERROR_INVALID_PARAMETER );
1907 return 0;
1909 ret = wine_cpsymbol_wcstombs( src, srclen, dst, dstlen );
1910 break;
1911 case CP_UTF7:
1912 FIXME("UTF-7 not supported\n");
1913 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1914 return 0;
1915 case CP_UNIXCP:
1916 if (unix_cptable)
1918 ret = wine_cp_wcstombs( unix_cptable, flags, src, srclen, dst, dstlen,
1919 defchar, used ? &used_tmp : NULL );
1920 break;
1922 /* fall through */
1923 case CP_UTF8:
1924 if (used) *used = FALSE; /* all chars are valid for UTF-8 */
1925 ret = wine_utf8_wcstombs( flags, src, srclen, dst, dstlen );
1926 break;
1927 default:
1928 if (!(table = get_codepage_table( page )))
1930 SetLastError( ERROR_INVALID_PARAMETER );
1931 return 0;
1933 ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
1934 defchar, used ? &used_tmp : NULL );
1935 if (used) *used = used_tmp;
1936 break;
1939 if (ret < 0)
1941 switch(ret)
1943 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1944 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1946 ret = 0;
1948 TRACE("cp %d %s -> %s, ret = %d\n",
1949 page, debugstr_wn(src, srclen), debugstr_an(dst, ret), ret);
1950 return ret;
1954 /***********************************************************************
1955 * GetThreadLocale (KERNEL32.@)
1957 * Get the current threads locale.
1959 * PARAMS
1960 * None.
1962 * RETURNS
1963 * The LCID currently associated with the calling thread.
1965 LCID WINAPI GetThreadLocale(void)
1967 LCID ret = NtCurrentTeb()->CurrentLocale;
1968 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
1969 return ret;
1972 /**********************************************************************
1973 * SetThreadLocale (KERNEL32.@)
1975 * Set the current threads locale.
1977 * PARAMS
1978 * lcid [I] LCID of the locale to set
1980 * RETURNS
1981 * Success: TRUE. The threads locale is set to lcid.
1982 * Failure: FALSE. Use GetLastError() to determine the cause.
1984 BOOL WINAPI SetThreadLocale( LCID lcid )
1986 TRACE("(0x%04X)\n", lcid);
1988 lcid = ConvertDefaultLocale(lcid);
1990 if (lcid != GetThreadLocale())
1992 if (!IsValidLocale(lcid, LCID_SUPPORTED))
1994 SetLastError(ERROR_INVALID_PARAMETER);
1995 return FALSE;
1998 NtCurrentTeb()->CurrentLocale = lcid;
1999 kernel_get_thread_data()->code_page = get_lcid_codepage( lcid );
2001 return TRUE;
2004 /**********************************************************************
2005 * SetThreadUILanguage (KERNEL32.@)
2007 * Set the current threads UI language.
2009 * PARAMS
2010 * langid [I] LANGID of the language to set, or 0 to use
2011 * the available language which is best supported
2012 * for console applications
2014 * RETURNS
2015 * Success: The return value is the same as the input value.
2016 * Failure: The return value differs from the input value.
2017 * Use GetLastError() to determine the cause.
2019 LANGID WINAPI SetThreadUILanguage( LANGID langid )
2021 TRACE("(0x%04x) stub - returning success\n", langid);
2022 return langid;
2025 /******************************************************************************
2026 * ConvertDefaultLocale (KERNEL32.@)
2028 * Convert a default locale identifier into a real identifier.
2030 * PARAMS
2031 * lcid [I] LCID identifier of the locale to convert
2033 * RETURNS
2034 * lcid unchanged, if not a default locale or its sublanguage is
2035 * not SUBLANG_NEUTRAL.
2036 * GetSystemDefaultLCID(), if lcid == LOCALE_SYSTEM_DEFAULT.
2037 * GetUserDefaultLCID(), if lcid == LOCALE_USER_DEFAULT or LOCALE_NEUTRAL.
2038 * Otherwise, lcid with sublanguage changed to SUBLANG_DEFAULT.
2040 LCID WINAPI ConvertDefaultLocale( LCID lcid )
2042 LANGID langid;
2044 switch (lcid)
2046 case LOCALE_SYSTEM_DEFAULT:
2047 lcid = GetSystemDefaultLCID();
2048 break;
2049 case LOCALE_USER_DEFAULT:
2050 case LOCALE_NEUTRAL:
2051 lcid = GetUserDefaultLCID();
2052 break;
2053 default:
2054 /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
2055 langid = LANGIDFROMLCID(lcid);
2056 if (SUBLANGID(langid) == SUBLANG_NEUTRAL)
2058 langid = MAKELANGID(PRIMARYLANGID(langid), SUBLANG_DEFAULT);
2059 lcid = MAKELCID(langid, SORTIDFROMLCID(lcid));
2062 return lcid;
2066 /******************************************************************************
2067 * IsValidLocale (KERNEL32.@)
2069 * Determine if a locale is valid.
2071 * PARAMS
2072 * lcid [I] LCID of the locale to check
2073 * flags [I] LCID_SUPPORTED = Valid, LCID_INSTALLED = Valid and installed on the system
2075 * RETURNS
2076 * TRUE, if lcid is valid,
2077 * FALSE, otherwise.
2079 * NOTES
2080 * Wine does not currently make the distinction between supported and installed. All
2081 * languages supported are installed by default.
2083 BOOL WINAPI IsValidLocale( LCID lcid, DWORD flags )
2085 /* check if language is registered in the kernel32 resources */
2086 return FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
2087 (LPCWSTR)LOCALE_ILANGUAGE, LANGIDFROMLCID(lcid)) != 0;
2091 static BOOL CALLBACK enum_lang_proc_a( HMODULE hModule, LPCSTR type,
2092 LPCSTR name, WORD LangID, LONG_PTR lParam )
2094 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
2095 char buf[20];
2097 sprintf(buf, "%08x", (UINT)LangID);
2098 return lpfnLocaleEnum( buf );
2101 static BOOL CALLBACK enum_lang_proc_w( HMODULE hModule, LPCWSTR type,
2102 LPCWSTR name, WORD LangID, LONG_PTR lParam )
2104 static const WCHAR formatW[] = {'%','0','8','x',0};
2105 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
2106 WCHAR buf[20];
2107 sprintfW( buf, formatW, (UINT)LangID );
2108 return lpfnLocaleEnum( buf );
2111 /******************************************************************************
2112 * EnumSystemLocalesA (KERNEL32.@)
2114 * Call a users function for each locale available on the system.
2116 * PARAMS
2117 * lpfnLocaleEnum [I] Callback function to call for each locale
2118 * dwFlags [I] LOCALE_SUPPORTED=All supported, LOCALE_INSTALLED=Installed only
2120 * RETURNS
2121 * Success: TRUE.
2122 * Failure: FALSE. Use GetLastError() to determine the cause.
2124 BOOL WINAPI EnumSystemLocalesA( LOCALE_ENUMPROCA lpfnLocaleEnum, DWORD dwFlags )
2126 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
2127 EnumResourceLanguagesA( kernel32_handle, (LPSTR)RT_STRING,
2128 (LPCSTR)LOCALE_ILANGUAGE, enum_lang_proc_a,
2129 (LONG_PTR)lpfnLocaleEnum);
2130 return TRUE;
2134 /******************************************************************************
2135 * EnumSystemLocalesW (KERNEL32.@)
2137 * See EnumSystemLocalesA.
2139 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum, DWORD dwFlags )
2141 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
2142 EnumResourceLanguagesW( kernel32_handle, (LPWSTR)RT_STRING,
2143 (LPCWSTR)LOCALE_ILANGUAGE, enum_lang_proc_w,
2144 (LONG_PTR)lpfnLocaleEnum);
2145 return TRUE;
2149 /***********************************************************************
2150 * VerLanguageNameA (KERNEL32.@)
2152 * Get the name of a language.
2154 * PARAMS
2155 * wLang [I] LANGID of the language
2156 * szLang [O] Destination for the language name
2158 * RETURNS
2159 * Success: The size of the language name. If szLang is non-NULL, it is filled
2160 * with the name.
2161 * Failure: 0. Use GetLastError() to determine the cause.
2164 DWORD WINAPI VerLanguageNameA( DWORD wLang, LPSTR szLang, DWORD nSize )
2166 return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
2170 /***********************************************************************
2171 * VerLanguageNameW (KERNEL32.@)
2173 * See VerLanguageNameA.
2175 DWORD WINAPI VerLanguageNameW( DWORD wLang, LPWSTR szLang, DWORD nSize )
2177 return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
2181 /******************************************************************************
2182 * GetStringTypeW (KERNEL32.@)
2184 * See GetStringTypeA.
2186 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
2188 if (count == -1) count = strlenW(src) + 1;
2189 switch(type)
2191 case CT_CTYPE1:
2192 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
2193 break;
2194 case CT_CTYPE2:
2195 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
2196 break;
2197 case CT_CTYPE3:
2199 WARN("CT_CTYPE3: semi-stub.\n");
2200 while (count--)
2202 int c = *src;
2203 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
2205 type1 = get_char_typeW( *src++ ) & 0xfff;
2206 /* try to construct type3 from type1 */
2207 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
2208 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
2209 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
2210 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
2211 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
2212 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
2213 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
2215 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
2216 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
2217 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
2218 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
2219 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
2220 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
2221 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
2222 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
2224 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
2225 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
2226 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
2227 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
2228 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
2229 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
2230 *chartype++ = type3;
2232 break;
2234 default:
2235 SetLastError( ERROR_INVALID_PARAMETER );
2236 return FALSE;
2238 return TRUE;
2242 /******************************************************************************
2243 * GetStringTypeExW (KERNEL32.@)
2245 * See GetStringTypeExA.
2247 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
2249 /* locale is ignored for Unicode */
2250 return GetStringTypeW( type, src, count, chartype );
2254 /******************************************************************************
2255 * GetStringTypeA (KERNEL32.@)
2257 * Get characteristics of the characters making up a string.
2259 * PARAMS
2260 * locale [I] Locale Id for the string
2261 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
2262 * src [I] String to analyse
2263 * count [I] Length of src in chars, or -1 if src is NUL terminated
2264 * chartype [O] Destination for the calculated characteristics
2266 * RETURNS
2267 * Success: TRUE. chartype is filled with the requested characteristics of each char
2268 * in src.
2269 * Failure: FALSE. Use GetLastError() to determine the cause.
2271 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
2273 UINT cp;
2274 INT countW;
2275 LPWSTR srcW;
2276 BOOL ret = FALSE;
2278 if(count == -1) count = strlen(src) + 1;
2280 if (!(cp = get_lcid_codepage( locale )))
2282 FIXME("For locale %04x using current ANSI code page\n", locale);
2283 cp = GetACP();
2286 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
2287 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
2289 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
2291 * NOTE: the target buffer has 1 word for each CHARACTER in the source
2292 * string, with multibyte characters there maybe be more bytes in count
2293 * than character space in the buffer!
2295 ret = GetStringTypeW(type, srcW, countW, chartype);
2296 HeapFree(GetProcessHeap(), 0, srcW);
2298 return ret;
2301 /******************************************************************************
2302 * GetStringTypeExA (KERNEL32.@)
2304 * Get characteristics of the characters making up a string.
2306 * PARAMS
2307 * locale [I] Locale Id for the string
2308 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
2309 * src [I] String to analyse
2310 * count [I] Length of src in chars, or -1 if src is NUL terminated
2311 * chartype [O] Destination for the calculated characteristics
2313 * RETURNS
2314 * Success: TRUE. chartype is filled with the requested characteristics of each char
2315 * in src.
2316 * Failure: FALSE. Use GetLastError() to determine the cause.
2318 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
2320 return GetStringTypeA(locale, type, src, count, chartype);
2324 /*************************************************************************
2325 * LCMapStringW (KERNEL32.@)
2327 * See LCMapStringA.
2329 INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
2330 LPWSTR dst, INT dstlen)
2332 LPWSTR dst_ptr;
2334 if (!src || !srclen || dstlen < 0)
2336 SetLastError(ERROR_INVALID_PARAMETER);
2337 return 0;
2340 /* mutually exclusive flags */
2341 if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
2342 (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
2343 (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
2344 (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE))
2346 SetLastError(ERROR_INVALID_FLAGS);
2347 return 0;
2350 if (!dstlen) dst = NULL;
2352 lcid = ConvertDefaultLocale(lcid);
2354 if (flags & LCMAP_SORTKEY)
2356 INT ret;
2357 if (src == dst)
2359 SetLastError(ERROR_INVALID_FLAGS);
2360 return 0;
2363 if (srclen < 0) srclen = strlenW(src);
2365 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
2366 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
2368 ret = wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
2369 if (ret == 0)
2370 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2371 return ret;
2374 /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
2375 if (flags & SORT_STRINGSORT)
2377 SetLastError(ERROR_INVALID_FLAGS);
2378 return 0;
2381 if (srclen < 0) srclen = strlenW(src) + 1;
2383 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
2384 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
2386 if (!dst) /* return required string length */
2388 INT len;
2390 for (len = 0; srclen; src++, srclen--)
2392 WCHAR wch = *src;
2393 /* tests show that win2k just ignores NORM_IGNORENONSPACE,
2394 * and skips white space and punctuation characters for
2395 * NORM_IGNORESYMBOLS.
2397 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2398 continue;
2399 len++;
2401 return len;
2404 if (flags & LCMAP_UPPERCASE)
2406 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2408 WCHAR wch = *src;
2409 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2410 continue;
2411 *dst_ptr++ = toupperW(wch);
2412 dstlen--;
2415 else if (flags & LCMAP_LOWERCASE)
2417 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2419 WCHAR wch = *src;
2420 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2421 continue;
2422 *dst_ptr++ = tolowerW(wch);
2423 dstlen--;
2426 else
2428 if (src == dst)
2430 SetLastError(ERROR_INVALID_FLAGS);
2431 return 0;
2433 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2435 WCHAR wch = *src;
2436 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2437 continue;
2438 *dst_ptr++ = wch;
2439 dstlen--;
2443 if (srclen)
2445 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2446 return 0;
2449 return dst_ptr - dst;
2452 /*************************************************************************
2453 * LCMapStringA (KERNEL32.@)
2455 * Map characters in a locale sensitive string.
2457 * PARAMS
2458 * lcid [I] LCID for the conversion.
2459 * flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
2460 * src [I] String to map
2461 * srclen [I] Length of src in chars, or -1 if src is NUL terminated
2462 * dst [O] Destination for mapped string
2463 * dstlen [I] Length of dst in characters
2465 * RETURNS
2466 * Success: The length of the mapped string in dst, including the NUL terminator.
2467 * Failure: 0. Use GetLastError() to determine the cause.
2469 INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
2470 LPSTR dst, INT dstlen)
2472 WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
2473 LPWSTR srcW, dstW;
2474 INT ret = 0, srclenW, dstlenW;
2475 UINT locale_cp = CP_ACP;
2477 if (!src || !srclen || dstlen < 0)
2479 SetLastError(ERROR_INVALID_PARAMETER);
2480 return 0;
2483 if (!(flags & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
2485 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
2486 if (srclenW)
2487 srcW = bufW;
2488 else
2490 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
2491 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2492 if (!srcW)
2494 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2495 return 0;
2497 MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
2500 if (flags & LCMAP_SORTKEY)
2502 if (src == dst)
2504 SetLastError(ERROR_INVALID_FLAGS);
2505 goto map_string_exit;
2507 ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
2508 if (ret == 0)
2509 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2510 goto map_string_exit;
2513 if (flags & SORT_STRINGSORT)
2515 SetLastError(ERROR_INVALID_FLAGS);
2516 goto map_string_exit;
2519 dstlenW = LCMapStringW(lcid, flags, srcW, srclenW, NULL, 0);
2520 if (!dstlenW)
2521 goto map_string_exit;
2523 dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
2524 if (!dstW)
2526 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2527 goto map_string_exit;
2530 LCMapStringW(lcid, flags, srcW, srclenW, dstW, dstlenW);
2531 ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
2532 HeapFree(GetProcessHeap(), 0, dstW);
2534 map_string_exit:
2535 if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
2536 return ret;
2539 /*************************************************************************
2540 * FoldStringA (KERNEL32.@)
2542 * Map characters in a string.
2544 * PARAMS
2545 * dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
2546 * src [I] String to map
2547 * srclen [I] Length of src, or -1 if src is NUL terminated
2548 * dst [O] Destination for mapped string
2549 * dstlen [I] Length of dst, or 0 to find the required length for the mapped string
2551 * RETURNS
2552 * Success: The length of the string written to dst, including the terminating NUL. If
2553 * dstlen is 0, the value returned is the same, but nothing is written to dst,
2554 * and dst may be NULL.
2555 * Failure: 0. Use GetLastError() to determine the cause.
2557 INT WINAPI FoldStringA(DWORD dwFlags, LPCSTR src, INT srclen,
2558 LPSTR dst, INT dstlen)
2560 INT ret = 0, srclenW = 0;
2561 WCHAR *srcW = NULL, *dstW = NULL;
2563 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2565 SetLastError(ERROR_INVALID_PARAMETER);
2566 return 0;
2569 srclenW = MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2570 src, srclen, NULL, 0);
2571 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2573 if (!srcW)
2575 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2576 goto FoldStringA_exit;
2579 MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2580 src, srclen, srcW, srclenW);
2582 dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
2584 ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
2585 if (ret && dstlen)
2587 dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
2589 if (!dstW)
2591 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2592 goto FoldStringA_exit;
2595 ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
2596 if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
2598 ret = 0;
2599 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2603 HeapFree(GetProcessHeap(), 0, dstW);
2605 FoldStringA_exit:
2606 HeapFree(GetProcessHeap(), 0, srcW);
2607 return ret;
2610 /*************************************************************************
2611 * FoldStringW (KERNEL32.@)
2613 * See FoldStringA.
2615 INT WINAPI FoldStringW(DWORD dwFlags, LPCWSTR src, INT srclen,
2616 LPWSTR dst, INT dstlen)
2618 int ret;
2620 switch (dwFlags & (MAP_COMPOSITE|MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES))
2622 case 0:
2623 if (dwFlags)
2624 break;
2625 /* Fall through for dwFlags == 0 */
2626 case MAP_PRECOMPOSED|MAP_COMPOSITE:
2627 case MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES:
2628 case MAP_COMPOSITE|MAP_EXPAND_LIGATURES:
2629 SetLastError(ERROR_INVALID_FLAGS);
2630 return 0;
2633 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2635 SetLastError(ERROR_INVALID_PARAMETER);
2636 return 0;
2639 ret = wine_fold_string(dwFlags, src, srclen, dst, dstlen);
2640 if (!ret)
2641 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2642 return ret;
2645 /******************************************************************************
2646 * CompareStringW (KERNEL32.@)
2648 * See CompareStringA.
2650 INT WINAPI CompareStringW(LCID lcid, DWORD style,
2651 LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
2653 INT ret;
2655 if (!str1 || !str2)
2657 SetLastError(ERROR_INVALID_PARAMETER);
2658 return 0;
2661 if( style & ~(NORM_IGNORECASE|NORM_IGNORENONSPACE|NORM_IGNORESYMBOLS|
2662 SORT_STRINGSORT|NORM_IGNOREKANATYPE|NORM_IGNOREWIDTH|LOCALE_USE_CP_ACP|0x10000000) )
2664 SetLastError(ERROR_INVALID_FLAGS);
2665 return 0;
2668 /* this style is related to diacritics in Arabic, Japanese, and Hebrew */
2669 if (style & 0x10000000)
2670 WARN("Ignoring unknown style 0x10000000\n");
2672 if (len1 < 0) len1 = strlenW(str1);
2673 if (len2 < 0) len2 = strlenW(str2);
2675 ret = wine_compare_string(style, str1, len1, str2, len2);
2677 if (ret) /* need to translate result */
2678 return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
2679 return CSTR_EQUAL;
2682 /******************************************************************************
2683 * CompareStringA (KERNEL32.@)
2685 * Compare two locale sensitive strings.
2687 * PARAMS
2688 * lcid [I] LCID for the comparison
2689 * style [I] Flags for the comparison (NORM_ constants from "winnls.h").
2690 * str1 [I] First string to compare
2691 * len1 [I] Length of str1, or -1 if str1 is NUL terminated
2692 * str2 [I] Second string to compare
2693 * len2 [I] Length of str2, or -1 if str2 is NUL terminated
2695 * RETURNS
2696 * Success: CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN depending on whether
2697 * str2 is less than, equal to or greater than str1 respectively.
2698 * Failure: FALSE. Use GetLastError() to determine the cause.
2700 INT WINAPI CompareStringA(LCID lcid, DWORD style,
2701 LPCSTR str1, INT len1, LPCSTR str2, INT len2)
2703 WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
2704 WCHAR *buf2W = buf1W + 130;
2705 LPWSTR str1W, str2W;
2706 INT len1W, len2W, ret;
2707 UINT locale_cp = CP_ACP;
2709 if (!str1 || !str2)
2711 SetLastError(ERROR_INVALID_PARAMETER);
2712 return 0;
2714 if (len1 < 0) len1 = strlen(str1);
2715 if (len2 < 0) len2 = strlen(str2);
2717 if (!(style & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
2719 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 130);
2720 if (len1W)
2721 str1W = buf1W;
2722 else
2724 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
2725 str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
2726 if (!str1W)
2728 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2729 return 0;
2731 MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
2733 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 130);
2734 if (len2W)
2735 str2W = buf2W;
2736 else
2738 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
2739 str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
2740 if (!str2W)
2742 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2743 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2744 return 0;
2746 MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
2749 ret = CompareStringW(lcid, style, str1W, len1W, str2W, len2W);
2751 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2752 if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
2753 return ret;
2756 /*************************************************************************
2757 * lstrcmp (KERNEL32.@)
2758 * lstrcmpA (KERNEL32.@)
2760 * Compare two strings using the current thread locale.
2762 * PARAMS
2763 * str1 [I] First string to compare
2764 * str2 [I] Second string to compare
2766 * RETURNS
2767 * Success: A number less than, equal to or greater than 0 depending on whether
2768 * str2 is less than, equal to or greater than str1 respectively.
2769 * Failure: FALSE. Use GetLastError() to determine the cause.
2771 int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
2773 int ret;
2775 if ((str1 == NULL) && (str2 == NULL)) return 0;
2776 if (str1 == NULL) return -1;
2777 if (str2 == NULL) return 1;
2779 ret = CompareStringA(GetThreadLocale(), LOCALE_USE_CP_ACP, str1, -1, str2, -1);
2780 if (ret) ret -= 2;
2782 return ret;
2785 /*************************************************************************
2786 * lstrcmpi (KERNEL32.@)
2787 * lstrcmpiA (KERNEL32.@)
2789 * Compare two strings using the current thread locale, ignoring case.
2791 * PARAMS
2792 * str1 [I] First string to compare
2793 * str2 [I] Second string to compare
2795 * RETURNS
2796 * Success: A number less than, equal to or greater than 0 depending on whether
2797 * str2 is less than, equal to or greater than str1 respectively.
2798 * Failure: FALSE. Use GetLastError() to determine the cause.
2800 int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
2802 int ret;
2804 if ((str1 == NULL) && (str2 == NULL)) return 0;
2805 if (str1 == NULL) return -1;
2806 if (str2 == NULL) return 1;
2808 ret = CompareStringA(GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, str1, -1, str2, -1);
2809 if (ret) ret -= 2;
2811 return ret;
2814 /*************************************************************************
2815 * lstrcmpW (KERNEL32.@)
2817 * See lstrcmpA.
2819 int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
2821 int ret;
2823 if ((str1 == NULL) && (str2 == NULL)) return 0;
2824 if (str1 == NULL) return -1;
2825 if (str2 == NULL) return 1;
2827 ret = CompareStringW(GetThreadLocale(), 0, str1, -1, str2, -1);
2828 if (ret) ret -= 2;
2830 return ret;
2833 /*************************************************************************
2834 * lstrcmpiW (KERNEL32.@)
2836 * See lstrcmpiA.
2838 int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
2840 int ret;
2842 if ((str1 == NULL) && (str2 == NULL)) return 0;
2843 if (str1 == NULL) return -1;
2844 if (str2 == NULL) return 1;
2846 ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2847 if (ret) ret -= 2;
2849 return ret;
2852 /******************************************************************************
2853 * LOCALE_Init
2855 void LOCALE_Init(void)
2857 extern void __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp,
2858 const union cptable *unix_cp );
2860 UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp;
2862 #ifdef __APPLE__
2863 /* MacOS doesn't set the locale environment variables so we have to do it ourselves */
2864 CFArrayRef preferred_locales, all_locales;
2865 CFStringRef user_language_string_ref = NULL;
2866 char user_locale[50];
2868 CFLocaleRef user_locale_ref = CFLocaleCopyCurrent();
2869 CFStringRef user_locale_string_ref = CFLocaleGetIdentifier( user_locale_ref );
2871 CFStringGetCString( user_locale_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 );
2872 CFRelease( user_locale_ref );
2873 if (!strchr( user_locale, '.' )) strcat( user_locale, ".UTF-8" );
2874 unix_cp = CP_UTF8; /* default to utf-8 even if we don't get a valid locale */
2875 setenv( "LANG", user_locale, 0 );
2876 TRACE( "setting locale to '%s'\n", user_locale );
2878 /* We still want to set the retrieve the preferred language as chosen in
2879 System Preferences.app, because it can differ from CFLocaleCopyCurrent().
2881 all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
2882 preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );
2883 if (preferred_locales && CFArrayGetCount( preferred_locales ))
2884 user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
2885 CFRelease( all_locales );
2886 #endif /* __APPLE__ */
2888 setlocale( LC_ALL, "" );
2890 unix_cp = setup_unix_locales();
2891 if (!lcid_LC_MESSAGES) lcid_LC_MESSAGES = lcid_LC_CTYPE;
2893 #ifdef __APPLE__
2894 /* Override lcid_LC_MESSAGES with user_language if LC_MESSAGES is set to default */
2895 if (lcid_LC_MESSAGES == lcid_LC_CTYPE && user_language_string_ref)
2897 struct locale_name locale_name;
2898 WCHAR buffer[128];
2899 CFStringGetCString( user_language_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 );
2900 strcpynAtoW( buffer, user_locale, sizeof(buffer)/sizeof(WCHAR) );
2901 parse_locale_name( buffer, &locale_name );
2902 lcid_LC_MESSAGES = locale_name.lcid;
2903 TRACE( "setting lcid_LC_MESSAGES to '%s'\n", user_locale );
2905 if (preferred_locales)
2906 CFRelease( preferred_locales );
2907 #endif
2909 NtSetDefaultUILanguage( LANGIDFROMLCID(lcid_LC_MESSAGES) );
2910 NtSetDefaultLocale( TRUE, lcid_LC_MESSAGES );
2911 NtSetDefaultLocale( FALSE, lcid_LC_CTYPE );
2913 ansi_cp = get_lcid_codepage( LOCALE_USER_DEFAULT );
2914 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
2915 (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
2916 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
2917 (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
2918 if (!unix_cp)
2919 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
2920 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) );
2922 if (!(ansi_cptable = wine_cp_get_table( ansi_cp )))
2923 ansi_cptable = wine_cp_get_table( 1252 );
2924 if (!(oem_cptable = wine_cp_get_table( oem_cp )))
2925 oem_cptable = wine_cp_get_table( 437 );
2926 if (!(mac_cptable = wine_cp_get_table( mac_cp )))
2927 mac_cptable = wine_cp_get_table( 10000 );
2928 if (unix_cp != CP_UTF8)
2930 if (!(unix_cptable = wine_cp_get_table( unix_cp )))
2931 unix_cptable = wine_cp_get_table( 28591 );
2934 __wine_init_codepages( ansi_cptable, oem_cptable, unix_cptable );
2936 TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
2937 ansi_cptable->info.codepage, oem_cptable->info.codepage,
2938 mac_cptable->info.codepage, unix_cp );
2940 setlocale(LC_NUMERIC, "C"); /* FIXME: oleaut32 depends on this */
2943 static HANDLE NLS_RegOpenKey(HANDLE hRootKey, LPCWSTR szKeyName)
2945 UNICODE_STRING keyName;
2946 OBJECT_ATTRIBUTES attr;
2947 HANDLE hkey;
2949 RtlInitUnicodeString( &keyName, szKeyName );
2950 InitializeObjectAttributes(&attr, &keyName, 0, hRootKey, NULL);
2952 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS)
2953 hkey = 0;
2955 return hkey;
2958 static BOOL NLS_RegEnumSubKey(HANDLE hKey, UINT ulIndex, LPWSTR szKeyName,
2959 ULONG keyNameSize)
2961 BYTE buffer[80];
2962 KEY_BASIC_INFORMATION *info = (KEY_BASIC_INFORMATION *)buffer;
2963 DWORD dwLen;
2965 if (NtEnumerateKey( hKey, ulIndex, KeyBasicInformation, buffer,
2966 sizeof(buffer), &dwLen) != STATUS_SUCCESS ||
2967 info->NameLength > keyNameSize)
2969 return FALSE;
2972 TRACE("info->Name %s info->NameLength %d\n", debugstr_w(info->Name), info->NameLength);
2974 memcpy( szKeyName, info->Name, info->NameLength);
2975 szKeyName[info->NameLength / sizeof(WCHAR)] = '\0';
2977 TRACE("returning %s\n", debugstr_w(szKeyName));
2978 return TRUE;
2981 static BOOL NLS_RegEnumValue(HANDLE hKey, UINT ulIndex,
2982 LPWSTR szValueName, ULONG valueNameSize,
2983 LPWSTR szValueData, ULONG valueDataSize)
2985 BYTE buffer[80];
2986 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2987 DWORD dwLen;
2989 if (NtEnumerateValueKey( hKey, ulIndex, KeyValueFullInformation,
2990 buffer, sizeof(buffer), &dwLen ) != STATUS_SUCCESS ||
2991 info->NameLength > valueNameSize ||
2992 info->DataLength > valueDataSize)
2994 return FALSE;
2997 TRACE("info->Name %s info->DataLength %d\n", debugstr_w(info->Name), info->DataLength);
2999 memcpy( szValueName, info->Name, info->NameLength);
3000 szValueName[info->NameLength / sizeof(WCHAR)] = '\0';
3001 memcpy( szValueData, buffer + info->DataOffset, info->DataLength );
3002 szValueData[info->DataLength / sizeof(WCHAR)] = '\0';
3004 TRACE("returning %s %s\n", debugstr_w(szValueName), debugstr_w(szValueData));
3005 return TRUE;
3008 static BOOL NLS_RegGetDword(HANDLE hKey, LPCWSTR szValueName, DWORD *lpVal)
3010 BYTE buffer[128];
3011 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
3012 DWORD dwSize = sizeof(buffer);
3013 UNICODE_STRING valueName;
3015 RtlInitUnicodeString( &valueName, szValueName );
3017 TRACE("%p, %s\n", hKey, debugstr_w(szValueName));
3018 if (NtQueryValueKey( hKey, &valueName, KeyValuePartialInformation,
3019 buffer, dwSize, &dwSize ) == STATUS_SUCCESS &&
3020 info->DataLength == sizeof(DWORD))
3022 memcpy(lpVal, info->Data, sizeof(DWORD));
3023 return TRUE;
3026 return FALSE;
3029 static BOOL NLS_GetLanguageGroupName(LGRPID lgrpid, LPWSTR szName, ULONG nameSize)
3031 LANGID langId;
3032 LPCWSTR szResourceName = MAKEINTRESOURCEW(((lgrpid + 0x2000) >> 4) + 1);
3033 HRSRC hResource;
3034 BOOL bRet = FALSE;
3036 /* FIXME: Is it correct to use the system default langid? */
3037 langId = GetSystemDefaultLangID();
3039 if (SUBLANGID(langId) == SUBLANG_NEUTRAL)
3040 langId = MAKELANGID( PRIMARYLANGID(langId), SUBLANG_DEFAULT );
3042 hResource = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING, szResourceName, langId );
3044 if (hResource)
3046 HGLOBAL hResDir = LoadResource( kernel32_handle, hResource );
3048 if (hResDir)
3050 ULONG iResourceIndex = lgrpid & 0xf;
3051 LPCWSTR lpResEntry = LockResource( hResDir );
3052 ULONG i;
3054 for (i = 0; i < iResourceIndex; i++)
3055 lpResEntry += *lpResEntry + 1;
3057 if (*lpResEntry < nameSize)
3059 memcpy( szName, lpResEntry + 1, *lpResEntry * sizeof(WCHAR) );
3060 szName[*lpResEntry] = '\0';
3061 bRet = TRUE;
3065 FreeResource( hResource );
3067 return bRet;
3070 /* Registry keys for NLS related information */
3072 static const WCHAR szCountryListName[] = {
3073 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
3074 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
3075 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3076 'T','e','l','e','p','h','o','n','y','\\',
3077 'C','o','u','n','t','r','y',' ','L','i','s','t','\0'
3081 /* Callback function ptrs for EnumSystemLanguageGroupsA/W */
3082 typedef struct
3084 LANGUAGEGROUP_ENUMPROCA procA;
3085 LANGUAGEGROUP_ENUMPROCW procW;
3086 DWORD dwFlags;
3087 LONG_PTR lParam;
3088 } ENUMLANGUAGEGROUP_CALLBACKS;
3090 /* Internal implementation of EnumSystemLanguageGroupsA/W */
3091 static BOOL NLS_EnumSystemLanguageGroups(ENUMLANGUAGEGROUP_CALLBACKS *lpProcs)
3093 WCHAR szNumber[10], szValue[4];
3094 HANDLE hKey;
3095 BOOL bContinue = TRUE;
3096 ULONG ulIndex = 0;
3098 if (!lpProcs)
3100 SetLastError(ERROR_INVALID_PARAMETER);
3101 return FALSE;
3104 switch (lpProcs->dwFlags)
3106 case 0:
3107 /* Default to LGRPID_INSTALLED */
3108 lpProcs->dwFlags = LGRPID_INSTALLED;
3109 /* Fall through... */
3110 case LGRPID_INSTALLED:
3111 case LGRPID_SUPPORTED:
3112 break;
3113 default:
3114 SetLastError(ERROR_INVALID_FLAGS);
3115 return FALSE;
3118 hKey = NLS_RegOpenKey( 0, szLangGroupsKeyName );
3120 if (!hKey)
3121 FIXME("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
3123 while (bContinue)
3125 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
3126 szValue, sizeof(szValue) ))
3128 BOOL bInstalled = szValue[0] == '1' ? TRUE : FALSE;
3129 LGRPID lgrpid = strtoulW( szNumber, NULL, 16 );
3131 TRACE("grpid %s (%sinstalled)\n", debugstr_w(szNumber),
3132 bInstalled ? "" : "not ");
3134 if (lpProcs->dwFlags == LGRPID_SUPPORTED || bInstalled)
3136 WCHAR szGrpName[48];
3138 if (!NLS_GetLanguageGroupName( lgrpid, szGrpName, sizeof(szGrpName) / sizeof(WCHAR) ))
3139 szGrpName[0] = '\0';
3141 if (lpProcs->procW)
3142 bContinue = lpProcs->procW( lgrpid, szNumber, szGrpName, lpProcs->dwFlags,
3143 lpProcs->lParam );
3144 else
3146 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
3147 char szGrpNameA[48];
3149 /* FIXME: MSDN doesn't say which code page the W->A translation uses,
3150 * or whether the language names are ever localised. Assume CP_ACP.
3153 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
3154 WideCharToMultiByte(CP_ACP, 0, szGrpName, -1, szGrpNameA, sizeof(szGrpNameA), 0, 0);
3156 bContinue = lpProcs->procA( lgrpid, szNumberA, szGrpNameA, lpProcs->dwFlags,
3157 lpProcs->lParam );
3161 ulIndex++;
3163 else
3164 bContinue = FALSE;
3166 if (!bContinue)
3167 break;
3170 if (hKey)
3171 NtClose( hKey );
3173 return TRUE;
3176 /******************************************************************************
3177 * EnumSystemLanguageGroupsA (KERNEL32.@)
3179 * Call a users function for each language group available on the system.
3181 * PARAMS
3182 * pLangGrpEnumProc [I] Callback function to call for each language group
3183 * dwFlags [I] LGRPID_SUPPORTED=All Supported, LGRPID_INSTALLED=Installed only
3184 * lParam [I] User parameter to pass to pLangGrpEnumProc
3186 * RETURNS
3187 * Success: TRUE.
3188 * Failure: FALSE. Use GetLastError() to determine the cause.
3190 BOOL WINAPI EnumSystemLanguageGroupsA(LANGUAGEGROUP_ENUMPROCA pLangGrpEnumProc,
3191 DWORD dwFlags, LONG_PTR lParam)
3193 ENUMLANGUAGEGROUP_CALLBACKS procs;
3195 TRACE("(%p,0x%08X,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
3197 procs.procA = pLangGrpEnumProc;
3198 procs.procW = NULL;
3199 procs.dwFlags = dwFlags;
3200 procs.lParam = lParam;
3202 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
3205 /******************************************************************************
3206 * EnumSystemLanguageGroupsW (KERNEL32.@)
3208 * See EnumSystemLanguageGroupsA.
3210 BOOL WINAPI EnumSystemLanguageGroupsW(LANGUAGEGROUP_ENUMPROCW pLangGrpEnumProc,
3211 DWORD dwFlags, LONG_PTR lParam)
3213 ENUMLANGUAGEGROUP_CALLBACKS procs;
3215 TRACE("(%p,0x%08X,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
3217 procs.procA = NULL;
3218 procs.procW = pLangGrpEnumProc;
3219 procs.dwFlags = dwFlags;
3220 procs.lParam = lParam;
3222 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
3225 /******************************************************************************
3226 * IsValidLanguageGroup (KERNEL32.@)
3228 * Determine if a language group is supported and/or installed.
3230 * PARAMS
3231 * lgrpid [I] Language Group Id (LGRPID_ values from "winnls.h")
3232 * dwFlags [I] LGRPID_SUPPORTED=Supported, LGRPID_INSTALLED=Installed
3234 * RETURNS
3235 * TRUE, if lgrpid is supported and/or installed, according to dwFlags.
3236 * FALSE otherwise.
3238 BOOL WINAPI IsValidLanguageGroup(LGRPID lgrpid, DWORD dwFlags)
3240 static const WCHAR szFormat[] = { '%','x','\0' };
3241 WCHAR szValueName[16], szValue[2];
3242 BOOL bSupported = FALSE, bInstalled = FALSE;
3243 HANDLE hKey;
3246 switch (dwFlags)
3248 case LGRPID_INSTALLED:
3249 case LGRPID_SUPPORTED:
3251 hKey = NLS_RegOpenKey( 0, szLangGroupsKeyName );
3253 sprintfW( szValueName, szFormat, lgrpid );
3255 if (NLS_RegGetDword( hKey, szValueName, (LPDWORD)&szValue ))
3257 bSupported = TRUE;
3259 if (szValue[0] == '1')
3260 bInstalled = TRUE;
3263 if (hKey)
3264 NtClose( hKey );
3266 break;
3269 if ((dwFlags == LGRPID_SUPPORTED && bSupported) ||
3270 (dwFlags == LGRPID_INSTALLED && bInstalled))
3271 return TRUE;
3273 return FALSE;
3276 /* Callback function ptrs for EnumLanguageGrouplocalesA/W */
3277 typedef struct
3279 LANGGROUPLOCALE_ENUMPROCA procA;
3280 LANGGROUPLOCALE_ENUMPROCW procW;
3281 DWORD dwFlags;
3282 LGRPID lgrpid;
3283 LONG_PTR lParam;
3284 } ENUMLANGUAGEGROUPLOCALE_CALLBACKS;
3286 /* Internal implementation of EnumLanguageGrouplocalesA/W */
3287 static BOOL NLS_EnumLanguageGroupLocales(ENUMLANGUAGEGROUPLOCALE_CALLBACKS *lpProcs)
3289 static const WCHAR szAlternateSortsKeyName[] = {
3290 'A','l','t','e','r','n','a','t','e',' ','S','o','r','t','s','\0'
3292 WCHAR szNumber[10], szValue[4];
3293 HANDLE hKey;
3294 BOOL bContinue = TRUE, bAlternate = FALSE;
3295 LGRPID lgrpid;
3296 ULONG ulIndex = 1; /* Ignore default entry of 1st key */
3298 if (!lpProcs || !lpProcs->lgrpid || lpProcs->lgrpid > LGRPID_ARMENIAN)
3300 SetLastError(ERROR_INVALID_PARAMETER);
3301 return FALSE;
3304 if (lpProcs->dwFlags)
3306 SetLastError(ERROR_INVALID_FLAGS);
3307 return FALSE;
3310 hKey = NLS_RegOpenKey( 0, szLocaleKeyName );
3312 if (!hKey)
3313 WARN("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
3315 while (bContinue)
3317 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
3318 szValue, sizeof(szValue) ))
3320 lgrpid = strtoulW( szValue, NULL, 16 );
3322 TRACE("lcid %s, grpid %d (%smatched)\n", debugstr_w(szNumber),
3323 lgrpid, lgrpid == lpProcs->lgrpid ? "" : "not ");
3325 if (lgrpid == lpProcs->lgrpid)
3327 LCID lcid;
3329 lcid = strtoulW( szNumber, NULL, 16 );
3331 /* FIXME: native returns extra text for a few (17/150) locales, e.g:
3332 * '00000437 ;Georgian'
3333 * At present we only pass the LCID string.
3336 if (lpProcs->procW)
3337 bContinue = lpProcs->procW( lgrpid, lcid, szNumber, lpProcs->lParam );
3338 else
3340 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
3342 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
3344 bContinue = lpProcs->procA( lgrpid, lcid, szNumberA, lpProcs->lParam );
3348 ulIndex++;
3350 else
3352 /* Finished enumerating this key */
3353 if (!bAlternate)
3355 /* Enumerate alternate sorts also */
3356 hKey = NLS_RegOpenKey( hKey, szAlternateSortsKeyName );
3357 bAlternate = TRUE;
3358 ulIndex = 0;
3360 else
3361 bContinue = FALSE; /* Finished both keys */
3364 if (!bContinue)
3365 break;
3368 if (hKey)
3369 NtClose( hKey );
3371 return TRUE;
3374 /******************************************************************************
3375 * EnumLanguageGroupLocalesA (KERNEL32.@)
3377 * Call a users function for every locale in a language group available on the system.
3379 * PARAMS
3380 * pLangGrpLcEnumProc [I] Callback function to call for each locale
3381 * lgrpid [I] Language group (LGRPID_ values from "winnls.h")
3382 * dwFlags [I] Reserved, set to 0
3383 * lParam [I] User parameter to pass to pLangGrpLcEnumProc
3385 * RETURNS
3386 * Success: TRUE.
3387 * Failure: FALSE. Use GetLastError() to determine the cause.
3389 BOOL WINAPI EnumLanguageGroupLocalesA(LANGGROUPLOCALE_ENUMPROCA pLangGrpLcEnumProc,
3390 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
3392 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
3394 TRACE("(%p,0x%08X,0x%08X,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
3396 callbacks.procA = pLangGrpLcEnumProc;
3397 callbacks.procW = NULL;
3398 callbacks.dwFlags = dwFlags;
3399 callbacks.lgrpid = lgrpid;
3400 callbacks.lParam = lParam;
3402 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
3405 /******************************************************************************
3406 * EnumLanguageGroupLocalesW (KERNEL32.@)
3408 * See EnumLanguageGroupLocalesA.
3410 BOOL WINAPI EnumLanguageGroupLocalesW(LANGGROUPLOCALE_ENUMPROCW pLangGrpLcEnumProc,
3411 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
3413 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
3415 TRACE("(%p,0x%08X,0x%08X,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
3417 callbacks.procA = NULL;
3418 callbacks.procW = pLangGrpLcEnumProc;
3419 callbacks.dwFlags = dwFlags;
3420 callbacks.lgrpid = lgrpid;
3421 callbacks.lParam = lParam;
3423 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
3426 /******************************************************************************
3427 * EnumSystemGeoID (KERNEL32.@)
3429 * Call a users function for every location available on the system.
3431 * PARAMS
3432 * geoclass [I] Type of information desired (SYSGEOTYPE enum from "winnls.h")
3433 * reserved [I] Reserved, set to 0
3434 * pGeoEnumProc [I] Callback function to call for each location
3436 * RETURNS
3437 * Success: TRUE.
3438 * Failure: FALSE. Use GetLastError() to determine the cause.
3440 BOOL WINAPI EnumSystemGeoID(GEOCLASS geoclass, GEOID reserved, GEO_ENUMPROC pGeoEnumProc)
3442 static const WCHAR szCountryCodeValueName[] = {
3443 'C','o','u','n','t','r','y','C','o','d','e','\0'
3445 WCHAR szNumber[10];
3446 HANDLE hKey;
3447 ULONG ulIndex = 0;
3449 TRACE("(0x%08X,0x%08X,%p)\n", geoclass, reserved, pGeoEnumProc);
3451 if (geoclass != GEOCLASS_NATION || reserved || !pGeoEnumProc)
3453 SetLastError(ERROR_INVALID_PARAMETER);
3454 return FALSE;
3457 hKey = NLS_RegOpenKey( 0, szCountryListName );
3459 while (NLS_RegEnumSubKey( hKey, ulIndex, szNumber, sizeof(szNumber) ))
3461 BOOL bContinue = TRUE;
3462 DWORD dwGeoId;
3463 HANDLE hSubKey = NLS_RegOpenKey( hKey, szNumber );
3465 if (hSubKey)
3467 if (NLS_RegGetDword( hSubKey, szCountryCodeValueName, &dwGeoId ))
3469 TRACE("Got geoid %d\n", dwGeoId);
3471 if (!pGeoEnumProc( dwGeoId ))
3472 bContinue = FALSE;
3475 NtClose( hSubKey );
3478 if (!bContinue)
3479 break;
3481 ulIndex++;
3484 if (hKey)
3485 NtClose( hKey );
3487 return TRUE;
3490 /******************************************************************************
3491 * InvalidateNLSCache (KERNEL32.@)
3493 * Invalidate the cache of NLS values.
3495 * PARAMS
3496 * None.
3498 * RETURNS
3499 * Success: TRUE.
3500 * Failure: FALSE.
3502 BOOL WINAPI InvalidateNLSCache(void)
3504 FIXME("() stub\n");
3505 return FALSE;
3508 /******************************************************************************
3509 * GetUserGeoID (KERNEL32.@)
3511 GEOID WINAPI GetUserGeoID( GEOCLASS GeoClass )
3513 GEOID ret = GEOID_NOT_AVAILABLE;
3514 static const WCHAR geoW[] = {'G','e','o',0};
3515 static const WCHAR nationW[] = {'N','a','t','i','o','n',0};
3516 WCHAR bufferW[40], *end;
3517 DWORD count;
3518 HANDLE hkey, hSubkey = 0;
3519 UNICODE_STRING keyW;
3520 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
3521 RtlInitUnicodeString( &keyW, nationW );
3522 count = sizeof(bufferW);
3524 if(!(hkey = create_registry_key())) return ret;
3526 switch( GeoClass ){
3527 case GEOCLASS_NATION:
3528 if ((hSubkey = NLS_RegOpenKey(hkey, geoW)))
3530 if((NtQueryValueKey(hSubkey, &keyW, KeyValuePartialInformation,
3531 (LPBYTE)bufferW, count, &count) == STATUS_SUCCESS ) && info->DataLength)
3532 ret = strtolW((LPCWSTR)info->Data, &end, 10);
3534 break;
3535 case GEOCLASS_REGION:
3536 FIXME("GEOCLASS_REGION not handled yet\n");
3537 break;
3540 NtClose(hkey);
3541 if (hSubkey) NtClose(hSubkey);
3542 return ret;
3545 /******************************************************************************
3546 * SetUserGeoID (KERNEL32.@)
3548 BOOL WINAPI SetUserGeoID( GEOID GeoID )
3550 static const WCHAR geoW[] = {'G','e','o',0};
3551 static const WCHAR nationW[] = {'N','a','t','i','o','n',0};
3552 static const WCHAR formatW[] = {'%','i',0};
3553 UNICODE_STRING nameW,keyW;
3554 WCHAR bufferW[10];
3555 OBJECT_ATTRIBUTES attr;
3556 HANDLE hkey;
3558 if(!(hkey = create_registry_key())) return FALSE;
3560 attr.Length = sizeof(attr);
3561 attr.RootDirectory = hkey;
3562 attr.ObjectName = &nameW;
3563 attr.Attributes = 0;
3564 attr.SecurityDescriptor = NULL;
3565 attr.SecurityQualityOfService = NULL;
3566 RtlInitUnicodeString( &nameW, geoW );
3567 RtlInitUnicodeString( &keyW, nationW );
3569 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS)
3572 NtClose(attr.RootDirectory);
3573 return FALSE;
3576 sprintfW(bufferW, formatW, GeoID);
3577 NtSetValueKey(hkey, &keyW, 0, REG_SZ, bufferW, (strlenW(bufferW) + 1) * sizeof(WCHAR));
3578 NtClose(attr.RootDirectory);
3579 NtClose(hkey);
3580 return TRUE;
3583 typedef struct
3585 union
3587 UILANGUAGE_ENUMPROCA procA;
3588 UILANGUAGE_ENUMPROCW procW;
3589 } u;
3590 DWORD flags;
3591 LONG_PTR param;
3592 } ENUM_UILANG_CALLBACK;
3594 static BOOL CALLBACK enum_uilang_proc_a( HMODULE hModule, LPCSTR type,
3595 LPCSTR name, WORD LangID, LONG_PTR lParam )
3597 ENUM_UILANG_CALLBACK *enum_uilang = (ENUM_UILANG_CALLBACK *)lParam;
3598 char buf[20];
3600 sprintf(buf, "%08x", (UINT)LangID);
3601 return enum_uilang->u.procA( buf, enum_uilang->param );
3604 static BOOL CALLBACK enum_uilang_proc_w( HMODULE hModule, LPCWSTR type,
3605 LPCWSTR name, WORD LangID, LONG_PTR lParam )
3607 static const WCHAR formatW[] = {'%','0','8','x',0};
3608 ENUM_UILANG_CALLBACK *enum_uilang = (ENUM_UILANG_CALLBACK *)lParam;
3609 WCHAR buf[20];
3611 sprintfW( buf, formatW, (UINT)LangID );
3612 return enum_uilang->u.procW( buf, enum_uilang->param );
3615 /******************************************************************************
3616 * EnumUILanguagesA (KERNEL32.@)
3618 BOOL WINAPI EnumUILanguagesA(UILANGUAGE_ENUMPROCA pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
3620 ENUM_UILANG_CALLBACK enum_uilang;
3622 TRACE("%p, %x, %lx\n", pUILangEnumProc, dwFlags, lParam);
3624 if(!pUILangEnumProc) {
3625 SetLastError(ERROR_INVALID_PARAMETER);
3626 return FALSE;
3628 if(dwFlags) {
3629 SetLastError(ERROR_INVALID_FLAGS);
3630 return FALSE;
3633 enum_uilang.u.procA = pUILangEnumProc;
3634 enum_uilang.flags = dwFlags;
3635 enum_uilang.param = lParam;
3637 EnumResourceLanguagesA( kernel32_handle, (LPCSTR)RT_STRING,
3638 (LPCSTR)LOCALE_ILANGUAGE, enum_uilang_proc_a,
3639 (LONG_PTR)&enum_uilang);
3640 return TRUE;
3643 /******************************************************************************
3644 * EnumUILanguagesW (KERNEL32.@)
3646 BOOL WINAPI EnumUILanguagesW(UILANGUAGE_ENUMPROCW pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
3648 ENUM_UILANG_CALLBACK enum_uilang;
3650 TRACE("%p, %x, %lx\n", pUILangEnumProc, dwFlags, lParam);
3653 if(!pUILangEnumProc) {
3654 SetLastError(ERROR_INVALID_PARAMETER);
3655 return FALSE;
3657 if(dwFlags) {
3658 SetLastError(ERROR_INVALID_FLAGS);
3659 return FALSE;
3662 enum_uilang.u.procW = pUILangEnumProc;
3663 enum_uilang.flags = dwFlags;
3664 enum_uilang.param = lParam;
3666 EnumResourceLanguagesW( kernel32_handle, (LPCWSTR)RT_STRING,
3667 (LPCWSTR)LOCALE_ILANGUAGE, enum_uilang_proc_w,
3668 (LONG_PTR)&enum_uilang);
3669 return TRUE;
3672 INT WINAPI GetGeoInfoW(GEOID GeoId, GEOTYPE GeoType, LPWSTR lpGeoData,
3673 int cchData, LANGID language)
3675 FIXME("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, language);
3676 return 0;
3679 INT WINAPI GetGeoInfoA(GEOID GeoId, GEOTYPE GeoType, LPSTR lpGeoData,
3680 int cchData, LANGID language)
3682 FIXME("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, language);
3683 return 0;