mshtml: Check for null pointers in IHTMLLocation_get*.
[wine/hramrach.git] / dlls / kernel32 / locale.c
blob9071ff984d66f22755b9eed7ae887113b8c08065
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 = entry;
245 return strcasecmp( 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, 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_SABBREVLANGNAME,
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_SABBREVLANGNAME: 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 #ifdef __APPLE__
1834 flags |= MB_COMPOSITE; /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
1835 #endif
1836 /* fall through */
1837 case CP_UTF8:
1838 ret = wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
1839 break;
1840 default:
1841 if (!(table = get_codepage_table( page )))
1843 SetLastError( ERROR_INVALID_PARAMETER );
1844 return 0;
1846 ret = wine_cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
1847 break;
1850 if (ret < 0)
1852 switch(ret)
1854 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1855 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1857 ret = 0;
1859 TRACE("cp %d %s -> %s, ret = %d\n",
1860 page, debugstr_an(src, srclen), debugstr_wn(dst, ret), ret);
1861 return ret;
1865 /***********************************************************************
1866 * WideCharToMultiByte (KERNEL32.@)
1868 * Convert a Unicode character string into a multibyte string.
1870 * PARAMS
1871 * page [I] Code page character set to convert to
1872 * flags [I] Mapping Flags (MB_ constants from "winnls.h").
1873 * src [I] Source string buffer
1874 * srclen [I] Length of src (in WCHARs), or -1 if src is NUL terminated
1875 * dst [O] Destination buffer
1876 * dstlen [I] Length of dst (in bytes), or 0 to compute the required length
1877 * defchar [I] Default character to use for conversion if no exact
1878 * conversion can be made
1879 * used [O] Set if default character was used in the conversion
1881 * RETURNS
1882 * Success: If dstlen > 0, the number of characters written to dst.
1883 * If dstlen == 0, number of characters needed to perform the
1884 * conversion. In both cases the count includes the terminating NUL.
1885 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1886 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1887 * and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
1888 * parameter was given.
1890 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
1891 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
1893 const union cptable *table;
1894 int ret, used_tmp;
1896 if (!src || (!dst && dstlen))
1898 SetLastError( ERROR_INVALID_PARAMETER );
1899 return 0;
1902 if (srclen < 0) srclen = strlenW(src) + 1;
1904 switch(page)
1906 case CP_SYMBOL:
1907 if( flags || defchar || used)
1909 SetLastError( ERROR_INVALID_PARAMETER );
1910 return 0;
1912 ret = wine_cpsymbol_wcstombs( src, srclen, dst, dstlen );
1913 break;
1914 case CP_UTF7:
1915 FIXME("UTF-7 not supported\n");
1916 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1917 return 0;
1918 case CP_UNIXCP:
1919 if (unix_cptable)
1921 ret = wine_cp_wcstombs( unix_cptable, flags, src, srclen, dst, dstlen,
1922 defchar, used ? &used_tmp : NULL );
1923 break;
1925 /* fall through */
1926 case CP_UTF8:
1927 if (used) *used = FALSE; /* all chars are valid for UTF-8 */
1928 ret = wine_utf8_wcstombs( flags, src, srclen, dst, dstlen );
1929 break;
1930 default:
1931 if (!(table = get_codepage_table( page )))
1933 SetLastError( ERROR_INVALID_PARAMETER );
1934 return 0;
1936 ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
1937 defchar, used ? &used_tmp : NULL );
1938 if (used) *used = used_tmp;
1939 break;
1942 if (ret < 0)
1944 switch(ret)
1946 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1947 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1949 ret = 0;
1951 TRACE("cp %d %s -> %s, ret = %d\n",
1952 page, debugstr_wn(src, srclen), debugstr_an(dst, ret), ret);
1953 return ret;
1957 /***********************************************************************
1958 * GetThreadLocale (KERNEL32.@)
1960 * Get the current threads locale.
1962 * PARAMS
1963 * None.
1965 * RETURNS
1966 * The LCID currently associated with the calling thread.
1968 LCID WINAPI GetThreadLocale(void)
1970 LCID ret = NtCurrentTeb()->CurrentLocale;
1971 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
1972 return ret;
1975 /**********************************************************************
1976 * SetThreadLocale (KERNEL32.@)
1978 * Set the current threads locale.
1980 * PARAMS
1981 * lcid [I] LCID of the locale to set
1983 * RETURNS
1984 * Success: TRUE. The threads locale is set to lcid.
1985 * Failure: FALSE. Use GetLastError() to determine the cause.
1987 BOOL WINAPI SetThreadLocale( LCID lcid )
1989 TRACE("(0x%04X)\n", lcid);
1991 lcid = ConvertDefaultLocale(lcid);
1993 if (lcid != GetThreadLocale())
1995 if (!IsValidLocale(lcid, LCID_SUPPORTED))
1997 SetLastError(ERROR_INVALID_PARAMETER);
1998 return FALSE;
2001 NtCurrentTeb()->CurrentLocale = lcid;
2002 kernel_get_thread_data()->code_page = get_lcid_codepage( lcid );
2004 return TRUE;
2007 /**********************************************************************
2008 * SetThreadUILanguage (KERNEL32.@)
2010 * Set the current threads UI language.
2012 * PARAMS
2013 * langid [I] LANGID of the language to set, or 0 to use
2014 * the available language which is best supported
2015 * for console applications
2017 * RETURNS
2018 * Success: The return value is the same as the input value.
2019 * Failure: The return value differs from the input value.
2020 * Use GetLastError() to determine the cause.
2022 LANGID WINAPI SetThreadUILanguage( LANGID langid )
2024 TRACE("(0x%04x) stub - returning success\n", langid);
2025 return langid;
2028 /******************************************************************************
2029 * ConvertDefaultLocale (KERNEL32.@)
2031 * Convert a default locale identifier into a real identifier.
2033 * PARAMS
2034 * lcid [I] LCID identifier of the locale to convert
2036 * RETURNS
2037 * lcid unchanged, if not a default locale or its sublanguage is
2038 * not SUBLANG_NEUTRAL.
2039 * GetSystemDefaultLCID(), if lcid == LOCALE_SYSTEM_DEFAULT.
2040 * GetUserDefaultLCID(), if lcid == LOCALE_USER_DEFAULT or LOCALE_NEUTRAL.
2041 * Otherwise, lcid with sublanguage changed to SUBLANG_DEFAULT.
2043 LCID WINAPI ConvertDefaultLocale( LCID lcid )
2045 LANGID langid;
2047 switch (lcid)
2049 case LOCALE_SYSTEM_DEFAULT:
2050 lcid = GetSystemDefaultLCID();
2051 break;
2052 case LOCALE_USER_DEFAULT:
2053 case LOCALE_NEUTRAL:
2054 lcid = GetUserDefaultLCID();
2055 break;
2056 default:
2057 /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
2058 langid = LANGIDFROMLCID(lcid);
2059 if (SUBLANGID(langid) == SUBLANG_NEUTRAL)
2061 langid = MAKELANGID(PRIMARYLANGID(langid), SUBLANG_DEFAULT);
2062 lcid = MAKELCID(langid, SORTIDFROMLCID(lcid));
2065 return lcid;
2069 /******************************************************************************
2070 * IsValidLocale (KERNEL32.@)
2072 * Determine if a locale is valid.
2074 * PARAMS
2075 * lcid [I] LCID of the locale to check
2076 * flags [I] LCID_SUPPORTED = Valid, LCID_INSTALLED = Valid and installed on the system
2078 * RETURNS
2079 * TRUE, if lcid is valid,
2080 * FALSE, otherwise.
2082 * NOTES
2083 * Wine does not currently make the distinction between supported and installed. All
2084 * languages supported are installed by default.
2086 BOOL WINAPI IsValidLocale( LCID lcid, DWORD flags )
2088 /* check if language is registered in the kernel32 resources */
2089 return FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
2090 (LPCWSTR)LOCALE_ILANGUAGE, LANGIDFROMLCID(lcid)) != 0;
2094 static BOOL CALLBACK enum_lang_proc_a( HMODULE hModule, LPCSTR type,
2095 LPCSTR name, WORD LangID, LONG_PTR lParam )
2097 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
2098 char buf[20];
2100 sprintf(buf, "%08x", (UINT)LangID);
2101 return lpfnLocaleEnum( buf );
2104 static BOOL CALLBACK enum_lang_proc_w( HMODULE hModule, LPCWSTR type,
2105 LPCWSTR name, WORD LangID, LONG_PTR lParam )
2107 static const WCHAR formatW[] = {'%','0','8','x',0};
2108 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
2109 WCHAR buf[20];
2110 sprintfW( buf, formatW, (UINT)LangID );
2111 return lpfnLocaleEnum( buf );
2114 /******************************************************************************
2115 * EnumSystemLocalesA (KERNEL32.@)
2117 * Call a users function for each locale available on the system.
2119 * PARAMS
2120 * lpfnLocaleEnum [I] Callback function to call for each locale
2121 * dwFlags [I] LOCALE_SUPPORTED=All supported, LOCALE_INSTALLED=Installed only
2123 * RETURNS
2124 * Success: TRUE.
2125 * Failure: FALSE. Use GetLastError() to determine the cause.
2127 BOOL WINAPI EnumSystemLocalesA( LOCALE_ENUMPROCA lpfnLocaleEnum, DWORD dwFlags )
2129 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
2130 EnumResourceLanguagesA( kernel32_handle, (LPSTR)RT_STRING,
2131 (LPCSTR)LOCALE_ILANGUAGE, enum_lang_proc_a,
2132 (LONG_PTR)lpfnLocaleEnum);
2133 return TRUE;
2137 /******************************************************************************
2138 * EnumSystemLocalesW (KERNEL32.@)
2140 * See EnumSystemLocalesA.
2142 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum, DWORD dwFlags )
2144 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
2145 EnumResourceLanguagesW( kernel32_handle, (LPWSTR)RT_STRING,
2146 (LPCWSTR)LOCALE_ILANGUAGE, enum_lang_proc_w,
2147 (LONG_PTR)lpfnLocaleEnum);
2148 return TRUE;
2152 /***********************************************************************
2153 * VerLanguageNameA (KERNEL32.@)
2155 * Get the name of a language.
2157 * PARAMS
2158 * wLang [I] LANGID of the language
2159 * szLang [O] Destination for the language name
2161 * RETURNS
2162 * Success: The size of the language name. If szLang is non-NULL, it is filled
2163 * with the name.
2164 * Failure: 0. Use GetLastError() to determine the cause.
2167 DWORD WINAPI VerLanguageNameA( DWORD wLang, LPSTR szLang, DWORD nSize )
2169 return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
2173 /***********************************************************************
2174 * VerLanguageNameW (KERNEL32.@)
2176 * See VerLanguageNameA.
2178 DWORD WINAPI VerLanguageNameW( DWORD wLang, LPWSTR szLang, DWORD nSize )
2180 return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
2184 /******************************************************************************
2185 * GetStringTypeW (KERNEL32.@)
2187 * See GetStringTypeA.
2189 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
2191 if (count == -1) count = strlenW(src) + 1;
2192 switch(type)
2194 case CT_CTYPE1:
2195 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
2196 break;
2197 case CT_CTYPE2:
2198 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
2199 break;
2200 case CT_CTYPE3:
2202 WARN("CT_CTYPE3: semi-stub.\n");
2203 while (count--)
2205 int c = *src;
2206 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
2208 type1 = get_char_typeW( *src++ ) & 0xfff;
2209 /* try to construct type3 from type1 */
2210 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
2211 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
2212 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
2213 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
2214 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
2215 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
2216 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
2218 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
2219 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
2220 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
2221 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
2222 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
2223 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
2224 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
2225 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
2227 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
2228 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
2229 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
2230 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
2231 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
2232 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
2233 *chartype++ = type3;
2235 break;
2237 default:
2238 SetLastError( ERROR_INVALID_PARAMETER );
2239 return FALSE;
2241 return TRUE;
2245 /******************************************************************************
2246 * GetStringTypeExW (KERNEL32.@)
2248 * See GetStringTypeExA.
2250 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
2252 /* locale is ignored for Unicode */
2253 return GetStringTypeW( type, src, count, chartype );
2257 /******************************************************************************
2258 * GetStringTypeA (KERNEL32.@)
2260 * Get characteristics of the characters making up a string.
2262 * PARAMS
2263 * locale [I] Locale Id for the string
2264 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
2265 * src [I] String to analyse
2266 * count [I] Length of src in chars, or -1 if src is NUL terminated
2267 * chartype [O] Destination for the calculated characteristics
2269 * RETURNS
2270 * Success: TRUE. chartype is filled with the requested characteristics of each char
2271 * in src.
2272 * Failure: FALSE. Use GetLastError() to determine the cause.
2274 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
2276 UINT cp;
2277 INT countW;
2278 LPWSTR srcW;
2279 BOOL ret = FALSE;
2281 if(count == -1) count = strlen(src) + 1;
2283 if (!(cp = get_lcid_codepage( locale )))
2285 FIXME("For locale %04x using current ANSI code page\n", locale);
2286 cp = GetACP();
2289 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
2290 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
2292 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
2294 * NOTE: the target buffer has 1 word for each CHARACTER in the source
2295 * string, with multibyte characters there maybe be more bytes in count
2296 * than character space in the buffer!
2298 ret = GetStringTypeW(type, srcW, countW, chartype);
2299 HeapFree(GetProcessHeap(), 0, srcW);
2301 return ret;
2304 /******************************************************************************
2305 * GetStringTypeExA (KERNEL32.@)
2307 * Get characteristics of the characters making up a string.
2309 * PARAMS
2310 * locale [I] Locale Id for the string
2311 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
2312 * src [I] String to analyse
2313 * count [I] Length of src in chars, or -1 if src is NUL terminated
2314 * chartype [O] Destination for the calculated characteristics
2316 * RETURNS
2317 * Success: TRUE. chartype is filled with the requested characteristics of each char
2318 * in src.
2319 * Failure: FALSE. Use GetLastError() to determine the cause.
2321 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
2323 return GetStringTypeA(locale, type, src, count, chartype);
2327 /*************************************************************************
2328 * LCMapStringW (KERNEL32.@)
2330 * See LCMapStringA.
2332 INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
2333 LPWSTR dst, INT dstlen)
2335 LPWSTR dst_ptr;
2337 if (!src || !srclen || dstlen < 0)
2339 SetLastError(ERROR_INVALID_PARAMETER);
2340 return 0;
2343 /* mutually exclusive flags */
2344 if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
2345 (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
2346 (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
2347 (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE))
2349 SetLastError(ERROR_INVALID_FLAGS);
2350 return 0;
2353 if (!dstlen) dst = NULL;
2355 lcid = ConvertDefaultLocale(lcid);
2357 if (flags & LCMAP_SORTKEY)
2359 INT ret;
2360 if (src == dst)
2362 SetLastError(ERROR_INVALID_FLAGS);
2363 return 0;
2366 if (srclen < 0) srclen = strlenW(src);
2368 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
2369 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
2371 ret = wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
2372 if (ret == 0)
2373 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2374 else
2375 ret++;
2376 return ret;
2379 /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
2380 if (flags & SORT_STRINGSORT)
2382 SetLastError(ERROR_INVALID_FLAGS);
2383 return 0;
2386 if (srclen < 0) srclen = strlenW(src) + 1;
2388 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
2389 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
2391 if (!dst) /* return required string length */
2393 INT len;
2395 for (len = 0; srclen; src++, srclen--)
2397 WCHAR wch = *src;
2398 /* tests show that win2k just ignores NORM_IGNORENONSPACE,
2399 * and skips white space and punctuation characters for
2400 * NORM_IGNORESYMBOLS.
2402 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2403 continue;
2404 len++;
2406 return len;
2409 if (flags & LCMAP_UPPERCASE)
2411 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2413 WCHAR wch = *src;
2414 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2415 continue;
2416 *dst_ptr++ = toupperW(wch);
2417 dstlen--;
2420 else if (flags & LCMAP_LOWERCASE)
2422 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2424 WCHAR wch = *src;
2425 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2426 continue;
2427 *dst_ptr++ = tolowerW(wch);
2428 dstlen--;
2431 else
2433 if (src == dst)
2435 SetLastError(ERROR_INVALID_FLAGS);
2436 return 0;
2438 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2440 WCHAR wch = *src;
2441 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2442 continue;
2443 *dst_ptr++ = wch;
2444 dstlen--;
2448 if (srclen)
2450 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2451 return 0;
2454 return dst_ptr - dst;
2457 /*************************************************************************
2458 * LCMapStringA (KERNEL32.@)
2460 * Map characters in a locale sensitive string.
2462 * PARAMS
2463 * lcid [I] LCID for the conversion.
2464 * flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
2465 * src [I] String to map
2466 * srclen [I] Length of src in chars, or -1 if src is NUL terminated
2467 * dst [O] Destination for mapped string
2468 * dstlen [I] Length of dst in characters
2470 * RETURNS
2471 * Success: The length of the mapped string in dst, including the NUL terminator.
2472 * Failure: 0. Use GetLastError() to determine the cause.
2474 INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
2475 LPSTR dst, INT dstlen)
2477 WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
2478 LPWSTR srcW, dstW;
2479 INT ret = 0, srclenW, dstlenW;
2480 UINT locale_cp = CP_ACP;
2482 if (!src || !srclen || dstlen < 0)
2484 SetLastError(ERROR_INVALID_PARAMETER);
2485 return 0;
2488 if (!(flags & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
2490 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
2491 if (srclenW)
2492 srcW = bufW;
2493 else
2495 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
2496 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2497 if (!srcW)
2499 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2500 return 0;
2502 MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
2505 if (flags & LCMAP_SORTKEY)
2507 if (src == dst)
2509 SetLastError(ERROR_INVALID_FLAGS);
2510 goto map_string_exit;
2512 ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
2513 if (ret == 0)
2514 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2515 else
2516 ret++;
2517 goto map_string_exit;
2520 if (flags & SORT_STRINGSORT)
2522 SetLastError(ERROR_INVALID_FLAGS);
2523 goto map_string_exit;
2526 dstlenW = LCMapStringW(lcid, flags, srcW, srclenW, NULL, 0);
2527 if (!dstlenW)
2528 goto map_string_exit;
2530 dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
2531 if (!dstW)
2533 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2534 goto map_string_exit;
2537 LCMapStringW(lcid, flags, srcW, srclenW, dstW, dstlenW);
2538 ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
2539 HeapFree(GetProcessHeap(), 0, dstW);
2541 map_string_exit:
2542 if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
2543 return ret;
2546 /*************************************************************************
2547 * FoldStringA (KERNEL32.@)
2549 * Map characters in a string.
2551 * PARAMS
2552 * dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
2553 * src [I] String to map
2554 * srclen [I] Length of src, or -1 if src is NUL terminated
2555 * dst [O] Destination for mapped string
2556 * dstlen [I] Length of dst, or 0 to find the required length for the mapped string
2558 * RETURNS
2559 * Success: The length of the string written to dst, including the terminating NUL. If
2560 * dstlen is 0, the value returned is the same, but nothing is written to dst,
2561 * and dst may be NULL.
2562 * Failure: 0. Use GetLastError() to determine the cause.
2564 INT WINAPI FoldStringA(DWORD dwFlags, LPCSTR src, INT srclen,
2565 LPSTR dst, INT dstlen)
2567 INT ret = 0, srclenW = 0;
2568 WCHAR *srcW = NULL, *dstW = NULL;
2570 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2572 SetLastError(ERROR_INVALID_PARAMETER);
2573 return 0;
2576 srclenW = MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2577 src, srclen, NULL, 0);
2578 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2580 if (!srcW)
2582 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2583 goto FoldStringA_exit;
2586 MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2587 src, srclen, srcW, srclenW);
2589 dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
2591 ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
2592 if (ret && dstlen)
2594 dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
2596 if (!dstW)
2598 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2599 goto FoldStringA_exit;
2602 ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
2603 if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
2605 ret = 0;
2606 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2610 HeapFree(GetProcessHeap(), 0, dstW);
2612 FoldStringA_exit:
2613 HeapFree(GetProcessHeap(), 0, srcW);
2614 return ret;
2617 /*************************************************************************
2618 * FoldStringW (KERNEL32.@)
2620 * See FoldStringA.
2622 INT WINAPI FoldStringW(DWORD dwFlags, LPCWSTR src, INT srclen,
2623 LPWSTR dst, INT dstlen)
2625 int ret;
2627 switch (dwFlags & (MAP_COMPOSITE|MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES))
2629 case 0:
2630 if (dwFlags)
2631 break;
2632 /* Fall through for dwFlags == 0 */
2633 case MAP_PRECOMPOSED|MAP_COMPOSITE:
2634 case MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES:
2635 case MAP_COMPOSITE|MAP_EXPAND_LIGATURES:
2636 SetLastError(ERROR_INVALID_FLAGS);
2637 return 0;
2640 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2642 SetLastError(ERROR_INVALID_PARAMETER);
2643 return 0;
2646 ret = wine_fold_string(dwFlags, src, srclen, dst, dstlen);
2647 if (!ret)
2648 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2649 return ret;
2652 /******************************************************************************
2653 * CompareStringW (KERNEL32.@)
2655 * See CompareStringA.
2657 INT WINAPI CompareStringW(LCID lcid, DWORD style,
2658 LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
2660 INT ret;
2662 if (!str1 || !str2)
2664 SetLastError(ERROR_INVALID_PARAMETER);
2665 return 0;
2668 if( style & ~(NORM_IGNORECASE|NORM_IGNORENONSPACE|NORM_IGNORESYMBOLS|
2669 SORT_STRINGSORT|NORM_IGNOREKANATYPE|NORM_IGNOREWIDTH|LOCALE_USE_CP_ACP|0x10000000) )
2671 SetLastError(ERROR_INVALID_FLAGS);
2672 return 0;
2675 /* this style is related to diacritics in Arabic, Japanese, and Hebrew */
2676 if (style & 0x10000000)
2677 WARN("Ignoring unknown style 0x10000000\n");
2679 if (len1 < 0) len1 = strlenW(str1);
2680 if (len2 < 0) len2 = strlenW(str2);
2682 ret = wine_compare_string(style, str1, len1, str2, len2);
2684 if (ret) /* need to translate result */
2685 return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
2686 return CSTR_EQUAL;
2689 /******************************************************************************
2690 * CompareStringA (KERNEL32.@)
2692 * Compare two locale sensitive strings.
2694 * PARAMS
2695 * lcid [I] LCID for the comparison
2696 * style [I] Flags for the comparison (NORM_ constants from "winnls.h").
2697 * str1 [I] First string to compare
2698 * len1 [I] Length of str1, or -1 if str1 is NUL terminated
2699 * str2 [I] Second string to compare
2700 * len2 [I] Length of str2, or -1 if str2 is NUL terminated
2702 * RETURNS
2703 * Success: CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN depending on whether
2704 * str2 is less than, equal to or greater than str1 respectively.
2705 * Failure: FALSE. Use GetLastError() to determine the cause.
2707 INT WINAPI CompareStringA(LCID lcid, DWORD style,
2708 LPCSTR str1, INT len1, LPCSTR str2, INT len2)
2710 WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
2711 WCHAR *buf2W = buf1W + 130;
2712 LPWSTR str1W, str2W;
2713 INT len1W, len2W, ret;
2714 UINT locale_cp = CP_ACP;
2716 if (!str1 || !str2)
2718 SetLastError(ERROR_INVALID_PARAMETER);
2719 return 0;
2721 if (len1 < 0) len1 = strlen(str1);
2722 if (len2 < 0) len2 = strlen(str2);
2724 if (!(style & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
2726 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 130);
2727 if (len1W)
2728 str1W = buf1W;
2729 else
2731 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
2732 str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
2733 if (!str1W)
2735 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2736 return 0;
2738 MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
2740 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 130);
2741 if (len2W)
2742 str2W = buf2W;
2743 else
2745 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
2746 str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
2747 if (!str2W)
2749 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2750 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2751 return 0;
2753 MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
2756 ret = CompareStringW(lcid, style, str1W, len1W, str2W, len2W);
2758 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2759 if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
2760 return ret;
2763 /*************************************************************************
2764 * lstrcmp (KERNEL32.@)
2765 * lstrcmpA (KERNEL32.@)
2767 * Compare two strings using the current thread locale.
2769 * PARAMS
2770 * str1 [I] First string to compare
2771 * str2 [I] Second string to compare
2773 * RETURNS
2774 * Success: A number less than, equal to or greater than 0 depending on whether
2775 * str2 is less than, equal to or greater than str1 respectively.
2776 * Failure: FALSE. Use GetLastError() to determine the cause.
2778 int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
2780 int ret;
2782 if ((str1 == NULL) && (str2 == NULL)) return 0;
2783 if (str1 == NULL) return -1;
2784 if (str2 == NULL) return 1;
2786 ret = CompareStringA(GetThreadLocale(), LOCALE_USE_CP_ACP, str1, -1, str2, -1);
2787 if (ret) ret -= 2;
2789 return ret;
2792 /*************************************************************************
2793 * lstrcmpi (KERNEL32.@)
2794 * lstrcmpiA (KERNEL32.@)
2796 * Compare two strings using the current thread locale, ignoring case.
2798 * PARAMS
2799 * str1 [I] First string to compare
2800 * str2 [I] Second string to compare
2802 * RETURNS
2803 * Success: A number less than, equal to or greater than 0 depending on whether
2804 * str2 is less than, equal to or greater than str1 respectively.
2805 * Failure: FALSE. Use GetLastError() to determine the cause.
2807 int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
2809 int ret;
2811 if ((str1 == NULL) && (str2 == NULL)) return 0;
2812 if (str1 == NULL) return -1;
2813 if (str2 == NULL) return 1;
2815 ret = CompareStringA(GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, str1, -1, str2, -1);
2816 if (ret) ret -= 2;
2818 return ret;
2821 /*************************************************************************
2822 * lstrcmpW (KERNEL32.@)
2824 * See lstrcmpA.
2826 int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
2828 int ret;
2830 if ((str1 == NULL) && (str2 == NULL)) return 0;
2831 if (str1 == NULL) return -1;
2832 if (str2 == NULL) return 1;
2834 ret = CompareStringW(GetThreadLocale(), 0, str1, -1, str2, -1);
2835 if (ret) ret -= 2;
2837 return ret;
2840 /*************************************************************************
2841 * lstrcmpiW (KERNEL32.@)
2843 * See lstrcmpiA.
2845 int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
2847 int ret;
2849 if ((str1 == NULL) && (str2 == NULL)) return 0;
2850 if (str1 == NULL) return -1;
2851 if (str2 == NULL) return 1;
2853 ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2854 if (ret) ret -= 2;
2856 return ret;
2859 /******************************************************************************
2860 * LOCALE_Init
2862 void LOCALE_Init(void)
2864 extern void CDECL __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp,
2865 const union cptable *unix_cp );
2867 UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp;
2869 #ifdef __APPLE__
2870 /* MacOS doesn't set the locale environment variables so we have to do it ourselves */
2871 CFArrayRef preferred_locales, all_locales;
2872 CFStringRef user_language_string_ref = NULL;
2873 char user_locale[50];
2874 char* p;
2876 CFLocaleRef user_locale_ref = CFLocaleCopyCurrent();
2877 CFStringRef user_locale_string_ref = CFLocaleGetIdentifier( user_locale_ref );
2879 CFStringGetCString( user_locale_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 );
2880 CFRelease( user_locale_ref );
2881 /* Strip modifiers because setlocale() can't parse them. */
2882 if ((p = strchr( user_locale, '@' ))) *p = 0;
2883 if (!strchr( user_locale, '.' )) strcat( user_locale, ".UTF-8" );
2884 unix_cp = CP_UTF8; /* default to utf-8 even if we don't get a valid locale */
2885 setenv( "LANG", user_locale, 0 );
2886 TRACE( "setting locale to '%s'\n", user_locale );
2888 /* We still want to set the retrieve the preferred language as chosen in
2889 System Preferences.app, because it can differ from CFLocaleCopyCurrent().
2891 all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
2892 preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );
2893 if (preferred_locales && CFArrayGetCount( preferred_locales ))
2894 user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
2895 CFRelease( all_locales );
2896 #endif /* __APPLE__ */
2898 setlocale( LC_ALL, "" );
2900 unix_cp = setup_unix_locales();
2901 if (!lcid_LC_MESSAGES) lcid_LC_MESSAGES = lcid_LC_CTYPE;
2903 #ifdef __APPLE__
2904 /* Override lcid_LC_MESSAGES with user_language if LC_MESSAGES is set to default */
2905 if (lcid_LC_MESSAGES == lcid_LC_CTYPE && user_language_string_ref)
2907 struct locale_name locale_name;
2908 WCHAR buffer[128];
2909 CFStringGetCString( user_language_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 );
2910 strcpynAtoW( buffer, user_locale, sizeof(buffer)/sizeof(WCHAR) );
2911 parse_locale_name( buffer, &locale_name );
2912 lcid_LC_MESSAGES = locale_name.lcid;
2913 TRACE( "setting lcid_LC_MESSAGES to '%s'\n", user_locale );
2915 if (preferred_locales)
2916 CFRelease( preferred_locales );
2917 #endif
2919 NtSetDefaultUILanguage( LANGIDFROMLCID(lcid_LC_MESSAGES) );
2920 NtSetDefaultLocale( TRUE, lcid_LC_MESSAGES );
2921 NtSetDefaultLocale( FALSE, lcid_LC_CTYPE );
2923 ansi_cp = get_lcid_codepage( LOCALE_USER_DEFAULT );
2924 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
2925 (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
2926 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
2927 (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
2928 if (!unix_cp)
2929 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
2930 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) );
2932 if (!(ansi_cptable = wine_cp_get_table( ansi_cp )))
2933 ansi_cptable = wine_cp_get_table( 1252 );
2934 if (!(oem_cptable = wine_cp_get_table( oem_cp )))
2935 oem_cptable = wine_cp_get_table( 437 );
2936 if (!(mac_cptable = wine_cp_get_table( mac_cp )))
2937 mac_cptable = wine_cp_get_table( 10000 );
2938 if (unix_cp != CP_UTF8)
2940 if (!(unix_cptable = wine_cp_get_table( unix_cp )))
2941 unix_cptable = wine_cp_get_table( 28591 );
2944 __wine_init_codepages( ansi_cptable, oem_cptable, unix_cptable );
2946 TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
2947 ansi_cptable->info.codepage, oem_cptable->info.codepage,
2948 mac_cptable->info.codepage, unix_cp );
2950 setlocale(LC_NUMERIC, "C"); /* FIXME: oleaut32 depends on this */
2953 static HANDLE NLS_RegOpenKey(HANDLE hRootKey, LPCWSTR szKeyName)
2955 UNICODE_STRING keyName;
2956 OBJECT_ATTRIBUTES attr;
2957 HANDLE hkey;
2959 RtlInitUnicodeString( &keyName, szKeyName );
2960 InitializeObjectAttributes(&attr, &keyName, 0, hRootKey, NULL);
2962 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS)
2963 hkey = 0;
2965 return hkey;
2968 static BOOL NLS_RegEnumSubKey(HANDLE hKey, UINT ulIndex, LPWSTR szKeyName,
2969 ULONG keyNameSize)
2971 BYTE buffer[80];
2972 KEY_BASIC_INFORMATION *info = (KEY_BASIC_INFORMATION *)buffer;
2973 DWORD dwLen;
2975 if (NtEnumerateKey( hKey, ulIndex, KeyBasicInformation, buffer,
2976 sizeof(buffer), &dwLen) != STATUS_SUCCESS ||
2977 info->NameLength > keyNameSize)
2979 return FALSE;
2982 TRACE("info->Name %s info->NameLength %d\n", debugstr_w(info->Name), info->NameLength);
2984 memcpy( szKeyName, info->Name, info->NameLength);
2985 szKeyName[info->NameLength / sizeof(WCHAR)] = '\0';
2987 TRACE("returning %s\n", debugstr_w(szKeyName));
2988 return TRUE;
2991 static BOOL NLS_RegEnumValue(HANDLE hKey, UINT ulIndex,
2992 LPWSTR szValueName, ULONG valueNameSize,
2993 LPWSTR szValueData, ULONG valueDataSize)
2995 BYTE buffer[80];
2996 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2997 DWORD dwLen;
2999 if (NtEnumerateValueKey( hKey, ulIndex, KeyValueFullInformation,
3000 buffer, sizeof(buffer), &dwLen ) != STATUS_SUCCESS ||
3001 info->NameLength > valueNameSize ||
3002 info->DataLength > valueDataSize)
3004 return FALSE;
3007 TRACE("info->Name %s info->DataLength %d\n", debugstr_w(info->Name), info->DataLength);
3009 memcpy( szValueName, info->Name, info->NameLength);
3010 szValueName[info->NameLength / sizeof(WCHAR)] = '\0';
3011 memcpy( szValueData, buffer + info->DataOffset, info->DataLength );
3012 szValueData[info->DataLength / sizeof(WCHAR)] = '\0';
3014 TRACE("returning %s %s\n", debugstr_w(szValueName), debugstr_w(szValueData));
3015 return TRUE;
3018 static BOOL NLS_RegGetDword(HANDLE hKey, LPCWSTR szValueName, DWORD *lpVal)
3020 BYTE buffer[128];
3021 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
3022 DWORD dwSize = sizeof(buffer);
3023 UNICODE_STRING valueName;
3025 RtlInitUnicodeString( &valueName, szValueName );
3027 TRACE("%p, %s\n", hKey, debugstr_w(szValueName));
3028 if (NtQueryValueKey( hKey, &valueName, KeyValuePartialInformation,
3029 buffer, dwSize, &dwSize ) == STATUS_SUCCESS &&
3030 info->DataLength == sizeof(DWORD))
3032 memcpy(lpVal, info->Data, sizeof(DWORD));
3033 return TRUE;
3036 return FALSE;
3039 static BOOL NLS_GetLanguageGroupName(LGRPID lgrpid, LPWSTR szName, ULONG nameSize)
3041 LANGID langId;
3042 LPCWSTR szResourceName = MAKEINTRESOURCEW(((lgrpid + 0x2000) >> 4) + 1);
3043 HRSRC hResource;
3044 BOOL bRet = FALSE;
3046 /* FIXME: Is it correct to use the system default langid? */
3047 langId = GetSystemDefaultLangID();
3049 if (SUBLANGID(langId) == SUBLANG_NEUTRAL)
3050 langId = MAKELANGID( PRIMARYLANGID(langId), SUBLANG_DEFAULT );
3052 hResource = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING, szResourceName, langId );
3054 if (hResource)
3056 HGLOBAL hResDir = LoadResource( kernel32_handle, hResource );
3058 if (hResDir)
3060 ULONG iResourceIndex = lgrpid & 0xf;
3061 LPCWSTR lpResEntry = LockResource( hResDir );
3062 ULONG i;
3064 for (i = 0; i < iResourceIndex; i++)
3065 lpResEntry += *lpResEntry + 1;
3067 if (*lpResEntry < nameSize)
3069 memcpy( szName, lpResEntry + 1, *lpResEntry * sizeof(WCHAR) );
3070 szName[*lpResEntry] = '\0';
3071 bRet = TRUE;
3075 FreeResource( hResource );
3077 return bRet;
3080 /* Registry keys for NLS related information */
3082 static const WCHAR szCountryListName[] = {
3083 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
3084 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
3085 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3086 'T','e','l','e','p','h','o','n','y','\\',
3087 'C','o','u','n','t','r','y',' ','L','i','s','t','\0'
3091 /* Callback function ptrs for EnumSystemLanguageGroupsA/W */
3092 typedef struct
3094 LANGUAGEGROUP_ENUMPROCA procA;
3095 LANGUAGEGROUP_ENUMPROCW procW;
3096 DWORD dwFlags;
3097 LONG_PTR lParam;
3098 } ENUMLANGUAGEGROUP_CALLBACKS;
3100 /* Internal implementation of EnumSystemLanguageGroupsA/W */
3101 static BOOL NLS_EnumSystemLanguageGroups(ENUMLANGUAGEGROUP_CALLBACKS *lpProcs)
3103 WCHAR szNumber[10], szValue[4];
3104 HANDLE hKey;
3105 BOOL bContinue = TRUE;
3106 ULONG ulIndex = 0;
3108 if (!lpProcs)
3110 SetLastError(ERROR_INVALID_PARAMETER);
3111 return FALSE;
3114 switch (lpProcs->dwFlags)
3116 case 0:
3117 /* Default to LGRPID_INSTALLED */
3118 lpProcs->dwFlags = LGRPID_INSTALLED;
3119 /* Fall through... */
3120 case LGRPID_INSTALLED:
3121 case LGRPID_SUPPORTED:
3122 break;
3123 default:
3124 SetLastError(ERROR_INVALID_FLAGS);
3125 return FALSE;
3128 hKey = NLS_RegOpenKey( 0, szLangGroupsKeyName );
3130 if (!hKey)
3131 FIXME("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
3133 while (bContinue)
3135 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
3136 szValue, sizeof(szValue) ))
3138 BOOL bInstalled = szValue[0] == '1' ? TRUE : FALSE;
3139 LGRPID lgrpid = strtoulW( szNumber, NULL, 16 );
3141 TRACE("grpid %s (%sinstalled)\n", debugstr_w(szNumber),
3142 bInstalled ? "" : "not ");
3144 if (lpProcs->dwFlags == LGRPID_SUPPORTED || bInstalled)
3146 WCHAR szGrpName[48];
3148 if (!NLS_GetLanguageGroupName( lgrpid, szGrpName, sizeof(szGrpName) / sizeof(WCHAR) ))
3149 szGrpName[0] = '\0';
3151 if (lpProcs->procW)
3152 bContinue = lpProcs->procW( lgrpid, szNumber, szGrpName, lpProcs->dwFlags,
3153 lpProcs->lParam );
3154 else
3156 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
3157 char szGrpNameA[48];
3159 /* FIXME: MSDN doesn't say which code page the W->A translation uses,
3160 * or whether the language names are ever localised. Assume CP_ACP.
3163 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
3164 WideCharToMultiByte(CP_ACP, 0, szGrpName, -1, szGrpNameA, sizeof(szGrpNameA), 0, 0);
3166 bContinue = lpProcs->procA( lgrpid, szNumberA, szGrpNameA, lpProcs->dwFlags,
3167 lpProcs->lParam );
3171 ulIndex++;
3173 else
3174 bContinue = FALSE;
3176 if (!bContinue)
3177 break;
3180 if (hKey)
3181 NtClose( hKey );
3183 return TRUE;
3186 /******************************************************************************
3187 * EnumSystemLanguageGroupsA (KERNEL32.@)
3189 * Call a users function for each language group available on the system.
3191 * PARAMS
3192 * pLangGrpEnumProc [I] Callback function to call for each language group
3193 * dwFlags [I] LGRPID_SUPPORTED=All Supported, LGRPID_INSTALLED=Installed only
3194 * lParam [I] User parameter to pass to pLangGrpEnumProc
3196 * RETURNS
3197 * Success: TRUE.
3198 * Failure: FALSE. Use GetLastError() to determine the cause.
3200 BOOL WINAPI EnumSystemLanguageGroupsA(LANGUAGEGROUP_ENUMPROCA pLangGrpEnumProc,
3201 DWORD dwFlags, LONG_PTR lParam)
3203 ENUMLANGUAGEGROUP_CALLBACKS procs;
3205 TRACE("(%p,0x%08X,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
3207 procs.procA = pLangGrpEnumProc;
3208 procs.procW = NULL;
3209 procs.dwFlags = dwFlags;
3210 procs.lParam = lParam;
3212 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
3215 /******************************************************************************
3216 * EnumSystemLanguageGroupsW (KERNEL32.@)
3218 * See EnumSystemLanguageGroupsA.
3220 BOOL WINAPI EnumSystemLanguageGroupsW(LANGUAGEGROUP_ENUMPROCW pLangGrpEnumProc,
3221 DWORD dwFlags, LONG_PTR lParam)
3223 ENUMLANGUAGEGROUP_CALLBACKS procs;
3225 TRACE("(%p,0x%08X,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
3227 procs.procA = NULL;
3228 procs.procW = pLangGrpEnumProc;
3229 procs.dwFlags = dwFlags;
3230 procs.lParam = lParam;
3232 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
3235 /******************************************************************************
3236 * IsValidLanguageGroup (KERNEL32.@)
3238 * Determine if a language group is supported and/or installed.
3240 * PARAMS
3241 * lgrpid [I] Language Group Id (LGRPID_ values from "winnls.h")
3242 * dwFlags [I] LGRPID_SUPPORTED=Supported, LGRPID_INSTALLED=Installed
3244 * RETURNS
3245 * TRUE, if lgrpid is supported and/or installed, according to dwFlags.
3246 * FALSE otherwise.
3248 BOOL WINAPI IsValidLanguageGroup(LGRPID lgrpid, DWORD dwFlags)
3250 static const WCHAR szFormat[] = { '%','x','\0' };
3251 WCHAR szValueName[16], szValue[2];
3252 BOOL bSupported = FALSE, bInstalled = FALSE;
3253 HANDLE hKey;
3256 switch (dwFlags)
3258 case LGRPID_INSTALLED:
3259 case LGRPID_SUPPORTED:
3261 hKey = NLS_RegOpenKey( 0, szLangGroupsKeyName );
3263 sprintfW( szValueName, szFormat, lgrpid );
3265 if (NLS_RegGetDword( hKey, szValueName, (LPDWORD)szValue ))
3267 bSupported = TRUE;
3269 if (szValue[0] == '1')
3270 bInstalled = TRUE;
3273 if (hKey)
3274 NtClose( hKey );
3276 break;
3279 if ((dwFlags == LGRPID_SUPPORTED && bSupported) ||
3280 (dwFlags == LGRPID_INSTALLED && bInstalled))
3281 return TRUE;
3283 return FALSE;
3286 /* Callback function ptrs for EnumLanguageGrouplocalesA/W */
3287 typedef struct
3289 LANGGROUPLOCALE_ENUMPROCA procA;
3290 LANGGROUPLOCALE_ENUMPROCW procW;
3291 DWORD dwFlags;
3292 LGRPID lgrpid;
3293 LONG_PTR lParam;
3294 } ENUMLANGUAGEGROUPLOCALE_CALLBACKS;
3296 /* Internal implementation of EnumLanguageGrouplocalesA/W */
3297 static BOOL NLS_EnumLanguageGroupLocales(ENUMLANGUAGEGROUPLOCALE_CALLBACKS *lpProcs)
3299 static const WCHAR szAlternateSortsKeyName[] = {
3300 'A','l','t','e','r','n','a','t','e',' ','S','o','r','t','s','\0'
3302 WCHAR szNumber[10], szValue[4];
3303 HANDLE hKey;
3304 BOOL bContinue = TRUE, bAlternate = FALSE;
3305 LGRPID lgrpid;
3306 ULONG ulIndex = 1; /* Ignore default entry of 1st key */
3308 if (!lpProcs || !lpProcs->lgrpid || lpProcs->lgrpid > LGRPID_ARMENIAN)
3310 SetLastError(ERROR_INVALID_PARAMETER);
3311 return FALSE;
3314 if (lpProcs->dwFlags)
3316 SetLastError(ERROR_INVALID_FLAGS);
3317 return FALSE;
3320 hKey = NLS_RegOpenKey( 0, szLocaleKeyName );
3322 if (!hKey)
3323 WARN("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
3325 while (bContinue)
3327 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
3328 szValue, sizeof(szValue) ))
3330 lgrpid = strtoulW( szValue, NULL, 16 );
3332 TRACE("lcid %s, grpid %d (%smatched)\n", debugstr_w(szNumber),
3333 lgrpid, lgrpid == lpProcs->lgrpid ? "" : "not ");
3335 if (lgrpid == lpProcs->lgrpid)
3337 LCID lcid;
3339 lcid = strtoulW( szNumber, NULL, 16 );
3341 /* FIXME: native returns extra text for a few (17/150) locales, e.g:
3342 * '00000437 ;Georgian'
3343 * At present we only pass the LCID string.
3346 if (lpProcs->procW)
3347 bContinue = lpProcs->procW( lgrpid, lcid, szNumber, lpProcs->lParam );
3348 else
3350 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
3352 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
3354 bContinue = lpProcs->procA( lgrpid, lcid, szNumberA, lpProcs->lParam );
3358 ulIndex++;
3360 else
3362 /* Finished enumerating this key */
3363 if (!bAlternate)
3365 /* Enumerate alternate sorts also */
3366 hKey = NLS_RegOpenKey( hKey, szAlternateSortsKeyName );
3367 bAlternate = TRUE;
3368 ulIndex = 0;
3370 else
3371 bContinue = FALSE; /* Finished both keys */
3374 if (!bContinue)
3375 break;
3378 if (hKey)
3379 NtClose( hKey );
3381 return TRUE;
3384 /******************************************************************************
3385 * EnumLanguageGroupLocalesA (KERNEL32.@)
3387 * Call a users function for every locale in a language group available on the system.
3389 * PARAMS
3390 * pLangGrpLcEnumProc [I] Callback function to call for each locale
3391 * lgrpid [I] Language group (LGRPID_ values from "winnls.h")
3392 * dwFlags [I] Reserved, set to 0
3393 * lParam [I] User parameter to pass to pLangGrpLcEnumProc
3395 * RETURNS
3396 * Success: TRUE.
3397 * Failure: FALSE. Use GetLastError() to determine the cause.
3399 BOOL WINAPI EnumLanguageGroupLocalesA(LANGGROUPLOCALE_ENUMPROCA pLangGrpLcEnumProc,
3400 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
3402 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
3404 TRACE("(%p,0x%08X,0x%08X,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
3406 callbacks.procA = pLangGrpLcEnumProc;
3407 callbacks.procW = NULL;
3408 callbacks.dwFlags = dwFlags;
3409 callbacks.lgrpid = lgrpid;
3410 callbacks.lParam = lParam;
3412 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
3415 /******************************************************************************
3416 * EnumLanguageGroupLocalesW (KERNEL32.@)
3418 * See EnumLanguageGroupLocalesA.
3420 BOOL WINAPI EnumLanguageGroupLocalesW(LANGGROUPLOCALE_ENUMPROCW pLangGrpLcEnumProc,
3421 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
3423 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
3425 TRACE("(%p,0x%08X,0x%08X,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
3427 callbacks.procA = NULL;
3428 callbacks.procW = pLangGrpLcEnumProc;
3429 callbacks.dwFlags = dwFlags;
3430 callbacks.lgrpid = lgrpid;
3431 callbacks.lParam = lParam;
3433 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
3436 /******************************************************************************
3437 * EnumSystemGeoID (KERNEL32.@)
3439 * Call a users function for every location available on the system.
3441 * PARAMS
3442 * geoclass [I] Type of information desired (SYSGEOTYPE enum from "winnls.h")
3443 * reserved [I] Reserved, set to 0
3444 * pGeoEnumProc [I] Callback function to call for each location
3446 * RETURNS
3447 * Success: TRUE.
3448 * Failure: FALSE. Use GetLastError() to determine the cause.
3450 BOOL WINAPI EnumSystemGeoID(GEOCLASS geoclass, GEOID reserved, GEO_ENUMPROC pGeoEnumProc)
3452 static const WCHAR szCountryCodeValueName[] = {
3453 'C','o','u','n','t','r','y','C','o','d','e','\0'
3455 WCHAR szNumber[10];
3456 HANDLE hKey;
3457 ULONG ulIndex = 0;
3459 TRACE("(0x%08X,0x%08X,%p)\n", geoclass, reserved, pGeoEnumProc);
3461 if (geoclass != GEOCLASS_NATION || reserved || !pGeoEnumProc)
3463 SetLastError(ERROR_INVALID_PARAMETER);
3464 return FALSE;
3467 hKey = NLS_RegOpenKey( 0, szCountryListName );
3469 while (NLS_RegEnumSubKey( hKey, ulIndex, szNumber, sizeof(szNumber) ))
3471 BOOL bContinue = TRUE;
3472 DWORD dwGeoId;
3473 HANDLE hSubKey = NLS_RegOpenKey( hKey, szNumber );
3475 if (hSubKey)
3477 if (NLS_RegGetDword( hSubKey, szCountryCodeValueName, &dwGeoId ))
3479 TRACE("Got geoid %d\n", dwGeoId);
3481 if (!pGeoEnumProc( dwGeoId ))
3482 bContinue = FALSE;
3485 NtClose( hSubKey );
3488 if (!bContinue)
3489 break;
3491 ulIndex++;
3494 if (hKey)
3495 NtClose( hKey );
3497 return TRUE;
3500 /******************************************************************************
3501 * InvalidateNLSCache (KERNEL32.@)
3503 * Invalidate the cache of NLS values.
3505 * PARAMS
3506 * None.
3508 * RETURNS
3509 * Success: TRUE.
3510 * Failure: FALSE.
3512 BOOL WINAPI InvalidateNLSCache(void)
3514 FIXME("() stub\n");
3515 return FALSE;
3518 /******************************************************************************
3519 * GetUserGeoID (KERNEL32.@)
3521 GEOID WINAPI GetUserGeoID( GEOCLASS GeoClass )
3523 GEOID ret = GEOID_NOT_AVAILABLE;
3524 static const WCHAR geoW[] = {'G','e','o',0};
3525 static const WCHAR nationW[] = {'N','a','t','i','o','n',0};
3526 WCHAR bufferW[40], *end;
3527 DWORD count;
3528 HANDLE hkey, hSubkey = 0;
3529 UNICODE_STRING keyW;
3530 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
3531 RtlInitUnicodeString( &keyW, nationW );
3532 count = sizeof(bufferW);
3534 if(!(hkey = create_registry_key())) return ret;
3536 switch( GeoClass ){
3537 case GEOCLASS_NATION:
3538 if ((hSubkey = NLS_RegOpenKey(hkey, geoW)))
3540 if((NtQueryValueKey(hSubkey, &keyW, KeyValuePartialInformation,
3541 bufferW, count, &count) == STATUS_SUCCESS ) && info->DataLength)
3542 ret = strtolW((LPCWSTR)info->Data, &end, 10);
3544 break;
3545 case GEOCLASS_REGION:
3546 FIXME("GEOCLASS_REGION not handled yet\n");
3547 break;
3550 NtClose(hkey);
3551 if (hSubkey) NtClose(hSubkey);
3552 return ret;
3555 /******************************************************************************
3556 * SetUserGeoID (KERNEL32.@)
3558 BOOL WINAPI SetUserGeoID( GEOID GeoID )
3560 static const WCHAR geoW[] = {'G','e','o',0};
3561 static const WCHAR nationW[] = {'N','a','t','i','o','n',0};
3562 static const WCHAR formatW[] = {'%','i',0};
3563 UNICODE_STRING nameW,keyW;
3564 WCHAR bufferW[10];
3565 OBJECT_ATTRIBUTES attr;
3566 HANDLE hkey;
3568 if(!(hkey = create_registry_key())) return FALSE;
3570 attr.Length = sizeof(attr);
3571 attr.RootDirectory = hkey;
3572 attr.ObjectName = &nameW;
3573 attr.Attributes = 0;
3574 attr.SecurityDescriptor = NULL;
3575 attr.SecurityQualityOfService = NULL;
3576 RtlInitUnicodeString( &nameW, geoW );
3577 RtlInitUnicodeString( &keyW, nationW );
3579 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS)
3582 NtClose(attr.RootDirectory);
3583 return FALSE;
3586 sprintfW(bufferW, formatW, GeoID);
3587 NtSetValueKey(hkey, &keyW, 0, REG_SZ, bufferW, (strlenW(bufferW) + 1) * sizeof(WCHAR));
3588 NtClose(attr.RootDirectory);
3589 NtClose(hkey);
3590 return TRUE;
3593 typedef struct
3595 union
3597 UILANGUAGE_ENUMPROCA procA;
3598 UILANGUAGE_ENUMPROCW procW;
3599 } u;
3600 DWORD flags;
3601 LONG_PTR param;
3602 } ENUM_UILANG_CALLBACK;
3604 static BOOL CALLBACK enum_uilang_proc_a( HMODULE hModule, LPCSTR type,
3605 LPCSTR name, WORD LangID, LONG_PTR lParam )
3607 ENUM_UILANG_CALLBACK *enum_uilang = (ENUM_UILANG_CALLBACK *)lParam;
3608 char buf[20];
3610 sprintf(buf, "%08x", (UINT)LangID);
3611 return enum_uilang->u.procA( buf, enum_uilang->param );
3614 static BOOL CALLBACK enum_uilang_proc_w( HMODULE hModule, LPCWSTR type,
3615 LPCWSTR name, WORD LangID, LONG_PTR lParam )
3617 static const WCHAR formatW[] = {'%','0','8','x',0};
3618 ENUM_UILANG_CALLBACK *enum_uilang = (ENUM_UILANG_CALLBACK *)lParam;
3619 WCHAR buf[20];
3621 sprintfW( buf, formatW, (UINT)LangID );
3622 return enum_uilang->u.procW( buf, enum_uilang->param );
3625 /******************************************************************************
3626 * EnumUILanguagesA (KERNEL32.@)
3628 BOOL WINAPI EnumUILanguagesA(UILANGUAGE_ENUMPROCA pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
3630 ENUM_UILANG_CALLBACK enum_uilang;
3632 TRACE("%p, %x, %lx\n", pUILangEnumProc, dwFlags, lParam);
3634 if(!pUILangEnumProc) {
3635 SetLastError(ERROR_INVALID_PARAMETER);
3636 return FALSE;
3638 if(dwFlags) {
3639 SetLastError(ERROR_INVALID_FLAGS);
3640 return FALSE;
3643 enum_uilang.u.procA = pUILangEnumProc;
3644 enum_uilang.flags = dwFlags;
3645 enum_uilang.param = lParam;
3647 EnumResourceLanguagesA( kernel32_handle, (LPCSTR)RT_STRING,
3648 (LPCSTR)LOCALE_ILANGUAGE, enum_uilang_proc_a,
3649 (LONG_PTR)&enum_uilang);
3650 return TRUE;
3653 /******************************************************************************
3654 * EnumUILanguagesW (KERNEL32.@)
3656 BOOL WINAPI EnumUILanguagesW(UILANGUAGE_ENUMPROCW pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
3658 ENUM_UILANG_CALLBACK enum_uilang;
3660 TRACE("%p, %x, %lx\n", pUILangEnumProc, dwFlags, lParam);
3663 if(!pUILangEnumProc) {
3664 SetLastError(ERROR_INVALID_PARAMETER);
3665 return FALSE;
3667 if(dwFlags) {
3668 SetLastError(ERROR_INVALID_FLAGS);
3669 return FALSE;
3672 enum_uilang.u.procW = pUILangEnumProc;
3673 enum_uilang.flags = dwFlags;
3674 enum_uilang.param = lParam;
3676 EnumResourceLanguagesW( kernel32_handle, (LPCWSTR)RT_STRING,
3677 (LPCWSTR)LOCALE_ILANGUAGE, enum_uilang_proc_w,
3678 (LONG_PTR)&enum_uilang);
3679 return TRUE;
3682 INT WINAPI GetGeoInfoW(GEOID GeoId, GEOTYPE GeoType, LPWSTR lpGeoData,
3683 int cchData, LANGID language)
3685 FIXME("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, language);
3686 return 0;
3689 INT WINAPI GetGeoInfoA(GEOID GeoId, GEOTYPE GeoType, LPSTR lpGeoData,
3690 int cchData, LANGID language)
3692 FIXME("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, language);
3693 return 0;