wiaservc: Add the class factory and IWiaDevMgr stubs.
[wine/hramrach.git] / dlls / kernel32 / locale.c
blobfb9b517362059d29715cf3c8c199e6c6e87af58a
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 const WCHAR szNlsKeyName[] = {
66 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
67 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
68 'C','o','n','t','r','o','l','\\','N','l','s','\0'
71 static const WCHAR szLocaleKeyName[] = {
72 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
73 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
74 'C','o','n','t','r','o','l','\\','N','l','s','\\','L','o','c','a','l','e',0
77 static const WCHAR szCodepageKeyName[] = {
78 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
79 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
80 'C','o','n','t','r','o','l','\\','N','l','s','\\','C','o','d','e','p','a','g','e',0
83 static const WCHAR szLangGroupsKeyName[] = {
84 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
85 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
86 'C','o','n','t','r','o','l','\\','N','l','s','\\',
87 'L','a','n','g','u','a','g','e',' ','G','r','o','u','p','s',0
90 /* Charset to codepage map, sorted by name. */
91 static const struct charset_entry
93 const char *charset_name;
94 UINT codepage;
95 } charset_names[] =
97 { "BIG5", 950 },
98 { "CP1250", 1250 },
99 { "CP1251", 1251 },
100 { "CP1252", 1252 },
101 { "CP1253", 1253 },
102 { "CP1254", 1254 },
103 { "CP1255", 1255 },
104 { "CP1256", 1256 },
105 { "CP1257", 1257 },
106 { "CP1258", 1258 },
107 { "CP932", 932 },
108 { "CP936", 936 },
109 { "CP949", 949 },
110 { "CP950", 950 },
111 { "EUCJP", 20932 },
112 { "GB2312", 936 },
113 { "IBM037", 37 },
114 { "IBM1026", 1026 },
115 { "IBM424", 424 },
116 { "IBM437", 437 },
117 { "IBM500", 500 },
118 { "IBM850", 850 },
119 { "IBM852", 852 },
120 { "IBM855", 855 },
121 { "IBM857", 857 },
122 { "IBM860", 860 },
123 { "IBM861", 861 },
124 { "IBM862", 862 },
125 { "IBM863", 863 },
126 { "IBM864", 864 },
127 { "IBM865", 865 },
128 { "IBM866", 866 },
129 { "IBM869", 869 },
130 { "IBM874", 874 },
131 { "IBM875", 875 },
132 { "ISO88591", 28591 },
133 { "ISO885910", 28600 },
134 { "ISO885913", 28603 },
135 { "ISO885914", 28604 },
136 { "ISO885915", 28605 },
137 { "ISO885916", 28606 },
138 { "ISO88592", 28592 },
139 { "ISO88593", 28593 },
140 { "ISO88594", 28594 },
141 { "ISO88595", 28595 },
142 { "ISO88596", 28596 },
143 { "ISO88597", 28597 },
144 { "ISO88598", 28598 },
145 { "ISO88599", 28599 },
146 { "KOI8R", 20866 },
147 { "KOI8U", 21866 },
148 { "UTF8", CP_UTF8 }
152 struct locale_name
154 WCHAR win_name[128]; /* Windows name ("en-US") */
155 WCHAR lang[128]; /* language ("en") (note: buffer contains the other strings too) */
156 WCHAR *country; /* country ("US") */
157 WCHAR *charset; /* charset ("UTF-8") for Unix format only */
158 WCHAR *script; /* script ("Latn") for Windows format only */
159 WCHAR *modifier; /* modifier or sort order */
160 LCID lcid; /* corresponding LCID */
161 int matches; /* number of elements matching LCID (0..4) */
162 UINT codepage; /* codepage corresponding to charset */
165 /* locale ids corresponding to the various Unix locale parameters */
166 static LCID lcid_LC_COLLATE;
167 static LCID lcid_LC_CTYPE;
168 static LCID lcid_LC_MESSAGES;
169 static LCID lcid_LC_MONETARY;
170 static LCID lcid_LC_NUMERIC;
171 static LCID lcid_LC_TIME;
172 static LCID lcid_LC_PAPER;
173 static LCID lcid_LC_MEASUREMENT;
174 static LCID lcid_LC_TELEPHONE;
176 /* Copy Ascii string to Unicode without using codepages */
177 static inline void strcpynAtoW( WCHAR *dst, const char *src, size_t n )
179 while (n > 1 && *src)
181 *dst++ = (unsigned char)*src++;
182 n--;
184 if (n) *dst = 0;
188 /***********************************************************************
189 * get_lcid_codepage
191 * Retrieve the ANSI codepage for a given locale.
193 static inline UINT get_lcid_codepage( LCID lcid )
195 UINT ret;
196 if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
197 sizeof(ret)/sizeof(WCHAR) )) ret = 0;
198 return ret;
202 /***********************************************************************
203 * get_codepage_table
205 * Find the table for a given codepage, handling CP_ACP etc. pseudo-codepages
207 static const union cptable *get_codepage_table( unsigned int codepage )
209 const union cptable *ret = NULL;
211 assert( ansi_cptable ); /* init must have been done already */
213 switch(codepage)
215 case CP_ACP:
216 return ansi_cptable;
217 case CP_OEMCP:
218 return oem_cptable;
219 case CP_MACCP:
220 return mac_cptable;
221 case CP_UTF7:
222 case CP_UTF8:
223 break;
224 case CP_THREAD_ACP:
225 if (!(codepage = kernel_get_thread_data()->code_page)) return ansi_cptable;
226 /* fall through */
227 default:
228 if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
229 if (codepage == oem_cptable->info.codepage) return oem_cptable;
230 if (codepage == mac_cptable->info.codepage) return mac_cptable;
231 ret = wine_cp_get_table( codepage );
232 break;
234 return ret;
238 /***********************************************************************
239 * charset_cmp (internal)
241 static int charset_cmp( const void *name, const void *entry )
243 const struct charset_entry *charset = entry;
244 return strcasecmp( name, charset->charset_name );
247 /***********************************************************************
248 * find_charset
250 static UINT find_charset( const WCHAR *name )
252 const struct charset_entry *entry;
253 char charset_name[16];
254 size_t i, j;
256 /* remove punctuation characters from charset name */
257 for (i = j = 0; name[i] && j < sizeof(charset_name)-1; i++)
258 if (isalnum((unsigned char)name[i])) charset_name[j++] = name[i];
259 charset_name[j] = 0;
261 entry = bsearch( charset_name, charset_names,
262 sizeof(charset_names)/sizeof(charset_names[0]),
263 sizeof(charset_names[0]), charset_cmp );
264 if (entry) return entry->codepage;
265 return 0;
269 /***********************************************************************
270 * find_locale_id_callback
272 static BOOL CALLBACK find_locale_id_callback( HMODULE hModule, LPCWSTR type,
273 LPCWSTR name, WORD LangID, LPARAM lParam )
275 struct locale_name *data = (struct locale_name *)lParam;
276 WCHAR buffer[128];
277 int matches = 0;
278 LCID lcid = MAKELCID( LangID, SORT_DEFAULT ); /* FIXME: handle sort order */
280 if (PRIMARYLANGID(LangID) == LANG_NEUTRAL) return TRUE; /* continue search */
282 /* first check exact name */
283 if (data->win_name[0] &&
284 GetLocaleInfoW( lcid, LOCALE_SNAME | LOCALE_NOUSEROVERRIDE,
285 buffer, sizeof(buffer)/sizeof(WCHAR) ))
287 if (!strcmpW( data->win_name, buffer ))
289 matches = 4; /* everything matches */
290 goto done;
294 if (!GetLocaleInfoW( lcid, LOCALE_SISO639LANGNAME | LOCALE_NOUSEROVERRIDE,
295 buffer, sizeof(buffer)/sizeof(WCHAR) ))
296 return TRUE;
297 if (strcmpW( buffer, data->lang )) return TRUE;
298 matches++; /* language name matched */
300 if (data->country)
302 if (GetLocaleInfoW( lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
303 buffer, sizeof(buffer)/sizeof(WCHAR) ))
305 if (strcmpW( buffer, data->country )) goto done;
306 matches++; /* country name matched */
309 else /* match default language */
311 if (SUBLANGID(LangID) == SUBLANG_DEFAULT) matches++;
314 if (data->codepage)
316 UINT unix_cp;
317 if (GetLocaleInfoW( lcid, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
318 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) ))
320 if (unix_cp == data->codepage) matches++;
324 /* FIXME: check sort order */
326 done:
327 if (matches > data->matches)
329 data->lcid = lcid;
330 data->matches = matches;
332 return (data->matches < 4); /* no need to continue for perfect match */
336 /***********************************************************************
337 * parse_locale_name
339 * Parse a locale name into a struct locale_name, handling both Windows and Unix formats.
340 * Unix format is: lang[_country][.charset][@modifier]
341 * Windows format is: lang[-script][-country][_modifier]
343 static void parse_locale_name( const WCHAR *str, struct locale_name *name )
345 static const WCHAR sepW[] = {'-','_','.','@',0};
346 static const WCHAR winsepW[] = {'-','_',0};
347 static const WCHAR posixW[] = {'P','O','S','I','X',0};
348 static const WCHAR cW[] = {'C',0};
349 static const WCHAR latinW[] = {'l','a','t','i','n',0};
350 static const WCHAR latnW[] = {'-','L','a','t','n',0};
351 WCHAR *p;
353 TRACE("%s\n", debugstr_w(str));
355 name->country = name->charset = name->script = name->modifier = NULL;
356 name->lcid = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
357 name->matches = 0;
358 name->codepage = 0;
359 name->win_name[0] = 0;
360 lstrcpynW( name->lang, str, sizeof(name->lang)/sizeof(WCHAR) );
362 if (!(p = strpbrkW( name->lang, sepW )))
364 if (!strcmpW( name->lang, posixW ) || !strcmpW( name->lang, cW ))
366 name->matches = 4; /* perfect match for default English lcid */
367 return;
369 strcpyW( name->win_name, name->lang );
371 else if (*p == '-') /* Windows format */
373 strcpyW( name->win_name, name->lang );
374 *p++ = 0;
375 name->country = p;
376 if (!(p = strpbrkW( p, winsepW ))) goto done;
377 if (*p == '-')
379 *p++ = 0;
380 name->script = name->country;
381 name->country = p;
382 if (!(p = strpbrkW( p, winsepW ))) goto done;
384 *p++ = 0;
385 name->modifier = p;
387 else /* Unix format */
389 if (*p == '_')
391 *p++ = 0;
392 name->country = p;
393 p = strpbrkW( p, sepW + 2 );
395 if (p && *p == '.')
397 *p++ = 0;
398 name->charset = p;
399 p = strchrW( p, '@' );
401 if (p)
403 *p++ = 0;
404 name->modifier = p;
407 if (name->charset)
408 name->codepage = find_charset( name->charset );
410 /* rebuild a Windows name if possible */
412 if (name->charset) goto done; /* can't specify charset in Windows format */
413 if (name->modifier && strcmpW( name->modifier, latinW ))
414 goto done; /* only Latn script supported for now */
415 strcpyW( name->win_name, name->lang );
416 if (name->modifier) strcatW( name->win_name, latnW );
417 if (name->country)
419 p = name->win_name + strlenW(name->win_name);
420 *p++ = '-';
421 strcpyW( p, name->country );
424 done:
425 EnumResourceLanguagesW( kernel32_handle, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
426 find_locale_id_callback, (LPARAM)name );
430 /***********************************************************************
431 * convert_default_lcid
433 * Get the default LCID to use for a given lctype in GetLocaleInfo.
435 static LCID convert_default_lcid( LCID lcid, LCTYPE lctype )
437 if (lcid == LOCALE_SYSTEM_DEFAULT ||
438 lcid == LOCALE_USER_DEFAULT ||
439 lcid == LOCALE_NEUTRAL)
441 LCID default_id = 0;
443 switch(lctype & 0xffff)
445 case LOCALE_SSORTNAME:
446 default_id = lcid_LC_COLLATE;
447 break;
449 case LOCALE_FONTSIGNATURE:
450 case LOCALE_IDEFAULTANSICODEPAGE:
451 case LOCALE_IDEFAULTCODEPAGE:
452 case LOCALE_IDEFAULTEBCDICCODEPAGE:
453 case LOCALE_IDEFAULTMACCODEPAGE:
454 case LOCALE_IDEFAULTUNIXCODEPAGE:
455 default_id = lcid_LC_CTYPE;
456 break;
458 case LOCALE_ICURRDIGITS:
459 case LOCALE_ICURRENCY:
460 case LOCALE_IINTLCURRDIGITS:
461 case LOCALE_INEGCURR:
462 case LOCALE_INEGSEPBYSPACE:
463 case LOCALE_INEGSIGNPOSN:
464 case LOCALE_INEGSYMPRECEDES:
465 case LOCALE_IPOSSEPBYSPACE:
466 case LOCALE_IPOSSIGNPOSN:
467 case LOCALE_IPOSSYMPRECEDES:
468 case LOCALE_SCURRENCY:
469 case LOCALE_SINTLSYMBOL:
470 case LOCALE_SMONDECIMALSEP:
471 case LOCALE_SMONGROUPING:
472 case LOCALE_SMONTHOUSANDSEP:
473 case LOCALE_SNATIVECURRNAME:
474 default_id = lcid_LC_MONETARY;
475 break;
477 case LOCALE_IDIGITS:
478 case LOCALE_IDIGITSUBSTITUTION:
479 case LOCALE_ILZERO:
480 case LOCALE_INEGNUMBER:
481 case LOCALE_SDECIMAL:
482 case LOCALE_SGROUPING:
483 case LOCALE_SNAN:
484 case LOCALE_SNATIVEDIGITS:
485 case LOCALE_SNEGATIVESIGN:
486 case LOCALE_SNEGINFINITY:
487 case LOCALE_SPOSINFINITY:
488 case LOCALE_SPOSITIVESIGN:
489 case LOCALE_STHOUSAND:
490 default_id = lcid_LC_NUMERIC;
491 break;
493 case LOCALE_ICALENDARTYPE:
494 case LOCALE_ICENTURY:
495 case LOCALE_IDATE:
496 case LOCALE_IDAYLZERO:
497 case LOCALE_IFIRSTDAYOFWEEK:
498 case LOCALE_IFIRSTWEEKOFYEAR:
499 case LOCALE_ILDATE:
500 case LOCALE_IMONLZERO:
501 case LOCALE_IOPTIONALCALENDAR:
502 case LOCALE_ITIME:
503 case LOCALE_ITIMEMARKPOSN:
504 case LOCALE_ITLZERO:
505 case LOCALE_S1159:
506 case LOCALE_S2359:
507 case LOCALE_SABBREVDAYNAME1:
508 case LOCALE_SABBREVDAYNAME2:
509 case LOCALE_SABBREVDAYNAME3:
510 case LOCALE_SABBREVDAYNAME4:
511 case LOCALE_SABBREVDAYNAME5:
512 case LOCALE_SABBREVDAYNAME6:
513 case LOCALE_SABBREVDAYNAME7:
514 case LOCALE_SABBREVMONTHNAME1:
515 case LOCALE_SABBREVMONTHNAME2:
516 case LOCALE_SABBREVMONTHNAME3:
517 case LOCALE_SABBREVMONTHNAME4:
518 case LOCALE_SABBREVMONTHNAME5:
519 case LOCALE_SABBREVMONTHNAME6:
520 case LOCALE_SABBREVMONTHNAME7:
521 case LOCALE_SABBREVMONTHNAME8:
522 case LOCALE_SABBREVMONTHNAME9:
523 case LOCALE_SABBREVMONTHNAME10:
524 case LOCALE_SABBREVMONTHNAME11:
525 case LOCALE_SABBREVMONTHNAME12:
526 case LOCALE_SABBREVMONTHNAME13:
527 case LOCALE_SDATE:
528 case LOCALE_SDAYNAME1:
529 case LOCALE_SDAYNAME2:
530 case LOCALE_SDAYNAME3:
531 case LOCALE_SDAYNAME4:
532 case LOCALE_SDAYNAME5:
533 case LOCALE_SDAYNAME6:
534 case LOCALE_SDAYNAME7:
535 case LOCALE_SDURATION:
536 case LOCALE_SLONGDATE:
537 case LOCALE_SMONTHNAME1:
538 case LOCALE_SMONTHNAME2:
539 case LOCALE_SMONTHNAME3:
540 case LOCALE_SMONTHNAME4:
541 case LOCALE_SMONTHNAME5:
542 case LOCALE_SMONTHNAME6:
543 case LOCALE_SMONTHNAME7:
544 case LOCALE_SMONTHNAME8:
545 case LOCALE_SMONTHNAME9:
546 case LOCALE_SMONTHNAME10:
547 case LOCALE_SMONTHNAME11:
548 case LOCALE_SMONTHNAME12:
549 case LOCALE_SMONTHNAME13:
550 case LOCALE_SSHORTDATE:
551 case LOCALE_SSHORTESTDAYNAME1:
552 case LOCALE_SSHORTESTDAYNAME2:
553 case LOCALE_SSHORTESTDAYNAME3:
554 case LOCALE_SSHORTESTDAYNAME4:
555 case LOCALE_SSHORTESTDAYNAME5:
556 case LOCALE_SSHORTESTDAYNAME6:
557 case LOCALE_SSHORTESTDAYNAME7:
558 case LOCALE_STIME:
559 case LOCALE_STIMEFORMAT:
560 case LOCALE_SYEARMONTH:
561 default_id = lcid_LC_TIME;
562 break;
564 case LOCALE_IPAPERSIZE:
565 default_id = lcid_LC_PAPER;
566 break;
568 case LOCALE_IMEASURE:
569 default_id = lcid_LC_MEASUREMENT;
570 break;
572 case LOCALE_ICOUNTRY:
573 default_id = lcid_LC_TELEPHONE;
574 break;
576 if (default_id) lcid = default_id;
578 return ConvertDefaultLocale( lcid );
581 /***********************************************************************
582 * is_genitive_name_supported
584 * Determine could LCTYPE basically support genitive name form or not.
586 static BOOL is_genitive_name_supported( LCTYPE lctype )
588 switch(lctype & 0xffff)
590 case LOCALE_SMONTHNAME1:
591 case LOCALE_SMONTHNAME2:
592 case LOCALE_SMONTHNAME3:
593 case LOCALE_SMONTHNAME4:
594 case LOCALE_SMONTHNAME5:
595 case LOCALE_SMONTHNAME6:
596 case LOCALE_SMONTHNAME7:
597 case LOCALE_SMONTHNAME8:
598 case LOCALE_SMONTHNAME9:
599 case LOCALE_SMONTHNAME10:
600 case LOCALE_SMONTHNAME11:
601 case LOCALE_SMONTHNAME12:
602 case LOCALE_SMONTHNAME13:
603 return TRUE;
604 default:
605 return FALSE;
609 /***********************************************************************
610 * create_registry_key
612 * Create the Control Panel\\International registry key.
614 static inline HANDLE create_registry_key(void)
616 static const WCHAR intlW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
617 'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
618 OBJECT_ATTRIBUTES attr;
619 UNICODE_STRING nameW;
620 HANDLE hkey;
622 if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
624 attr.Length = sizeof(attr);
625 attr.RootDirectory = hkey;
626 attr.ObjectName = &nameW;
627 attr.Attributes = 0;
628 attr.SecurityDescriptor = NULL;
629 attr.SecurityQualityOfService = NULL;
630 RtlInitUnicodeString( &nameW, intlW );
632 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS) hkey = 0;
633 NtClose( attr.RootDirectory );
634 return hkey;
638 /* update the registry settings for a given locale parameter */
639 /* return TRUE if an update was needed */
640 static BOOL locale_update_registry( HKEY hkey, const WCHAR *name, LCID lcid,
641 const LCTYPE *values, UINT nb_values )
643 static const WCHAR formatW[] = { '%','0','8','x',0 };
644 WCHAR bufferW[40];
645 UNICODE_STRING nameW;
646 DWORD count, i;
648 RtlInitUnicodeString( &nameW, name );
649 count = sizeof(bufferW);
650 if (!NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation, bufferW, count, &count))
652 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
653 LPCWSTR text = (LPCWSTR)info->Data;
655 if (strtoulW( text, NULL, 16 ) == lcid) return FALSE; /* already set correctly */
656 TRACE( "updating registry, locale %s changed %s -> %08x\n",
657 debugstr_w(name), debugstr_w(text), lcid );
659 else TRACE( "updating registry, locale %s changed none -> %08x\n", debugstr_w(name), lcid );
660 sprintfW( bufferW, formatW, lcid );
661 NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, (strlenW(bufferW) + 1) * sizeof(WCHAR) );
663 for (i = 0; i < nb_values; i++)
665 GetLocaleInfoW( lcid, values[i] | LOCALE_NOUSEROVERRIDE, bufferW,
666 sizeof(bufferW)/sizeof(WCHAR) );
667 SetLocaleInfoW( lcid, values[i], bufferW );
669 return TRUE;
673 /***********************************************************************
674 * LOCALE_InitRegistry
676 * Update registry contents on startup if the user locale has changed.
677 * This simulates the action of the Windows control panel.
679 void LOCALE_InitRegistry(void)
681 static const WCHAR acpW[] = {'A','C','P',0};
682 static const WCHAR oemcpW[] = {'O','E','M','C','P',0};
683 static const WCHAR maccpW[] = {'M','A','C','C','P',0};
684 static const WCHAR localeW[] = {'L','o','c','a','l','e',0};
685 static const WCHAR lc_ctypeW[] = { 'L','C','_','C','T','Y','P','E',0 };
686 static const WCHAR lc_monetaryW[] = { 'L','C','_','M','O','N','E','T','A','R','Y',0 };
687 static const WCHAR lc_numericW[] = { 'L','C','_','N','U','M','E','R','I','C',0 };
688 static const WCHAR lc_timeW[] = { 'L','C','_','T','I','M','E',0 };
689 static const WCHAR lc_measurementW[] = { 'L','C','_','M','E','A','S','U','R','E','M','E','N','T',0 };
690 static const WCHAR lc_telephoneW[] = { 'L','C','_','T','E','L','E','P','H','O','N','E',0 };
691 static const WCHAR lc_paperW[] = { 'L','C','_','P','A','P','E','R',0};
692 static const struct
694 LPCWSTR name;
695 USHORT value;
696 } update_cp_values[] = {
697 { acpW, LOCALE_IDEFAULTANSICODEPAGE },
698 { oemcpW, LOCALE_IDEFAULTCODEPAGE },
699 { maccpW, LOCALE_IDEFAULTMACCODEPAGE }
701 static const LCTYPE lc_messages_values[] = {
702 LOCALE_SABBREVLANGNAME,
703 LOCALE_SCOUNTRY,
704 LOCALE_SLIST };
705 static const LCTYPE lc_monetary_values[] = {
706 LOCALE_SCURRENCY,
707 LOCALE_ICURRENCY,
708 LOCALE_INEGCURR,
709 LOCALE_ICURRDIGITS,
710 LOCALE_ILZERO,
711 LOCALE_SMONDECIMALSEP,
712 LOCALE_SMONGROUPING,
713 LOCALE_SMONTHOUSANDSEP };
714 static const LCTYPE lc_numeric_values[] = {
715 LOCALE_SDECIMAL,
716 LOCALE_STHOUSAND,
717 LOCALE_IDIGITS,
718 LOCALE_IDIGITSUBSTITUTION,
719 LOCALE_SNATIVEDIGITS,
720 LOCALE_INEGNUMBER,
721 LOCALE_SNEGATIVESIGN,
722 LOCALE_SPOSITIVESIGN,
723 LOCALE_SGROUPING };
724 static const LCTYPE lc_time_values[] = {
725 LOCALE_S1159,
726 LOCALE_S2359,
727 LOCALE_STIME,
728 LOCALE_ITIME,
729 LOCALE_ITLZERO,
730 LOCALE_SSHORTDATE,
731 LOCALE_SLONGDATE,
732 LOCALE_SDATE,
733 LOCALE_ITIMEMARKPOSN,
734 LOCALE_ICALENDARTYPE,
735 LOCALE_IFIRSTDAYOFWEEK,
736 LOCALE_IFIRSTWEEKOFYEAR,
737 LOCALE_STIMEFORMAT,
738 LOCALE_SYEARMONTH,
739 LOCALE_IDATE };
740 static const LCTYPE lc_measurement_values[] = { LOCALE_IMEASURE };
741 static const LCTYPE lc_telephone_values[] = { LOCALE_ICOUNTRY };
742 static const LCTYPE lc_paper_values[] = { LOCALE_IPAPERSIZE };
744 UNICODE_STRING nameW;
745 WCHAR bufferW[80];
746 DWORD count, i;
747 HANDLE hkey;
748 LCID lcid = GetUserDefaultLCID();
750 if (!(hkey = create_registry_key()))
751 return; /* don't do anything if we can't create the registry key */
753 locale_update_registry( hkey, localeW, lcid_LC_MESSAGES, lc_messages_values,
754 sizeof(lc_messages_values)/sizeof(lc_messages_values[0]) );
755 locale_update_registry( hkey, lc_monetaryW, lcid_LC_MONETARY, lc_monetary_values,
756 sizeof(lc_monetary_values)/sizeof(lc_monetary_values[0]) );
757 locale_update_registry( hkey, lc_numericW, lcid_LC_NUMERIC, lc_numeric_values,
758 sizeof(lc_numeric_values)/sizeof(lc_numeric_values[0]) );
759 locale_update_registry( hkey, lc_timeW, lcid_LC_TIME, lc_time_values,
760 sizeof(lc_time_values)/sizeof(lc_time_values[0]) );
761 locale_update_registry( hkey, lc_measurementW, lcid_LC_MEASUREMENT, lc_measurement_values,
762 sizeof(lc_measurement_values)/sizeof(lc_measurement_values[0]) );
763 locale_update_registry( hkey, lc_telephoneW, lcid_LC_TELEPHONE, lc_telephone_values,
764 sizeof(lc_telephone_values)/sizeof(lc_telephone_values[0]) );
765 locale_update_registry( hkey, lc_paperW, lcid_LC_PAPER, lc_paper_values,
766 sizeof(lc_paper_values)/sizeof(lc_paper_values[0]) );
768 if (locale_update_registry( hkey, lc_ctypeW, lcid_LC_CTYPE, NULL, 0 ))
770 OBJECT_ATTRIBUTES attr;
771 HANDLE nls_key;
773 RtlInitUnicodeString( &nameW, szCodepageKeyName );
774 InitializeObjectAttributes( &attr, &nameW, 0, 0, NULL );
775 if (!NtCreateKey( &nls_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
777 for (i = 0; i < sizeof(update_cp_values)/sizeof(update_cp_values[0]); i++)
779 count = GetLocaleInfoW( lcid, update_cp_values[i].value | LOCALE_NOUSEROVERRIDE,
780 bufferW, sizeof(bufferW)/sizeof(WCHAR) );
781 RtlInitUnicodeString( &nameW, update_cp_values[i].name );
782 NtSetValueKey( nls_key, &nameW, 0, REG_SZ, bufferW, count * sizeof(WCHAR) );
784 NtClose( nls_key );
788 NtClose( hkey );
792 /***********************************************************************
793 * setup_unix_locales
795 static UINT setup_unix_locales(void)
797 struct locale_name locale_name;
798 WCHAR buffer[128], ctype_buff[128];
799 char *locale;
800 UINT unix_cp = 0;
802 if ((locale = setlocale( LC_CTYPE, NULL )))
804 strcpynAtoW( ctype_buff, locale, sizeof(ctype_buff)/sizeof(WCHAR) );
805 parse_locale_name( ctype_buff, &locale_name );
806 lcid_LC_CTYPE = locale_name.lcid;
807 unix_cp = locale_name.codepage;
809 if (!lcid_LC_CTYPE) /* this one needs a default value */
810 lcid_LC_CTYPE = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
812 TRACE( "got lcid %04x (%d matches) for LC_CTYPE=%s\n",
813 locale_name.lcid, locale_name.matches, debugstr_a(locale) );
815 #define GET_UNIX_LOCALE(cat) do \
816 if ((locale = setlocale( cat, NULL ))) \
818 strcpynAtoW( buffer, locale, sizeof(buffer)/sizeof(WCHAR) ); \
819 if (!strcmpW( buffer, ctype_buff )) lcid_##cat = lcid_LC_CTYPE; \
820 else { \
821 parse_locale_name( buffer, &locale_name ); \
822 lcid_##cat = locale_name.lcid; \
823 TRACE( "got lcid %04x (%d matches) for " #cat "=%s\n", \
824 locale_name.lcid, locale_name.matches, debugstr_a(locale) ); \
826 } while (0)
828 GET_UNIX_LOCALE( LC_COLLATE );
829 GET_UNIX_LOCALE( LC_MESSAGES );
830 GET_UNIX_LOCALE( LC_MONETARY );
831 GET_UNIX_LOCALE( LC_NUMERIC );
832 GET_UNIX_LOCALE( LC_TIME );
833 #ifdef LC_PAPER
834 GET_UNIX_LOCALE( LC_PAPER );
835 #endif
836 #ifdef LC_MEASUREMENT
837 GET_UNIX_LOCALE( LC_MEASUREMENT );
838 #endif
839 #ifdef LC_TELEPHONE
840 GET_UNIX_LOCALE( LC_TELEPHONE );
841 #endif
843 #undef GET_UNIX_LOCALE
845 return unix_cp;
849 /***********************************************************************
850 * GetUserDefaultLangID (KERNEL32.@)
852 * Get the default language Id for the current user.
854 * PARAMS
855 * None.
857 * RETURNS
858 * The current LANGID of the default language for the current user.
860 LANGID WINAPI GetUserDefaultLangID(void)
862 return LANGIDFROMLCID(GetUserDefaultLCID());
866 /***********************************************************************
867 * GetSystemDefaultLangID (KERNEL32.@)
869 * Get the default language Id for the system.
871 * PARAMS
872 * None.
874 * RETURNS
875 * The current LANGID of the default language for the system.
877 LANGID WINAPI GetSystemDefaultLangID(void)
879 return LANGIDFROMLCID(GetSystemDefaultLCID());
883 /***********************************************************************
884 * GetUserDefaultLCID (KERNEL32.@)
886 * Get the default locale Id for the current user.
888 * PARAMS
889 * None.
891 * RETURNS
892 * The current LCID of the default locale for the current user.
894 LCID WINAPI GetUserDefaultLCID(void)
896 LCID lcid;
897 NtQueryDefaultLocale( TRUE, &lcid );
898 return lcid;
902 /***********************************************************************
903 * GetSystemDefaultLCID (KERNEL32.@)
905 * Get the default locale Id for the system.
907 * PARAMS
908 * None.
910 * RETURNS
911 * The current LCID of the default locale for the system.
913 LCID WINAPI GetSystemDefaultLCID(void)
915 LCID lcid;
916 NtQueryDefaultLocale( FALSE, &lcid );
917 return lcid;
921 /***********************************************************************
922 * GetUserDefaultUILanguage (KERNEL32.@)
924 * Get the default user interface language Id for the current user.
926 * PARAMS
927 * None.
929 * RETURNS
930 * The current LANGID of the default UI language for the current user.
932 LANGID WINAPI GetUserDefaultUILanguage(void)
934 LANGID lang;
935 NtQueryDefaultUILanguage( &lang );
936 return lang;
940 /***********************************************************************
941 * GetSystemDefaultUILanguage (KERNEL32.@)
943 * Get the default user interface language Id for the system.
945 * PARAMS
946 * None.
948 * RETURNS
949 * The current LANGID of the default UI language for the system. This is
950 * typically the same language used during the installation process.
952 LANGID WINAPI GetSystemDefaultUILanguage(void)
954 LANGID lang;
955 NtQueryInstallUILanguage( &lang );
956 return lang;
960 /***********************************************************************
961 * LocaleNameToLCID (KERNEL32.@)
963 LCID WINAPI LocaleNameToLCID( LPCWSTR name, DWORD flags )
965 struct locale_name locale_name;
967 if (flags) FIXME( "unsupported flags %x\n", flags );
969 parse_locale_name( name, &locale_name );
971 TRACE( "found lcid %x for %s, matches %d\n",
972 locale_name.lcid, debugstr_w(name), locale_name.matches );
974 if (!locale_name.matches)
975 WARN( "locale %s not recognized, defaulting to English\n", debugstr_w(name) );
976 else if (locale_name.matches == 1)
977 WARN( "locale %s not recognized, defaulting to %s\n",
978 debugstr_w(name), debugstr_w(locale_name.lang) );
980 return locale_name.lcid;
984 /***********************************************************************
985 * LCIDToLocaleName (KERNEL32.@)
987 INT WINAPI LCIDToLocaleName( LCID lcid, LPWSTR name, INT count, DWORD flags )
989 if (flags) FIXME( "unsupported flags %x\n", flags );
991 return GetLocaleInfoW( lcid, LOCALE_SNAME | LOCALE_NOUSEROVERRIDE, name, count );
995 /******************************************************************************
996 * get_locale_value_name
998 * Gets the registry value name for a given lctype.
1000 static const WCHAR *get_locale_value_name( DWORD lctype )
1002 static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
1003 static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
1004 static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
1005 static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
1006 static const WCHAR iDateW[] = {'i','D','a','t','e',0};
1007 static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
1008 static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
1009 static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
1010 static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
1011 static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
1012 static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
1013 static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
1014 static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
1015 static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
1016 static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
1017 static const WCHAR iTimePrefixW[] = {'i','T','i','m','e','P','r','e','f','i','x',0};
1018 static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
1019 static const WCHAR s1159W[] = {'s','1','1','5','9',0};
1020 static const WCHAR s2359W[] = {'s','2','3','5','9',0};
1021 static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
1022 static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
1023 static const WCHAR sDateW[] = {'s','D','a','t','e',0};
1024 static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
1025 static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
1026 static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
1027 static const WCHAR sListW[] = {'s','L','i','s','t',0};
1028 static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
1029 static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
1030 static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
1031 static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
1032 static const WCHAR sNativeDigitsW[] = {'s','N','a','t','i','v','e','D','i','g','i','t','s',0};
1033 static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
1034 static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
1035 static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
1036 static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
1037 static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
1038 static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
1039 static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
1040 static const WCHAR NumShapeW[] = {'N','u','m','s','h','a','p','e',0};
1042 switch (lctype)
1044 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
1045 * the values are stored in the registry, confirmed under Windows.
1047 case LOCALE_ICALENDARTYPE: return iCalendarTypeW;
1048 case LOCALE_ICURRDIGITS: return iCurrDigitsW;
1049 case LOCALE_ICURRENCY: return iCurrencyW;
1050 case LOCALE_IDIGITS: return iDigitsW;
1051 case LOCALE_IFIRSTDAYOFWEEK: return iFirstDayOfWeekW;
1052 case LOCALE_IFIRSTWEEKOFYEAR: return iFirstWeekOfYearW;
1053 case LOCALE_ILZERO: return iLZeroW;
1054 case LOCALE_IMEASURE: return iMeasureW;
1055 case LOCALE_INEGCURR: return iNegCurrW;
1056 case LOCALE_INEGNUMBER: return iNegNumberW;
1057 case LOCALE_IPAPERSIZE: return iPaperSizeW;
1058 case LOCALE_ITIME: return iTimeW;
1059 case LOCALE_S1159: return s1159W;
1060 case LOCALE_S2359: return s2359W;
1061 case LOCALE_SCURRENCY: return sCurrencyW;
1062 case LOCALE_SDATE: return sDateW;
1063 case LOCALE_SDECIMAL: return sDecimalW;
1064 case LOCALE_SGROUPING: return sGroupingW;
1065 case LOCALE_SLIST: return sListW;
1066 case LOCALE_SLONGDATE: return sLongDateW;
1067 case LOCALE_SMONDECIMALSEP: return sMonDecimalSepW;
1068 case LOCALE_SMONGROUPING: return sMonGroupingW;
1069 case LOCALE_SMONTHOUSANDSEP: return sMonThousandSepW;
1070 case LOCALE_SNEGATIVESIGN: return sNegativeSignW;
1071 case LOCALE_SPOSITIVESIGN: return sPositiveSignW;
1072 case LOCALE_SSHORTDATE: return sShortDateW;
1073 case LOCALE_STHOUSAND: return sThousandW;
1074 case LOCALE_STIME: return sTimeW;
1075 case LOCALE_STIMEFORMAT: return sTimeFormatW;
1076 case LOCALE_SYEARMONTH: return sYearMonthW;
1078 /* The following are not listed under MSDN as supported,
1079 * but seem to be used and also stored in the registry.
1081 case LOCALE_ICOUNTRY: return iCountryW;
1082 case LOCALE_IDATE: return iDateW;
1083 case LOCALE_ILDATE: return iLDateW;
1084 case LOCALE_ITLZERO: return iTLZeroW;
1085 case LOCALE_SCOUNTRY: return sCountryW;
1086 case LOCALE_SABBREVLANGNAME: return sLanguageW;
1088 /* The following are used in XP and later */
1089 case LOCALE_IDIGITSUBSTITUTION: return NumShapeW;
1090 case LOCALE_SNATIVEDIGITS: return sNativeDigitsW;
1091 case LOCALE_ITIMEMARKPOSN: return iTimePrefixW;
1093 return NULL;
1097 /******************************************************************************
1098 * get_registry_locale_info
1100 * Retrieve user-modified locale info from the registry.
1101 * Return length, 0 on error, -1 if not found.
1103 static INT get_registry_locale_info( LPCWSTR value, LPWSTR buffer, INT len )
1105 DWORD size;
1106 INT ret;
1107 HANDLE hkey;
1108 NTSTATUS status;
1109 UNICODE_STRING nameW;
1110 KEY_VALUE_PARTIAL_INFORMATION *info;
1111 static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
1113 if (!(hkey = create_registry_key())) return -1;
1115 RtlInitUnicodeString( &nameW, value );
1116 size = info_size + len * sizeof(WCHAR);
1118 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
1120 NtClose( hkey );
1121 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1122 return 0;
1125 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
1127 if (!status)
1129 ret = (size - info_size) / sizeof(WCHAR);
1130 /* append terminating null if needed */
1131 if (!ret || ((WCHAR *)info->Data)[ret-1])
1133 if (ret < len || !buffer) ret++;
1134 else
1136 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1137 ret = 0;
1140 if (ret && buffer)
1142 memcpy( buffer, info->Data, (ret-1) * sizeof(WCHAR) );
1143 buffer[ret-1] = 0;
1146 else if (status == STATUS_BUFFER_OVERFLOW && !buffer)
1148 ret = (size - info_size) / sizeof(WCHAR) + 1;
1150 else if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1152 ret = -1;
1154 else
1156 SetLastError( RtlNtStatusToDosError(status) );
1157 ret = 0;
1159 NtClose( hkey );
1160 HeapFree( GetProcessHeap(), 0, info );
1161 return ret;
1165 /******************************************************************************
1166 * GetLocaleInfoA (KERNEL32.@)
1168 * Get information about an aspect of a locale.
1170 * PARAMS
1171 * lcid [I] LCID of the locale
1172 * lctype [I] LCTYPE_ flags from "winnls.h"
1173 * buffer [O] Destination for the information
1174 * len [I] Length of buffer in characters
1176 * RETURNS
1177 * Success: The size of the data requested. If buffer is non-NULL, it is filled
1178 * with the information.
1179 * Failure: 0. Use GetLastError() to determine the cause.
1181 * NOTES
1182 * - LOCALE_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
1183 * - The string returned is NUL terminated, except for LOCALE_FONTSIGNATURE,
1184 * which is a bit string.
1186 INT WINAPI GetLocaleInfoA( LCID lcid, LCTYPE lctype, LPSTR buffer, INT len )
1188 WCHAR *bufferW;
1189 INT lenW, ret;
1191 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
1193 if (len < 0 || (len && !buffer))
1195 SetLastError( ERROR_INVALID_PARAMETER );
1196 return 0;
1198 if (lctype & LOCALE_RETURN_GENITIVE_NAMES )
1200 SetLastError( ERROR_INVALID_FLAGS );
1201 return 0;
1204 if (!len) buffer = NULL;
1206 if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
1208 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
1210 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1211 return 0;
1213 if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
1215 if ((lctype & LOCALE_RETURN_NUMBER) ||
1216 ((lctype & ~LOCALE_LOCALEINFOFLAGSMASK) == LOCALE_FONTSIGNATURE))
1218 /* it's not an ASCII string, just bytes */
1219 ret *= sizeof(WCHAR);
1220 if (buffer)
1222 if (ret <= len) memcpy( buffer, bufferW, ret );
1223 else
1225 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1226 ret = 0;
1230 else
1232 UINT codepage = CP_ACP;
1233 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
1234 ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
1237 HeapFree( GetProcessHeap(), 0, bufferW );
1238 return ret;
1242 /******************************************************************************
1243 * GetLocaleInfoW (KERNEL32.@)
1245 * See GetLocaleInfoA.
1247 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
1249 LANGID lang_id;
1250 HRSRC hrsrc;
1251 HGLOBAL hmem;
1252 INT ret;
1253 UINT lcflags;
1254 const WCHAR *p;
1255 unsigned int i;
1257 if (len < 0 || (len && !buffer))
1259 SetLastError( ERROR_INVALID_PARAMETER );
1260 return 0;
1262 if (lctype & LOCALE_RETURN_GENITIVE_NAMES &&
1263 !is_genitive_name_supported( lctype ))
1265 SetLastError( ERROR_INVALID_FLAGS );
1266 return 0;
1269 if (!len) buffer = NULL;
1271 lcid = convert_default_lcid( lcid, lctype );
1273 lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
1274 lctype &= 0xffff;
1276 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
1278 /* first check for overrides in the registry */
1280 if (!(lcflags & LOCALE_NOUSEROVERRIDE) &&
1281 lcid == convert_default_lcid( LOCALE_USER_DEFAULT, lctype ))
1283 const WCHAR *value = get_locale_value_name(lctype);
1285 if (value)
1287 if (lcflags & LOCALE_RETURN_NUMBER)
1289 WCHAR tmp[16];
1290 ret = get_registry_locale_info( value, tmp, sizeof(tmp)/sizeof(WCHAR) );
1291 if (ret > 0)
1293 WCHAR *end;
1294 UINT number = strtolW( tmp, &end, 10 );
1295 if (*end) /* invalid number */
1297 SetLastError( ERROR_INVALID_FLAGS );
1298 return 0;
1300 ret = sizeof(UINT)/sizeof(WCHAR);
1301 if (!buffer) return ret;
1302 if (ret > len)
1304 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1305 return 0;
1307 memcpy( buffer, &number, sizeof(number) );
1310 else ret = get_registry_locale_info( value, buffer, len );
1312 if (ret != -1) return ret;
1316 /* now load it from kernel resources */
1318 lang_id = LANGIDFROMLCID( lcid );
1320 /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
1321 if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
1322 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
1324 if (!(hrsrc = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
1325 ULongToPtr((lctype >> 4) + 1), lang_id )))
1327 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
1328 return 0;
1330 if (!(hmem = LoadResource( kernel32_handle, hrsrc )))
1331 return 0;
1333 p = LockResource( hmem );
1334 for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
1336 if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
1337 else if (is_genitive_name_supported( lctype ) && *p)
1339 /* genitive form's stored after a null separator from a nominative */
1340 for (i = 1; i <= *p; i++) if (!p[i]) break;
1342 if (i <= *p && (lcflags & LOCALE_RETURN_GENITIVE_NAMES))
1344 ret = *p - i + 1;
1345 p += i;
1347 else ret = i;
1349 else
1350 ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
1352 if (!buffer) return ret;
1354 if (ret > len)
1356 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1357 return 0;
1360 if (lcflags & LOCALE_RETURN_NUMBER)
1362 UINT number;
1363 WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
1364 if (!tmp) return 0;
1365 memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
1366 tmp[*p] = 0;
1367 number = strtolW( tmp, &end, 10 );
1368 if (!*end)
1369 memcpy( buffer, &number, sizeof(number) );
1370 else /* invalid number */
1372 SetLastError( ERROR_INVALID_FLAGS );
1373 ret = 0;
1375 HeapFree( GetProcessHeap(), 0, tmp );
1377 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning number %d\n",
1378 lcid, lctype, buffer, len, number );
1380 else
1382 memcpy( buffer, p + 1, ret * sizeof(WCHAR) );
1383 if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
1385 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning %d %s\n",
1386 lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
1388 return ret;
1392 /******************************************************************************
1393 * SetLocaleInfoA [KERNEL32.@]
1395 * Set information about an aspect of a locale.
1397 * PARAMS
1398 * lcid [I] LCID of the locale
1399 * lctype [I] LCTYPE_ flags from "winnls.h"
1400 * data [I] Information to set
1402 * RETURNS
1403 * Success: TRUE. The information given will be returned by GetLocaleInfoA()
1404 * whenever it is called without LOCALE_NOUSEROVERRIDE.
1405 * Failure: FALSE. Use GetLastError() to determine the cause.
1407 * NOTES
1408 * - Values are only be set for the current user locale; the system locale
1409 * settings cannot be changed.
1410 * - Any settings changed by this call are lost when the locale is changed by
1411 * the control panel (in Wine, this happens every time you change LANG).
1412 * - The native implementation of this function does not check that lcid matches
1413 * the current user locale, and simply sets the new values. Wine warns you in
1414 * this case, but behaves the same.
1416 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
1418 UINT codepage = CP_ACP;
1419 WCHAR *strW;
1420 DWORD len;
1421 BOOL ret;
1423 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
1425 if (!data)
1427 SetLastError( ERROR_INVALID_PARAMETER );
1428 return FALSE;
1430 len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
1431 if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1433 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1434 return FALSE;
1436 MultiByteToWideChar( codepage, 0, data, -1, strW, len );
1437 ret = SetLocaleInfoW( lcid, lctype, strW );
1438 HeapFree( GetProcessHeap(), 0, strW );
1439 return ret;
1443 /******************************************************************************
1444 * SetLocaleInfoW (KERNEL32.@)
1446 * See SetLocaleInfoA.
1448 BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
1450 const WCHAR *value;
1451 static const WCHAR intlW[] = {'i','n','t','l',0 };
1452 UNICODE_STRING valueW;
1453 NTSTATUS status;
1454 HANDLE hkey;
1456 lctype &= 0xffff;
1457 value = get_locale_value_name( lctype );
1459 if (!data || !value)
1461 SetLastError( ERROR_INVALID_PARAMETER );
1462 return FALSE;
1465 if (lctype == LOCALE_IDATE || lctype == LOCALE_ILDATE)
1467 SetLastError( ERROR_INVALID_FLAGS );
1468 return FALSE;
1471 TRACE("setting %x (%s) to %s\n", lctype, debugstr_w(value), debugstr_w(data) );
1473 /* FIXME: should check that data to set is sane */
1475 /* FIXME: profile functions should map to registry */
1476 WriteProfileStringW( intlW, value, data );
1478 if (!(hkey = create_registry_key())) return FALSE;
1479 RtlInitUnicodeString( &valueW, value );
1480 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
1482 if (lctype == LOCALE_SSHORTDATE || lctype == LOCALE_SLONGDATE)
1484 /* Set I-value from S value */
1485 WCHAR *lpD, *lpM, *lpY;
1486 WCHAR szBuff[2];
1488 lpD = strrchrW(data, 'd');
1489 lpM = strrchrW(data, 'M');
1490 lpY = strrchrW(data, 'y');
1492 if (lpD <= lpM)
1494 szBuff[0] = '1'; /* D-M-Y */
1496 else
1498 if (lpY <= lpM)
1499 szBuff[0] = '2'; /* Y-M-D */
1500 else
1501 szBuff[0] = '0'; /* M-D-Y */
1504 szBuff[1] = '\0';
1506 if (lctype == LOCALE_SSHORTDATE)
1507 lctype = LOCALE_IDATE;
1508 else
1509 lctype = LOCALE_ILDATE;
1511 value = get_locale_value_name( lctype );
1513 WriteProfileStringW( intlW, value, szBuff );
1515 RtlInitUnicodeString( &valueW, value );
1516 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, szBuff, sizeof(szBuff) );
1519 NtClose( hkey );
1521 if (status) SetLastError( RtlNtStatusToDosError(status) );
1522 return !status;
1526 /******************************************************************************
1527 * GetACP (KERNEL32.@)
1529 * Get the current Ansi code page Id for the system.
1531 * PARAMS
1532 * None.
1534 * RETURNS
1535 * The current Ansi code page identifier for the system.
1537 UINT WINAPI GetACP(void)
1539 assert( ansi_cptable );
1540 return ansi_cptable->info.codepage;
1544 /******************************************************************************
1545 * SetCPGlobal (KERNEL32.@)
1547 * Set the current Ansi code page Id for the system.
1549 * PARAMS
1550 * acp [I] code page ID to be the new ACP.
1552 * RETURNS
1553 * The previous ACP.
1555 UINT WINAPI SetCPGlobal( UINT acp )
1557 UINT ret = GetACP();
1558 const union cptable *new_cptable = wine_cp_get_table( acp );
1560 if (new_cptable) ansi_cptable = new_cptable;
1561 return ret;
1565 /***********************************************************************
1566 * GetOEMCP (KERNEL32.@)
1568 * Get the current OEM code page Id for the system.
1570 * PARAMS
1571 * None.
1573 * RETURNS
1574 * The current OEM code page identifier for the system.
1576 UINT WINAPI GetOEMCP(void)
1578 assert( oem_cptable );
1579 return oem_cptable->info.codepage;
1583 /***********************************************************************
1584 * IsValidCodePage (KERNEL32.@)
1586 * Determine if a given code page identifier is valid.
1588 * PARAMS
1589 * codepage [I] Code page Id to verify.
1591 * RETURNS
1592 * TRUE, If codepage is valid and available on the system,
1593 * FALSE otherwise.
1595 BOOL WINAPI IsValidCodePage( UINT codepage )
1597 switch(codepage) {
1598 case CP_UTF7:
1599 case CP_UTF8:
1600 return TRUE;
1601 default:
1602 return wine_cp_get_table( codepage ) != NULL;
1607 /***********************************************************************
1608 * IsDBCSLeadByteEx (KERNEL32.@)
1610 * Determine if a character is a lead byte in a given code page.
1612 * PARAMS
1613 * codepage [I] Code page for the test.
1614 * testchar [I] Character to test
1616 * RETURNS
1617 * TRUE, if testchar is a lead byte in codepage,
1618 * FALSE otherwise.
1620 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
1622 const union cptable *table = get_codepage_table( codepage );
1623 return table && wine_is_dbcs_leadbyte( table, testchar );
1627 /***********************************************************************
1628 * IsDBCSLeadByte (KERNEL32.@)
1629 * IsDBCSLeadByte (KERNEL.207)
1631 * Determine if a character is a lead byte.
1633 * PARAMS
1634 * testchar [I] Character to test
1636 * RETURNS
1637 * TRUE, if testchar is a lead byte in the Ansii code page,
1638 * FALSE otherwise.
1640 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
1642 if (!ansi_cptable) return FALSE;
1643 return wine_is_dbcs_leadbyte( ansi_cptable, testchar );
1647 /***********************************************************************
1648 * GetCPInfo (KERNEL32.@)
1650 * Get information about a code page.
1652 * PARAMS
1653 * codepage [I] Code page number
1654 * cpinfo [O] Destination for code page information
1656 * RETURNS
1657 * Success: TRUE. cpinfo is updated with the information about codepage.
1658 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1660 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
1662 const union cptable *table;
1664 if (!cpinfo)
1666 SetLastError( ERROR_INVALID_PARAMETER );
1667 return FALSE;
1670 if (!(table = get_codepage_table( codepage )))
1672 switch(codepage)
1674 case CP_UTF7:
1675 case CP_UTF8:
1676 cpinfo->DefaultChar[0] = 0x3f;
1677 cpinfo->DefaultChar[1] = 0;
1678 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1679 cpinfo->MaxCharSize = (codepage == CP_UTF7) ? 5 : 4;
1680 return TRUE;
1683 SetLastError( ERROR_INVALID_PARAMETER );
1684 return FALSE;
1686 if (table->info.def_char & 0xff00)
1688 cpinfo->DefaultChar[0] = (table->info.def_char & 0xff00) >> 8;
1689 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
1691 else
1693 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
1694 cpinfo->DefaultChar[1] = 0;
1696 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
1697 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
1698 else
1699 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1701 return TRUE;
1704 /***********************************************************************
1705 * GetCPInfoExA (KERNEL32.@)
1707 * Get extended information about a code page.
1709 * PARAMS
1710 * codepage [I] Code page number
1711 * dwFlags [I] Reserved, must to 0.
1712 * cpinfo [O] Destination for code page information
1714 * RETURNS
1715 * Success: TRUE. cpinfo is updated with the information about codepage.
1716 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1718 BOOL WINAPI GetCPInfoExA( UINT codepage, DWORD dwFlags, LPCPINFOEXA cpinfo )
1720 CPINFOEXW cpinfoW;
1722 if (!GetCPInfoExW( codepage, dwFlags, &cpinfoW ))
1723 return FALSE;
1725 /* the layout is the same except for CodePageName */
1726 memcpy(cpinfo, &cpinfoW, sizeof(CPINFOEXA));
1727 WideCharToMultiByte(CP_ACP, 0, cpinfoW.CodePageName, -1, cpinfo->CodePageName, sizeof(cpinfo->CodePageName), NULL, NULL);
1728 return TRUE;
1731 /***********************************************************************
1732 * GetCPInfoExW (KERNEL32.@)
1734 * Unicode version of GetCPInfoExA.
1736 BOOL WINAPI GetCPInfoExW( UINT codepage, DWORD dwFlags, LPCPINFOEXW cpinfo )
1738 if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
1739 return FALSE;
1741 switch(codepage)
1743 case CP_UTF7:
1745 static const WCHAR utf7[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','7',')',0};
1747 cpinfo->CodePage = CP_UTF7;
1748 cpinfo->UnicodeDefaultChar = 0x3f;
1749 strcpyW(cpinfo->CodePageName, utf7);
1750 break;
1753 case CP_UTF8:
1755 static const WCHAR utf8[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','8',')',0};
1757 cpinfo->CodePage = CP_UTF8;
1758 cpinfo->UnicodeDefaultChar = 0x3f;
1759 strcpyW(cpinfo->CodePageName, utf8);
1760 break;
1763 default:
1765 const union cptable *table = get_codepage_table( codepage );
1767 cpinfo->CodePage = table->info.codepage;
1768 cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
1769 MultiByteToWideChar( CP_ACP, 0, table->info.name, -1, cpinfo->CodePageName,
1770 sizeof(cpinfo->CodePageName)/sizeof(WCHAR));
1771 break;
1774 return TRUE;
1777 /***********************************************************************
1778 * EnumSystemCodePagesA (KERNEL32.@)
1780 * Call a user defined function for every code page installed on the system.
1782 * PARAMS
1783 * lpfnCodePageEnum [I] User CODEPAGE_ENUMPROC to call with each found code page
1784 * flags [I] Reserved, set to 0.
1786 * RETURNS
1787 * TRUE, If all code pages have been enumerated, or
1788 * FALSE if lpfnCodePageEnum returned FALSE to stop the enumeration.
1790 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
1792 const union cptable *table;
1793 char buffer[10];
1794 int index = 0;
1796 for (;;)
1798 if (!(table = wine_cp_enum_table( index++ ))) break;
1799 sprintf( buffer, "%d", table->info.codepage );
1800 if (!lpfnCodePageEnum( buffer )) break;
1802 return TRUE;
1806 /***********************************************************************
1807 * EnumSystemCodePagesW (KERNEL32.@)
1809 * See EnumSystemCodePagesA.
1811 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
1813 const union cptable *table;
1814 WCHAR buffer[10], *p;
1815 int page, index = 0;
1817 for (;;)
1819 if (!(table = wine_cp_enum_table( index++ ))) break;
1820 p = buffer + sizeof(buffer)/sizeof(WCHAR);
1821 *--p = 0;
1822 page = table->info.codepage;
1825 *--p = '0' + (page % 10);
1826 page /= 10;
1827 } while( page );
1828 if (!lpfnCodePageEnum( p )) break;
1830 return TRUE;
1834 /***********************************************************************
1835 * MultiByteToWideChar (KERNEL32.@)
1837 * Convert a multibyte character string into a Unicode string.
1839 * PARAMS
1840 * page [I] Codepage character set to convert from
1841 * flags [I] Character mapping flags
1842 * src [I] Source string buffer
1843 * srclen [I] Length of src (in bytes), or -1 if src is NUL terminated
1844 * dst [O] Destination buffer
1845 * dstlen [I] Length of dst (in WCHARs), or 0 to compute the required length
1847 * RETURNS
1848 * Success: If dstlen > 0, the number of characters written to dst.
1849 * If dstlen == 0, the number of characters needed to perform the
1850 * conversion. In both cases the count includes the terminating NUL.
1851 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1852 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1853 * and dstlen != 0; ERROR_INVALID_PARAMETER, if an invalid parameter
1854 * is passed, and ERROR_NO_UNICODE_TRANSLATION if no translation is
1855 * possible for src.
1857 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
1858 LPWSTR dst, INT dstlen )
1860 const union cptable *table;
1861 int ret;
1863 if (!src || (!dst && dstlen))
1865 SetLastError( ERROR_INVALID_PARAMETER );
1866 return 0;
1869 if (srclen < 0) srclen = strlen(src) + 1;
1871 switch(page)
1873 case CP_SYMBOL:
1874 if( flags)
1876 SetLastError( ERROR_INVALID_PARAMETER );
1877 return 0;
1879 ret = wine_cpsymbol_mbstowcs( src, srclen, dst, dstlen );
1880 break;
1881 case CP_UTF7:
1882 FIXME("UTF-7 not supported\n");
1883 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1884 return 0;
1885 case CP_UNIXCP:
1886 if (unix_cptable)
1888 ret = wine_cp_mbstowcs( unix_cptable, flags, src, srclen, dst, dstlen );
1889 break;
1891 #ifdef __APPLE__
1892 flags |= MB_COMPOSITE; /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
1893 #endif
1894 /* fall through */
1895 case CP_UTF8:
1896 ret = wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
1897 break;
1898 default:
1899 if (!(table = get_codepage_table( page )))
1901 SetLastError( ERROR_INVALID_PARAMETER );
1902 return 0;
1904 ret = wine_cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
1905 break;
1908 if (ret < 0)
1910 switch(ret)
1912 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1913 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1915 ret = 0;
1917 TRACE("cp %d %s -> %s, ret = %d\n",
1918 page, debugstr_an(src, srclen), debugstr_wn(dst, ret), ret);
1919 return ret;
1923 /***********************************************************************
1924 * WideCharToMultiByte (KERNEL32.@)
1926 * Convert a Unicode character string into a multibyte string.
1928 * PARAMS
1929 * page [I] Code page character set to convert to
1930 * flags [I] Mapping Flags (MB_ constants from "winnls.h").
1931 * src [I] Source string buffer
1932 * srclen [I] Length of src (in WCHARs), or -1 if src is NUL terminated
1933 * dst [O] Destination buffer
1934 * dstlen [I] Length of dst (in bytes), or 0 to compute the required length
1935 * defchar [I] Default character to use for conversion if no exact
1936 * conversion can be made
1937 * used [O] Set if default character was used in the conversion
1939 * RETURNS
1940 * Success: If dstlen > 0, the number of characters written to dst.
1941 * If dstlen == 0, number of characters needed to perform the
1942 * conversion. In both cases the count includes the terminating NUL.
1943 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1944 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1945 * and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
1946 * parameter was given.
1948 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
1949 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
1951 const union cptable *table;
1952 int ret, used_tmp;
1954 if (!src || (!dst && dstlen))
1956 SetLastError( ERROR_INVALID_PARAMETER );
1957 return 0;
1960 if (srclen < 0) srclen = strlenW(src) + 1;
1962 switch(page)
1964 case CP_SYMBOL:
1965 if( flags || defchar || used)
1967 SetLastError( ERROR_INVALID_PARAMETER );
1968 return 0;
1970 ret = wine_cpsymbol_wcstombs( src, srclen, dst, dstlen );
1971 break;
1972 case CP_UTF7:
1973 FIXME("UTF-7 not supported\n");
1974 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1975 return 0;
1976 case CP_UNIXCP:
1977 if (unix_cptable)
1979 ret = wine_cp_wcstombs( unix_cptable, flags, src, srclen, dst, dstlen,
1980 defchar, used ? &used_tmp : NULL );
1981 break;
1983 /* fall through */
1984 case CP_UTF8:
1985 if (used) *used = FALSE; /* all chars are valid for UTF-8 */
1986 ret = wine_utf8_wcstombs( flags, src, srclen, dst, dstlen );
1987 break;
1988 default:
1989 if (!(table = get_codepage_table( page )))
1991 SetLastError( ERROR_INVALID_PARAMETER );
1992 return 0;
1994 ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
1995 defchar, used ? &used_tmp : NULL );
1996 if (used) *used = used_tmp;
1997 break;
2000 if (ret < 0)
2002 switch(ret)
2004 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
2005 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
2007 ret = 0;
2009 TRACE("cp %d %s -> %s, ret = %d\n",
2010 page, debugstr_wn(src, srclen), debugstr_an(dst, ret), ret);
2011 return ret;
2015 /***********************************************************************
2016 * GetThreadLocale (KERNEL32.@)
2018 * Get the current threads locale.
2020 * PARAMS
2021 * None.
2023 * RETURNS
2024 * The LCID currently associated with the calling thread.
2026 LCID WINAPI GetThreadLocale(void)
2028 LCID ret = NtCurrentTeb()->CurrentLocale;
2029 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
2030 return ret;
2033 /**********************************************************************
2034 * SetThreadLocale (KERNEL32.@)
2036 * Set the current threads locale.
2038 * PARAMS
2039 * lcid [I] LCID of the locale to set
2041 * RETURNS
2042 * Success: TRUE. The threads locale is set to lcid.
2043 * Failure: FALSE. Use GetLastError() to determine the cause.
2045 BOOL WINAPI SetThreadLocale( LCID lcid )
2047 TRACE("(0x%04X)\n", lcid);
2049 lcid = ConvertDefaultLocale(lcid);
2051 if (lcid != GetThreadLocale())
2053 if (!IsValidLocale(lcid, LCID_SUPPORTED))
2055 SetLastError(ERROR_INVALID_PARAMETER);
2056 return FALSE;
2059 NtCurrentTeb()->CurrentLocale = lcid;
2060 kernel_get_thread_data()->code_page = get_lcid_codepage( lcid );
2062 return TRUE;
2065 /**********************************************************************
2066 * SetThreadUILanguage (KERNEL32.@)
2068 * Set the current threads UI language.
2070 * PARAMS
2071 * langid [I] LANGID of the language to set, or 0 to use
2072 * the available language which is best supported
2073 * for console applications
2075 * RETURNS
2076 * Success: The return value is the same as the input value.
2077 * Failure: The return value differs from the input value.
2078 * Use GetLastError() to determine the cause.
2080 LANGID WINAPI SetThreadUILanguage( LANGID langid )
2082 TRACE("(0x%04x) stub - returning success\n", langid);
2083 return langid;
2086 /******************************************************************************
2087 * ConvertDefaultLocale (KERNEL32.@)
2089 * Convert a default locale identifier into a real identifier.
2091 * PARAMS
2092 * lcid [I] LCID identifier of the locale to convert
2094 * RETURNS
2095 * lcid unchanged, if not a default locale or its sublanguage is
2096 * not SUBLANG_NEUTRAL.
2097 * GetSystemDefaultLCID(), if lcid == LOCALE_SYSTEM_DEFAULT.
2098 * GetUserDefaultLCID(), if lcid == LOCALE_USER_DEFAULT or LOCALE_NEUTRAL.
2099 * Otherwise, lcid with sublanguage changed to SUBLANG_DEFAULT.
2101 LCID WINAPI ConvertDefaultLocale( LCID lcid )
2103 LANGID langid;
2105 switch (lcid)
2107 case LOCALE_SYSTEM_DEFAULT:
2108 lcid = GetSystemDefaultLCID();
2109 break;
2110 case LOCALE_USER_DEFAULT:
2111 case LOCALE_NEUTRAL:
2112 lcid = GetUserDefaultLCID();
2113 break;
2114 default:
2115 /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
2116 langid = LANGIDFROMLCID(lcid);
2117 if (SUBLANGID(langid) == SUBLANG_NEUTRAL)
2119 langid = MAKELANGID(PRIMARYLANGID(langid), SUBLANG_DEFAULT);
2120 lcid = MAKELCID(langid, SORTIDFROMLCID(lcid));
2123 return lcid;
2127 /******************************************************************************
2128 * IsValidLocale (KERNEL32.@)
2130 * Determine if a locale is valid.
2132 * PARAMS
2133 * lcid [I] LCID of the locale to check
2134 * flags [I] LCID_SUPPORTED = Valid, LCID_INSTALLED = Valid and installed on the system
2136 * RETURNS
2137 * TRUE, if lcid is valid,
2138 * FALSE, otherwise.
2140 * NOTES
2141 * Wine does not currently make the distinction between supported and installed. All
2142 * languages supported are installed by default.
2144 BOOL WINAPI IsValidLocale( LCID lcid, DWORD flags )
2146 /* check if language is registered in the kernel32 resources */
2147 return FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
2148 (LPCWSTR)LOCALE_ILANGUAGE, LANGIDFROMLCID(lcid)) != 0;
2152 static BOOL CALLBACK enum_lang_proc_a( HMODULE hModule, LPCSTR type,
2153 LPCSTR name, WORD LangID, LONG_PTR lParam )
2155 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
2156 char buf[20];
2158 sprintf(buf, "%08x", (UINT)LangID);
2159 return lpfnLocaleEnum( buf );
2162 static BOOL CALLBACK enum_lang_proc_w( HMODULE hModule, LPCWSTR type,
2163 LPCWSTR name, WORD LangID, LONG_PTR lParam )
2165 static const WCHAR formatW[] = {'%','0','8','x',0};
2166 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
2167 WCHAR buf[20];
2168 sprintfW( buf, formatW, (UINT)LangID );
2169 return lpfnLocaleEnum( buf );
2172 /******************************************************************************
2173 * EnumSystemLocalesA (KERNEL32.@)
2175 * Call a users function for each locale available on the system.
2177 * PARAMS
2178 * lpfnLocaleEnum [I] Callback function to call for each locale
2179 * dwFlags [I] LOCALE_SUPPORTED=All supported, LOCALE_INSTALLED=Installed only
2181 * RETURNS
2182 * Success: TRUE.
2183 * Failure: FALSE. Use GetLastError() to determine the cause.
2185 BOOL WINAPI EnumSystemLocalesA( LOCALE_ENUMPROCA lpfnLocaleEnum, DWORD dwFlags )
2187 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
2188 EnumResourceLanguagesA( kernel32_handle, (LPSTR)RT_STRING,
2189 (LPCSTR)LOCALE_ILANGUAGE, enum_lang_proc_a,
2190 (LONG_PTR)lpfnLocaleEnum);
2191 return TRUE;
2195 /******************************************************************************
2196 * EnumSystemLocalesW (KERNEL32.@)
2198 * See EnumSystemLocalesA.
2200 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum, DWORD dwFlags )
2202 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
2203 EnumResourceLanguagesW( kernel32_handle, (LPWSTR)RT_STRING,
2204 (LPCWSTR)LOCALE_ILANGUAGE, enum_lang_proc_w,
2205 (LONG_PTR)lpfnLocaleEnum);
2206 return TRUE;
2210 /***********************************************************************
2211 * VerLanguageNameA (KERNEL32.@)
2213 * Get the name of a language.
2215 * PARAMS
2216 * wLang [I] LANGID of the language
2217 * szLang [O] Destination for the language name
2219 * RETURNS
2220 * Success: The size of the language name. If szLang is non-NULL, it is filled
2221 * with the name.
2222 * Failure: 0. Use GetLastError() to determine the cause.
2225 DWORD WINAPI VerLanguageNameA( DWORD wLang, LPSTR szLang, DWORD nSize )
2227 return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
2231 /***********************************************************************
2232 * VerLanguageNameW (KERNEL32.@)
2234 * See VerLanguageNameA.
2236 DWORD WINAPI VerLanguageNameW( DWORD wLang, LPWSTR szLang, DWORD nSize )
2238 return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
2242 /******************************************************************************
2243 * GetStringTypeW (KERNEL32.@)
2245 * See GetStringTypeA.
2247 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
2249 if (count == -1) count = strlenW(src) + 1;
2250 switch(type)
2252 case CT_CTYPE1:
2253 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
2254 break;
2255 case CT_CTYPE2:
2256 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
2257 break;
2258 case CT_CTYPE3:
2260 WARN("CT_CTYPE3: semi-stub.\n");
2261 while (count--)
2263 int c = *src;
2264 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
2266 type1 = get_char_typeW( *src++ ) & 0xfff;
2267 /* try to construct type3 from type1 */
2268 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
2269 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
2270 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
2271 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
2272 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
2273 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
2274 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
2276 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
2277 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
2278 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
2279 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
2280 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
2281 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
2282 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
2283 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
2285 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
2286 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
2287 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
2288 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
2289 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
2290 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
2291 *chartype++ = type3;
2293 break;
2295 default:
2296 SetLastError( ERROR_INVALID_PARAMETER );
2297 return FALSE;
2299 return TRUE;
2303 /******************************************************************************
2304 * GetStringTypeExW (KERNEL32.@)
2306 * See GetStringTypeExA.
2308 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
2310 /* locale is ignored for Unicode */
2311 return GetStringTypeW( type, src, count, chartype );
2315 /******************************************************************************
2316 * GetStringTypeA (KERNEL32.@)
2318 * Get characteristics of the characters making up a string.
2320 * PARAMS
2321 * locale [I] Locale Id for the string
2322 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
2323 * src [I] String to analyse
2324 * count [I] Length of src in chars, or -1 if src is NUL terminated
2325 * chartype [O] Destination for the calculated characteristics
2327 * RETURNS
2328 * Success: TRUE. chartype is filled with the requested characteristics of each char
2329 * in src.
2330 * Failure: FALSE. Use GetLastError() to determine the cause.
2332 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
2334 UINT cp;
2335 INT countW;
2336 LPWSTR srcW;
2337 BOOL ret = FALSE;
2339 if(count == -1) count = strlen(src) + 1;
2341 if (!(cp = get_lcid_codepage( locale )))
2343 FIXME("For locale %04x using current ANSI code page\n", locale);
2344 cp = GetACP();
2347 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
2348 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
2350 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
2352 * NOTE: the target buffer has 1 word for each CHARACTER in the source
2353 * string, with multibyte characters there maybe be more bytes in count
2354 * than character space in the buffer!
2356 ret = GetStringTypeW(type, srcW, countW, chartype);
2357 HeapFree(GetProcessHeap(), 0, srcW);
2359 return ret;
2362 /******************************************************************************
2363 * GetStringTypeExA (KERNEL32.@)
2365 * Get characteristics of the characters making up a string.
2367 * PARAMS
2368 * locale [I] Locale Id for the string
2369 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
2370 * src [I] String to analyse
2371 * count [I] Length of src in chars, or -1 if src is NUL terminated
2372 * chartype [O] Destination for the calculated characteristics
2374 * RETURNS
2375 * Success: TRUE. chartype is filled with the requested characteristics of each char
2376 * in src.
2377 * Failure: FALSE. Use GetLastError() to determine the cause.
2379 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
2381 return GetStringTypeA(locale, type, src, count, chartype);
2385 /*************************************************************************
2386 * LCMapStringW (KERNEL32.@)
2388 * See LCMapStringA.
2390 INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
2391 LPWSTR dst, INT dstlen)
2393 LPWSTR dst_ptr;
2395 if (!src || !srclen || dstlen < 0)
2397 SetLastError(ERROR_INVALID_PARAMETER);
2398 return 0;
2401 /* mutually exclusive flags */
2402 if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
2403 (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
2404 (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
2405 (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE))
2407 SetLastError(ERROR_INVALID_FLAGS);
2408 return 0;
2411 if (!dstlen) dst = NULL;
2413 lcid = ConvertDefaultLocale(lcid);
2415 if (flags & LCMAP_SORTKEY)
2417 INT ret;
2418 if (src == dst)
2420 SetLastError(ERROR_INVALID_FLAGS);
2421 return 0;
2424 if (srclen < 0) srclen = strlenW(src);
2426 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
2427 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
2429 ret = wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
2430 if (ret == 0)
2431 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2432 else
2433 ret++;
2434 return ret;
2437 /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
2438 if (flags & SORT_STRINGSORT)
2440 SetLastError(ERROR_INVALID_FLAGS);
2441 return 0;
2444 if (srclen < 0) srclen = strlenW(src) + 1;
2446 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
2447 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
2449 if (!dst) /* return required string length */
2451 INT len;
2453 for (len = 0; srclen; src++, srclen--)
2455 WCHAR wch = *src;
2456 /* tests show that win2k just ignores NORM_IGNORENONSPACE,
2457 * and skips white space and punctuation characters for
2458 * NORM_IGNORESYMBOLS.
2460 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2461 continue;
2462 len++;
2464 return len;
2467 if (flags & LCMAP_UPPERCASE)
2469 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2471 WCHAR wch = *src;
2472 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2473 continue;
2474 *dst_ptr++ = toupperW(wch);
2475 dstlen--;
2478 else if (flags & LCMAP_LOWERCASE)
2480 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2482 WCHAR wch = *src;
2483 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2484 continue;
2485 *dst_ptr++ = tolowerW(wch);
2486 dstlen--;
2489 else
2491 if (src == dst)
2493 SetLastError(ERROR_INVALID_FLAGS);
2494 return 0;
2496 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2498 WCHAR wch = *src;
2499 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2500 continue;
2501 *dst_ptr++ = wch;
2502 dstlen--;
2506 if (srclen)
2508 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2509 return 0;
2512 return dst_ptr - dst;
2515 /*************************************************************************
2516 * LCMapStringA (KERNEL32.@)
2518 * Map characters in a locale sensitive string.
2520 * PARAMS
2521 * lcid [I] LCID for the conversion.
2522 * flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
2523 * src [I] String to map
2524 * srclen [I] Length of src in chars, or -1 if src is NUL terminated
2525 * dst [O] Destination for mapped string
2526 * dstlen [I] Length of dst in characters
2528 * RETURNS
2529 * Success: The length of the mapped string in dst, including the NUL terminator.
2530 * Failure: 0. Use GetLastError() to determine the cause.
2532 INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
2533 LPSTR dst, INT dstlen)
2535 WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
2536 LPWSTR srcW, dstW;
2537 INT ret = 0, srclenW, dstlenW;
2538 UINT locale_cp = CP_ACP;
2540 if (!src || !srclen || dstlen < 0)
2542 SetLastError(ERROR_INVALID_PARAMETER);
2543 return 0;
2546 if (!(flags & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
2548 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
2549 if (srclenW)
2550 srcW = bufW;
2551 else
2553 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
2554 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2555 if (!srcW)
2557 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2558 return 0;
2560 MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
2563 if (flags & LCMAP_SORTKEY)
2565 if (src == dst)
2567 SetLastError(ERROR_INVALID_FLAGS);
2568 goto map_string_exit;
2570 ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
2571 if (ret == 0)
2572 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2573 else
2574 ret++;
2575 goto map_string_exit;
2578 if (flags & SORT_STRINGSORT)
2580 SetLastError(ERROR_INVALID_FLAGS);
2581 goto map_string_exit;
2584 dstlenW = LCMapStringW(lcid, flags, srcW, srclenW, NULL, 0);
2585 if (!dstlenW)
2586 goto map_string_exit;
2588 dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
2589 if (!dstW)
2591 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2592 goto map_string_exit;
2595 LCMapStringW(lcid, flags, srcW, srclenW, dstW, dstlenW);
2596 ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
2597 HeapFree(GetProcessHeap(), 0, dstW);
2599 map_string_exit:
2600 if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
2601 return ret;
2604 /*************************************************************************
2605 * FoldStringA (KERNEL32.@)
2607 * Map characters in a string.
2609 * PARAMS
2610 * dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
2611 * src [I] String to map
2612 * srclen [I] Length of src, or -1 if src is NUL terminated
2613 * dst [O] Destination for mapped string
2614 * dstlen [I] Length of dst, or 0 to find the required length for the mapped string
2616 * RETURNS
2617 * Success: The length of the string written to dst, including the terminating NUL. If
2618 * dstlen is 0, the value returned is the same, but nothing is written to dst,
2619 * and dst may be NULL.
2620 * Failure: 0. Use GetLastError() to determine the cause.
2622 INT WINAPI FoldStringA(DWORD dwFlags, LPCSTR src, INT srclen,
2623 LPSTR dst, INT dstlen)
2625 INT ret = 0, srclenW = 0;
2626 WCHAR *srcW = NULL, *dstW = NULL;
2628 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2630 SetLastError(ERROR_INVALID_PARAMETER);
2631 return 0;
2634 srclenW = MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2635 src, srclen, NULL, 0);
2636 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2638 if (!srcW)
2640 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2641 goto FoldStringA_exit;
2644 MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2645 src, srclen, srcW, srclenW);
2647 dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
2649 ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
2650 if (ret && dstlen)
2652 dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
2654 if (!dstW)
2656 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2657 goto FoldStringA_exit;
2660 ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
2661 if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
2663 ret = 0;
2664 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2668 HeapFree(GetProcessHeap(), 0, dstW);
2670 FoldStringA_exit:
2671 HeapFree(GetProcessHeap(), 0, srcW);
2672 return ret;
2675 /*************************************************************************
2676 * FoldStringW (KERNEL32.@)
2678 * See FoldStringA.
2680 INT WINAPI FoldStringW(DWORD dwFlags, LPCWSTR src, INT srclen,
2681 LPWSTR dst, INT dstlen)
2683 int ret;
2685 switch (dwFlags & (MAP_COMPOSITE|MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES))
2687 case 0:
2688 if (dwFlags)
2689 break;
2690 /* Fall through for dwFlags == 0 */
2691 case MAP_PRECOMPOSED|MAP_COMPOSITE:
2692 case MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES:
2693 case MAP_COMPOSITE|MAP_EXPAND_LIGATURES:
2694 SetLastError(ERROR_INVALID_FLAGS);
2695 return 0;
2698 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2700 SetLastError(ERROR_INVALID_PARAMETER);
2701 return 0;
2704 ret = wine_fold_string(dwFlags, src, srclen, dst, dstlen);
2705 if (!ret)
2706 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2707 return ret;
2710 /******************************************************************************
2711 * CompareStringW (KERNEL32.@)
2713 * See CompareStringA.
2715 INT WINAPI CompareStringW(LCID lcid, DWORD style,
2716 LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
2718 INT ret;
2720 if (!str1 || !str2)
2722 SetLastError(ERROR_INVALID_PARAMETER);
2723 return 0;
2726 if( style & ~(NORM_IGNORECASE|NORM_IGNORENONSPACE|NORM_IGNORESYMBOLS|
2727 SORT_STRINGSORT|NORM_IGNOREKANATYPE|NORM_IGNOREWIDTH|LOCALE_USE_CP_ACP|0x10000000) )
2729 SetLastError(ERROR_INVALID_FLAGS);
2730 return 0;
2733 /* this style is related to diacritics in Arabic, Japanese, and Hebrew */
2734 if (style & 0x10000000)
2735 WARN("Ignoring unknown style 0x10000000\n");
2737 if (len1 < 0) len1 = strlenW(str1);
2738 if (len2 < 0) len2 = strlenW(str2);
2740 ret = wine_compare_string(style, str1, len1, str2, len2);
2742 if (ret) /* need to translate result */
2743 return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
2744 return CSTR_EQUAL;
2747 /******************************************************************************
2748 * CompareStringA (KERNEL32.@)
2750 * Compare two locale sensitive strings.
2752 * PARAMS
2753 * lcid [I] LCID for the comparison
2754 * style [I] Flags for the comparison (NORM_ constants from "winnls.h").
2755 * str1 [I] First string to compare
2756 * len1 [I] Length of str1, or -1 if str1 is NUL terminated
2757 * str2 [I] Second string to compare
2758 * len2 [I] Length of str2, or -1 if str2 is NUL terminated
2760 * RETURNS
2761 * Success: CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN depending on whether
2762 * str2 is less than, equal to or greater than str1 respectively.
2763 * Failure: FALSE. Use GetLastError() to determine the cause.
2765 INT WINAPI CompareStringA(LCID lcid, DWORD style,
2766 LPCSTR str1, INT len1, LPCSTR str2, INT len2)
2768 WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
2769 WCHAR *buf2W = buf1W + 130;
2770 LPWSTR str1W, str2W;
2771 INT len1W, len2W, ret;
2772 UINT locale_cp = CP_ACP;
2774 if (!str1 || !str2)
2776 SetLastError(ERROR_INVALID_PARAMETER);
2777 return 0;
2779 if (len1 < 0) len1 = strlen(str1);
2780 if (len2 < 0) len2 = strlen(str2);
2782 if (!(style & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
2784 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 130);
2785 if (len1W)
2786 str1W = buf1W;
2787 else
2789 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
2790 str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
2791 if (!str1W)
2793 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2794 return 0;
2796 MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
2798 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 130);
2799 if (len2W)
2800 str2W = buf2W;
2801 else
2803 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
2804 str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
2805 if (!str2W)
2807 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2808 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2809 return 0;
2811 MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
2814 ret = CompareStringW(lcid, style, str1W, len1W, str2W, len2W);
2816 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2817 if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
2818 return ret;
2821 /*************************************************************************
2822 * lstrcmp (KERNEL32.@)
2823 * lstrcmpA (KERNEL32.@)
2825 * Compare two strings using the current thread locale.
2827 * PARAMS
2828 * str1 [I] First string to compare
2829 * str2 [I] Second string to compare
2831 * RETURNS
2832 * Success: A number less than, equal to or greater than 0 depending on whether
2833 * str2 is less than, equal to or greater than str1 respectively.
2834 * Failure: FALSE. Use GetLastError() to determine the cause.
2836 int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
2838 int ret;
2840 if ((str1 == NULL) && (str2 == NULL)) return 0;
2841 if (str1 == NULL) return -1;
2842 if (str2 == NULL) return 1;
2844 ret = CompareStringA(GetThreadLocale(), LOCALE_USE_CP_ACP, str1, -1, str2, -1);
2845 if (ret) ret -= 2;
2847 return ret;
2850 /*************************************************************************
2851 * lstrcmpi (KERNEL32.@)
2852 * lstrcmpiA (KERNEL32.@)
2854 * Compare two strings using the current thread locale, ignoring case.
2856 * PARAMS
2857 * str1 [I] First string to compare
2858 * str2 [I] Second string to compare
2860 * RETURNS
2861 * Success: A number less than, equal to or greater than 0 depending on whether
2862 * str2 is less than, equal to or greater than str1 respectively.
2863 * Failure: FALSE. Use GetLastError() to determine the cause.
2865 int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
2867 int ret;
2869 if ((str1 == NULL) && (str2 == NULL)) return 0;
2870 if (str1 == NULL) return -1;
2871 if (str2 == NULL) return 1;
2873 ret = CompareStringA(GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, str1, -1, str2, -1);
2874 if (ret) ret -= 2;
2876 return ret;
2879 /*************************************************************************
2880 * lstrcmpW (KERNEL32.@)
2882 * See lstrcmpA.
2884 int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
2886 int ret;
2888 if ((str1 == NULL) && (str2 == NULL)) return 0;
2889 if (str1 == NULL) return -1;
2890 if (str2 == NULL) return 1;
2892 ret = CompareStringW(GetThreadLocale(), 0, str1, -1, str2, -1);
2893 if (ret) ret -= 2;
2895 return ret;
2898 /*************************************************************************
2899 * lstrcmpiW (KERNEL32.@)
2901 * See lstrcmpiA.
2903 int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
2905 int ret;
2907 if ((str1 == NULL) && (str2 == NULL)) return 0;
2908 if (str1 == NULL) return -1;
2909 if (str2 == NULL) return 1;
2911 ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2912 if (ret) ret -= 2;
2914 return ret;
2917 /******************************************************************************
2918 * LOCALE_Init
2920 void LOCALE_Init(void)
2922 extern void CDECL __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp,
2923 const union cptable *unix_cp );
2925 UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp;
2927 #ifdef __APPLE__
2928 /* MacOS doesn't set the locale environment variables so we have to do it ourselves */
2929 CFArrayRef preferred_locales, all_locales;
2930 CFStringRef user_language_string_ref = NULL;
2931 char user_locale[50];
2932 char* p;
2934 CFLocaleRef user_locale_ref = CFLocaleCopyCurrent();
2935 CFStringRef user_locale_string_ref = CFLocaleGetIdentifier( user_locale_ref );
2937 CFStringGetCString( user_locale_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 );
2938 CFRelease( user_locale_ref );
2939 /* Strip modifiers because setlocale() can't parse them. */
2940 if ((p = strchr( user_locale, '@' ))) *p = 0;
2941 if (!strchr( user_locale, '.' )) strcat( user_locale, ".UTF-8" );
2942 unix_cp = CP_UTF8; /* default to utf-8 even if we don't get a valid locale */
2943 setenv( "LANG", user_locale, 0 );
2944 TRACE( "setting locale to '%s'\n", user_locale );
2946 /* We still want to set the retrieve the preferred language as chosen in
2947 System Preferences.app, because it can differ from CFLocaleCopyCurrent().
2949 all_locales = CFLocaleCopyAvailableLocaleIdentifiers();
2950 preferred_locales = CFBundleCopyLocalizationsForPreferences( all_locales, NULL );
2951 if (preferred_locales && CFArrayGetCount( preferred_locales ))
2952 user_language_string_ref = CFArrayGetValueAtIndex( preferred_locales, 0 );
2953 CFRelease( all_locales );
2954 #endif /* __APPLE__ */
2956 setlocale( LC_ALL, "" );
2958 unix_cp = setup_unix_locales();
2959 if (!lcid_LC_MESSAGES) lcid_LC_MESSAGES = lcid_LC_CTYPE;
2961 #ifdef __APPLE__
2962 /* Override lcid_LC_MESSAGES with user_language if LC_MESSAGES is set to default */
2963 if (lcid_LC_MESSAGES == lcid_LC_CTYPE && user_language_string_ref)
2965 struct locale_name locale_name;
2966 WCHAR buffer[128];
2967 CFStringGetCString( user_language_string_ref, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 );
2968 strcpynAtoW( buffer, user_locale, sizeof(buffer)/sizeof(WCHAR) );
2969 parse_locale_name( buffer, &locale_name );
2970 lcid_LC_MESSAGES = locale_name.lcid;
2971 TRACE( "setting lcid_LC_MESSAGES to '%s'\n", user_locale );
2973 if (preferred_locales)
2974 CFRelease( preferred_locales );
2975 #endif
2977 NtSetDefaultUILanguage( LANGIDFROMLCID(lcid_LC_MESSAGES) );
2978 NtSetDefaultLocale( TRUE, lcid_LC_MESSAGES );
2979 NtSetDefaultLocale( FALSE, lcid_LC_CTYPE );
2981 ansi_cp = get_lcid_codepage( LOCALE_USER_DEFAULT );
2982 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
2983 (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
2984 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
2985 (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
2986 if (!unix_cp)
2987 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
2988 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) );
2990 if (!(ansi_cptable = wine_cp_get_table( ansi_cp )))
2991 ansi_cptable = wine_cp_get_table( 1252 );
2992 if (!(oem_cptable = wine_cp_get_table( oem_cp )))
2993 oem_cptable = wine_cp_get_table( 437 );
2994 if (!(mac_cptable = wine_cp_get_table( mac_cp )))
2995 mac_cptable = wine_cp_get_table( 10000 );
2996 if (unix_cp != CP_UTF8)
2998 if (!(unix_cptable = wine_cp_get_table( unix_cp )))
2999 unix_cptable = wine_cp_get_table( 28591 );
3002 __wine_init_codepages( ansi_cptable, oem_cptable, unix_cptable );
3004 TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
3005 ansi_cptable->info.codepage, oem_cptable->info.codepage,
3006 mac_cptable->info.codepage, unix_cp );
3008 setlocale(LC_NUMERIC, "C"); /* FIXME: oleaut32 depends on this */
3011 static HANDLE NLS_RegOpenKey(HANDLE hRootKey, LPCWSTR szKeyName)
3013 UNICODE_STRING keyName;
3014 OBJECT_ATTRIBUTES attr;
3015 HANDLE hkey;
3017 RtlInitUnicodeString( &keyName, szKeyName );
3018 InitializeObjectAttributes(&attr, &keyName, 0, hRootKey, NULL);
3020 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS)
3021 hkey = 0;
3023 return hkey;
3026 static BOOL NLS_RegEnumSubKey(HANDLE hKey, UINT ulIndex, LPWSTR szKeyName,
3027 ULONG keyNameSize)
3029 BYTE buffer[80];
3030 KEY_BASIC_INFORMATION *info = (KEY_BASIC_INFORMATION *)buffer;
3031 DWORD dwLen;
3033 if (NtEnumerateKey( hKey, ulIndex, KeyBasicInformation, buffer,
3034 sizeof(buffer), &dwLen) != STATUS_SUCCESS ||
3035 info->NameLength > keyNameSize)
3037 return FALSE;
3040 TRACE("info->Name %s info->NameLength %d\n", debugstr_w(info->Name), info->NameLength);
3042 memcpy( szKeyName, info->Name, info->NameLength);
3043 szKeyName[info->NameLength / sizeof(WCHAR)] = '\0';
3045 TRACE("returning %s\n", debugstr_w(szKeyName));
3046 return TRUE;
3049 static BOOL NLS_RegEnumValue(HANDLE hKey, UINT ulIndex,
3050 LPWSTR szValueName, ULONG valueNameSize,
3051 LPWSTR szValueData, ULONG valueDataSize)
3053 BYTE buffer[80];
3054 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
3055 DWORD dwLen;
3057 if (NtEnumerateValueKey( hKey, ulIndex, KeyValueFullInformation,
3058 buffer, sizeof(buffer), &dwLen ) != STATUS_SUCCESS ||
3059 info->NameLength > valueNameSize ||
3060 info->DataLength > valueDataSize)
3062 return FALSE;
3065 TRACE("info->Name %s info->DataLength %d\n", debugstr_w(info->Name), info->DataLength);
3067 memcpy( szValueName, info->Name, info->NameLength);
3068 szValueName[info->NameLength / sizeof(WCHAR)] = '\0';
3069 memcpy( szValueData, buffer + info->DataOffset, info->DataLength );
3070 szValueData[info->DataLength / sizeof(WCHAR)] = '\0';
3072 TRACE("returning %s %s\n", debugstr_w(szValueName), debugstr_w(szValueData));
3073 return TRUE;
3076 static BOOL NLS_RegGetDword(HANDLE hKey, LPCWSTR szValueName, DWORD *lpVal)
3078 BYTE buffer[128];
3079 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
3080 DWORD dwSize = sizeof(buffer);
3081 UNICODE_STRING valueName;
3083 RtlInitUnicodeString( &valueName, szValueName );
3085 TRACE("%p, %s\n", hKey, debugstr_w(szValueName));
3086 if (NtQueryValueKey( hKey, &valueName, KeyValuePartialInformation,
3087 buffer, dwSize, &dwSize ) == STATUS_SUCCESS &&
3088 info->DataLength == sizeof(DWORD))
3090 memcpy(lpVal, info->Data, sizeof(DWORD));
3091 return TRUE;
3094 return FALSE;
3097 static BOOL NLS_GetLanguageGroupName(LGRPID lgrpid, LPWSTR szName, ULONG nameSize)
3099 LANGID langId;
3100 LPCWSTR szResourceName = MAKEINTRESOURCEW(((lgrpid + 0x2000) >> 4) + 1);
3101 HRSRC hResource;
3102 BOOL bRet = FALSE;
3104 /* FIXME: Is it correct to use the system default langid? */
3105 langId = GetSystemDefaultLangID();
3107 if (SUBLANGID(langId) == SUBLANG_NEUTRAL)
3108 langId = MAKELANGID( PRIMARYLANGID(langId), SUBLANG_DEFAULT );
3110 hResource = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING, szResourceName, langId );
3112 if (hResource)
3114 HGLOBAL hResDir = LoadResource( kernel32_handle, hResource );
3116 if (hResDir)
3118 ULONG iResourceIndex = lgrpid & 0xf;
3119 LPCWSTR lpResEntry = LockResource( hResDir );
3120 ULONG i;
3122 for (i = 0; i < iResourceIndex; i++)
3123 lpResEntry += *lpResEntry + 1;
3125 if (*lpResEntry < nameSize)
3127 memcpy( szName, lpResEntry + 1, *lpResEntry * sizeof(WCHAR) );
3128 szName[*lpResEntry] = '\0';
3129 bRet = TRUE;
3133 FreeResource( hResource );
3135 return bRet;
3138 /* Registry keys for NLS related information */
3140 static const WCHAR szCountryListName[] = {
3141 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
3142 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
3143 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3144 'T','e','l','e','p','h','o','n','y','\\',
3145 'C','o','u','n','t','r','y',' ','L','i','s','t','\0'
3149 /* Callback function ptrs for EnumSystemLanguageGroupsA/W */
3150 typedef struct
3152 LANGUAGEGROUP_ENUMPROCA procA;
3153 LANGUAGEGROUP_ENUMPROCW procW;
3154 DWORD dwFlags;
3155 LONG_PTR lParam;
3156 } ENUMLANGUAGEGROUP_CALLBACKS;
3158 /* Internal implementation of EnumSystemLanguageGroupsA/W */
3159 static BOOL NLS_EnumSystemLanguageGroups(ENUMLANGUAGEGROUP_CALLBACKS *lpProcs)
3161 WCHAR szNumber[10], szValue[4];
3162 HANDLE hKey;
3163 BOOL bContinue = TRUE;
3164 ULONG ulIndex = 0;
3166 if (!lpProcs)
3168 SetLastError(ERROR_INVALID_PARAMETER);
3169 return FALSE;
3172 switch (lpProcs->dwFlags)
3174 case 0:
3175 /* Default to LGRPID_INSTALLED */
3176 lpProcs->dwFlags = LGRPID_INSTALLED;
3177 /* Fall through... */
3178 case LGRPID_INSTALLED:
3179 case LGRPID_SUPPORTED:
3180 break;
3181 default:
3182 SetLastError(ERROR_INVALID_FLAGS);
3183 return FALSE;
3186 hKey = NLS_RegOpenKey( 0, szLangGroupsKeyName );
3188 if (!hKey)
3189 FIXME("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
3191 while (bContinue)
3193 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
3194 szValue, sizeof(szValue) ))
3196 BOOL bInstalled = szValue[0] == '1' ? TRUE : FALSE;
3197 LGRPID lgrpid = strtoulW( szNumber, NULL, 16 );
3199 TRACE("grpid %s (%sinstalled)\n", debugstr_w(szNumber),
3200 bInstalled ? "" : "not ");
3202 if (lpProcs->dwFlags == LGRPID_SUPPORTED || bInstalled)
3204 WCHAR szGrpName[48];
3206 if (!NLS_GetLanguageGroupName( lgrpid, szGrpName, sizeof(szGrpName) / sizeof(WCHAR) ))
3207 szGrpName[0] = '\0';
3209 if (lpProcs->procW)
3210 bContinue = lpProcs->procW( lgrpid, szNumber, szGrpName, lpProcs->dwFlags,
3211 lpProcs->lParam );
3212 else
3214 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
3215 char szGrpNameA[48];
3217 /* FIXME: MSDN doesn't say which code page the W->A translation uses,
3218 * or whether the language names are ever localised. Assume CP_ACP.
3221 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
3222 WideCharToMultiByte(CP_ACP, 0, szGrpName, -1, szGrpNameA, sizeof(szGrpNameA), 0, 0);
3224 bContinue = lpProcs->procA( lgrpid, szNumberA, szGrpNameA, lpProcs->dwFlags,
3225 lpProcs->lParam );
3229 ulIndex++;
3231 else
3232 bContinue = FALSE;
3234 if (!bContinue)
3235 break;
3238 if (hKey)
3239 NtClose( hKey );
3241 return TRUE;
3244 /******************************************************************************
3245 * EnumSystemLanguageGroupsA (KERNEL32.@)
3247 * Call a users function for each language group available on the system.
3249 * PARAMS
3250 * pLangGrpEnumProc [I] Callback function to call for each language group
3251 * dwFlags [I] LGRPID_SUPPORTED=All Supported, LGRPID_INSTALLED=Installed only
3252 * lParam [I] User parameter to pass to pLangGrpEnumProc
3254 * RETURNS
3255 * Success: TRUE.
3256 * Failure: FALSE. Use GetLastError() to determine the cause.
3258 BOOL WINAPI EnumSystemLanguageGroupsA(LANGUAGEGROUP_ENUMPROCA pLangGrpEnumProc,
3259 DWORD dwFlags, LONG_PTR lParam)
3261 ENUMLANGUAGEGROUP_CALLBACKS procs;
3263 TRACE("(%p,0x%08X,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
3265 procs.procA = pLangGrpEnumProc;
3266 procs.procW = NULL;
3267 procs.dwFlags = dwFlags;
3268 procs.lParam = lParam;
3270 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
3273 /******************************************************************************
3274 * EnumSystemLanguageGroupsW (KERNEL32.@)
3276 * See EnumSystemLanguageGroupsA.
3278 BOOL WINAPI EnumSystemLanguageGroupsW(LANGUAGEGROUP_ENUMPROCW pLangGrpEnumProc,
3279 DWORD dwFlags, LONG_PTR lParam)
3281 ENUMLANGUAGEGROUP_CALLBACKS procs;
3283 TRACE("(%p,0x%08X,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
3285 procs.procA = NULL;
3286 procs.procW = pLangGrpEnumProc;
3287 procs.dwFlags = dwFlags;
3288 procs.lParam = lParam;
3290 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
3293 /******************************************************************************
3294 * IsValidLanguageGroup (KERNEL32.@)
3296 * Determine if a language group is supported and/or installed.
3298 * PARAMS
3299 * lgrpid [I] Language Group Id (LGRPID_ values from "winnls.h")
3300 * dwFlags [I] LGRPID_SUPPORTED=Supported, LGRPID_INSTALLED=Installed
3302 * RETURNS
3303 * TRUE, if lgrpid is supported and/or installed, according to dwFlags.
3304 * FALSE otherwise.
3306 BOOL WINAPI IsValidLanguageGroup(LGRPID lgrpid, DWORD dwFlags)
3308 static const WCHAR szFormat[] = { '%','x','\0' };
3309 WCHAR szValueName[16], szValue[2];
3310 BOOL bSupported = FALSE, bInstalled = FALSE;
3311 HANDLE hKey;
3314 switch (dwFlags)
3316 case LGRPID_INSTALLED:
3317 case LGRPID_SUPPORTED:
3319 hKey = NLS_RegOpenKey( 0, szLangGroupsKeyName );
3321 sprintfW( szValueName, szFormat, lgrpid );
3323 if (NLS_RegGetDword( hKey, szValueName, (LPDWORD)szValue ))
3325 bSupported = TRUE;
3327 if (szValue[0] == '1')
3328 bInstalled = TRUE;
3331 if (hKey)
3332 NtClose( hKey );
3334 break;
3337 if ((dwFlags == LGRPID_SUPPORTED && bSupported) ||
3338 (dwFlags == LGRPID_INSTALLED && bInstalled))
3339 return TRUE;
3341 return FALSE;
3344 /* Callback function ptrs for EnumLanguageGrouplocalesA/W */
3345 typedef struct
3347 LANGGROUPLOCALE_ENUMPROCA procA;
3348 LANGGROUPLOCALE_ENUMPROCW procW;
3349 DWORD dwFlags;
3350 LGRPID lgrpid;
3351 LONG_PTR lParam;
3352 } ENUMLANGUAGEGROUPLOCALE_CALLBACKS;
3354 /* Internal implementation of EnumLanguageGrouplocalesA/W */
3355 static BOOL NLS_EnumLanguageGroupLocales(ENUMLANGUAGEGROUPLOCALE_CALLBACKS *lpProcs)
3357 static const WCHAR szAlternateSortsKeyName[] = {
3358 'A','l','t','e','r','n','a','t','e',' ','S','o','r','t','s','\0'
3360 WCHAR szNumber[10], szValue[4];
3361 HANDLE hKey;
3362 BOOL bContinue = TRUE, bAlternate = FALSE;
3363 LGRPID lgrpid;
3364 ULONG ulIndex = 1; /* Ignore default entry of 1st key */
3366 if (!lpProcs || !lpProcs->lgrpid || lpProcs->lgrpid > LGRPID_ARMENIAN)
3368 SetLastError(ERROR_INVALID_PARAMETER);
3369 return FALSE;
3372 if (lpProcs->dwFlags)
3374 SetLastError(ERROR_INVALID_FLAGS);
3375 return FALSE;
3378 hKey = NLS_RegOpenKey( 0, szLocaleKeyName );
3380 if (!hKey)
3381 WARN("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
3383 while (bContinue)
3385 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
3386 szValue, sizeof(szValue) ))
3388 lgrpid = strtoulW( szValue, NULL, 16 );
3390 TRACE("lcid %s, grpid %d (%smatched)\n", debugstr_w(szNumber),
3391 lgrpid, lgrpid == lpProcs->lgrpid ? "" : "not ");
3393 if (lgrpid == lpProcs->lgrpid)
3395 LCID lcid;
3397 lcid = strtoulW( szNumber, NULL, 16 );
3399 /* FIXME: native returns extra text for a few (17/150) locales, e.g:
3400 * '00000437 ;Georgian'
3401 * At present we only pass the LCID string.
3404 if (lpProcs->procW)
3405 bContinue = lpProcs->procW( lgrpid, lcid, szNumber, lpProcs->lParam );
3406 else
3408 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
3410 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
3412 bContinue = lpProcs->procA( lgrpid, lcid, szNumberA, lpProcs->lParam );
3416 ulIndex++;
3418 else
3420 /* Finished enumerating this key */
3421 if (!bAlternate)
3423 /* Enumerate alternate sorts also */
3424 hKey = NLS_RegOpenKey( hKey, szAlternateSortsKeyName );
3425 bAlternate = TRUE;
3426 ulIndex = 0;
3428 else
3429 bContinue = FALSE; /* Finished both keys */
3432 if (!bContinue)
3433 break;
3436 if (hKey)
3437 NtClose( hKey );
3439 return TRUE;
3442 /******************************************************************************
3443 * EnumLanguageGroupLocalesA (KERNEL32.@)
3445 * Call a users function for every locale in a language group available on the system.
3447 * PARAMS
3448 * pLangGrpLcEnumProc [I] Callback function to call for each locale
3449 * lgrpid [I] Language group (LGRPID_ values from "winnls.h")
3450 * dwFlags [I] Reserved, set to 0
3451 * lParam [I] User parameter to pass to pLangGrpLcEnumProc
3453 * RETURNS
3454 * Success: TRUE.
3455 * Failure: FALSE. Use GetLastError() to determine the cause.
3457 BOOL WINAPI EnumLanguageGroupLocalesA(LANGGROUPLOCALE_ENUMPROCA pLangGrpLcEnumProc,
3458 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
3460 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
3462 TRACE("(%p,0x%08X,0x%08X,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
3464 callbacks.procA = pLangGrpLcEnumProc;
3465 callbacks.procW = NULL;
3466 callbacks.dwFlags = dwFlags;
3467 callbacks.lgrpid = lgrpid;
3468 callbacks.lParam = lParam;
3470 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
3473 /******************************************************************************
3474 * EnumLanguageGroupLocalesW (KERNEL32.@)
3476 * See EnumLanguageGroupLocalesA.
3478 BOOL WINAPI EnumLanguageGroupLocalesW(LANGGROUPLOCALE_ENUMPROCW pLangGrpLcEnumProc,
3479 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
3481 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
3483 TRACE("(%p,0x%08X,0x%08X,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
3485 callbacks.procA = NULL;
3486 callbacks.procW = pLangGrpLcEnumProc;
3487 callbacks.dwFlags = dwFlags;
3488 callbacks.lgrpid = lgrpid;
3489 callbacks.lParam = lParam;
3491 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
3494 /******************************************************************************
3495 * EnumSystemGeoID (KERNEL32.@)
3497 * Call a users function for every location available on the system.
3499 * PARAMS
3500 * geoclass [I] Type of information desired (SYSGEOTYPE enum from "winnls.h")
3501 * reserved [I] Reserved, set to 0
3502 * pGeoEnumProc [I] Callback function to call for each location
3504 * RETURNS
3505 * Success: TRUE.
3506 * Failure: FALSE. Use GetLastError() to determine the cause.
3508 BOOL WINAPI EnumSystemGeoID(GEOCLASS geoclass, GEOID reserved, GEO_ENUMPROC pGeoEnumProc)
3510 static const WCHAR szCountryCodeValueName[] = {
3511 'C','o','u','n','t','r','y','C','o','d','e','\0'
3513 WCHAR szNumber[10];
3514 HANDLE hKey;
3515 ULONG ulIndex = 0;
3517 TRACE("(0x%08X,0x%08X,%p)\n", geoclass, reserved, pGeoEnumProc);
3519 if (geoclass != GEOCLASS_NATION || reserved || !pGeoEnumProc)
3521 SetLastError(ERROR_INVALID_PARAMETER);
3522 return FALSE;
3525 hKey = NLS_RegOpenKey( 0, szCountryListName );
3527 while (NLS_RegEnumSubKey( hKey, ulIndex, szNumber, sizeof(szNumber) ))
3529 BOOL bContinue = TRUE;
3530 DWORD dwGeoId;
3531 HANDLE hSubKey = NLS_RegOpenKey( hKey, szNumber );
3533 if (hSubKey)
3535 if (NLS_RegGetDword( hSubKey, szCountryCodeValueName, &dwGeoId ))
3537 TRACE("Got geoid %d\n", dwGeoId);
3539 if (!pGeoEnumProc( dwGeoId ))
3540 bContinue = FALSE;
3543 NtClose( hSubKey );
3546 if (!bContinue)
3547 break;
3549 ulIndex++;
3552 if (hKey)
3553 NtClose( hKey );
3555 return TRUE;
3558 /******************************************************************************
3559 * InvalidateNLSCache (KERNEL32.@)
3561 * Invalidate the cache of NLS values.
3563 * PARAMS
3564 * None.
3566 * RETURNS
3567 * Success: TRUE.
3568 * Failure: FALSE.
3570 BOOL WINAPI InvalidateNLSCache(void)
3572 FIXME("() stub\n");
3573 return FALSE;
3576 /******************************************************************************
3577 * GetUserGeoID (KERNEL32.@)
3579 GEOID WINAPI GetUserGeoID( GEOCLASS GeoClass )
3581 GEOID ret = GEOID_NOT_AVAILABLE;
3582 static const WCHAR geoW[] = {'G','e','o',0};
3583 static const WCHAR nationW[] = {'N','a','t','i','o','n',0};
3584 WCHAR bufferW[40], *end;
3585 DWORD count;
3586 HANDLE hkey, hSubkey = 0;
3587 UNICODE_STRING keyW;
3588 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
3589 RtlInitUnicodeString( &keyW, nationW );
3590 count = sizeof(bufferW);
3592 if(!(hkey = create_registry_key())) return ret;
3594 switch( GeoClass ){
3595 case GEOCLASS_NATION:
3596 if ((hSubkey = NLS_RegOpenKey(hkey, geoW)))
3598 if((NtQueryValueKey(hSubkey, &keyW, KeyValuePartialInformation,
3599 bufferW, count, &count) == STATUS_SUCCESS ) && info->DataLength)
3600 ret = strtolW((LPCWSTR)info->Data, &end, 10);
3602 break;
3603 case GEOCLASS_REGION:
3604 FIXME("GEOCLASS_REGION not handled yet\n");
3605 break;
3608 NtClose(hkey);
3609 if (hSubkey) NtClose(hSubkey);
3610 return ret;
3613 /******************************************************************************
3614 * SetUserGeoID (KERNEL32.@)
3616 BOOL WINAPI SetUserGeoID( GEOID GeoID )
3618 static const WCHAR geoW[] = {'G','e','o',0};
3619 static const WCHAR nationW[] = {'N','a','t','i','o','n',0};
3620 static const WCHAR formatW[] = {'%','i',0};
3621 UNICODE_STRING nameW,keyW;
3622 WCHAR bufferW[10];
3623 OBJECT_ATTRIBUTES attr;
3624 HANDLE hkey;
3626 if(!(hkey = create_registry_key())) return FALSE;
3628 attr.Length = sizeof(attr);
3629 attr.RootDirectory = hkey;
3630 attr.ObjectName = &nameW;
3631 attr.Attributes = 0;
3632 attr.SecurityDescriptor = NULL;
3633 attr.SecurityQualityOfService = NULL;
3634 RtlInitUnicodeString( &nameW, geoW );
3635 RtlInitUnicodeString( &keyW, nationW );
3637 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS)
3640 NtClose(attr.RootDirectory);
3641 return FALSE;
3644 sprintfW(bufferW, formatW, GeoID);
3645 NtSetValueKey(hkey, &keyW, 0, REG_SZ, bufferW, (strlenW(bufferW) + 1) * sizeof(WCHAR));
3646 NtClose(attr.RootDirectory);
3647 NtClose(hkey);
3648 return TRUE;
3651 typedef struct
3653 union
3655 UILANGUAGE_ENUMPROCA procA;
3656 UILANGUAGE_ENUMPROCW procW;
3657 } u;
3658 DWORD flags;
3659 LONG_PTR param;
3660 } ENUM_UILANG_CALLBACK;
3662 static BOOL CALLBACK enum_uilang_proc_a( HMODULE hModule, LPCSTR type,
3663 LPCSTR name, WORD LangID, LONG_PTR lParam )
3665 ENUM_UILANG_CALLBACK *enum_uilang = (ENUM_UILANG_CALLBACK *)lParam;
3666 char buf[20];
3668 sprintf(buf, "%08x", (UINT)LangID);
3669 return enum_uilang->u.procA( buf, enum_uilang->param );
3672 static BOOL CALLBACK enum_uilang_proc_w( HMODULE hModule, LPCWSTR type,
3673 LPCWSTR name, WORD LangID, LONG_PTR lParam )
3675 static const WCHAR formatW[] = {'%','0','8','x',0};
3676 ENUM_UILANG_CALLBACK *enum_uilang = (ENUM_UILANG_CALLBACK *)lParam;
3677 WCHAR buf[20];
3679 sprintfW( buf, formatW, (UINT)LangID );
3680 return enum_uilang->u.procW( buf, enum_uilang->param );
3683 /******************************************************************************
3684 * EnumUILanguagesA (KERNEL32.@)
3686 BOOL WINAPI EnumUILanguagesA(UILANGUAGE_ENUMPROCA pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
3688 ENUM_UILANG_CALLBACK enum_uilang;
3690 TRACE("%p, %x, %lx\n", pUILangEnumProc, dwFlags, lParam);
3692 if(!pUILangEnumProc) {
3693 SetLastError(ERROR_INVALID_PARAMETER);
3694 return FALSE;
3696 if(dwFlags) {
3697 SetLastError(ERROR_INVALID_FLAGS);
3698 return FALSE;
3701 enum_uilang.u.procA = pUILangEnumProc;
3702 enum_uilang.flags = dwFlags;
3703 enum_uilang.param = lParam;
3705 EnumResourceLanguagesA( kernel32_handle, (LPCSTR)RT_STRING,
3706 (LPCSTR)LOCALE_ILANGUAGE, enum_uilang_proc_a,
3707 (LONG_PTR)&enum_uilang);
3708 return TRUE;
3711 /******************************************************************************
3712 * EnumUILanguagesW (KERNEL32.@)
3714 BOOL WINAPI EnumUILanguagesW(UILANGUAGE_ENUMPROCW pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
3716 ENUM_UILANG_CALLBACK enum_uilang;
3718 TRACE("%p, %x, %lx\n", pUILangEnumProc, dwFlags, lParam);
3721 if(!pUILangEnumProc) {
3722 SetLastError(ERROR_INVALID_PARAMETER);
3723 return FALSE;
3725 if(dwFlags) {
3726 SetLastError(ERROR_INVALID_FLAGS);
3727 return FALSE;
3730 enum_uilang.u.procW = pUILangEnumProc;
3731 enum_uilang.flags = dwFlags;
3732 enum_uilang.param = lParam;
3734 EnumResourceLanguagesW( kernel32_handle, (LPCWSTR)RT_STRING,
3735 (LPCWSTR)LOCALE_ILANGUAGE, enum_uilang_proc_w,
3736 (LONG_PTR)&enum_uilang);
3737 return TRUE;
3740 INT WINAPI GetGeoInfoW(GEOID GeoId, GEOTYPE GeoType, LPWSTR lpGeoData,
3741 int cchData, LANGID language)
3743 FIXME("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, language);
3744 return 0;
3747 INT WINAPI GetGeoInfoA(GEOID GeoId, GEOTYPE GeoType, LPSTR lpGeoData,
3748 int cchData, LANGID language)
3750 FIXME("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, language);
3751 return 0;