Compile fix for GCC 2.7.x.
[wine/multimedia.git] / ole / ole2nls.c
blob45d4aa23fa006e6586a4a2feaef887b4052567bf
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
7 */
9 #include <string.h>
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <stdlib.h>
13 #include <locale.h>
15 #include "windef.h"
16 #include "winbase.h"
17 #include "wingdi.h"
18 #include "winuser.h"
19 #include "wine/unicode.h"
20 #include "options.h"
21 #include "winver.h"
22 #include "winnls.h"
23 #include "winreg.h"
24 #include "winerror.h"
25 #include "debugtools.h"
27 DEFAULT_DEBUG_CHANNEL(string);
29 /* Locale name to id map. used by EnumSystemLocales, GetLocaleInfoA
30 * MUST contain all #defines from winnls.h
31 * last entry has NULL name, 0 id.
32 */
33 #define LOCALE_ENTRY(x) {#x,LOCALE_##x}
34 static const struct tagLOCALE_NAME2ID {
35 const char *name;
36 LCTYPE id;
37 } locale_name2id[]= {
38 LOCALE_ENTRY(ILANGUAGE),
39 LOCALE_ENTRY(SLANGUAGE),
40 LOCALE_ENTRY(SENGLANGUAGE),
41 LOCALE_ENTRY(SABBREVLANGNAME),
42 LOCALE_ENTRY(SNATIVELANGNAME),
43 LOCALE_ENTRY(ICOUNTRY),
44 LOCALE_ENTRY(SCOUNTRY),
45 LOCALE_ENTRY(SENGCOUNTRY),
46 LOCALE_ENTRY(SABBREVCTRYNAME),
47 LOCALE_ENTRY(SNATIVECTRYNAME),
48 LOCALE_ENTRY(IDEFAULTLANGUAGE),
49 LOCALE_ENTRY(IDEFAULTCOUNTRY),
50 LOCALE_ENTRY(IDEFAULTCODEPAGE),
51 LOCALE_ENTRY(IDEFAULTANSICODEPAGE),
52 LOCALE_ENTRY(IDEFAULTMACCODEPAGE),
53 LOCALE_ENTRY(SLIST),
54 LOCALE_ENTRY(IMEASURE),
55 LOCALE_ENTRY(SDECIMAL),
56 LOCALE_ENTRY(STHOUSAND),
57 LOCALE_ENTRY(SGROUPING),
58 LOCALE_ENTRY(IDIGITS),
59 LOCALE_ENTRY(ILZERO),
60 LOCALE_ENTRY(INEGNUMBER),
61 LOCALE_ENTRY(SNATIVEDIGITS),
62 LOCALE_ENTRY(SCURRENCY),
63 LOCALE_ENTRY(SINTLSYMBOL),
64 LOCALE_ENTRY(SMONDECIMALSEP),
65 LOCALE_ENTRY(SMONTHOUSANDSEP),
66 LOCALE_ENTRY(SMONGROUPING),
67 LOCALE_ENTRY(ICURRDIGITS),
68 LOCALE_ENTRY(IINTLCURRDIGITS),
69 LOCALE_ENTRY(ICURRENCY),
70 LOCALE_ENTRY(INEGCURR),
71 LOCALE_ENTRY(SDATE),
72 LOCALE_ENTRY(STIME),
73 LOCALE_ENTRY(SSHORTDATE),
74 LOCALE_ENTRY(SLONGDATE),
75 LOCALE_ENTRY(STIMEFORMAT),
76 LOCALE_ENTRY(IDATE),
77 LOCALE_ENTRY(ILDATE),
78 LOCALE_ENTRY(ITIME),
79 LOCALE_ENTRY(ITIMEMARKPOSN),
80 LOCALE_ENTRY(ICENTURY),
81 LOCALE_ENTRY(ITLZERO),
82 LOCALE_ENTRY(IDAYLZERO),
83 LOCALE_ENTRY(IMONLZERO),
84 LOCALE_ENTRY(S1159),
85 LOCALE_ENTRY(S2359),
86 LOCALE_ENTRY(ICALENDARTYPE),
87 LOCALE_ENTRY(IOPTIONALCALENDAR),
88 LOCALE_ENTRY(IFIRSTDAYOFWEEK),
89 LOCALE_ENTRY(IFIRSTWEEKOFYEAR),
90 LOCALE_ENTRY(SDAYNAME1),
91 LOCALE_ENTRY(SDAYNAME2),
92 LOCALE_ENTRY(SDAYNAME3),
93 LOCALE_ENTRY(SDAYNAME4),
94 LOCALE_ENTRY(SDAYNAME5),
95 LOCALE_ENTRY(SDAYNAME6),
96 LOCALE_ENTRY(SDAYNAME7),
97 LOCALE_ENTRY(SABBREVDAYNAME1),
98 LOCALE_ENTRY(SABBREVDAYNAME2),
99 LOCALE_ENTRY(SABBREVDAYNAME3),
100 LOCALE_ENTRY(SABBREVDAYNAME4),
101 LOCALE_ENTRY(SABBREVDAYNAME5),
102 LOCALE_ENTRY(SABBREVDAYNAME6),
103 LOCALE_ENTRY(SABBREVDAYNAME7),
104 LOCALE_ENTRY(SMONTHNAME1),
105 LOCALE_ENTRY(SMONTHNAME2),
106 LOCALE_ENTRY(SMONTHNAME3),
107 LOCALE_ENTRY(SMONTHNAME4),
108 LOCALE_ENTRY(SMONTHNAME5),
109 LOCALE_ENTRY(SMONTHNAME6),
110 LOCALE_ENTRY(SMONTHNAME7),
111 LOCALE_ENTRY(SMONTHNAME8),
112 LOCALE_ENTRY(SMONTHNAME9),
113 LOCALE_ENTRY(SMONTHNAME10),
114 LOCALE_ENTRY(SMONTHNAME11),
115 LOCALE_ENTRY(SMONTHNAME12),
116 LOCALE_ENTRY(SMONTHNAME13),
117 LOCALE_ENTRY(SABBREVMONTHNAME1),
118 LOCALE_ENTRY(SABBREVMONTHNAME2),
119 LOCALE_ENTRY(SABBREVMONTHNAME3),
120 LOCALE_ENTRY(SABBREVMONTHNAME4),
121 LOCALE_ENTRY(SABBREVMONTHNAME5),
122 LOCALE_ENTRY(SABBREVMONTHNAME6),
123 LOCALE_ENTRY(SABBREVMONTHNAME7),
124 LOCALE_ENTRY(SABBREVMONTHNAME8),
125 LOCALE_ENTRY(SABBREVMONTHNAME9),
126 LOCALE_ENTRY(SABBREVMONTHNAME10),
127 LOCALE_ENTRY(SABBREVMONTHNAME11),
128 LOCALE_ENTRY(SABBREVMONTHNAME12),
129 LOCALE_ENTRY(SABBREVMONTHNAME13),
130 LOCALE_ENTRY(SPOSITIVESIGN),
131 LOCALE_ENTRY(SNEGATIVESIGN),
132 LOCALE_ENTRY(IPOSSIGNPOSN),
133 LOCALE_ENTRY(INEGSIGNPOSN),
134 LOCALE_ENTRY(IPOSSYMPRECEDES),
135 LOCALE_ENTRY(IPOSSEPBYSPACE),
136 LOCALE_ENTRY(INEGSYMPRECEDES),
137 LOCALE_ENTRY(INEGSEPBYSPACE),
138 LOCALE_ENTRY(FONTSIGNATURE),
139 LOCALE_ENTRY(SISO639LANGNAME),
140 LOCALE_ENTRY(SISO3166CTRYNAME),
141 {NULL,0}
144 static char *GetLocaleSubkeyName( DWORD lctype );
146 /***********************************************************************
147 * GetUserDefaultLCID (KERNEL32.@)
149 LCID WINAPI GetUserDefaultLCID(void)
151 return MAKELCID( GetUserDefaultLangID() , SORT_DEFAULT );
154 /***********************************************************************
155 * GetSystemDefaultLCID (KERNEL32.@)
157 LCID WINAPI GetSystemDefaultLCID(void)
159 return GetUserDefaultLCID();
162 #define NLS_MAX_LANGUAGES 20
163 typedef struct {
164 char lang[128];
165 char country[128];
166 LANGID found_lang_id[NLS_MAX_LANGUAGES];
167 char found_language[NLS_MAX_LANGUAGES][3];
168 char found_country[NLS_MAX_LANGUAGES][3];
169 int n_found;
170 } LANG_FIND_DATA;
172 static BOOL CALLBACK NLS_FindLanguageID_ProcA(HMODULE hModule, LPCSTR type,
173 LPCSTR name, WORD LangID, LONG lParam)
175 LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
176 LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
177 char buf_language[128];
178 char buf_country[128];
179 char buf_en_language[128];
181 TRACE("%04X\n", (UINT)LangID);
182 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
183 return TRUE; /* continue search */
185 buf_language[0] = 0;
186 buf_country[0] = 0;
188 GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE,
189 buf_language, sizeof(buf_language));
190 TRACE("LOCALE_SISO639LANGNAME: %s\n", buf_language);
192 GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
193 buf_country, sizeof(buf_country));
194 TRACE("LOCALE_SISO3166CTRYNAME: %s\n", buf_country);
196 if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_language))
198 if(l_data->country && strlen(l_data->country) > 0)
200 if(!strcasecmp(l_data->country, buf_country))
202 l_data->found_lang_id[0] = LangID;
203 l_data->n_found = 1;
204 TRACE("Found lang_id %04X for %s_%s\n", LangID, l_data->lang, l_data->country);
205 return FALSE; /* stop enumeration */
208 else /* l_data->country not specified */
210 if(l_data->n_found < NLS_MAX_LANGUAGES)
212 l_data->found_lang_id[l_data->n_found] = LangID;
213 strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
214 strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
215 l_data->n_found++;
216 TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
217 return TRUE; /* continue search */
222 /* Just in case, check LOCALE_SENGLANGUAGE too,
223 * in hope that possible alias name might have that value.
225 buf_en_language[0] = 0;
226 GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
227 buf_en_language, sizeof(buf_en_language));
228 TRACE("LOCALE_SENGLANGUAGE: %s\n", buf_en_language);
230 if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_en_language))
232 l_data->found_lang_id[l_data->n_found] = LangID;
233 strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
234 strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
235 l_data->n_found++;
236 TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
239 return TRUE; /* continue search */
242 /***********************************************************************
243 * NLS_GetLanguageID
245 * INPUT:
246 * Lang: a string whose two first chars are the iso name of a language.
247 * Country: a string whose two first chars are the iso name of country
248 * Charset: a string defining the chossen charset encoding
249 * Dialect: a string defining a variation of the locale
251 * all those values are from the standardized format of locale
252 * name in unix which is: Lang[_Country][.Charset][@Dialect]
254 * RETURNS:
255 * the numeric code of the language used by Windows
257 * FIXME: Charset and Dialect are not handled
259 static LANGID NLS_GetLanguageID(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
261 LANG_FIND_DATA l_data;
262 char lang_string[256];
264 if(!Lang)
266 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
267 goto END;
270 memset(&l_data, 0, sizeof(LANG_FIND_DATA));
271 strncpy(l_data.lang, Lang, sizeof(l_data.lang));
273 if(Country && strlen(Country) > 0)
274 strncpy(l_data.country, Country, sizeof(l_data.country));
276 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
277 (LPCSTR)LOCALE_ILANGUAGE, NLS_FindLanguageID_ProcA, (LONG)&l_data);
279 strcpy(lang_string, l_data.lang);
280 if(l_data.country && strlen(l_data.country) > 0)
282 strcat(lang_string, "_");
283 strcat(lang_string, l_data.country);
286 if(!l_data.n_found)
288 if(l_data.country && strlen(l_data.country) > 0)
290 MESSAGE("Warning: Language '%s' was not found, retrying without country name...\n", lang_string);
291 l_data.country[0] = 0;
292 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
293 (LPCSTR)LOCALE_ILANGUAGE, NLS_FindLanguageID_ProcA, (LONG)&l_data);
297 /* Re-evaluate lang_string */
298 strcpy(lang_string, l_data.lang);
299 if(l_data.country && strlen(l_data.country) > 0)
301 strcat(lang_string, "_");
302 strcat(lang_string, l_data.country);
305 if(!l_data.n_found)
307 MESSAGE("Warning: Language '%s' was not recognized, defaulting to English\n", lang_string);
308 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
310 else
312 if(l_data.n_found == 1)
313 TRACE("For language '%s' lang_id %04X was found\n", lang_string, l_data.found_lang_id[0]);
314 else /* l_data->n_found > 1 */
316 int i;
317 MESSAGE("For language '%s' several language ids were found:\n", lang_string);
318 for(i = 0; i < l_data.n_found; i++)
319 MESSAGE("%s_%s - %04X; ", l_data.found_language[i], l_data.found_country[i], l_data.found_lang_id[i]);
321 MESSAGE("\nInstead of using first in the list, suggest to define\n"
322 "your LANG environment variable like this: LANG=%s_%s\n",
323 l_data.found_language[0], l_data.found_country[0]);
326 END:
327 TRACE("Returning %04X\n", l_data.found_lang_id[0]);
328 return l_data.found_lang_id[0];
331 /***********************************************************************
332 * GetUserDefaultLangID (KERNEL32.@)
334 LANGID WINAPI GetUserDefaultLangID(void)
336 /* caching result, if defined from environment, which should (?) not change during a WINE session */
337 static LANGID userLCID = 0;
339 if (userLCID == 0)
341 char buf[256];
342 char *lang,*country,*charset,*dialect,*next;
344 if (GetEnvironmentVariableA( "LANGUAGE", buf, sizeof(buf) )) goto ok;
345 if (GetEnvironmentVariableA( "LANG", buf, sizeof(buf) )) goto ok;
346 if (GetEnvironmentVariableA( "LC_ALL", buf, sizeof(buf) )) goto ok;
347 if (GetEnvironmentVariableA( "LC_MESSAGES", buf, sizeof(buf) )) goto ok;
348 if (GetEnvironmentVariableA( "LC_CTYPE", buf, sizeof(buf) )) goto ok;
350 return userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
353 if (!strcmp(buf,"POSIX") || !strcmp(buf,"C"))
354 return userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
356 lang=buf;
358 do {
359 next=strchr(lang,':'); if (next) *next++='\0';
360 dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
361 charset=strchr(lang,'.'); if (charset) *charset++='\0';
362 country=strchr(lang,'_'); if (country) *country++='\0';
364 userLCID = NLS_GetLanguageID(lang, country, charset, dialect);
366 lang=next;
367 } while (lang && !userLCID);
369 if (!userLCID)
371 MESSAGE( "Warning: language '%s' not recognized, defaulting to English\n",
372 buf );
373 userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
376 return userLCID;
379 /***********************************************************************
380 * GetSystemDefaultLangID (KERNEL32.@)
382 LANGID WINAPI GetSystemDefaultLangID(void)
384 return GetUserDefaultLangID();
387 /******************************************************************************
388 * ConvertDefaultLocale (KERNEL32.@)
390 LCID WINAPI ConvertDefaultLocale (LCID lcid)
391 { switch (lcid)
392 { case LOCALE_SYSTEM_DEFAULT:
393 return GetSystemDefaultLCID();
394 case LOCALE_USER_DEFAULT:
395 return GetUserDefaultLCID();
396 case LOCALE_NEUTRAL:
397 return MAKELCID (LANG_NEUTRAL, SUBLANG_NEUTRAL);
399 return MAKELANGID( PRIMARYLANGID(lcid), SUBLANG_NEUTRAL);
402 /* Enhanced version of LoadStringW.
403 * It takes LanguageId to find and load language dependant resources.
404 * Resource is copied as is: "binary" strings are not truncated.
405 * Return: length of resource + 1 to distinguish absent resources
406 * from the resources with zero length.
408 static INT NLS_LoadStringExW(HMODULE hModule, LANGID lang_id, UINT res_id, LPWSTR buffer, INT buflen)
410 HRSRC hrsrc;
411 HGLOBAL hmem;
412 WCHAR *p;
413 int string_num;
414 int i;
416 /* Replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
417 if(SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
418 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
420 hrsrc = FindResourceExW(hModule, RT_STRINGW, (LPCWSTR)((res_id >> 4) + 1), lang_id);
422 if(!hrsrc) return 0;
423 hmem = LoadResource(hModule, hrsrc);
424 if(!hmem) return 0;
426 p = LockResource(hmem);
427 string_num = res_id & 0x000f;
428 for(i = 0; i < string_num; i++)
429 p += *p + 1;
431 TRACE("strlen = %d\n", (int)*p );
433 if (buffer == NULL) return *p;
434 i = min(buflen - 1, *p);
435 if (i > 0) {
436 memcpy(buffer, p + 1, i * sizeof (WCHAR));
437 buffer[i] = (WCHAR) 0;
438 } else {
439 if (buflen > 1)
440 buffer[0] = (WCHAR) 0;
443 FreeResource(hmem);
444 TRACE("%s loaded!\n", debugstr_w(buffer));
445 return (i + 1);
448 /******************************************************************************
449 * GetLocaleInfoA (KERNEL32.@)
451 * NOTES
452 * LANG_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
454 * MS online documentation states that the string returned is NULL terminated
455 * except for LOCALE_FONTSIGNATURE which "will return a non-NULL
456 * terminated string".
458 INT WINAPI GetLocaleInfoA(LCID lcid,LCTYPE LCType,LPSTR buf,INT len)
460 LPCSTR retString = NULL;
461 int found = 0, i;
462 char *pacKey;
463 char acBuffer[128];
464 DWORD dwBufferSize=128;
465 BOOL NoUserOverride;
467 TRACE("(lcid=0x%lx,lctype=0x%lx,%p,%x)\n",lcid,LCType,buf,len);
469 if (len && (! buf) ) {
470 SetLastError(ERROR_INSUFFICIENT_BUFFER);
471 return 0;
474 if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT)
476 lcid = GetSystemDefaultLCID();
478 else if (lcid == LANG_USER_DEFAULT) /*0x800*/
480 lcid = GetUserDefaultLCID();
483 /* LOCALE_NOUSEROVERRIDE means: do not get user redefined settings
484 from the registry. Instead, use system default values. */
485 NoUserOverride = (LCType & LOCALE_NOUSEROVERRIDE) != 0;
487 LCType &= ~(LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP);
489 /* First, check if it's in the registry. */
490 /* All user customized values are stored in the registry by SetLocaleInfo */
491 if ( !NoUserOverride && (pacKey = GetLocaleSubkeyName(LCType)) )
493 char acRealKey[128];
494 HKEY hKey;
496 sprintf( acRealKey, "Control Panel\\International\\%s", pacKey );
498 if ( RegOpenKeyExA( HKEY_CURRENT_USER, acRealKey,
499 0, KEY_READ, &hKey) == ERROR_SUCCESS )
501 if ( RegQueryValueExA( hKey, NULL, NULL, NULL, (LPBYTE)acBuffer,
502 &dwBufferSize ) == ERROR_SUCCESS )
504 retString = acBuffer;
505 found = 1;
507 RegCloseKey(hKey);
511 /* If not in the registry, get it from the NLS entries. */
512 if(!found) {
513 WCHAR wcBuffer[128];
514 int res_size;
516 /* check if language is registered in the kernel32 resources */
517 if((res_size = NLS_LoadStringExW(GetModuleHandleA("KERNEL32"), LANGIDFROMLCID(lcid),
518 LCType, wcBuffer, sizeof(wcBuffer)/sizeof(wcBuffer[0])))) {
519 WideCharToMultiByte(CP_ACP, 0, wcBuffer, res_size, acBuffer, dwBufferSize, NULL, NULL);
520 retString = acBuffer;
521 found = 1;
525 /* if not found report a most descriptive error */
526 if(!found) {
527 retString=0;
528 /* If we are through all of this, retLen should not be zero anymore.
529 If it is, the value is not supported */
530 i=0;
531 while (locale_name2id[i].name!=NULL) {
532 if (LCType == locale_name2id[i].id) {
533 retString = locale_name2id[i].name;
534 break;
536 i++;
538 if(!retString)
539 FIXME("Unkown LC type %lX\n", LCType);
540 else
541 FIXME("'%s' is not defined for your language (%04X).\n"
542 "Please define it in dlls/kernel/nls/YourLanguage.nls\n"
543 "and submit patch for inclusion into the next Wine release.\n",
544 retString, LOWORD(lcid));
545 SetLastError(ERROR_INVALID_PARAMETER);
546 return 0;
549 /* a FONTSIGNATURE is not a string, just 6 DWORDs */
550 if (LCType == LOCALE_FONTSIGNATURE) {
551 if (len) {
552 len = (len < sizeof(FONTSIGNATURE)) ? len : sizeof(FONTSIGNATURE);
553 memcpy(buf, retString, len);
554 return len;
556 return sizeof(FONTSIGNATURE);
558 /* if len=0 return only the length, don't touch the buffer*/
559 if (len) {
560 lstrcpynA(buf,retString,len);
561 return strlen(buf) + 1;
563 return strlen(retString)+1;
566 /******************************************************************************
567 * GetLocaleInfoW (KERNEL32.@)
569 * NOTES
570 * MS documentation states that len "specifies the size, in bytes (ANSI version)
571 * or characters (Unicode version), of" wbuf. Thus the number returned is
572 * the same between GetLocaleInfoW and GetLocaleInfoA.
574 INT WINAPI GetLocaleInfoW(LCID lcid,LCTYPE LCType,LPWSTR wbuf,INT len)
575 { WORD wlen;
576 LPSTR abuf;
578 if (len && (! wbuf) )
579 { SetLastError(ERROR_INSUFFICIENT_BUFFER);
580 return 0;
583 abuf = (LPSTR)HeapAlloc(GetProcessHeap(),0,len);
584 wlen = GetLocaleInfoA(lcid, LCType, abuf, len);
586 if (wlen && len) /* if len=0 return only the length*/
587 MultiByteToWideChar( CP_ACP, 0, abuf, -1, wbuf, len );
589 HeapFree(GetProcessHeap(),0,abuf);
590 return wlen;
593 /******************************************************************************
595 * GetLocaleSubkeyName [helper function]
597 * - For use with the registry.
598 * - Gets the registry subkey name for a given lctype.
600 static char *GetLocaleSubkeyName( DWORD lctype )
602 char *pacKey=NULL;
604 switch ( lctype )
606 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
607 * the values are stored in the registry, confirmed under Windows,
608 * for the ones that actually assign pacKey. Cases that aren't finished
609 * have not been confirmed, so that must be done before they can be
610 * added.
612 case LOCALE_SDATE : /* The date separator. */
613 pacKey = "sDate";
614 break;
615 case LOCALE_ICURRDIGITS:
616 pacKey = "iCurrDigits";
617 break;
618 case LOCALE_SDECIMAL :
619 pacKey = "sDecimal";
620 break;
621 case LOCALE_ICURRENCY:
622 pacKey = "iCurrency";
623 break;
624 case LOCALE_SGROUPING :
625 pacKey = "sGrouping";
626 break;
627 case LOCALE_IDIGITS:
628 pacKey = "iDigits";
629 break;
630 case LOCALE_SLIST :
631 pacKey = "sList";
632 break;
633 /* case LOCALE_ICALENDARTYPE: */
634 /* case LOCALE_IFIRSTDAYOFWEEK: */
635 /* case LOCALE_IFIRSTWEEKOFYEAR: */
636 /* case LOCALE_SYEARMONTH : */
637 /* case LOCALE_SPOSITIVESIGN : */
638 /* case LOCALE_IPAPERSIZE: */
639 /* break; */
640 case LOCALE_SLONGDATE :
641 pacKey = "sLongDate";
642 break;
643 case LOCALE_SMONDECIMALSEP :
644 pacKey = "sMonDecimalSep";
645 break;
646 case LOCALE_SMONGROUPING:
647 pacKey = "sMonGrouping";
648 break;
649 case LOCALE_IMEASURE:
650 pacKey = "iMeasure";
651 break;
652 case LOCALE_SMONTHOUSANDSEP :
653 pacKey = "sMonThousandSep";
654 break;
655 case LOCALE_INEGCURR:
656 pacKey = "iNegCurr";
657 break;
658 case LOCALE_SNEGATIVESIGN :
659 pacKey = "sNegativeSign";
660 break;
661 case LOCALE_INEGNUMBER:
662 pacKey = "iNegNumber";
663 break;
664 case LOCALE_SSHORTDATE :
665 pacKey = "sShortDate";
666 break;
667 case LOCALE_ILDATE: /* Long Date format ordering specifier. */
668 pacKey = "iLDate";
669 break;
670 case LOCALE_ILZERO:
671 pacKey = "iLZero";
672 break;
673 case LOCALE_ITLZERO:
674 pacKey = "iTLZero";
675 break;
676 case LOCALE_ITIME: /* Time format specifier. */
677 pacKey = "iTime";
678 break;
679 case LOCALE_STHOUSAND :
680 pacKey = "sThousand";
681 break;
682 case LOCALE_S1159: /* AM */
683 pacKey = "s1159";
684 break;
685 case LOCALE_STIME:
686 pacKey = "sTime";
687 break;
688 case LOCALE_S2359: /* PM */
689 pacKey = "s2359";
690 break;
691 case LOCALE_STIMEFORMAT :
692 pacKey = "sTimeFormat";
693 break;
694 case LOCALE_SCURRENCY:
695 pacKey = "sCurrency";
696 break;
698 /* The following are not listed under MSDN as supported,
699 * but seem to be used and also stored in the registry.
702 case LOCALE_IDATE:
703 pacKey = "iDate";
704 break;
705 case LOCALE_SCOUNTRY:
706 pacKey = "sCountry";
707 break;
708 case LOCALE_ICOUNTRY:
709 pacKey = "iCountry";
710 break;
711 case LOCALE_SLANGUAGE:
712 pacKey = "sLanguage";
713 break;
715 default:
716 break;
719 return( pacKey );
722 /******************************************************************************
723 * SetLocaleInfoA [KERNEL32.@]
725 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
727 HKEY hKey;
728 char *pacKey;
729 char acRealKey[128];
731 if ( (pacKey = GetLocaleSubkeyName(lctype)) )
733 sprintf( acRealKey, "Control Panel\\International\\%s", pacKey );
734 if ( RegCreateKeyA( HKEY_CURRENT_USER, acRealKey,
735 &hKey ) == ERROR_SUCCESS )
737 if ( RegSetValueExA( hKey, NULL, 0, REG_SZ,
738 data, strlen(data)+1 ) != ERROR_SUCCESS )
740 ERR("SetLocaleInfoA: %s did not work\n", pacKey );
742 RegCloseKey( hKey );
745 else
747 FIXME("(%ld,%ld,%s): stub\n",lcid,lctype,data);
749 return TRUE;
752 /******************************************************************************
753 * IsValidLocale [KERNEL32.@]
755 BOOL WINAPI IsValidLocale(LCID lcid,DWORD flags)
757 /* check if language is registered in the kernel32 resources */
758 if(!FindResourceExW(GetModuleHandleA("KERNEL32"), RT_STRINGW, (LPCWSTR)LOCALE_ILANGUAGE, LOWORD(lcid)))
759 return FALSE;
760 else
761 return TRUE;
764 static BOOL CALLBACK EnumResourceLanguagesProcW(HMODULE hModule, LPCWSTR type,
765 LPCWSTR name, WORD LangID, LONG lParam)
767 CHAR bufA[20];
768 WCHAR bufW[20];
769 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
770 sprintf(bufA, "%08X", (UINT)LangID);
771 MultiByteToWideChar(CP_ACP, 0, bufA, -1, bufW, sizeof(bufW)/sizeof(bufW[0]));
772 return lpfnLocaleEnum(bufW);
775 /******************************************************************************
776 * EnumSystemLocalesW [KERNEL32.@]
778 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum,
779 DWORD flags )
781 TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
783 EnumResourceLanguagesW(GetModuleHandleA("KERNEL32"), RT_STRINGW,
784 (LPCWSTR)LOCALE_ILANGUAGE, EnumResourceLanguagesProcW,
785 (LONG)lpfnLocaleEnum);
787 return TRUE;
790 static BOOL CALLBACK EnumResourceLanguagesProcA(HMODULE hModule, LPCSTR type,
791 LPCSTR name, WORD LangID, LONG lParam)
793 CHAR bufA[20];
794 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
795 sprintf(bufA, "%08X", (UINT)LangID);
796 return lpfnLocaleEnum(bufA);
799 /******************************************************************************
800 * EnumSystemLocalesA [KERNEL32.@]
802 BOOL WINAPI EnumSystemLocalesA(LOCALE_ENUMPROCA lpfnLocaleEnum,
803 DWORD flags)
805 TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
807 EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
808 (LPCSTR)LOCALE_ILANGUAGE, EnumResourceLanguagesProcA,
809 (LONG)lpfnLocaleEnum);
811 return TRUE;
814 /***********************************************************************
815 * VerLanguageNameA [KERNEL32.@]
817 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
819 if(!szLang)
820 return 0;
822 return GetLocaleInfoA(MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize);
825 /***********************************************************************
826 * VerLanguageNameW [KERNEL32.@]
828 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
830 if(!szLang)
831 return 0;
833 return GetLocaleInfoW(MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize);
837 static const unsigned char LCM_Unicode_LUT[] = {
838 6 , 3, /* - 1 */
839 6 , 4, /* - 2 */
840 6 , 5, /* - 3 */
841 6 , 6, /* - 4 */
842 6 , 7, /* - 5 */
843 6 , 8, /* - 6 */
844 6 , 9, /* - 7 */
845 6 , 10, /* - 8 */
846 7 , 5, /* - 9 */
847 7 , 6, /* - 10 */
848 7 , 7, /* - 11 */
849 7 , 8, /* - 12 */
850 7 , 9, /* - 13 */
851 6 , 11, /* - 14 */
852 6 , 12, /* - 15 */
853 6 , 13, /* - 16 */
854 6 , 14, /* - 17 */
855 6 , 15, /* - 18 */
856 6 , 16, /* - 19 */
857 6 , 17, /* - 20 */
858 6 , 18, /* - 21 */
859 6 , 19, /* - 22 */
860 6 , 20, /* - 23 */
861 6 , 21, /* - 24 */
862 6 , 22, /* - 25 */
863 6 , 23, /* - 26 */
864 6 , 24, /* - 27 */
865 6 , 25, /* - 28 */
866 6 , 26, /* - 29 */
867 6 , 27, /* - 30 */
868 6 , 28, /* - 31 */
869 7 , 2, /* - 32 */
870 7 , 28, /* ! - 33 */
871 7 , 29, /* " - 34 */ /* " */
872 7 , 31, /* # - 35 */
873 7 , 33, /* $ - 36 */
874 7 , 35, /* % - 37 */
875 7 , 37, /* & - 38 */
876 6 , 128, /* ' - 39 */
877 7 , 39, /* ( - 40 */
878 7 , 42, /* ) - 41 */
879 7 , 45, /* * - 42 */
880 8 , 3, /* + - 43 */
881 7 , 47, /* , - 44 */
882 6 , 130, /* - - 45 */
883 7 , 51, /* . - 46 */
884 7 , 53, /* / - 47 */
885 12 , 3, /* 0 - 48 */
886 12 , 33, /* 1 - 49 */
887 12 , 51, /* 2 - 50 */
888 12 , 70, /* 3 - 51 */
889 12 , 88, /* 4 - 52 */
890 12 , 106, /* 5 - 53 */
891 12 , 125, /* 6 - 54 */
892 12 , 144, /* 7 - 55 */
893 12 , 162, /* 8 - 56 */
894 12 , 180, /* 9 - 57 */
895 7 , 55, /* : - 58 */
896 7 , 58, /* ; - 59 */
897 8 , 14, /* < - 60 */
898 8 , 18, /* = - 61 */
899 8 , 20, /* > - 62 */
900 7 , 60, /* ? - 63 */
901 7 , 62, /* @ - 64 */
902 14 , 2, /* A - 65 */
903 14 , 9, /* B - 66 */
904 14 , 10, /* C - 67 */
905 14 , 26, /* D - 68 */
906 14 , 33, /* E - 69 */
907 14 , 35, /* F - 70 */
908 14 , 37, /* G - 71 */
909 14 , 44, /* H - 72 */
910 14 , 50, /* I - 73 */
911 14 , 53, /* J - 74 */
912 14 , 54, /* K - 75 */
913 14 , 72, /* L - 76 */
914 14 , 81, /* M - 77 */
915 14 , 112, /* N - 78 */
916 14 , 124, /* O - 79 */
917 14 , 126, /* P - 80 */
918 14 , 137, /* Q - 81 */
919 14 , 138, /* R - 82 */
920 14 , 145, /* S - 83 */
921 14 , 153, /* T - 84 */
922 14 , 159, /* U - 85 */
923 14 , 162, /* V - 86 */
924 14 , 164, /* W - 87 */
925 14 , 166, /* X - 88 */
926 14 , 167, /* Y - 89 */
927 14 , 169, /* Z - 90 */
928 7 , 63, /* [ - 91 */
929 7 , 65, /* \ - 92 */
930 7 , 66, /* ] - 93 */
931 7 , 67, /* ^ - 94 */
932 7 , 68, /* _ - 95 */
933 7 , 72, /* ` - 96 */
934 14 , 2, /* a - 97 */
935 14 , 9, /* b - 98 */
936 14 , 10, /* c - 99 */
937 14 , 26, /* d - 100 */
938 14 , 33, /* e - 101 */
939 14 , 35, /* f - 102 */
940 14 , 37, /* g - 103 */
941 14 , 44, /* h - 104 */
942 14 , 50, /* i - 105 */
943 14 , 53, /* j - 106 */
944 14 , 54, /* k - 107 */
945 14 , 72, /* l - 108 */
946 14 , 81, /* m - 109 */
947 14 , 112, /* n - 110 */
948 14 , 124, /* o - 111 */
949 14 , 126, /* p - 112 */
950 14 , 137, /* q - 113 */
951 14 , 138, /* r - 114 */
952 14 , 145, /* s - 115 */
953 14 , 153, /* t - 116 */
954 14 , 159, /* u - 117 */
955 14 , 162, /* v - 118 */
956 14 , 164, /* w - 119 */
957 14 , 166, /* x - 120 */
958 14 , 167, /* y - 121 */
959 14 , 169, /* z - 122 */
960 7 , 74, /* { - 123 */
961 7 , 76, /* | - 124 */
962 7 , 78, /* } - 125 */
963 7 , 80, /* ~ - 126 */
964 6 , 29, /* \x7f - 127 */
965 6 , 30, /* € - 128 */
966 6 , 31, /* � - 129 */
967 7 , 123, /* ‚ - 130 */
968 14 , 35, /* ƒ - 131 */
969 7 , 127, /* „ - 132 */
970 10 , 21, /* … - 133 */
971 10 , 15, /* † - 134 */
972 10 , 16, /* ‡ - 135 */
973 7 , 67, /* ˆ - 136 */
974 10 , 22, /* ‰ - 137 */
975 14 , 145, /* Š - 138 */
976 7 , 136, /* ‹ - 139 */
977 14 + 16 , 124, /* Œ - 140 */
978 6 , 43, /* � - 141 */
979 6 , 44, /* Ž - 142 */
980 6 , 45, /* � - 143 */
981 6 , 46, /* � - 144 */
982 7 , 121, /* ‘ - 145 */
983 7 , 122, /* ’ - 146 */
984 7 , 125, /* “ - 147 */
985 7 , 126, /* ” - 148 */
986 10 , 17, /* • - 149 */
987 6 , 137, /* – - 150 */
988 6 , 139, /* — - 151 */
989 7 , 93, /* ˜ - 152 */
990 14 , 156, /* ™ - 153 */
991 14 , 145, /* š - 154 */
992 7 , 137, /* › - 155 */
993 14 + 16 , 124, /* œ - 156 */
994 6 , 59, /* � - 157 */
995 6 , 60, /* ž - 158 */
996 14 , 167, /* Ÿ - 159 */
997 7 , 4, /*   - 160 */
998 7 , 81, /* ¡ - 161 */
999 10 , 2, /* ¢ - 162 */
1000 10 , 3, /* £ - 163 */
1001 10 , 4, /* ¤ - 164 */
1002 10 , 5, /* ¥ - 165 */
1003 7 , 82, /* ¦ - 166 */
1004 10 , 6, /* § - 167 */
1005 7 , 83, /* ¨ - 168 */
1006 10 , 7, /* © - 169 */
1007 14 , 2, /* ª - 170 */
1008 8 , 24, /* « - 171 */
1009 10 , 8, /* ¬ - 172 */
1010 6 , 131, /* ­ - 173 */
1011 10 , 9, /* ® - 174 */
1012 7 , 84, /* ¯ - 175 */
1013 10 , 10, /* ° - 176 */
1014 8 , 23, /* ± - 177 */
1015 12 , 51, /* ² - 178 */
1016 12 , 70, /* ³ - 179 */
1017 7 , 85, /* ´ - 180 */
1018 10 , 11, /* µ - 181 */
1019 10 , 12, /* ¶ - 182 */
1020 10 , 13, /* · - 183 */
1021 7 , 86, /* ¸ - 184 */
1022 12 , 33, /* ¹ - 185 */
1023 14 , 124, /* º - 186 */
1024 8 , 26, /* » - 187 */
1025 12 , 21, /* ¼ - 188 */
1026 12 , 25, /* ½ - 189 */
1027 12 , 29, /* ¾ - 190 */
1028 7 , 87, /* ¿ - 191 */
1029 14 , 2, /* À - 192 */
1030 14 , 2, /* Á - 193 */
1031 14 , 2, /* Â - 194 */
1032 14 , 2, /* Ã - 195 */
1033 14 , 2, /* Ä - 196 */
1034 14 , 2, /* Å - 197 */
1035 14 + 16 , 2, /* Æ - 198 */
1036 14 , 10, /* Ç - 199 */
1037 14 , 33, /* È - 200 */
1038 14 , 33, /* É - 201 */
1039 14 , 33, /* Ê - 202 */
1040 14 , 33, /* Ë - 203 */
1041 14 , 50, /* Ì - 204 */
1042 14 , 50, /* Í - 205 */
1043 14 , 50, /* Î - 206 */
1044 14 , 50, /* Ï - 207 */
1045 14 , 26, /* Ð - 208 */
1046 14 , 112, /* Ñ - 209 */
1047 14 , 124, /* Ò - 210 */
1048 14 , 124, /* Ó - 211 */
1049 14 , 124, /* Ô - 212 */
1050 14 , 124, /* Õ - 213 */
1051 14 , 124, /* Ö - 214 */
1052 8 , 28, /* × - 215 */
1053 14 , 124, /* Ø - 216 */
1054 14 , 159, /* Ù - 217 */
1055 14 , 159, /* Ú - 218 */
1056 14 , 159, /* Û - 219 */
1057 14 , 159, /* Ü - 220 */
1058 14 , 167, /* Ý - 221 */
1059 14 + 32 , 153, /* Þ - 222 */
1060 14 + 48 , 145, /* ß - 223 */
1061 14 , 2, /* à - 224 */
1062 14 , 2, /* á - 225 */
1063 14 , 2, /* â - 226 */
1064 14 , 2, /* ã - 227 */
1065 14 , 2, /* ä - 228 */
1066 14 , 2, /* å - 229 */
1067 14 + 16 , 2, /* æ - 230 */
1068 14 , 10, /* ç - 231 */
1069 14 , 33, /* è - 232 */
1070 14 , 33, /* é - 233 */
1071 14 , 33, /* ê - 234 */
1072 14 , 33, /* ë - 235 */
1073 14 , 50, /* ì - 236 */
1074 14 , 50, /* í - 237 */
1075 14 , 50, /* î - 238 */
1076 14 , 50, /* ï - 239 */
1077 14 , 26, /* ð - 240 */
1078 14 , 112, /* ñ - 241 */
1079 14 , 124, /* ò - 242 */
1080 14 , 124, /* ó - 243 */
1081 14 , 124, /* ô - 244 */
1082 14 , 124, /* õ - 245 */
1083 14 , 124, /* ö - 246 */
1084 8 , 29, /* ÷ - 247 */
1085 14 , 124, /* ø - 248 */
1086 14 , 159, /* ù - 249 */
1087 14 , 159, /* ú - 250 */
1088 14 , 159, /* û - 251 */
1089 14 , 159, /* ü - 252 */
1090 14 , 167, /* ý - 253 */
1091 14 + 32 , 153, /* þ - 254 */
1092 14 , 167 /* ÿ - 255 */ };
1094 static const unsigned char LCM_Unicode_LUT_2[] = { 33, 44, 145 };
1096 #define LCM_Diacritic_Start 131
1098 static const unsigned char LCM_Diacritic_LUT[] = {
1099 123, /* ƒ - 131 */
1100 2, /* „ - 132 */
1101 2, /* … - 133 */
1102 2, /* † - 134 */
1103 2, /* ‡ - 135 */
1104 3, /* ˆ - 136 */
1105 2, /* ‰ - 137 */
1106 20, /* Š - 138 */
1107 2, /* ‹ - 139 */
1108 2, /* Œ - 140 */
1109 2, /* � - 141 */
1110 2, /* Ž - 142 */
1111 2, /* � - 143 */
1112 2, /* � - 144 */
1113 2, /* ‘ - 145 */
1114 2, /* ’ - 146 */
1115 2, /* “ - 147 */
1116 2, /* ” - 148 */
1117 2, /* • - 149 */
1118 2, /* – - 150 */
1119 2, /* — - 151 */
1120 2, /* ˜ - 152 */
1121 2, /* ™ - 153 */
1122 20, /* š - 154 */
1123 2, /* › - 155 */
1124 2, /* œ - 156 */
1125 2, /* � - 157 */
1126 2, /* ž - 158 */
1127 19, /* Ÿ - 159 */
1128 2, /*   - 160 */
1129 2, /* ¡ - 161 */
1130 2, /* ¢ - 162 */
1131 2, /* £ - 163 */
1132 2, /* ¤ - 164 */
1133 2, /* ¥ - 165 */
1134 2, /* ¦ - 166 */
1135 2, /* § - 167 */
1136 2, /* ¨ - 168 */
1137 2, /* © - 169 */
1138 3, /* ª - 170 */
1139 2, /* « - 171 */
1140 2, /* ¬ - 172 */
1141 2, /* ­ - 173 */
1142 2, /* ® - 174 */
1143 2, /* ¯ - 175 */
1144 2, /* ° - 176 */
1145 2, /* ± - 177 */
1146 2, /* ² - 178 */
1147 2, /* ³ - 179 */
1148 2, /* ´ - 180 */
1149 2, /* µ - 181 */
1150 2, /* ¶ - 182 */
1151 2, /* · - 183 */
1152 2, /* ¸ - 184 */
1153 2, /* ¹ - 185 */
1154 3, /* º - 186 */
1155 2, /* » - 187 */
1156 2, /* ¼ - 188 */
1157 2, /* ½ - 189 */
1158 2, /* ¾ - 190 */
1159 2, /* ¿ - 191 */
1160 15, /* À - 192 */
1161 14, /* Á - 193 */
1162 18, /* Â - 194 */
1163 25, /* Ã - 195 */
1164 19, /* Ä - 196 */
1165 26, /* Å - 197 */
1166 2, /* Æ - 198 */
1167 28, /* Ç - 199 */
1168 15, /* È - 200 */
1169 14, /* É - 201 */
1170 18, /* Ê - 202 */
1171 19, /* Ë - 203 */
1172 15, /* Ì - 204 */
1173 14, /* Í - 205 */
1174 18, /* Î - 206 */
1175 19, /* Ï - 207 */
1176 104, /* Ð - 208 */
1177 25, /* Ñ - 209 */
1178 15, /* Ò - 210 */
1179 14, /* Ó - 211 */
1180 18, /* Ô - 212 */
1181 25, /* Õ - 213 */
1182 19, /* Ö - 214 */
1183 2, /* × - 215 */
1184 33, /* Ø - 216 */
1185 15, /* Ù - 217 */
1186 14, /* Ú - 218 */
1187 18, /* Û - 219 */
1188 19, /* Ü - 220 */
1189 14, /* Ý - 221 */
1190 2, /* Þ - 222 */
1191 2, /* ß - 223 */
1192 15, /* à - 224 */
1193 14, /* á - 225 */
1194 18, /* â - 226 */
1195 25, /* ã - 227 */
1196 19, /* ä - 228 */
1197 26, /* å - 229 */
1198 2, /* æ - 230 */
1199 28, /* ç - 231 */
1200 15, /* è - 232 */
1201 14, /* é - 233 */
1202 18, /* ê - 234 */
1203 19, /* ë - 235 */
1204 15, /* ì - 236 */
1205 14, /* í - 237 */
1206 18, /* î - 238 */
1207 19, /* ï - 239 */
1208 104, /* ð - 240 */
1209 25, /* ñ - 241 */
1210 15, /* ò - 242 */
1211 14, /* ó - 243 */
1212 18, /* ô - 244 */
1213 25, /* õ - 245 */
1214 19, /* ö - 246 */
1215 2, /* ÷ - 247 */
1216 33, /* ø - 248 */
1217 15, /* ù - 249 */
1218 14, /* ú - 250 */
1219 18, /* û - 251 */
1220 19, /* ü - 252 */
1221 14, /* ý - 253 */
1222 2, /* þ - 254 */
1223 19, /* ÿ - 255 */
1226 /******************************************************************************
1227 * OLE2NLS_isPunctuation [INTERNAL]
1229 static int OLE2NLS_isPunctuation(unsigned char c)
1231 /* "punctuation character" in this context is a character which is
1232 considered "less important" during word sort comparison.
1233 See LCMapString implementation for the precise definition
1234 of "less important". */
1236 return (LCM_Unicode_LUT[-2+2*c]==6);
1239 /******************************************************************************
1240 * OLE2NLS_isNonSpacing [INTERNAL]
1242 static int OLE2NLS_isNonSpacing(unsigned char c)
1244 /* This function is used by LCMapStringA. Characters
1245 for which it returns true are ignored when mapping a
1246 string with NORM_IGNORENONSPACE */
1247 return ((c==136) || (c==170) || (c==186));
1250 /******************************************************************************
1251 * OLE2NLS_isSymbol [INTERNAL]
1252 * FIXME: handle current locale
1254 static int OLE2NLS_isSymbol(unsigned char c)
1256 /* This function is used by LCMapStringA. Characters
1257 for which it returns true are ignored when mapping a
1258 string with NORM_IGNORESYMBOLS */
1259 return ( (c!=0) && !(isalpha(c) || isdigit(c)) );
1262 /******************************************************************************
1263 * identity [Internal]
1265 static int identity(int c)
1267 return c;
1270 /*************************************************************************
1271 * LCMapStringA [KERNEL32.@]
1273 * Convert a string, or generate a sort key from it.
1275 * If (mapflags & LCMAP_SORTKEY), the function will generate
1276 * a sort key for the source string. Else, it will convert it
1277 * accordingly to the flags LCMAP_UPPERCASE, LCMAP_LOWERCASE,...
1279 * RETURNS
1280 * Error : 0.
1281 * Success : length of the result string.
1283 * NOTES
1284 * If called with scrlen = -1, the function will compute the length
1285 * of the 0-terminated string strsrc by itself.
1287 * If called with dstlen = 0, returns the buffer length that
1288 * would be required.
1290 * NORM_IGNOREWIDTH means to compare ASCII and wide characters
1291 * as if they are equal.
1292 * In the only code page implemented so far, there may not be
1293 * wide characters in strings passed to LCMapStringA,
1294 * so there is nothing to be done for this flag.
1296 INT WINAPI LCMapStringA(
1297 LCID lcid, /* [in] locale identifier created with MAKELCID;
1298 LOCALE_SYSTEM_DEFAULT and LOCALE_USER_DEFAULT are
1299 predefined values. */
1300 DWORD mapflags, /* [in] flags */
1301 LPCSTR srcstr, /* [in] source buffer */
1302 INT srclen, /* [in] source length */
1303 LPSTR dststr, /* [out] destination buffer */
1304 INT dstlen) /* [in] destination buffer length */
1306 int i;
1308 TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1309 lcid,mapflags,debugstr_an(srcstr,srclen),srclen,dststr,dstlen);
1311 if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
1313 ERR("(src=%s,dest=%s): Invalid NULL string\n",
1314 debugstr_an(srcstr,srclen), dststr);
1315 SetLastError(ERROR_INVALID_PARAMETER);
1316 return 0;
1318 if (srclen == -1)
1319 srclen = strlen(srcstr) + 1 ; /* (include final '\0') */
1321 #define LCMAPSTRINGA_SUPPORTED_FLAGS (LCMAP_UPPERCASE | \
1322 LCMAP_LOWERCASE | \
1323 LCMAP_SORTKEY | \
1324 NORM_IGNORECASE | \
1325 NORM_IGNORENONSPACE | \
1326 SORT_STRINGSORT | \
1327 NORM_IGNOREWIDTH | \
1328 NORM_IGNOREKANATYPE)
1329 /* FIXME: as long as we don't support Katakana nor Hiragana
1330 * characters, we can support NORM_IGNOREKANATYPE
1332 if (mapflags & ~LCMAPSTRINGA_SUPPORTED_FLAGS)
1334 FIXME("(0x%04lx,0x%08lx,%p,%d,%p,%d): "
1335 "unimplemented flags: 0x%08lx\n",
1336 lcid,
1337 mapflags,
1338 srcstr,
1339 srclen,
1340 dststr,
1341 dstlen,
1342 mapflags & ~LCMAPSTRINGA_SUPPORTED_FLAGS
1346 if ( !(mapflags & LCMAP_SORTKEY) )
1348 int i,j;
1349 int (*f)(int) = identity;
1350 int flag_ignorenonspace = mapflags & NORM_IGNORENONSPACE;
1351 int flag_ignoresymbols = mapflags & NORM_IGNORESYMBOLS;
1353 if (flag_ignorenonspace || flag_ignoresymbols)
1355 /* For some values of mapflags, the length of the resulting
1356 string is not known at this point. Windows does map the string
1357 and does not SetLastError ERROR_INSUFFICIENT_BUFFER in
1358 these cases. */
1359 if (dstlen==0)
1361 /* Compute required length */
1362 for (i=j=0; i < srclen; i++)
1364 if ( !(flag_ignorenonspace && OLE2NLS_isNonSpacing(srcstr[i]))
1365 && !(flag_ignoresymbols && OLE2NLS_isSymbol(srcstr[i])) )
1366 j++;
1368 return j;
1371 else
1373 if (dstlen==0)
1374 return srclen;
1375 if (dstlen<srclen)
1377 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1378 return 0;
1381 if (mapflags & LCMAP_UPPERCASE)
1382 f = toupper;
1383 else if (mapflags & LCMAP_LOWERCASE)
1384 f = tolower;
1385 /* FIXME: NORM_IGNORENONSPACE requires another conversion */
1386 for (i=j=0; (i<srclen) && (j<dstlen) ; i++)
1388 if ( !(flag_ignorenonspace && OLE2NLS_isNonSpacing(srcstr[i]))
1389 && !(flag_ignoresymbols && OLE2NLS_isSymbol(srcstr[i])) )
1391 dststr[j] = (CHAR) f(srcstr[i]);
1392 j++;
1395 return j;
1398 /* FIXME: This function completely ignores the "lcid" parameter. */
1399 /* else ... (mapflags & LCMAP_SORTKEY) */
1401 int unicode_len=0;
1402 int case_len=0;
1403 int diacritic_len=0;
1404 int delayed_punctuation_len=0;
1405 char *case_component;
1406 char *diacritic_component;
1407 char *delayed_punctuation_component;
1408 int room,count;
1409 int flag_stringsort = mapflags & SORT_STRINGSORT;
1411 /* compute how much room we will need */
1412 for (i=0;i<srclen;i++)
1414 int ofs;
1415 unsigned char source_char = srcstr[i];
1416 if (source_char!='\0')
1418 if (flag_stringsort || !OLE2NLS_isPunctuation(source_char))
1420 unicode_len++;
1421 if ( LCM_Unicode_LUT[-2+2*source_char] & ~15 )
1422 unicode_len++; /* double letter */
1424 else
1426 delayed_punctuation_len++;
1430 if (isupper(source_char))
1431 case_len=unicode_len;
1433 ofs = source_char - LCM_Diacritic_Start;
1434 if ((ofs>=0) && (LCM_Diacritic_LUT[ofs]!=2))
1435 diacritic_len=unicode_len;
1438 if (mapflags & NORM_IGNORECASE)
1439 case_len=0;
1440 if (mapflags & NORM_IGNORENONSPACE)
1441 diacritic_len=0;
1443 room = 2 * unicode_len /* "unicode" component */
1444 + diacritic_len /* "diacritic" component */
1445 + case_len /* "case" component */
1446 + 4 * delayed_punctuation_len /* punctuation in word sort mode */
1447 + 4 /* four '\1' separators */
1448 + 1 ; /* terminal '\0' */
1449 if (dstlen==0)
1450 return room;
1451 else if (dstlen<room)
1453 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1454 return 0;
1456 #if 0
1457 /*FIXME the Pointercheck should not be nessesary */
1458 if (IsBadWritePtr (dststr,room))
1459 { ERR("bad destination buffer (dststr) : %p,%d\n",dststr,dstlen);
1460 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1461 return 0;
1463 #endif
1464 /* locate each component, write separators */
1465 diacritic_component = dststr + 2*unicode_len ;
1466 *diacritic_component++ = '\1';
1467 case_component = diacritic_component + diacritic_len ;
1468 *case_component++ = '\1';
1469 delayed_punctuation_component = case_component + case_len ;
1470 *delayed_punctuation_component++ = '\1';
1471 *delayed_punctuation_component++ = '\1';
1473 /* read source string char by char, write
1474 corresponding weight in each component. */
1475 for (i=0,count=0;i<srclen;i++)
1477 unsigned char source_char=srcstr[i];
1478 if (source_char!='\0')
1480 int type,longcode;
1481 type = LCM_Unicode_LUT[-2+2*source_char];
1482 longcode = type >> 4;
1483 type &= 15;
1484 if (!flag_stringsort && OLE2NLS_isPunctuation(source_char))
1486 WORD encrypted_location = (1<<15) + 7 + 4*count;
1487 *delayed_punctuation_component++ = (unsigned char) (encrypted_location>>8);
1488 *delayed_punctuation_component++ = (unsigned char) (encrypted_location&255);
1489 /* big-endian is used here because it lets string comparison be
1490 compatible with numerical comparison */
1492 *delayed_punctuation_component++ = type;
1493 *delayed_punctuation_component++ = LCM_Unicode_LUT[-1+2*source_char];
1494 /* assumption : a punctuation character is never a
1495 double or accented letter */
1497 else
1499 dststr[2*count] = type;
1500 dststr[2*count+1] = LCM_Unicode_LUT[-1+2*source_char];
1501 if (longcode)
1503 if (count<case_len)
1504 case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
1505 if (count<diacritic_len)
1506 diacritic_component[count] = 2; /* assumption: a double letter
1507 is never accented */
1508 count++;
1510 dststr[2*count] = type;
1511 dststr[2*count+1] = *(LCM_Unicode_LUT_2 - 1 + longcode);
1512 /* 16 in the first column of LCM_Unicode_LUT --> longcode = 1
1513 32 in the first column of LCM_Unicode_LUT --> longcode = 2
1514 48 in the first column of LCM_Unicode_LUT --> longcode = 3 */
1517 if (count<case_len)
1518 case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
1519 if (count<diacritic_len)
1521 int ofs = source_char - LCM_Diacritic_Start;
1522 diacritic_component[count] = (ofs>=0 ? LCM_Diacritic_LUT[ofs] : 2);
1524 count++;
1528 dststr[room-1] = '\0';
1529 return room;
1533 /*************************************************************************
1534 * LCMapStringW [KERNEL32.@]
1536 * Convert a string, or generate a sort key from it.
1538 * NOTE
1540 * See LCMapStringA for documentation
1542 INT WINAPI LCMapStringW(
1543 LCID lcid,DWORD mapflags,LPCWSTR srcstr,INT srclen,LPWSTR dststr,
1544 INT dstlen)
1546 int i;
1548 TRACE("(0x%04lx,0x%08lx,%p,%d,%p,%d)\n",
1549 lcid, mapflags, srcstr, srclen, dststr, dstlen);
1551 if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
1553 ERR("(src=%p,dst=%p): Invalid NULL string\n", srcstr, dststr);
1554 SetLastError(ERROR_INVALID_PARAMETER);
1555 return 0;
1557 if (srclen==-1)
1558 srclen = strlenW(srcstr)+1;
1560 /* FIXME: Both this function and it's companion LCMapStringA()
1561 * completely ignore the "lcid" parameter. In place of the "lcid"
1562 * parameter the application must set the "LC_COLLATE" or "LC_ALL"
1563 * environment variable prior to invoking this function. */
1564 if (mapflags & LCMAP_SORTKEY)
1566 /* Possible values of LC_COLLATE. */
1567 char *lc_collate_default = 0; /* value prior to this function */
1568 char *lc_collate_env = 0; /* value retrieved from the environment */
1570 /* General purpose index into strings of any type. */
1571 int str_idx = 0;
1573 /* Lengths of various strings where the length is measured in
1574 * wide characters for wide character strings and in bytes for
1575 * native strings. The lengths include the NULL terminator. */
1576 size_t returned_len = 0;
1577 size_t src_native_len = 0;
1578 size_t dst_native_len = 0;
1579 size_t dststr_libc_len = 0;
1581 /* Native (character set determined by locale) versions of the
1582 * strings source and destination strings. */
1583 LPSTR src_native = 0;
1584 LPSTR dst_native = 0;
1586 /* Version of the source and destination strings using the
1587 * "wchar_t" Unicode data type needed by various libc functions. */
1588 wchar_t *srcstr_libc = 0;
1589 wchar_t *dststr_libc = 0;
1591 if(!(srcstr_libc = (wchar_t *)HeapAlloc(GetProcessHeap(), 0,
1592 srclen * sizeof(wchar_t))))
1594 ERR("Unable to allocate %d bytes for srcstr_libc\n",
1595 srclen * sizeof(wchar_t));
1596 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1597 return 0;
1600 /* Convert source string to a libc Unicode string. */
1601 for(str_idx = 0; str_idx < srclen; str_idx++)
1603 srcstr_libc[str_idx] = srcstr[str_idx];
1606 /* src_native should contain at most 3 bytes for each
1607 * multibyte characters in the original srcstr string. */
1608 src_native_len = 3 * srclen;
1609 if(!(src_native = (LPSTR)HeapAlloc(GetProcessHeap(), 0,
1610 src_native_len)))
1612 ERR("Unable to allocate %d bytes for src_native\n", src_native_len);
1613 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1614 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1615 return 0;
1618 /* FIXME: Prior to to setting the LC_COLLATE locale category the
1619 * current value is backed up so it can be restored after the
1620 * last LC_COLLATE sensitive function returns.
1622 * Even though the locale is adjusted for a minimum amount of
1623 * time a race condition exists where other threads may be
1624 * affected if they invoke LC_COLLATE sensitive functions. One
1625 * possible solution is to wrap all LC_COLLATE sensitive Wine
1626 * functions, like LCMapStringW(), in a mutex.
1628 * Another enhancement to the following would be to set the
1629 * LC_COLLATE locale category as a function of the "lcid"
1630 * parameter instead of the "LC_COLLATE" environment variable. */
1631 if(!(lc_collate_default = setlocale(LC_COLLATE, NULL)))
1633 ERR("Unable to query the LC_COLLATE catagory\n");
1634 SetLastError(ERROR_INVALID_PARAMETER);
1635 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1636 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1637 return 0;
1640 if(!(lc_collate_env = setlocale(LC_COLLATE, "")))
1642 ERR("Unable to inherit the LC_COLLATE locale category from the "
1643 "environment. The \"LC_COLLATE\" environment variable is "
1644 "\"%s\".\n", getenv("LC_COLLATE") ?
1645 getenv("LC_COLLATE") : "<unset>");
1646 SetLastError(ERROR_INVALID_PARAMETER);
1647 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1648 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1649 return 0;
1652 TRACE("lc_collate_default = %s\n", lc_collate_default);
1653 TRACE("lc_collate_env = %s\n", lc_collate_env);
1655 /* Convert the libc Unicode string to a native multibyte character
1656 * string. */
1657 returned_len = wcstombs(src_native, srcstr_libc, src_native_len) + 1;
1658 if(returned_len == 0)
1660 ERR("wcstombs failed. The string specified (%s) may contain an invalid character.\n",
1661 debugstr_w(srcstr));
1662 SetLastError(ERROR_INVALID_PARAMETER);
1663 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1664 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1665 setlocale(LC_COLLATE, lc_collate_default);
1666 return 0;
1668 else if(returned_len > src_native_len)
1670 src_native[src_native_len - 1] = 0;
1671 ERR("wcstombs returned a string (%s) that was longer (%d bytes) "
1672 "than expected (%d bytes).\n", src_native, returned_len,
1673 dst_native_len);
1675 /* Since this is an internal error I'm not sure what the correct
1676 * error code is. */
1677 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1679 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1680 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1681 setlocale(LC_COLLATE, lc_collate_default);
1682 return 0;
1684 src_native_len = returned_len;
1686 TRACE("src_native = %s src_native_len = %d\n",
1687 src_native, src_native_len);
1689 /* dst_native seems to contain at most 4 bytes for each byte in
1690 * the original src_native string. Change if need be since this
1691 * isn't documented by the strxfrm() man page. */
1692 dst_native_len = 4 * src_native_len;
1693 if(!(dst_native = (LPSTR)HeapAlloc(GetProcessHeap(), 0, dst_native_len)))
1695 ERR("Unable to allocate %d bytes for dst_native\n", dst_native_len);
1696 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1697 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1698 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1699 setlocale(LC_COLLATE, lc_collate_default);
1700 return 0;
1703 /* The actual translation is done by the following call to
1704 * strxfrm(). The surrounding code could have been simplified
1705 * by calling wcsxfrm() instead except that wcsxfrm() is not
1706 * available on older Linux systems (RedHat 4.1 with
1707 * libc-5.3.12-17).
1709 * Also, it is possible that the translation could be done by
1710 * various tables as it is done in LCMapStringA(). However, I'm
1711 * not sure what those tables are. */
1712 returned_len = strxfrm(dst_native, src_native, dst_native_len) + 1;
1714 if(returned_len > dst_native_len)
1716 dst_native[dst_native_len - 1] = 0;
1717 ERR("strxfrm returned a string (%s) that was longer (%d bytes) "
1718 "than expected (%d bytes).\n", dst_native, returned_len,
1719 dst_native_len);
1721 /* Since this is an internal error I'm not sure what the correct
1722 * error code is. */
1723 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1725 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1726 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1727 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1728 setlocale(LC_COLLATE, lc_collate_default);
1729 return 0;
1731 dst_native_len = returned_len;
1733 TRACE("dst_native = %s dst_native_len = %d\n",
1734 dst_native, dst_native_len);
1736 dststr_libc_len = dst_native_len;
1737 if(!(dststr_libc = (wchar_t *)HeapAlloc(GetProcessHeap(), 0,
1738 dststr_libc_len * sizeof(wchar_t))))
1740 ERR("Unable to allocate %d bytes for dststr_libc\n",
1741 dststr_libc_len * sizeof(wchar_t));
1742 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1743 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1744 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1745 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1746 setlocale(LC_COLLATE, lc_collate_default);
1747 return 0;
1750 /* Convert the native multibyte string to a libc Unicode string. */
1751 returned_len = mbstowcs(dststr_libc, dst_native, dst_native_len) + 1;
1753 /* Restore LC_COLLATE now that the last LC_COLLATE sensitive
1754 * function has returned. */
1755 setlocale(LC_COLLATE, lc_collate_default);
1757 if(returned_len == 0)
1759 ERR("mbstowcs failed. The native version of the translated string "
1760 "(%s) may contain an invalid character.\n", dst_native);
1761 SetLastError(ERROR_INVALID_PARAMETER);
1762 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1763 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1764 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1765 if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
1766 return 0;
1768 if(dstlen)
1770 if(returned_len > dstlen)
1772 ERR("mbstowcs returned a string that was longer (%d chars) "
1773 "than the buffer provided (%d chars).\n", returned_len,
1774 dstlen);
1775 SetLastError(ERROR_INSUFFICIENT_BUFFER);
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 dstlen = returned_len;
1784 /* Convert a libc Unicode string to the destination string. */
1785 for(str_idx = 0; str_idx < dstlen; str_idx++)
1787 dststr[str_idx] = dststr_libc[str_idx];
1789 TRACE("1st 4 int sized chunks of dststr = %x %x %x %x\n",
1790 *(((int *)dststr) + 0),
1791 *(((int *)dststr) + 1),
1792 *(((int *)dststr) + 2),
1793 *(((int *)dststr) + 3));
1795 else
1797 dstlen = returned_len;
1799 TRACE("dstlen (return) = %d\n", dstlen);
1800 if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
1801 if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
1802 if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
1803 if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
1804 return dstlen;
1806 else
1808 int (*f)(int)=identity;
1810 if (dstlen==0)
1811 return srclen;
1812 if (dstlen<srclen)
1814 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1815 return 0;
1818 if (mapflags & LCMAP_UPPERCASE)
1819 f = toupper;
1820 else if (mapflags & LCMAP_LOWERCASE)
1821 f = tolower;
1822 for (i=0; i < srclen; i++)
1823 dststr[i] = (WCHAR) f(srcstr[i]);
1824 return srclen;
1829 /***********************************************************************
1830 * OLE2NLS_EstimateMappingLength
1832 * Estimates the number of characters required to hold the string
1833 * computed by LCMapStringA.
1835 * The size is always over-estimated, with a fixed limit on the
1836 * amount of estimation error.
1838 * Note that len == -1 is not permitted.
1840 static inline int OLE2NLS_EstimateMappingLength(LCID lcid, DWORD dwMapFlags,
1841 LPCSTR str, DWORD len)
1843 /* Estimate only for small strings to keep the estimation error from
1844 * becoming too large. */
1845 if (len < 128) return len * 8 + 5;
1846 else return LCMapStringA(lcid, dwMapFlags, str, len, NULL, 0);
1849 /******************************************************************************
1850 * CompareStringA [KERNEL32.@]
1851 * Compares two strings using locale
1853 * RETURNS
1855 * success: CSTR_LESS_THAN, CSTR_EQUAL, CSTR_GREATER_THAN
1856 * failure: 0
1858 * NOTES
1860 * Defaults to a word sort, but uses a string sort if
1861 * SORT_STRINGSORT is set.
1862 * Calls SetLastError for ERROR_INVALID_FLAGS, ERROR_INVALID_PARAMETER.
1864 * BUGS
1866 * This implementation ignores the locale
1868 * FIXME
1870 * Quite inefficient.
1872 int WINAPI CompareStringA(
1873 LCID lcid, /* [in] locale ID */
1874 DWORD fdwStyle, /* [in] comparison-style options */
1875 LPCSTR s1, /* [in] first string */
1876 int l1, /* [in] length of first string */
1877 LPCSTR s2, /* [in] second string */
1878 int l2) /* [in] length of second string */
1880 int mapstring_flags;
1881 int len1,len2;
1882 int result;
1883 LPSTR sk1,sk2;
1884 TRACE("%s and %s\n",
1885 debugstr_an (s1,l1), debugstr_an (s2,l2));
1887 if ( (s1==NULL) || (s2==NULL) )
1889 ERR("(s1=%s,s2=%s): Invalid NULL string\n",
1890 debugstr_an(s1,l1), debugstr_an(s2,l2));
1891 SetLastError(ERROR_INVALID_PARAMETER);
1892 return 0;
1895 if(fdwStyle & NORM_IGNORESYMBOLS)
1896 FIXME("IGNORESYMBOLS not supported\n");
1898 if (l1 == -1) l1 = strlen(s1);
1899 if (l2 == -1) l2 = strlen(s2);
1901 mapstring_flags = LCMAP_SORTKEY | fdwStyle ;
1902 len1 = OLE2NLS_EstimateMappingLength(lcid, mapstring_flags, s1, l1);
1903 len2 = OLE2NLS_EstimateMappingLength(lcid, mapstring_flags, s2, l2);
1905 if ((len1==0)||(len2==0))
1906 return 0; /* something wrong happened */
1908 sk1 = (LPSTR)HeapAlloc(GetProcessHeap(), 0, len1 + len2);
1909 sk2 = sk1 + len1;
1910 if ( (!LCMapStringA(lcid,mapstring_flags,s1,l1,sk1,len1))
1911 || (!LCMapStringA(lcid,mapstring_flags,s2,l2,sk2,len2)) )
1913 ERR("Bug in LCmapStringA.\n");
1914 result = 0;
1916 else
1918 /* strcmp doesn't necessarily return -1, 0, or 1 */
1919 result = strcmp(sk1,sk2);
1921 HeapFree(GetProcessHeap(),0,sk1);
1923 if (result < 0)
1924 return 1;
1925 if (result == 0)
1926 return 2;
1928 /* must be greater, if we reach this point */
1929 return 3;
1932 /******************************************************************************
1933 * CompareStringW [KERNEL32.@]
1934 * This implementation ignores the locale
1935 * FIXME : Does only string sort. Should
1936 * be reimplemented the same way as CompareStringA.
1938 int WINAPI CompareStringW(LCID lcid, DWORD fdwStyle,
1939 LPCWSTR s1, int l1, LPCWSTR s2, int l2)
1941 int len,ret;
1942 if(fdwStyle & NORM_IGNORENONSPACE)
1943 FIXME("IGNORENONSPACE not supported\n");
1944 if(fdwStyle & NORM_IGNORESYMBOLS)
1945 FIXME("IGNORESYMBOLS not supported\n");
1947 /* Is strcmp defaulting to string sort or to word sort?? */
1948 /* FIXME: Handle NORM_STRINGSORT */
1949 l1 = (l1==-1)?strlenW(s1):l1;
1950 l2 = (l2==-1)?strlenW(s2):l2;
1951 len = l1<l2 ? l1:l2;
1952 ret = (fdwStyle & NORM_IGNORECASE) ? strncmpiW(s1,s2,len) : strncmpW(s1,s2,len);
1953 /* not equal, return 1 or 3 */
1954 if(ret!=0) {
1955 /* need to translate result */
1956 return ((int)ret < 0) ? 1 : 3;
1958 /* same len, return 2 */
1959 if(l1==l2) return 2;
1960 /* the longer one is lexically greater */
1961 return (l1<l2)? 1 : 3;
1964 /******************************************************************************
1965 * OLE_GetFormatA [Internal]
1967 * FIXME
1968 * If datelen == 0, it should return the reguired string length.
1970 This function implements stuff for GetDateFormat() and
1971 GetTimeFormat().
1973 d single-digit (no leading zero) day (of month)
1974 dd two-digit day (of month)
1975 ddd short day-of-week name
1976 dddd long day-of-week name
1977 M single-digit month
1978 MM two-digit month
1979 MMM short month name
1980 MMMM full month name
1981 y two-digit year, no leading 0
1982 yy two-digit year
1983 yyyy four-digit year
1984 gg era string
1985 h hours with no leading zero (12-hour)
1986 hh hours with full two digits
1987 H hours with no leading zero (24-hour)
1988 HH hours with full two digits
1989 m minutes with no leading zero
1990 mm minutes with full two digits
1991 s seconds with no leading zero
1992 ss seconds with full two digits
1993 t time marker (A or P)
1994 tt time marker (AM, PM)
1995 '' used to quote literal characters
1996 '' (within a quoted string) indicates a literal '
1998 These functions REQUIRE valid locale, date, and format.
2000 static INT OLE_GetFormatA(LCID locale,
2001 DWORD flags,
2002 DWORD tflags,
2003 const SYSTEMTIME* xtime,
2004 LPCSTR _format, /*in*/
2005 LPSTR date, /*out*/
2006 INT datelen)
2008 INT inpos, outpos;
2009 int count, type, inquote, Overflow;
2010 char buf[40];
2011 char format[40];
2012 char * pos;
2013 int buflen;
2015 const char * _dgfmt[] = { "%d", "%02d" };
2016 const char ** dgfmt = _dgfmt - 1;
2018 /* report, for debugging */
2019 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",
2020 locale, flags, tflags,
2021 xtime->wYear,xtime->wMonth,xtime->wDayOfWeek,xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2022 _format, _format, date, datelen);
2024 if(datelen == 0) {
2025 FIXME("datelen = 0, returning 255\n");
2026 return 255;
2029 /* initalize state variables and output buffer */
2030 inpos = outpos = 0;
2031 count = 0; inquote = 0; Overflow = 0;
2032 type = '\0';
2033 date[0] = buf[0] = '\0';
2035 strcpy(format,_format);
2037 /* alter the formatstring, while it works for all languages now in wine
2038 its possible that it fails when the time looks like ss:mm:hh as example*/
2039 if (tflags & (TIME_NOMINUTESORSECONDS))
2040 { if ((pos = strstr ( format, ":mm")))
2041 { memcpy ( pos, pos+3, strlen(format)-(pos-format)-2 );
2044 if (tflags & (TIME_NOSECONDS))
2045 { if ((pos = strstr ( format, ":ss")))
2046 { memcpy ( pos, pos+3, strlen(format)-(pos-format)-2 );
2050 for (inpos = 0;; inpos++) {
2051 /* 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]); */
2052 if (inquote) {
2053 if (format[inpos] == '\'') {
2054 if (format[inpos+1] == '\'') {
2055 inpos += 1;
2056 date[outpos++] = '\'';
2057 } else {
2058 inquote = 0;
2059 continue; /* we did nothing to the output */
2061 } else if (format[inpos] == '\0') {
2062 date[outpos++] = '\0';
2063 if (outpos > datelen) Overflow = 1;
2064 break;
2065 } else {
2066 date[outpos++] = format[inpos];
2067 if (outpos > datelen) {
2068 Overflow = 1;
2069 date[outpos-1] = '\0'; /* this is the last place where
2070 it's safe to write */
2071 break;
2074 } else if ( (count && (format[inpos] != type))
2075 || count == 4
2076 || (count == 2 && strchr("ghHmst", type)) ) {
2077 if (type == 'h' && (tflags & TIME_FORCE24HOURFORMAT)) type= 'H';
2078 if (type == 'd') {
2079 if (count == 4) {
2080 GetLocaleInfoA(locale,
2081 LOCALE_SDAYNAME1
2082 + (xtime->wDayOfWeek+6)%7,
2083 buf, sizeof(buf));
2084 } else if (count == 3) {
2085 GetLocaleInfoA(locale,
2086 LOCALE_SABBREVDAYNAME1
2087 + (xtime->wDayOfWeek+6)%7,
2088 buf, sizeof(buf));
2089 } else {
2090 sprintf(buf, dgfmt[count], xtime->wDay);
2092 } else if (type == 'M') {
2093 if (count == 3) {
2094 GetLocaleInfoA(locale,
2095 LOCALE_SABBREVMONTHNAME1
2096 + xtime->wMonth - 1,
2097 buf, sizeof(buf));
2098 } else if (count == 4) {
2099 GetLocaleInfoA(locale,
2100 LOCALE_SMONTHNAME1
2101 + xtime->wMonth - 1,
2102 buf, sizeof(buf));
2103 } else {
2104 sprintf(buf, dgfmt[count], xtime->wMonth);
2106 } else if (type == 'y') {
2107 if (count == 4) {
2108 sprintf(buf, "%d", xtime->wYear);
2109 } else if (count == 3) {
2110 strcpy(buf, "yyy");
2111 WARN("unknown format, c=%c, n=%d\n", type, count);
2112 } else {
2113 sprintf(buf, dgfmt[count], xtime->wYear % 100);
2115 } else if (type == 'g') {
2116 if (count == 2) {
2117 FIXME("LOCALE_ICALENDARTYPE unimp.\n");
2118 strcpy(buf, "AD");
2119 } else {
2120 strcpy(buf, "g");
2121 WARN("unknown format, c=%c, n=%d\n", type, count);
2123 } else if (type == 'h') {
2124 /* gives us hours 1:00 -- 12:00 */
2125 sprintf(buf, dgfmt[count], (xtime->wHour-1)%12 +1);
2126 } else if (type == 'H') {
2127 /* 24-hour time */
2128 sprintf(buf, dgfmt[count], xtime->wHour);
2129 } else if ( type == 'm') {
2130 sprintf(buf, dgfmt[count], xtime->wMinute);
2131 } else if ( type == 's') {
2132 sprintf(buf, dgfmt[count], xtime->wSecond);
2133 } else if (type == 't') {
2134 if ((tflags & TIME_NOTIMEMARKER))
2135 buf[0]='\0';
2136 else if (count == 1) {
2137 sprintf(buf, "%c", (xtime->wHour < 12) ? 'A' : 'P');
2138 } else if (count == 2) {
2139 /* sprintf(buf, "%s", (xtime->wHour < 12) ? "AM" : "PM"); */
2140 GetLocaleInfoA(locale,
2141 (xtime->wHour<12)
2142 ? LOCALE_S1159 : LOCALE_S2359,
2143 buf, sizeof(buf));
2147 /* we need to check the next char in the format string
2148 again, no matter what happened */
2149 inpos--;
2151 /* add the contents of buf to the output */
2152 buflen = strlen(buf);
2153 if (outpos + buflen < datelen) {
2154 date[outpos] = '\0'; /* for strcat to hook onto */
2155 strcat(date, buf);
2156 outpos += buflen;
2157 } else {
2158 date[outpos] = '\0';
2159 strncat(date, buf, datelen - outpos);
2160 date[datelen - 1] = '\0';
2161 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2162 WARN("insufficient buffer\n");
2163 return 0;
2166 /* reset the variables we used to keep track of this item */
2167 count = 0;
2168 type = '\0';
2169 } else if (format[inpos] == '\0') {
2170 /* we can't check for this at the loop-head, because
2171 that breaks the printing of the last format-item */
2172 date[outpos] = '\0';
2173 break;
2174 } else if (count) {
2175 /* continuing a code for an item */
2176 count +=1;
2177 continue;
2178 } else if (strchr("hHmstyMdg", format[inpos])) {
2179 type = format[inpos];
2180 count = 1;
2181 continue;
2182 } else if (format[inpos] == '\'') {
2183 inquote = 1;
2184 continue;
2185 } else {
2186 date[outpos++] = format[inpos];
2188 /* now deal with a possible buffer overflow */
2189 if (outpos >= datelen) {
2190 date[datelen - 1] = '\0';
2191 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2192 return 0;
2196 if (Overflow) {
2197 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2200 /* finish it off with a string terminator */
2201 outpos++;
2202 /* sanity check */
2203 if (outpos > datelen-1) outpos = datelen-1;
2204 date[outpos] = '\0';
2206 TRACE("returns string '%s', len %d\n", date, outpos);
2207 return outpos;
2210 /******************************************************************************
2211 * OLE_GetFormatW [INTERNAL]
2213 static INT OLE_GetFormatW(LCID locale, DWORD flags, DWORD tflags,
2214 const SYSTEMTIME* xtime,
2215 LPCWSTR format,
2216 LPWSTR output, INT outlen)
2218 INT inpos, outpos;
2219 int count, type=0, inquote;
2220 int Overflow; /* loop check */
2221 char tmp[16];
2222 WCHAR buf[40];
2223 int buflen=0;
2224 WCHAR arg0[] = {0}, arg1[] = {'%','d',0};
2225 WCHAR arg2[] = {'%','0','2','d',0};
2226 WCHAR *argarr[3];
2227 int datevars=0, timevars=0;
2229 argarr[0] = arg0;
2230 argarr[1] = arg1;
2231 argarr[2] = arg2;
2233 /* make a debug report */
2234 TRACE("args: 0x%lx, 0x%lx, 0x%lx, time(d=%d,h=%d,m=%d,s=%d), fmt:%s (at %p), "
2235 "%p with max len %d\n",
2236 locale, flags, tflags,
2237 xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2238 debugstr_w(format), format, output, outlen);
2240 if(outlen == 0) {
2241 FIXME("outlen = 0, returning 255\n");
2242 return 255;
2245 /* initialize state variables */
2246 inpos = outpos = 0;
2247 count = 0;
2248 inquote = Overflow = 0;
2249 /* this is really just a sanity check */
2250 output[0] = buf[0] = 0;
2252 /* this loop is the core of the function */
2253 for (inpos = 0; /* we have several break points */ ; inpos++) {
2254 if (inquote) {
2255 if (format[inpos] == (WCHAR) '\'') {
2256 if (format[inpos+1] == '\'') {
2257 inpos++;
2258 output[outpos++] = '\'';
2259 } else {
2260 inquote = 0;
2261 continue;
2263 } else if (format[inpos] == 0) {
2264 output[outpos++] = 0;
2265 if (outpos > outlen) Overflow = 1;
2266 break; /* normal exit (within a quote) */
2267 } else {
2268 output[outpos++] = format[inpos]; /* copy input */
2269 if (outpos > outlen) {
2270 Overflow = 1;
2271 output[outpos-1] = 0;
2272 break;
2275 } else if ( (count && (format[inpos] != type))
2276 || ( (count==4 && type =='y') ||
2277 (count==4 && type =='M') ||
2278 (count==4 && type =='d') ||
2279 (count==2 && type =='g') ||
2280 (count==2 && type =='h') ||
2281 (count==2 && type =='H') ||
2282 (count==2 && type =='m') ||
2283 (count==2 && type =='s') ||
2284 (count==2 && type =='t') ) ) {
2285 switch(type)
2287 case 'd':
2288 if (count == 4) {
2289 GetLocaleInfoW(locale,
2290 LOCALE_SDAYNAME1 + (xtime->wDayOfWeek +6)%7,
2291 buf, sizeof(buf)/sizeof(WCHAR) );
2292 } else if (count == 3) {
2293 GetLocaleInfoW(locale,
2294 LOCALE_SABBREVDAYNAME1 +
2295 (xtime->wDayOfWeek +6)%7,
2296 buf, sizeof(buf)/sizeof(WCHAR) );
2297 } else {
2298 sprintf( tmp, "%.*d", count, xtime->wDay );
2299 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2301 break;
2303 case 'M':
2304 if (count == 4) {
2305 GetLocaleInfoW(locale, LOCALE_SMONTHNAME1 +
2306 xtime->wMonth -1, buf,
2307 sizeof(buf)/sizeof(WCHAR) );
2308 } else if (count == 3) {
2309 GetLocaleInfoW(locale, LOCALE_SABBREVMONTHNAME1 +
2310 xtime->wMonth -1, buf,
2311 sizeof(buf)/sizeof(WCHAR) );
2312 } else {
2313 sprintf( tmp, "%.*d", count, xtime->wMonth );
2314 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2316 break;
2317 case 'y':
2318 if (count == 4) {
2319 sprintf( tmp, "%d", xtime->wYear );
2320 } else if (count == 3) {
2321 strcpy( tmp, "yyy" );
2322 } else {
2323 sprintf( tmp, "%.*d", count, xtime->wYear % 100 );
2325 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2326 break;
2328 case 'g':
2329 if (count == 2) {
2330 FIXME("LOCALE_ICALENDARTYPE unimplemented\n");
2331 strcpy( tmp, "AD" );
2332 } else {
2333 /* Win API sez we copy it verbatim */
2334 strcpy( tmp, "g" );
2336 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2337 break;
2339 case 'h':
2340 /* hours 1:00-12:00 --- is this right? */
2341 sprintf( tmp, "%.*d", count, (xtime->wHour-1)%12 +1);
2342 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2343 break;
2345 case 'H':
2346 sprintf( tmp, "%.*d", count, xtime->wHour );
2347 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2348 break;
2350 case 'm':
2351 sprintf( tmp, "%.*d", count, xtime->wMinute );
2352 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2353 break;
2355 case 's':
2356 sprintf( tmp, "%.*d", count, xtime->wSecond );
2357 MultiByteToWideChar( CP_ACP, 0, tmp, -1, buf, sizeof(buf)/sizeof(WCHAR) );
2358 break;
2360 case 't':
2361 GetLocaleInfoW(locale, (xtime->wHour < 12) ?
2362 LOCALE_S1159 : LOCALE_S2359,
2363 buf, sizeof(buf) );
2364 if (count == 1) {
2365 buf[1] = 0;
2367 break;
2370 /* no matter what happened, we need to check this next
2371 character the next time we loop through */
2372 inpos--;
2374 /* cat buf onto the output */
2375 outlen = strlenW(buf);
2376 if (outpos + buflen < outlen) {
2377 strcpyW( output + outpos, buf );
2378 outpos += buflen;
2379 } else {
2380 lstrcpynW( output + outpos, buf, outlen - outpos );
2381 Overflow = 1;
2382 break; /* Abnormal exit */
2385 /* reset the variables we used this time */
2386 count = 0;
2387 type = '\0';
2388 } else if (format[inpos] == 0) {
2389 /* we can't check for this at the beginning, because that
2390 would keep us from printing a format spec that ended the
2391 string */
2392 output[outpos] = 0;
2393 break; /* NORMAL EXIT */
2394 } else if (count) {
2395 /* how we keep track of the middle of a format spec */
2396 count++;
2397 continue;
2398 } else if ( (datevars && (format[inpos]=='d' ||
2399 format[inpos]=='M' ||
2400 format[inpos]=='y' ||
2401 format[inpos]=='g') ) ||
2402 (timevars && (format[inpos]=='H' ||
2403 format[inpos]=='h' ||
2404 format[inpos]=='m' ||
2405 format[inpos]=='s' ||
2406 format[inpos]=='t') ) ) {
2407 type = format[inpos];
2408 count = 1;
2409 continue;
2410 } else if (format[inpos] == '\'') {
2411 inquote = 1;
2412 continue;
2413 } else {
2414 /* unquoted literals */
2415 output[outpos++] = format[inpos];
2419 if (Overflow) {
2420 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2421 WARN(" buffer overflow\n");
2424 /* final string terminator and sanity check */
2425 outpos++;
2426 if (outpos > outlen-1) outpos = outlen-1;
2427 output[outpos] = '0';
2429 TRACE(" returning %s\n", debugstr_w(output));
2431 return (!Overflow) ? outlen : 0;
2436 /******************************************************************************
2437 * GetDateFormatA [KERNEL32.@]
2438 * Makes an ASCII string of the date
2440 * This function uses format to format the date, or, if format
2441 * is NULL, uses the default for the locale. format is a string
2442 * of literal fields and characters as follows:
2444 * - d single-digit (no leading zero) day (of month)
2445 * - dd two-digit day (of month)
2446 * - ddd short day-of-week name
2447 * - dddd long day-of-week name
2448 * - M single-digit month
2449 * - MM two-digit month
2450 * - MMM short month name
2451 * - MMMM full month name
2452 * - y two-digit year, no leading 0
2453 * - yy two-digit year
2454 * - yyyy four-digit year
2455 * - gg era string
2458 INT WINAPI GetDateFormatA(LCID locale,DWORD flags,
2459 const SYSTEMTIME* xtime,
2460 LPCSTR format, LPSTR date,INT datelen)
2463 char format_buf[40];
2464 LPCSTR thisformat;
2465 SYSTEMTIME t;
2466 LPSYSTEMTIME thistime;
2467 LCID thislocale;
2468 INT ret;
2469 FILETIME ft;
2470 BOOL res;
2472 TRACE("(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",
2473 locale,flags,xtime,format,date,datelen);
2475 if (!locale) {
2476 locale = LOCALE_SYSTEM_DEFAULT;
2479 if (locale == LOCALE_SYSTEM_DEFAULT) {
2480 thislocale = GetSystemDefaultLCID();
2481 } else if (locale == LOCALE_USER_DEFAULT) {
2482 thislocale = GetUserDefaultLCID();
2483 } else {
2484 thislocale = locale;
2487 if (xtime == NULL) {
2488 GetSystemTime(&t);
2489 } else {
2490 /* Silently correct wDayOfWeek by transforming to FileTime and back again */
2491 res=SystemTimeToFileTime(xtime,&ft);
2492 /* Check year(?)/month and date for range and set ERROR_INVALID_PARAMETER on error */
2493 /*FIXME: SystemTimeToFileTime doesn't yet do that check */
2494 if(!res)
2496 SetLastError(ERROR_INVALID_PARAMETER);
2497 return 0;
2499 FileTimeToSystemTime(&ft,&t);
2502 thistime = &t;
2504 if (format == NULL) {
2505 GetLocaleInfoA(thislocale, ((flags&DATE_LONGDATE)
2506 ? LOCALE_SLONGDATE
2507 : LOCALE_SSHORTDATE),
2508 format_buf, sizeof(format_buf));
2509 thisformat = format_buf;
2510 } else {
2511 thisformat = format;
2515 ret = OLE_GetFormatA(thislocale, flags, 0, thistime, thisformat,
2516 date, datelen);
2519 TRACE(
2520 "GetDateFormatA() returning %d, with data=%s\n",
2521 ret, date);
2522 return ret;
2525 /******************************************************************************
2526 * GetDateFormatW [KERNEL32.@]
2527 * Makes a Unicode string of the date
2529 * Acts the same as GetDateFormatA(), except that it's Unicode.
2530 * Accepts & returns sizes as counts of Unicode characters.
2533 INT WINAPI GetDateFormatW(LCID locale,DWORD flags,
2534 const SYSTEMTIME* xtime,
2535 LPCWSTR format,
2536 LPWSTR date, INT datelen)
2538 unsigned short datearr[] = {'1','9','9','4','-','1','-','1',0};
2540 FIXME("STUB (should call OLE_GetFormatW)\n");
2541 lstrcpynW(date, datearr, datelen);
2542 return ( datelen < 9) ? datelen : 9;
2547 /**************************************************************************
2548 * EnumDateFormatsA (KERNEL32.@)
2550 BOOL WINAPI EnumDateFormatsA(
2551 DATEFMT_ENUMPROCA lpDateFmtEnumProc, LCID Locale, DWORD dwFlags)
2553 LCID Loc = GetUserDefaultLCID();
2554 if(!lpDateFmtEnumProc)
2556 SetLastError(ERROR_INVALID_PARAMETER);
2557 return FALSE;
2560 switch( Loc )
2563 case 0x00000407: /* (Loc,"de_DE") */
2565 switch(dwFlags)
2567 case DATE_SHORTDATE:
2568 if(!(*lpDateFmtEnumProc)("dd.MM.yy")) return TRUE;
2569 if(!(*lpDateFmtEnumProc)("d.M.yyyy")) return TRUE;
2570 if(!(*lpDateFmtEnumProc)("d.MM.yy")) return TRUE;
2571 if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
2572 return TRUE;
2573 case DATE_LONGDATE:
2574 if(!(*lpDateFmtEnumProc)("dddd,d. MMMM yyyy")) return TRUE;
2575 if(!(*lpDateFmtEnumProc)("d. MMMM yyyy")) return TRUE;
2576 if(!(*lpDateFmtEnumProc)("d. MMM yyyy")) return TRUE;
2577 return TRUE;
2578 default:
2579 FIXME("Unknown date format (%ld)\n", dwFlags);
2580 SetLastError(ERROR_INVALID_PARAMETER);
2581 return FALSE;
2585 case 0x0000040c: /* (Loc,"fr_FR") */
2587 switch(dwFlags)
2589 case DATE_SHORTDATE:
2590 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2591 if(!(*lpDateFmtEnumProc)("dd.MM.yy")) return TRUE;
2592 if(!(*lpDateFmtEnumProc)("dd-MM-yy")) return TRUE;
2593 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2594 return TRUE;
2595 case DATE_LONGDATE:
2596 if(!(*lpDateFmtEnumProc)("dddd d MMMM yyyy")) return TRUE;
2597 if(!(*lpDateFmtEnumProc)("d MMM yy")) return TRUE;
2598 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2599 return TRUE;
2600 default:
2601 FIXME("Unknown date format (%ld)\n", dwFlags);
2602 SetLastError(ERROR_INVALID_PARAMETER);
2603 return FALSE;
2607 case 0x00000c0c: /* (Loc,"fr_CA") */
2609 switch(dwFlags)
2611 case DATE_SHORTDATE:
2612 if(!(*lpDateFmtEnumProc)("yy-MM-dd")) return TRUE;
2613 if(!(*lpDateFmtEnumProc)("dd-MM-yy")) return TRUE;
2614 if(!(*lpDateFmtEnumProc)("yy MM dd")) return TRUE;
2615 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2616 return TRUE;
2617 case DATE_LONGDATE:
2618 if(!(*lpDateFmtEnumProc)("d MMMM, yyyy")) return TRUE;
2619 if(!(*lpDateFmtEnumProc)("d MMM yyyy")) return TRUE;
2620 return TRUE;
2621 default:
2622 FIXME("Unknown date format (%ld)\n", dwFlags);
2623 SetLastError(ERROR_INVALID_PARAMETER);
2624 return FALSE;
2628 case 0x00000809: /* (Loc,"en_UK") */
2630 switch(dwFlags)
2632 case DATE_SHORTDATE:
2633 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2634 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2635 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2636 if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
2637 return TRUE;
2638 case DATE_LONGDATE:
2639 if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
2640 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2641 return TRUE;
2642 default:
2643 FIXME("Unknown date format (%ld)\n", dwFlags);
2644 SetLastError(ERROR_INVALID_PARAMETER);
2645 return FALSE;
2649 case 0x00000c09: /* (Loc,"en_AU") */
2651 switch(dwFlags)
2653 case DATE_SHORTDATE:
2654 if(!(*lpDateFmtEnumProc)("d/MM/yy")) return TRUE;
2655 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2656 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2657 return TRUE;
2658 case DATE_LONGDATE:
2659 if(!(*lpDateFmtEnumProc)("dddd,d MMMM yyyy")) return TRUE;
2660 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2661 return TRUE;
2662 default:
2663 FIXME("Unknown date format (%ld)\n", dwFlags);
2664 SetLastError(ERROR_INVALID_PARAMETER);
2665 return FALSE;
2669 case 0x00001009: /* (Loc,"en_CA") */
2671 switch(dwFlags)
2673 case DATE_SHORTDATE:
2674 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2675 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2676 if(!(*lpDateFmtEnumProc)("yy-MM-dd")) return TRUE;
2677 if(!(*lpDateFmtEnumProc)("M/dd/yy")) return TRUE;
2678 return TRUE;
2679 case DATE_LONGDATE:
2680 if(!(*lpDateFmtEnumProc)("d-MMM-yy")) return TRUE;
2681 if(!(*lpDateFmtEnumProc)("MMMM d, yyyy")) return TRUE;
2682 return TRUE;
2683 default:
2684 FIXME("Unknown date format (%ld)\n", dwFlags);
2685 SetLastError(ERROR_INVALID_PARAMETER);
2686 return FALSE;
2690 case 0x00001409: /* (Loc,"en_NZ") */
2692 switch(dwFlags)
2694 case DATE_SHORTDATE:
2695 if(!(*lpDateFmtEnumProc)("d/MM/yy")) return TRUE;
2696 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2697 if(!(*lpDateFmtEnumProc)("d.MM.yy")) return TRUE;
2698 return TRUE;
2699 case DATE_LONGDATE:
2700 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2701 if(!(*lpDateFmtEnumProc)("dddd, d MMMM yyyy")) return TRUE;
2702 return TRUE;
2703 default:
2704 FIXME("Unknown date format (%ld)\n", dwFlags);
2705 SetLastError(ERROR_INVALID_PARAMETER);
2706 return FALSE;
2710 case 0x00001809: /* (Loc,"en_IE") */
2712 switch(dwFlags)
2714 case DATE_SHORTDATE:
2715 if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
2716 if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
2717 if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
2718 return TRUE;
2719 case DATE_LONGDATE:
2720 if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
2721 if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
2722 return TRUE;
2723 default:
2724 FIXME("Unknown date format (%ld)\n", dwFlags);
2725 SetLastError(ERROR_INVALID_PARAMETER);
2726 return FALSE;
2730 case 0x00001c09: /* (Loc,"en_ZA") */
2732 switch(dwFlags)
2734 case DATE_SHORTDATE:
2735 if(!(*lpDateFmtEnumProc)("yy/MM/dd")) return TRUE;
2736 return TRUE;
2737 case DATE_LONGDATE:
2738 if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
2739 return TRUE;
2740 default:
2741 FIXME("Unknown date format (%ld)\n", dwFlags);
2742 SetLastError(ERROR_INVALID_PARAMETER);
2743 return FALSE;
2747 case 0x00002009: /* (Loc,"en_JM") */
2749 switch(dwFlags)
2751 case DATE_SHORTDATE:
2752 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2753 return TRUE;
2754 case DATE_LONGDATE:
2755 if(!(*lpDateFmtEnumProc)("dddd,MMMM dd,yyyy")) return TRUE;
2756 if(!(*lpDateFmtEnumProc)("MMMM dd,yyyy")) return TRUE;
2757 if(!(*lpDateFmtEnumProc)("dddd,dd MMMM,yyyy")) return TRUE;
2758 if(!(*lpDateFmtEnumProc)("dd MMMM,yyyy")) return TRUE;
2759 return TRUE;
2760 default:
2761 FIXME("Unknown date format (%ld)\n", dwFlags);
2762 SetLastError(ERROR_INVALID_PARAMETER);
2763 return FALSE;
2767 case 0x00002809: /* (Loc,"en_BZ") */
2768 case 0x00002c09: /* (Loc,"en_TT") */
2770 switch(dwFlags)
2772 case DATE_SHORTDATE:
2773 if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
2774 return TRUE;
2775 case DATE_LONGDATE:
2776 if(!(*lpDateFmtEnumProc)("dddd,dd MMMM yyyy")) return TRUE;
2777 return TRUE;
2778 default:
2779 FIXME("Unknown date format (%ld)\n", dwFlags);
2780 SetLastError(ERROR_INVALID_PARAMETER);
2781 return FALSE;
2785 default: /* default to US English "en_US" */
2787 switch(dwFlags)
2789 case DATE_SHORTDATE:
2790 if(!(*lpDateFmtEnumProc)("M/d/yy")) return TRUE;
2791 if(!(*lpDateFmtEnumProc)("M/d/yyyy")) return TRUE;
2792 if(!(*lpDateFmtEnumProc)("MM/dd/yy")) return TRUE;
2793 if(!(*lpDateFmtEnumProc)("MM/dd/yyyy")) return TRUE;
2794 if(!(*lpDateFmtEnumProc)("yy/MM/dd")) return TRUE;
2795 if(!(*lpDateFmtEnumProc)("dd-MMM-yy")) return TRUE;
2796 return TRUE;
2797 case DATE_LONGDATE:
2798 if(!(*lpDateFmtEnumProc)("dddd, MMMM dd, yyyy")) return TRUE;
2799 if(!(*lpDateFmtEnumProc)("MMMM dd, yyyy")) return TRUE;
2800 if(!(*lpDateFmtEnumProc)("dddd, dd MMMM, yyyy")) return TRUE;
2801 if(!(*lpDateFmtEnumProc)("dd MMMM, yyyy")) return TRUE;
2802 return TRUE;
2803 default:
2804 FIXME("Unknown date format (%ld)\n", dwFlags);
2805 SetLastError(ERROR_INVALID_PARAMETER);
2806 return FALSE;
2812 /**************************************************************************
2813 * EnumDateFormatsW (KERNEL32.@)
2815 BOOL WINAPI EnumDateFormatsW(
2816 DATEFMT_ENUMPROCW lpDateFmtEnumProc, LCID Locale, DWORD dwFlags)
2818 FIXME("(%p, %ld, %ld): stub\n", lpDateFmtEnumProc, Locale, dwFlags);
2819 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2820 return FALSE;
2823 /**************************************************************************
2824 * EnumTimeFormatsA (KERNEL32.@)
2826 BOOL WINAPI EnumTimeFormatsA(
2827 TIMEFMT_ENUMPROCA lpTimeFmtEnumProc, LCID Locale, DWORD dwFlags)
2829 LCID Loc = GetUserDefaultLCID();
2830 if(!lpTimeFmtEnumProc)
2832 SetLastError(ERROR_INVALID_PARAMETER);
2833 return FALSE;
2835 if(dwFlags)
2837 FIXME("Unknown time format (%ld)\n", dwFlags);
2840 switch( Loc )
2842 case 0x00000407: /* (Loc,"de_DE") */
2844 if(!(*lpTimeFmtEnumProc)("HH.mm")) return TRUE;
2845 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2846 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2847 if(!(*lpTimeFmtEnumProc)("H.mm")) return TRUE;
2848 if(!(*lpTimeFmtEnumProc)("H.mm'Uhr'")) return TRUE;
2849 return TRUE;
2852 case 0x0000040c: /* (Loc,"fr_FR") */
2853 case 0x00000c0c: /* (Loc,"fr_CA") */
2855 if(!(*lpTimeFmtEnumProc)("H:mm")) return TRUE;
2856 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2857 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2858 if(!(*lpTimeFmtEnumProc)("HH.mm")) return TRUE;
2859 if(!(*lpTimeFmtEnumProc)("HH'h'mm")) return TRUE;
2860 return TRUE;
2863 case 0x00000809: /* (Loc,"en_UK") */
2864 case 0x00000c09: /* (Loc,"en_AU") */
2865 case 0x00001409: /* (Loc,"en_NZ") */
2866 case 0x00001809: /* (Loc,"en_IE") */
2868 if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
2869 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2870 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2871 return TRUE;
2874 case 0x00001c09: /* (Loc,"en_ZA") */
2875 case 0x00002809: /* (Loc,"en_BZ") */
2876 case 0x00002c09: /* (Loc,"en_TT") */
2878 if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
2879 if(!(*lpTimeFmtEnumProc)("hh:mm:ss tt")) return TRUE;
2880 return TRUE;
2883 default: /* default to US style "en_US" */
2885 if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
2886 if(!(*lpTimeFmtEnumProc)("hh:mm:ss tt")) return TRUE;
2887 if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
2888 if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
2889 return TRUE;
2894 /**************************************************************************
2895 * EnumTimeFormatsW (KERNEL32.@)
2897 BOOL WINAPI EnumTimeFormatsW(
2898 TIMEFMT_ENUMPROCW lpTimeFmtEnumProc, LCID Locale, DWORD dwFlags)
2900 FIXME("(%p,%ld,%ld): stub\n", lpTimeFmtEnumProc, Locale, dwFlags);
2901 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2902 return FALSE;
2905 /**************************************************************************
2906 * This function is used just locally !
2907 * Description: Inverts a string.
2909 static void OLE_InvertString(char* string)
2911 char sTmpArray[128];
2912 INT counter, i = 0;
2914 for (counter = strlen(string); counter > 0; counter--)
2916 memcpy(sTmpArray + i, string + counter-1, 1);
2917 i++;
2919 memcpy(sTmpArray + i, "\0", 1);
2920 strcpy(string, sTmpArray);
2923 /***************************************************************************************
2924 * This function is used just locally !
2925 * Description: Test if the given string (psNumber) is valid or not.
2926 * The valid characters are the following:
2927 * - Characters '0' through '9'.
2928 * - One decimal point (dot) if the number is a floating-point value.
2929 * - A minus sign in the first character position if the number is
2930 * a negative value.
2931 * If the function succeeds, psBefore/psAfter will point to the string
2932 * on the right/left of the decimal symbol. pbNegative indicates if the
2933 * number is negative.
2935 static INT OLE_GetNumberComponents(char* pInput, char* psBefore, char* psAfter, BOOL* pbNegative)
2937 char sNumberSet[] = "0123456789";
2938 BOOL bInDecimal = FALSE;
2940 /* Test if we do have a minus sign */
2941 if ( *pInput == '-' )
2943 *pbNegative = TRUE;
2944 pInput++; /* Jump to the next character. */
2947 while(*pInput != '\0')
2949 /* Do we have a valid numeric character */
2950 if ( strchr(sNumberSet, *pInput) != NULL )
2952 if (bInDecimal == TRUE)
2953 *psAfter++ = *pInput;
2954 else
2955 *psBefore++ = *pInput;
2957 else
2959 /* Is this a decimal point (dot) */
2960 if ( *pInput == '.' )
2962 /* Is it the first time we find it */
2963 if ((bInDecimal == FALSE))
2964 bInDecimal = TRUE;
2965 else
2966 return -1; /* ERROR: Invalid parameter */
2968 else
2970 /* It's neither a numeric character, nor a decimal point.
2971 * Thus, return an error.
2973 return -1;
2976 pInput++;
2979 /* Add an End of Line character to the output buffers */
2980 *psBefore = '\0';
2981 *psAfter = '\0';
2983 return 0;
2986 /**************************************************************************
2987 * This function is used just locally !
2988 * Description: A number could be formatted using different numbers
2989 * of "digits in group" (example: 4;3;2;0).
2990 * The first parameter of this function is an array
2991 * containing the rule to be used. Its format is the following:
2992 * |NDG|DG1|DG2|...|0|
2993 * where NDG is the number of used "digits in group" and DG1, DG2,
2994 * are the corresponding "digits in group".
2995 * Thus, this function returns the grouping value in the array
2996 * pointed by the second parameter.
2998 static INT OLE_GetGrouping(char* sRule, INT index)
3000 char sData[2], sRuleSize[2];
3001 INT nData, nRuleSize;
3003 memcpy(sRuleSize, sRule, 1);
3004 memcpy(sRuleSize+1, "\0", 1);
3005 nRuleSize = atoi(sRuleSize);
3007 if (index > 0 && index < nRuleSize)
3009 memcpy(sData, sRule+index, 1);
3010 memcpy(sData+1, "\0", 1);
3011 nData = atoi(sData);
3014 else
3016 memcpy(sData, sRule+nRuleSize-1, 1);
3017 memcpy(sData+1, "\0", 1);
3018 nData = atoi(sData);
3021 return nData;
3024 /**************************************************************************
3025 * GetNumberFormatA (KERNEL32.@)
3027 INT WINAPI GetNumberFormatA(LCID locale, DWORD dwflags,
3028 LPCSTR lpvalue, const NUMBERFMTA * lpFormat,
3029 LPSTR lpNumberStr, int cchNumber)
3031 char sNumberDigits[3], sDecimalSymbol[5], sDigitsInGroup[11], sDigitGroupSymbol[5], sILZero[2];
3032 INT nNumberDigits, nNumberDecimal, i, j, nCounter, nStep, nRuleIndex, nGrouping, nDigits, retVal, nLZ;
3033 char sNumber[128], sDestination[128], sDigitsAfterDecimal[10], sDigitsBeforeDecimal[128];
3034 char sRule[10], sSemiColumn[]=";", sBuffer[5], sNegNumber[2];
3035 char *pStr = NULL, *pTmpStr = NULL;
3036 LCID systemDefaultLCID;
3037 BOOL bNegative = FALSE;
3038 enum Operations
3040 USE_PARAMETER,
3041 USE_LOCALEINFO,
3042 USE_SYSTEMDEFAULT,
3043 RETURN_ERROR
3044 } used_operation;
3046 strncpy(sNumber, lpvalue, 128);
3047 sNumber[127] = '\0';
3049 /* Make sure we have a valid input string, get the number
3050 * of digits before and after the decimal symbol, and check
3051 * if this is a negative number.
3053 if ( OLE_GetNumberComponents(sNumber, sDigitsBeforeDecimal, sDigitsAfterDecimal, &bNegative) != -1)
3055 nNumberDecimal = strlen(sDigitsBeforeDecimal);
3056 nDigits = strlen(sDigitsAfterDecimal);
3058 else
3060 SetLastError(ERROR_INVALID_PARAMETER);
3061 return 0;
3064 /* Which source will we use to format the string */
3065 used_operation = RETURN_ERROR;
3066 if (lpFormat != NULL)
3068 if (dwflags == 0)
3069 used_operation = USE_PARAMETER;
3071 else
3073 if (dwflags & LOCALE_NOUSEROVERRIDE)
3074 used_operation = USE_LOCALEINFO;
3075 else
3076 used_operation = USE_SYSTEMDEFAULT;
3079 /* Load the fields we need */
3080 switch(used_operation)
3082 case USE_LOCALEINFO:
3083 GetLocaleInfoA(locale, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
3084 GetLocaleInfoA(locale, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
3085 GetLocaleInfoA(locale, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
3086 GetLocaleInfoA(locale, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
3087 GetLocaleInfoA(locale, LOCALE_ILZERO, sILZero, sizeof(sILZero));
3088 GetLocaleInfoA(locale, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
3089 break;
3090 case USE_PARAMETER:
3091 sprintf(sNumberDigits, "%d",lpFormat->NumDigits);
3092 strcpy(sDecimalSymbol, lpFormat->lpDecimalSep);
3093 sprintf(sDigitsInGroup, "%d;0",lpFormat->Grouping);
3094 strcpy(sDigitGroupSymbol, lpFormat->lpThousandSep);
3095 sprintf(sILZero, "%d",lpFormat->LeadingZero);
3096 sprintf(sNegNumber, "%d",lpFormat->NegativeOrder);
3097 break;
3098 case USE_SYSTEMDEFAULT:
3099 systemDefaultLCID = GetSystemDefaultLCID();
3100 GetLocaleInfoA(systemDefaultLCID, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
3101 GetLocaleInfoA(systemDefaultLCID, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
3102 GetLocaleInfoA(systemDefaultLCID, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
3103 GetLocaleInfoA(systemDefaultLCID, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
3104 GetLocaleInfoA(systemDefaultLCID, LOCALE_ILZERO, sILZero, sizeof(sILZero));
3105 GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
3106 break;
3107 default:
3108 SetLastError(ERROR_INVALID_PARAMETER);
3109 return 0;
3112 nNumberDigits = atoi(sNumberDigits);
3114 /* Remove the ";" */
3115 i=0;
3116 j = 1;
3117 for (nCounter=0; nCounter<strlen(sDigitsInGroup); nCounter++)
3119 if ( memcmp(sDigitsInGroup + nCounter, sSemiColumn, 1) != 0 )
3121 memcpy(sRule + j, sDigitsInGroup + nCounter, 1);
3122 i++;
3123 j++;
3126 sprintf(sBuffer, "%d", i);
3127 memcpy(sRule, sBuffer, 1); /* Number of digits in the groups ( used by OLE_GetGrouping() ) */
3128 memcpy(sRule + j, "\0", 1);
3130 /* First, format the digits before the decimal. */
3131 if ((nNumberDecimal>0) && (atoi(sDigitsBeforeDecimal) != 0))
3133 /* Working on an inverted string is easier ! */
3134 OLE_InvertString(sDigitsBeforeDecimal);
3136 nStep = nCounter = i = j = 0;
3137 nRuleIndex = 1;
3138 nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
3140 /* Here, we will loop until we reach the end of the string.
3141 * An internal counter (j) is used in order to know when to
3142 * insert the "digit group symbol".
3144 while (nNumberDecimal > 0)
3146 i = nCounter + nStep;
3147 memcpy(sDestination + i, sDigitsBeforeDecimal + nCounter, 1);
3148 nCounter++;
3149 j++;
3150 if (j >= nGrouping)
3152 j = 0;
3153 if (nRuleIndex < sRule[0])
3154 nRuleIndex++;
3155 nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
3156 memcpy(sDestination + i+1, sDigitGroupSymbol, strlen(sDigitGroupSymbol));
3157 nStep+= strlen(sDigitGroupSymbol);
3160 nNumberDecimal--;
3163 memcpy(sDestination + i+1, "\0", 1);
3164 /* Get the string in the right order ! */
3165 OLE_InvertString(sDestination);
3167 else
3169 nLZ = atoi(sILZero);
3170 if (nLZ != 0)
3172 /* Use 0.xxx instead of .xxx */
3173 memcpy(sDestination, "0", 1);
3174 memcpy(sDestination+1, "\0", 1);
3176 else
3177 memcpy(sDestination, "\0", 1);
3181 /* Second, format the digits after the decimal. */
3182 j = 0;
3183 nCounter = nNumberDigits;
3184 if ( (nDigits>0) && (pStr = strstr (sNumber, ".")) )
3186 i = strlen(sNumber) - strlen(pStr) + 1;
3187 strncpy ( sDigitsAfterDecimal, sNumber + i, nNumberDigits);
3188 j = strlen(sDigitsAfterDecimal);
3189 if (j < nNumberDigits)
3190 nCounter = nNumberDigits-j;
3192 for (i=0;i<nCounter;i++)
3193 memcpy(sDigitsAfterDecimal+i+j, "0", 1);
3194 memcpy(sDigitsAfterDecimal + nNumberDigits, "\0", 1);
3196 i = strlen(sDestination);
3197 j = strlen(sDigitsAfterDecimal);
3198 /* Finally, construct the resulting formatted string. */
3200 for (nCounter=0; nCounter<i; nCounter++)
3201 memcpy(sNumber + nCounter, sDestination + nCounter, 1);
3203 memcpy(sNumber + nCounter, sDecimalSymbol, strlen(sDecimalSymbol));
3205 for (i=0; i<j; i++)
3206 memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), sDigitsAfterDecimal + i, 1);
3207 memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), "\0", 1);
3209 /* Is it a negative number */
3210 if (bNegative == TRUE)
3212 i = atoi(sNegNumber);
3213 pStr = sDestination;
3214 pTmpStr = sNumber;
3215 switch (i)
3217 case 0:
3218 *pStr++ = '(';
3219 while (*sNumber != '\0')
3220 *pStr++ = *pTmpStr++;
3221 *pStr++ = ')';
3222 break;
3223 case 1:
3224 *pStr++ = '-';
3225 while (*pTmpStr != '\0')
3226 *pStr++ = *pTmpStr++;
3227 break;
3228 case 2:
3229 *pStr++ = '-';
3230 *pStr++ = ' ';
3231 while (*pTmpStr != '\0')
3232 *pStr++ = *pTmpStr++;
3233 break;
3234 case 3:
3235 while (*pTmpStr != '\0')
3236 *pStr++ = *pTmpStr++;
3237 *pStr++ = '-';
3238 break;
3239 case 4:
3240 while (*pTmpStr != '\0')
3241 *pStr++ = *pTmpStr++;
3242 *pStr++ = ' ';
3243 *pStr++ = '-';
3244 break;
3245 default:
3246 while (*pTmpStr != '\0')
3247 *pStr++ = *pTmpStr++;
3248 break;
3251 else
3252 strcpy(sDestination, sNumber);
3254 /* If cchNumber is zero, then returns the number of bytes or characters
3255 * required to hold the formatted number string
3257 if (cchNumber==0)
3258 retVal = strlen(sDestination) + 1;
3259 else
3261 strncpy (lpNumberStr, sDestination, cchNumber-1);
3262 *(lpNumberStr+cchNumber-1) = '\0'; /* ensure we got a NULL at the end */
3263 retVal = strlen(lpNumberStr);
3266 return retVal;
3269 /**************************************************************************
3270 * GetNumberFormatW (KERNEL32.@)
3272 INT WINAPI GetNumberFormatW(LCID locale, DWORD dwflags,
3273 LPCWSTR lpvalue, const NUMBERFMTW * lpFormat,
3274 LPWSTR lpNumberStr, int cchNumber)
3276 FIXME("%s: stub, no reformatting done\n",debugstr_w(lpvalue));
3278 lstrcpynW( lpNumberStr, lpvalue, cchNumber );
3279 return cchNumber? strlenW( lpNumberStr ) : 0;
3282 /**************************************************************************
3283 * GetCurrencyFormatA (KERNEL32.@)
3285 INT WINAPI GetCurrencyFormatA(LCID locale, DWORD dwflags,
3286 LPCSTR lpvalue, const CURRENCYFMTA * lpFormat,
3287 LPSTR lpCurrencyStr, int cchCurrency)
3289 UINT nPosOrder, nNegOrder;
3290 INT retVal;
3291 char sDestination[128], sNegOrder[8], sPosOrder[8], sCurrencySymbol[8];
3292 char *pDestination = sDestination;
3293 char sNumberFormated[128];
3294 char *pNumberFormated = sNumberFormated;
3295 LCID systemDefaultLCID;
3296 BOOL bIsPositive = FALSE, bValidFormat = FALSE;
3297 enum Operations
3299 USE_PARAMETER,
3300 USE_LOCALEINFO,
3301 USE_SYSTEMDEFAULT,
3302 RETURN_ERROR
3303 } used_operation;
3305 NUMBERFMTA numberFmt;
3307 /* Which source will we use to format the string */
3308 used_operation = RETURN_ERROR;
3309 if (lpFormat != NULL)
3311 if (dwflags == 0)
3312 used_operation = USE_PARAMETER;
3314 else
3316 if (dwflags & LOCALE_NOUSEROVERRIDE)
3317 used_operation = USE_LOCALEINFO;
3318 else
3319 used_operation = USE_SYSTEMDEFAULT;
3322 /* Load the fields we need */
3323 switch(used_operation)
3325 case USE_LOCALEINFO:
3326 /* Specific to CURRENCYFMTA */
3327 GetLocaleInfoA(locale, LOCALE_INEGCURR, sNegOrder, sizeof(sNegOrder));
3328 GetLocaleInfoA(locale, LOCALE_ICURRENCY, sPosOrder, sizeof(sPosOrder));
3329 GetLocaleInfoA(locale, LOCALE_SCURRENCY, sCurrencySymbol, sizeof(sCurrencySymbol));
3331 nPosOrder = atoi(sPosOrder);
3332 nNegOrder = atoi(sNegOrder);
3333 break;
3334 case USE_PARAMETER:
3335 /* Specific to CURRENCYFMTA */
3336 nNegOrder = lpFormat->NegativeOrder;
3337 nPosOrder = lpFormat->PositiveOrder;
3338 strcpy(sCurrencySymbol, lpFormat->lpCurrencySymbol);
3339 break;
3340 case USE_SYSTEMDEFAULT:
3341 systemDefaultLCID = GetSystemDefaultLCID();
3342 /* Specific to CURRENCYFMTA */
3343 GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGCURR, sNegOrder, sizeof(sNegOrder));
3344 GetLocaleInfoA(systemDefaultLCID, LOCALE_ICURRENCY, sPosOrder, sizeof(sPosOrder));
3345 GetLocaleInfoA(systemDefaultLCID, LOCALE_SCURRENCY, sCurrencySymbol, sizeof(sCurrencySymbol));
3347 nPosOrder = atoi(sPosOrder);
3348 nNegOrder = atoi(sNegOrder);
3349 break;
3350 default:
3351 SetLastError(ERROR_INVALID_PARAMETER);
3352 return 0;
3355 /* Construct a temporary number format structure */
3356 if (lpFormat != NULL)
3358 numberFmt.NumDigits = lpFormat->NumDigits;
3359 numberFmt.LeadingZero = lpFormat->LeadingZero;
3360 numberFmt.Grouping = lpFormat->Grouping;
3361 numberFmt.NegativeOrder = 0;
3362 numberFmt.lpDecimalSep = lpFormat->lpDecimalSep;
3363 numberFmt.lpThousandSep = lpFormat->lpThousandSep;
3364 bValidFormat = TRUE;
3367 /* Make a call to GetNumberFormatA() */
3368 if (*lpvalue == '-')
3370 bIsPositive = FALSE;
3371 retVal = GetNumberFormatA(locale,0,lpvalue+1,(bValidFormat)?&numberFmt:NULL,pNumberFormated,128);
3373 else
3375 bIsPositive = TRUE;
3376 retVal = GetNumberFormatA(locale,0,lpvalue,(bValidFormat)?&numberFmt:NULL,pNumberFormated,128);
3379 if (retVal == 0)
3380 return 0;
3382 /* construct the formatted string */
3383 if (bIsPositive)
3385 switch (nPosOrder)
3387 case 0: /* Prefix, no separation */
3388 strcpy (pDestination, sCurrencySymbol);
3389 strcat (pDestination, pNumberFormated);
3390 break;
3391 case 1: /* Suffix, no separation */
3392 strcpy (pDestination, pNumberFormated);
3393 strcat (pDestination, sCurrencySymbol);
3394 break;
3395 case 2: /* Prefix, 1 char separation */
3396 strcpy (pDestination, sCurrencySymbol);
3397 strcat (pDestination, " ");
3398 strcat (pDestination, pNumberFormated);
3399 break;
3400 case 3: /* Suffix, 1 char separation */
3401 strcpy (pDestination, pNumberFormated);
3402 strcat (pDestination, " ");
3403 strcat (pDestination, sCurrencySymbol);
3404 break;
3405 default:
3406 SetLastError(ERROR_INVALID_PARAMETER);
3407 return 0;
3410 else /* negative number */
3412 switch (nNegOrder)
3414 case 0: /* format: ($1.1) */
3415 strcpy (pDestination, "(");
3416 strcat (pDestination, sCurrencySymbol);
3417 strcat (pDestination, pNumberFormated);
3418 strcat (pDestination, ")");
3419 break;
3420 case 1: /* format: -$1.1 */
3421 strcpy (pDestination, "-");
3422 strcat (pDestination, sCurrencySymbol);
3423 strcat (pDestination, pNumberFormated);
3424 break;
3425 case 2: /* format: $-1.1 */
3426 strcpy (pDestination, sCurrencySymbol);
3427 strcat (pDestination, "-");
3428 strcat (pDestination, pNumberFormated);
3429 break;
3430 case 3: /* format: $1.1- */
3431 strcpy (pDestination, sCurrencySymbol);
3432 strcat (pDestination, pNumberFormated);
3433 strcat (pDestination, "-");
3434 break;
3435 case 4: /* format: (1.1$) */
3436 strcpy (pDestination, "(");
3437 strcat (pDestination, pNumberFormated);
3438 strcat (pDestination, sCurrencySymbol);
3439 strcat (pDestination, ")");
3440 break;
3441 case 5: /* format: -1.1$ */
3442 strcpy (pDestination, "-");
3443 strcat (pDestination, pNumberFormated);
3444 strcat (pDestination, sCurrencySymbol);
3445 break;
3446 case 6: /* format: 1.1-$ */
3447 strcpy (pDestination, pNumberFormated);
3448 strcat (pDestination, "-");
3449 strcat (pDestination, sCurrencySymbol);
3450 break;
3451 case 7: /* format: 1.1$- */
3452 strcpy (pDestination, pNumberFormated);
3453 strcat (pDestination, sCurrencySymbol);
3454 strcat (pDestination, "-");
3455 break;
3456 case 8: /* format: -1.1 $ */
3457 strcpy (pDestination, "-");
3458 strcat (pDestination, pNumberFormated);
3459 strcat (pDestination, " ");
3460 strcat (pDestination, sCurrencySymbol);
3461 break;
3462 case 9: /* format: -$ 1.1 */
3463 strcpy (pDestination, "-");
3464 strcat (pDestination, sCurrencySymbol);
3465 strcat (pDestination, " ");
3466 strcat (pDestination, pNumberFormated);
3467 break;
3468 case 10: /* format: 1.1 $- */
3469 strcpy (pDestination, pNumberFormated);
3470 strcat (pDestination, " ");
3471 strcat (pDestination, sCurrencySymbol);
3472 strcat (pDestination, "-");
3473 break;
3474 case 11: /* format: $ 1.1- */
3475 strcpy (pDestination, sCurrencySymbol);
3476 strcat (pDestination, " ");
3477 strcat (pDestination, pNumberFormated);
3478 strcat (pDestination, "-");
3479 break;
3480 case 12: /* format: $ -1.1 */
3481 strcpy (pDestination, sCurrencySymbol);
3482 strcat (pDestination, " ");
3483 strcat (pDestination, "-");
3484 strcat (pDestination, pNumberFormated);
3485 break;
3486 case 13: /* format: 1.1- $ */
3487 strcpy (pDestination, pNumberFormated);
3488 strcat (pDestination, "-");
3489 strcat (pDestination, " ");
3490 strcat (pDestination, sCurrencySymbol);
3491 break;
3492 case 14: /* format: ($ 1.1) */
3493 strcpy (pDestination, "(");
3494 strcat (pDestination, sCurrencySymbol);
3495 strcat (pDestination, " ");
3496 strcat (pDestination, pNumberFormated);
3497 strcat (pDestination, ")");
3498 break;
3499 case 15: /* format: (1.1 $) */
3500 strcpy (pDestination, "(");
3501 strcat (pDestination, pNumberFormated);
3502 strcat (pDestination, " ");
3503 strcat (pDestination, sCurrencySymbol);
3504 strcat (pDestination, ")");
3505 break;
3506 default:
3507 SetLastError(ERROR_INVALID_PARAMETER);
3508 return 0;
3512 if (cchCurrency == 0)
3513 return strlen(pDestination) + 1;
3515 else
3517 strncpy (lpCurrencyStr, pDestination, cchCurrency-1);
3518 *(lpCurrencyStr+cchCurrency-1) = '\0'; /* ensure we got a NULL at the end */
3519 return strlen(lpCurrencyStr);
3523 /**************************************************************************
3524 * GetCurrencyFormatW (KERNEL32.@)
3526 INT WINAPI GetCurrencyFormatW(LCID locale, DWORD dwflags,
3527 LPCWSTR lpvalue, const CURRENCYFMTW * lpFormat,
3528 LPWSTR lpCurrencyStr, int cchCurrency)
3530 FIXME("This API function is NOT implemented !\n");
3531 return 0;
3534 /******************************************************************************
3535 * OLE2NLS_CheckLocale [intern]
3537 static LCID OLE2NLS_CheckLocale (LCID locale)
3539 if (!locale)
3540 { locale = LOCALE_SYSTEM_DEFAULT;
3543 if (locale == LOCALE_SYSTEM_DEFAULT)
3544 { return GetSystemDefaultLCID();
3546 else if (locale == LOCALE_USER_DEFAULT)
3547 { return GetUserDefaultLCID();
3549 else
3550 { return locale;
3553 /******************************************************************************
3554 * GetTimeFormatA [KERNEL32.@]
3555 * Makes an ASCII string of the time
3557 * Formats date according to format, or locale default if format is
3558 * NULL. The format consists of literal characters and fields as follows:
3560 * h hours with no leading zero (12-hour)
3561 * hh hours with full two digits
3562 * H hours with no leading zero (24-hour)
3563 * HH hours with full two digits
3564 * m minutes with no leading zero
3565 * mm minutes with full two digits
3566 * s seconds with no leading zero
3567 * ss seconds with full two digits
3568 * t time marker (A or P)
3569 * tt time marker (AM, PM)
3572 INT WINAPI
3573 GetTimeFormatA(LCID locale, /* [in] */
3574 DWORD flags, /* [in] */
3575 const SYSTEMTIME* xtime, /* [in] */
3576 LPCSTR format, /* [in] */
3577 LPSTR timestr, /* [out] */
3578 INT timelen /* [in] */)
3579 { char format_buf[40];
3580 LPCSTR thisformat;
3581 SYSTEMTIME t;
3582 const SYSTEMTIME* thistime;
3583 LCID thislocale=0;
3584 DWORD thisflags=LOCALE_STIMEFORMAT; /* standard timeformat */
3585 INT ret;
3587 TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,xtime,format,timestr,timelen);
3589 thislocale = OLE2NLS_CheckLocale ( locale );
3591 if (format == NULL)
3592 { if (flags & LOCALE_NOUSEROVERRIDE) /* use system default */
3593 { thislocale = GetSystemDefaultLCID();
3595 GetLocaleInfoA(thislocale, thisflags, format_buf, sizeof(format_buf));
3596 thisformat = format_buf;
3598 else
3599 { thisformat = format;
3602 if (xtime == NULL) /* NULL means use the current local time */
3603 { GetLocalTime(&t);
3604 thistime = &t;
3606 else
3607 { thistime = xtime;
3608 /* Check that hour,min and sec is in range */
3610 ret = OLE_GetFormatA(thislocale, thisflags, flags, thistime, thisformat,
3611 timestr, timelen);
3612 return ret;
3616 /******************************************************************************
3617 * GetTimeFormatW [KERNEL32.@]
3618 * Makes a Unicode string of the time
3620 INT WINAPI
3621 GetTimeFormatW(LCID locale, /* [in] */
3622 DWORD flags, /* [in] */
3623 const SYSTEMTIME* xtime, /* [in] */
3624 LPCWSTR format, /* [in] */
3625 LPWSTR timestr, /* [out] */
3626 INT timelen /* [in] */)
3627 { WCHAR format_buf[40];
3628 LPCWSTR thisformat;
3629 SYSTEMTIME t;
3630 const SYSTEMTIME* thistime;
3631 LCID thislocale=0;
3632 DWORD thisflags=LOCALE_STIMEFORMAT; /* standard timeformat */
3633 INT ret;
3635 TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,
3636 xtime,debugstr_w(format),timestr,timelen);
3638 thislocale = OLE2NLS_CheckLocale ( locale );
3640 if (format == NULL)
3641 { if (flags & LOCALE_NOUSEROVERRIDE) /* use system default */
3642 { thislocale = GetSystemDefaultLCID();
3644 GetLocaleInfoW(thislocale, thisflags, format_buf, 40);
3645 thisformat = format_buf;
3647 else
3648 { thisformat = format;
3651 if (xtime == NULL) /* NULL means use the current local time */
3652 { GetLocalTime(&t);
3653 thistime = &t;
3655 else
3656 { thistime = xtime;
3659 ret = OLE_GetFormatW(thislocale, thisflags, flags, thistime, thisformat,
3660 timestr, timelen);
3661 return ret;
3664 /******************************************************************************
3665 * EnumCalendarInfoA [KERNEL32.@]
3667 BOOL WINAPI EnumCalendarInfoA(
3668 CALINFO_ENUMPROCA calinfoproc,LCID locale,CALID calendar,CALTYPE caltype
3670 FIXME("(%p,0x%04lx,0x%08lx,0x%08lx),stub!\n",calinfoproc,locale,calendar,caltype);
3671 return FALSE;