Finish any pending WaitCommEvents if the event mask is set to 0.
[wine.git] / ole / ole2nls.c
blob213b5cbc694e356f838c04460736a907db602eeb
1 /*
2 * National Language Support library
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 David Lee Lambert
6 * Copyright 2000 Julio César Gázquez
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <string.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <locale.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "wine/unicode.h"
34 #include "options.h"
35 #include "winver.h"
36 #include "winnls.h"
37 #include "winreg.h"
38 #include "winerror.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(string);
43 /* Locale name to id map. used by EnumSystemLocales, GetLocaleInfoA
44 * MUST contain all #defines from winnls.h
45 * last entry has NULL name, 0 id.
46 */
47 #define LOCALE_ENTRY(x) {#x,LOCALE_##x}
48 static const struct tagLOCALE_NAME2ID {
49 const char *name;
50 LCTYPE id;
51 } locale_name2id[]= {
52 LOCALE_ENTRY(ILANGUAGE),
53 LOCALE_ENTRY(SLANGUAGE),
54 LOCALE_ENTRY(SENGLANGUAGE),
55 LOCALE_ENTRY(SABBREVLANGNAME),
56 LOCALE_ENTRY(SNATIVELANGNAME),
57 LOCALE_ENTRY(ICOUNTRY),
58 LOCALE_ENTRY(SCOUNTRY),
59 LOCALE_ENTRY(SENGCOUNTRY),
60 LOCALE_ENTRY(SABBREVCTRYNAME),
61 LOCALE_ENTRY(SNATIVECTRYNAME),
62 LOCALE_ENTRY(IDEFAULTLANGUAGE),
63 LOCALE_ENTRY(IDEFAULTCOUNTRY),
64 LOCALE_ENTRY(IDEFAULTCODEPAGE),
65 LOCALE_ENTRY(IDEFAULTANSICODEPAGE),
66 LOCALE_ENTRY(IDEFAULTMACCODEPAGE),
67 LOCALE_ENTRY(SLIST),
68 LOCALE_ENTRY(IMEASURE),
69 LOCALE_ENTRY(SDECIMAL),
70 LOCALE_ENTRY(STHOUSAND),
71 LOCALE_ENTRY(SGROUPING),
72 LOCALE_ENTRY(IDIGITS),
73 LOCALE_ENTRY(ILZERO),
74 LOCALE_ENTRY(INEGNUMBER),
75 LOCALE_ENTRY(SNATIVEDIGITS),
76 LOCALE_ENTRY(SCURRENCY),
77 LOCALE_ENTRY(SINTLSYMBOL),
78 LOCALE_ENTRY(SMONDECIMALSEP),
79 LOCALE_ENTRY(SMONTHOUSANDSEP),
80 LOCALE_ENTRY(SMONGROUPING),
81 LOCALE_ENTRY(ICURRDIGITS),
82 LOCALE_ENTRY(IINTLCURRDIGITS),
83 LOCALE_ENTRY(ICURRENCY),
84 LOCALE_ENTRY(INEGCURR),
85 LOCALE_ENTRY(SDATE),
86 LOCALE_ENTRY(STIME),
87 LOCALE_ENTRY(SSHORTDATE),
88 LOCALE_ENTRY(SLONGDATE),
89 LOCALE_ENTRY(STIMEFORMAT),
90 LOCALE_ENTRY(IDATE),
91 LOCALE_ENTRY(ILDATE),
92 LOCALE_ENTRY(ITIME),
93 LOCALE_ENTRY(ITIMEMARKPOSN),
94 LOCALE_ENTRY(ICENTURY),
95 LOCALE_ENTRY(ITLZERO),
96 LOCALE_ENTRY(IDAYLZERO),
97 LOCALE_ENTRY(IMONLZERO),
98 LOCALE_ENTRY(S1159),
99 LOCALE_ENTRY(S2359),
100 LOCALE_ENTRY(ICALENDARTYPE),
101 LOCALE_ENTRY(IOPTIONALCALENDAR),
102 LOCALE_ENTRY(IFIRSTDAYOFWEEK),
103 LOCALE_ENTRY(IFIRSTWEEKOFYEAR),
104 LOCALE_ENTRY(SDAYNAME1),
105 LOCALE_ENTRY(SDAYNAME2),
106 LOCALE_ENTRY(SDAYNAME3),
107 LOCALE_ENTRY(SDAYNAME4),
108 LOCALE_ENTRY(SDAYNAME5),
109 LOCALE_ENTRY(SDAYNAME6),
110 LOCALE_ENTRY(SDAYNAME7),
111 LOCALE_ENTRY(SABBREVDAYNAME1),
112 LOCALE_ENTRY(SABBREVDAYNAME2),
113 LOCALE_ENTRY(SABBREVDAYNAME3),
114 LOCALE_ENTRY(SABBREVDAYNAME4),
115 LOCALE_ENTRY(SABBREVDAYNAME5),
116 LOCALE_ENTRY(SABBREVDAYNAME6),
117 LOCALE_ENTRY(SABBREVDAYNAME7),
118 LOCALE_ENTRY(SMONTHNAME1),
119 LOCALE_ENTRY(SMONTHNAME2),
120 LOCALE_ENTRY(SMONTHNAME3),
121 LOCALE_ENTRY(SMONTHNAME4),
122 LOCALE_ENTRY(SMONTHNAME5),
123 LOCALE_ENTRY(SMONTHNAME6),
124 LOCALE_ENTRY(SMONTHNAME7),
125 LOCALE_ENTRY(SMONTHNAME8),
126 LOCALE_ENTRY(SMONTHNAME9),
127 LOCALE_ENTRY(SMONTHNAME10),
128 LOCALE_ENTRY(SMONTHNAME11),
129 LOCALE_ENTRY(SMONTHNAME12),
130 LOCALE_ENTRY(SMONTHNAME13),
131 LOCALE_ENTRY(SABBREVMONTHNAME1),
132 LOCALE_ENTRY(SABBREVMONTHNAME2),
133 LOCALE_ENTRY(SABBREVMONTHNAME3),
134 LOCALE_ENTRY(SABBREVMONTHNAME4),
135 LOCALE_ENTRY(SABBREVMONTHNAME5),
136 LOCALE_ENTRY(SABBREVMONTHNAME6),
137 LOCALE_ENTRY(SABBREVMONTHNAME7),
138 LOCALE_ENTRY(SABBREVMONTHNAME8),
139 LOCALE_ENTRY(SABBREVMONTHNAME9),
140 LOCALE_ENTRY(SABBREVMONTHNAME10),
141 LOCALE_ENTRY(SABBREVMONTHNAME11),
142 LOCALE_ENTRY(SABBREVMONTHNAME12),
143 LOCALE_ENTRY(SABBREVMONTHNAME13),
144 LOCALE_ENTRY(SPOSITIVESIGN),
145 LOCALE_ENTRY(SNEGATIVESIGN),
146 LOCALE_ENTRY(IPOSSIGNPOSN),
147 LOCALE_ENTRY(INEGSIGNPOSN),
148 LOCALE_ENTRY(IPOSSYMPRECEDES),
149 LOCALE_ENTRY(IPOSSEPBYSPACE),
150 LOCALE_ENTRY(INEGSYMPRECEDES),
151 LOCALE_ENTRY(INEGSEPBYSPACE),
152 LOCALE_ENTRY(FONTSIGNATURE),
153 LOCALE_ENTRY(SISO639LANGNAME),
154 LOCALE_ENTRY(SISO3166CTRYNAME),
155 {NULL,0}
158 static char *GetLocaleSubkeyName( DWORD lctype );
160 /***********************************************************************
161 * GetUserDefaultLCID (KERNEL32.@)
163 LCID WINAPI GetUserDefaultLCID(void)
165 return MAKELCID( GetUserDefaultLangID() , SORT_DEFAULT );
168 /***********************************************************************
169 * GetSystemDefaultLCID (KERNEL32.@)
171 LCID WINAPI GetSystemDefaultLCID(void)
173 return GetUserDefaultLCID();
176 #define NLS_MAX_LANGUAGES 20
177 typedef struct {
178 char lang[128];
179 char country[128];
180 LANGID found_lang_id[NLS_MAX_LANGUAGES];
181 char found_language[NLS_MAX_LANGUAGES][3];
182 char found_country[NLS_MAX_LANGUAGES][3];
183 int n_found;
184 } LANG_FIND_DATA;
186 static BOOL CALLBACK NLS_FindLanguageID_ProcA(HMODULE hModule, LPCSTR type,
187 LPCSTR name, WORD LangID, LONG lParam)
189 LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
190 LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
191 char buf_language[128];
192 char buf_country[128];
193 char buf_en_language[128];
195 TRACE("%04X\n", (UINT)LangID);
196 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
197 return TRUE; /* continue search */
199 buf_language[0] = 0;
200 buf_country[0] = 0;
202 GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE,
203 buf_language, sizeof(buf_language));
204 TRACE("LOCALE_SISO639LANGNAME: %s\n", buf_language);
206 GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
207 buf_country, sizeof(buf_country));
208 TRACE("LOCALE_SISO3166CTRYNAME: %s\n", buf_country);
210 if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_language))
212 if(l_data->country && strlen(l_data->country) > 0)
214 if(!strcasecmp(l_data->country, buf_country))
216 l_data->found_lang_id[0] = LangID;
217 l_data->n_found = 1;
218 TRACE("Found lang_id %04X for %s_%s\n", LangID, l_data->lang, l_data->country);
219 return FALSE; /* stop enumeration */
222 else /* l_data->country not specified */
224 if(l_data->n_found < NLS_MAX_LANGUAGES)
226 l_data->found_lang_id[l_data->n_found] = LangID;
227 strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
228 strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
229 l_data->n_found++;
230 TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
231 return TRUE; /* continue search */
236 /* Just in case, check LOCALE_SENGLANGUAGE too,
237 * in hope that possible alias name might have that value.
239 buf_en_language[0] = 0;
240 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
241 buf_en_language, sizeof(buf_en_language));
242 TRACE("LOCALE_SENGLANGUAGE: %s\n", buf_en_language);
244 if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_en_language))
246 l_data->found_lang_id[l_data->n_found] = LangID;
247 strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
248 strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
249 l_data->n_found++;
250 TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
253 return TRUE; /* continue search */
256 /***********************************************************************
257 * NLS_GetLanguageID
259 * INPUT:
260 * Lang: a string whose two first chars are the iso name of a language.
261 * Country: a string whose two first chars are the iso name of country
262 * Charset: a string defining the chossen charset encoding
263 * Dialect: a string defining a variation of the locale
265 * all those values are from the standardized format of locale
266 * name in unix which is: Lang[_Country][.Charset][@Dialect]
268 * RETURNS:
269 * the numeric code of the language used by Windows
271 * FIXME: Charset and Dialect are not handled
273 static LANGID NLS_GetLanguageID(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
275 LANG_FIND_DATA l_data;
276 char lang_string[256];
278 if(!Lang)
280 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
281 goto END;
284 memset(&l_data, 0, sizeof(LANG_FIND_DATA));
285 strncpy(l_data.lang, Lang, sizeof(l_data.lang));
287 if(Country && strlen(Country) > 0)
288 strncpy(l_data.country, Country, sizeof(l_data.country));
290 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
291 (LPCSTR)LOCALE_ILANGUAGE, NLS_FindLanguageID_ProcA, (LONG)&l_data);
293 strcpy(lang_string, l_data.lang);
294 if(l_data.country && strlen(l_data.country) > 0)
296 strcat(lang_string, "_");
297 strcat(lang_string, l_data.country);
300 if(!l_data.n_found)
302 if(l_data.country && strlen(l_data.country) > 0)
304 MESSAGE("Warning: Language '%s' was not found, retrying without country name...\n", lang_string);
305 l_data.country[0] = 0;
306 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
307 (LPCSTR)LOCALE_ILANGUAGE, NLS_FindLanguageID_ProcA, (LONG)&l_data);
311 /* Re-evaluate lang_string */
312 strcpy(lang_string, l_data.lang);
313 if(l_data.country && strlen(l_data.country) > 0)
315 strcat(lang_string, "_");
316 strcat(lang_string, l_data.country);
319 if(!l_data.n_found)
321 MESSAGE("Warning: Language '%s' was not recognized, defaulting to English\n", lang_string);
322 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
324 else
326 if(l_data.n_found == 1)
327 TRACE("For language '%s' lang_id %04X was found\n", lang_string, l_data.found_lang_id[0]);
328 else /* l_data->n_found > 1 */
330 int i;
331 MESSAGE("For language '%s' several language ids were found:\n", lang_string);
332 for(i = 0; i < l_data.n_found; i++)
333 MESSAGE("%s_%s - %04X; ", l_data.found_language[i], l_data.found_country[i], l_data.found_lang_id[i]);
335 MESSAGE("\nInstead of using first in the list, suggest to define\n"
336 "your LANG environment variable like this: LANG=%s_%s\n",
337 l_data.found_language[0], l_data.found_country[0]);
340 END:
341 TRACE("Returning %04X\n", l_data.found_lang_id[0]);
342 return l_data.found_lang_id[0];
345 /***********************************************************************
346 * GetUserDefaultLangID (KERNEL32.@)
348 LANGID WINAPI GetUserDefaultLangID(void)
350 /* caching result, if defined from environment, which should (?) not change during a WINE session */
351 static LANGID userLCID = 0;
353 if (userLCID == 0)
355 char buf[256];
356 char *lang,*country,*charset,*dialect,*next;
358 if (GetEnvironmentVariableA( "LANGUAGE", buf, sizeof(buf) )) goto ok;
359 if (GetEnvironmentVariableA( "LANG", buf, sizeof(buf) )) goto ok;
360 if (GetEnvironmentVariableA( "LC_ALL", buf, sizeof(buf) )) goto ok;
361 if (GetEnvironmentVariableA( "LC_MESSAGES", buf, sizeof(buf) )) goto ok;
362 if (GetEnvironmentVariableA( "LC_CTYPE", buf, sizeof(buf) )) goto ok;
364 return userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
367 if (!strcmp(buf,"POSIX") || !strcmp(buf,"C"))
368 return userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
370 lang=buf;
372 do {
373 next=strchr(lang,':'); if (next) *next++='\0';
374 dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
375 charset=strchr(lang,'.'); if (charset) *charset++='\0';
376 country=strchr(lang,'_'); if (country) *country++='\0';
378 userLCID = NLS_GetLanguageID(lang, country, charset, dialect);
380 lang=next;
381 } while (lang && !userLCID);
383 if (!userLCID)
385 MESSAGE( "Warning: language '%s' not recognized, defaulting to English\n",
386 buf );
387 userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
390 return userLCID;
393 /***********************************************************************
394 * GetSystemDefaultLangID (KERNEL32.@)
396 LANGID WINAPI GetSystemDefaultLangID(void)
398 return GetUserDefaultLangID();
401 /******************************************************************************
402 * ConvertDefaultLocale (KERNEL32.@)
404 LCID WINAPI ConvertDefaultLocale (LCID lcid)
405 { switch (lcid)
406 { case LOCALE_SYSTEM_DEFAULT:
407 return GetSystemDefaultLCID();
408 case LOCALE_USER_DEFAULT:
409 return GetUserDefaultLCID();
410 case LOCALE_NEUTRAL:
411 return MAKELCID (LANG_NEUTRAL, SUBLANG_NEUTRAL);
413 return MAKELANGID( PRIMARYLANGID(lcid), SUBLANG_NEUTRAL);
416 /* Enhanced version of LoadStringW.
417 * It takes LanguageId to find and load language dependant resources.
418 * Resource is copied as is: "binary" strings are not truncated.
419 * Return: length of resource + 1 to distinguish absent resources
420 * from the resources with zero length.
422 static INT NLS_LoadStringExW(HMODULE hModule, LANGID lang_id, UINT res_id, LPWSTR buffer, INT buflen)
424 HRSRC hrsrc;
425 HGLOBAL hmem;
426 WCHAR *p;
427 int string_num;
428 int i;
430 /* Replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
431 if(SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
432 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
434 hrsrc = FindResourceExW(hModule, RT_STRINGW, (LPCWSTR)((res_id >> 4) + 1), lang_id);
436 if(!hrsrc) return 0;
437 hmem = LoadResource(hModule, hrsrc);
438 if(!hmem) return 0;
440 p = LockResource(hmem);
441 string_num = res_id & 0x000f;
442 for(i = 0; i < string_num; i++)
443 p += *p + 1;
445 TRACE("strlen = %d\n", (int)*p );
447 if (buffer == NULL) return *p;
448 i = min(buflen - 1, *p);
449 if (i > 0) {
450 memcpy(buffer, p + 1, i * sizeof (WCHAR));
451 buffer[i] = (WCHAR) 0;
452 } else {
453 if (buflen > 1)
454 buffer[0] = (WCHAR) 0;
457 FreeResource(hmem);
458 TRACE("%s loaded!\n", debugstr_w(buffer));
459 return (i + 1);
462 /******************************************************************************
463 * GetLocaleInfoA (KERNEL32.@)
465 * NOTES
466 * LANG_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
468 * MS online documentation states that the string returned is NULL terminated
469 * except for LOCALE_FONTSIGNATURE which "will return a non-NULL
470 * terminated string".
472 INT WINAPI GetLocaleInfoA(LCID lcid,LCTYPE LCType,LPSTR buf,INT len)
474 LPCSTR retString = NULL;
475 int found = 0, i;
476 char *pacKey;
477 char acBuffer[128];
478 DWORD dwBufferSize=128;
479 BOOL NoUserOverride;
481 TRACE("(lcid=0x%lx,lctype=0x%lx,%p,%x)\n",lcid,LCType,buf,len);
483 if (len && (! buf) ) {
484 SetLastError(ERROR_INSUFFICIENT_BUFFER);
485 return 0;
488 if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT)
490 lcid = GetSystemDefaultLCID();
492 else if (lcid == LANG_USER_DEFAULT) /*0x800*/
494 lcid = GetUserDefaultLCID();
497 /* LOCALE_NOUSEROVERRIDE means: do not get user redefined settings
498 from the registry. Instead, use system default values. */
499 NoUserOverride = (LCType & LOCALE_NOUSEROVERRIDE) != 0;
501 LCType &= ~(LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP);
503 /* First, check if it's in the registry. */
504 /* All user customized values are stored in the registry by SetLocaleInfo */
505 if ( !NoUserOverride && (pacKey = GetLocaleSubkeyName(LCType)) )
507 char acRealKey[128];
508 HKEY hKey;
510 sprintf( acRealKey, "Control Panel\\International\\%s", pacKey );
512 if ( RegOpenKeyExA( HKEY_CURRENT_USER, acRealKey,
513 0, KEY_READ, &hKey) == ERROR_SUCCESS )
515 if ( RegQueryValueExA( hKey, NULL, NULL, NULL, (LPBYTE)acBuffer,
516 &dwBufferSize ) == ERROR_SUCCESS )
518 retString = acBuffer;
519 found = 1;
521 RegCloseKey(hKey);
525 /* If not in the registry, get it from the NLS entries. */
526 if(!found) {
527 WCHAR wcBuffer[128];
528 int res_size;
530 /* check if language is registered in the kernel32 resources */
531 if((res_size = NLS_LoadStringExW(GetModuleHandleA("KERNEL32"), LANGIDFROMLCID(lcid),
532 LCType, wcBuffer, sizeof(wcBuffer)/sizeof(wcBuffer[0])))) {
533 WideCharToMultiByte(CP_ACP, 0, wcBuffer, res_size, acBuffer, dwBufferSize, NULL, NULL);
534 retString = acBuffer;
535 found = 1;
539 /* if not found report a most descriptive error */
540 if(!found) {
541 retString=0;
542 /* If we are through all of this, retLen should not be zero anymore.
543 If it is, the value is not supported */
544 i=0;
545 while (locale_name2id[i].name!=NULL) {
546 if (LCType == locale_name2id[i].id) {
547 retString = locale_name2id[i].name;
548 break;
550 i++;
552 if(!retString)
553 FIXME("Unkown LC type %lX\n", LCType);
554 else
555 FIXME("'%s' is not defined for your language (%04X).\n"
556 "Please define it in dlls/kernel/nls/YourLanguage.nls\n"
557 "and submit patch for inclusion into the next Wine release.\n",
558 retString, LOWORD(lcid));
559 SetLastError(ERROR_INVALID_PARAMETER);
560 return 0;
563 /* a FONTSIGNATURE is not a string, just 6 DWORDs */
564 if (LCType == LOCALE_FONTSIGNATURE) {
565 if (len) {
566 len = (len < sizeof(FONTSIGNATURE)) ? len : sizeof(FONTSIGNATURE);
567 memcpy(buf, retString, len);
568 return len;
570 return sizeof(FONTSIGNATURE);
572 /* if len=0 return only the length, don't touch the buffer*/
573 if (len) {
574 lstrcpynA(buf,retString,len);
575 return strlen(buf) + 1;
577 return strlen(retString)+1;
580 /******************************************************************************
581 * GetLocaleInfoW (KERNEL32.@)
583 * NOTES
584 * MS documentation states that len "specifies the size, in bytes (ANSI version)
585 * or characters (Unicode version), of" wbuf. Thus the number returned is
586 * the same between GetLocaleInfoW and GetLocaleInfoA.
588 INT WINAPI GetLocaleInfoW(LCID lcid,LCTYPE LCType,LPWSTR wbuf,INT len)
589 { WORD wlen;
590 LPSTR abuf;
592 if (len && (! wbuf) )
593 { SetLastError(ERROR_INSUFFICIENT_BUFFER);
594 return 0;
597 abuf = (LPSTR)HeapAlloc(GetProcessHeap(),0,len);
598 wlen = GetLocaleInfoA(lcid, LCType, abuf, len);
600 if (wlen && len) /* if len=0 return only the length*/
601 MultiByteToWideChar( CP_ACP, 0, abuf, -1, wbuf, len );
603 HeapFree(GetProcessHeap(),0,abuf);
604 return wlen;
607 /******************************************************************************
609 * GetLocaleSubkeyName [helper function]
611 * - For use with the registry.
612 * - Gets the registry subkey name for a given lctype.
614 static char *GetLocaleSubkeyName( DWORD lctype )
616 char *pacKey=NULL;
618 switch ( lctype )
620 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
621 * the values are stored in the registry, confirmed under Windows,
622 * for the ones that actually assign pacKey. Cases that aren't finished
623 * have not been confirmed, so that must be done before they can be
624 * added.
626 case LOCALE_SDATE : /* The date separator. */
627 pacKey = "sDate";
628 break;
629 case LOCALE_ICURRDIGITS:
630 pacKey = "iCurrDigits";
631 break;
632 case LOCALE_SDECIMAL :
633 pacKey = "sDecimal";
634 break;
635 case LOCALE_ICURRENCY:
636 pacKey = "iCurrency";
637 break;
638 case LOCALE_SGROUPING :
639 pacKey = "sGrouping";
640 break;
641 case LOCALE_IDIGITS:
642 pacKey = "iDigits";
643 break;
644 case LOCALE_SLIST :
645 pacKey = "sList";
646 break;
647 /* case LOCALE_ICALENDARTYPE: */
648 /* case LOCALE_IFIRSTDAYOFWEEK: */
649 /* case LOCALE_IFIRSTWEEKOFYEAR: */
650 /* case LOCALE_SYEARMONTH : */
651 /* case LOCALE_SPOSITIVESIGN : */
652 /* case LOCALE_IPAPERSIZE: */
653 /* break; */
654 case LOCALE_SLONGDATE :
655 pacKey = "sLongDate";
656 break;
657 case LOCALE_SMONDECIMALSEP :
658 pacKey = "sMonDecimalSep";
659 break;
660 case LOCALE_SMONGROUPING:
661 pacKey = "sMonGrouping";
662 break;
663 case LOCALE_IMEASURE:
664 pacKey = "iMeasure";
665 break;
666 case LOCALE_SMONTHOUSANDSEP :
667 pacKey = "sMonThousandSep";
668 break;
669 case LOCALE_INEGCURR:
670 pacKey = "iNegCurr";
671 break;
672 case LOCALE_SNEGATIVESIGN :
673 pacKey = "sNegativeSign";
674 break;
675 case LOCALE_INEGNUMBER:
676 pacKey = "iNegNumber";
677 break;
678 case LOCALE_SSHORTDATE :
679 pacKey = "sShortDate";
680 break;
681 case LOCALE_ILDATE: /* Long Date format ordering specifier. */
682 pacKey = "iLDate";
683 break;
684 case LOCALE_ILZERO:
685 pacKey = "iLZero";
686 break;
687 case LOCALE_ITLZERO:
688 pacKey = "iTLZero";
689 break;
690 case LOCALE_ITIME: /* Time format specifier. */
691 pacKey = "iTime";
692 break;
693 case LOCALE_STHOUSAND :
694 pacKey = "sThousand";
695 break;
696 case LOCALE_S1159: /* AM */
697 pacKey = "s1159";
698 break;
699 case LOCALE_STIME:
700 pacKey = "sTime";
701 break;
702 case LOCALE_S2359: /* PM */
703 pacKey = "s2359";
704 break;
705 case LOCALE_STIMEFORMAT :
706 pacKey = "sTimeFormat";
707 break;
708 case LOCALE_SCURRENCY:
709 pacKey = "sCurrency";
710 break;
712 /* The following are not listed under MSDN as supported,
713 * but seem to be used and also stored in the registry.
716 case LOCALE_IDATE:
717 pacKey = "iDate";
718 break;
719 case LOCALE_SCOUNTRY:
720 pacKey = "sCountry";
721 break;
722 case LOCALE_ICOUNTRY:
723 pacKey = "iCountry";
724 break;
725 case LOCALE_SLANGUAGE:
726 pacKey = "sLanguage";
727 break;
729 default:
730 break;
733 return( pacKey );
736 /******************************************************************************
737 * SetLocaleInfoA [KERNEL32.@]
739 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
741 HKEY hKey;
742 char *pacKey;
743 char acRealKey[128];
745 if ( (pacKey = GetLocaleSubkeyName(lctype)) )
747 sprintf( acRealKey, "Control Panel\\International\\%s", pacKey );
748 if ( RegCreateKeyA( HKEY_CURRENT_USER, acRealKey,
749 &hKey ) == ERROR_SUCCESS )
751 if ( RegSetValueExA( hKey, NULL, 0, REG_SZ,
752 data, strlen(data)+1 ) != ERROR_SUCCESS )
754 ERR("SetLocaleInfoA: %s did not work\n", pacKey );
756 RegCloseKey( hKey );
759 else
761 FIXME("(%ld,%ld,%s): stub\n",lcid,lctype,data);
763 return TRUE;
766 /******************************************************************************
767 * IsValidLocale [KERNEL32.@]
769 BOOL WINAPI IsValidLocale(LCID lcid,DWORD flags)
771 /* check if language is registered in the kernel32 resources */
772 if(!FindResourceExW(GetModuleHandleA("KERNEL32"), RT_STRINGW, (LPCWSTR)LOCALE_ILANGUAGE, LOWORD(lcid)))
773 return FALSE;
774 else
775 return TRUE;
778 static BOOL CALLBACK EnumResourceLanguagesProcW(HMODULE hModule, LPCWSTR type,
779 LPCWSTR name, WORD LangID, LONG lParam)
781 CHAR bufA[20];
782 WCHAR bufW[20];
783 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
784 sprintf(bufA, "%08X", (UINT)LangID);
785 MultiByteToWideChar(CP_ACP, 0, bufA, -1, bufW, sizeof(bufW)/sizeof(bufW[0]));
786 return lpfnLocaleEnum(bufW);
789 /******************************************************************************
790 * EnumSystemLocalesW [KERNEL32.@]
792 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum,
793 DWORD flags )
795 TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
797 EnumResourceLanguagesW(GetModuleHandleA("KERNEL32"), RT_STRINGW,
798 (LPCWSTR)LOCALE_ILANGUAGE, EnumResourceLanguagesProcW,
799 (LONG)lpfnLocaleEnum);
801 return TRUE;
804 static BOOL CALLBACK EnumResourceLanguagesProcA(HMODULE hModule, LPCSTR type,
805 LPCSTR name, WORD LangID, LONG lParam)
807 CHAR bufA[20];
808 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
809 sprintf(bufA, "%08X", (UINT)LangID);
810 return lpfnLocaleEnum(bufA);
813 /******************************************************************************
814 * EnumSystemLocalesA [KERNEL32.@]
816 BOOL WINAPI EnumSystemLocalesA(LOCALE_ENUMPROCA lpfnLocaleEnum,
817 DWORD flags)
819 TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
821 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
822 (LPCSTR)LOCALE_ILANGUAGE, EnumResourceLanguagesProcA,
823 (LONG)lpfnLocaleEnum);
825 return TRUE;
828 /***********************************************************************
829 * VerLanguageNameA [KERNEL32.@]
831 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
833 if(!szLang)
834 return 0;
836 return GetLocaleInfoA(MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize);
839 /***********************************************************************
840 * VerLanguageNameW [KERNEL32.@]
842 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
844 if(!szLang)
845 return 0;
847 return GetLocaleInfoW(MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize);
851 static const unsigned char LCM_Unicode_LUT[] = {
852 6 , 3, /* - 1 */
853 6 , 4, /* - 2 */
854 6 , 5, /* - 3 */
855 6 , 6, /* - 4 */
856 6 , 7, /* - 5 */
857 6 , 8, /* - 6 */
858 6 , 9, /* - 7 */
859 6 , 10, /* - 8 */
860 7 , 5, /* - 9 */
861 7 , 6, /* - 10 */
862 7 , 7, /* - 11 */
863 7 , 8, /* - 12 */
864 7 , 9, /* - 13 */
865 6 , 11, /* - 14 */
866 6 , 12, /* - 15 */
867 6 , 13, /* - 16 */
868 6 , 14, /* - 17 */
869 6 , 15, /* - 18 */
870 6 , 16, /* - 19 */
871 6 , 17, /* - 20 */
872 6 , 18, /* - 21 */
873 6 , 19, /* - 22 */
874 6 , 20, /* - 23 */
875 6 , 21, /* - 24 */
876 6 , 22, /* - 25 */
877 6 , 23, /* - 26 */
878 6 , 24, /* - 27 */
879 6 , 25, /* - 28 */
880 6 , 26, /* - 29 */
881 6 , 27, /* - 30 */
882 6 , 28, /* - 31 */
883 7 , 2, /* - 32 */
884 7 , 28, /* ! - 33 */
885 7 , 29, /* " - 34 */ /* " */
886 7 , 31, /* # - 35 */
887 7 , 33, /* $ - 36 */
888 7 , 35, /* % - 37 */
889 7 , 37, /* & - 38 */
890 6 , 128, /* ' - 39 */
891 7 , 39, /* ( - 40 */
892 7 , 42, /* ) - 41 */
893 7 , 45, /* * - 42 */
894 8 , 3, /* + - 43 */
895 7 , 47, /* , - 44 */
896 6 , 130, /* - - 45 */
897 7 , 51, /* . - 46 */
898 7 , 53, /* / - 47 */
899 12 , 3, /* 0 - 48 */
900 12 , 33, /* 1 - 49 */
901 12 , 51, /* 2 - 50 */
902 12 , 70, /* 3 - 51 */
903 12 , 88, /* 4 - 52 */
904 12 , 106, /* 5 - 53 */
905 12 , 125, /* 6 - 54 */
906 12 , 144, /* 7 - 55 */
907 12 , 162, /* 8 - 56 */
908 12 , 180, /* 9 - 57 */
909 7 , 55, /* : - 58 */
910 7 , 58, /* ; - 59 */
911 8 , 14, /* < - 60 */
912 8 , 18, /* = - 61 */
913 8 , 20, /* > - 62 */
914 7 , 60, /* ? - 63 */
915 7 , 62, /* @ - 64 */
916 14 , 2, /* A - 65 */
917 14 , 9, /* B - 66 */
918 14 , 10, /* C - 67 */
919 14 , 26, /* D - 68 */
920 14 , 33, /* E - 69 */
921 14 , 35, /* F - 70 */
922 14 , 37, /* G - 71 */
923 14 , 44, /* H - 72 */
924 14 , 50, /* I - 73 */
925 14 , 53, /* J - 74 */
926 14 , 54, /* K - 75 */
927 14 , 72, /* L - 76 */
928 14 , 81, /* M - 77 */
929 14 , 112, /* N - 78 */
930 14 , 124, /* O - 79 */
931 14 , 126, /* P - 80 */
932 14 , 137, /* Q - 81 */
933 14 , 138, /* R - 82 */
934 14 , 145, /* S - 83 */
935 14 , 153, /* T - 84 */
936 14 , 159, /* U - 85 */
937 14 , 162, /* V - 86 */
938 14 , 164, /* W - 87 */
939 14 , 166, /* X - 88 */
940 14 , 167, /* Y - 89 */
941 14 , 169, /* Z - 90 */
942 7 , 63, /* [ - 91 */
943 7 , 65, /* \ - 92 */
944 7 , 66, /* ] - 93 */
945 7 , 67, /* ^ - 94 */
946 7 , 68, /* _ - 95 */
947 7 , 72, /* ` - 96 */
948 14 , 2, /* a - 97 */
949 14 , 9, /* b - 98 */
950 14 , 10, /* c - 99 */
951 14 , 26, /* d - 100 */
952 14 , 33, /* e - 101 */
953 14 , 35, /* f - 102 */
954 14 , 37, /* g - 103 */
955 14 , 44, /* h - 104 */
956 14 , 50, /* i - 105 */
957 14 , 53, /* j - 106 */
958 14 , 54, /* k - 107 */
959 14 , 72, /* l - 108 */
960 14 , 81, /* m - 109 */
961 14 , 112, /* n - 110 */
962 14 , 124, /* o - 111 */
963 14 , 126, /* p - 112 */
964 14 , 137, /* q - 113 */
965 14 , 138, /* r - 114 */
966 14 , 145, /* s - 115 */
967 14 , 153, /* t - 116 */
968 14 , 159, /* u - 117 */
969 14 , 162, /* v - 118 */
970 14 , 164, /* w - 119 */
971 14 , 166, /* x - 120 */
972 14 , 167, /* y - 121 */
973 14 , 169, /* z - 122 */
974 7 , 74, /* { - 123 */
975 7 , 76, /* | - 124 */
976 7 , 78, /* } - 125 */
977 7 , 80, /* ~ - 126 */
978 6 , 29, /* \x7f - 127 */
979 6 , 30, /* € - 128 */
980 6 , 31, /* � - 129 */
981 7 , 123, /* ‚ - 130 */
982 14 , 35, /* ƒ - 131 */
983 7 , 127, /* „ - 132 */
984 10 , 21, /* … - 133 */
985 10 , 15, /* † - 134 */
986 10 , 16, /* ‡ - 135 */
987 7 , 67, /* ˆ - 136 */
988 10 , 22, /* ‰ - 137 */
989 14 , 145, /* Š - 138 */
990 7 , 136, /* ‹ - 139 */
991 14 + 16 , 124, /* Π- 140 */
992 6 , 43, /* � - 141 */
993 6 , 44, /* Ž - 142 */
994 6 , 45, /* � - 143 */
995 6 , 46, /* � - 144 */
996 7 , 121, /* ‘ - 145 */
997 7 , 122, /* ’ - 146 */
998 7 , 125, /* “ - 147 */
999 7 , 126, /* ” - 148 */
1000 10 , 17, /* • - 149 */
1001 6 , 137, /* – - 150 */
1002 6 , 139, /* — - 151 */
1003 7 , 93, /* ˜ - 152 */
1004 14 , 156, /* ™ - 153 */
1005 14 , 145, /* š - 154 */
1006 7 , 137, /* › - 155 */
1007 14 + 16 , 124, /* œ - 156 */
1008 6 , 59, /* � - 157 */
1009 6 , 60, /* ž - 158 */
1010 14 , 167, /* Ÿ - 159 */
1011 7 , 4, /*   - 160 */
1012 7 , 81, /* ¡ - 161 */
1013 10 , 2, /* ¢ - 162 */
1014 10 , 3, /* £ - 163 */
1015 10 , 4, /* ¤ - 164 */
1016 10 , 5, /* ¥ - 165 */
1017 7 , 82, /* ¦ - 166 */
1018 10 , 6, /* § - 167 */
1019 7 , 83, /* ¨ - 168 */
1020 10 , 7, /* © - 169 */
1021 14 , 2, /* ª - 170 */
1022 8 , 24, /* « - 171 */
1023 10 , 8, /* ¬ - 172 */
1024 6 , 131, /* ­ - 173 */
1025 10 , 9, /* ® - 174 */
1026 7 , 84, /* ¯ - 175 */
1027 10 , 10, /* ° - 176 */
1028 8 , 23, /* ± - 177 */
1029 12 , 51, /* ² - 178 */
1030 12 , 70, /* ³ - 179 */
1031 7 , 85, /* ´ - 180 */
1032 10 , 11, /* µ - 181 */
1033 10 , 12, /* ¶ - 182 */
1034 10 , 13, /* · - 183 */
1035 7 , 86, /* ¸ - 184 */
1036 12 , 33, /* ¹ - 185 */
1037 14 , 124, /* º - 186 */
1038 8 , 26, /* » - 187 */
1039 12 , 21, /* ¼ - 188 */
1040 12 , 25, /* ½ - 189 */
1041 12 , 29, /* ¾ - 190 */
1042 7 , 87, /* ¿ - 191 */
1043 14 , 2, /* À - 192 */
1044 14 , 2, /* Á - 193 */
1045 14 , 2, /* Â - 194 */
1046 14 , 2, /* Ã - 195 */
1047 14 , 2, /* Ä - 196 */
1048 14 , 2, /* Å - 197 */
1049 14 + 16 , 2, /* Æ - 198 */
1050 14 , 10, /* Ç - 199 */
1051 14 , 33, /* È - 200 */
1052 14 , 33, /* É - 201 */
1053 14 , 33, /* Ê - 202 */
1054 14 , 33, /* Ë - 203 */
1055 14 , 50, /* Ì - 204 */
1056 14 , 50, /* Í - 205 */
1057 14 , 50, /* Î - 206 */
1058 14 , 50, /* Ï - 207 */
1059 14 , 26, /* Ð - 208 */
1060 14 , 112, /* Ñ - 209 */
1061 14 , 124, /* Ò - 210 */
1062 14 , 124, /* Ó - 211 */
1063 14 , 124, /* Ô - 212 */
1064 14 , 124, /* Õ - 213 */
1065 14 , 124, /* Ö - 214 */
1066 8 , 28, /* × - 215 */
1067 14 , 124, /* Ø - 216 */
1068 14 , 159, /* Ù - 217 */
1069 14 , 159, /* Ú - 218 */
1070 14 , 159, /* Û - 219 */
1071 14 , 159, /* Ü - 220 */
1072 14 , 167, /* Ý - 221 */
1073 14 + 32 , 153, /* Þ - 222 */
1074 14 + 48 , 145, /* ß - 223 */
1075 14 , 2, /* à - 224 */
1076 14 , 2, /* á - 225 */
1077 14 , 2, /* â - 226 */
1078 14 , 2, /* ã - 227 */
1079 14 , 2, /* ä - 228 */
1080 14 , 2, /* å - 229 */
1081 14 + 16 , 2, /* æ - 230 */
1082 14 , 10, /* ç - 231 */
1083 14 , 33, /* è - 232 */
1084 14 , 33, /* é - 233 */
1085 14 , 33, /* ê - 234 */
1086 14 , 33, /* ë - 235 */
1087 14 , 50, /* ì - 236 */
1088 14 , 50, /* í - 237 */
1089 14 , 50, /* î - 238 */
1090 14 , 50, /* ï - 239 */
1091 14 , 26, /* ð - 240 */
1092 14 , 112, /* ñ - 241 */
1093 14 , 124, /* ò - 242 */
1094 14 , 124, /* ó - 243 */
1095 14 , 124, /* ô - 244 */
1096 14 , 124, /* õ - 245 */
1097 14 , 124, /* ö - 246 */
1098 8 , 29, /* ÷ - 247 */
1099 14 , 124, /* ø - 248 */
1100 14 , 159, /* ù - 249 */
1101 14 , 159, /* ú - 250 */
1102 14 , 159, /* û - 251 */
1103 14 , 159, /* ü - 252 */
1104 14 , 167, /* ý - 253 */
1105 14 + 32 , 153, /* þ - 254 */
1106 14 , 167 /* ÿ - 255 */ };
1108 static const unsigned char LCM_Unicode_LUT_2[] = { 33, 44, 145 };
1110 #define LCM_Diacritic_Start 131
1112 static const unsigned char LCM_Diacritic_LUT[] = {
1113 123, /* ƒ - 131 */
1114 2, /* „ - 132 */
1115 2, /* … - 133 */
1116 2, /* † - 134 */
1117 2, /* ‡ - 135 */
1118 3, /* ˆ - 136 */
1119 2, /* ‰ - 137 */
1120 20, /* Š - 138 */
1121 2, /* ‹ - 139 */
1122 2, /* Π- 140 */
1123 2, /* � - 141 */
1124 2, /* Ž - 142 */
1125 2, /* � - 143 */
1126 2, /* � - 144 */
1127 2, /* ‘ - 145 */
1128 2, /* ’ - 146 */
1129 2, /* “ - 147 */
1130 2, /* ” - 148 */
1131 2, /* • - 149 */
1132 2, /* – - 150 */
1133 2, /* — - 151 */
1134 2, /* ˜ - 152 */
1135 2, /* ™ - 153 */
1136 20, /* š - 154 */
1137 2, /* › - 155 */
1138 2, /* œ - 156 */
1139 2, /* � - 157 */
1140 2, /* ž - 158 */
1141 19, /* Ÿ - 159 */
1142 2, /*   - 160 */
1143 2, /* ¡ - 161 */
1144 2, /* ¢ - 162 */
1145 2, /* £ - 163 */
1146 2, /* ¤ - 164 */
1147 2, /* ¥ - 165 */
1148 2, /* ¦ - 166 */
1149 2, /* § - 167 */
1150 2, /* ¨ - 168 */
1151 2, /* © - 169 */
1152 3, /* ª - 170 */
1153 2, /* « - 171 */
1154 2, /* ¬ - 172 */
1155 2, /* ­ - 173 */
1156 2, /* ® - 174 */
1157 2, /* ¯ - 175 */
1158 2, /* ° - 176 */
1159 2, /* ± - 177 */
1160 2, /* ² - 178 */
1161 2, /* ³ - 179 */
1162 2, /* ´ - 180 */
1163 2, /* µ - 181 */
1164 2, /* ¶ - 182 */
1165 2, /* · - 183 */
1166 2, /* ¸ - 184 */
1167 2, /* ¹ - 185 */
1168 3, /* º - 186 */
1169 2, /* » - 187 */
1170 2, /* ¼ - 188 */
1171 2, /* ½ - 189 */
1172 2, /* ¾ - 190 */
1173 2, /* ¿ - 191 */
1174 15, /* À - 192 */
1175 14, /* Á - 193 */
1176 18, /* Â - 194 */
1177 25, /* Ã - 195 */
1178 19, /* Ä - 196 */
1179 26, /* Å - 197 */
1180 2, /* Æ - 198 */
1181 28, /* Ç - 199 */
1182 15, /* È - 200 */
1183 14, /* É - 201 */
1184 18, /* Ê - 202 */
1185 19, /* Ë - 203 */
1186 15, /* Ì - 204 */
1187 14, /* Í - 205 */
1188 18, /* Î - 206 */
1189 19, /* Ï - 207 */
1190 104, /* Ð - 208 */
1191 25, /* Ñ - 209 */
1192 15, /* Ò - 210 */
1193 14, /* Ó - 211 */
1194 18, /* Ô - 212 */
1195 25, /* Õ - 213 */
1196 19, /* Ö - 214 */
1197 2, /* × - 215 */
1198 33, /* Ø - 216 */
1199 15, /* Ù - 217 */
1200 14, /* Ú - 218 */
1201 18, /* Û - 219 */
1202 19, /* Ü - 220 */
1203 14, /* Ý - 221 */
1204 2, /* Þ - 222 */
1205 2, /* ß - 223 */
1206 15, /* à - 224 */
1207 14, /* á - 225 */
1208 18, /* â - 226 */
1209 25, /* ã - 227 */
1210 19, /* ä - 228 */
1211 26, /* å - 229 */
1212 2, /* æ - 230 */
1213 28, /* ç - 231 */
1214 15, /* è - 232 */
1215 14, /* é - 233 */
1216 18, /* ê - 234 */
1217 19, /* ë - 235 */
1218 15, /* ì - 236 */
1219 14, /* í - 237 */
1220 18, /* î - 238 */
1221 19, /* ï - 239 */
1222 104, /* ð - 240 */
1223 25, /* ñ - 241 */
1224 15, /* ò - 242 */
1225 14, /* ó - 243 */
1226 18, /* ô - 244 */
1227 25, /* õ - 245 */
1228 19, /* ö - 246 */
1229 2, /* ÷ - 247 */
1230 33, /* ø - 248 */
1231 15, /* ù - 249 */
1232 14, /* ú - 250 */
1233 18, /* û - 251 */
1234 19, /* ü - 252 */
1235 14, /* ý - 253 */
1236 2, /* þ - 254 */
1237 19, /* ÿ - 255 */
1240 /******************************************************************************
1241 * OLE2NLS_isPunctuation [INTERNAL]
1243 static int OLE2NLS_isPunctuation(unsigned char c)
1245 /* "punctuation character" in this context is a character which is
1246 considered "less important" during word sort comparison.
1247 See LCMapString implementation for the precise definition
1248 of "less important". */
1250 return (LCM_Unicode_LUT[-2+2*c]==6);
1253 /******************************************************************************
1254 * OLE2NLS_isNonSpacing [INTERNAL]
1256 static int OLE2NLS_isNonSpacing(unsigned char c)
1258 /* This function is used by LCMapStringA. Characters
1259 for which it returns true are ignored when mapping a
1260 string with NORM_IGNORENONSPACE */
1261 return ((c==136) || (c==170) || (c==186));
1264 /******************************************************************************
1265 * OLE2NLS_isSymbol [INTERNAL]
1266 * FIXME: handle current locale
1268 static int OLE2NLS_isSymbol(unsigned char c)
1270 /* This function is used by LCMapStringA. Characters
1271 for which it returns true are ignored when mapping a
1272 string with NORM_IGNORESYMBOLS */
1273 return ( (c!=0) && !(isalpha(c) || isdigit(c)) );
1276 /******************************************************************************
1277 * identity [Internal]
1279 static int identity(int c)
1281 return c;
1284 /*************************************************************************
1285 * LCMapStringA [KERNEL32.@]
1287 * Convert a string, or generate a sort key from it.
1289 * If (mapflags & LCMAP_SORTKEY), the function will generate
1290 * a sort key for the source string. Else, it will convert it
1291 * accordingly to the flags LCMAP_UPPERCASE, LCMAP_LOWERCASE,...
1293 * RETURNS
1294 * Error : 0.
1295 * Success : length of the result string.
1297 * NOTES
1298 * If called with scrlen = -1, the function will compute the length
1299 * of the 0-terminated string strsrc by itself.
1301 * If called with dstlen = 0, returns the buffer length that
1302 * would be required.
1304 * NORM_IGNOREWIDTH means to compare ASCII and wide characters
1305 * as if they are equal.
1306 * In the only code page implemented so far, there may not be
1307 * wide characters in strings passed to LCMapStringA,
1308 * so there is nothing to be done for this flag.
1310 INT WINAPI LCMapStringA(
1311 LCID lcid, /* [in] locale identifier created with MAKELCID;
1312 LOCALE_SYSTEM_DEFAULT and LOCALE_USER_DEFAULT are
1313 predefined values. */
1314 DWORD mapflags, /* [in] flags */
1315 LPCSTR srcstr, /* [in] source buffer */
1316 INT srclen, /* [in] source length */
1317 LPSTR dststr, /* [out] destination buffer */
1318 INT dstlen) /* [in] destination buffer length */
1320 int i;
1322 TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1323 lcid,mapflags,debugstr_an(srcstr,srclen),srclen,dststr,dstlen);
1325 if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
1327 ERR("(src=%s,dest=%s): Invalid NULL string\n",
1328 debugstr_an(srcstr,srclen), dststr);
1329 SetLastError(ERROR_INVALID_PARAMETER);
1330 return 0;
1332 if (srclen == -1)
1333 srclen = strlen(srcstr) + 1 ; /* (include final '\0') */
1335 #define LCMAPSTRINGA_SUPPORTED_FLAGS (LCMAP_UPPERCASE | \
1336 LCMAP_LOWERCASE | \
1337 LCMAP_SORTKEY | \
1338 NORM_IGNORECASE | \
1339 NORM_IGNORENONSPACE | \
1340 SORT_STRINGSORT | \
1341 NORM_IGNOREWIDTH | \
1342 NORM_IGNOREKANATYPE)
1343 /* FIXME: as long as we don't support Katakana nor Hiragana
1344 * characters, we can support NORM_IGNOREKANATYPE
1346 if (mapflags & ~LCMAPSTRINGA_SUPPORTED_FLAGS)
1348 FIXME("(0x%04lx,0x%08lx,%p,%d,%p,%d): "
1349 "unimplemented flags: 0x%08lx\n",
1350 lcid,
1351 mapflags,
1352 srcstr,
1353 srclen,
1354 dststr,
1355 dstlen,
1356 mapflags & ~LCMAPSTRINGA_SUPPORTED_FLAGS
1360 if ( !(mapflags & LCMAP_SORTKEY) )
1362 int i,j;
1363 int (*f)(int) = identity;
1364 int flag_ignorenonspace = mapflags & NORM_IGNORENONSPACE;
1365 int flag_ignoresymbols = mapflags & NORM_IGNORESYMBOLS;
1367 if (flag_ignorenonspace || flag_ignoresymbols)
1369 /* For some values of mapflags, the length of the resulting
1370 string is not known at this point. Windows does map the string
1371 and does not SetLastError ERROR_INSUFFICIENT_BUFFER in
1372 these cases. */
1373 if (dstlen==0)
1375 /* Compute required length */
1376 for (i=j=0; i < srclen; i++)
1378 if ( !(flag_ignorenonspace && OLE2NLS_isNonSpacing(srcstr[i]))
1379 && !(flag_ignoresymbols && OLE2NLS_isSymbol(srcstr[i])) )
1380 j++;
1382 return j;
1385 else
1387 if (dstlen==0)
1388 return srclen;
1389 if (dstlen<srclen)
1391 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1392 return 0;
1395 if (mapflags & LCMAP_UPPERCASE)
1396 f = toupper;
1397 else if (mapflags & LCMAP_LOWERCASE)
1398 f = tolower;
1399 /* FIXME: NORM_IGNORENONSPACE requires another conversion */
1400 for (i=j=0; (i<srclen) && (j<dstlen) ; i++)
1402 if ( !(flag_ignorenonspace && OLE2NLS_isNonSpacing(srcstr[i]))
1403 && !(flag_ignoresymbols && OLE2NLS_isSymbol(srcstr[i])) )
1405 dststr[j] = (CHAR) f(srcstr[i]);
1406 j++;
1409 return j;
1412 /* FIXME: This function completely ignores the "lcid" parameter. */
1413 /* else ... (mapflags & LCMAP_SORTKEY) */
1415 int unicode_len=0;
1416 int case_len=0;
1417 int diacritic_len=0;
1418 int delayed_punctuation_len=0;
1419 char *case_component;
1420 char *diacritic_component;
1421 char *delayed_punctuation_component;
1422 int room,count;
1423 int flag_stringsort = mapflags & SORT_STRINGSORT;
1425 /* compute how much room we will need */
1426 for (i=0;i<srclen;i++)
1428 int ofs;
1429 unsigned char source_char = srcstr[i];
1430 if (source_char!='\0')
1432 if (flag_stringsort || !OLE2NLS_isPunctuation(source_char))
1434 unicode_len++;
1435 if ( LCM_Unicode_LUT[-2+2*source_char] & ~15 )
1436 unicode_len++; /* double letter */
1438 else
1440 delayed_punctuation_len++;
1444 if (isupper(source_char))
1445 case_len=unicode_len;
1447 ofs = source_char - LCM_Diacritic_Start;
1448 if ((ofs>=0) && (LCM_Diacritic_LUT[ofs]!=2))
1449 diacritic_len=unicode_len;
1452 if (mapflags & NORM_IGNORECASE)
1453 case_len=0;
1454 if (mapflags & NORM_IGNORENONSPACE)
1455 diacritic_len=0;
1457 room = 2 * unicode_len /* "unicode" component */
1458 + diacritic_len /* "diacritic" component */
1459 + case_len /* "case" component */
1460 + 4 * delayed_punctuation_len /* punctuation in word sort mode */
1461 + 4 /* four '\1' separators */
1462 + 1 ; /* terminal '\0' */
1463 if (dstlen==0)
1464 return room;
1465 else if (dstlen<room)
1467 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1468 return 0;
1470 #if 0
1471 /*FIXME the Pointercheck should not be nessesary */
1472 if (IsBadWritePtr (dststr,room))
1473 { ERR("bad destination buffer (dststr) : %p,%d\n",dststr,dstlen);
1474 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1475 return 0;
1477 #endif
1478 /* locate each component, write separators */
1479 diacritic_component = dststr + 2*unicode_len ;
1480 *diacritic_component++ = '\1';
1481 case_component = diacritic_component + diacritic_len ;
1482 *case_component++ = '\1';
1483 delayed_punctuation_component = case_component + case_len ;
1484 *delayed_punctuation_component++ = '\1';
1485 *delayed_punctuation_component++ = '\1';
1487 /* read source string char by char, write
1488 corresponding weight in each component. */
1489 for (i=0,count=0;i<srclen;i++)
1491 unsigned char source_char=srcstr[i];
1492 if (source_char!='\0')
1494 int type,longcode;
1495 type = LCM_Unicode_LUT[-2+2*source_char];
1496 longcode = type >> 4;
1497 type &= 15;
1498 if (!flag_stringsort && OLE2NLS_isPunctuation(source_char))
1500 WORD encrypted_location = (1<<15) + 7 + 4*count;
1501 *delayed_punctuation_component++ = (unsigned char) (encrypted_location>>8);
1502 *delayed_punctuation_component++ = (unsigned char) (encrypted_location&255);
1503 /* big-endian is used here because it lets string comparison be
1504 compatible with numerical comparison */
1506 *delayed_punctuation_component++ = type;
1507 *delayed_punctuation_component++ = LCM_Unicode_LUT[-1+2*source_char];
1508 /* assumption : a punctuation character is never a
1509 double or accented letter */
1511 else
1513 dststr[2*count] = type;
1514 dststr[2*count+1] = LCM_Unicode_LUT[-1+2*source_char];
1515 if (longcode)
1517 if (count<case_len)
1518 case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
1519 if (count<diacritic_len)
1520 diacritic_component[count] = 2; /* assumption: a double letter
1521 is never accented */
1522 count++;
1524 dststr[2*count] = type;
1525 dststr[2*count+1] = *(LCM_Unicode_LUT_2 - 1 + longcode);
1526 /* 16 in the first column of LCM_Unicode_LUT --> longcode = 1
1527 32 in the first column of LCM_Unicode_LUT --> longcode = 2
1528 48 in the first column of LCM_Unicode_LUT --> longcode = 3 */
1531 if (count<case_len)
1532 case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
1533 if (count<diacritic_len)
1535 int ofs = source_char - LCM_Diacritic_Start;
1536 diacritic_component[count] = (ofs>=0 ? LCM_Diacritic_LUT[ofs] : 2);
1538 count++;
1542 dststr[room-1] = '\0';
1543 return room;
1547 /*************************************************************************
1548 * LCMapStringW [KERNEL32.@]
1550 * Convert a string, or generate a sort key from it.
1552 * NOTE
1554 * See LCMapStringA for documentation
1556 INT WINAPI LCMapStringW(
1557 LCID lcid,DWORD mapflags,LPCWSTR srcstr,INT srclen,LPWSTR dststr,
1558 INT dstlen)
1560 int i;
1562 TRACE("(0x%04lx,0x%08lx,%p,%d,%p,%d)\n",
1563 lcid, mapflags, srcstr, srclen, dststr, dstlen);
1565 if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
1567 ERR("(src=%p,dst=%p): Invalid NULL string\n", srcstr, dststr);
1568 SetLastError(ERROR_INVALID_PARAMETER);
1569 return 0;
1571 if (srclen==-1)
1572 srclen = strlenW(srcstr)+1;
1574 /* FIXME: Both this function and it's companion LCMapStringA()
1575 * completely ignore the "lcid" parameter. In place of the "lcid"
1576 * parameter the application must set the "LC_COLLATE" or "LC_ALL"
1577 * environment variable prior to invoking this function. */
1578 if (mapflags & LCMAP_SORTKEY)
1580 /* Possible values of LC_COLLATE. */
1581 char *lc_collate_default = 0; /* value prior to this function */
1582 char *lc_collate_env = 0; /* value retrieved from the environment */
1584 /* General purpose index into strings of any type. */
1585 int str_idx = 0;
1587 /* Lengths of various strings where the length is measured in
1588 * wide characters for wide character strings and in bytes for
1589 * native strings. The lengths include the NULL terminator. */
1590 size_t returned_len = 0;
1591 size_t src_native_len = 0;
1592 size_t dst_native_len = 0;
1593 size_t dststr_libc_len = 0;
1595 /* Native (character set determined by locale) versions of the
1596 * strings source and destination strings. */
1597 LPSTR src_native = 0;
1598 LPSTR dst_native = 0;
1600 /* Version of the source and destination strings using the
1601 * "wchar_t" Unicode data type needed by various libc functions. */
1602 wchar_t *srcstr_libc = 0;
1603 wchar_t *dststr_libc = 0;
1605 if(!(srcstr_libc = (wchar_t *)HeapAlloc(GetProcessHeap(), 0,
1606 srclen * sizeof(wchar_t))))
1608 ERR("Unable to allocate %d bytes for srcstr_libc\n",
1609 srclen * sizeof(wchar_t));
1610 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1611 return 0;
1614 /* Convert source string to a libc Unicode string. */
1615 for(str_idx = 0; str_idx < srclen; str_idx++)
1617 srcstr_libc[str_idx] = srcstr[str_idx];
1620 /* src_native should contain at most 3 bytes for each
1621 * multibyte characters in the original srcstr string. */
1622 src_native_len = 3 * srclen;
1623 if(!(src_native = (LPSTR)HeapAlloc(GetProcessHeap(), 0,
1624 src_native_len)))
1626 ERR("Unable to allocate %d bytes for src_native\n", src_native_len);
1627 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1628 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1629 return 0;
1632 /* FIXME: Prior to to setting the LC_COLLATE locale category the
1633 * current value is backed up so it can be restored after the
1634 * last LC_COLLATE sensitive function returns.
1636 * Even though the locale is adjusted for a minimum amount of
1637 * time a race condition exists where other threads may be
1638 * affected if they invoke LC_COLLATE sensitive functions. One
1639 * possible solution is to wrap all LC_COLLATE sensitive Wine
1640 * functions, like LCMapStringW(), in a mutex.
1642 * Another enhancement to the following would be to set the
1643 * LC_COLLATE locale category as a function of the "lcid"
1644 * parameter instead of the "LC_COLLATE" environment variable. */
1645 if(!(lc_collate_default = setlocale(LC_COLLATE, NULL)))
1647 ERR("Unable to query the LC_COLLATE catagory\n");
1648 SetLastError(ERROR_INVALID_PARAMETER);
1649 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1650 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1651 return 0;
1654 if(!(lc_collate_env = setlocale(LC_COLLATE, "")))
1656 ERR("Unable to inherit the LC_COLLATE locale category from the "
1657 "environment. The \"LC_COLLATE\" environment variable is "
1658 "\"%s\".\n", getenv("LC_COLLATE") ?
1659 getenv("LC_COLLATE") : "<unset>");
1660 SetLastError(ERROR_INVALID_PARAMETER);
1661 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1662 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1663 return 0;
1666 TRACE("lc_collate_default = %s\n", lc_collate_default);
1667 TRACE("lc_collate_env = %s\n", lc_collate_env);
1669 /* Convert the libc Unicode string to a native multibyte character
1670 * string. */
1671 returned_len = wcstombs(src_native, srcstr_libc, src_native_len) + 1;
1672 if(returned_len == 0)
1674 ERR("wcstombs failed. The string specified (%s) may contain an invalid character.\n",
1675 debugstr_w(srcstr));
1676 SetLastError(ERROR_INVALID_PARAMETER);
1677 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1678 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1679 setlocale(LC_COLLATE, lc_collate_default);
1680 return 0;
1682 else if(returned_len > src_native_len)
1684 src_native[src_native_len - 1] = 0;
1685 ERR("wcstombs returned a string (%s) that was longer (%d bytes) "
1686 "than expected (%d bytes).\n", src_native, returned_len,
1687 dst_native_len);
1689 /* Since this is an internal error I'm not sure what the correct
1690 * error code is. */
1691 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1693 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1694 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1695 setlocale(LC_COLLATE, lc_collate_default);
1696 return 0;
1698 src_native_len = returned_len;
1700 TRACE("src_native = %s src_native_len = %d\n",
1701 src_native, src_native_len);
1703 /* dst_native seems to contain at most 4 bytes for each byte in
1704 * the original src_native string. Change if need be since this
1705 * isn't documented by the strxfrm() man page. */
1706 dst_native_len = 4 * src_native_len;
1707 if(!(dst_native = (LPSTR)HeapAlloc(GetProcessHeap(), 0, dst_native_len)))
1709 ERR("Unable to allocate %d bytes for dst_native\n", dst_native_len);
1710 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1711 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1712 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1713 setlocale(LC_COLLATE, lc_collate_default);
1714 return 0;
1717 /* The actual translation is done by the following call to
1718 * strxfrm(). The surrounding code could have been simplified
1719 * by calling wcsxfrm() instead except that wcsxfrm() is not
1720 * available on older Linux systems (RedHat 4.1 with
1721 * libc-5.3.12-17).
1723 * Also, it is possible that the translation could be done by
1724 * various tables as it is done in LCMapStringA(). However, I'm
1725 * not sure what those tables are. */
1726 returned_len = strxfrm(dst_native, src_native, dst_native_len) + 1;
1728 if(returned_len > dst_native_len)
1730 dst_native[dst_native_len - 1] = 0;
1731 ERR("strxfrm returned a string (%s) that was longer (%d bytes) "
1732 "than expected (%d bytes).\n", dst_native, returned_len,
1733 dst_native_len);
1735 /* Since this is an internal error I'm not sure what the correct
1736 * error code is. */
1737 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1739 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1740 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1741 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1742 setlocale(LC_COLLATE, lc_collate_default);
1743 return 0;
1745 dst_native_len = returned_len;
1747 TRACE("dst_native = %s dst_native_len = %d\n",
1748 dst_native, dst_native_len);
1750 dststr_libc_len = dst_native_len;
1751 if(!(dststr_libc = (wchar_t *)HeapAlloc(GetProcessHeap(), 0,
1752 dststr_libc_len * sizeof(wchar_t))))
1754 ERR("Unable to allocate %d bytes for dststr_libc\n",
1755 dststr_libc_len * sizeof(wchar_t));
1756 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1757 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1758 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1759 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1760 setlocale(LC_COLLATE, lc_collate_default);
1761 return 0;
1764 /* Convert the native multibyte string to a libc Unicode string. */
1765 returned_len = mbstowcs(dststr_libc, dst_native, dst_native_len) + 1;
1767 /* Restore LC_COLLATE now that the last LC_COLLATE sensitive
1768 * function has returned. */
1769 setlocale(LC_COLLATE, lc_collate_default);
1771 if(returned_len == 0)
1773 ERR("mbstowcs failed. The native version of the translated string "
1774 "(%s) may contain an invalid character.\n", dst_native);
1775 SetLastError(ERROR_INVALID_PARAMETER);
1776 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1777 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1778 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1779 if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
1780 return 0;
1782 if(dstlen)
1784 if(returned_len > dstlen)
1786 ERR("mbstowcs returned a string that was longer (%d chars) "
1787 "than the buffer provided (%d chars).\n", returned_len,
1788 dstlen);
1789 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1790 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1791 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1792 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1793 if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
1794 return 0;
1796 dstlen = returned_len;
1798 /* Convert a libc Unicode string to the destination string. */
1799 for(str_idx = 0; str_idx < dstlen; str_idx++)
1801 dststr[str_idx] = dststr_libc[str_idx];
1803 TRACE("1st 4 int sized chunks of dststr = %x %x %x %x\n",
1804 *(((int *)dststr) + 0),
1805 *(((int *)dststr) + 1),
1806 *(((int *)dststr) + 2),
1807 *(((int *)dststr) + 3));
1809 else
1811 dstlen = returned_len;
1813 TRACE("dstlen (return) = %d\n", dstlen);
1814 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1815 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1816 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1817 if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
1818 return dstlen;
1820 else
1822 int (*f)(int)=identity;
1824 if (dstlen==0)
1825 return srclen;
1826 if (dstlen<srclen)
1828 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1829 return 0;
1832 if (mapflags & LCMAP_UPPERCASE)
1833 f = toupper;
1834 else if (mapflags & LCMAP_LOWERCASE)
1835 f = tolower;
1836 for (i=0; i < srclen; i++)
1837 dststr[i] = (WCHAR) f(srcstr[i]);
1838 return srclen;
1843 /***********************************************************************
1844 * OLE2NLS_EstimateMappingLength
1846 * Estimates the number of characters required to hold the string
1847 * computed by LCMapStringA.
1849 * The size is always over-estimated, with a fixed limit on the
1850 * amount of estimation error.
1852 * Note that len == -1 is not permitted.
1854 static inline int OLE2NLS_EstimateMappingLength(LCID lcid, DWORD dwMapFlags,
1855 LPCSTR str, DWORD len)
1857 /* Estimate only for small strings to keep the estimation error from
1858 * becoming too large. */
1859 if (len < 128) return len * 8 + 5;
1860 else return LCMapStringA(lcid, dwMapFlags, str, len, NULL, 0);
1863 /******************************************************************************
1864 * CompareStringA [KERNEL32.@]
1865 * Compares two strings using locale
1867 * RETURNS
1869 * success: CSTR_LESS_THAN, CSTR_EQUAL, CSTR_GREATER_THAN
1870 * failure: 0
1872 * NOTES
1874 * Defaults to a word sort, but uses a string sort if
1875 * SORT_STRINGSORT is set.
1876 * Calls SetLastError for ERROR_INVALID_FLAGS, ERROR_INVALID_PARAMETER.
1878 * BUGS
1880 * This implementation ignores the locale
1882 * FIXME
1884 * Quite inefficient.
1886 int WINAPI CompareStringA(
1887 LCID lcid, /* [in] locale ID */
1888 DWORD fdwStyle, /* [in] comparison-style options */
1889 LPCSTR s1, /* [in] first string */
1890 int l1, /* [in] length of first string */
1891 LPCSTR s2, /* [in] second string */
1892 int l2) /* [in] length of second string */
1894 int mapstring_flags;
1895 int len1,len2;
1896 int result;
1897 LPSTR sk1,sk2;
1898 TRACE("%s and %s\n",
1899 debugstr_an (s1,l1), debugstr_an (s2,l2));
1901 if ( (s1==NULL) || (s2==NULL) )
1903 ERR("(s1=%s,s2=%s): Invalid NULL string\n",
1904 debugstr_an(s1,l1), debugstr_an(s2,l2));
1905 SetLastError(ERROR_INVALID_PARAMETER);
1906 return 0;
1909 if(fdwStyle & NORM_IGNORESYMBOLS)
1910 FIXME("IGNORESYMBOLS not supported\n");
1912 if (l1 == -1) l1 = strlen(s1);
1913 if (l2 == -1) l2 = strlen(s2);
1915 mapstring_flags = LCMAP_SORTKEY | fdwStyle ;
1916 len1 = OLE2NLS_EstimateMappingLength(lcid, mapstring_flags, s1, l1);
1917 len2 = OLE2NLS_EstimateMappingLength(lcid, mapstring_flags, s2, l2);
1919 if ((len1==0)||(len2==0))
1920 return 0; /* something wrong happened */
1922 sk1 = (LPSTR)HeapAlloc(GetProcessHeap(), 0, len1 + len2);
1923 sk2 = sk1 + len1;
1924 if ( (!LCMapStringA(lcid,mapstring_flags,s1,l1,sk1,len1))
1925 || (!LCMapStringA(lcid,mapstring_flags,s2,l2,sk2,len2)) )
1927 ERR("Bug in LCmapStringA.\n");
1928 result = 0;
1930 else
1932 /* strcmp doesn't necessarily return -1, 0, or 1 */
1933 result = strcmp(sk1,sk2);
1935 HeapFree(GetProcessHeap(),0,sk1);
1937 if (result < 0)
1938 return 1;
1939 if (result == 0)
1940 return 2;
1942 /* must be greater, if we reach this point */
1943 return 3;
1946 /******************************************************************************
1947 * CompareStringW [KERNEL32.@]
1948 * This implementation ignores the locale
1949 * FIXME : Does only string sort. Should
1950 * be reimplemented the same way as CompareStringA.
1952 int WINAPI CompareStringW(LCID lcid, DWORD fdwStyle,
1953 LPCWSTR s1, int l1, LPCWSTR s2, int l2)
1955 int len,ret;
1956 if(fdwStyle & NORM_IGNORENONSPACE)
1957 FIXME("IGNORENONSPACE not supported\n");
1958 if(fdwStyle & NORM_IGNORESYMBOLS)
1959 FIXME("IGNORESYMBOLS not supported\n");
1961 /* Is strcmp defaulting to string sort or to word sort?? */
1962 /* FIXME: Handle NORM_STRINGSORT */
1963 l1 = (l1==-1)?strlenW(s1):l1;
1964 l2 = (l2==-1)?strlenW(s2):l2;
1965 len = l1<l2 ? l1:l2;
1966 ret = (fdwStyle & NORM_IGNORECASE) ? strncmpiW(s1,s2,len) : strncmpW(s1,s2,len);
1967 /* not equal, return 1 or 3 */
1968 if(ret!=0) {
1969 /* need to translate result */
1970 return ((int)ret < 0) ? 1 : 3;
1972 /* same len, return 2 */
1973 if(l1==l2) return 2;
1974 /* the longer one is lexically greater */
1975 return (l1<l2)? 1 : 3;
1978 /******************************************************************************
1979 * OLE_GetFormatA [Internal]
1981 * FIXME
1982 * If datelen == 0, it should return the reguired string length.
1984 This function implements stuff for GetDateFormat() and
1985 GetTimeFormat().
1987 d single-digit (no leading zero) day (of month)
1988 dd two-digit day (of month)
1989 ddd short day-of-week name
1990 dddd long day-of-week name
1991 M single-digit month
1992 MM two-digit month
1993 MMM short month name
1994 MMMM full month name
1995 y two-digit year, no leading 0
1996 yy two-digit year
1997 yyyy four-digit year
1998 gg era string
1999 h hours with no leading zero (12-hour)
2000 hh hours with full two digits
2001 H hours with no leading zero (24-hour)
2002 HH hours with full two digits
2003 m minutes with no leading zero
2004 mm minutes with full two digits
2005 s seconds with no leading zero
2006 ss seconds with full two digits
2007 t time marker (A or P)
2008 tt time marker (AM, PM)
2009 '' used to quote literal characters
2010 '' (within a quoted string) indicates a literal '
2012 These functions REQUIRE valid locale, date, and format.
2014 static INT OLE_GetFormatA(LCID locale,
2015 DWORD flags,
2016 DWORD tflags,
2017 const SYSTEMTIME* xtime,
2018 LPCSTR _format, /*in*/
2019 LPSTR date, /*out*/
2020 INT datelen)
2022 INT inpos, outpos;
2023 int count, type, inquote, Overflow;
2024 char buf[40];
2025 char format[40];
2026 char * pos;
2027 int buflen;
2029 const char * _dgfmt[] = { "%d", "%02d" };
2030 const char ** dgfmt = _dgfmt - 1;
2032 /* report, for debugging */
2033 TRACE("(0x%lx,0x%lx, 0x%lx, time(y=%d m=%d wd=%d d=%d,h=%d,m=%d,s=%d), fmt=%p \'%s\' , %p, len=%d)\n",
2034 locale, flags, tflags,
2035 xtime->wYear,xtime->wMonth,xtime->wDayOfWeek,xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2036 _format, _format, date, datelen);
2038 if(datelen == 0) {
2039 FIXME("datelen = 0, returning 255\n");
2040 return 255;
2043 /* initalize state variables and output buffer */
2044 inpos = outpos = 0;
2045 count = 0; inquote = 0; Overflow = 0;
2046 type = '\0';
2047 date[0] = buf[0] = '\0';
2049 strcpy(format,_format);
2051 /* alter the formatstring, while it works for all languages now in wine
2052 its possible that it fails when the time looks like ss:mm:hh as example*/
2053 if (tflags & (TIME_NOMINUTESORSECONDS))
2054 { if ((pos = strstr ( format, ":mm")))
2055 { memcpy ( pos, pos+3, strlen(format)-(pos-format)-2 );
2058 if (tflags & (TIME_NOSECONDS))
2059 { if ((pos = strstr ( format, ":ss")))
2060 { memcpy ( pos, pos+3, strlen(format)-(pos-format)-2 );
2064 for (inpos = 0;; inpos++) {
2065 /* TRACE("STATE inpos=%2d outpos=%2d count=%d inquote=%d type=%c buf,date = %c,%c\n", inpos, outpos, count, inquote, type, buf[inpos], date[outpos]); */
2066 if (inquote) {
2067 if (format[inpos] == '\'') {
2068 if (format[inpos+1] == '\'') {
2069 inpos += 1;
2070 date[outpos++] = '\'';
2071 } else {
2072 inquote = 0;
2073 continue; /* we did nothing to the output */
2075 } else if (format[inpos] == '\0') {
2076 date[outpos++] = '\0';
2077 if (outpos > datelen) Overflow = 1;
2078 break;
2079 } else {
2080 date[outpos++] = format[inpos];
2081 if (outpos > datelen) {
2082 Overflow = 1;
2083 date[outpos-1] = '\0'; /* this is the last place where
2084 it's safe to write */
2085 break;
2088 } else if ( (count && (format[inpos] != type))
2089 || count == 4
2090 || (count == 2 && strchr("ghHmst", type)) ) {
2091 if (type == 'h' && (tflags & TIME_FORCE24HOURFORMAT)) type= 'H';
2092 if (type == 'd') {
2093 if (count == 4) {
2094 GetLocaleInfoA(locale,
2095 LOCALE_SDAYNAME1
2096 + (xtime->wDayOfWeek+6)%7,
2097 buf, sizeof(buf));
2098 } else if (count == 3) {
2099 GetLocaleInfoA(locale,
2100 LOCALE_SABBREVDAYNAME1
2101 + (xtime->wDayOfWeek+6)%7,
2102 buf, sizeof(buf));
2103 } else {
2104 sprintf(buf, dgfmt[count], xtime->wDay);
2106 } else if (type == 'M') {
2107 if (count == 3) {
2108 GetLocaleInfoA(locale,
2109 LOCALE_SABBREVMONTHNAME1
2110 + xtime->wMonth - 1,
2111 buf, sizeof(buf));
2112 } else if (count == 4) {
2113 GetLocaleInfoA(locale,
2114 LOCALE_SMONTHNAME1
2115 + xtime->wMonth - 1,
2116 buf, sizeof(buf));
2117 } else {
2118 sprintf(buf, dgfmt[count], xtime->wMonth);
2120 } else if (type == 'y') {
2121 if (count == 4) {
2122 sprintf(buf, "%d", xtime->wYear);
2123 } else if (count == 3) {
2124 strcpy(buf, "yyy");
2125 WARN("unknown format, c=%c, n=%d\n", type, count);
2126 } else {
2127 sprintf(buf, dgfmt[count], xtime->wYear % 100);
2129 } else if (type == 'g') {
2130 if (count == 2) {
2131 FIXME("LOCALE_ICALENDARTYPE unimp.\n");
2132 strcpy(buf, "AD");
2133 } else {
2134 strcpy(buf, "g");
2135 WARN("unknown format, c=%c, n=%d\n", type, count);
2137 } else if (type == 'h') {
2138 /* gives us hours 1:00 -- 12:00 */
2139 sprintf(buf, dgfmt[count], (xtime->wHour-1)%12 +1);
2140 } else if (type == 'H') {
2141 /* 24-hour time */
2142 sprintf(buf, dgfmt[count], xtime->wHour);
2143 } else if ( type == 'm') {
2144 sprintf(buf, dgfmt[count], xtime->wMinute);
2145 } else if ( type == 's') {
2146 sprintf(buf, dgfmt[count], xtime->wSecond);
2147 } else if (type == 't') {
2148 if ((tflags & TIME_NOTIMEMARKER))
2149 buf[0]='\0';
2150 else if (count == 1) {
2151 sprintf(buf, "%c", (xtime->wHour < 12) ? 'A' : 'P');
2152 } else if (count == 2) {
2153 /* sprintf(buf, "%s", (xtime->wHour < 12) ? "AM" : "PM"); */
2154 GetLocaleInfoA(locale,
2155 (xtime->wHour<12)
2156 ? LOCALE_S1159 : LOCALE_S2359,
2157 buf, sizeof(buf));
2161 /* we need to check the next char in the format string
2162 again, no matter what happened */
2163 inpos--;
2165 /* add the contents of buf to the output */
2166 buflen = strlen(buf);
2167 if (outpos + buflen < datelen) {
2168 date[outpos] = '\0'; /* for strcat to hook onto */
2169 strcat(date, buf);
2170 outpos += buflen;
2171 } else {
2172 date[outpos] = '\0';
2173 strncat(date, buf, datelen - outpos);
2174 date[datelen - 1] = '\0';
2175 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2176 WARN("insufficient buffer\n");
2177 return 0;
2180 /* reset the variables we used to keep track of this item */
2181 count = 0;
2182 type = '\0';
2183 } else if (format[inpos] == '\0') {
2184 /* we can't check for this at the loop-head, because
2185 that breaks the printing of the last format-item */
2186 date[outpos] = '\0';
2187 break;
2188 } else if (count) {
2189 /* continuing a code for an item */
2190 count +=1;
2191 continue;
2192 } else if (strchr("hHmstyMdg", format[inpos])) {
2193 type = format[inpos];
2194 count = 1;
2195 continue;
2196 } else if (format[inpos] == '\'') {
2197 inquote = 1;
2198 continue;
2199 } else {
2200 date[outpos++] = format[inpos];
2202 /* now deal with a possible buffer overflow */
2203 if (outpos >= datelen) {
2204 date[datelen - 1] = '\0';
2205 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2206 return 0;
2210 if (Overflow) {
2211 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2214 /* finish it off with a string terminator */
2215 outpos++;
2216 /* sanity check */
2217 if (outpos > datelen-1) outpos = datelen-1;
2218 date[outpos] = '\0';
2220 TRACE("returns string '%s', len %d\n", date, outpos);
2221 return outpos;
2224 /******************************************************************************
2225 * OLE_GetFormatW [INTERNAL]
2227 static INT OLE_GetFormatW(LCID locale, DWORD flags, DWORD tflags,
2228 const SYSTEMTIME* xtime,
2229 LPCWSTR format,
2230 LPWSTR output, INT outlen)
2232 INT inpos, outpos;
2233 int count, type=0, inquote;
2234 int Overflow; /* loop check */
2235 char tmp[16];
2236 WCHAR buf[40];
2237 int buflen=0;
2238 WCHAR arg0[] = {0}, arg1[] = {'%','d',0};
2239 WCHAR arg2[] = {'%','0','2','d',0};
2240 WCHAR *argarr[3];
2241 int datevars=0, timevars=0;
2243 argarr[0] = arg0;
2244 argarr[1] = arg1;
2245 argarr[2] = arg2;
2247 /* make a debug report */
2248 TRACE("args: 0x%lx, 0x%lx, 0x%lx, time(d=%d,h=%d,m=%d,s=%d), fmt:%s (at %p), "
2249 "%p with max len %d\n",
2250 locale, flags, tflags,
2251 xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2252 debugstr_w(format), format, output, outlen);
2254 if(outlen == 0) {
2255 FIXME("outlen = 0, returning 255\n");
2256 return 255;
2259 /* initialize state variables */
2260 inpos = outpos = 0;
2261 count = 0;
2262 inquote = Overflow = 0;
2263 /* this is really just a sanity check */
2264 output[0] = buf[0] = 0;
2266 /* this loop is the core of the function */
2267 for (inpos = 0; /* we have several break points */ ; inpos++) {
2268 if (inquote) {
2269 if (format[inpos] == (WCHAR) '\'') {
2270 if (format[inpos+1] == '\'') {
2271 inpos++;
2272 output[outpos++] = '\'';
2273 } else {
2274 inquote = 0;
2275 continue;
2277 } else if (format[inpos] == 0) {
2278 output[outpos++] = 0;
2279 if (outpos > outlen) Overflow = 1;
2280 break; /* normal exit (within a quote) */
2281 } else {
2282 output[outpos++] = format[inpos]; /* copy input */
2283 if (outpos > outlen) {
2284 Overflow = 1;
2285 output[outpos-1] = 0;
2286 break;
2289 } else if ( (count && (format[inpos] != type))
2290 || ( (count==4 && type =='y') ||
2291 (count==4 && type =='M') ||
2292 (count==4 && type =='d') ||
2293 (count==2 && type =='g') ||
2294 (count==2 && type =='h') ||
2295 (count==2 && type =='H') ||
2296 (count==2 && type =='m') ||
2297 (count==2 && type =='s') ||
2298 (count==2 && type =='t') ) ) {
2299 switch(type)
2301 case 'd':
2302 if (count == 4) {
2303 GetLocaleInfoW(locale,
2304 LOCALE_SDAYNAME1 + (xtime->wDayOfWeek +6)%7,
2305 buf, sizeof(buf)/sizeof(WCHAR) );
2306 } else if (count == 3) {
2307 GetLocaleInfoW(locale,
2308 LOCALE_SABBREVDAYNAME1 +
2309 (xtime->wDayOfWeek +6)%7,
2310 buf, sizeof(buf)/sizeof(WCHAR) );
2311 } else {
2312 sprintf( tmp, "%.*d", count, xtime->wDay );
2313 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2315 break;
2317 case 'M':
2318 if (count == 4) {
2319 GetLocaleInfoW(locale, LOCALE_SMONTHNAME1 +
2320 xtime->wMonth -1, buf,
2321 sizeof(buf)/sizeof(WCHAR) );
2322 } else if (count == 3) {
2323 GetLocaleInfoW(locale, LOCALE_SABBREVMONTHNAME1 +
2324 xtime->wMonth -1, buf,
2325 sizeof(buf)/sizeof(WCHAR) );
2326 } else {
2327 sprintf( tmp, "%.*d", count, xtime->wMonth );
2328 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2330 break;
2331 case 'y':
2332 if (count == 4) {
2333 sprintf( tmp, "%d", xtime->wYear );
2334 } else if (count == 3) {
2335 strcpy( tmp, "yyy" );
2336 } else {
2337 sprintf( tmp, "%.*d", count, xtime->wYear % 100 );
2339 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2340 break;
2342 case 'g':
2343 if (count == 2) {
2344 FIXME("LOCALE_ICALENDARTYPE unimplemented\n");
2345 strcpy( tmp, "AD" );
2346 } else {
2347 /* Win API sez we copy it verbatim */
2348 strcpy( tmp, "g" );
2350 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2351 break;
2353 case 'h':
2354 /* hours 1:00-12:00 --- is this right? */
2355 sprintf( tmp, "%.*d", count, (xtime->wHour-1)%12 +1);
2356 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2357 break;
2359 case 'H':
2360 sprintf( tmp, "%.*d", count, xtime->wHour );
2361 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2362 break;
2364 case 'm':
2365 sprintf( tmp, "%.*d", count, xtime->wMinute );
2366 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2367 break;
2369 case 's':
2370 sprintf( tmp, "%.*d", count, xtime->wSecond );
2371 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2372 break;
2374 case 't':
2375 GetLocaleInfoW(locale, (xtime->wHour < 12) ?
2376 LOCALE_S1159 : LOCALE_S2359,
2377 buf, sizeof(buf) );
2378 if (count == 1) {
2379 buf[1] = 0;
2381 break;
2384 /* no matter what happened, we need to check this next
2385 character the next time we loop through */
2386 inpos--;
2388 /* cat buf onto the output */
2389 outlen = strlenW(buf);
2390 if (outpos + buflen < outlen) {
2391 strcpyW( output + outpos, buf );
2392 outpos += buflen;
2393 } else {
2394 lstrcpynW( output + outpos, buf, outlen - outpos );
2395 Overflow = 1;
2396 break; /* Abnormal exit */
2399 /* reset the variables we used this time */
2400 count = 0;
2401 type = '\0';
2402 } else if (format[inpos] == 0) {
2403 /* we can't check for this at the beginning, because that
2404 would keep us from printing a format spec that ended the
2405 string */
2406 output[outpos] = 0;
2407 break; /* NORMAL EXIT */
2408 } else if (count) {
2409 /* how we keep track of the middle of a format spec */
2410 count++;
2411 continue;
2412 } else if ( (datevars && (format[inpos]=='d' ||
2413 format[inpos]=='M' ||
2414 format[inpos]=='y' ||
2415 format[inpos]=='g') ) ||
2416 (timevars && (format[inpos]=='H' ||
2417 format[inpos]=='h' ||
2418 format[inpos]=='m' ||
2419 format[inpos]=='s' ||
2420 format[inpos]=='t') ) ) {
2421 type = format[inpos];
2422 count = 1;
2423 continue;
2424 } else if (format[inpos] == '\'') {
2425 inquote = 1;
2426 continue;
2427 } else {
2428 /* unquoted literals */
2429 output[outpos++] = format[inpos];
2433 if (Overflow) {
2434 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2435 WARN(" buffer overflow\n");
2438 /* final string terminator and sanity check */
2439 outpos++;
2440 if (outpos > outlen-1) outpos = outlen-1;
2441 output[outpos] = '0';
2443 TRACE(" returning %s\n", debugstr_w(output));
2445 return (!Overflow) ? outlen : 0;
2450 /******************************************************************************
2451 * GetDateFormatA [KERNEL32.@]
2452 * Makes an ASCII string of the date
2454 * This function uses format to format the date, or, if format
2455 * is NULL, uses the default for the locale. format is a string
2456 * of literal fields and characters as follows:
2458 * - d single-digit (no leading zero) day (of month)
2459 * - dd two-digit day (of month)
2460 * - ddd short day-of-week name
2461 * - dddd long day-of-week name
2462 * - M single-digit month
2463 * - MM two-digit month
2464 * - MMM short month name
2465 * - MMMM full month name
2466 * - y two-digit year, no leading 0
2467 * - yy two-digit year
2468 * - yyyy four-digit year
2469 * - gg era string
2472 INT WINAPI GetDateFormatA(LCID locale,DWORD flags,
2473 const SYSTEMTIME* xtime,
2474 LPCSTR format, LPSTR date,INT datelen)
2477 char format_buf[40];
2478 LPCSTR thisformat;
2479 SYSTEMTIME t;
2480 LPSYSTEMTIME thistime;
2481 LCID thislocale;
2482 INT ret;
2483 FILETIME ft;
2484 BOOL res;
2486 TRACE("(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",
2487 locale,flags,xtime,format,date,datelen);
2489 if (!locale) {
2490 locale = LOCALE_SYSTEM_DEFAULT;
2493 if (locale == LOCALE_SYSTEM_DEFAULT) {
2494 thislocale = GetSystemDefaultLCID();
2495 } else if (locale == LOCALE_USER_DEFAULT) {
2496 thislocale = GetUserDefaultLCID();
2497 } else {
2498 thislocale = locale;
2501 if (xtime == NULL) {
2502 GetSystemTime(&t);
2503 } else {
2504 /* Silently correct wDayOfWeek by transforming to FileTime and back again */
2505 res=SystemTimeToFileTime(xtime,&ft);
2506 /* Check year(?)/month and date for range and set ERROR_INVALID_PARAMETER on error */
2507 /*FIXME: SystemTimeToFileTime doesn't yet do that check */
2508 if(!res)
2510 SetLastError(ERROR_INVALID_PARAMETER);
2511 return 0;
2513 FileTimeToSystemTime(&ft,&t);
2516 thistime = &t;
2518 if (format == NULL) {
2519 GetLocaleInfoA(thislocale, ((flags&DATE_LONGDATE)
2520 ? LOCALE_SLONGDATE
2521 : LOCALE_SSHORTDATE),
2522 format_buf, sizeof(format_buf));
2523 thisformat = format_buf;
2524 } else {
2525 thisformat = format;
2529 ret = OLE_GetFormatA(thislocale, flags, 0, thistime, thisformat,
2530 date, datelen);
2533 TRACE(
2534 "GetDateFormatA() returning %d, with data=%s\n",
2535 ret, date);
2536 return ret;
2539 /******************************************************************************
2540 * GetDateFormatW [KERNEL32.@]
2541 * Makes a Unicode string of the date
2543 * Acts the same as GetDateFormatA(), except that it's Unicode.
2544 * Accepts & returns sizes as counts of Unicode characters.
2547 INT WINAPI GetDateFormatW(LCID locale,DWORD flags,
2548 const SYSTEMTIME* xtime,
2549 LPCWSTR format,
2550 LPWSTR date, INT datelen)
2552 unsigned short datearr[] = {'1','9','9','4','-','1','-','1',0};
2554 FIXME("STUB (should call OLE_GetFormatW)\n");
2555 lstrcpynW(date, datearr, datelen);
2556 return ( datelen < 9) ? datelen : 9;
2561 /**************************************************************************
2562 * EnumDateFormatsA (KERNEL32.@)
2564 BOOL WINAPI EnumDateFormatsA(
2565 DATEFMT_ENUMPROCA lpDateFmtEnumProc, LCID Locale, DWORD dwFlags)
2567 LCID Loc = GetUserDefaultLCID();
2568 if(!lpDateFmtEnumProc)
2570 SetLastError(ERROR_INVALID_PARAMETER);
2571 return FALSE;
2574 switch( Loc )
2577 case 0x00000407: /* (Loc,"de_DE") */
2579 switch(dwFlags)
2581 case DATE_SHORTDATE:
2582 if(!(*lpDateFmtEnumProc)("dd.MM.yy")) return TRUE;
2583 if(!(*lpDateFmtEnumProc)("d.M.yyyy")) return TRUE;
2584 if(!(*lpDateFmtEnumProc)("d.MM.yy")) return TRUE;
2585 if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
2586 return TRUE;
2587 case DATE_LONGDATE:
2588 if(!(*lpDateFmtEnumProc)("dddd,d. MMMM yyyy")) return TRUE;
2589 if(!(*lpDateFmtEnumProc)("d. MMMM yyyy")) return TRUE;
2590 if(!(*lpDateFmtEnumProc)("d. MMM yyyy")) return TRUE;
2591 return TRUE;
2592 default:
2593 FIXME("Unknown date format (%ld)\n", dwFlags);
2594 SetLastError(ERROR_INVALID_PARAMETER);
2595 return FALSE;
2599 case 0x0000040c: /* (Loc,"fr_FR") */
2601 switch(dwFlags)
2603 case DATE_SHORTDATE:
2604 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2605 if(!(*lpDateFmtEnumProc)("dd.MM.yy")) return TRUE;
2606 if(!(*lpDateFmtEnumProc)("dd-MM-yy")) return TRUE;
2607 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2608 return TRUE;
2609 case DATE_LONGDATE:
2610 if(!(*lpDateFmtEnumProc)("dddd d MMMM yyyy")) return TRUE;
2611 if(!(*lpDateFmtEnumProc)("d MMM yy")) return TRUE;
2612 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2613 return TRUE;
2614 default:
2615 FIXME("Unknown date format (%ld)\n", dwFlags);
2616 SetLastError(ERROR_INVALID_PARAMETER);
2617 return FALSE;
2621 case 0x00000c0c: /* (Loc,"fr_CA") */
2623 switch(dwFlags)
2625 case DATE_SHORTDATE:
2626 if(!(*lpDateFmtEnumProc)("yy-MM-dd")) return TRUE;
2627 if(!(*lpDateFmtEnumProc)("dd-MM-yy")) return TRUE;
2628 if(!(*lpDateFmtEnumProc)("yy MM dd")) return TRUE;
2629 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2630 return TRUE;
2631 case DATE_LONGDATE:
2632 if(!(*lpDateFmtEnumProc)("d MMMM, yyyy")) return TRUE;
2633 if(!(*lpDateFmtEnumProc)("d MMM yyyy")) return TRUE;
2634 return TRUE;
2635 default:
2636 FIXME("Unknown date format (%ld)\n", dwFlags);
2637 SetLastError(ERROR_INVALID_PARAMETER);
2638 return FALSE;
2642 case 0x00000809: /* (Loc,"en_UK") */
2644 switch(dwFlags)
2646 case DATE_SHORTDATE:
2647 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2648 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2649 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2650 if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
2651 return TRUE;
2652 case DATE_LONGDATE:
2653 if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
2654 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2655 return TRUE;
2656 default:
2657 FIXME("Unknown date format (%ld)\n", dwFlags);
2658 SetLastError(ERROR_INVALID_PARAMETER);
2659 return FALSE;
2663 case 0x00000c09: /* (Loc,"en_AU") */
2665 switch(dwFlags)
2667 case DATE_SHORTDATE:
2668 if(!(*lpDateFmtEnumProc)("d/MM/yy")) return TRUE;
2669 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2670 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2671 return TRUE;
2672 case DATE_LONGDATE:
2673 if(!(*lpDateFmtEnumProc)("dddd,d MMMM yyyy")) return TRUE;
2674 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2675 return TRUE;
2676 default:
2677 FIXME("Unknown date format (%ld)\n", dwFlags);
2678 SetLastError(ERROR_INVALID_PARAMETER);
2679 return FALSE;
2683 case 0x00001009: /* (Loc,"en_CA") */
2685 switch(dwFlags)
2687 case DATE_SHORTDATE:
2688 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2689 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2690 if(!(*lpDateFmtEnumProc)("yy-MM-dd")) return TRUE;
2691 if(!(*lpDateFmtEnumProc)("M/dd/yy")) return TRUE;
2692 return TRUE;
2693 case DATE_LONGDATE:
2694 if(!(*lpDateFmtEnumProc)("d-MMM-yy")) return TRUE;
2695 if(!(*lpDateFmtEnumProc)("MMMM d, yyyy")) return TRUE;
2696 return TRUE;
2697 default:
2698 FIXME("Unknown date format (%ld)\n", dwFlags);
2699 SetLastError(ERROR_INVALID_PARAMETER);
2700 return FALSE;
2704 case 0x00001409: /* (Loc,"en_NZ") */
2706 switch(dwFlags)
2708 case DATE_SHORTDATE:
2709 if(!(*lpDateFmtEnumProc)("d/MM/yy")) return TRUE;
2710 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2711 if(!(*lpDateFmtEnumProc)("d.MM.yy")) return TRUE;
2712 return TRUE;
2713 case DATE_LONGDATE:
2714 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2715 if(!(*lpDateFmtEnumProc)("dddd, d MMMM yyyy")) return TRUE;
2716 return TRUE;
2717 default:
2718 FIXME("Unknown date format (%ld)\n", dwFlags);
2719 SetLastError(ERROR_INVALID_PARAMETER);
2720 return FALSE;
2724 case 0x00001809: /* (Loc,"en_IE") */
2726 switch(dwFlags)
2728 case DATE_SHORTDATE:
2729 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2730 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2731 if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
2732 return TRUE;
2733 case DATE_LONGDATE:
2734 if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
2735 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2736 return TRUE;
2737 default:
2738 FIXME("Unknown date format (%ld)\n", dwFlags);
2739 SetLastError(ERROR_INVALID_PARAMETER);
2740 return FALSE;
2744 case 0x00001c09: /* (Loc,"en_ZA") */
2746 switch(dwFlags)
2748 case DATE_SHORTDATE:
2749 if(!(*lpDateFmtEnumProc)("yy/MM/dd")) return TRUE;
2750 return TRUE;
2751 case DATE_LONGDATE:
2752 if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
2753 return TRUE;
2754 default:
2755 FIXME("Unknown date format (%ld)\n", dwFlags);
2756 SetLastError(ERROR_INVALID_PARAMETER);
2757 return FALSE;
2761 case 0x00002009: /* (Loc,"en_JM") */
2763 switch(dwFlags)
2765 case DATE_SHORTDATE:
2766 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2767 return TRUE;
2768 case DATE_LONGDATE:
2769 if(!(*lpDateFmtEnumProc)("dddd,MMMM dd,yyyy")) return TRUE;
2770 if(!(*lpDateFmtEnumProc)("MMMM dd,yyyy")) return TRUE;
2771 if(!(*lpDateFmtEnumProc)("dddd,dd MMMM,yyyy")) return TRUE;
2772 if(!(*lpDateFmtEnumProc)("dd MMMM,yyyy")) return TRUE;
2773 return TRUE;
2774 default:
2775 FIXME("Unknown date format (%ld)\n", dwFlags);
2776 SetLastError(ERROR_INVALID_PARAMETER);
2777 return FALSE;
2781 case 0x00002809: /* (Loc,"en_BZ") */
2782 case 0x00002c09: /* (Loc,"en_TT") */
2784 switch(dwFlags)
2786 case DATE_SHORTDATE:
2787 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2788 return TRUE;
2789 case DATE_LONGDATE:
2790 if(!(*lpDateFmtEnumProc)("dddd,dd MMMM yyyy")) return TRUE;
2791 return TRUE;
2792 default:
2793 FIXME("Unknown date format (%ld)\n", dwFlags);
2794 SetLastError(ERROR_INVALID_PARAMETER);
2795 return FALSE;
2799 default: /* default to US English "en_US" */
2801 switch(dwFlags)
2803 case DATE_SHORTDATE:
2804 if(!(*lpDateFmtEnumProc)("M/d/yy")) return TRUE;
2805 if(!(*lpDateFmtEnumProc)("M/d/yyyy")) return TRUE;
2806 if(!(*lpDateFmtEnumProc)("MM/dd/yy")) return TRUE;
2807 if(!(*lpDateFmtEnumProc)("MM/dd/yyyy")) return TRUE;
2808 if(!(*lpDateFmtEnumProc)("yy/MM/dd")) return TRUE;
2809 if(!(*lpDateFmtEnumProc)("dd-MMM-yy")) return TRUE;
2810 return TRUE;
2811 case DATE_LONGDATE:
2812 if(!(*lpDateFmtEnumProc)("dddd, MMMM dd, yyyy")) return TRUE;
2813 if(!(*lpDateFmtEnumProc)("MMMM dd, yyyy")) return TRUE;
2814 if(!(*lpDateFmtEnumProc)("dddd, dd MMMM, yyyy")) return TRUE;
2815 if(!(*lpDateFmtEnumProc)("dd MMMM, yyyy")) return TRUE;
2816 return TRUE;
2817 default:
2818 FIXME("Unknown date format (%ld)\n", dwFlags);
2819 SetLastError(ERROR_INVALID_PARAMETER);
2820 return FALSE;
2826 /**************************************************************************
2827 * EnumDateFormatsW (KERNEL32.@)
2829 BOOL WINAPI EnumDateFormatsW(
2830 DATEFMT_ENUMPROCW lpDateFmtEnumProc, LCID Locale, DWORD dwFlags)
2832 FIXME("(%p, %ld, %ld): stub\n", lpDateFmtEnumProc, Locale, dwFlags);
2833 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2834 return FALSE;
2837 /**************************************************************************
2838 * EnumTimeFormatsA (KERNEL32.@)
2840 BOOL WINAPI EnumTimeFormatsA(
2841 TIMEFMT_ENUMPROCA lpTimeFmtEnumProc, LCID Locale, DWORD dwFlags)
2843 LCID Loc = GetUserDefaultLCID();
2844 if(!lpTimeFmtEnumProc)
2846 SetLastError(ERROR_INVALID_PARAMETER);
2847 return FALSE;
2849 if(dwFlags)
2851 FIXME("Unknown time format (%ld)\n", dwFlags);
2854 switch( Loc )
2856 case 0x00000407: /* (Loc,"de_DE") */
2858 if(!(*lpTimeFmtEnumProc)("HH.mm")) return TRUE;
2859 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2860 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2861 if(!(*lpTimeFmtEnumProc)("H.mm")) return TRUE;
2862 if(!(*lpTimeFmtEnumProc)("H.mm'Uhr'")) return TRUE;
2863 return TRUE;
2866 case 0x0000040c: /* (Loc,"fr_FR") */
2867 case 0x00000c0c: /* (Loc,"fr_CA") */
2869 if(!(*lpTimeFmtEnumProc)("H:mm")) return TRUE;
2870 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2871 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2872 if(!(*lpTimeFmtEnumProc)("HH.mm")) return TRUE;
2873 if(!(*lpTimeFmtEnumProc)("HH'h'mm")) return TRUE;
2874 return TRUE;
2877 case 0x00000809: /* (Loc,"en_UK") */
2878 case 0x00000c09: /* (Loc,"en_AU") */
2879 case 0x00001409: /* (Loc,"en_NZ") */
2880 case 0x00001809: /* (Loc,"en_IE") */
2882 if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
2883 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2884 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2885 return TRUE;
2888 case 0x00001c09: /* (Loc,"en_ZA") */
2889 case 0x00002809: /* (Loc,"en_BZ") */
2890 case 0x00002c09: /* (Loc,"en_TT") */
2892 if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
2893 if(!(*lpTimeFmtEnumProc)("hh:mm:ss tt")) return TRUE;
2894 return TRUE;
2897 default: /* default to US style "en_US" */
2899 if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
2900 if(!(*lpTimeFmtEnumProc)("hh:mm:ss tt")) return TRUE;
2901 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2902 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2903 return TRUE;
2908 /**************************************************************************
2909 * EnumTimeFormatsW (KERNEL32.@)
2911 BOOL WINAPI EnumTimeFormatsW(
2912 TIMEFMT_ENUMPROCW lpTimeFmtEnumProc, LCID Locale, DWORD dwFlags)
2914 FIXME("(%p,%ld,%ld): stub\n", lpTimeFmtEnumProc, Locale, dwFlags);
2915 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2916 return FALSE;
2919 /**************************************************************************
2920 * This function is used just locally !
2921 * Description: Inverts a string.
2923 static void OLE_InvertString(char* string)
2925 char sTmpArray[128];
2926 INT counter, i = 0;
2928 for (counter = strlen(string); counter > 0; counter--)
2930 memcpy(sTmpArray + i, string + counter-1, 1);
2931 i++;
2933 memcpy(sTmpArray + i, "\0", 1);
2934 strcpy(string, sTmpArray);
2937 /***************************************************************************************
2938 * This function is used just locally !
2939 * Description: Test if the given string (psNumber) is valid or not.
2940 * The valid characters are the following:
2941 * - Characters '0' through '9'.
2942 * - One decimal point (dot) if the number is a floating-point value.
2943 * - A minus sign in the first character position if the number is
2944 * a negative value.
2945 * If the function succeeds, psBefore/psAfter will point to the string
2946 * on the right/left of the decimal symbol. pbNegative indicates if the
2947 * number is negative.
2949 static INT OLE_GetNumberComponents(char* pInput, char* psBefore, char* psAfter, BOOL* pbNegative)
2951 char sNumberSet[] = "0123456789";
2952 BOOL bInDecimal = FALSE;
2954 /* Test if we do have a minus sign */
2955 if ( *pInput == '-' )
2957 *pbNegative = TRUE;
2958 pInput++; /* Jump to the next character. */
2961 while(*pInput != '\0')
2963 /* Do we have a valid numeric character */
2964 if ( strchr(sNumberSet, *pInput) != NULL )
2966 if (bInDecimal == TRUE)
2967 *psAfter++ = *pInput;
2968 else
2969 *psBefore++ = *pInput;
2971 else
2973 /* Is this a decimal point (dot) */
2974 if ( *pInput == '.' )
2976 /* Is it the first time we find it */
2977 if ((bInDecimal == FALSE))
2978 bInDecimal = TRUE;
2979 else
2980 return -1; /* ERROR: Invalid parameter */
2982 else
2984 /* It's neither a numeric character, nor a decimal point.
2985 * Thus, return an error.
2987 return -1;
2990 pInput++;
2993 /* Add an End of Line character to the output buffers */
2994 *psBefore = '\0';
2995 *psAfter = '\0';
2997 return 0;
3000 /**************************************************************************
3001 * This function is used just locally !
3002 * Description: A number could be formatted using different numbers
3003 * of "digits in group" (example: 4;3;2;0).
3004 * The first parameter of this function is an array
3005 * containing the rule to be used. Its format is the following:
3006 * |NDG|DG1|DG2|...|0|
3007 * where NDG is the number of used "digits in group" and DG1, DG2,
3008 * are the corresponding "digits in group".
3009 * Thus, this function returns the grouping value in the array
3010 * pointed by the second parameter.
3012 static INT OLE_GetGrouping(char* sRule, INT index)
3014 char sData[2], sRuleSize[2];
3015 INT nData, nRuleSize;
3017 memcpy(sRuleSize, sRule, 1);
3018 memcpy(sRuleSize+1, "\0", 1);
3019 nRuleSize = atoi(sRuleSize);
3021 if (index > 0 && index < nRuleSize)
3023 memcpy(sData, sRule+index, 1);
3024 memcpy(sData+1, "\0", 1);
3025 nData = atoi(sData);
3028 else
3030 memcpy(sData, sRule+nRuleSize-1, 1);
3031 memcpy(sData+1, "\0", 1);
3032 nData = atoi(sData);
3035 return nData;
3038 /**************************************************************************
3039 * GetNumberFormatA (KERNEL32.@)
3041 INT WINAPI GetNumberFormatA(LCID locale, DWORD dwflags,
3042 LPCSTR lpvalue, const NUMBERFMTA * lpFormat,
3043 LPSTR lpNumberStr, int cchNumber)
3045 char sNumberDigits[3], sDecimalSymbol[5], sDigitsInGroup[11], sDigitGroupSymbol[5], sILZero[2];
3046 INT nNumberDigits, nNumberDecimal, i, j, nCounter, nStep, nRuleIndex, nGrouping, nDigits, retVal, nLZ;
3047 char sNumber[128], sDestination[128], sDigitsAfterDecimal[10], sDigitsBeforeDecimal[128];
3048 char sRule[10], sSemiColumn[]=";", sBuffer[5], sNegNumber[2];
3049 char *pStr = NULL, *pTmpStr = NULL;
3050 LCID systemDefaultLCID;
3051 BOOL bNegative = FALSE;
3052 enum Operations
3054 USE_PARAMETER,
3055 USE_LOCALEINFO,
3056 USE_SYSTEMDEFAULT,
3057 RETURN_ERROR
3058 } used_operation;
3060 strncpy(sNumber, lpvalue, 128);
3061 sNumber[127] = '\0';
3063 /* Make sure we have a valid input string, get the number
3064 * of digits before and after the decimal symbol, and check
3065 * if this is a negative number.
3067 if ( OLE_GetNumberComponents(sNumber, sDigitsBeforeDecimal, sDigitsAfterDecimal, &bNegative) != -1)
3069 nNumberDecimal = strlen(sDigitsBeforeDecimal);
3070 nDigits = strlen(sDigitsAfterDecimal);
3072 else
3074 SetLastError(ERROR_INVALID_PARAMETER);
3075 return 0;
3078 /* Which source will we use to format the string */
3079 used_operation = RETURN_ERROR;
3080 if (lpFormat != NULL)
3082 if (dwflags == 0)
3083 used_operation = USE_PARAMETER;
3085 else
3087 if (dwflags & LOCALE_NOUSEROVERRIDE)
3088 used_operation = USE_LOCALEINFO;
3089 else
3090 used_operation = USE_SYSTEMDEFAULT;
3093 /* Load the fields we need */
3094 switch(used_operation)
3096 case USE_LOCALEINFO:
3097 GetLocaleInfoA(locale, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
3098 GetLocaleInfoA(locale, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
3099 GetLocaleInfoA(locale, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
3100 GetLocaleInfoA(locale, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
3101 GetLocaleInfoA(locale, LOCALE_ILZERO, sILZero, sizeof(sILZero));
3102 GetLocaleInfoA(locale, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
3103 break;
3104 case USE_PARAMETER:
3105 sprintf(sNumberDigits, "%d",lpFormat->NumDigits);
3106 strcpy(sDecimalSymbol, lpFormat->lpDecimalSep);
3107 sprintf(sDigitsInGroup, "%d;0",lpFormat->Grouping);
3108 strcpy(sDigitGroupSymbol, lpFormat->lpThousandSep);
3109 sprintf(sILZero, "%d",lpFormat->LeadingZero);
3110 sprintf(sNegNumber, "%d",lpFormat->NegativeOrder);
3111 break;
3112 case USE_SYSTEMDEFAULT:
3113 systemDefaultLCID = GetSystemDefaultLCID();
3114 GetLocaleInfoA(systemDefaultLCID, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
3115 GetLocaleInfoA(systemDefaultLCID, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
3116 GetLocaleInfoA(systemDefaultLCID, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
3117 GetLocaleInfoA(systemDefaultLCID, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
3118 GetLocaleInfoA(systemDefaultLCID, LOCALE_ILZERO, sILZero, sizeof(sILZero));
3119 GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
3120 break;
3121 default:
3122 SetLastError(ERROR_INVALID_PARAMETER);
3123 return 0;
3126 nNumberDigits = atoi(sNumberDigits);
3128 /* Remove the ";" */
3129 i=0;
3130 j = 1;
3131 for (nCounter=0; nCounter<strlen(sDigitsInGroup); nCounter++)
3133 if ( memcmp(sDigitsInGroup + nCounter, sSemiColumn, 1) != 0 )
3135 memcpy(sRule + j, sDigitsInGroup + nCounter, 1);
3136 i++;
3137 j++;
3140 sprintf(sBuffer, "%d", i);
3141 memcpy(sRule, sBuffer, 1); /* Number of digits in the groups ( used by OLE_GetGrouping() ) */
3142 memcpy(sRule + j, "\0", 1);
3144 /* First, format the digits before the decimal. */
3145 if ((nNumberDecimal>0) && (atoi(sDigitsBeforeDecimal) != 0))
3147 /* Working on an inverted string is easier ! */
3148 OLE_InvertString(sDigitsBeforeDecimal);
3150 nStep = nCounter = i = j = 0;
3151 nRuleIndex = 1;
3152 nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
3154 /* Here, we will loop until we reach the end of the string.
3155 * An internal counter (j) is used in order to know when to
3156 * insert the "digit group symbol".
3158 while (nNumberDecimal > 0)
3160 i = nCounter + nStep;
3161 memcpy(sDestination + i, sDigitsBeforeDecimal + nCounter, 1);
3162 nCounter++;
3163 j++;
3164 if (j >= nGrouping)
3166 j = 0;
3167 if (nRuleIndex < sRule[0])
3168 nRuleIndex++;
3169 nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
3170 memcpy(sDestination + i+1, sDigitGroupSymbol, strlen(sDigitGroupSymbol));
3171 nStep+= strlen(sDigitGroupSymbol);
3174 nNumberDecimal--;
3177 memcpy(sDestination + i+1, "\0", 1);
3178 /* Get the string in the right order ! */
3179 OLE_InvertString(sDestination);
3181 else
3183 nLZ = atoi(sILZero);
3184 if (nLZ != 0)
3186 /* Use 0.xxx instead of .xxx */
3187 memcpy(sDestination, "0", 1);
3188 memcpy(sDestination+1, "\0", 1);
3190 else
3191 memcpy(sDestination, "\0", 1);
3195 /* Second, format the digits after the decimal. */
3196 j = 0;
3197 nCounter = nNumberDigits;
3198 if ( (nDigits>0) && (pStr = strstr (sNumber, ".")) )
3200 i = strlen(sNumber) - strlen(pStr) + 1;
3201 strncpy ( sDigitsAfterDecimal, sNumber + i, nNumberDigits);
3202 j = strlen(sDigitsAfterDecimal);
3203 if (j < nNumberDigits)
3204 nCounter = nNumberDigits-j;
3206 for (i=0;i<nCounter;i++)
3207 memcpy(sDigitsAfterDecimal+i+j, "0", 1);
3208 memcpy(sDigitsAfterDecimal + nNumberDigits, "\0", 1);
3210 i = strlen(sDestination);
3211 j = strlen(sDigitsAfterDecimal);
3212 /* Finally, construct the resulting formatted string. */
3214 for (nCounter=0; nCounter<i; nCounter++)
3215 memcpy(sNumber + nCounter, sDestination + nCounter, 1);
3217 memcpy(sNumber + nCounter, sDecimalSymbol, strlen(sDecimalSymbol));
3219 for (i=0; i<j; i++)
3220 memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), sDigitsAfterDecimal + i, 1);
3221 memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), "\0", 1);
3223 /* Is it a negative number */
3224 if (bNegative == TRUE)
3226 i = atoi(sNegNumber);
3227 pStr = sDestination;
3228 pTmpStr = sNumber;
3229 switch (i)
3231 case 0:
3232 *pStr++ = '(';
3233 while (*sNumber != '\0')
3234 *pStr++ = *pTmpStr++;
3235 *pStr++ = ')';
3236 break;
3237 case 1:
3238 *pStr++ = '-';
3239 while (*pTmpStr != '\0')
3240 *pStr++ = *pTmpStr++;
3241 break;
3242 case 2:
3243 *pStr++ = '-';
3244 *pStr++ = ' ';
3245 while (*pTmpStr != '\0')
3246 *pStr++ = *pTmpStr++;
3247 break;
3248 case 3:
3249 while (*pTmpStr != '\0')
3250 *pStr++ = *pTmpStr++;
3251 *pStr++ = '-';
3252 break;
3253 case 4:
3254 while (*pTmpStr != '\0')
3255 *pStr++ = *pTmpStr++;
3256 *pStr++ = ' ';
3257 *pStr++ = '-';
3258 break;
3259 default:
3260 while (*pTmpStr != '\0')
3261 *pStr++ = *pTmpStr++;
3262 break;
3265 else
3266 strcpy(sDestination, sNumber);
3268 /* If cchNumber is zero, then returns the number of bytes or characters
3269 * required to hold the formatted number string
3271 if (cchNumber==0)
3272 retVal = strlen(sDestination) + 1;
3273 else
3275 strncpy (lpNumberStr, sDestination, cchNumber-1);
3276 *(lpNumberStr+cchNumber-1) = '\0'; /* ensure we got a NULL at the end */
3277 retVal = strlen(lpNumberStr);
3280 return retVal;
3283 /**************************************************************************
3284 * GetNumberFormatW (KERNEL32.@)
3286 INT WINAPI GetNumberFormatW(LCID locale, DWORD dwflags,
3287 LPCWSTR lpvalue, const NUMBERFMTW * lpFormat,
3288 LPWSTR lpNumberStr, int cchNumber)
3290 FIXME("%s: stub, no reformatting done\n",debugstr_w(lpvalue));
3292 lstrcpynW( lpNumberStr, lpvalue, cchNumber );
3293 return cchNumber? strlenW( lpNumberStr ) : 0;
3296 /**************************************************************************
3297 * GetCurrencyFormatA (KERNEL32.@)
3299 INT WINAPI GetCurrencyFormatA(LCID locale, DWORD dwflags,
3300 LPCSTR lpvalue, const CURRENCYFMTA * lpFormat,
3301 LPSTR lpCurrencyStr, int cchCurrency)
3303 UINT nPosOrder, nNegOrder;
3304 INT retVal;
3305 char sDestination[128], sNegOrder[8], sPosOrder[8], sCurrencySymbol[8];
3306 char *pDestination = sDestination;
3307 char sNumberFormated[128];
3308 char *pNumberFormated = sNumberFormated;
3309 LCID systemDefaultLCID;
3310 BOOL bIsPositive = FALSE, bValidFormat = FALSE;
3311 enum Operations
3313 USE_PARAMETER,
3314 USE_LOCALEINFO,
3315 USE_SYSTEMDEFAULT,
3316 RETURN_ERROR
3317 } used_operation;
3319 NUMBERFMTA numberFmt;
3321 /* Which source will we use to format the string */
3322 used_operation = RETURN_ERROR;
3323 if (lpFormat != NULL)
3325 if (dwflags == 0)
3326 used_operation = USE_PARAMETER;
3328 else
3330 if (dwflags & LOCALE_NOUSEROVERRIDE)
3331 used_operation = USE_LOCALEINFO;
3332 else
3333 used_operation = USE_SYSTEMDEFAULT;
3336 /* Load the fields we need */
3337 switch(used_operation)
3339 case USE_LOCALEINFO:
3340 /* Specific to CURRENCYFMTA */
3341 GetLocaleInfoA(locale, LOCALE_INEGCURR, sNegOrder, sizeof(sNegOrder));
3342 GetLocaleInfoA(locale, LOCALE_ICURRENCY, sPosOrder, sizeof(sPosOrder));
3343 GetLocaleInfoA(locale, LOCALE_SCURRENCY, sCurrencySymbol, sizeof(sCurrencySymbol));
3345 nPosOrder = atoi(sPosOrder);
3346 nNegOrder = atoi(sNegOrder);
3347 break;
3348 case USE_PARAMETER:
3349 /* Specific to CURRENCYFMTA */
3350 nNegOrder = lpFormat->NegativeOrder;
3351 nPosOrder = lpFormat->PositiveOrder;
3352 strcpy(sCurrencySymbol, lpFormat->lpCurrencySymbol);
3353 break;
3354 case USE_SYSTEMDEFAULT:
3355 systemDefaultLCID = GetSystemDefaultLCID();
3356 /* Specific to CURRENCYFMTA */
3357 GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGCURR, sNegOrder, sizeof(sNegOrder));
3358 GetLocaleInfoA(systemDefaultLCID, LOCALE_ICURRENCY, sPosOrder, sizeof(sPosOrder));
3359 GetLocaleInfoA(systemDefaultLCID, LOCALE_SCURRENCY, sCurrencySymbol, sizeof(sCurrencySymbol));
3361 nPosOrder = atoi(sPosOrder);
3362 nNegOrder = atoi(sNegOrder);
3363 break;
3364 default:
3365 SetLastError(ERROR_INVALID_PARAMETER);
3366 return 0;
3369 /* Construct a temporary number format structure */
3370 if (lpFormat != NULL)
3372 numberFmt.NumDigits = lpFormat->NumDigits;
3373 numberFmt.LeadingZero = lpFormat->LeadingZero;
3374 numberFmt.Grouping = lpFormat->Grouping;
3375 numberFmt.NegativeOrder = 0;
3376 numberFmt.lpDecimalSep = lpFormat->lpDecimalSep;
3377 numberFmt.lpThousandSep = lpFormat->lpThousandSep;
3378 bValidFormat = TRUE;
3381 /* Make a call to GetNumberFormatA() */
3382 if (*lpvalue == '-')
3384 bIsPositive = FALSE;
3385 retVal = GetNumberFormatA(locale,0,lpvalue+1,(bValidFormat)?&numberFmt:NULL,pNumberFormated,128);
3387 else
3389 bIsPositive = TRUE;
3390 retVal = GetNumberFormatA(locale,0,lpvalue,(bValidFormat)?&numberFmt:NULL,pNumberFormated,128);
3393 if (retVal == 0)
3394 return 0;
3396 /* construct the formatted string */
3397 if (bIsPositive)
3399 switch (nPosOrder)
3401 case 0: /* Prefix, no separation */
3402 strcpy (pDestination, sCurrencySymbol);
3403 strcat (pDestination, pNumberFormated);
3404 break;
3405 case 1: /* Suffix, no separation */
3406 strcpy (pDestination, pNumberFormated);
3407 strcat (pDestination, sCurrencySymbol);
3408 break;
3409 case 2: /* Prefix, 1 char separation */
3410 strcpy (pDestination, sCurrencySymbol);
3411 strcat (pDestination, " ");
3412 strcat (pDestination, pNumberFormated);
3413 break;
3414 case 3: /* Suffix, 1 char separation */
3415 strcpy (pDestination, pNumberFormated);
3416 strcat (pDestination, " ");
3417 strcat (pDestination, sCurrencySymbol);
3418 break;
3419 default:
3420 SetLastError(ERROR_INVALID_PARAMETER);
3421 return 0;
3424 else /* negative number */
3426 switch (nNegOrder)
3428 case 0: /* format: ($1.1) */
3429 strcpy (pDestination, "(");
3430 strcat (pDestination, sCurrencySymbol);
3431 strcat (pDestination, pNumberFormated);
3432 strcat (pDestination, ")");
3433 break;
3434 case 1: /* format: -$1.1 */
3435 strcpy (pDestination, "-");
3436 strcat (pDestination, sCurrencySymbol);
3437 strcat (pDestination, pNumberFormated);
3438 break;
3439 case 2: /* format: $-1.1 */
3440 strcpy (pDestination, sCurrencySymbol);
3441 strcat (pDestination, "-");
3442 strcat (pDestination, pNumberFormated);
3443 break;
3444 case 3: /* format: $1.1- */
3445 strcpy (pDestination, sCurrencySymbol);
3446 strcat (pDestination, pNumberFormated);
3447 strcat (pDestination, "-");
3448 break;
3449 case 4: /* format: (1.1$) */
3450 strcpy (pDestination, "(");
3451 strcat (pDestination, pNumberFormated);
3452 strcat (pDestination, sCurrencySymbol);
3453 strcat (pDestination, ")");
3454 break;
3455 case 5: /* format: -1.1$ */
3456 strcpy (pDestination, "-");
3457 strcat (pDestination, pNumberFormated);
3458 strcat (pDestination, sCurrencySymbol);
3459 break;
3460 case 6: /* format: 1.1-$ */
3461 strcpy (pDestination, pNumberFormated);
3462 strcat (pDestination, "-");
3463 strcat (pDestination, sCurrencySymbol);
3464 break;
3465 case 7: /* format: 1.1$- */
3466 strcpy (pDestination, pNumberFormated);
3467 strcat (pDestination, sCurrencySymbol);
3468 strcat (pDestination, "-");
3469 break;
3470 case 8: /* format: -1.1 $ */
3471 strcpy (pDestination, "-");
3472 strcat (pDestination, pNumberFormated);
3473 strcat (pDestination, " ");
3474 strcat (pDestination, sCurrencySymbol);
3475 break;
3476 case 9: /* format: -$ 1.1 */
3477 strcpy (pDestination, "-");
3478 strcat (pDestination, sCurrencySymbol);
3479 strcat (pDestination, " ");
3480 strcat (pDestination, pNumberFormated);
3481 break;
3482 case 10: /* format: 1.1 $- */
3483 strcpy (pDestination, pNumberFormated);
3484 strcat (pDestination, " ");
3485 strcat (pDestination, sCurrencySymbol);
3486 strcat (pDestination, "-");
3487 break;
3488 case 11: /* format: $ 1.1- */
3489 strcpy (pDestination, sCurrencySymbol);
3490 strcat (pDestination, " ");
3491 strcat (pDestination, pNumberFormated);
3492 strcat (pDestination, "-");
3493 break;
3494 case 12: /* format: $ -1.1 */
3495 strcpy (pDestination, sCurrencySymbol);
3496 strcat (pDestination, " ");
3497 strcat (pDestination, "-");
3498 strcat (pDestination, pNumberFormated);
3499 break;
3500 case 13: /* format: 1.1- $ */
3501 strcpy (pDestination, pNumberFormated);
3502 strcat (pDestination, "-");
3503 strcat (pDestination, " ");
3504 strcat (pDestination, sCurrencySymbol);
3505 break;
3506 case 14: /* format: ($ 1.1) */
3507 strcpy (pDestination, "(");
3508 strcat (pDestination, sCurrencySymbol);
3509 strcat (pDestination, " ");
3510 strcat (pDestination, pNumberFormated);
3511 strcat (pDestination, ")");
3512 break;
3513 case 15: /* format: (1.1 $) */
3514 strcpy (pDestination, "(");
3515 strcat (pDestination, pNumberFormated);
3516 strcat (pDestination, " ");
3517 strcat (pDestination, sCurrencySymbol);
3518 strcat (pDestination, ")");
3519 break;
3520 default:
3521 SetLastError(ERROR_INVALID_PARAMETER);
3522 return 0;
3526 if (cchCurrency == 0)
3527 return strlen(pDestination) + 1;
3529 else
3531 strncpy (lpCurrencyStr, pDestination, cchCurrency-1);
3532 *(lpCurrencyStr+cchCurrency-1) = '\0'; /* ensure we got a NULL at the end */
3533 return strlen(lpCurrencyStr);
3537 /**************************************************************************
3538 * GetCurrencyFormatW (KERNEL32.@)
3540 INT WINAPI GetCurrencyFormatW(LCID locale, DWORD dwflags,
3541 LPCWSTR lpvalue, const CURRENCYFMTW * lpFormat,
3542 LPWSTR lpCurrencyStr, int cchCurrency)
3544 FIXME("This API function is NOT implemented !\n");
3545 return 0;
3548 /******************************************************************************
3549 * OLE2NLS_CheckLocale [intern]
3551 static LCID OLE2NLS_CheckLocale (LCID locale)
3553 if (!locale)
3554 { locale = LOCALE_SYSTEM_DEFAULT;
3557 if (locale == LOCALE_SYSTEM_DEFAULT)
3558 { return GetSystemDefaultLCID();
3560 else if (locale == LOCALE_USER_DEFAULT)
3561 { return GetUserDefaultLCID();
3563 else
3564 { return locale;
3567 /******************************************************************************
3568 * GetTimeFormatA [KERNEL32.@]
3569 * Makes an ASCII string of the time
3571 * Formats date according to format, or locale default if format is
3572 * NULL. The format consists of literal characters and fields as follows:
3574 * h hours with no leading zero (12-hour)
3575 * hh hours with full two digits
3576 * H hours with no leading zero (24-hour)
3577 * HH hours with full two digits
3578 * m minutes with no leading zero
3579 * mm minutes with full two digits
3580 * s seconds with no leading zero
3581 * ss seconds with full two digits
3582 * t time marker (A or P)
3583 * tt time marker (AM, PM)
3586 INT WINAPI
3587 GetTimeFormatA(LCID locale, /* [in] */
3588 DWORD flags, /* [in] */
3589 const SYSTEMTIME* xtime, /* [in] */
3590 LPCSTR format, /* [in] */
3591 LPSTR timestr, /* [out] */
3592 INT timelen /* [in] */)
3593 { char format_buf[40];
3594 LPCSTR thisformat;
3595 SYSTEMTIME t;
3596 const SYSTEMTIME* thistime;
3597 LCID thislocale=0;
3598 DWORD thisflags=LOCALE_STIMEFORMAT; /* standard timeformat */
3599 INT ret;
3601 TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,xtime,format,timestr,timelen);
3603 thislocale = OLE2NLS_CheckLocale ( locale );
3605 if (format == NULL)
3606 { if (flags & LOCALE_NOUSEROVERRIDE) /* use system default */
3607 { thislocale = GetSystemDefaultLCID();
3609 GetLocaleInfoA(thislocale, thisflags, format_buf, sizeof(format_buf));
3610 thisformat = format_buf;
3612 else
3613 { thisformat = format;
3616 if (xtime == NULL) /* NULL means use the current local time */
3617 { GetLocalTime(&t);
3618 thistime = &t;
3620 else
3621 { thistime = xtime;
3622 /* Check that hour,min and sec is in range */
3624 ret = OLE_GetFormatA(thislocale, thisflags, flags, thistime, thisformat,
3625 timestr, timelen);
3626 return ret;
3630 /******************************************************************************
3631 * GetTimeFormatW [KERNEL32.@]
3632 * Makes a Unicode string of the time
3634 INT WINAPI
3635 GetTimeFormatW(LCID locale, /* [in] */
3636 DWORD flags, /* [in] */
3637 const SYSTEMTIME* xtime, /* [in] */
3638 LPCWSTR format, /* [in] */
3639 LPWSTR timestr, /* [out] */
3640 INT timelen /* [in] */)
3641 { WCHAR format_buf[40];
3642 LPCWSTR thisformat;
3643 SYSTEMTIME t;
3644 const SYSTEMTIME* thistime;
3645 LCID thislocale=0;
3646 DWORD thisflags=LOCALE_STIMEFORMAT; /* standard timeformat */
3647 INT ret;
3649 TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,
3650 xtime,debugstr_w(format),timestr,timelen);
3652 thislocale = OLE2NLS_CheckLocale ( locale );
3654 if (format == NULL)
3655 { if (flags & LOCALE_NOUSEROVERRIDE) /* use system default */
3656 { thislocale = GetSystemDefaultLCID();
3658 GetLocaleInfoW(thislocale, thisflags, format_buf, 40);
3659 thisformat = format_buf;
3661 else
3662 { thisformat = format;
3665 if (xtime == NULL) /* NULL means use the current local time */
3666 { GetLocalTime(&t);
3667 thistime = &t;
3669 else
3670 { thistime = xtime;
3673 ret = OLE_GetFormatW(thislocale, thisflags, flags, thistime, thisformat,
3674 timestr, timelen);
3675 return ret;
3678 /******************************************************************************
3679 * EnumCalendarInfoA [KERNEL32.@]
3681 BOOL WINAPI EnumCalendarInfoA(
3682 CALINFO_ENUMPROCA calinfoproc,LCID locale,CALID calendar,CALTYPE caltype
3684 FIXME("(%p,0x%04lx,0x%08lx,0x%08lx),stub!\n",calinfoproc,locale,calendar,caltype);
3685 return FALSE;