include: Add cmdtxt.idl.
[wine.git] / dlls / kernel32 / locale.c
blobc1888ac5a1eabf10672257e9e2a884c03127ec4f
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|\
57 LOCALE_RETURN_NUMBER|LOCALE_RETURN_GENITIVE_NAMES)
59 /* current code pages */
60 static const union cptable *ansi_cptable;
61 static const union cptable *oem_cptable;
62 static const union cptable *mac_cptable;
63 static const union cptable *unix_cptable; /* NULL if UTF8 */
65 static HANDLE NLS_RegOpenKey(HANDLE hRootKey, LPCWSTR szKeyName);
67 static const WCHAR szNlsKeyName[] = {
68 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
69 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
70 'C','o','n','t','r','o','l','\\','N','l','s','\0'
73 static const WCHAR szLocaleKeyName[] = {
74 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
75 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
76 'C','o','n','t','r','o','l','\\','N','l','s','\\','L','o','c','a','l','e',0
79 static const WCHAR szCodepageKeyName[] = {
80 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
81 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
82 'C','o','n','t','r','o','l','\\','N','l','s','\\','C','o','d','e','p','a','g','e',0
85 static const WCHAR szLangGroupsKeyName[] = {
86 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
87 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
88 'C','o','n','t','r','o','l','\\','N','l','s','\\',
89 'L','a','n','g','u','a','g','e',' ','G','r','o','u','p','s',0
92 /* Charset to codepage map, sorted by name. */
93 static const struct charset_entry
95 const char *charset_name;
96 UINT codepage;
97 } charset_names[] =
99 { "BIG5", 950 },
100 { "CP1250", 1250 },
101 { "CP1251", 1251 },
102 { "CP1252", 1252 },
103 { "CP1253", 1253 },
104 { "CP1254", 1254 },
105 { "CP1255", 1255 },
106 { "CP1256", 1256 },
107 { "CP1257", 1257 },
108 { "CP1258", 1258 },
109 { "CP932", 932 },
110 { "CP936", 936 },
111 { "CP949", 949 },
112 { "CP950", 950 },
113 { "EUCJP", 20932 },
114 { "GB2312", 936 },
115 { "IBM037", 37 },
116 { "IBM1026", 1026 },
117 { "IBM424", 424 },
118 { "IBM437", 437 },
119 { "IBM500", 500 },
120 { "IBM850", 850 },
121 { "IBM852", 852 },
122 { "IBM855", 855 },
123 { "IBM857", 857 },
124 { "IBM860", 860 },
125 { "IBM861", 861 },
126 { "IBM862", 862 },
127 { "IBM863", 863 },
128 { "IBM864", 864 },
129 { "IBM865", 865 },
130 { "IBM866", 866 },
131 { "IBM869", 869 },
132 { "IBM874", 874 },
133 { "IBM875", 875 },
134 { "ISO88591", 28591 },
135 { "ISO885910", 28600 },
136 { "ISO885913", 28603 },
137 { "ISO885914", 28604 },
138 { "ISO885915", 28605 },
139 { "ISO885916", 28606 },
140 { "ISO88592", 28592 },
141 { "ISO88593", 28593 },
142 { "ISO88594", 28594 },
143 { "ISO88595", 28595 },
144 { "ISO88596", 28596 },
145 { "ISO88597", 28597 },
146 { "ISO88598", 28598 },
147 { "ISO88599", 28599 },
148 { "KOI8R", 20866 },
149 { "KOI8U", 21866 },
150 { "UTF8", CP_UTF8 }
154 struct locale_name
156 WCHAR win_name[128]; /* Windows name ("en-US") */
157 WCHAR lang[128]; /* language ("en") (note: buffer contains the other strings too) */
158 WCHAR *country; /* country ("US") */
159 WCHAR *charset; /* charset ("UTF-8") for Unix format only */
160 WCHAR *script; /* script ("Latn") for Windows format only */
161 WCHAR *modifier; /* modifier or sort order */
162 LCID lcid; /* corresponding LCID */
163 int matches; /* number of elements matching LCID (0..4) */
164 UINT codepage; /* codepage corresponding to charset */
167 /* locale ids corresponding to the various Unix locale parameters */
168 static LCID lcid_LC_COLLATE;
169 static LCID lcid_LC_CTYPE;
170 static LCID lcid_LC_MESSAGES;
171 static LCID lcid_LC_MONETARY;
172 static LCID lcid_LC_NUMERIC;
173 static LCID lcid_LC_TIME;
174 static LCID lcid_LC_PAPER;
175 static LCID lcid_LC_MEASUREMENT;
176 static LCID lcid_LC_TELEPHONE;
178 /* Copy Ascii string to Unicode without using codepages */
179 static inline void strcpynAtoW( WCHAR *dst, const char *src, size_t n )
181 while (n > 1 && *src)
183 *dst++ = (unsigned char)*src++;
184 n--;
186 if (n) *dst = 0;
190 /***********************************************************************
191 * get_lcid_codepage
193 * Retrieve the ANSI codepage for a given locale.
195 static inline UINT get_lcid_codepage( LCID lcid )
197 UINT ret;
198 if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
199 sizeof(ret)/sizeof(WCHAR) )) ret = 0;
200 return ret;
204 /***********************************************************************
205 * get_codepage_table
207 * Find the table for a given codepage, handling CP_ACP etc. pseudo-codepages
209 static const union cptable *get_codepage_table( unsigned int codepage )
211 const union cptable *ret = NULL;
213 assert( ansi_cptable ); /* init must have been done already */
215 switch(codepage)
217 case CP_ACP:
218 return ansi_cptable;
219 case CP_OEMCP:
220 return oem_cptable;
221 case CP_MACCP:
222 return mac_cptable;
223 case CP_UTF7:
224 case CP_UTF8:
225 break;
226 case CP_THREAD_ACP:
227 if (!(codepage = kernel_get_thread_data()->code_page)) return ansi_cptable;
228 /* fall through */
229 default:
230 if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
231 if (codepage == oem_cptable->info.codepage) return oem_cptable;
232 if (codepage == mac_cptable->info.codepage) return mac_cptable;
233 ret = wine_cp_get_table( codepage );
234 break;
236 return ret;
240 /***********************************************************************
241 * charset_cmp (internal)
243 static int charset_cmp( const void *name, const void *entry )
245 const struct charset_entry *charset = entry;
246 return strcasecmp( name, charset->charset_name );
249 /***********************************************************************
250 * find_charset
252 static UINT find_charset( const WCHAR *name )
254 const struct charset_entry *entry;
255 char charset_name[16];
256 size_t i, j;
258 /* remove punctuation characters from charset name */
259 for (i = j = 0; name[i] && j < sizeof(charset_name)-1; i++)
260 if (isalnum((unsigned char)name[i])) charset_name[j++] = name[i];
261 charset_name[j] = 0;
263 entry = bsearch( charset_name, charset_names,
264 sizeof(charset_names)/sizeof(charset_names[0]),
265 sizeof(charset_names[0]), charset_cmp );
266 if (entry) return entry->codepage;
267 return 0;
271 /***********************************************************************
272 * find_locale_id_callback
274 static BOOL CALLBACK find_locale_id_callback( HMODULE hModule, LPCWSTR type,
275 LPCWSTR name, WORD LangID, LPARAM lParam )
277 struct locale_name *data = (struct locale_name *)lParam;
278 WCHAR buffer[128];
279 int matches = 0;
280 LCID lcid = MAKELCID( LangID, SORT_DEFAULT ); /* FIXME: handle sort order */
282 if (PRIMARYLANGID(LangID) == LANG_NEUTRAL) return TRUE; /* continue search */
284 /* first check exact name */
285 if (data->win_name[0] &&
286 GetLocaleInfoW( lcid, LOCALE_SNAME | LOCALE_NOUSEROVERRIDE,
287 buffer, sizeof(buffer)/sizeof(WCHAR) ))
289 if (!strcmpW( data->win_name, buffer ))
291 matches = 4; /* everything matches */
292 goto done;
296 if (!GetLocaleInfoW( lcid, LOCALE_SISO639LANGNAME | LOCALE_NOUSEROVERRIDE,
297 buffer, sizeof(buffer)/sizeof(WCHAR) ))
298 return TRUE;
299 if (strcmpW( buffer, data->lang )) return TRUE;
300 matches++; /* language name matched */
302 if (data->country)
304 if (GetLocaleInfoW( lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
305 buffer, sizeof(buffer)/sizeof(WCHAR) ))
307 if (strcmpW( buffer, data->country )) goto done;
308 matches++; /* country name matched */
311 else /* match default language */
313 if (SUBLANGID(LangID) == SUBLANG_DEFAULT) matches++;
316 if (data->codepage)
318 UINT unix_cp;
319 if (GetLocaleInfoW( lcid, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
320 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) ))
322 if (unix_cp == data->codepage) matches++;
326 /* FIXME: check sort order */
328 done:
329 if (matches > data->matches)
331 data->lcid = lcid;
332 data->matches = matches;
334 return (data->matches < 4); /* no need to continue for perfect match */
338 /***********************************************************************
339 * parse_locale_name
341 * Parse a locale name into a struct locale_name, handling both Windows and Unix formats.
342 * Unix format is: lang[_country][.charset][@modifier]
343 * Windows format is: lang[-script][-country][_modifier]
345 static void parse_locale_name( const WCHAR *str, struct locale_name *name )
347 static const WCHAR sepW[] = {'-','_','.','@',0};
348 static const WCHAR winsepW[] = {'-','_',0};
349 static const WCHAR posixW[] = {'P','O','S','I','X',0};
350 static const WCHAR cW[] = {'C',0};
351 static const WCHAR latinW[] = {'l','a','t','i','n',0};
352 static const WCHAR latnW[] = {'-','L','a','t','n',0};
353 WCHAR *p;
355 TRACE("%s\n", debugstr_w(str));
357 name->country = name->charset = name->script = name->modifier = NULL;
358 name->lcid = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
359 name->matches = 0;
360 name->codepage = 0;
361 name->win_name[0] = 0;
362 lstrcpynW( name->lang, str, sizeof(name->lang)/sizeof(WCHAR) );
364 if (!(p = strpbrkW( name->lang, sepW )))
366 if (!strcmpW( name->lang, posixW ) || !strcmpW( name->lang, cW ))
368 name->matches = 4; /* perfect match for default English lcid */
369 return;
371 strcpyW( name->win_name, name->lang );
373 else if (*p == '-') /* Windows format */
375 strcpyW( name->win_name, name->lang );
376 *p++ = 0;
377 name->country = p;
378 if (!(p = strpbrkW( p, winsepW ))) goto done;
379 if (*p == '-')
381 *p++ = 0;
382 name->script = name->country;
383 name->country = p;
384 if (!(p = strpbrkW( p, winsepW ))) goto done;
386 *p++ = 0;
387 name->modifier = p;
389 else /* Unix format */
391 if (*p == '_')
393 *p++ = 0;
394 name->country = p;
395 p = strpbrkW( p, sepW + 2 );
397 if (p && *p == '.')
399 *p++ = 0;
400 name->charset = p;
401 p = strchrW( p, '@' );
403 if (p)
405 *p++ = 0;
406 name->modifier = p;
409 if (name->charset)
410 name->codepage = find_charset( name->charset );
412 /* rebuild a Windows name if possible */
414 if (name->charset) goto done; /* can't specify charset in Windows format */
415 if (name->modifier && strcmpW( name->modifier, latinW ))
416 goto done; /* only Latn script supported for now */
417 strcpyW( name->win_name, name->lang );
418 if (name->modifier) strcatW( name->win_name, latnW );
419 if (name->country)
421 p = name->win_name + strlenW(name->win_name);
422 *p++ = '-';
423 strcpyW( p, name->country );
426 done:
427 EnumResourceLanguagesW( kernel32_handle, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
428 find_locale_id_callback, (LPARAM)name );
432 /***********************************************************************
433 * convert_default_lcid
435 * Get the default LCID to use for a given lctype in GetLocaleInfo.
437 static LCID convert_default_lcid( LCID lcid, LCTYPE lctype )
439 if (lcid == LOCALE_SYSTEM_DEFAULT ||
440 lcid == LOCALE_USER_DEFAULT ||
441 lcid == LOCALE_NEUTRAL)
443 LCID default_id = 0;
445 switch(lctype & 0xffff)
447 case LOCALE_SSORTNAME:
448 default_id = lcid_LC_COLLATE;
449 break;
451 case LOCALE_FONTSIGNATURE:
452 case LOCALE_IDEFAULTANSICODEPAGE:
453 case LOCALE_IDEFAULTCODEPAGE:
454 case LOCALE_IDEFAULTEBCDICCODEPAGE:
455 case LOCALE_IDEFAULTMACCODEPAGE:
456 case LOCALE_IDEFAULTUNIXCODEPAGE:
457 default_id = lcid_LC_CTYPE;
458 break;
460 case LOCALE_ICURRDIGITS:
461 case LOCALE_ICURRENCY:
462 case LOCALE_IINTLCURRDIGITS:
463 case LOCALE_INEGCURR:
464 case LOCALE_INEGSEPBYSPACE:
465 case LOCALE_INEGSIGNPOSN:
466 case LOCALE_INEGSYMPRECEDES:
467 case LOCALE_IPOSSEPBYSPACE:
468 case LOCALE_IPOSSIGNPOSN:
469 case LOCALE_IPOSSYMPRECEDES:
470 case LOCALE_SCURRENCY:
471 case LOCALE_SINTLSYMBOL:
472 case LOCALE_SMONDECIMALSEP:
473 case LOCALE_SMONGROUPING:
474 case LOCALE_SMONTHOUSANDSEP:
475 case LOCALE_SNATIVECURRNAME:
476 default_id = lcid_LC_MONETARY;
477 break;
479 case LOCALE_IDIGITS:
480 case LOCALE_IDIGITSUBSTITUTION:
481 case LOCALE_ILZERO:
482 case LOCALE_INEGNUMBER:
483 case LOCALE_SDECIMAL:
484 case LOCALE_SGROUPING:
485 case LOCALE_SNAN:
486 case LOCALE_SNATIVEDIGITS:
487 case LOCALE_SNEGATIVESIGN:
488 case LOCALE_SNEGINFINITY:
489 case LOCALE_SPOSINFINITY:
490 case LOCALE_SPOSITIVESIGN:
491 case LOCALE_STHOUSAND:
492 default_id = lcid_LC_NUMERIC;
493 break;
495 case LOCALE_ICALENDARTYPE:
496 case LOCALE_ICENTURY:
497 case LOCALE_IDATE:
498 case LOCALE_IDAYLZERO:
499 case LOCALE_IFIRSTDAYOFWEEK:
500 case LOCALE_IFIRSTWEEKOFYEAR:
501 case LOCALE_ILDATE:
502 case LOCALE_IMONLZERO:
503 case LOCALE_IOPTIONALCALENDAR:
504 case LOCALE_ITIME:
505 case LOCALE_ITIMEMARKPOSN:
506 case LOCALE_ITLZERO:
507 case LOCALE_S1159:
508 case LOCALE_S2359:
509 case LOCALE_SABBREVDAYNAME1:
510 case LOCALE_SABBREVDAYNAME2:
511 case LOCALE_SABBREVDAYNAME3:
512 case LOCALE_SABBREVDAYNAME4:
513 case LOCALE_SABBREVDAYNAME5:
514 case LOCALE_SABBREVDAYNAME6:
515 case LOCALE_SABBREVDAYNAME7:
516 case LOCALE_SABBREVMONTHNAME1:
517 case LOCALE_SABBREVMONTHNAME2:
518 case LOCALE_SABBREVMONTHNAME3:
519 case LOCALE_SABBREVMONTHNAME4:
520 case LOCALE_SABBREVMONTHNAME5:
521 case LOCALE_SABBREVMONTHNAME6:
522 case LOCALE_SABBREVMONTHNAME7:
523 case LOCALE_SABBREVMONTHNAME8:
524 case LOCALE_SABBREVMONTHNAME9:
525 case LOCALE_SABBREVMONTHNAME10:
526 case LOCALE_SABBREVMONTHNAME11:
527 case LOCALE_SABBREVMONTHNAME12:
528 case LOCALE_SABBREVMONTHNAME13:
529 case LOCALE_SDATE:
530 case LOCALE_SDAYNAME1:
531 case LOCALE_SDAYNAME2:
532 case LOCALE_SDAYNAME3:
533 case LOCALE_SDAYNAME4:
534 case LOCALE_SDAYNAME5:
535 case LOCALE_SDAYNAME6:
536 case LOCALE_SDAYNAME7:
537 case LOCALE_SDURATION:
538 case LOCALE_SLONGDATE:
539 case LOCALE_SMONTHNAME1:
540 case LOCALE_SMONTHNAME2:
541 case LOCALE_SMONTHNAME3:
542 case LOCALE_SMONTHNAME4:
543 case LOCALE_SMONTHNAME5:
544 case LOCALE_SMONTHNAME6:
545 case LOCALE_SMONTHNAME7:
546 case LOCALE_SMONTHNAME8:
547 case LOCALE_SMONTHNAME9:
548 case LOCALE_SMONTHNAME10:
549 case LOCALE_SMONTHNAME11:
550 case LOCALE_SMONTHNAME12:
551 case LOCALE_SMONTHNAME13:
552 case LOCALE_SSHORTDATE:
553 case LOCALE_SSHORTESTDAYNAME1:
554 case LOCALE_SSHORTESTDAYNAME2:
555 case LOCALE_SSHORTESTDAYNAME3:
556 case LOCALE_SSHORTESTDAYNAME4:
557 case LOCALE_SSHORTESTDAYNAME5:
558 case LOCALE_SSHORTESTDAYNAME6:
559 case LOCALE_SSHORTESTDAYNAME7:
560 case LOCALE_STIME:
561 case LOCALE_STIMEFORMAT:
562 case LOCALE_SYEARMONTH:
563 default_id = lcid_LC_TIME;
564 break;
566 case LOCALE_IPAPERSIZE:
567 default_id = lcid_LC_PAPER;
568 break;
570 case LOCALE_IMEASURE:
571 default_id = lcid_LC_MEASUREMENT;
572 break;
574 case LOCALE_ICOUNTRY:
575 default_id = lcid_LC_TELEPHONE;
576 break;
578 if (default_id) lcid = default_id;
580 return ConvertDefaultLocale( lcid );
583 /***********************************************************************
584 * is_genitive_name_supported
586 * Determine could LCTYPE basically support genitive name form or not.
588 static BOOL is_genitive_name_supported( LCTYPE lctype )
590 switch(lctype & 0xffff)
592 case LOCALE_SMONTHNAME1:
593 case LOCALE_SMONTHNAME2:
594 case LOCALE_SMONTHNAME3:
595 case LOCALE_SMONTHNAME4:
596 case LOCALE_SMONTHNAME5:
597 case LOCALE_SMONTHNAME6:
598 case LOCALE_SMONTHNAME7:
599 case LOCALE_SMONTHNAME8:
600 case LOCALE_SMONTHNAME9:
601 case LOCALE_SMONTHNAME10:
602 case LOCALE_SMONTHNAME11:
603 case LOCALE_SMONTHNAME12:
604 case LOCALE_SMONTHNAME13:
605 return TRUE;
606 default:
607 return FALSE;
611 /***********************************************************************
612 * create_registry_key
614 * Create the Control Panel\\International registry key.
616 static inline HANDLE create_registry_key(void)
618 static const WCHAR intlW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
619 'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
620 OBJECT_ATTRIBUTES attr;
621 UNICODE_STRING nameW;
622 HANDLE hkey;
624 if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
626 attr.Length = sizeof(attr);
627 attr.RootDirectory = hkey;
628 attr.ObjectName = &nameW;
629 attr.Attributes = 0;
630 attr.SecurityDescriptor = NULL;
631 attr.SecurityQualityOfService = NULL;
632 RtlInitUnicodeString( &nameW, intlW );
634 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS) hkey = 0;
635 NtClose( attr.RootDirectory );
636 return hkey;
640 /* update the registry settings for a given locale parameter */
641 /* return TRUE if an update was needed */
642 static BOOL locale_update_registry( HKEY hkey, const WCHAR *name, LCID lcid,
643 const LCTYPE *values, UINT nb_values )
645 static const WCHAR formatW[] = { '%','0','8','x',0 };
646 WCHAR bufferW[40];
647 UNICODE_STRING nameW;
648 DWORD count, i;
650 RtlInitUnicodeString( &nameW, name );
651 count = sizeof(bufferW);
652 if (!NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation, bufferW, count, &count))
654 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
655 LPCWSTR text = (LPCWSTR)info->Data;
657 if (strtoulW( text, NULL, 16 ) == lcid) return FALSE; /* already set correctly */
658 TRACE( "updating registry, locale %s changed %s -> %08x\n",
659 debugstr_w(name), debugstr_w(text), lcid );
661 else TRACE( "updating registry, locale %s changed none -> %08x\n", debugstr_w(name), lcid );
662 sprintfW( bufferW, formatW, lcid );
663 NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, (strlenW(bufferW) + 1) * sizeof(WCHAR) );
665 for (i = 0; i < nb_values; i++)
667 GetLocaleInfoW( lcid, values[i] | LOCALE_NOUSEROVERRIDE, bufferW,
668 sizeof(bufferW)/sizeof(WCHAR) );
669 SetLocaleInfoW( lcid, values[i], bufferW );
671 return TRUE;
675 /***********************************************************************
676 * LOCALE_InitRegistry
678 * Update registry contents on startup if the user locale has changed.
679 * This simulates the action of the Windows control panel.
681 void LOCALE_InitRegistry(void)
683 static const WCHAR acpW[] = {'A','C','P',0};
684 static const WCHAR oemcpW[] = {'O','E','M','C','P',0};
685 static const WCHAR maccpW[] = {'M','A','C','C','P',0};
686 static const WCHAR localeW[] = {'L','o','c','a','l','e',0};
687 static const WCHAR lc_ctypeW[] = { 'L','C','_','C','T','Y','P','E',0 };
688 static const WCHAR lc_monetaryW[] = { 'L','C','_','M','O','N','E','T','A','R','Y',0 };
689 static const WCHAR lc_numericW[] = { 'L','C','_','N','U','M','E','R','I','C',0 };
690 static const WCHAR lc_timeW[] = { 'L','C','_','T','I','M','E',0 };
691 static const WCHAR lc_measurementW[] = { 'L','C','_','M','E','A','S','U','R','E','M','E','N','T',0 };
692 static const WCHAR lc_telephoneW[] = { 'L','C','_','T','E','L','E','P','H','O','N','E',0 };
693 static const WCHAR lc_paperW[] = { 'L','C','_','P','A','P','E','R',0};
694 static const struct
696 LPCWSTR name;
697 USHORT value;
698 } update_cp_values[] = {
699 { acpW, LOCALE_IDEFAULTANSICODEPAGE },
700 { oemcpW, LOCALE_IDEFAULTCODEPAGE },
701 { maccpW, LOCALE_IDEFAULTMACCODEPAGE }
703 static const LCTYPE lc_messages_values[] = {
704 LOCALE_SABBREVLANGNAME,
705 LOCALE_SCOUNTRY,
706 LOCALE_SLIST };
707 static const LCTYPE lc_monetary_values[] = {
708 LOCALE_SCURRENCY,
709 LOCALE_ICURRENCY,
710 LOCALE_INEGCURR,
711 LOCALE_ICURRDIGITS,
712 LOCALE_ILZERO,
713 LOCALE_SMONDECIMALSEP,
714 LOCALE_SMONGROUPING,
715 LOCALE_SMONTHOUSANDSEP };
716 static const LCTYPE lc_numeric_values[] = {
717 LOCALE_SDECIMAL,
718 LOCALE_STHOUSAND,
719 LOCALE_IDIGITS,
720 LOCALE_IDIGITSUBSTITUTION,
721 LOCALE_SNATIVEDIGITS,
722 LOCALE_INEGNUMBER,
723 LOCALE_SNEGATIVESIGN,
724 LOCALE_SPOSITIVESIGN,
725 LOCALE_SGROUPING };
726 static const LCTYPE lc_time_values[] = {
727 LOCALE_S1159,
728 LOCALE_S2359,
729 LOCALE_STIME,
730 LOCALE_ITIME,
731 LOCALE_ITLZERO,
732 LOCALE_SSHORTDATE,
733 LOCALE_SLONGDATE,
734 LOCALE_SDATE,
735 LOCALE_ITIMEMARKPOSN,
736 LOCALE_ICALENDARTYPE,
737 LOCALE_IFIRSTDAYOFWEEK,
738 LOCALE_IFIRSTWEEKOFYEAR,
739 LOCALE_STIMEFORMAT,
740 LOCALE_SYEARMONTH,
741 LOCALE_IDATE };
742 static const LCTYPE lc_measurement_values[] = { LOCALE_IMEASURE };
743 static const LCTYPE lc_telephone_values[] = { LOCALE_ICOUNTRY };
744 static const LCTYPE lc_paper_values[] = { LOCALE_IPAPERSIZE };
746 UNICODE_STRING nameW;
747 WCHAR bufferW[80];
748 DWORD count, i;
749 HANDLE hkey;
750 LCID lcid = GetUserDefaultLCID();
752 if (!(hkey = create_registry_key()))
753 return; /* don't do anything if we can't create the registry key */
755 locale_update_registry( hkey, localeW, lcid_LC_MESSAGES, lc_messages_values,
756 sizeof(lc_messages_values)/sizeof(lc_messages_values[0]) );
757 locale_update_registry( hkey, lc_monetaryW, lcid_LC_MONETARY, lc_monetary_values,
758 sizeof(lc_monetary_values)/sizeof(lc_monetary_values[0]) );
759 locale_update_registry( hkey, lc_numericW, lcid_LC_NUMERIC, lc_numeric_values,
760 sizeof(lc_numeric_values)/sizeof(lc_numeric_values[0]) );
761 locale_update_registry( hkey, lc_timeW, lcid_LC_TIME, lc_time_values,
762 sizeof(lc_time_values)/sizeof(lc_time_values[0]) );
763 locale_update_registry( hkey, lc_measurementW, lcid_LC_MEASUREMENT, lc_measurement_values,
764 sizeof(lc_measurement_values)/sizeof(lc_measurement_values[0]) );
765 locale_update_registry( hkey, lc_telephoneW, lcid_LC_TELEPHONE, lc_telephone_values,
766 sizeof(lc_telephone_values)/sizeof(lc_telephone_values[0]) );
767 locale_update_registry( hkey, lc_paperW, lcid_LC_PAPER, lc_paper_values,
768 sizeof(lc_paper_values)/sizeof(lc_paper_values[0]) );
770 if (locale_update_registry( hkey, lc_ctypeW, lcid_LC_CTYPE, NULL, 0 ))
772 HKEY nls_key = NLS_RegOpenKey( 0, szCodepageKeyName );
774 for (i = 0; i < sizeof(update_cp_values)/sizeof(update_cp_values[0]); i++)
776 count = GetLocaleInfoW( lcid, update_cp_values[i].value | LOCALE_NOUSEROVERRIDE,
777 bufferW, sizeof(bufferW)/sizeof(WCHAR) );
778 RtlInitUnicodeString( &nameW, update_cp_values[i].name );
779 NtSetValueKey( nls_key, &nameW, 0, REG_SZ, bufferW, count * sizeof(WCHAR) );
781 NtClose( nls_key );
784 NtClose( hkey );
788 /***********************************************************************
789 * setup_unix_locales
791 static UINT setup_unix_locales(void)
793 struct locale_name locale_name;
794 WCHAR buffer[128], ctype_buff[128];
795 char *locale;
796 UINT unix_cp = 0;
798 if ((locale = setlocale( LC_CTYPE, NULL )))
800 strcpynAtoW( ctype_buff, locale, sizeof(ctype_buff)/sizeof(WCHAR) );
801 parse_locale_name( ctype_buff, &locale_name );
802 lcid_LC_CTYPE = locale_name.lcid;
803 unix_cp = locale_name.codepage;
805 if (!lcid_LC_CTYPE) /* this one needs a default value */
806 lcid_LC_CTYPE = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
808 TRACE( "got lcid %04x (%d matches) for LC_CTYPE=%s\n",
809 locale_name.lcid, locale_name.matches, debugstr_a(locale) );
811 #define GET_UNIX_LOCALE(cat) do \
812 if ((locale = setlocale( cat, NULL ))) \
814 strcpynAtoW( buffer, locale, sizeof(buffer)/sizeof(WCHAR) ); \
815 if (!strcmpW( buffer, ctype_buff )) lcid_##cat = lcid_LC_CTYPE; \
816 else { \
817 parse_locale_name( buffer, &locale_name ); \
818 lcid_##cat = locale_name.lcid; \
819 TRACE( "got lcid %04x (%d matches) for " #cat "=%s\n", \
820 locale_name.lcid, locale_name.matches, debugstr_a(locale) ); \
822 } while (0)
824 GET_UNIX_LOCALE( LC_COLLATE );
825 GET_UNIX_LOCALE( LC_MESSAGES );
826 GET_UNIX_LOCALE( LC_MONETARY );
827 GET_UNIX_LOCALE( LC_NUMERIC );
828 GET_UNIX_LOCALE( LC_TIME );
829 #ifdef LC_PAPER
830 GET_UNIX_LOCALE( LC_PAPER );
831 #endif
832 #ifdef LC_MEASUREMENT
833 GET_UNIX_LOCALE( LC_MEASUREMENT );
834 #endif
835 #ifdef LC_TELEPHONE
836 GET_UNIX_LOCALE( LC_TELEPHONE );
837 #endif
839 #undef GET_UNIX_LOCALE
841 return unix_cp;
845 /***********************************************************************
846 * GetUserDefaultLangID (KERNEL32.@)
848 * Get the default language Id for the current user.
850 * PARAMS
851 * None.
853 * RETURNS
854 * The current LANGID of the default language for the current user.
856 LANGID WINAPI GetUserDefaultLangID(void)
858 return LANGIDFROMLCID(GetUserDefaultLCID());
862 /***********************************************************************
863 * GetSystemDefaultLangID (KERNEL32.@)
865 * Get the default language Id for the system.
867 * PARAMS
868 * None.
870 * RETURNS
871 * The current LANGID of the default language for the system.
873 LANGID WINAPI GetSystemDefaultLangID(void)
875 return LANGIDFROMLCID(GetSystemDefaultLCID());
879 /***********************************************************************
880 * GetUserDefaultLCID (KERNEL32.@)
882 * Get the default locale Id for the current user.
884 * PARAMS
885 * None.
887 * RETURNS
888 * The current LCID of the default locale for the current user.
890 LCID WINAPI GetUserDefaultLCID(void)
892 LCID lcid;
893 NtQueryDefaultLocale( TRUE, &lcid );
894 return lcid;
898 /***********************************************************************
899 * GetSystemDefaultLCID (KERNEL32.@)
901 * Get the default locale Id for the system.
903 * PARAMS
904 * None.
906 * RETURNS
907 * The current LCID of the default locale for the system.
909 LCID WINAPI GetSystemDefaultLCID(void)
911 LCID lcid;
912 NtQueryDefaultLocale( FALSE, &lcid );
913 return lcid;
917 /***********************************************************************
918 * GetUserDefaultUILanguage (KERNEL32.@)
920 * Get the default user interface language Id for the current user.
922 * PARAMS
923 * None.
925 * RETURNS
926 * The current LANGID of the default UI language for the current user.
928 LANGID WINAPI GetUserDefaultUILanguage(void)
930 LANGID lang;
931 NtQueryDefaultUILanguage( &lang );
932 return lang;
936 /***********************************************************************
937 * GetSystemDefaultUILanguage (KERNEL32.@)
939 * Get the default user interface language Id for the system.
941 * PARAMS
942 * None.
944 * RETURNS
945 * The current LANGID of the default UI language for the system. This is
946 * typically the same language used during the installation process.
948 LANGID WINAPI GetSystemDefaultUILanguage(void)
950 LANGID lang;
951 NtQueryInstallUILanguage( &lang );
952 return lang;
956 /***********************************************************************
957 * LocaleNameToLCID (KERNEL32.@)
959 LCID WINAPI LocaleNameToLCID( LPCWSTR name, DWORD flags )
961 struct locale_name locale_name;
963 if (flags) FIXME( "unsupported flags %x\n", flags );
965 parse_locale_name( name, &locale_name );
967 TRACE( "found lcid %x for %s, matches %d\n",
968 locale_name.lcid, debugstr_w(name), locale_name.matches );
970 if (!locale_name.matches)
971 WARN( "locale %s not recognized, defaulting to English\n", debugstr_w(name) );
972 else if (locale_name.matches == 1)
973 WARN( "locale %s not recognized, defaulting to %s\n",
974 debugstr_w(name), debugstr_w(locale_name.lang) );
976 return locale_name.lcid;
980 /***********************************************************************
981 * LCIDToLocaleName (KERNEL32.@)
983 INT WINAPI LCIDToLocaleName( LCID lcid, LPWSTR name, INT count, DWORD flags )
985 if (flags) FIXME( "unsupported flags %x\n", flags );
987 return GetLocaleInfoW( lcid, LOCALE_SNAME | LOCALE_NOUSEROVERRIDE, name, count );
991 /******************************************************************************
992 * get_locale_value_name
994 * Gets the registry value name for a given lctype.
996 static const WCHAR *get_locale_value_name( DWORD lctype )
998 static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
999 static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
1000 static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
1001 static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
1002 static const WCHAR iDateW[] = {'i','D','a','t','e',0};
1003 static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
1004 static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
1005 static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
1006 static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
1007 static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
1008 static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
1009 static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
1010 static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
1011 static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
1012 static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
1013 static const WCHAR iTimePrefixW[] = {'i','T','i','m','e','P','r','e','f','i','x',0};
1014 static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
1015 static const WCHAR s1159W[] = {'s','1','1','5','9',0};
1016 static const WCHAR s2359W[] = {'s','2','3','5','9',0};
1017 static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
1018 static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
1019 static const WCHAR sDateW[] = {'s','D','a','t','e',0};
1020 static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
1021 static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
1022 static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
1023 static const WCHAR sListW[] = {'s','L','i','s','t',0};
1024 static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
1025 static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
1026 static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
1027 static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
1028 static const WCHAR sNativeDigitsW[] = {'s','N','a','t','i','v','e','D','i','g','i','t','s',0};
1029 static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
1030 static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
1031 static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
1032 static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
1033 static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
1034 static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
1035 static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
1036 static const WCHAR NumShapeW[] = {'N','u','m','s','h','a','p','e',0};
1038 switch (lctype)
1040 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
1041 * the values are stored in the registry, confirmed under Windows.
1043 case LOCALE_ICALENDARTYPE: return iCalendarTypeW;
1044 case LOCALE_ICURRDIGITS: return iCurrDigitsW;
1045 case LOCALE_ICURRENCY: return iCurrencyW;
1046 case LOCALE_IDIGITS: return iDigitsW;
1047 case LOCALE_IFIRSTDAYOFWEEK: return iFirstDayOfWeekW;
1048 case LOCALE_IFIRSTWEEKOFYEAR: return iFirstWeekOfYearW;
1049 case LOCALE_ILZERO: return iLZeroW;
1050 case LOCALE_IMEASURE: return iMeasureW;
1051 case LOCALE_INEGCURR: return iNegCurrW;
1052 case LOCALE_INEGNUMBER: return iNegNumberW;
1053 case LOCALE_IPAPERSIZE: return iPaperSizeW;
1054 case LOCALE_ITIME: return iTimeW;
1055 case LOCALE_S1159: return s1159W;
1056 case LOCALE_S2359: return s2359W;
1057 case LOCALE_SCURRENCY: return sCurrencyW;
1058 case LOCALE_SDATE: return sDateW;
1059 case LOCALE_SDECIMAL: return sDecimalW;
1060 case LOCALE_SGROUPING: return sGroupingW;
1061 case LOCALE_SLIST: return sListW;
1062 case LOCALE_SLONGDATE: return sLongDateW;
1063 case LOCALE_SMONDECIMALSEP: return sMonDecimalSepW;
1064 case LOCALE_SMONGROUPING: return sMonGroupingW;
1065 case LOCALE_SMONTHOUSANDSEP: return sMonThousandSepW;
1066 case LOCALE_SNEGATIVESIGN: return sNegativeSignW;
1067 case LOCALE_SPOSITIVESIGN: return sPositiveSignW;
1068 case LOCALE_SSHORTDATE: return sShortDateW;
1069 case LOCALE_STHOUSAND: return sThousandW;
1070 case LOCALE_STIME: return sTimeW;
1071 case LOCALE_STIMEFORMAT: return sTimeFormatW;
1072 case LOCALE_SYEARMONTH: return sYearMonthW;
1074 /* The following are not listed under MSDN as supported,
1075 * but seem to be used and also stored in the registry.
1077 case LOCALE_ICOUNTRY: return iCountryW;
1078 case LOCALE_IDATE: return iDateW;
1079 case LOCALE_ILDATE: return iLDateW;
1080 case LOCALE_ITLZERO: return iTLZeroW;
1081 case LOCALE_SCOUNTRY: return sCountryW;
1082 case LOCALE_SABBREVLANGNAME: return sLanguageW;
1084 /* The following are used in XP and later */
1085 case LOCALE_IDIGITSUBSTITUTION: return NumShapeW;
1086 case LOCALE_SNATIVEDIGITS: return sNativeDigitsW;
1087 case LOCALE_ITIMEMARKPOSN: return iTimePrefixW;
1089 return NULL;
1093 /******************************************************************************
1094 * get_registry_locale_info
1096 * Retrieve user-modified locale info from the registry.
1097 * Return length, 0 on error, -1 if not found.
1099 static INT get_registry_locale_info( LPCWSTR value, LPWSTR buffer, INT len )
1101 DWORD size;
1102 INT ret;
1103 HANDLE hkey;
1104 NTSTATUS status;
1105 UNICODE_STRING nameW;
1106 KEY_VALUE_PARTIAL_INFORMATION *info;
1107 static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
1109 if (!(hkey = create_registry_key())) return -1;
1111 RtlInitUnicodeString( &nameW, value );
1112 size = info_size + len * sizeof(WCHAR);
1114 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
1116 NtClose( hkey );
1117 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1118 return 0;
1121 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
1123 if (!status)
1125 ret = (size - info_size) / sizeof(WCHAR);
1126 /* append terminating null if needed */
1127 if (!ret || ((WCHAR *)info->Data)[ret-1])
1129 if (ret < len || !buffer) ret++;
1130 else
1132 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1133 ret = 0;
1136 if (ret && buffer)
1138 memcpy( buffer, info->Data, (ret-1) * sizeof(WCHAR) );
1139 buffer[ret-1] = 0;
1142 else if (status == STATUS_BUFFER_OVERFLOW && !buffer)
1144 ret = (size - info_size) / sizeof(WCHAR) + 1;
1146 else if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1148 ret = -1;
1150 else
1152 SetLastError( RtlNtStatusToDosError(status) );
1153 ret = 0;
1155 NtClose( hkey );
1156 HeapFree( GetProcessHeap(), 0, info );
1157 return ret;
1161 /******************************************************************************
1162 * GetLocaleInfoA (KERNEL32.@)
1164 * Get information about an aspect of a locale.
1166 * PARAMS
1167 * lcid [I] LCID of the locale
1168 * lctype [I] LCTYPE_ flags from "winnls.h"
1169 * buffer [O] Destination for the information
1170 * len [I] Length of buffer in characters
1172 * RETURNS
1173 * Success: The size of the data requested. If buffer is non-NULL, it is filled
1174 * with the information.
1175 * Failure: 0. Use GetLastError() to determine the cause.
1177 * NOTES
1178 * - LOCALE_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
1179 * - The string returned is NUL terminated, except for LOCALE_FONTSIGNATURE,
1180 * which is a bit string.
1182 INT WINAPI GetLocaleInfoA( LCID lcid, LCTYPE lctype, LPSTR buffer, INT len )
1184 WCHAR *bufferW;
1185 INT lenW, ret;
1187 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
1189 if (len < 0 || (len && !buffer))
1191 SetLastError( ERROR_INVALID_PARAMETER );
1192 return 0;
1194 if (lctype & LOCALE_RETURN_GENITIVE_NAMES )
1196 SetLastError( ERROR_INVALID_FLAGS );
1197 return 0;
1200 if (!len) buffer = NULL;
1202 if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
1204 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
1206 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1207 return 0;
1209 if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
1211 if ((lctype & LOCALE_RETURN_NUMBER) ||
1212 ((lctype & ~LOCALE_LOCALEINFOFLAGSMASK) == LOCALE_FONTSIGNATURE))
1214 /* it's not an ASCII string, just bytes */
1215 ret *= sizeof(WCHAR);
1216 if (buffer)
1218 if (ret <= len) memcpy( buffer, bufferW, ret );
1219 else
1221 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1222 ret = 0;
1226 else
1228 UINT codepage = CP_ACP;
1229 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
1230 ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
1233 HeapFree( GetProcessHeap(), 0, bufferW );
1234 return ret;
1238 /******************************************************************************
1239 * GetLocaleInfoW (KERNEL32.@)
1241 * See GetLocaleInfoA.
1243 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
1245 LANGID lang_id;
1246 HRSRC hrsrc;
1247 HGLOBAL hmem;
1248 INT ret;
1249 UINT lcflags;
1250 const WCHAR *p;
1251 unsigned int i;
1253 if (len < 0 || (len && !buffer))
1255 SetLastError( ERROR_INVALID_PARAMETER );
1256 return 0;
1258 if (lctype & LOCALE_RETURN_GENITIVE_NAMES &&
1259 !is_genitive_name_supported( lctype ))
1261 SetLastError( ERROR_INVALID_FLAGS );
1262 return 0;
1265 if (!len) buffer = NULL;
1267 lcid = convert_default_lcid( lcid, lctype );
1269 lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
1270 lctype &= 0xffff;
1272 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
1274 /* first check for overrides in the registry */
1276 if (!(lcflags & LOCALE_NOUSEROVERRIDE) &&
1277 lcid == convert_default_lcid( LOCALE_USER_DEFAULT, lctype ))
1279 const WCHAR *value = get_locale_value_name(lctype);
1281 if (value)
1283 if (lcflags & LOCALE_RETURN_NUMBER)
1285 WCHAR tmp[16];
1286 ret = get_registry_locale_info( value, tmp, sizeof(tmp)/sizeof(WCHAR) );
1287 if (ret > 0)
1289 WCHAR *end;
1290 UINT number = strtolW( tmp, &end, 10 );
1291 if (*end) /* invalid number */
1293 SetLastError( ERROR_INVALID_FLAGS );
1294 return 0;
1296 ret = sizeof(UINT)/sizeof(WCHAR);
1297 if (!buffer) return ret;
1298 if (ret > len)
1300 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1301 return 0;
1303 memcpy( buffer, &number, sizeof(number) );
1306 else ret = get_registry_locale_info( value, buffer, len );
1308 if (ret != -1) return ret;
1312 /* now load it from kernel resources */
1314 lang_id = LANGIDFROMLCID( lcid );
1316 /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
1317 if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
1318 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
1320 if (!(hrsrc = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
1321 ULongToPtr((lctype >> 4) + 1), lang_id )))
1323 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
1324 return 0;
1326 if (!(hmem = LoadResource( kernel32_handle, hrsrc )))
1327 return 0;
1329 p = LockResource( hmem );
1330 for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
1332 if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
1333 else if (is_genitive_name_supported( lctype ) && *p)
1335 /* genitive form's stored after a null separator from a nominative */
1336 for (i = 1; i <= *p; i++) if (!p[i]) break;
1338 if (i <= *p && (lcflags & LOCALE_RETURN_GENITIVE_NAMES))
1340 ret = *p - i + 1;
1341 p += i;
1343 else ret = i;
1345 else
1346 ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
1348 if (!buffer) return ret;
1350 if (ret > len)
1352 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1353 return 0;
1356 if (lcflags & LOCALE_RETURN_NUMBER)
1358 UINT number;
1359 WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
1360 if (!tmp) return 0;
1361 memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
1362 tmp[*p] = 0;
1363 number = strtolW( tmp, &end, 10 );
1364 if (!*end)
1365 memcpy( buffer, &number, sizeof(number) );
1366 else /* invalid number */
1368 SetLastError( ERROR_INVALID_FLAGS );
1369 ret = 0;
1371 HeapFree( GetProcessHeap(), 0, tmp );
1373 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning number %d\n",
1374 lcid, lctype, buffer, len, number );
1376 else
1378 memcpy( buffer, p + 1, ret * sizeof(WCHAR) );
1379 if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
1381 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning %d %s\n",
1382 lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
1384 return ret;
1388 /******************************************************************************
1389 * SetLocaleInfoA [KERNEL32.@]
1391 * Set information about an aspect of a locale.
1393 * PARAMS
1394 * lcid [I] LCID of the locale
1395 * lctype [I] LCTYPE_ flags from "winnls.h"
1396 * data [I] Information to set
1398 * RETURNS
1399 * Success: TRUE. The information given will be returned by GetLocaleInfoA()
1400 * whenever it is called without LOCALE_NOUSEROVERRIDE.
1401 * Failure: FALSE. Use GetLastError() to determine the cause.
1403 * NOTES
1404 * - Values are only be set for the current user locale; the system locale
1405 * settings cannot be changed.
1406 * - Any settings changed by this call are lost when the locale is changed by
1407 * the control panel (in Wine, this happens every time you change LANG).
1408 * - The native implementation of this function does not check that lcid matches
1409 * the current user locale, and simply sets the new values. Wine warns you in
1410 * this case, but behaves the same.
1412 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
1414 UINT codepage = CP_ACP;
1415 WCHAR *strW;
1416 DWORD len;
1417 BOOL ret;
1419 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
1421 if (!data)
1423 SetLastError( ERROR_INVALID_PARAMETER );
1424 return FALSE;
1426 len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
1427 if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1429 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1430 return FALSE;
1432 MultiByteToWideChar( codepage, 0, data, -1, strW, len );
1433 ret = SetLocaleInfoW( lcid, lctype, strW );
1434 HeapFree( GetProcessHeap(), 0, strW );
1435 return ret;
1439 /******************************************************************************
1440 * SetLocaleInfoW (KERNEL32.@)
1442 * See SetLocaleInfoA.
1444 BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
1446 const WCHAR *value;
1447 static const WCHAR intlW[] = {'i','n','t','l',0 };
1448 UNICODE_STRING valueW;
1449 NTSTATUS status;
1450 HANDLE hkey;
1452 lctype &= 0xffff;
1453 value = get_locale_value_name( lctype );
1455 if (!data || !value)
1457 SetLastError( ERROR_INVALID_PARAMETER );
1458 return FALSE;
1461 if (lctype == LOCALE_IDATE || lctype == LOCALE_ILDATE)
1463 SetLastError( ERROR_INVALID_FLAGS );
1464 return FALSE;
1467 TRACE("setting %x (%s) to %s\n", lctype, debugstr_w(value), debugstr_w(data) );
1469 /* FIXME: should check that data to set is sane */
1471 /* FIXME: profile functions should map to registry */
1472 WriteProfileStringW( intlW, value, data );
1474 if (!(hkey = create_registry_key())) return FALSE;
1475 RtlInitUnicodeString( &valueW, value );
1476 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
1478 if (lctype == LOCALE_SSHORTDATE || lctype == LOCALE_SLONGDATE)
1480 /* Set I-value from S value */
1481 WCHAR *lpD, *lpM, *lpY;
1482 WCHAR szBuff[2];
1484 lpD = strrchrW(data, 'd');
1485 lpM = strrchrW(data, 'M');
1486 lpY = strrchrW(data, 'y');
1488 if (lpD <= lpM)
1490 szBuff[0] = '1'; /* D-M-Y */
1492 else
1494 if (lpY <= lpM)
1495 szBuff[0] = '2'; /* Y-M-D */
1496 else
1497 szBuff[0] = '0'; /* M-D-Y */
1500 szBuff[1] = '\0';
1502 if (lctype == LOCALE_SSHORTDATE)
1503 lctype = LOCALE_IDATE;
1504 else
1505 lctype = LOCALE_ILDATE;
1507 value = get_locale_value_name( lctype );
1509 WriteProfileStringW( intlW, value, szBuff );
1511 RtlInitUnicodeString( &valueW, value );
1512 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, szBuff, sizeof(szBuff) );
1515 NtClose( hkey );
1517 if (status) SetLastError( RtlNtStatusToDosError(status) );
1518 return !status;
1522 /******************************************************************************
1523 * GetACP (KERNEL32.@)
1525 * Get the current Ansi code page Id for the system.
1527 * PARAMS
1528 * None.
1530 * RETURNS
1531 * The current Ansi code page identifier for the system.
1533 UINT WINAPI GetACP(void)
1535 assert( ansi_cptable );
1536 return ansi_cptable->info.codepage;
1540 /******************************************************************************
1541 * SetCPGlobal (KERNEL32.@)
1543 * Set the current Ansi code page Id for the system.
1545 * PARAMS
1546 * acp [I] code page ID to be the new ACP.
1548 * RETURNS
1549 * The previous ACP.
1551 UINT WINAPI SetCPGlobal( UINT acp )
1553 UINT ret = GetACP();
1554 const union cptable *new_cptable = wine_cp_get_table( acp );
1556 if (new_cptable) ansi_cptable = new_cptable;
1557 return ret;
1561 /***********************************************************************
1562 * GetOEMCP (KERNEL32.@)
1564 * Get the current OEM code page Id for the system.
1566 * PARAMS
1567 * None.
1569 * RETURNS
1570 * The current OEM code page identifier for the system.
1572 UINT WINAPI GetOEMCP(void)
1574 assert( oem_cptable );
1575 return oem_cptable->info.codepage;
1579 /***********************************************************************
1580 * IsValidCodePage (KERNEL32.@)
1582 * Determine if a given code page identifier is valid.
1584 * PARAMS
1585 * codepage [I] Code page Id to verify.
1587 * RETURNS
1588 * TRUE, If codepage is valid and available on the system,
1589 * FALSE otherwise.
1591 BOOL WINAPI IsValidCodePage( UINT codepage )
1593 switch(codepage) {
1594 case CP_UTF7:
1595 case CP_UTF8:
1596 return TRUE;
1597 default:
1598 return wine_cp_get_table( codepage ) != NULL;
1603 /***********************************************************************
1604 * IsDBCSLeadByteEx (KERNEL32.@)
1606 * Determine if a character is a lead byte in a given code page.
1608 * PARAMS
1609 * codepage [I] Code page for the test.
1610 * testchar [I] Character to test
1612 * RETURNS
1613 * TRUE, if testchar is a lead byte in codepage,
1614 * FALSE otherwise.
1616 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
1618 const union cptable *table = get_codepage_table( codepage );
1619 return table && wine_is_dbcs_leadbyte( table, testchar );
1623 /***********************************************************************
1624 * IsDBCSLeadByte (KERNEL32.@)
1625 * IsDBCSLeadByte (KERNEL.207)
1627 * Determine if a character is a lead byte.
1629 * PARAMS
1630 * testchar [I] Character to test
1632 * RETURNS
1633 * TRUE, if testchar is a lead byte in the Ansii code page,
1634 * FALSE otherwise.
1636 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
1638 if (!ansi_cptable) return FALSE;
1639 return wine_is_dbcs_leadbyte( ansi_cptable, testchar );
1643 /***********************************************************************
1644 * GetCPInfo (KERNEL32.@)
1646 * Get information about a code page.
1648 * PARAMS
1649 * codepage [I] Code page number
1650 * cpinfo [O] Destination for code page information
1652 * RETURNS
1653 * Success: TRUE. cpinfo is updated with the information about codepage.
1654 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1656 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
1658 const union cptable *table;
1660 if (!cpinfo)
1662 SetLastError( ERROR_INVALID_PARAMETER );
1663 return FALSE;
1666 if (!(table = get_codepage_table( codepage )))
1668 switch(codepage)
1670 case CP_UTF7:
1671 case CP_UTF8:
1672 cpinfo->DefaultChar[0] = 0x3f;
1673 cpinfo->DefaultChar[1] = 0;
1674 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1675 cpinfo->MaxCharSize = (codepage == CP_UTF7) ? 5 : 4;
1676 return TRUE;
1679 SetLastError( ERROR_INVALID_PARAMETER );
1680 return FALSE;
1682 if (table->info.def_char & 0xff00)
1684 cpinfo->DefaultChar[0] = (table->info.def_char & 0xff00) >> 8;
1685 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
1687 else
1689 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
1690 cpinfo->DefaultChar[1] = 0;
1692 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
1693 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
1694 else
1695 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1697 return TRUE;
1700 /***********************************************************************
1701 * GetCPInfoExA (KERNEL32.@)
1703 * Get extended information about a code page.
1705 * PARAMS
1706 * codepage [I] Code page number
1707 * dwFlags [I] Reserved, must to 0.
1708 * cpinfo [O] Destination for code page information
1710 * RETURNS
1711 * Success: TRUE. cpinfo is updated with the information about codepage.
1712 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1714 BOOL WINAPI GetCPInfoExA( UINT codepage, DWORD dwFlags, LPCPINFOEXA cpinfo )
1716 CPINFOEXW cpinfoW;
1718 if (!GetCPInfoExW( codepage, dwFlags, &cpinfoW ))
1719 return FALSE;
1721 /* the layout is the same except for CodePageName */
1722 memcpy(cpinfo, &cpinfoW, sizeof(CPINFOEXA));
1723 WideCharToMultiByte(CP_ACP, 0, cpinfoW.CodePageName, -1, cpinfo->CodePageName, sizeof(cpinfo->CodePageName), NULL, NULL);
1724 return TRUE;
1727 /***********************************************************************
1728 * GetCPInfoExW (KERNEL32.@)
1730 * Unicode version of GetCPInfoExA.
1732 BOOL WINAPI GetCPInfoExW( UINT codepage, DWORD dwFlags, LPCPINFOEXW cpinfo )
1734 if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
1735 return FALSE;
1737 switch(codepage)
1739 case CP_UTF7:
1741 static const WCHAR utf7[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','7',')',0};
1743 cpinfo->CodePage = CP_UTF7;
1744 cpinfo->UnicodeDefaultChar = 0x3f;
1745 strcpyW(cpinfo->CodePageName, utf7);
1746 break;
1749 case CP_UTF8:
1751 static const WCHAR utf8[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','8',')',0};
1753 cpinfo->CodePage = CP_UTF8;
1754 cpinfo->UnicodeDefaultChar = 0x3f;
1755 strcpyW(cpinfo->CodePageName, utf8);
1756 break;
1759 default:
1761 const union cptable *table = get_codepage_table( codepage );
1763 cpinfo->CodePage = table->info.codepage;
1764 cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
1765 MultiByteToWideChar( CP_ACP, 0, table->info.name, -1, cpinfo->CodePageName,
1766 sizeof(cpinfo->CodePageName)/sizeof(WCHAR));
1767 break;
1770 return TRUE;
1773 /***********************************************************************
1774 * EnumSystemCodePagesA (KERNEL32.@)
1776 * Call a user defined function for every code page installed on the system.
1778 * PARAMS
1779 * lpfnCodePageEnum [I] User CODEPAGE_ENUMPROC to call with each found code page
1780 * flags [I] Reserved, set to 0.
1782 * RETURNS
1783 * TRUE, If all code pages have been enumerated, or
1784 * FALSE if lpfnCodePageEnum returned FALSE to stop the enumeration.
1786 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
1788 const union cptable *table;
1789 char buffer[10];
1790 int index = 0;
1792 for (;;)
1794 if (!(table = wine_cp_enum_table( index++ ))) break;
1795 sprintf( buffer, "%d", table->info.codepage );
1796 if (!lpfnCodePageEnum( buffer )) break;
1798 return TRUE;
1802 /***********************************************************************
1803 * EnumSystemCodePagesW (KERNEL32.@)
1805 * See EnumSystemCodePagesA.
1807 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
1809 const union cptable *table;
1810 WCHAR buffer[10], *p;
1811 int page, index = 0;
1813 for (;;)
1815 if (!(table = wine_cp_enum_table( index++ ))) break;
1816 p = buffer + sizeof(buffer)/sizeof(WCHAR);
1817 *--p = 0;
1818 page = table->info.codepage;
1821 *--p = '0' + (page % 10);
1822 page /= 10;
1823 } while( page );
1824 if (!lpfnCodePageEnum( p )) break;
1826 return TRUE;
1830 /***********************************************************************
1831 * MultiByteToWideChar (KERNEL32.@)
1833 * Convert a multibyte character string into a Unicode string.
1835 * PARAMS
1836 * page [I] Codepage character set to convert from
1837 * flags [I] Character mapping flags
1838 * src [I] Source string buffer
1839 * srclen [I] Length of src (in bytes), or -1 if src is NUL terminated
1840 * dst [O] Destination buffer
1841 * dstlen [I] Length of dst (in WCHARs), or 0 to compute the required length
1843 * RETURNS
1844 * Success: If dstlen > 0, the number of characters written to dst.
1845 * If dstlen == 0, the number of characters needed to perform the
1846 * conversion. In both cases the count includes the terminating NUL.
1847 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1848 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1849 * and dstlen != 0; ERROR_INVALID_PARAMETER, if an invalid parameter
1850 * is passed, and ERROR_NO_UNICODE_TRANSLATION if no translation is
1851 * possible for src.
1853 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
1854 LPWSTR dst, INT dstlen )
1856 const union cptable *table;
1857 int ret;
1859 if (!src || (!dst && dstlen))
1861 SetLastError( ERROR_INVALID_PARAMETER );
1862 return 0;
1865 if (srclen < 0) srclen = strlen(src) + 1;
1867 switch(page)
1869 case CP_SYMBOL:
1870 if( flags)
1872 SetLastError( ERROR_INVALID_PARAMETER );
1873 return 0;
1875 ret = wine_cpsymbol_mbstowcs( src, srclen, dst, dstlen );
1876 break;
1877 case CP_UTF7:
1878 FIXME("UTF-7 not supported\n");
1879 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1880 return 0;
1881 case CP_UNIXCP:
1882 if (unix_cptable)
1884 ret = wine_cp_mbstowcs( unix_cptable, flags, src, srclen, dst, dstlen );
1885 break;
1887 #ifdef __APPLE__
1888 flags |= MB_COMPOSITE; /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
1889 #endif
1890 /* fall through */
1891 case CP_UTF8:
1892 ret = wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
1893 break;
1894 default:
1895 if (!(table = get_codepage_table( page )))
1897 SetLastError( ERROR_INVALID_PARAMETER );
1898 return 0;
1900 ret = wine_cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
1901 break;
1904 if (ret < 0)
1906 switch(ret)
1908 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1909 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1911 ret = 0;
1913 TRACE("cp %d %s -> %s, ret = %d\n",
1914 page, debugstr_an(src, srclen), debugstr_wn(dst, ret), ret);
1915 return ret;
1919 /***********************************************************************
1920 * WideCharToMultiByte (KERNEL32.@)
1922 * Convert a Unicode character string into a multibyte string.
1924 * PARAMS
1925 * page [I] Code page character set to convert to
1926 * flags [I] Mapping Flags (MB_ constants from "winnls.h").
1927 * src [I] Source string buffer
1928 * srclen [I] Length of src (in WCHARs), or -1 if src is NUL terminated
1929 * dst [O] Destination buffer
1930 * dstlen [I] Length of dst (in bytes), or 0 to compute the required length
1931 * defchar [I] Default character to use for conversion if no exact
1932 * conversion can be made
1933 * used [O] Set if default character was used in the conversion
1935 * RETURNS
1936 * Success: If dstlen > 0, the number of characters written to dst.
1937 * If dstlen == 0, number of characters needed to perform the
1938 * conversion. In both cases the count includes the terminating NUL.
1939 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1940 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1941 * and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
1942 * parameter was given.
1944 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
1945 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
1947 const union cptable *table;
1948 int ret, used_tmp;
1950 if (!src || (!dst && dstlen))
1952 SetLastError( ERROR_INVALID_PARAMETER );
1953 return 0;
1956 if (srclen < 0) srclen = strlenW(src) + 1;
1958 switch(page)
1960 case CP_SYMBOL:
1961 if( flags || defchar || used)
1963 SetLastError( ERROR_INVALID_PARAMETER );
1964 return 0;
1966 ret = wine_cpsymbol_wcstombs( src, srclen, dst, dstlen );
1967 break;
1968 case CP_UTF7:
1969 FIXME("UTF-7 not supported\n");
1970 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1971 return 0;
1972 case CP_UNIXCP:
1973 if (unix_cptable)
1975 ret = wine_cp_wcstombs( unix_cptable, flags, src, srclen, dst, dstlen,
1976 defchar, used ? &used_tmp : NULL );
1977 break;
1979 /* fall through */
1980 case CP_UTF8:
1981 if (used) *used = FALSE; /* all chars are valid for UTF-8 */
1982 ret = wine_utf8_wcstombs( flags, src, srclen, dst, dstlen );
1983 break;
1984 default:
1985 if (!(table = get_codepage_table( page )))
1987 SetLastError( ERROR_INVALID_PARAMETER );
1988 return 0;
1990 ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
1991 defchar, used ? &used_tmp : NULL );
1992 if (used) *used = used_tmp;
1993 break;
1996 if (ret < 0)
1998 switch(ret)
2000 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
2001 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
2003 ret = 0;
2005 TRACE("cp %d %s -> %s, ret = %d\n",
2006 page, debugstr_wn(src, srclen), debugstr_an(dst, ret), ret);
2007 return ret;
2011 /***********************************************************************
2012 * GetThreadLocale (KERNEL32.@)
2014 * Get the current threads locale.
2016 * PARAMS
2017 * None.
2019 * RETURNS
2020 * The LCID currently associated with the calling thread.
2022 LCID WINAPI GetThreadLocale(void)
2024 LCID ret = NtCurrentTeb()->CurrentLocale;
2025 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
2026 return ret;
2029 /**********************************************************************
2030 * SetThreadLocale (KERNEL32.@)
2032 * Set the current threads locale.
2034 * PARAMS
2035 * lcid [I] LCID of the locale to set
2037 * RETURNS
2038 * Success: TRUE. The threads locale is set to lcid.
2039 * Failure: FALSE. Use GetLastError() to determine the cause.
2041 BOOL WINAPI SetThreadLocale( LCID lcid )
2043 TRACE("(0x%04X)\n", lcid);
2045 lcid = ConvertDefaultLocale(lcid);
2047 if (lcid != GetThreadLocale())
2049 if (!IsValidLocale(lcid, LCID_SUPPORTED))
2051 SetLastError(ERROR_INVALID_PARAMETER);
2052 return FALSE;
2055 NtCurrentTeb()->CurrentLocale = lcid;
2056 kernel_get_thread_data()->code_page = get_lcid_codepage( lcid );
2058 return TRUE;
2061 /**********************************************************************
2062 * SetThreadUILanguage (KERNEL32.@)
2064 * Set the current threads UI language.
2066 * PARAMS
2067 * langid [I] LANGID of the language to set, or 0 to use
2068 * the available language which is best supported
2069 * for console applications
2071 * RETURNS
2072 * Success: The return value is the same as the input value.
2073 * Failure: The return value differs from the input value.
2074 * Use GetLastError() to determine the cause.
2076 LANGID WINAPI SetThreadUILanguage( LANGID langid )
2078 TRACE("(0x%04x) stub - returning success\n", langid);
2079 return langid;
2082 /******************************************************************************
2083 * ConvertDefaultLocale (KERNEL32.@)
2085 * Convert a default locale identifier into a real identifier.
2087 * PARAMS
2088 * lcid [I] LCID identifier of the locale to convert
2090 * RETURNS
2091 * lcid unchanged, if not a default locale or its sublanguage is
2092 * not SUBLANG_NEUTRAL.
2093 * GetSystemDefaultLCID(), if lcid == LOCALE_SYSTEM_DEFAULT.
2094 * GetUserDefaultLCID(), if lcid == LOCALE_USER_DEFAULT or LOCALE_NEUTRAL.
2095 * Otherwise, lcid with sublanguage changed to SUBLANG_DEFAULT.
2097 LCID WINAPI ConvertDefaultLocale( LCID lcid )
2099 LANGID langid;
2101 switch (lcid)
2103 case LOCALE_SYSTEM_DEFAULT:
2104 lcid = GetSystemDefaultLCID();
2105 break;
2106 case LOCALE_USER_DEFAULT:
2107 case LOCALE_NEUTRAL:
2108 lcid = GetUserDefaultLCID();
2109 break;
2110 default:
2111 /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
2112 langid = LANGIDFROMLCID(lcid);
2113 if (SUBLANGID(langid) == SUBLANG_NEUTRAL)
2115 langid = MAKELANGID(PRIMARYLANGID(langid), SUBLANG_DEFAULT);
2116 lcid = MAKELCID(langid, SORTIDFROMLCID(lcid));
2119 return lcid;
2123 /******************************************************************************
2124 * IsValidLocale (KERNEL32.@)
2126 * Determine if a locale is valid.
2128 * PARAMS
2129 * lcid [I] LCID of the locale to check
2130 * flags [I] LCID_SUPPORTED = Valid, LCID_INSTALLED = Valid and installed on the system
2132 * RETURNS
2133 * TRUE, if lcid is valid,
2134 * FALSE, otherwise.
2136 * NOTES
2137 * Wine does not currently make the distinction between supported and installed. All
2138 * languages supported are installed by default.
2140 BOOL WINAPI IsValidLocale( LCID lcid, DWORD flags )
2142 /* check if language is registered in the kernel32 resources */
2143 return FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
2144 (LPCWSTR)LOCALE_ILANGUAGE, LANGIDFROMLCID(lcid)) != 0;
2148 static BOOL CALLBACK enum_lang_proc_a( HMODULE hModule, LPCSTR type,
2149 LPCSTR name, WORD LangID, LONG_PTR lParam )
2151 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
2152 char buf[20];
2154 sprintf(buf, "%08x", (UINT)LangID);
2155 return lpfnLocaleEnum( buf );
2158 static BOOL CALLBACK enum_lang_proc_w( HMODULE hModule, LPCWSTR type,
2159 LPCWSTR name, WORD LangID, LONG_PTR lParam )
2161 static const WCHAR formatW[] = {'%','0','8','x',0};
2162 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
2163 WCHAR buf[20];
2164 sprintfW( buf, formatW, (UINT)LangID );
2165 return lpfnLocaleEnum( buf );
2168 /******************************************************************************
2169 * EnumSystemLocalesA (KERNEL32.@)
2171 * Call a users function for each locale available on the system.
2173 * PARAMS
2174 * lpfnLocaleEnum [I] Callback function to call for each locale
2175 * dwFlags [I] LOCALE_SUPPORTED=All supported, LOCALE_INSTALLED=Installed only
2177 * RETURNS
2178 * Success: TRUE.
2179 * Failure: FALSE. Use GetLastError() to determine the cause.
2181 BOOL WINAPI EnumSystemLocalesA( LOCALE_ENUMPROCA lpfnLocaleEnum, DWORD dwFlags )
2183 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
2184 EnumResourceLanguagesA( kernel32_handle, (LPSTR)RT_STRING,
2185 (LPCSTR)LOCALE_ILANGUAGE, enum_lang_proc_a,
2186 (LONG_PTR)lpfnLocaleEnum);
2187 return TRUE;
2191 /******************************************************************************
2192 * EnumSystemLocalesW (KERNEL32.@)
2194 * See EnumSystemLocalesA.
2196 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum, DWORD dwFlags )
2198 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
2199 EnumResourceLanguagesW( kernel32_handle, (LPWSTR)RT_STRING,
2200 (LPCWSTR)LOCALE_ILANGUAGE, enum_lang_proc_w,
2201 (LONG_PTR)lpfnLocaleEnum);
2202 return TRUE;
2206 /***********************************************************************
2207 * VerLanguageNameA (KERNEL32.@)
2209 * Get the name of a language.
2211 * PARAMS
2212 * wLang [I] LANGID of the language
2213 * szLang [O] Destination for the language name
2215 * RETURNS
2216 * Success: The size of the language name. If szLang is non-NULL, it is filled
2217 * with the name.
2218 * Failure: 0. Use GetLastError() to determine the cause.
2221 DWORD WINAPI VerLanguageNameA( DWORD wLang, LPSTR szLang, DWORD nSize )
2223 return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
2227 /***********************************************************************
2228 * VerLanguageNameW (KERNEL32.@)
2230 * See VerLanguageNameA.
2232 DWORD WINAPI VerLanguageNameW( DWORD wLang, LPWSTR szLang, DWORD nSize )
2234 return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
2238 /******************************************************************************
2239 * GetStringTypeW (KERNEL32.@)
2241 * See GetStringTypeA.
2243 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
2245 if (count == -1) count = strlenW(src) + 1;
2246 switch(type)
2248 case CT_CTYPE1:
2249 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
2250 break;
2251 case CT_CTYPE2:
2252 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
2253 break;
2254 case CT_CTYPE3:
2256 WARN("CT_CTYPE3: semi-stub.\n");
2257 while (count--)
2259 int c = *src;
2260 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
2262 type1 = get_char_typeW( *src++ ) & 0xfff;
2263 /* try to construct type3 from type1 */
2264 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
2265 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
2266 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
2267 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
2268 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
2269 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
2270 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
2272 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
2273 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
2274 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
2275 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
2276 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
2277 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
2278 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
2279 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
2281 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
2282 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
2283 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
2284 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
2285 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
2286 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
2287 *chartype++ = type3;
2289 break;
2291 default:
2292 SetLastError( ERROR_INVALID_PARAMETER );
2293 return FALSE;
2295 return TRUE;
2299 /******************************************************************************
2300 * GetStringTypeExW (KERNEL32.@)
2302 * See GetStringTypeExA.
2304 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
2306 /* locale is ignored for Unicode */
2307 return GetStringTypeW( type, src, count, chartype );
2311 /******************************************************************************
2312 * GetStringTypeA (KERNEL32.@)
2314 * Get characteristics of the characters making up a string.
2316 * PARAMS
2317 * locale [I] Locale Id for the string
2318 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
2319 * src [I] String to analyse
2320 * count [I] Length of src in chars, or -1 if src is NUL terminated
2321 * chartype [O] Destination for the calculated characteristics
2323 * RETURNS
2324 * Success: TRUE. chartype is filled with the requested characteristics of each char
2325 * in src.
2326 * Failure: FALSE. Use GetLastError() to determine the cause.
2328 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
2330 UINT cp;
2331 INT countW;
2332 LPWSTR srcW;
2333 BOOL ret = FALSE;
2335 if(count == -1) count = strlen(src) + 1;
2337 if (!(cp = get_lcid_codepage( locale )))
2339 FIXME("For locale %04x using current ANSI code page\n", locale);
2340 cp = GetACP();
2343 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
2344 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
2346 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
2348 * NOTE: the target buffer has 1 word for each CHARACTER in the source
2349 * string, with multibyte characters there maybe be more bytes in count
2350 * than character space in the buffer!
2352 ret = GetStringTypeW(type, srcW, countW, chartype);
2353 HeapFree(GetProcessHeap(), 0, srcW);
2355 return ret;
2358 /******************************************************************************
2359 * GetStringTypeExA (KERNEL32.@)
2361 * Get characteristics of the characters making up a string.
2363 * PARAMS
2364 * locale [I] Locale Id for the string
2365 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
2366 * src [I] String to analyse
2367 * count [I] Length of src in chars, or -1 if src is NUL terminated
2368 * chartype [O] Destination for the calculated characteristics
2370 * RETURNS
2371 * Success: TRUE. chartype is filled with the requested characteristics of each char
2372 * in src.
2373 * Failure: FALSE. Use GetLastError() to determine the cause.
2375 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
2377 return GetStringTypeA(locale, type, src, count, chartype);
2381 /*************************************************************************
2382 * LCMapStringW (KERNEL32.@)
2384 * See LCMapStringA.
2386 INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
2387 LPWSTR dst, INT dstlen)
2389 LPWSTR dst_ptr;
2391 if (!src || !srclen || dstlen < 0)
2393 SetLastError(ERROR_INVALID_PARAMETER);
2394 return 0;
2397 /* mutually exclusive flags */
2398 if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
2399 (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
2400 (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
2401 (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE))
2403 SetLastError(ERROR_INVALID_FLAGS);
2404 return 0;
2407 if (!dstlen) dst = NULL;
2409 lcid = ConvertDefaultLocale(lcid);
2411 if (flags & LCMAP_SORTKEY)
2413 INT ret;
2414 if (src == dst)
2416 SetLastError(ERROR_INVALID_FLAGS);
2417 return 0;
2420 if (srclen < 0) srclen = strlenW(src);
2422 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
2423 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
2425 ret = wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
2426 if (ret == 0)
2427 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2428 else
2429 ret++;
2430 return ret;
2433 /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
2434 if (flags & SORT_STRINGSORT)
2436 SetLastError(ERROR_INVALID_FLAGS);
2437 return 0;
2440 if (srclen < 0) srclen = strlenW(src) + 1;
2442 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
2443 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
2445 if (!dst) /* return required string length */
2447 INT len;
2449 for (len = 0; srclen; src++, srclen--)
2451 WCHAR wch = *src;
2452 /* tests show that win2k just ignores NORM_IGNORENONSPACE,
2453 * and skips white space and punctuation characters for
2454 * NORM_IGNORESYMBOLS.
2456 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2457 continue;
2458 len++;
2460 return len;
2463 if (flags & LCMAP_UPPERCASE)
2465 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2467 WCHAR wch = *src;
2468 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2469 continue;
2470 *dst_ptr++ = toupperW(wch);
2471 dstlen--;
2474 else if (flags & LCMAP_LOWERCASE)
2476 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2478 WCHAR wch = *src;
2479 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2480 continue;
2481 *dst_ptr++ = tolowerW(wch);
2482 dstlen--;
2485 else
2487 if (src == dst)
2489 SetLastError(ERROR_INVALID_FLAGS);
2490 return 0;
2492 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2494 WCHAR wch = *src;
2495 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2496 continue;
2497 *dst_ptr++ = wch;
2498 dstlen--;
2502 if (srclen)
2504 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2505 return 0;
2508 return dst_ptr - dst;
2511 /*************************************************************************
2512 * LCMapStringA (KERNEL32.@)
2514 * Map characters in a locale sensitive string.
2516 * PARAMS
2517 * lcid [I] LCID for the conversion.
2518 * flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
2519 * src [I] String to map
2520 * srclen [I] Length of src in chars, or -1 if src is NUL terminated
2521 * dst [O] Destination for mapped string
2522 * dstlen [I] Length of dst in characters
2524 * RETURNS
2525 * Success: The length of the mapped string in dst, including the NUL terminator.
2526 * Failure: 0. Use GetLastError() to determine the cause.
2528 INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
2529 LPSTR dst, INT dstlen)
2531 WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
2532 LPWSTR srcW, dstW;
2533 INT ret = 0, srclenW, dstlenW;
2534 UINT locale_cp = CP_ACP;
2536 if (!src || !srclen || dstlen < 0)
2538 SetLastError(ERROR_INVALID_PARAMETER);
2539 return 0;
2542 if (!(flags & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
2544 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
2545 if (srclenW)
2546 srcW = bufW;
2547 else
2549 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
2550 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2551 if (!srcW)
2553 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2554 return 0;
2556 MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
2559 if (flags & LCMAP_SORTKEY)
2561 if (src == dst)
2563 SetLastError(ERROR_INVALID_FLAGS);
2564 goto map_string_exit;
2566 ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
2567 if (ret == 0)
2568 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2569 else
2570 ret++;
2571 goto map_string_exit;
2574 if (flags & SORT_STRINGSORT)
2576 SetLastError(ERROR_INVALID_FLAGS);
2577 goto map_string_exit;
2580 dstlenW = LCMapStringW(lcid, flags, srcW, srclenW, NULL, 0);
2581 if (!dstlenW)
2582 goto map_string_exit;
2584 dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
2585 if (!dstW)
2587 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2588 goto map_string_exit;
2591 LCMapStringW(lcid, flags, srcW, srclenW, dstW, dstlenW);
2592 ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
2593 HeapFree(GetProcessHeap(), 0, dstW);
2595 map_string_exit:
2596 if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
2597 return ret;
2600 /*************************************************************************
2601 * FoldStringA (KERNEL32.@)
2603 * Map characters in a string.
2605 * PARAMS
2606 * dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
2607 * src [I] String to map
2608 * srclen [I] Length of src, or -1 if src is NUL terminated
2609 * dst [O] Destination for mapped string
2610 * dstlen [I] Length of dst, or 0 to find the required length for the mapped string
2612 * RETURNS
2613 * Success: The length of the string written to dst, including the terminating NUL. If
2614 * dstlen is 0, the value returned is the same, but nothing is written to dst,
2615 * and dst may be NULL.
2616 * Failure: 0. Use GetLastError() to determine the cause.
2618 INT WINAPI FoldStringA(DWORD dwFlags, LPCSTR src, INT srclen,
2619 LPSTR dst, INT dstlen)
2621 INT ret = 0, srclenW = 0;
2622 WCHAR *srcW = NULL, *dstW = NULL;
2624 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2626 SetLastError(ERROR_INVALID_PARAMETER);
2627 return 0;
2630 srclenW = MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2631 src, srclen, NULL, 0);
2632 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2634 if (!srcW)
2636 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2637 goto FoldStringA_exit;
2640 MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2641 src, srclen, srcW, srclenW);
2643 dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
2645 ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
2646 if (ret && dstlen)
2648 dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
2650 if (!dstW)
2652 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2653 goto FoldStringA_exit;
2656 ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
2657 if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
2659 ret = 0;
2660 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2664 HeapFree(GetProcessHeap(), 0, dstW);
2666 FoldStringA_exit:
2667 HeapFree(GetProcessHeap(), 0, srcW);
2668 return ret;
2671 /*************************************************************************
2672 * FoldStringW (KERNEL32.@)
2674 * See FoldStringA.
2676 INT WINAPI FoldStringW(DWORD dwFlags, LPCWSTR src, INT srclen,
2677 LPWSTR dst, INT dstlen)
2679 int ret;
2681 switch (dwFlags & (MAP_COMPOSITE|MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES))
2683 case 0:
2684 if (dwFlags)
2685 break;
2686 /* Fall through for dwFlags == 0 */
2687 case MAP_PRECOMPOSED|MAP_COMPOSITE:
2688 case MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES:
2689 case MAP_COMPOSITE|MAP_EXPAND_LIGATURES:
2690 SetLastError(ERROR_INVALID_FLAGS);
2691 return 0;
2694 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2696 SetLastError(ERROR_INVALID_PARAMETER);
2697 return 0;
2700 ret = wine_fold_string(dwFlags, src, srclen, dst, dstlen);
2701 if (!ret)
2702 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2703 return ret;
2706 /******************************************************************************
2707 * CompareStringW (KERNEL32.@)
2709 * See CompareStringA.
2711 INT WINAPI CompareStringW(LCID lcid, DWORD style,
2712 LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
2714 INT ret;
2716 if (!str1 || !str2)
2718 SetLastError(ERROR_INVALID_PARAMETER);
2719 return 0;
2722 if( style & ~(NORM_IGNORECASE|NORM_IGNORENONSPACE|NORM_IGNORESYMBOLS|
2723 SORT_STRINGSORT|NORM_IGNOREKANATYPE|NORM_IGNOREWIDTH|LOCALE_USE_CP_ACP|0x10000000) )
2725 SetLastError(ERROR_INVALID_FLAGS);
2726 return 0;
2729 /* this style is related to diacritics in Arabic, Japanese, and Hebrew */
2730 if (style & 0x10000000)
2731 WARN("Ignoring unknown style 0x10000000\n");
2733 if (len1 < 0) len1 = strlenW(str1);
2734 if (len2 < 0) len2 = strlenW(str2);
2736 ret = wine_compare_string(style, str1, len1, str2, len2);
2738 if (ret) /* need to translate result */
2739 return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
2740 return CSTR_EQUAL;
2743 /******************************************************************************
2744 * CompareStringA (KERNEL32.@)
2746 * Compare two locale sensitive strings.
2748 * PARAMS
2749 * lcid [I] LCID for the comparison
2750 * style [I] Flags for the comparison (NORM_ constants from "winnls.h").
2751 * str1 [I] First string to compare
2752 * len1 [I] Length of str1, or -1 if str1 is NUL terminated
2753 * str2 [I] Second string to compare
2754 * len2 [I] Length of str2, or -1 if str2 is NUL terminated
2756 * RETURNS
2757 * Success: CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN depending on whether
2758 * str2 is less than, equal to or greater than str1 respectively.
2759 * Failure: FALSE. Use GetLastError() to determine the cause.
2761 INT WINAPI CompareStringA(LCID lcid, DWORD style,
2762 LPCSTR str1, INT len1, LPCSTR str2, INT len2)
2764 WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
2765 WCHAR *buf2W = buf1W + 130;
2766 LPWSTR str1W, str2W;
2767 INT len1W, len2W, ret;
2768 UINT locale_cp = CP_ACP;
2770 if (!str1 || !str2)
2772 SetLastError(ERROR_INVALID_PARAMETER);
2773 return 0;
2775 if (len1 < 0) len1 = strlen(str1);
2776 if (len2 < 0) len2 = strlen(str2);
2778 if (!(style & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
2780 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 130);
2781 if (len1W)
2782 str1W = buf1W;
2783 else
2785 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
2786 str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
2787 if (!str1W)
2789 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2790 return 0;
2792 MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
2794 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 130);
2795 if (len2W)
2796 str2W = buf2W;
2797 else
2799 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
2800 str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
2801 if (!str2W)
2803 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2804 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2805 return 0;
2807 MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
2810 ret = CompareStringW(lcid, style, str1W, len1W, str2W, len2W);
2812 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2813 if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
2814 return ret;
2817 /*************************************************************************
2818 * lstrcmp (KERNEL32.@)
2819 * lstrcmpA (KERNEL32.@)
2821 * Compare two strings using the current thread locale.
2823 * PARAMS
2824 * str1 [I] First string to compare
2825 * str2 [I] Second string to compare
2827 * RETURNS
2828 * Success: A number less than, equal to or greater than 0 depending on whether
2829 * str2 is less than, equal to or greater than str1 respectively.
2830 * Failure: FALSE. Use GetLastError() to determine the cause.
2832 int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
2834 int ret;
2836 if ((str1 == NULL) && (str2 == NULL)) return 0;
2837 if (str1 == NULL) return -1;
2838 if (str2 == NULL) return 1;
2840 ret = CompareStringA(GetThreadLocale(), LOCALE_USE_CP_ACP, str1, -1, str2, -1);
2841 if (ret) ret -= 2;
2843 return ret;
2846 /*************************************************************************
2847 * lstrcmpi (KERNEL32.@)
2848 * lstrcmpiA (KERNEL32.@)
2850 * Compare two strings using the current thread locale, ignoring case.
2852 * PARAMS
2853 * str1 [I] First string to compare
2854 * str2 [I] Second string to compare
2856 * RETURNS
2857 * Success: A number less than, equal to or greater than 0 depending on whether
2858 * str2 is less than, equal to or greater than str1 respectively.
2859 * Failure: FALSE. Use GetLastError() to determine the cause.
2861 int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
2863 int ret;
2865 if ((str1 == NULL) && (str2 == NULL)) return 0;
2866 if (str1 == NULL) return -1;
2867 if (str2 == NULL) return 1;
2869 ret = CompareStringA(GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, str1, -1, str2, -1);
2870 if (ret) ret -= 2;
2872 return ret;
2875 /*************************************************************************
2876 * lstrcmpW (KERNEL32.@)
2878 * See lstrcmpA.
2880 int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
2882 int ret;
2884 if ((str1 == NULL) && (str2 == NULL)) return 0;
2885 if (str1 == NULL) return -1;
2886 if (str2 == NULL) return 1;
2888 ret = CompareStringW(GetThreadLocale(), 0, str1, -1, str2, -1);
2889 if (ret) ret -= 2;
2891 return ret;
2894 /*************************************************************************
2895 * lstrcmpiW (KERNEL32.@)
2897 * See lstrcmpiA.
2899 int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
2901 int ret;
2903 if ((str1 == NULL) && (str2 == NULL)) return 0;
2904 if (str1 == NULL) return -1;
2905 if (str2 == NULL) return 1;
2907 ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2908 if (ret) ret -= 2;
2910 return ret;
2913 /******************************************************************************
2914 * LOCALE_Init
2916 void LOCALE_Init(void)
2918 extern void CDECL __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp,
2919 const union cptable *unix_cp );
2921 UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp;
2923 #ifdef __APPLE__
2924 /* MacOS doesn't set the locale environment variables so we have to do it ourselves */
2925 CFArrayRef preferred_locales, all_locales;
2926 CFStringRef user_language_string_ref = NULL;
2927 char user_locale[50];
2928 char* p;
2930 CFLocaleRef user_locale_ref = CFLocaleCopyCurrent();
2931 CFStringRef user_locale_string_ref = CFLocaleGetIdentifier( user_locale_ref );
2933 CFStringGetCString( user_locale_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 );
2934 CFRelease( user_locale_ref );
2935 /* Strip modifiers because setlocale() can't parse them. */
2936 if ((p = strchr( user_locale, '@' ))) *p = 0;
2937 if (!strchr( user_locale, '.' )) strcat( user_locale, ".UTF-8" );
2938 unix_cp = CP_UTF8; /* default to utf-8 even if we don't get a valid locale */
2939 setenv( "LANG", user_locale, 0 );
2940 TRACE( "setting locale to '%s'\n", user_locale );
2942 /* We still want to set the retrieve the preferred language as chosen in
2943 System Preferences.app, because it can differ from CFLocaleCopyCurrent().
2945 all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
2946 preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );
2947 if (preferred_locales && CFArrayGetCount( preferred_locales ))
2948 user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
2949 CFRelease( all_locales );
2950 #endif /* __APPLE__ */
2952 setlocale( LC_ALL, "" );
2954 unix_cp = setup_unix_locales();
2955 if (!lcid_LC_MESSAGES) lcid_LC_MESSAGES = lcid_LC_CTYPE;
2957 #ifdef __APPLE__
2958 /* Override lcid_LC_MESSAGES with user_language if LC_MESSAGES is set to default */
2959 if (lcid_LC_MESSAGES == lcid_LC_CTYPE && user_language_string_ref)
2961 struct locale_name locale_name;
2962 WCHAR buffer[128];
2963 CFStringGetCString( user_language_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 );
2964 strcpynAtoW( buffer, user_locale, sizeof(buffer)/sizeof(WCHAR) );
2965 parse_locale_name( buffer, &locale_name );
2966 lcid_LC_MESSAGES = locale_name.lcid;
2967 TRACE( "setting lcid_LC_MESSAGES to '%s'\n", user_locale );
2969 if (preferred_locales)
2970 CFRelease( preferred_locales );
2971 #endif
2973 NtSetDefaultUILanguage( LANGIDFROMLCID(lcid_LC_MESSAGES) );
2974 NtSetDefaultLocale( TRUE, lcid_LC_MESSAGES );
2975 NtSetDefaultLocale( FALSE, lcid_LC_CTYPE );
2977 ansi_cp = get_lcid_codepage( LOCALE_USER_DEFAULT );
2978 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
2979 (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
2980 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
2981 (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
2982 if (!unix_cp)
2983 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
2984 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) );
2986 if (!(ansi_cptable = wine_cp_get_table( ansi_cp )))
2987 ansi_cptable = wine_cp_get_table( 1252 );
2988 if (!(oem_cptable = wine_cp_get_table( oem_cp )))
2989 oem_cptable = wine_cp_get_table( 437 );
2990 if (!(mac_cptable = wine_cp_get_table( mac_cp )))
2991 mac_cptable = wine_cp_get_table( 10000 );
2992 if (unix_cp != CP_UTF8)
2994 if (!(unix_cptable = wine_cp_get_table( unix_cp )))
2995 unix_cptable = wine_cp_get_table( 28591 );
2998 __wine_init_codepages( ansi_cptable, oem_cptable, unix_cptable );
3000 TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
3001 ansi_cptable->info.codepage, oem_cptable->info.codepage,
3002 mac_cptable->info.codepage, unix_cp );
3004 setlocale(LC_NUMERIC, "C"); /* FIXME: oleaut32 depends on this */
3007 static HANDLE NLS_RegOpenKey(HANDLE hRootKey, LPCWSTR szKeyName)
3009 UNICODE_STRING keyName;
3010 OBJECT_ATTRIBUTES attr;
3011 HANDLE hkey;
3013 RtlInitUnicodeString( &keyName, szKeyName );
3014 InitializeObjectAttributes(&attr, &keyName, 0, hRootKey, NULL);
3016 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS)
3017 hkey = 0;
3019 return hkey;
3022 static BOOL NLS_RegEnumSubKey(HANDLE hKey, UINT ulIndex, LPWSTR szKeyName,
3023 ULONG keyNameSize)
3025 BYTE buffer[80];
3026 KEY_BASIC_INFORMATION *info = (KEY_BASIC_INFORMATION *)buffer;
3027 DWORD dwLen;
3029 if (NtEnumerateKey( hKey, ulIndex, KeyBasicInformation, buffer,
3030 sizeof(buffer), &dwLen) != STATUS_SUCCESS ||
3031 info->NameLength > keyNameSize)
3033 return FALSE;
3036 TRACE("info->Name %s info->NameLength %d\n", debugstr_w(info->Name), info->NameLength);
3038 memcpy( szKeyName, info->Name, info->NameLength);
3039 szKeyName[info->NameLength / sizeof(WCHAR)] = '\0';
3041 TRACE("returning %s\n", debugstr_w(szKeyName));
3042 return TRUE;
3045 static BOOL NLS_RegEnumValue(HANDLE hKey, UINT ulIndex,
3046 LPWSTR szValueName, ULONG valueNameSize,
3047 LPWSTR szValueData, ULONG valueDataSize)
3049 BYTE buffer[80];
3050 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
3051 DWORD dwLen;
3053 if (NtEnumerateValueKey( hKey, ulIndex, KeyValueFullInformation,
3054 buffer, sizeof(buffer), &dwLen ) != STATUS_SUCCESS ||
3055 info->NameLength > valueNameSize ||
3056 info->DataLength > valueDataSize)
3058 return FALSE;
3061 TRACE("info->Name %s info->DataLength %d\n", debugstr_w(info->Name), info->DataLength);
3063 memcpy( szValueName, info->Name, info->NameLength);
3064 szValueName[info->NameLength / sizeof(WCHAR)] = '\0';
3065 memcpy( szValueData, buffer + info->DataOffset, info->DataLength );
3066 szValueData[info->DataLength / sizeof(WCHAR)] = '\0';
3068 TRACE("returning %s %s\n", debugstr_w(szValueName), debugstr_w(szValueData));
3069 return TRUE;
3072 static BOOL NLS_RegGetDword(HANDLE hKey, LPCWSTR szValueName, DWORD *lpVal)
3074 BYTE buffer[128];
3075 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
3076 DWORD dwSize = sizeof(buffer);
3077 UNICODE_STRING valueName;
3079 RtlInitUnicodeString( &valueName, szValueName );
3081 TRACE("%p, %s\n", hKey, debugstr_w(szValueName));
3082 if (NtQueryValueKey( hKey, &valueName, KeyValuePartialInformation,
3083 buffer, dwSize, &dwSize ) == STATUS_SUCCESS &&
3084 info->DataLength == sizeof(DWORD))
3086 memcpy(lpVal, info->Data, sizeof(DWORD));
3087 return TRUE;
3090 return FALSE;
3093 static BOOL NLS_GetLanguageGroupName(LGRPID lgrpid, LPWSTR szName, ULONG nameSize)
3095 LANGID langId;
3096 LPCWSTR szResourceName = MAKEINTRESOURCEW(((lgrpid + 0x2000) >> 4) + 1);
3097 HRSRC hResource;
3098 BOOL bRet = FALSE;
3100 /* FIXME: Is it correct to use the system default langid? */
3101 langId = GetSystemDefaultLangID();
3103 if (SUBLANGID(langId) == SUBLANG_NEUTRAL)
3104 langId = MAKELANGID( PRIMARYLANGID(langId), SUBLANG_DEFAULT );
3106 hResource = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING, szResourceName, langId );
3108 if (hResource)
3110 HGLOBAL hResDir = LoadResource( kernel32_handle, hResource );
3112 if (hResDir)
3114 ULONG iResourceIndex = lgrpid & 0xf;
3115 LPCWSTR lpResEntry = LockResource( hResDir );
3116 ULONG i;
3118 for (i = 0; i < iResourceIndex; i++)
3119 lpResEntry += *lpResEntry + 1;
3121 if (*lpResEntry < nameSize)
3123 memcpy( szName, lpResEntry + 1, *lpResEntry * sizeof(WCHAR) );
3124 szName[*lpResEntry] = '\0';
3125 bRet = TRUE;
3129 FreeResource( hResource );
3131 return bRet;
3134 /* Registry keys for NLS related information */
3136 static const WCHAR szCountryListName[] = {
3137 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
3138 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
3139 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3140 'T','e','l','e','p','h','o','n','y','\\',
3141 'C','o','u','n','t','r','y',' ','L','i','s','t','\0'
3145 /* Callback function ptrs for EnumSystemLanguageGroupsA/W */
3146 typedef struct
3148 LANGUAGEGROUP_ENUMPROCA procA;
3149 LANGUAGEGROUP_ENUMPROCW procW;
3150 DWORD dwFlags;
3151 LONG_PTR lParam;
3152 } ENUMLANGUAGEGROUP_CALLBACKS;
3154 /* Internal implementation of EnumSystemLanguageGroupsA/W */
3155 static BOOL NLS_EnumSystemLanguageGroups(ENUMLANGUAGEGROUP_CALLBACKS *lpProcs)
3157 WCHAR szNumber[10], szValue[4];
3158 HANDLE hKey;
3159 BOOL bContinue = TRUE;
3160 ULONG ulIndex = 0;
3162 if (!lpProcs)
3164 SetLastError(ERROR_INVALID_PARAMETER);
3165 return FALSE;
3168 switch (lpProcs->dwFlags)
3170 case 0:
3171 /* Default to LGRPID_INSTALLED */
3172 lpProcs->dwFlags = LGRPID_INSTALLED;
3173 /* Fall through... */
3174 case LGRPID_INSTALLED:
3175 case LGRPID_SUPPORTED:
3176 break;
3177 default:
3178 SetLastError(ERROR_INVALID_FLAGS);
3179 return FALSE;
3182 hKey = NLS_RegOpenKey( 0, szLangGroupsKeyName );
3184 if (!hKey)
3185 FIXME("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
3187 while (bContinue)
3189 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
3190 szValue, sizeof(szValue) ))
3192 BOOL bInstalled = szValue[0] == '1' ? TRUE : FALSE;
3193 LGRPID lgrpid = strtoulW( szNumber, NULL, 16 );
3195 TRACE("grpid %s (%sinstalled)\n", debugstr_w(szNumber),
3196 bInstalled ? "" : "not ");
3198 if (lpProcs->dwFlags == LGRPID_SUPPORTED || bInstalled)
3200 WCHAR szGrpName[48];
3202 if (!NLS_GetLanguageGroupName( lgrpid, szGrpName, sizeof(szGrpName) / sizeof(WCHAR) ))
3203 szGrpName[0] = '\0';
3205 if (lpProcs->procW)
3206 bContinue = lpProcs->procW( lgrpid, szNumber, szGrpName, lpProcs->dwFlags,
3207 lpProcs->lParam );
3208 else
3210 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
3211 char szGrpNameA[48];
3213 /* FIXME: MSDN doesn't say which code page the W->A translation uses,
3214 * or whether the language names are ever localised. Assume CP_ACP.
3217 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
3218 WideCharToMultiByte(CP_ACP, 0, szGrpName, -1, szGrpNameA, sizeof(szGrpNameA), 0, 0);
3220 bContinue = lpProcs->procA( lgrpid, szNumberA, szGrpNameA, lpProcs->dwFlags,
3221 lpProcs->lParam );
3225 ulIndex++;
3227 else
3228 bContinue = FALSE;
3230 if (!bContinue)
3231 break;
3234 if (hKey)
3235 NtClose( hKey );
3237 return TRUE;
3240 /******************************************************************************
3241 * EnumSystemLanguageGroupsA (KERNEL32.@)
3243 * Call a users function for each language group available on the system.
3245 * PARAMS
3246 * pLangGrpEnumProc [I] Callback function to call for each language group
3247 * dwFlags [I] LGRPID_SUPPORTED=All Supported, LGRPID_INSTALLED=Installed only
3248 * lParam [I] User parameter to pass to pLangGrpEnumProc
3250 * RETURNS
3251 * Success: TRUE.
3252 * Failure: FALSE. Use GetLastError() to determine the cause.
3254 BOOL WINAPI EnumSystemLanguageGroupsA(LANGUAGEGROUP_ENUMPROCA pLangGrpEnumProc,
3255 DWORD dwFlags, LONG_PTR lParam)
3257 ENUMLANGUAGEGROUP_CALLBACKS procs;
3259 TRACE("(%p,0x%08X,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
3261 procs.procA = pLangGrpEnumProc;
3262 procs.procW = NULL;
3263 procs.dwFlags = dwFlags;
3264 procs.lParam = lParam;
3266 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
3269 /******************************************************************************
3270 * EnumSystemLanguageGroupsW (KERNEL32.@)
3272 * See EnumSystemLanguageGroupsA.
3274 BOOL WINAPI EnumSystemLanguageGroupsW(LANGUAGEGROUP_ENUMPROCW pLangGrpEnumProc,
3275 DWORD dwFlags, LONG_PTR lParam)
3277 ENUMLANGUAGEGROUP_CALLBACKS procs;
3279 TRACE("(%p,0x%08X,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
3281 procs.procA = NULL;
3282 procs.procW = pLangGrpEnumProc;
3283 procs.dwFlags = dwFlags;
3284 procs.lParam = lParam;
3286 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
3289 /******************************************************************************
3290 * IsValidLanguageGroup (KERNEL32.@)
3292 * Determine if a language group is supported and/or installed.
3294 * PARAMS
3295 * lgrpid [I] Language Group Id (LGRPID_ values from "winnls.h")
3296 * dwFlags [I] LGRPID_SUPPORTED=Supported, LGRPID_INSTALLED=Installed
3298 * RETURNS
3299 * TRUE, if lgrpid is supported and/or installed, according to dwFlags.
3300 * FALSE otherwise.
3302 BOOL WINAPI IsValidLanguageGroup(LGRPID lgrpid, DWORD dwFlags)
3304 static const WCHAR szFormat[] = { '%','x','\0' };
3305 WCHAR szValueName[16], szValue[2];
3306 BOOL bSupported = FALSE, bInstalled = FALSE;
3307 HANDLE hKey;
3310 switch (dwFlags)
3312 case LGRPID_INSTALLED:
3313 case LGRPID_SUPPORTED:
3315 hKey = NLS_RegOpenKey( 0, szLangGroupsKeyName );
3317 sprintfW( szValueName, szFormat, lgrpid );
3319 if (NLS_RegGetDword( hKey, szValueName, (LPDWORD)szValue ))
3321 bSupported = TRUE;
3323 if (szValue[0] == '1')
3324 bInstalled = TRUE;
3327 if (hKey)
3328 NtClose( hKey );
3330 break;
3333 if ((dwFlags == LGRPID_SUPPORTED && bSupported) ||
3334 (dwFlags == LGRPID_INSTALLED && bInstalled))
3335 return TRUE;
3337 return FALSE;
3340 /* Callback function ptrs for EnumLanguageGrouplocalesA/W */
3341 typedef struct
3343 LANGGROUPLOCALE_ENUMPROCA procA;
3344 LANGGROUPLOCALE_ENUMPROCW procW;
3345 DWORD dwFlags;
3346 LGRPID lgrpid;
3347 LONG_PTR lParam;
3348 } ENUMLANGUAGEGROUPLOCALE_CALLBACKS;
3350 /* Internal implementation of EnumLanguageGrouplocalesA/W */
3351 static BOOL NLS_EnumLanguageGroupLocales(ENUMLANGUAGEGROUPLOCALE_CALLBACKS *lpProcs)
3353 static const WCHAR szAlternateSortsKeyName[] = {
3354 'A','l','t','e','r','n','a','t','e',' ','S','o','r','t','s','\0'
3356 WCHAR szNumber[10], szValue[4];
3357 HANDLE hKey;
3358 BOOL bContinue = TRUE, bAlternate = FALSE;
3359 LGRPID lgrpid;
3360 ULONG ulIndex = 1; /* Ignore default entry of 1st key */
3362 if (!lpProcs || !lpProcs->lgrpid || lpProcs->lgrpid > LGRPID_ARMENIAN)
3364 SetLastError(ERROR_INVALID_PARAMETER);
3365 return FALSE;
3368 if (lpProcs->dwFlags)
3370 SetLastError(ERROR_INVALID_FLAGS);
3371 return FALSE;
3374 hKey = NLS_RegOpenKey( 0, szLocaleKeyName );
3376 if (!hKey)
3377 WARN("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
3379 while (bContinue)
3381 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
3382 szValue, sizeof(szValue) ))
3384 lgrpid = strtoulW( szValue, NULL, 16 );
3386 TRACE("lcid %s, grpid %d (%smatched)\n", debugstr_w(szNumber),
3387 lgrpid, lgrpid == lpProcs->lgrpid ? "" : "not ");
3389 if (lgrpid == lpProcs->lgrpid)
3391 LCID lcid;
3393 lcid = strtoulW( szNumber, NULL, 16 );
3395 /* FIXME: native returns extra text for a few (17/150) locales, e.g:
3396 * '00000437 ;Georgian'
3397 * At present we only pass the LCID string.
3400 if (lpProcs->procW)
3401 bContinue = lpProcs->procW( lgrpid, lcid, szNumber, lpProcs->lParam );
3402 else
3404 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
3406 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
3408 bContinue = lpProcs->procA( lgrpid, lcid, szNumberA, lpProcs->lParam );
3412 ulIndex++;
3414 else
3416 /* Finished enumerating this key */
3417 if (!bAlternate)
3419 /* Enumerate alternate sorts also */
3420 hKey = NLS_RegOpenKey( hKey, szAlternateSortsKeyName );
3421 bAlternate = TRUE;
3422 ulIndex = 0;
3424 else
3425 bContinue = FALSE; /* Finished both keys */
3428 if (!bContinue)
3429 break;
3432 if (hKey)
3433 NtClose( hKey );
3435 return TRUE;
3438 /******************************************************************************
3439 * EnumLanguageGroupLocalesA (KERNEL32.@)
3441 * Call a users function for every locale in a language group available on the system.
3443 * PARAMS
3444 * pLangGrpLcEnumProc [I] Callback function to call for each locale
3445 * lgrpid [I] Language group (LGRPID_ values from "winnls.h")
3446 * dwFlags [I] Reserved, set to 0
3447 * lParam [I] User parameter to pass to pLangGrpLcEnumProc
3449 * RETURNS
3450 * Success: TRUE.
3451 * Failure: FALSE. Use GetLastError() to determine the cause.
3453 BOOL WINAPI EnumLanguageGroupLocalesA(LANGGROUPLOCALE_ENUMPROCA pLangGrpLcEnumProc,
3454 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
3456 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
3458 TRACE("(%p,0x%08X,0x%08X,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
3460 callbacks.procA = pLangGrpLcEnumProc;
3461 callbacks.procW = NULL;
3462 callbacks.dwFlags = dwFlags;
3463 callbacks.lgrpid = lgrpid;
3464 callbacks.lParam = lParam;
3466 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
3469 /******************************************************************************
3470 * EnumLanguageGroupLocalesW (KERNEL32.@)
3472 * See EnumLanguageGroupLocalesA.
3474 BOOL WINAPI EnumLanguageGroupLocalesW(LANGGROUPLOCALE_ENUMPROCW pLangGrpLcEnumProc,
3475 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
3477 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
3479 TRACE("(%p,0x%08X,0x%08X,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
3481 callbacks.procA = NULL;
3482 callbacks.procW = pLangGrpLcEnumProc;
3483 callbacks.dwFlags = dwFlags;
3484 callbacks.lgrpid = lgrpid;
3485 callbacks.lParam = lParam;
3487 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
3490 /******************************************************************************
3491 * EnumSystemGeoID (KERNEL32.@)
3493 * Call a users function for every location available on the system.
3495 * PARAMS
3496 * geoclass [I] Type of information desired (SYSGEOTYPE enum from "winnls.h")
3497 * reserved [I] Reserved, set to 0
3498 * pGeoEnumProc [I] Callback function to call for each location
3500 * RETURNS
3501 * Success: TRUE.
3502 * Failure: FALSE. Use GetLastError() to determine the cause.
3504 BOOL WINAPI EnumSystemGeoID(GEOCLASS geoclass, GEOID reserved, GEO_ENUMPROC pGeoEnumProc)
3506 static const WCHAR szCountryCodeValueName[] = {
3507 'C','o','u','n','t','r','y','C','o','d','e','\0'
3509 WCHAR szNumber[10];
3510 HANDLE hKey;
3511 ULONG ulIndex = 0;
3513 TRACE("(0x%08X,0x%08X,%p)\n", geoclass, reserved, pGeoEnumProc);
3515 if (geoclass != GEOCLASS_NATION || reserved || !pGeoEnumProc)
3517 SetLastError(ERROR_INVALID_PARAMETER);
3518 return FALSE;
3521 hKey = NLS_RegOpenKey( 0, szCountryListName );
3523 while (NLS_RegEnumSubKey( hKey, ulIndex, szNumber, sizeof(szNumber) ))
3525 BOOL bContinue = TRUE;
3526 DWORD dwGeoId;
3527 HANDLE hSubKey = NLS_RegOpenKey( hKey, szNumber );
3529 if (hSubKey)
3531 if (NLS_RegGetDword( hSubKey, szCountryCodeValueName, &dwGeoId ))
3533 TRACE("Got geoid %d\n", dwGeoId);
3535 if (!pGeoEnumProc( dwGeoId ))
3536 bContinue = FALSE;
3539 NtClose( hSubKey );
3542 if (!bContinue)
3543 break;
3545 ulIndex++;
3548 if (hKey)
3549 NtClose( hKey );
3551 return TRUE;
3554 /******************************************************************************
3555 * InvalidateNLSCache (KERNEL32.@)
3557 * Invalidate the cache of NLS values.
3559 * PARAMS
3560 * None.
3562 * RETURNS
3563 * Success: TRUE.
3564 * Failure: FALSE.
3566 BOOL WINAPI InvalidateNLSCache(void)
3568 FIXME("() stub\n");
3569 return FALSE;
3572 /******************************************************************************
3573 * GetUserGeoID (KERNEL32.@)
3575 GEOID WINAPI GetUserGeoID( GEOCLASS GeoClass )
3577 GEOID ret = GEOID_NOT_AVAILABLE;
3578 static const WCHAR geoW[] = {'G','e','o',0};
3579 static const WCHAR nationW[] = {'N','a','t','i','o','n',0};
3580 WCHAR bufferW[40], *end;
3581 DWORD count;
3582 HANDLE hkey, hSubkey = 0;
3583 UNICODE_STRING keyW;
3584 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
3585 RtlInitUnicodeString( &keyW, nationW );
3586 count = sizeof(bufferW);
3588 if(!(hkey = create_registry_key())) return ret;
3590 switch( GeoClass ){
3591 case GEOCLASS_NATION:
3592 if ((hSubkey = NLS_RegOpenKey(hkey, geoW)))
3594 if((NtQueryValueKey(hSubkey, &keyW, KeyValuePartialInformation,
3595 bufferW, count, &count) == STATUS_SUCCESS ) && info->DataLength)
3596 ret = strtolW((LPCWSTR)info->Data, &end, 10);
3598 break;
3599 case GEOCLASS_REGION:
3600 FIXME("GEOCLASS_REGION not handled yet\n");
3601 break;
3604 NtClose(hkey);
3605 if (hSubkey) NtClose(hSubkey);
3606 return ret;
3609 /******************************************************************************
3610 * SetUserGeoID (KERNEL32.@)
3612 BOOL WINAPI SetUserGeoID( GEOID GeoID )
3614 static const WCHAR geoW[] = {'G','e','o',0};
3615 static const WCHAR nationW[] = {'N','a','t','i','o','n',0};
3616 static const WCHAR formatW[] = {'%','i',0};
3617 UNICODE_STRING nameW,keyW;
3618 WCHAR bufferW[10];
3619 OBJECT_ATTRIBUTES attr;
3620 HANDLE hkey;
3622 if(!(hkey = create_registry_key())) return FALSE;
3624 attr.Length = sizeof(attr);
3625 attr.RootDirectory = hkey;
3626 attr.ObjectName = &nameW;
3627 attr.Attributes = 0;
3628 attr.SecurityDescriptor = NULL;
3629 attr.SecurityQualityOfService = NULL;
3630 RtlInitUnicodeString( &nameW, geoW );
3631 RtlInitUnicodeString( &keyW, nationW );
3633 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS)
3636 NtClose(attr.RootDirectory);
3637 return FALSE;
3640 sprintfW(bufferW, formatW, GeoID);
3641 NtSetValueKey(hkey, &keyW, 0, REG_SZ, bufferW, (strlenW(bufferW) + 1) * sizeof(WCHAR));
3642 NtClose(attr.RootDirectory);
3643 NtClose(hkey);
3644 return TRUE;
3647 typedef struct
3649 union
3651 UILANGUAGE_ENUMPROCA procA;
3652 UILANGUAGE_ENUMPROCW procW;
3653 } u;
3654 DWORD flags;
3655 LONG_PTR param;
3656 } ENUM_UILANG_CALLBACK;
3658 static BOOL CALLBACK enum_uilang_proc_a( HMODULE hModule, LPCSTR type,
3659 LPCSTR name, WORD LangID, LONG_PTR lParam )
3661 ENUM_UILANG_CALLBACK *enum_uilang = (ENUM_UILANG_CALLBACK *)lParam;
3662 char buf[20];
3664 sprintf(buf, "%08x", (UINT)LangID);
3665 return enum_uilang->u.procA( buf, enum_uilang->param );
3668 static BOOL CALLBACK enum_uilang_proc_w( HMODULE hModule, LPCWSTR type,
3669 LPCWSTR name, WORD LangID, LONG_PTR lParam )
3671 static const WCHAR formatW[] = {'%','0','8','x',0};
3672 ENUM_UILANG_CALLBACK *enum_uilang = (ENUM_UILANG_CALLBACK *)lParam;
3673 WCHAR buf[20];
3675 sprintfW( buf, formatW, (UINT)LangID );
3676 return enum_uilang->u.procW( buf, enum_uilang->param );
3679 /******************************************************************************
3680 * EnumUILanguagesA (KERNEL32.@)
3682 BOOL WINAPI EnumUILanguagesA(UILANGUAGE_ENUMPROCA pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
3684 ENUM_UILANG_CALLBACK enum_uilang;
3686 TRACE("%p, %x, %lx\n", pUILangEnumProc, dwFlags, lParam);
3688 if(!pUILangEnumProc) {
3689 SetLastError(ERROR_INVALID_PARAMETER);
3690 return FALSE;
3692 if(dwFlags) {
3693 SetLastError(ERROR_INVALID_FLAGS);
3694 return FALSE;
3697 enum_uilang.u.procA = pUILangEnumProc;
3698 enum_uilang.flags = dwFlags;
3699 enum_uilang.param = lParam;
3701 EnumResourceLanguagesA( kernel32_handle, (LPCSTR)RT_STRING,
3702 (LPCSTR)LOCALE_ILANGUAGE, enum_uilang_proc_a,
3703 (LONG_PTR)&enum_uilang);
3704 return TRUE;
3707 /******************************************************************************
3708 * EnumUILanguagesW (KERNEL32.@)
3710 BOOL WINAPI EnumUILanguagesW(UILANGUAGE_ENUMPROCW pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
3712 ENUM_UILANG_CALLBACK enum_uilang;
3714 TRACE("%p, %x, %lx\n", pUILangEnumProc, dwFlags, lParam);
3717 if(!pUILangEnumProc) {
3718 SetLastError(ERROR_INVALID_PARAMETER);
3719 return FALSE;
3721 if(dwFlags) {
3722 SetLastError(ERROR_INVALID_FLAGS);
3723 return FALSE;
3726 enum_uilang.u.procW = pUILangEnumProc;
3727 enum_uilang.flags = dwFlags;
3728 enum_uilang.param = lParam;
3730 EnumResourceLanguagesW( kernel32_handle, (LPCWSTR)RT_STRING,
3731 (LPCWSTR)LOCALE_ILANGUAGE, enum_uilang_proc_w,
3732 (LONG_PTR)&enum_uilang);
3733 return TRUE;
3736 INT WINAPI GetGeoInfoW(GEOID GeoId, GEOTYPE GeoType, LPWSTR lpGeoData,
3737 int cchData, LANGID language)
3739 FIXME("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, language);
3740 return 0;
3743 INT WINAPI GetGeoInfoA(GEOID GeoId, GEOTYPE GeoType, LPSTR lpGeoData,
3744 int cchData, LANGID language)
3746 FIXME("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, language);
3747 return 0;