- Fixed resource functions to work directly from the HMODULE instead of
[wine] / ole / ole2nls.c
1 /*
2  *      National Language Support library
3  *
4  *      Copyright 1995  Martin von Loewis
5  *      Copyright 1998  David Lee Lambert
6  *      Copyright 2000  Julio César Gázquez
7  */
8
9 #include <string.h>
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <stdlib.h>
13 #include <locale.h>
14 #include "windef.h"
15 #include "wingdi.h"
16 #include "winuser.h"
17 #include "wine/unicode.h"
18 #include "heap.h"
19 #include "options.h"
20 #include "winver.h"
21 #include "winnls.h"
22 #include "winreg.h"
23 #include "winerror.h"
24 #include "debugtools.h"
25 #include "main.h"
26
27 DEFAULT_DEBUG_CHANNEL(string);
28
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}
142 };
143
144 static char *GetLocaleSubkeyName( DWORD lctype );
145
146 /***********************************************************************
147  *           GetUserDefaultLCID       [KERNEL32.425]
148  */
149 LCID WINAPI GetUserDefaultLCID(void)
150 {
151         return MAKELCID( GetUserDefaultLangID() , SORT_DEFAULT );
152 }
153
154 /***********************************************************************
155  *         GetSystemDefaultLCID       [KERNEL32.400]
156  */
157 LCID WINAPI GetSystemDefaultLCID(void)
158 {
159         return GetUserDefaultLCID();
160 }
161
162 /***********************************************************************
163  *         GetUserDefaultLangID       [KERNEL32.426]
164  */
165 LANGID WINAPI GetUserDefaultLangID(void)
166 {
167         /* caching result, if defined from environment, which should (?) not change during a WINE session */
168         static  LANGID  userLCID = 0;
169
170         if (userLCID == 0)
171         {
172                 char buf[256];
173                 char *lang,*country,*charset,*dialect,*next;
174
175                 if (GetEnvironmentVariableA( "LANGUAGE", buf, sizeof(buf) )) goto ok;
176                 if (GetEnvironmentVariableA( "LANG", buf, sizeof(buf) )) goto ok;
177                 if (GetEnvironmentVariableA( "LC_ALL", buf, sizeof(buf) )) goto ok;
178                 if (GetEnvironmentVariableA( "LC_MESSAGES", buf, sizeof(buf) )) goto ok;
179                 if (GetEnvironmentVariableA( "LC_CTYPE", buf, sizeof(buf) )) goto ok;
180
181                 return userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
182
183         ok:
184                 if (!strcmp(buf,"POSIX") || !strcmp(buf,"C"))
185                     return userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
186                 
187                 lang=buf;
188                 
189                 do {
190                         next=strchr(lang,':'); if (next) *next++='\0';
191                         dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
192                         charset=strchr(lang,'.'); if (charset) *charset++='\0';
193                         country=strchr(lang,'_'); if (country) *country++='\0';
194                         
195                         userLCID = MAIN_GetLanguageID(lang, country, charset, dialect);
196                         
197                         lang=next;
198                 } while (lang && !userLCID);
199
200                 if (!userLCID)
201                 {
202                     MESSAGE( "Warning: language '%s' not recognized, defaulting to English\n",
203                              buf );
204                     userLCID = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
205                 }
206         }
207         return userLCID;
208 }
209
210 /***********************************************************************
211  *         GetSystemDefaultLangID     [KERNEL32.401]
212  */
213 LANGID WINAPI GetSystemDefaultLangID(void)
214 {
215         return GetUserDefaultLangID();
216 }
217
218 /******************************************************************************
219  * ConvertDefaultLocale [KERNEL32.147]
220  */
221 LCID WINAPI ConvertDefaultLocale (LCID lcid)
222 {       switch (lcid)
223         {  case LOCALE_SYSTEM_DEFAULT:
224              return GetSystemDefaultLCID();
225            case LOCALE_USER_DEFAULT:
226              return GetUserDefaultLCID();
227            case LOCALE_NEUTRAL:
228              return MAKELCID (LANG_NEUTRAL, SUBLANG_NEUTRAL);
229         }  
230         return MAKELANGID( PRIMARYLANGID(lcid), SUBLANG_NEUTRAL);
231 }
232
233 /* Enhanced version of LoadStringW.
234  * It takes LanguageId to find and load language dependant resources.
235  * Resource is copied as is: "binary" strings are not truncated.
236  * Return: length of resource + 1 to distinguish absent resources
237  * from the resources with zero length.
238  */
239 static INT NLS_LoadStringExW(HMODULE hModule, LANGID lang_id, UINT res_id, LPWSTR buffer, INT buflen)
240 {
241     HRSRC hrsrc;
242     HGLOBAL hmem;
243     WCHAR *p;
244     int string_num;
245     int i;
246
247     hrsrc = FindResourceExW(hModule, RT_STRINGW, (LPCWSTR)((res_id >> 4) + 1), lang_id);
248
249     if(!hrsrc) return 0;
250     hmem = LoadResource(hModule, hrsrc);
251     if(!hmem) return 0;
252     
253     p = LockResource(hmem);
254     string_num = res_id & 0x000f;
255     for(i = 0; i < string_num; i++)
256         p += *p + 1;
257     
258     TRACE("strlen = %d\n", (int)*p );
259     
260     if (buffer == NULL) return *p;
261     i = min(buflen - 1, *p);
262     if (i > 0) {
263         memcpy(buffer, p + 1, i * sizeof (WCHAR));
264         buffer[i] = (WCHAR) 0;
265     } else {
266         if (buflen > 1)
267             buffer[0] = (WCHAR) 0;
268     }
269
270     FreeResource(hmem);
271     TRACE("\"%s\" loaded!\n", debugstr_w(buffer));
272     return (i + 1);
273 }
274
275 /******************************************************************************
276  * GetLocaleInfoA [KERNEL32.342]
277  *
278  * NOTES 
279  *  LANG_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
280  *
281  *  MS online documentation states that the string returned is NULL terminated
282  *  except for LOCALE_FONTSIGNATURE  which "will return a non-NULL
283  *  terminated string".
284  */
285 INT WINAPI GetLocaleInfoA(LCID lcid,LCTYPE LCType,LPSTR buf,INT len)
286 {
287     LPCSTR  retString = NULL;
288     int found = 0, i;
289     char    *pacKey;
290     char    acBuffer[128];
291     DWORD   dwBufferSize=128;
292     BOOL NoUserOverride;
293
294   TRACE("(lcid=0x%lx,lctype=0x%lx,%p,%x)\n",lcid,LCType,buf,len);
295
296   if (len && (! buf) ) {
297     SetLastError(ERROR_INSUFFICIENT_BUFFER);
298                 return 0;
299         }
300
301         if (lcid == LOCALE_NEUTRAL || lcid == LANG_SYSTEM_DEFAULT)
302         {
303             lcid = GetSystemDefaultLCID();
304         } 
305         else if (lcid == LANG_USER_DEFAULT) /*0x800*/
306         {
307             lcid = GetUserDefaultLCID();
308         }
309
310     /* LOCALE_NOUSEROVERRIDE means: do not get user redefined settings
311        from the registry. Instead, use system default values. */
312     NoUserOverride = (LCType & LOCALE_NOUSEROVERRIDE) != 0;
313
314         LCType &= ~(LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP);
315
316     /* First, check if it's in the registry. */
317     /* All user customized values are stored in the registry by SetLocaleInfo */
318     if ( !NoUserOverride && (pacKey = GetLocaleSubkeyName(LCType)) )
319     {
320         char    acRealKey[128];
321         HKEY    hKey;
322
323         sprintf( acRealKey, "Control Panel\\International\\%s", pacKey );
324
325         if ( RegOpenKeyExA( HKEY_CURRENT_USER, acRealKey, 
326                             0, KEY_READ, &hKey) == ERROR_SUCCESS )
327         {
328             if ( RegQueryValueExA( hKey, NULL, NULL, NULL, (LPBYTE)acBuffer, 
329                                    &dwBufferSize ) == ERROR_SUCCESS )
330             {
331                 retString = acBuffer;
332                 found = 1;
333             }
334             RegCloseKey(hKey);
335         }
336     }
337
338     /* If not in the registry, get it from the NLS entries. */
339     if(!found) {
340         WCHAR wcBuffer[128];
341         int res_size;
342
343         /* check if language is registered in the kernel32 resources */
344         if((res_size = NLS_LoadStringExW(GetModuleHandleA("KERNEL32"), LANGIDFROMLCID(lcid),
345                 LCType, wcBuffer, sizeof(wcBuffer)/sizeof(wcBuffer[0])))) {
346             WideCharToMultiByte(CP_ACP, 0, wcBuffer, res_size, acBuffer, dwBufferSize, NULL, NULL);
347             retString = acBuffer;
348             found = 1;
349         }
350     }
351
352     /* if not found report a most descriptive error */
353     if(!found) {
354         retString=0;
355         /* If we are through all of this, retLen should not be zero anymore.
356            If it is, the value is not supported */
357         i=0;
358         while (locale_name2id[i].name!=NULL) {
359             if (LCType == locale_name2id[i].id) {
360                 retString = locale_name2id[i].name;
361                 break;
362             }
363             i++;
364         }
365         if(!retString)
366             FIXME("Unkown LC type %lX\n", LCType);
367         else
368             FIXME("'%s' is not defined for your language (%04X).\n"
369                 "Please define it in dlls/kernel/nls/YourLanguage.nls\n"
370                 "and submit patch for inclusion into the next Wine release.\n",
371                         retString, LOWORD(lcid));
372         SetLastError(ERROR_INVALID_PARAMETER);
373         return 0;                       
374     }
375
376     /* a FONTSIGNATURE is not a string, just 6 DWORDs  */
377     if (LCType == LOCALE_FONTSIGNATURE) {
378         if (len) {
379             len = (len < sizeof(FONTSIGNATURE)) ? len : sizeof(FONTSIGNATURE);
380             memcpy(buf, retString, len);
381             return len;
382         }
383         return sizeof(FONTSIGNATURE);
384     }
385     /* if len=0 return only the length, don't touch the buffer*/
386     if (len) {
387         lstrcpynA(buf,retString,len);
388         return strlen(buf) + 1;
389     }
390     return strlen(retString)+1;
391 }
392
393 /******************************************************************************
394  *              GetLocaleInfoW  [KERNEL32.343]
395  *
396  * NOTES
397  *  MS documentation states that len "specifies the size, in bytes (ANSI version)
398  *  or characters (Unicode version), of" wbuf. Thus the number returned is
399  *  the same between GetLocaleInfoW and GetLocaleInfoA.
400  */
401 INT WINAPI GetLocaleInfoW(LCID lcid,LCTYPE LCType,LPWSTR wbuf,INT len)
402 {       WORD wlen;
403         LPSTR abuf;
404         
405         if (len && (! wbuf) )
406         { SetLastError(ERROR_INSUFFICIENT_BUFFER);
407           return 0;
408         }
409
410         abuf = (LPSTR)HeapAlloc(GetProcessHeap(),0,len);
411         wlen = GetLocaleInfoA(lcid, LCType, abuf, len);
412
413         if (wlen && len)        /* if len=0 return only the length*/
414           lstrcpynAtoW(wbuf,abuf,len);
415
416         HeapFree(GetProcessHeap(),0,abuf);
417         return wlen;
418 }
419
420 /******************************************************************************
421  *
422  *              GetLocaleSubkeyName [helper function]
423  *
424  *          - For use with the registry.
425  *          - Gets the registry subkey name for a given lctype.
426  */
427 static char *GetLocaleSubkeyName( DWORD lctype )
428 {
429     char    *pacKey=NULL;
430
431     switch ( lctype )
432     {
433     /* These values are used by SetLocaleInfo and GetLocaleInfo, and
434      * the values are stored in the registry, confirmed under Windows,
435      * for the ones that actually assign pacKey. Cases that aren't finished
436      * have not been confirmed, so that must be done before they can be
437      * added.
438      */
439     case LOCALE_SDATE :        /* The date separator. */
440         pacKey = "sDate";
441         break;
442     case LOCALE_ICURRDIGITS:
443         pacKey = "iCurrDigits";
444         break;
445     case LOCALE_SDECIMAL :
446         pacKey = "sDecimal";
447         break;
448     case LOCALE_ICURRENCY:
449         pacKey = "iCurrency";
450         break;
451     case LOCALE_SGROUPING :
452         pacKey = "sGrouping";
453         break;
454     case LOCALE_IDIGITS:
455         pacKey = "iDigits";
456         break;
457     case LOCALE_SLIST :
458         pacKey = "sList";
459         break;
460     /* case LOCALE_ICALENDARTYPE: */
461     /* case LOCALE_IFIRSTDAYOFWEEK: */
462     /* case LOCALE_IFIRSTWEEKOFYEAR: */
463     /* case LOCALE_SYEARMONTH : */
464     /* case LOCALE_SPOSITIVESIGN : */
465     /* case LOCALE_IPAPERSIZE: */
466     /*     break; */
467     case LOCALE_SLONGDATE :
468         pacKey = "sLongDate";
469         break;
470     case LOCALE_SMONDECIMALSEP :
471         pacKey = "sMonDecimalSep";
472         break;
473     case LOCALE_SMONGROUPING:
474         pacKey = "sMonGrouping";
475         break;
476     case LOCALE_IMEASURE:
477         pacKey = "iMeasure";
478         break;
479     case LOCALE_SMONTHOUSANDSEP :
480         pacKey = "sMonThousandSep";
481         break;
482     case LOCALE_INEGCURR:
483         pacKey = "iNegCurr";
484         break;
485     case LOCALE_SNEGATIVESIGN :
486         pacKey = "sNegativeSign";
487         break;
488     case LOCALE_INEGNUMBER:
489         pacKey = "iNegNumber";
490         break;
491     case LOCALE_SSHORTDATE :
492         pacKey = "sShortDate";
493         break;
494     case LOCALE_ILDATE:        /* Long Date format ordering specifier. */
495         pacKey = "iLDate";
496         break;
497     case LOCALE_ILZERO:
498         pacKey = "iLZero";
499         break;
500     case LOCALE_ITLZERO:
501         pacKey = "iTLZero";
502         break;
503     case LOCALE_ITIME:        /* Time format specifier. */
504         pacKey = "iTime";
505         break;
506     case LOCALE_STHOUSAND :
507         pacKey = "sThousand";
508         break;
509     case LOCALE_S1159:        /* AM */
510         pacKey = "s1159";
511         break;
512     case LOCALE_STIME:
513         pacKey = "sTime";
514         break;
515     case LOCALE_S2359:        /* PM */
516         pacKey = "s2359";
517         break;
518     case LOCALE_STIMEFORMAT :
519         pacKey = "sTimeFormat";
520         break;
521     case LOCALE_SCURRENCY:
522         pacKey = "sCurrency";
523         break;
524
525     /* The following are not listed under MSDN as supported, 
526      * but seem to be used and also stored in the registry.
527      */
528
529     case LOCALE_IDATE:
530         pacKey = "iDate";
531         break;
532     case LOCALE_SCOUNTRY:
533         pacKey = "sCountry";
534         break;
535     case LOCALE_ICOUNTRY:
536         pacKey = "iCountry";
537         break;
538     case LOCALE_SLANGUAGE:
539         pacKey = "sLanguage";
540         break;
541
542     default:
543         break;
544     }
545
546     return( pacKey );
547 }
548
549 /******************************************************************************
550  *              SetLocaleInfoA  [KERNEL32.656]
551  */
552 BOOL16 WINAPI SetLocaleInfoA(DWORD lcid, DWORD lctype, LPCSTR data)
553 {
554     HKEY    hKey;
555     char    *pacKey;
556     char    acRealKey[128];
557
558     if ( (pacKey = GetLocaleSubkeyName(lctype)) )
559     {
560         sprintf( acRealKey, "Control Panel\\International\\%s", pacKey );
561         if ( RegCreateKeyA( HKEY_CURRENT_USER, acRealKey, 
562                                &hKey ) == ERROR_SUCCESS )
563         {
564             if ( RegSetValueExA( hKey, NULL, 0, REG_SZ, 
565                                  data, strlen(data)+1 ) != ERROR_SUCCESS )
566             {
567                 ERR("SetLocaleInfoA: %s did not work\n", pacKey );
568             }
569             RegCloseKey( hKey );
570         }
571     }
572     else
573     {
574     FIXME("(%ld,%ld,%s): stub\n",lcid,lctype,data);
575     }
576     return TRUE;
577 }
578
579 /******************************************************************************
580  *              IsValidLocale   [KERNEL32.489]
581  */
582 BOOL WINAPI IsValidLocale(LCID lcid,DWORD flags)
583 {
584     /* check if language is registered in the kernel32 resources */
585     if(!FindResourceExW(GetModuleHandleA("KERNEL32"), RT_STRINGW, (LPCWSTR)LOCALE_ILANGUAGE, LOWORD(lcid)))
586         return FALSE;
587     else
588         return TRUE;
589 }
590
591 static BOOL CALLBACK EnumResourceLanguagesProcW(HMODULE hModule, LPCWSTR type,
592                 LPCWSTR name, WORD LangID, LONG lParam)
593 {
594     CHAR bufA[20];
595     WCHAR bufW[20];
596     LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
597     sprintf(bufA, "%08X", (UINT)LangID);
598     MultiByteToWideChar(CP_ACP, 0, bufA, -1, bufW, sizeof(bufW)/sizeof(bufW[0]));
599     return lpfnLocaleEnum(bufW);
600 }
601
602 /******************************************************************************
603  *              EnumSystemLocalesW      [KERNEL32.209]
604  */
605 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum,
606                                     DWORD flags )
607 {
608     TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
609
610     EnumResourceLanguagesW(GetModuleHandleA("KERNEL32"), RT_STRINGW,
611             (LPCWSTR)LOCALE_ILANGUAGE, EnumResourceLanguagesProcW,
612             (LONG)lpfnLocaleEnum);
613
614     return TRUE;
615 }
616
617 static BOOL CALLBACK EnumResourceLanguagesProcA(HMODULE hModule, LPCSTR type,
618                 LPCSTR name, WORD LangID, LONG lParam)
619 {
620     CHAR bufA[20];
621     LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
622     sprintf(bufA, "%08X", (UINT)LangID);
623     return lpfnLocaleEnum(bufA);
624 }
625
626 /******************************************************************************
627  *              EnumSystemLocalesA      [KERNEL32.208]
628  */
629 BOOL WINAPI EnumSystemLocalesA(LOCALE_ENUMPROCA lpfnLocaleEnum,
630                                    DWORD flags)
631 {
632     TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
633
634     EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
635             (LPCSTR)LOCALE_ILANGUAGE, EnumResourceLanguagesProcA,
636             (LONG)lpfnLocaleEnum);
637
638     return TRUE;
639 }
640
641 static const unsigned char CT_CType2_LUT[] = {
642   C2_NOTAPPLICABLE, /*   -   0 */
643   C2_NOTAPPLICABLE, /*   -   1 */
644   C2_NOTAPPLICABLE, /*   -   2 */
645   C2_NOTAPPLICABLE, /*   -   3 */
646   C2_NOTAPPLICABLE, /*   -   4 */
647   C2_NOTAPPLICABLE, /*   -   5 */
648   C2_NOTAPPLICABLE, /*   -   6 */
649   C2_NOTAPPLICABLE, /*   -   7 */
650   C2_NOTAPPLICABLE, /*   -   8 */
651   C2_SEGMENTSEPARATOR, /*   -   9 */
652   C2_NOTAPPLICABLE, /*   -  10 */
653   C2_NOTAPPLICABLE, /*   -  11 */
654   C2_NOTAPPLICABLE, /*   -  12 */
655   C2_NOTAPPLICABLE, /*   -  13 */
656   C2_NOTAPPLICABLE, /*   -  14 */
657   C2_NOTAPPLICABLE, /*   -  15 */
658   C2_NOTAPPLICABLE, /*   -  16 */
659   C2_NOTAPPLICABLE, /*   -  17 */
660   C2_NOTAPPLICABLE, /*   -  18 */
661   C2_NOTAPPLICABLE, /*   -  19 */
662   C2_NOTAPPLICABLE, /*   -  20 */
663   C2_NOTAPPLICABLE, /*   -  21 */
664   C2_NOTAPPLICABLE, /*   -  22 */
665   C2_NOTAPPLICABLE, /*   -  23 */
666   C2_NOTAPPLICABLE, /*   -  24 */
667   C2_NOTAPPLICABLE, /*   -  25 */
668   C2_NOTAPPLICABLE, /*   -  26 */
669   C2_NOTAPPLICABLE, /*   -  27 */
670   C2_NOTAPPLICABLE, /*   -  28 */
671   C2_NOTAPPLICABLE, /*   -  29 */
672   C2_NOTAPPLICABLE, /*   -  30 */
673   C2_NOTAPPLICABLE, /*   -  31 */
674   C2_WHITESPACE, /*   -  32 */
675   C2_OTHERNEUTRAL, /* ! -  33 */
676   C2_OTHERNEUTRAL, /* " -  34 */ /* " */
677   C2_EUROPETERMINATOR, /* # -  35 */
678   C2_EUROPETERMINATOR, /* $ -  36 */
679   C2_EUROPETERMINATOR, /* % -  37 */
680   C2_LEFTTORIGHT, /* & -  38 */
681   C2_OTHERNEUTRAL, /* ' -  39 */
682   C2_OTHERNEUTRAL, /* ( -  40 */
683   C2_OTHERNEUTRAL, /* ) -  41 */
684   C2_OTHERNEUTRAL, /* * -  42 */
685   C2_EUROPETERMINATOR, /* + -  43 */
686   C2_COMMONSEPARATOR, /* , -  44 */
687   C2_EUROPETERMINATOR, /* - -  45 */
688   C2_EUROPESEPARATOR, /* . -  46 */
689   C2_EUROPESEPARATOR, /* / -  47 */
690   C2_EUROPENUMBER, /* 0 -  48 */
691   C2_EUROPENUMBER, /* 1 -  49 */
692   C2_EUROPENUMBER, /* 2 -  50 */
693   C2_EUROPENUMBER, /* 3 -  51 */
694   C2_EUROPENUMBER, /* 4 -  52 */
695   C2_EUROPENUMBER, /* 5 -  53 */
696   C2_EUROPENUMBER, /* 6 -  54 */
697   C2_EUROPENUMBER, /* 7 -  55 */
698   C2_EUROPENUMBER, /* 8 -  56 */
699   C2_EUROPENUMBER, /* 9 -  57 */
700   C2_COMMONSEPARATOR, /* : -  58 */
701   C2_OTHERNEUTRAL, /* ; -  59 */
702   C2_OTHERNEUTRAL, /* < -  60 */
703   C2_OTHERNEUTRAL, /* = -  61 */
704   C2_OTHERNEUTRAL, /* > -  62 */
705   C2_OTHERNEUTRAL, /* ? -  63 */
706   C2_LEFTTORIGHT, /* @ -  64 */
707   C2_LEFTTORIGHT, /* A -  65 */
708   C2_LEFTTORIGHT, /* B -  66 */
709   C2_LEFTTORIGHT, /* C -  67 */
710   C2_LEFTTORIGHT, /* D -  68 */
711   C2_LEFTTORIGHT, /* E -  69 */
712   C2_LEFTTORIGHT, /* F -  70 */
713   C2_LEFTTORIGHT, /* G -  71 */
714   C2_LEFTTORIGHT, /* H -  72 */
715   C2_LEFTTORIGHT, /* I -  73 */
716   C2_LEFTTORIGHT, /* J -  74 */
717   C2_LEFTTORIGHT, /* K -  75 */
718   C2_LEFTTORIGHT, /* L -  76 */
719   C2_LEFTTORIGHT, /* M -  77 */
720   C2_LEFTTORIGHT, /* N -  78 */
721   C2_LEFTTORIGHT, /* O -  79 */
722   C2_LEFTTORIGHT, /* P -  80 */
723   C2_LEFTTORIGHT, /* Q -  81 */
724   C2_LEFTTORIGHT, /* R -  82 */
725   C2_LEFTTORIGHT, /* S -  83 */
726   C2_LEFTTORIGHT, /* T -  84 */
727   C2_LEFTTORIGHT, /* U -  85 */
728   C2_LEFTTORIGHT, /* V -  86 */
729   C2_LEFTTORIGHT, /* W -  87 */
730   C2_LEFTTORIGHT, /* X -  88 */
731   C2_LEFTTORIGHT, /* Y -  89 */
732   C2_LEFTTORIGHT, /* Z -  90 */
733   C2_OTHERNEUTRAL, /* [ -  91 */
734   C2_OTHERNEUTRAL, /* \ -  92 */
735   C2_OTHERNEUTRAL, /* ] -  93 */
736   C2_OTHERNEUTRAL, /* ^ -  94 */
737   C2_OTHERNEUTRAL, /* _ -  95 */
738   C2_OTHERNEUTRAL, /* ` -  96 */
739   C2_LEFTTORIGHT, /* a -  97 */
740   C2_LEFTTORIGHT, /* b -  98 */
741   C2_LEFTTORIGHT, /* c -  99 */
742   C2_LEFTTORIGHT, /* d - 100 */
743   C2_LEFTTORIGHT, /* e - 101 */
744   C2_LEFTTORIGHT, /* f - 102 */
745   C2_LEFTTORIGHT, /* g - 103 */
746   C2_LEFTTORIGHT, /* h - 104 */
747   C2_LEFTTORIGHT, /* i - 105 */
748   C2_LEFTTORIGHT, /* j - 106 */
749   C2_LEFTTORIGHT, /* k - 107 */
750   C2_LEFTTORIGHT, /* l - 108 */
751   C2_LEFTTORIGHT, /* m - 109 */
752   C2_LEFTTORIGHT, /* n - 110 */
753   C2_LEFTTORIGHT, /* o - 111 */
754   C2_LEFTTORIGHT, /* p - 112 */
755   C2_LEFTTORIGHT, /* q - 113 */
756   C2_LEFTTORIGHT, /* r - 114 */
757   C2_LEFTTORIGHT, /* s - 115 */
758   C2_LEFTTORIGHT, /* t - 116 */
759   C2_LEFTTORIGHT, /* u - 117 */
760   C2_LEFTTORIGHT, /* v - 118 */
761   C2_LEFTTORIGHT, /* w - 119 */
762   C2_LEFTTORIGHT, /* x - 120 */
763   C2_LEFTTORIGHT, /* y - 121 */
764   C2_LEFTTORIGHT, /* z - 122 */
765   C2_OTHERNEUTRAL, /* { - 123 */
766   C2_OTHERNEUTRAL, /* | - 124 */
767   C2_OTHERNEUTRAL, /* } - 125 */
768   C2_OTHERNEUTRAL, /* ~ - 126 */
769   C2_NOTAPPLICABLE, /* \7f - 127 */
770   C2_NOTAPPLICABLE, /* \80 - 128 */
771   C2_NOTAPPLICABLE, /* \81 - 129 */
772   C2_OTHERNEUTRAL, /* \82 - 130 */
773   C2_LEFTTORIGHT, /* \83 - 131 */
774   C2_OTHERNEUTRAL, /* \84 - 132 */
775   C2_OTHERNEUTRAL, /* \85 - 133 */
776   C2_OTHERNEUTRAL, /* \86 - 134 */
777   C2_OTHERNEUTRAL, /* \87 - 135 */
778   C2_LEFTTORIGHT, /* \88 - 136 */
779   C2_EUROPETERMINATOR, /* \89 - 137 */
780   C2_LEFTTORIGHT, /* \8a - 138 */
781   C2_OTHERNEUTRAL, /* \8b - 139 */
782   C2_LEFTTORIGHT, /* \8c - 140 */
783   C2_NOTAPPLICABLE, /* \8d - 141 */
784   C2_NOTAPPLICABLE, /* \8e - 142 */
785   C2_NOTAPPLICABLE, /* \8f - 143 */
786   C2_NOTAPPLICABLE, /* \90 - 144 */
787   C2_OTHERNEUTRAL, /* \91 - 145 */
788   C2_OTHERNEUTRAL, /* \92 - 146 */
789   C2_OTHERNEUTRAL, /* \93 - 147 */
790   C2_OTHERNEUTRAL, /* \94 - 148 */
791   C2_OTHERNEUTRAL, /* \95 - 149 */
792   C2_OTHERNEUTRAL, /* \96 - 150 */
793   C2_OTHERNEUTRAL, /* \97 - 151 */
794   C2_LEFTTORIGHT, /* \98 - 152 */
795   C2_OTHERNEUTRAL, /* \99 - 153 */
796   C2_LEFTTORIGHT, /* \9a - 154 */
797   C2_OTHERNEUTRAL, /* \9b - 155 */
798   C2_LEFTTORIGHT, /* \9c - 156 */
799   C2_NOTAPPLICABLE, /* \9d - 157 */
800   C2_NOTAPPLICABLE, /* \9e - 158 */
801   C2_LEFTTORIGHT, /* \9f - 159 */
802   C2_WHITESPACE, /*   - 160 */
803   C2_OTHERNEUTRAL, /* ¡ - 161 */
804   C2_EUROPETERMINATOR, /* ¢ - 162 */
805   C2_EUROPETERMINATOR, /* £ - 163 */
806   C2_EUROPETERMINATOR, /* ¤ - 164 */
807   C2_EUROPETERMINATOR, /* ¥ - 165 */
808   C2_OTHERNEUTRAL, /* ¦ - 166 */
809   C2_OTHERNEUTRAL, /* § - 167 */
810   C2_OTHERNEUTRAL, /* ¨ - 168 */
811   C2_OTHERNEUTRAL, /* © - 169 */
812   C2_OTHERNEUTRAL, /* ª - 170 */
813   C2_OTHERNEUTRAL, /* « - 171 */
814   C2_OTHERNEUTRAL, /* ¬ - 172 */
815   C2_OTHERNEUTRAL, /* ­ - 173 */
816   C2_OTHERNEUTRAL, /* ® - 174 */
817   C2_OTHERNEUTRAL, /* ¯ - 175 */
818   C2_EUROPETERMINATOR, /* ° - 176 */
819   C2_EUROPETERMINATOR, /* ± - 177 */
820   C2_EUROPENUMBER, /* ² - 178 */
821   C2_EUROPENUMBER, /* ³ - 179 */
822   C2_OTHERNEUTRAL, /* ´ - 180 */
823   C2_OTHERNEUTRAL, /* µ - 181 */
824   C2_OTHERNEUTRAL, /* ¶ - 182 */
825   C2_OTHERNEUTRAL, /* · - 183 */
826   C2_OTHERNEUTRAL, /* ¸ - 184 */
827   C2_EUROPENUMBER, /* ¹ - 185 */
828   C2_OTHERNEUTRAL, /* º - 186 */
829   C2_OTHERNEUTRAL, /* » - 187 */
830   C2_OTHERNEUTRAL, /* ¼ - 188 */
831   C2_OTHERNEUTRAL, /* ½ - 189 */
832   C2_OTHERNEUTRAL, /* ¾ - 190 */
833   C2_OTHERNEUTRAL, /* ¿ - 191 */
834   C2_LEFTTORIGHT, /* À - 192 */
835   C2_LEFTTORIGHT, /* Á - 193 */
836   C2_LEFTTORIGHT, /* Â - 194 */
837   C2_LEFTTORIGHT, /* Ã - 195 */
838   C2_LEFTTORIGHT, /* Ä - 196 */
839   C2_LEFTTORIGHT, /* Å - 197 */
840   C2_LEFTTORIGHT, /* Æ - 198 */
841   C2_LEFTTORIGHT, /* Ç - 199 */
842   C2_LEFTTORIGHT, /* È - 200 */
843   C2_LEFTTORIGHT, /* É - 201 */
844   C2_LEFTTORIGHT, /* Ê - 202 */
845   C2_LEFTTORIGHT, /* Ë - 203 */
846   C2_LEFTTORIGHT, /* Ì - 204 */
847   C2_LEFTTORIGHT, /* Í - 205 */
848   C2_LEFTTORIGHT, /* Î - 206 */
849   C2_LEFTTORIGHT, /* Ï - 207 */
850   C2_LEFTTORIGHT, /* Ð - 208 */
851   C2_LEFTTORIGHT, /* Ñ - 209 */
852   C2_LEFTTORIGHT, /* Ò - 210 */
853   C2_LEFTTORIGHT, /* Ó - 211 */
854   C2_LEFTTORIGHT, /* Ô - 212 */
855   C2_LEFTTORIGHT, /* Õ - 213 */
856   C2_LEFTTORIGHT, /* Ö - 214 */
857   C2_OTHERNEUTRAL, /* × - 215 */
858   C2_LEFTTORIGHT, /* Ø - 216 */
859   C2_LEFTTORIGHT, /* Ù - 217 */
860   C2_LEFTTORIGHT, /* Ú - 218 */
861   C2_LEFTTORIGHT, /* Û - 219 */
862   C2_LEFTTORIGHT, /* Ü - 220 */
863   C2_LEFTTORIGHT, /* Ý - 221 */
864   C2_LEFTTORIGHT, /* Þ - 222 */
865   C2_LEFTTORIGHT, /* ß - 223 */
866   C2_LEFTTORIGHT, /* à - 224 */
867   C2_LEFTTORIGHT, /* á - 225 */
868   C2_LEFTTORIGHT, /* â - 226 */
869   C2_LEFTTORIGHT, /* ã - 227 */
870   C2_LEFTTORIGHT, /* ä - 228 */
871   C2_LEFTTORIGHT, /* å - 229 */
872   C2_LEFTTORIGHT, /* æ - 230 */
873   C2_LEFTTORIGHT, /* ç - 231 */
874   C2_LEFTTORIGHT, /* è - 232 */
875   C2_LEFTTORIGHT, /* é - 233 */
876   C2_LEFTTORIGHT, /* ê - 234 */
877   C2_LEFTTORIGHT, /* ë - 235 */
878   C2_LEFTTORIGHT, /* ì - 236 */
879   C2_LEFTTORIGHT, /* í - 237 */
880   C2_LEFTTORIGHT, /* î - 238 */
881   C2_LEFTTORIGHT, /* ï - 239 */
882   C2_LEFTTORIGHT, /* ð - 240 */
883   C2_LEFTTORIGHT, /* ñ - 241 */
884   C2_LEFTTORIGHT, /* ò - 242 */
885   C2_LEFTTORIGHT, /* ó - 243 */
886   C2_LEFTTORIGHT, /* ô - 244 */
887   C2_LEFTTORIGHT, /* õ - 245 */
888   C2_LEFTTORIGHT, /* ö - 246 */
889   C2_OTHERNEUTRAL, /* ÷ - 247 */
890   C2_LEFTTORIGHT, /* ø - 248 */
891   C2_LEFTTORIGHT, /* ù - 249 */
892   C2_LEFTTORIGHT, /* ú - 250 */
893   C2_LEFTTORIGHT, /* û - 251 */
894   C2_LEFTTORIGHT, /* ü - 252 */
895   C2_LEFTTORIGHT, /* ý - 253 */
896   C2_LEFTTORIGHT, /* þ - 254 */
897   C2_LEFTTORIGHT /* ÿ - 255 */
898 };
899
900 const WORD OLE2NLS_CT_CType3_LUT[] = { 
901   0x0000, /*   -   0 */
902   0x0000, /*   -   1 */
903   0x0000, /*   -   2 */
904   0x0000, /*   -   3 */
905   0x0000, /*   -   4 */
906   0x0000, /*   -   5 */
907   0x0000, /*   -   6 */
908   0x0000, /*   -   7 */
909   0x0000, /*   -   8 */
910   0x0008, /*   -   9 */
911   0x0008, /*   -  10 */
912   0x0008, /*   -  11 */
913   0x0008, /*   -  12 */
914   0x0008, /*   -  13 */
915   0x0000, /*   -  14 */
916   0x0000, /*   -  15 */
917   0x0000, /*   -  16 */
918   0x0000, /*   -  17 */
919   0x0000, /*   -  18 */
920   0x0000, /*   -  19 */
921   0x0000, /*   -  20 */
922   0x0000, /*   -  21 */
923   0x0000, /*   -  22 */
924   0x0000, /*   -  23 */
925   0x0000, /*   -  24 */
926   0x0000, /*   -  25 */
927   0x0000, /*   -  26 */
928   0x0000, /*   -  27 */
929   0x0000, /*   -  28 */
930   0x0000, /*   -  29 */
931   0x0000, /*   -  30 */
932   0x0000, /*   -  31 */
933   0x0048, /*   -  32 */
934   0x0048, /* ! -  33 */
935   0x0448, /* " -  34 */ /* " */
936   0x0048, /* # -  35 */
937   0x0448, /* $ -  36 */
938   0x0048, /* % -  37 */
939   0x0048, /* & -  38 */
940   0x0440, /* ' -  39 */
941   0x0048, /* ( -  40 */
942   0x0048, /* ) -  41 */
943   0x0048, /* * -  42 */
944   0x0048, /* + -  43 */
945   0x0048, /* , -  44 */
946   0x0440, /* - -  45 */
947   0x0048, /* . -  46 */
948   0x0448, /* / -  47 */
949   0x0040, /* 0 -  48 */
950   0x0040, /* 1 -  49 */
951   0x0040, /* 2 -  50 */
952   0x0040, /* 3 -  51 */
953   0x0040, /* 4 -  52 */
954   0x0040, /* 5 -  53 */
955   0x0040, /* 6 -  54 */
956   0x0040, /* 7 -  55 */
957   0x0040, /* 8 -  56 */
958   0x0040, /* 9 -  57 */
959   0x0048, /* : -  58 */
960   0x0048, /* ; -  59 */
961   0x0048, /* < -  60 */
962   0x0448, /* = -  61 */
963   0x0048, /* > -  62 */
964   0x0048, /* ? -  63 */
965   0x0448, /* @ -  64 */
966   0x8040, /* A -  65 */
967   0x8040, /* B -  66 */
968   0x8040, /* C -  67 */
969   0x8040, /* D -  68 */
970   0x8040, /* E -  69 */
971   0x8040, /* F -  70 */
972   0x8040, /* G -  71 */
973   0x8040, /* H -  72 */
974   0x8040, /* I -  73 */
975   0x8040, /* J -  74 */
976   0x8040, /* K -  75 */
977   0x8040, /* L -  76 */
978   0x8040, /* M -  77 */
979   0x8040, /* N -  78 */
980   0x8040, /* O -  79 */
981   0x8040, /* P -  80 */
982   0x8040, /* Q -  81 */
983   0x8040, /* R -  82 */
984   0x8040, /* S -  83 */
985   0x8040, /* T -  84 */
986   0x8040, /* U -  85 */
987   0x8040, /* V -  86 */
988   0x8040, /* W -  87 */
989   0x8040, /* X -  88 */
990   0x8040, /* Y -  89 */
991   0x8040, /* Z -  90 */
992   0x0048, /* [ -  91 */
993   0x0448, /* \ -  92 */
994   0x0048, /* ] -  93 */
995   0x0448, /* ^ -  94 */
996   0x0448, /* _ -  95 */
997   0x0448, /* ` -  96 */
998   0x8040, /* a -  97 */
999   0x8040, /* b -  98 */
1000   0x8040, /* c -  99 */
1001   0x8040, /* d - 100 */
1002   0x8040, /* e - 101 */
1003   0x8040, /* f - 102 */
1004   0x8040, /* g - 103 */
1005   0x8040, /* h - 104 */
1006   0x8040, /* i - 105 */
1007   0x8040, /* j - 106 */
1008   0x8040, /* k - 107 */
1009   0x8040, /* l - 108 */
1010   0x8040, /* m - 109 */
1011   0x8040, /* n - 110 */
1012   0x8040, /* o - 111 */
1013   0x8040, /* p - 112 */
1014   0x8040, /* q - 113 */
1015   0x8040, /* r - 114 */
1016   0x8040, /* s - 115 */
1017   0x8040, /* t - 116 */
1018   0x8040, /* u - 117 */
1019   0x8040, /* v - 118 */
1020   0x8040, /* w - 119 */
1021   0x8040, /* x - 120 */
1022   0x8040, /* y - 121 */
1023   0x8040, /* z - 122 */
1024   0x0048, /* { - 123 */
1025   0x0048, /* | - 124 */
1026   0x0048, /* } - 125 */
1027   0x0448, /* ~ - 126 */
1028   0x0000, /* \7f - 127 */
1029   0x0000, /* \80 - 128 */
1030   0x0000, /* \81 - 129 */
1031   0x0008, /* \82 - 130 */
1032   0x8000, /* \83 - 131 */
1033   0x0008, /* \84 - 132 */
1034   0x0008, /* \85 - 133 */
1035   0x0008, /* \86 - 134 */
1036   0x0008, /* \87 - 135 */
1037   0x0001, /* \88 - 136 */
1038   0x0008, /* \89 - 137 */
1039   0x8003, /* \8a - 138 */
1040   0x0008, /* \8b - 139 */
1041   0x8000, /* \8c - 140 */
1042   0x0000, /* \8d - 141 */
1043   0x0000, /* \8e - 142 */
1044   0x0000, /* \8f - 143 */
1045   0x0000, /* \90 - 144 */
1046   0x0088, /* \91 - 145 */
1047   0x0088, /* \92 - 146 */
1048   0x0088, /* \93 - 147 */
1049   0x0088, /* \94 - 148 */
1050   0x0008, /* \95 - 149 */
1051   0x0400, /* \96 - 150 */
1052   0x0400, /* \97 - 151 */
1053   0x0408, /* \98 - 152 */
1054   0x0000, /* \99 - 153 */
1055   0x8003, /* \9a - 154 */
1056   0x0008, /* \9b - 155 */
1057   0x8000, /* \9c - 156 */
1058   0x0000, /* \9d - 157 */
1059   0x0000, /* \9e - 158 */
1060   0x8003, /* \9f - 159 */
1061   0x0008, /*   - 160 */
1062   0x0008, /* ¡ - 161 */
1063   0x0048, /* ¢ - 162 */
1064   0x0048, /* £ - 163 */
1065   0x0008, /* ¤ - 164 */
1066   0x0048, /* ¥ - 165 */
1067   0x0048, /* ¦ - 166 */
1068   0x0008, /* § - 167 */
1069   0x0408, /* ¨ - 168 */
1070   0x0008, /* © - 169 */
1071   0x0400, /* ª - 170 */
1072   0x0008, /* « - 171 */
1073   0x0048, /* ¬ - 172 */
1074   0x0408, /* ­ - 173 */
1075   0x0008, /* ® - 174 */
1076   0x0448, /* ¯ - 175 */
1077   0x0008, /* ° - 176 */
1078   0x0008, /* ± - 177 */
1079   0x0000, /* ² - 178 */
1080   0x0000, /* ³ - 179 */
1081   0x0408, /* ´ - 180 */
1082   0x0008, /* µ - 181 */
1083   0x0008, /* ¶ - 182 */
1084   0x0008, /* · - 183 */
1085   0x0408, /* ¸ - 184 */
1086   0x0000, /* ¹ - 185 */
1087   0x0400, /* º - 186 */
1088   0x0008, /* » - 187 */
1089   0x0000, /* ¼ - 188 */
1090   0x0000, /* ½ - 189 */
1091   0x0000, /* ¾ - 190 */
1092   0x0008, /* ¿ - 191 */
1093   0x8003, /* À - 192 */
1094   0x8003, /* Á - 193 */
1095   0x8003, /* Â - 194 */
1096   0x8003, /* Ã - 195 */
1097   0x8003, /* Ä - 196 */
1098   0x8003, /* Å - 197 */
1099   0x8000, /* Æ - 198 */
1100   0x8003, /* Ç - 199 */
1101   0x8003, /* È - 200 */
1102   0x8003, /* É - 201 */
1103   0x8003, /* Ê - 202 */
1104   0x8003, /* Ë - 203 */
1105   0x8003, /* Ì - 204 */
1106   0x8003, /* Í - 205 */
1107   0x8003, /* Î - 206 */
1108   0x8003, /* Ï - 207 */
1109   0x8000, /* Ð - 208 */
1110   0x8003, /* Ñ - 209 */
1111   0x8003, /* Ò - 210 */
1112   0x8003, /* Ó - 211 */
1113   0x8003, /* Ô - 212 */
1114   0x8003, /* Õ - 213 */
1115   0x8003, /* Ö - 214 */
1116   0x0008, /* × - 215 */
1117   0x8003, /* Ø - 216 */
1118   0x8003, /* Ù - 217 */
1119   0x8003, /* Ú - 218 */
1120   0x8003, /* Û - 219 */
1121   0x8003, /* Ü - 220 */
1122   0x8003, /* Ý - 221 */
1123   0x8000, /* Þ - 222 */
1124   0x8000, /* ß - 223 */
1125   0x8003, /* à - 224 */
1126   0x8003, /* á - 225 */
1127   0x8003, /* â - 226 */
1128   0x8003, /* ã - 227 */
1129   0x8003, /* ä - 228 */
1130   0x8003, /* å - 229 */
1131   0x8000, /* æ - 230 */
1132   0x8003, /* ç - 231 */
1133   0x8003, /* è - 232 */
1134   0x8003, /* é - 233 */
1135   0x8003, /* ê - 234 */
1136   0x8003, /* ë - 235 */
1137   0x8003, /* ì - 236 */
1138   0x8003, /* í - 237 */
1139   0x8003, /* î - 238 */
1140   0x8003, /* ï - 239 */
1141   0x8000, /* ð - 240 */
1142   0x8003, /* ñ - 241 */
1143   0x8003, /* ò - 242 */
1144   0x8003, /* ó - 243 */
1145   0x8003, /* ô - 244 */
1146   0x8003, /* õ - 245 */
1147   0x8003, /* ö - 246 */
1148   0x0008, /* ÷ - 247 */
1149   0x8003, /* ø - 248 */
1150   0x8003, /* ù - 249 */
1151   0x8003, /* ú - 250 */
1152   0x8003, /* û - 251 */
1153   0x8003, /* ü - 252 */
1154   0x8003, /* ý - 253 */
1155   0x8000, /* þ - 254 */
1156   0x8003  /* ÿ - 255 */
1157 };
1158
1159 /******************************************************************************
1160  *              GetStringTypeA  [KERNEL32.396]
1161  */
1162 BOOL WINAPI GetStringTypeA(LCID locale,DWORD dwInfoType,LPCSTR src,
1163                                INT cchSrc,LPWORD chartype)
1164 {
1165         return GetStringTypeExA(locale,dwInfoType,src,cchSrc,chartype);
1166 }
1167
1168 /******************************************************************************
1169  *              GetStringTypeExA        [KERNEL32.397]
1170  *
1171  * FIXME: Ignores the locale.
1172  */
1173 BOOL WINAPI GetStringTypeExA(LCID locale,DWORD dwInfoType,LPCSTR src,
1174                                  INT cchSrc,LPWORD chartype)
1175 {
1176         int     i;
1177         
1178         if ((src==NULL) || (chartype==NULL) || (src==(LPSTR)chartype))
1179         {
1180           SetLastError(ERROR_INVALID_PARAMETER);
1181           return FALSE;
1182         }
1183
1184         if (cchSrc==-1)
1185           cchSrc=strlen(src)+1;
1186           
1187         switch (dwInfoType) {
1188         case CT_CTYPE1:
1189           for (i=0;i<cchSrc;i++) 
1190           {
1191             chartype[i] = 0;
1192             if (isdigit(src[i])) chartype[i]|=C1_DIGIT;
1193             if (isalpha(src[i])) chartype[i]|=C1_ALPHA;
1194             if (islower(src[i])) chartype[i]|=C1_LOWER;
1195             if (isupper(src[i])) chartype[i]|=C1_UPPER;
1196             if (isspace(src[i])) chartype[i]|=C1_SPACE;
1197             if (ispunct(src[i])) chartype[i]|=C1_PUNCT;
1198             if (iscntrl(src[i])) chartype[i]|=C1_CNTRL;
1199 /* FIXME: isblank() is a GNU extension */
1200 /*              if (isblank(src[i])) chartype[i]|=C1_BLANK; */
1201             if ((src[i] == ' ') || (src[i] == '\t')) chartype[i]|=C1_BLANK;
1202             /* C1_XDIGIT */
1203         }
1204         return TRUE;
1205
1206         case CT_CTYPE2:
1207           for (i=0;i<cchSrc;i++) 
1208           {
1209             chartype[i]=(WORD)CT_CType2_LUT[i];
1210           }
1211           return TRUE;
1212
1213         case CT_CTYPE3:
1214           for (i=0;i<cchSrc;i++) 
1215           {
1216             chartype[i]=OLE2NLS_CT_CType3_LUT[i];
1217           }
1218           return TRUE;
1219
1220         default:
1221           ERR("Unknown dwInfoType:%ld\n",dwInfoType);
1222           return FALSE;
1223         }
1224 }
1225
1226 /******************************************************************************
1227  *              GetStringTypeW  [KERNEL32.399]
1228  *
1229  * NOTES
1230  * Yes, this is missing LCID locale. MS fault.
1231  */
1232 BOOL WINAPI GetStringTypeW(DWORD dwInfoType,LPCWSTR src,INT cchSrc,
1233                                LPWORD chartype)
1234 {
1235         return GetStringTypeExW(0/*defaultlocale*/,dwInfoType,src,cchSrc,chartype);
1236 }
1237
1238 /******************************************************************************
1239  *              GetStringTypeExW        [KERNEL32.398]
1240  *
1241  * FIXME: unicode chars are assumed chars
1242  */
1243 BOOL WINAPI GetStringTypeExW(LCID locale,DWORD dwInfoType,LPCWSTR src,
1244                                  INT cchSrc,LPWORD chartype)
1245 {
1246         int     i;
1247         UINT    ch;
1248
1249         if (cchSrc==-1)
1250           cchSrc=lstrlenW(src)+1;
1251         
1252         switch (dwInfoType) {
1253         case CT_CTYPE2:
1254                 FIXME("CT_CTYPE2 not supported.\n");
1255                 return FALSE;
1256         case CT_CTYPE3:
1257                 FIXME("CT_CTYPE3 not supported.\n");
1258                 return FALSE;
1259         default:break;
1260         }
1261         for (i=0;i<cchSrc;i++) {
1262                 chartype[i] = 0;
1263                 ch = src[i];
1264                 if ( ch >= (UINT)0x100 ) continue;
1265                 if (isdigit(ch)) chartype[i]|=C1_DIGIT;
1266                 if (isalpha(ch)) chartype[i]|=C1_ALPHA;
1267                 if (islower(ch)) chartype[i]|=C1_LOWER;
1268                 if (isupper(ch)) chartype[i]|=C1_UPPER;
1269                 if (isspace(ch)) chartype[i]|=C1_SPACE;
1270                 if (ispunct(ch)) chartype[i]|=C1_PUNCT;
1271                 if (iscntrl(ch)) chartype[i]|=C1_CNTRL;
1272 /* FIXME: isblank() is a GNU extension */
1273 /*              if (isblank(ch)) chartype[i]|=C1_BLANK; */
1274                 if ((ch == ' ') || (ch == '\t')) chartype[i]|=C1_BLANK;
1275                 /* C1_XDIGIT */
1276         }
1277         return TRUE;
1278 }
1279
1280 /***********************************************************************
1281  *           VerLanguageNameA              [KERNEL32.709][VERSION.9]
1282  */
1283 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
1284 {
1285     if(!szLang)
1286         return 0;
1287
1288     return GetLocaleInfoA(MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize);
1289 }
1290
1291 /***********************************************************************
1292  *           VerLanguageNameW              [KERNEL32.710][VERSION.10]
1293  */
1294 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
1295 {
1296     if(!szLang)
1297         return 0;
1298
1299     return GetLocaleInfoW(MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize);
1300 }
1301
1302  
1303 static const unsigned char LCM_Unicode_LUT[] = {
1304   6      ,   3, /*   -   1 */  
1305   6      ,   4, /*   -   2 */  
1306   6      ,   5, /*   -   3 */  
1307   6      ,   6, /*   -   4 */  
1308   6      ,   7, /*   -   5 */  
1309   6      ,   8, /*   -   6 */  
1310   6      ,   9, /*   -   7 */  
1311   6      ,  10, /*   -   8 */  
1312   7      ,   5, /*   -   9 */  
1313   7      ,   6, /*   -  10 */  
1314   7      ,   7, /*   -  11 */  
1315   7      ,   8, /*   -  12 */  
1316   7      ,   9, /*   -  13 */  
1317   6      ,  11, /*   -  14 */  
1318   6      ,  12, /*   -  15 */  
1319   6      ,  13, /*   -  16 */  
1320   6      ,  14, /*   -  17 */  
1321   6      ,  15, /*   -  18 */  
1322   6      ,  16, /*   -  19 */  
1323   6      ,  17, /*   -  20 */  
1324   6      ,  18, /*   -  21 */  
1325   6      ,  19, /*   -  22 */  
1326   6      ,  20, /*   -  23 */  
1327   6      ,  21, /*   -  24 */  
1328   6      ,  22, /*   -  25 */  
1329   6      ,  23, /*   -  26 */  
1330   6      ,  24, /*   -  27 */  
1331   6      ,  25, /*   -  28 */  
1332   6      ,  26, /*   -  29 */  
1333   6      ,  27, /*   -  30 */  
1334   6      ,  28, /*   -  31 */  
1335   7      ,   2, /*   -  32 */
1336   7      ,  28, /* ! -  33 */
1337   7      ,  29, /* " -  34 */ /* " */
1338   7      ,  31, /* # -  35 */
1339   7      ,  33, /* $ -  36 */
1340   7      ,  35, /* % -  37 */
1341   7      ,  37, /* & -  38 */
1342   6      , 128, /* ' -  39 */
1343   7      ,  39, /* ( -  40 */
1344   7      ,  42, /* ) -  41 */
1345   7      ,  45, /* * -  42 */
1346   8      ,   3, /* + -  43 */
1347   7      ,  47, /* , -  44 */
1348   6      , 130, /* - -  45 */
1349   7      ,  51, /* . -  46 */
1350   7      ,  53, /* / -  47 */
1351  12      ,   3, /* 0 -  48 */
1352  12      ,  33, /* 1 -  49 */
1353  12      ,  51, /* 2 -  50 */
1354  12      ,  70, /* 3 -  51 */
1355  12      ,  88, /* 4 -  52 */
1356  12      , 106, /* 5 -  53 */
1357  12      , 125, /* 6 -  54 */
1358  12      , 144, /* 7 -  55 */
1359  12      , 162, /* 8 -  56 */
1360  12      , 180, /* 9 -  57 */
1361   7      ,  55, /* : -  58 */
1362   7      ,  58, /* ; -  59 */
1363   8      ,  14, /* < -  60 */
1364   8      ,  18, /* = -  61 */
1365   8      ,  20, /* > -  62 */
1366   7      ,  60, /* ? -  63 */
1367   7      ,  62, /* @ -  64 */
1368  14      ,   2, /* A -  65 */
1369  14      ,   9, /* B -  66 */
1370  14      ,  10, /* C -  67 */
1371  14      ,  26, /* D -  68 */
1372  14      ,  33, /* E -  69 */
1373  14      ,  35, /* F -  70 */
1374  14      ,  37, /* G -  71 */
1375  14      ,  44, /* H -  72 */
1376  14      ,  50, /* I -  73 */
1377  14      ,  53, /* J -  74 */
1378  14      ,  54, /* K -  75 */
1379  14      ,  72, /* L -  76 */
1380  14      ,  81, /* M -  77 */
1381  14      , 112, /* N -  78 */
1382  14      , 124, /* O -  79 */
1383  14      , 126, /* P -  80 */
1384  14      , 137, /* Q -  81 */
1385  14      , 138, /* R -  82 */
1386  14      , 145, /* S -  83 */
1387  14      , 153, /* T -  84 */
1388  14      , 159, /* U -  85 */
1389  14      , 162, /* V -  86 */
1390  14      , 164, /* W -  87 */
1391  14      , 166, /* X -  88 */
1392  14      , 167, /* Y -  89 */
1393  14      , 169, /* Z -  90 */
1394   7      ,  63, /* [ -  91 */
1395   7      ,  65, /* \ -  92 */
1396   7      ,  66, /* ] -  93 */
1397   7      ,  67, /* ^ -  94 */
1398   7      ,  68, /* _ -  95 */
1399   7      ,  72, /* ` -  96 */
1400  14      ,   2, /* a -  97 */
1401  14      ,   9, /* b -  98 */
1402  14      ,  10, /* c -  99 */
1403  14      ,  26, /* d - 100 */
1404  14      ,  33, /* e - 101 */
1405  14      ,  35, /* f - 102 */
1406  14      ,  37, /* g - 103 */
1407  14      ,  44, /* h - 104 */
1408  14      ,  50, /* i - 105 */
1409  14      ,  53, /* j - 106 */
1410  14      ,  54, /* k - 107 */
1411  14      ,  72, /* l - 108 */
1412  14      ,  81, /* m - 109 */
1413  14      , 112, /* n - 110 */
1414  14      , 124, /* o - 111 */
1415  14      , 126, /* p - 112 */
1416  14      , 137, /* q - 113 */
1417  14      , 138, /* r - 114 */
1418  14      , 145, /* s - 115 */
1419  14      , 153, /* t - 116 */
1420  14      , 159, /* u - 117 */
1421  14      , 162, /* v - 118 */
1422  14      , 164, /* w - 119 */
1423  14      , 166, /* x - 120 */
1424  14      , 167, /* y - 121 */
1425  14      , 169, /* z - 122 */
1426   7      ,  74, /* { - 123 */
1427   7      ,  76, /* | - 124 */
1428   7      ,  78, /* } - 125 */
1429   7      ,  80, /* ~ - 126 */
1430   6      ,  29, /* \7f - 127 */
1431   6      ,  30, /* \80 - 128 */
1432   6      ,  31, /* \81 - 129 */
1433   7      , 123, /* \82 - 130 */
1434  14      ,  35, /* \83 - 131 */
1435   7      , 127, /* \84 - 132 */
1436  10      ,  21, /* \85 - 133 */
1437  10      ,  15, /* \86 - 134 */
1438  10      ,  16, /* \87 - 135 */
1439   7      ,  67, /* \88 - 136 */
1440  10      ,  22, /* \89 - 137 */
1441  14      , 145, /* \8a - 138 */
1442   7      , 136, /* \8b - 139 */
1443  14 + 16 , 124, /* \8c - 140 */
1444   6      ,  43, /* \8d - 141 */
1445   6      ,  44, /* \8e - 142 */
1446   6      ,  45, /* \8f - 143 */
1447   6      ,  46, /* \90 - 144 */
1448   7      , 121, /* \91 - 145 */
1449   7      , 122, /* \92 - 146 */
1450   7      , 125, /* \93 - 147 */
1451   7      , 126, /* \94 - 148 */
1452  10      ,  17, /* \95 - 149 */
1453   6      , 137, /* \96 - 150 */
1454   6      , 139, /* \97 - 151 */
1455   7      ,  93, /* \98 - 152 */
1456  14      , 156, /* \99 - 153 */
1457  14      , 145, /* \9a - 154 */
1458   7      , 137, /* \9b - 155 */
1459  14 + 16 , 124, /* \9c - 156 */
1460   6      ,  59, /* \9d - 157 */
1461   6      ,  60, /* \9e - 158 */
1462  14      , 167, /* \9f - 159 */
1463   7      ,   4, /*   - 160 */
1464   7      ,  81, /* ¡ - 161 */
1465  10      ,   2, /* ¢ - 162 */
1466  10      ,   3, /* £ - 163 */
1467  10      ,   4, /* ¤ - 164 */
1468  10      ,   5, /* ¥ - 165 */
1469   7      ,  82, /* ¦ - 166 */
1470  10      ,   6, /* § - 167 */
1471   7      ,  83, /* ¨ - 168 */
1472  10      ,   7, /* © - 169 */
1473  14      ,   2, /* ª - 170 */
1474   8      ,  24, /* « - 171 */
1475  10      ,   8, /* ¬ - 172 */
1476   6      , 131, /* ­ - 173 */
1477  10      ,   9, /* ® - 174 */
1478   7      ,  84, /* ¯ - 175 */
1479  10      ,  10, /* ° - 176 */
1480   8      ,  23, /* ± - 177 */
1481  12      ,  51, /* ² - 178 */
1482  12      ,  70, /* ³ - 179 */
1483   7      ,  85, /* ´ - 180 */
1484  10      ,  11, /* µ - 181 */
1485  10      ,  12, /* ¶ - 182 */
1486  10      ,  13, /* · - 183 */
1487   7      ,  86, /* ¸ - 184 */
1488  12      ,  33, /* ¹ - 185 */
1489  14      , 124, /* º - 186 */
1490   8      ,  26, /* » - 187 */
1491  12      ,  21, /* ¼ - 188 */
1492  12      ,  25, /* ½ - 189 */
1493  12      ,  29, /* ¾ - 190 */
1494   7      ,  87, /* ¿ - 191 */
1495  14      ,   2, /* À - 192 */
1496  14      ,   2, /* Á - 193 */
1497  14      ,   2, /* Â - 194 */
1498  14      ,   2, /* Ã - 195 */
1499  14      ,   2, /* Ä - 196 */
1500  14      ,   2, /* Å - 197 */
1501  14 + 16 ,   2, /* Æ - 198 */
1502  14      ,  10, /* Ç - 199 */
1503  14      ,  33, /* È - 200 */
1504  14      ,  33, /* É - 201 */
1505  14      ,  33, /* Ê - 202 */
1506  14      ,  33, /* Ë - 203 */
1507  14      ,  50, /* Ì - 204 */
1508  14      ,  50, /* Í - 205 */
1509  14      ,  50, /* Î - 206 */
1510  14      ,  50, /* Ï - 207 */
1511  14      ,  26, /* Ð - 208 */
1512  14      , 112, /* Ñ - 209 */
1513  14      , 124, /* Ò - 210 */
1514  14      , 124, /* Ó - 211 */
1515  14      , 124, /* Ô - 212 */
1516  14      , 124, /* Õ - 213 */
1517  14      , 124, /* Ö - 214 */
1518   8      ,  28, /* × - 215 */
1519  14      , 124, /* Ø - 216 */
1520  14      , 159, /* Ù - 217 */
1521  14      , 159, /* Ú - 218 */
1522  14      , 159, /* Û - 219 */
1523  14      , 159, /* Ü - 220 */
1524  14      , 167, /* Ý - 221 */
1525  14 + 32 , 153, /* Þ - 222 */
1526  14 + 48 , 145, /* ß - 223 */
1527  14      ,   2, /* à - 224 */
1528  14      ,   2, /* á - 225 */
1529  14      ,   2, /* â - 226 */
1530  14      ,   2, /* ã - 227 */
1531  14      ,   2, /* ä - 228 */
1532  14      ,   2, /* å - 229 */
1533  14 + 16 ,   2, /* æ - 230 */
1534  14      ,  10, /* ç - 231 */
1535  14      ,  33, /* è - 232 */
1536  14      ,  33, /* é - 233 */
1537  14      ,  33, /* ê - 234 */
1538  14      ,  33, /* ë - 235 */
1539  14      ,  50, /* ì - 236 */
1540  14      ,  50, /* í - 237 */
1541  14      ,  50, /* î - 238 */
1542  14      ,  50, /* ï - 239 */
1543  14      ,  26, /* ð - 240 */
1544  14      , 112, /* ñ - 241 */
1545  14      , 124, /* ò - 242 */
1546  14      , 124, /* ó - 243 */
1547  14      , 124, /* ô - 244 */
1548  14      , 124, /* õ - 245 */
1549  14      , 124, /* ö - 246 */
1550   8      ,  29, /* ÷ - 247 */
1551  14      , 124, /* ø - 248 */
1552  14      , 159, /* ù - 249 */
1553  14      , 159, /* ú - 250 */
1554  14      , 159, /* û - 251 */
1555  14      , 159, /* ü - 252 */
1556  14      , 167, /* ý - 253 */
1557  14 + 32 , 153, /* þ - 254 */
1558  14      , 167  /* ÿ - 255 */ };
1559
1560 static const unsigned char LCM_Unicode_LUT_2[] = { 33, 44, 145 };
1561
1562 #define LCM_Diacritic_Start 131
1563
1564 static const unsigned char LCM_Diacritic_LUT[] = { 
1565 123,  /* \83 - 131 */
1566   2,  /* \84 - 132 */
1567   2,  /* \85 - 133 */
1568   2,  /* \86 - 134 */
1569   2,  /* \87 - 135 */
1570   3,  /* \88 - 136 */
1571   2,  /* \89 - 137 */
1572  20,  /* \8a - 138 */
1573   2,  /* \8b - 139 */
1574   2,  /* \8c - 140 */
1575   2,  /* \8d - 141 */
1576   2,  /* \8e - 142 */
1577   2,  /* \8f - 143 */
1578   2,  /* \90 - 144 */
1579   2,  /* \91 - 145 */
1580   2,  /* \92 - 146 */
1581   2,  /* \93 - 147 */
1582   2,  /* \94 - 148 */
1583   2,  /* \95 - 149 */
1584   2,  /* \96 - 150 */
1585   2,  /* \97 - 151 */
1586   2,  /* \98 - 152 */
1587   2,  /* \99 - 153 */
1588  20,  /* \9a - 154 */
1589   2,  /* \9b - 155 */
1590   2,  /* \9c - 156 */
1591   2,  /* \9d - 157 */
1592   2,  /* \9e - 158 */
1593  19,  /* \9f - 159 */
1594   2,  /*   - 160 */
1595   2,  /* ¡ - 161 */
1596   2,  /* ¢ - 162 */
1597   2,  /* £ - 163 */
1598   2,  /* ¤ - 164 */
1599   2,  /* ¥ - 165 */
1600   2,  /* ¦ - 166 */
1601   2,  /* § - 167 */
1602   2,  /* ¨ - 168 */
1603   2,  /* © - 169 */
1604   3,  /* ª - 170 */
1605   2,  /* « - 171 */
1606   2,  /* ¬ - 172 */
1607   2,  /* ­ - 173 */
1608   2,  /* ® - 174 */
1609   2,  /* ¯ - 175 */
1610   2,  /* ° - 176 */
1611   2,  /* ± - 177 */
1612   2,  /* ² - 178 */
1613   2,  /* ³ - 179 */
1614   2,  /* ´ - 180 */
1615   2,  /* µ - 181 */
1616   2,  /* ¶ - 182 */
1617   2,  /* · - 183 */
1618   2,  /* ¸ - 184 */
1619   2,  /* ¹ - 185 */
1620   3,  /* º - 186 */
1621   2,  /* » - 187 */
1622   2,  /* ¼ - 188 */
1623   2,  /* ½ - 189 */
1624   2,  /* ¾ - 190 */
1625   2,  /* ¿ - 191 */
1626  15,  /* À - 192 */
1627  14,  /* Á - 193 */
1628  18,  /* Â - 194 */
1629  25,  /* Ã - 195 */
1630  19,  /* Ä - 196 */
1631  26,  /* Å - 197 */
1632   2,  /* Æ - 198 */
1633  28,  /* Ç - 199 */
1634  15,  /* È - 200 */
1635  14,  /* É - 201 */
1636  18,  /* Ê - 202 */
1637  19,  /* Ë - 203 */
1638  15,  /* Ì - 204 */
1639  14,  /* Í - 205 */
1640  18,  /* Î - 206 */
1641  19,  /* Ï - 207 */
1642 104,  /* Ð - 208 */
1643  25,  /* Ñ - 209 */
1644  15,  /* Ò - 210 */
1645  14,  /* Ó - 211 */
1646  18,  /* Ô - 212 */
1647  25,  /* Õ - 213 */
1648  19,  /* Ö - 214 */
1649   2,  /* × - 215 */
1650  33,  /* Ø - 216 */
1651  15,  /* Ù - 217 */
1652  14,  /* Ú - 218 */
1653  18,  /* Û - 219 */
1654  19,  /* Ü - 220 */
1655  14,  /* Ý - 221 */
1656   2,  /* Þ - 222 */
1657   2,  /* ß - 223 */
1658  15,  /* à - 224 */
1659  14,  /* á - 225 */
1660  18,  /* â - 226 */
1661  25,  /* ã - 227 */
1662  19,  /* ä - 228 */
1663  26,  /* å - 229 */
1664   2,  /* æ - 230 */
1665  28,  /* ç - 231 */
1666  15,  /* è - 232 */
1667  14,  /* é - 233 */
1668  18,  /* ê - 234 */
1669  19,  /* ë - 235 */
1670  15,  /* ì - 236 */
1671  14,  /* í - 237 */
1672  18,  /* î - 238 */
1673  19,  /* ï - 239 */
1674 104,  /* ð - 240 */
1675  25,  /* ñ - 241 */
1676  15,  /* ò - 242 */
1677  14,  /* ó - 243 */
1678  18,  /* ô - 244 */
1679  25,  /* õ - 245 */
1680  19,  /* ö - 246 */
1681   2,  /* ÷ - 247 */
1682  33,  /* ø - 248 */
1683  15,  /* ù - 249 */
1684  14,  /* ú - 250 */
1685  18,  /* û - 251 */
1686  19,  /* ü - 252 */
1687  14,  /* ý - 253 */
1688   2,  /* þ - 254 */
1689  19,  /* ÿ - 255 */
1690 } ;
1691
1692 /******************************************************************************
1693  * OLE2NLS_isPunctuation [INTERNAL]
1694  */
1695 static int OLE2NLS_isPunctuation(unsigned char c) 
1696 {
1697   /* "punctuation character" in this context is a character which is 
1698      considered "less important" during word sort comparison.
1699      See LCMapString implementation for the precise definition 
1700      of "less important". */
1701
1702   return (LCM_Unicode_LUT[-2+2*c]==6);
1703 }
1704
1705 /******************************************************************************
1706  * OLE2NLS_isNonSpacing [INTERNAL]
1707  */
1708 static int OLE2NLS_isNonSpacing(unsigned char c) 
1709 {
1710   /* This function is used by LCMapStringA.  Characters 
1711      for which it returns true are ignored when mapping a
1712      string with NORM_IGNORENONSPACE */
1713   return ((c==136) || (c==170) || (c==186));
1714 }
1715
1716 /******************************************************************************
1717  * OLE2NLS_isSymbol [INTERNAL]
1718  */
1719 static int OLE2NLS_isSymbol(unsigned char c) 
1720 {
1721   /* This function is used by LCMapStringA.  Characters 
1722      for which it returns true are ignored when mapping a
1723      string with NORM_IGNORESYMBOLS */
1724   return ( (c!=0) && !IsCharAlphaNumericA(c) );
1725 }
1726
1727 /******************************************************************************
1728  *              identity        [Internal]
1729  */
1730 static int identity(int c)
1731 {
1732   return c;
1733 }
1734
1735 /*************************************************************************
1736  *              LCMapStringA                [KERNEL32.492]
1737  *
1738  * Convert a string, or generate a sort key from it.
1739  *
1740  * If (mapflags & LCMAP_SORTKEY), the function will generate
1741  * a sort key for the source string.  Else, it will convert it
1742  * accordingly to the flags LCMAP_UPPERCASE, LCMAP_LOWERCASE,...
1743  *
1744  * RETURNS
1745  *    Error : 0.
1746  *    Success : length of the result string.
1747  *
1748  * NOTES
1749  *    If called with scrlen = -1, the function will compute the length
1750  *      of the 0-terminated string strsrc by itself.      
1751  * 
1752  *    If called with dstlen = 0, returns the buffer length that 
1753  *      would be required.
1754  *
1755  *    NORM_IGNOREWIDTH means to compare ASCII and wide characters
1756  *    as if they are equal.  
1757  *    In the only code page implemented so far, there may not be
1758  *    wide characters in strings passed to LCMapStringA,
1759  *    so there is nothing to be done for this flag.
1760  */
1761 INT WINAPI LCMapStringA(
1762         LCID lcid /* locale identifier created with MAKELCID; 
1763                      LOCALE_SYSTEM_DEFAULT and LOCALE_USER_DEFAULT are 
1764                      predefined values. */,
1765         DWORD mapflags /* flags */,
1766         LPCSTR srcstr  /* source buffer */,
1767         INT srclen   /* source length */,
1768         LPSTR dststr   /* destination buffer */,
1769         INT dstlen   /* destination buffer length */) 
1770 {
1771   int i;
1772
1773   TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1774         lcid,mapflags,srcstr,srclen,dststr,dstlen);
1775
1776   if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
1777   {
1778     ERR("(src=%s,dest=%s): Invalid NULL string\n", srcstr, dststr);
1779     SetLastError(ERROR_INVALID_PARAMETER);
1780     return 0;
1781   }
1782   if (srclen == -1) 
1783     srclen = strlen(srcstr) + 1 ;    /* (include final '\0') */
1784
1785 #define LCMAPSTRINGA_SUPPORTED_FLAGS (LCMAP_UPPERCASE     | \
1786                                         LCMAP_LOWERCASE     | \
1787                                         LCMAP_SORTKEY       | \
1788                                         NORM_IGNORECASE     | \
1789                                         NORM_IGNORENONSPACE | \
1790                                         SORT_STRINGSORT     | \
1791                                         NORM_IGNOREWIDTH    | \
1792                                         NORM_IGNOREKANATYPE)
1793   /* FIXME: as long as we don't support Kanakana nor Hirigana 
1794    * characters, we can support NORM_IGNOREKANATYPE
1795    */
1796   if (mapflags & ~LCMAPSTRINGA_SUPPORTED_FLAGS)
1797   {
1798     FIXME("(0x%04lx,0x%08lx,%p,%d,%p,%d): "
1799           "unimplemented flags: 0x%08lx\n",
1800           lcid,
1801           mapflags,
1802           srcstr,
1803           srclen,
1804           dststr,
1805           dstlen,
1806           mapflags & ~LCMAPSTRINGA_SUPPORTED_FLAGS
1807      );
1808   }
1809
1810   if ( !(mapflags & LCMAP_SORTKEY) )
1811   {
1812     int i,j;
1813     int (*f)(int) = identity; 
1814     int flag_ignorenonspace = mapflags & NORM_IGNORENONSPACE;
1815     int flag_ignoresymbols = mapflags & NORM_IGNORESYMBOLS;
1816
1817     if (flag_ignorenonspace || flag_ignoresymbols)
1818     {
1819       /* For some values of mapflags, the length of the resulting 
1820          string is not known at this point.  Windows does map the string
1821          and does not SetLastError ERROR_INSUFFICIENT_BUFFER in
1822          these cases. */
1823       if (dstlen==0)
1824       {
1825         /* Compute required length */
1826         for (i=j=0; i < srclen; i++)
1827         {
1828           if ( !(flag_ignorenonspace && OLE2NLS_isNonSpacing(srcstr[i]))
1829                && !(flag_ignoresymbols && OLE2NLS_isSymbol(srcstr[i])) )
1830             j++;
1831         }
1832         return j;
1833       }
1834     }
1835     else
1836     {
1837       if (dstlen==0)
1838         return srclen;  
1839       if (dstlen<srclen) 
1840            {
1841              SetLastError(ERROR_INSUFFICIENT_BUFFER);
1842              return 0;
1843            }
1844     }
1845     if (mapflags & LCMAP_UPPERCASE)
1846       f = toupper;
1847     else if (mapflags & LCMAP_LOWERCASE)
1848       f = tolower;
1849     /* FIXME: NORM_IGNORENONSPACE requires another conversion */
1850     for (i=j=0; (i<srclen) && (j<dstlen) ; i++)
1851     {
1852       if ( !(flag_ignorenonspace && OLE2NLS_isNonSpacing(srcstr[i]))
1853            && !(flag_ignoresymbols && OLE2NLS_isSymbol(srcstr[i])) )
1854       {
1855         dststr[j] = (CHAR) f(srcstr[i]);
1856         j++;
1857       }
1858     }
1859     return j;
1860   }
1861
1862   /* FIXME: This function completely ignores the "lcid" parameter. */
1863   /* else ... (mapflags & LCMAP_SORTKEY)  */
1864   {
1865     int unicode_len=0;
1866     int case_len=0;
1867     int diacritic_len=0;
1868     int delayed_punctuation_len=0;
1869     char *case_component;
1870     char *diacritic_component;
1871     char *delayed_punctuation_component;
1872     int room,count;
1873     int flag_stringsort = mapflags & SORT_STRINGSORT;
1874
1875     /* compute how much room we will need */
1876     for (i=0;i<srclen;i++)
1877     {
1878       int ofs;
1879       unsigned char source_char = srcstr[i];
1880       if (source_char!='\0') 
1881       {
1882         if (flag_stringsort || !OLE2NLS_isPunctuation(source_char))
1883         {
1884           unicode_len++;
1885           if ( LCM_Unicode_LUT[-2+2*source_char] & ~15 )
1886             unicode_len++;             /* double letter */
1887         }
1888         else
1889         {
1890           delayed_punctuation_len++;
1891         }         
1892       }
1893           
1894       if (isupper(source_char))
1895         case_len=unicode_len; 
1896
1897       ofs = source_char - LCM_Diacritic_Start;
1898       if ((ofs>=0) && (LCM_Diacritic_LUT[ofs]!=2))
1899         diacritic_len=unicode_len;
1900     }
1901
1902     if (mapflags & NORM_IGNORECASE)
1903       case_len=0;                   
1904     if (mapflags & NORM_IGNORENONSPACE)
1905       diacritic_len=0;
1906
1907     room =  2 * unicode_len              /* "unicode" component */
1908       +     diacritic_len                /* "diacritic" component */
1909       +     case_len                     /* "case" component */
1910       +     4 * delayed_punctuation_len  /* punctuation in word sort mode */
1911       +     4                            /* four '\1' separators */
1912       +     1  ;                         /* terminal '\0' */
1913     if (dstlen==0)
1914       return room;      
1915     else if (dstlen<room)
1916     {
1917       SetLastError(ERROR_INSUFFICIENT_BUFFER);
1918       return 0;
1919     }
1920 #if 0
1921     /*FIXME the Pointercheck should not be nessesary */
1922     if (IsBadWritePtr (dststr,room))
1923     { ERR("bad destination buffer (dststr) : %p,%d\n",dststr,dstlen);
1924       SetLastError(ERROR_INSUFFICIENT_BUFFER);
1925       return 0;
1926     }
1927 #endif
1928     /* locate each component, write separators */
1929     diacritic_component = dststr + 2*unicode_len ;
1930     *diacritic_component++ = '\1'; 
1931     case_component = diacritic_component + diacritic_len ;
1932     *case_component++ = '\1'; 
1933     delayed_punctuation_component = case_component + case_len ;
1934     *delayed_punctuation_component++ = '\1';
1935     *delayed_punctuation_component++ = '\1';
1936
1937     /* read source string char by char, write 
1938        corresponding weight in each component. */
1939     for (i=0,count=0;i<srclen;i++)
1940     {
1941       unsigned char source_char=srcstr[i];
1942       if (source_char!='\0') 
1943       {
1944         int type,longcode;
1945         type = LCM_Unicode_LUT[-2+2*source_char];
1946         longcode = type >> 4;
1947         type &= 15;
1948         if (!flag_stringsort && OLE2NLS_isPunctuation(source_char)) 
1949         {
1950           UINT16 encrypted_location = (1<<15) + 7 + 4*count;
1951           *delayed_punctuation_component++ = (unsigned char) (encrypted_location>>8);
1952           *delayed_punctuation_component++ = (unsigned char) (encrypted_location&255);
1953                      /* big-endian is used here because it lets string comparison be
1954                         compatible with numerical comparison */
1955
1956           *delayed_punctuation_component++ = type;
1957           *delayed_punctuation_component++ = LCM_Unicode_LUT[-1+2*source_char];  
1958                      /* assumption : a punctuation character is never a 
1959                         double or accented letter */
1960         }
1961         else
1962         {
1963           dststr[2*count] = type;
1964           dststr[2*count+1] = LCM_Unicode_LUT[-1+2*source_char];  
1965           if (longcode)
1966           {
1967             if (count<case_len)
1968               case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
1969             if (count<diacritic_len)
1970               diacritic_component[count] = 2; /* assumption: a double letter
1971                                                  is never accented */
1972             count++;
1973             
1974             dststr[2*count] = type;
1975             dststr[2*count+1] = *(LCM_Unicode_LUT_2 - 1 + longcode); 
1976             /* 16 in the first column of LCM_Unicode_LUT  -->  longcode = 1 
1977                32 in the first column of LCM_Unicode_LUT  -->  longcode = 2 
1978                48 in the first column of LCM_Unicode_LUT  -->  longcode = 3 */
1979           }
1980
1981           if (count<case_len)
1982             case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
1983           if (count<diacritic_len)
1984           {
1985             int ofs = source_char - LCM_Diacritic_Start;
1986             diacritic_component[count] = (ofs>=0 ? LCM_Diacritic_LUT[ofs] : 2);
1987           }
1988           count++;
1989         }
1990       }
1991     }
1992     dststr[room-1] = '\0';
1993     return room;
1994   }
1995 }
1996                      
1997 /*************************************************************************
1998  *              LCMapStringW                [KERNEL32.493]
1999  *
2000  * Convert a string, or generate a sort key from it.
2001  *
2002  * NOTE
2003  *
2004  * See LCMapStringA for documentation
2005  */
2006 INT WINAPI LCMapStringW(
2007         LCID lcid,DWORD mapflags,LPCWSTR srcstr,INT srclen,LPWSTR dststr,
2008         INT dstlen)
2009 {
2010   int i;
2011  
2012   TRACE("(0x%04lx,0x%08lx,%p,%d,%p,%d)\n",
2013                  lcid, mapflags, srcstr, srclen, dststr, dstlen);
2014   
2015   if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
2016   {
2017     ERR("(src=%p,dst=%p): Invalid NULL string\n", srcstr, dststr);
2018     SetLastError(ERROR_INVALID_PARAMETER);
2019     return 0;
2020   }
2021   if (srclen==-1) 
2022     srclen = lstrlenW(srcstr)+1;
2023
2024   /* FIXME: Both this function and it's companion LCMapStringA()
2025    * completely ignore the "lcid" parameter.  In place of the "lcid"
2026    * parameter the application must set the "LC_COLLATE" or "LC_ALL"
2027    * environment variable prior to invoking this function.  */
2028   if (mapflags & LCMAP_SORTKEY) 
2029   {
2030       /* Possible values of LC_COLLATE. */
2031       char *lc_collate_default = 0; /* value prior to this function */
2032       char *lc_collate_env = 0;     /* value retrieved from the environment */
2033
2034       /* General purpose index into strings of any type. */
2035       int str_idx = 0;
2036
2037       /* Lengths of various strings where the length is measured in
2038        * wide characters for wide character strings and in bytes for
2039        * native strings.  The lengths include the NULL terminator.  */
2040       size_t returned_len    = 0;
2041       size_t src_native_len  = 0;
2042       size_t dst_native_len  = 0;
2043       size_t dststr_libc_len = 0;
2044
2045       /* Native (character set determined by locale) versions of the
2046        * strings source and destination strings.  */
2047       LPSTR src_native = 0;
2048       LPSTR dst_native = 0;
2049
2050       /* Version of the source and destination strings using the
2051        * "wchar_t" Unicode data type needed by various libc functions.  */
2052       wchar_t *srcstr_libc = 0;
2053       wchar_t *dststr_libc = 0;
2054
2055       if(!(srcstr_libc = (wchar_t *)HeapAlloc(GetProcessHeap(), 0,
2056                                        srclen * sizeof(wchar_t))))
2057       {
2058           ERR("Unable to allocate %d bytes for srcstr_libc\n",
2059               srclen * sizeof(wchar_t));
2060           SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2061           return 0;
2062       }
2063
2064       /* Convert source string to a libc Unicode string. */
2065       for(str_idx = 0; str_idx < srclen; str_idx++)
2066       {
2067           srcstr_libc[str_idx] = srcstr[str_idx];
2068       }
2069
2070       /* src_native should contain at most 3 bytes for each
2071        * multibyte characters in the original srcstr string.  */
2072       src_native_len = 3 * srclen;
2073       if(!(src_native = (LPSTR)HeapAlloc(GetProcessHeap(), 0,
2074                                           src_native_len)))
2075       {
2076           ERR("Unable to allocate %d bytes for src_native\n", src_native_len);
2077           SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2078           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2079           return 0;
2080       }
2081
2082       /* FIXME: Prior to to setting the LC_COLLATE locale category the
2083        * current value is backed up so it can be restored after the
2084        * last LC_COLLATE sensitive function returns.
2085        * 
2086        * Even though the locale is adjusted for a minimum amount of
2087        * time a race condition exists where other threads may be
2088        * affected if they invoke LC_COLLATE sensitive functions.  One
2089        * possible solution is to wrap all LC_COLLATE sensitive Wine
2090        * functions, like LCMapStringW(), in a mutex.
2091        *
2092        * Another enhancement to the following would be to set the
2093        * LC_COLLATE locale category as a function of the "lcid"
2094        * parameter instead of the "LC_COLLATE" environment variable. */
2095       if(!(lc_collate_default = setlocale(LC_COLLATE, NULL)))
2096       {
2097           ERR("Unable to query the LC_COLLATE catagory\n");
2098           SetLastError(ERROR_INVALID_PARAMETER);
2099           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2100           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2101           return 0;
2102       }
2103
2104       if(!(lc_collate_env = setlocale(LC_COLLATE, "")))
2105       {
2106           ERR("Unable to inherit the LC_COLLATE locale category from the "
2107               "environment.  The \"LC_COLLATE\" environment variable is "
2108               "\"%s\".\n", getenv("LC_COLLATE") ?
2109               getenv("LC_COLLATE") : "<unset>");
2110           SetLastError(ERROR_INVALID_PARAMETER);
2111           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2112           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2113           return 0;
2114       }
2115
2116       TRACE("lc_collate_default = %s\n", lc_collate_default);
2117       TRACE("lc_collate_env = %s\n", lc_collate_env);
2118
2119       /* Convert the libc Unicode string to a native multibyte character
2120        * string. */
2121       returned_len = wcstombs(src_native, srcstr_libc, src_native_len) + 1;
2122       if(returned_len == 0)
2123       {
2124           LPSTR srcstr_ascii = (LPSTR)HEAP_strdupWtoA(GetProcessHeap(),
2125                                            0, srcstr);
2126           ERR("wcstombs failed.  The string specified (%s) may contains an "
2127               "invalid character.\n", srcstr_ascii);
2128           SetLastError(ERROR_INVALID_PARAMETER);
2129           if(srcstr_ascii) HeapFree(GetProcessHeap(), 0, srcstr_ascii);
2130           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2131           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2132           setlocale(LC_COLLATE, lc_collate_default);
2133           return 0;
2134       }
2135       else if(returned_len > src_native_len)
2136       {
2137           src_native[src_native_len - 1] = 0;
2138           ERR("wcstombs returned a string (%s) that was longer (%d bytes) " 
2139               "than expected (%d bytes).\n", src_native, returned_len,
2140               dst_native_len);
2141
2142           /* Since this is an internal error I'm not sure what the correct
2143            * error code is.  */
2144           SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2145
2146           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2147           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2148           setlocale(LC_COLLATE, lc_collate_default);
2149           return 0;
2150       }
2151       src_native_len = returned_len;
2152
2153       TRACE("src_native = %s  src_native_len = %d\n",
2154              src_native, src_native_len);
2155
2156       /* dst_native seems to contain at most 4 bytes for each byte in
2157        * the original src_native string.  Change if need be since this
2158        * isn't documented by the strxfrm() man page. */
2159       dst_native_len = 4 * src_native_len;
2160       if(!(dst_native = (LPSTR)HeapAlloc(GetProcessHeap(), 0, dst_native_len)))
2161       {
2162           ERR("Unable to allocate %d bytes for dst_native\n", dst_native_len);
2163           SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2164           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2165           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2166           setlocale(LC_COLLATE, lc_collate_default);
2167           return 0;
2168       }
2169
2170       /* The actual translation is done by the following call to
2171        * strxfrm().  The surrounding code could have been simplified
2172        * by calling wcsxfrm() instead except that wcsxfrm() is not
2173        * available on older Linux systems (RedHat 4.1 with
2174        * libc-5.3.12-17).
2175        *
2176        * Also, it is possible that the translation could be done by
2177        * various tables as it is done in LCMapStringA().  However, I'm
2178        * not sure what those tables are. */
2179       returned_len = strxfrm(dst_native, src_native, dst_native_len) + 1;
2180       
2181       if(returned_len > dst_native_len)
2182       {
2183           dst_native[dst_native_len - 1] = 0;
2184           ERR("strxfrm returned a string (%s) that was longer (%d bytes) " 
2185               "than expected (%d bytes).\n", dst_native, returned_len,
2186               dst_native_len);
2187
2188           /* Since this is an internal error I'm not sure what the correct
2189            * error code is.  */
2190           SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2191
2192           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2193           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2194           if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
2195           setlocale(LC_COLLATE, lc_collate_default);
2196           return 0;
2197       }
2198       dst_native_len = returned_len;
2199       
2200       TRACE("dst_native = %s  dst_native_len = %d\n",
2201              dst_native, dst_native_len);
2202
2203       dststr_libc_len = dst_native_len;
2204       if(!(dststr_libc = (wchar_t *)HeapAlloc(GetProcessHeap(), 0,
2205                                        dststr_libc_len * sizeof(wchar_t))))
2206       {
2207           ERR("Unable to allocate %d bytes for dststr_libc\n",
2208               dststr_libc_len * sizeof(wchar_t));
2209           SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2210           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2211           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2212           if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
2213           setlocale(LC_COLLATE, lc_collate_default);
2214           return 0;
2215       }
2216
2217       /* Convert the native multibyte string to a libc Unicode string. */
2218       returned_len = mbstowcs(dststr_libc, dst_native, dst_native_len) + 1;
2219
2220       /* Restore LC_COLLATE now that the last LC_COLLATE sensitive
2221        * function has returned. */
2222       setlocale(LC_COLLATE, lc_collate_default); 
2223
2224       if(returned_len == 0)
2225       {
2226           ERR("mbstowcs failed.  The native version of the translated string "
2227               "(%s) may contain an invalid character.\n", dst_native);
2228           SetLastError(ERROR_INVALID_PARAMETER);
2229           if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2230           if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2231           if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
2232           if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
2233           return 0;
2234       }
2235       if(dstlen)
2236       {
2237           if(returned_len > dstlen)
2238           {
2239               ERR("mbstowcs returned a string that was longer (%d chars) " 
2240                   "than the buffer provided (%d chars).\n", returned_len,
2241                   dstlen);
2242               SetLastError(ERROR_INSUFFICIENT_BUFFER);
2243               if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2244               if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2245               if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
2246               if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
2247               return 0;          
2248           }
2249           dstlen = returned_len;
2250
2251           /* Convert a libc Unicode string to the destination string. */
2252           for(str_idx = 0; str_idx < dstlen; str_idx++)
2253           {
2254               dststr[str_idx] = dststr_libc[str_idx];
2255           }
2256           TRACE("1st 4 int sized chunks of dststr = %x %x %x %x\n",
2257                          *(((int *)dststr) + 0),
2258                          *(((int *)dststr) + 1),
2259                          *(((int *)dststr) + 2),
2260                          *(((int *)dststr) + 3));
2261       }
2262       else
2263       {
2264           dstlen = returned_len;
2265       }
2266       TRACE("dstlen (return) = %d\n", dstlen);
2267       if(srcstr_libc) HeapFree(GetProcessHeap(), 0, srcstr_libc);
2268       if(src_native) HeapFree(GetProcessHeap(), 0, src_native);
2269       if(dst_native) HeapFree(GetProcessHeap(), 0, dst_native);
2270       if(dststr_libc) HeapFree(GetProcessHeap(), 0, dststr_libc);
2271       return dstlen;
2272   }
2273   else
2274   {
2275     int (*f)(int)=identity; 
2276
2277     if (dstlen==0)
2278         return srclen;  
2279     if (dstlen<srclen) 
2280     {
2281         SetLastError(ERROR_INSUFFICIENT_BUFFER);
2282         return 0;
2283     }
2284
2285     if (mapflags & LCMAP_UPPERCASE)
2286       f = toupper;
2287     else if (mapflags & LCMAP_LOWERCASE)
2288       f = tolower;
2289     for (i=0; i < srclen; i++)
2290       dststr[i] = (WCHAR) f(srcstr[i]);
2291     return srclen;
2292   }
2293 }
2294
2295
2296 /***********************************************************************
2297  *           OLE2NLS_EstimateMappingLength
2298  *
2299  * Estimates the number of characters required to hold the string
2300  * computed by LCMapStringA.
2301  *
2302  * The size is always over-estimated, with a fixed limit on the
2303  * amount of estimation error.
2304  *
2305  * Note that len == -1 is not permitted.
2306  */
2307 static inline int OLE2NLS_EstimateMappingLength(LCID lcid, DWORD dwMapFlags,
2308                                                 LPCSTR str, DWORD len)
2309 {
2310     /* Estimate only for small strings to keep the estimation error from
2311      * becoming too large. */
2312     if (len < 128) return len * 8 + 5;
2313     else return LCMapStringA(lcid, dwMapFlags, str, len, NULL, 0);
2314 }
2315
2316 /******************************************************************************
2317  *              CompareStringA  [KERNEL32.143]
2318  * Compares two strings using locale
2319  *
2320  * RETURNS
2321  *
2322  * success: CSTR_LESS_THAN, CSTR_EQUAL, CSTR_GREATER_THAN
2323  * failure: 0
2324  *
2325  * NOTES
2326  *
2327  * Defaults to a word sort, but uses a string sort if
2328  * SORT_STRINGSORT is set.
2329  * Calls SetLastError for ERROR_INVALID_FLAGS, ERROR_INVALID_PARAMETER.
2330  * 
2331  * BUGS
2332  *
2333  * This implementation ignores the locale
2334  *
2335  * FIXME
2336  * 
2337  * Quite inefficient.
2338  */
2339 UINT WINAPI CompareStringA(
2340     DWORD lcid,     /* locale ID */
2341     DWORD fdwStyle, /* comparison-style options */
2342     LPCSTR s1,      /* first string */
2343     DWORD l1,       /* length of first string */
2344     LPCSTR s2,      /* second string */
2345     DWORD l2)       /* length of second string */
2346 {
2347   int mapstring_flags;
2348   int len1,len2;
2349   int result;
2350   LPSTR sk1,sk2;
2351   TRACE("%s and %s\n",
2352         debugstr_a (s1), debugstr_a (s2));
2353
2354   if ( (s1==NULL) || (s2==NULL) )
2355   {    
2356     ERR("(s1=%s,s2=%s): Invalid NULL string\n", s1, s2);
2357     SetLastError(ERROR_INVALID_PARAMETER);
2358     return 0;
2359   }
2360
2361   if(fdwStyle & NORM_IGNORESYMBOLS)
2362     FIXME("IGNORESYMBOLS not supported\n");
2363
2364   if (l1 == -1) l1 = strlen(s1);
2365   if (l2 == -1) l2 = strlen(s2);
2366         
2367   mapstring_flags = LCMAP_SORTKEY | fdwStyle ;
2368   len1 = OLE2NLS_EstimateMappingLength(lcid, mapstring_flags, s1, l1);
2369   len2 = OLE2NLS_EstimateMappingLength(lcid, mapstring_flags, s2, l2);
2370
2371   if ((len1==0)||(len2==0))
2372     return 0;     /* something wrong happened */
2373
2374   sk1 = (LPSTR)HeapAlloc(GetProcessHeap(), 0, len1 + len2);
2375   sk2 = sk1 + len1;
2376   if ( (!LCMapStringA(lcid,mapstring_flags,s1,l1,sk1,len1))
2377          || (!LCMapStringA(lcid,mapstring_flags,s2,l2,sk2,len2)) )
2378   {
2379     ERR("Bug in LCmapStringA.\n");
2380     result = 0;
2381   }
2382   else
2383   {
2384     /* strcmp doesn't necessarily return -1, 0, or 1 */
2385     result = strcmp(sk1,sk2);
2386   }
2387   HeapFree(GetProcessHeap(),0,sk1);
2388
2389   if (result < 0)
2390     return 1;
2391   if (result == 0)
2392     return 2;
2393
2394   /* must be greater, if we reach this point */
2395   return 3;
2396 }
2397
2398 /******************************************************************************
2399  *              CompareStringW  [KERNEL32.144]
2400  * This implementation ignores the locale
2401  * FIXME :  Does only string sort.  Should
2402  * be reimplemented the same way as CompareStringA.
2403  */
2404 UINT WINAPI CompareStringW(DWORD lcid, DWORD fdwStyle, 
2405                                LPCWSTR s1, DWORD l1, LPCWSTR s2,DWORD l2)
2406 {
2407         int len,ret;
2408         if(fdwStyle & NORM_IGNORENONSPACE)
2409                 FIXME("IGNORENONSPACE not supprted\n");
2410         if(fdwStyle & NORM_IGNORESYMBOLS)
2411                 FIXME("IGNORESYMBOLS not supported\n");
2412
2413         /* Is strcmp defaulting to string sort or to word sort?? */
2414         /* FIXME: Handle NORM_STRINGSORT */
2415         l1 = (l1==-1)?lstrlenW(s1):l1;
2416         l2 = (l2==-1)?lstrlenW(s2):l2;
2417         len = l1<l2 ? l1:l2;
2418         ret = (fdwStyle & NORM_IGNORECASE) ? strncmpiW(s1,s2,len) : strncmpW(s1,s2,len);
2419         /* not equal, return 1 or 3 */
2420         if(ret!=0) return ret+2;
2421         /* same len, return 2 */
2422         if(l1==l2) return 2;
2423         /* the longer one is lexically greater */
2424         return (l1<l2)? 1 : 3;
2425 }
2426
2427 /******************************************************************************
2428  *              OLE_GetFormatA  [Internal]
2429  *
2430  * FIXME
2431  *    If datelen == 0, it should return the reguired string length.
2432  *
2433  This function implements stuff for GetDateFormat() and 
2434  GetTimeFormat().
2435
2436   d    single-digit (no leading zero) day (of month)
2437   dd   two-digit day (of month)
2438   ddd  short day-of-week name
2439   dddd long day-of-week name
2440   M    single-digit month
2441   MM   two-digit month
2442   MMM  short month name
2443   MMMM full month name
2444   y    two-digit year, no leading 0
2445   yy   two-digit year
2446   yyyy four-digit year
2447   gg   era string
2448   h    hours with no leading zero (12-hour)
2449   hh   hours with full two digits
2450   H    hours with no leading zero (24-hour)
2451   HH   hours with full two digits
2452   m    minutes with no leading zero
2453   mm   minutes with full two digits
2454   s    seconds with no leading zero
2455   ss   seconds with full two digits
2456   t    time marker (A or P)
2457   tt   time marker (AM, PM)
2458   ''   used to quote literal characters
2459   ''   (within a quoted string) indicates a literal '
2460
2461  These functions REQUIRE valid locale, date,  and format. 
2462  */
2463 static INT OLE_GetFormatA(LCID locale,
2464                             DWORD flags,
2465                             DWORD tflags,
2466                             LPSYSTEMTIME xtime,
2467                             LPCSTR _format,     /*in*/
2468                             LPSTR date,         /*out*/
2469                             INT datelen)
2470 {
2471    INT inpos, outpos;
2472    int count, type, inquote, Overflow;
2473    char buf[40];
2474    char format[40];
2475    char * pos;
2476    int buflen;
2477
2478    const char * _dgfmt[] = { "%d", "%02d" };
2479    const char ** dgfmt = _dgfmt - 1; 
2480
2481    /* report, for debugging */
2482    TRACE("(0x%lx,0x%lx, 0x%lx, time(d=%d,h=%d,m=%d,s=%d), fmt=%p \'%s\' , %p, len=%d)\n",
2483          locale, flags, tflags,
2484          xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2485          _format, _format, date, datelen);
2486   
2487    if(datelen == 0) {
2488      FIXME("datelen = 0, returning 255\n");
2489      return 255;
2490    }
2491
2492    /* initalize state variables and output buffer */
2493    inpos = outpos = 0;
2494    count = 0; inquote = 0; Overflow = 0;
2495    type = '\0';
2496    date[0] = buf[0] = '\0';
2497       
2498    strcpy(format,_format);
2499
2500    /* alter the formatstring, while it works for all languages now in wine
2501    its possible that it fails when the time looks like ss:mm:hh as example*/   
2502    if (tflags & (TIME_NOMINUTESORSECONDS))
2503    { if ((pos = strstr ( format, ":mm")))
2504      { memcpy ( pos, pos+3, strlen(format)-(pos-format)-2 );
2505      }
2506    }
2507    if (tflags & (TIME_NOSECONDS))
2508    { if ((pos = strstr ( format, ":ss")))
2509      { memcpy ( pos, pos+3, strlen(format)-(pos-format)-2 );
2510      }
2511    }
2512    
2513    for (inpos = 0;; inpos++) {
2514       /* 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]); */
2515       if (inquote) {
2516          if (format[inpos] == '\'') {
2517             if (format[inpos+1] == '\'') {
2518                inpos += 1;
2519                date[outpos++] = '\'';
2520             } else {
2521                inquote = 0;
2522                continue; /* we did nothing to the output */
2523             }
2524          } else if (format[inpos] == '\0') {
2525             date[outpos++] = '\0';
2526             if (outpos > datelen) Overflow = 1;
2527             break;
2528          } else {
2529             date[outpos++] = format[inpos];
2530             if (outpos > datelen) {
2531                Overflow = 1;
2532                date[outpos-1] = '\0'; /* this is the last place where
2533                                          it's safe to write */
2534                break;
2535             }
2536          }
2537       } else if (  (count && (format[inpos] != type))
2538                    || count == 4
2539                    || (count == 2 && strchr("ghHmst", type)) )
2540        {
2541             if         (type == 'd') {
2542                if        (count == 4) {
2543                   GetLocaleInfoA(locale,
2544                                    LOCALE_SDAYNAME1
2545                                    + xtime->wDayOfWeek - 1,
2546                                    buf, sizeof(buf));
2547                } else if (count == 3) {
2548                            GetLocaleInfoA(locale, 
2549                                             LOCALE_SABBREVDAYNAME1 
2550                                             + xtime->wDayOfWeek - 1,
2551                                             buf, sizeof(buf));
2552                       } else {
2553                   sprintf(buf, dgfmt[count], xtime->wDay);
2554                }
2555             } else if (type == 'M') {
2556                if (count == 3) {
2557                   GetLocaleInfoA(locale, 
2558                                    LOCALE_SABBREVMONTHNAME1
2559                                    + xtime->wMonth - 1,
2560                                    buf, sizeof(buf));
2561                } else if (count == 4) {
2562                   GetLocaleInfoA(locale,
2563                                    LOCALE_SMONTHNAME1
2564                                    + xtime->wMonth - 1,
2565                                    buf, sizeof(buf));
2566                  } else {
2567                   sprintf(buf, dgfmt[count], xtime->wMonth);
2568                }
2569             } else if (type == 'y') {
2570                if (count == 4) {
2571                       sprintf(buf, "%d", xtime->wYear);
2572                } else if (count == 3) {
2573                   strcpy(buf, "yyy");
2574                   WARN("unknown format, c=%c, n=%d\n",  type, count);
2575                  } else {
2576                   sprintf(buf, dgfmt[count], xtime->wYear % 100);
2577                }
2578             } else if (type == 'g') {
2579                if        (count == 2) {
2580                   FIXME("LOCALE_ICALENDARTYPE unimp.\n");
2581                   strcpy(buf, "AD");
2582             } else {
2583                   strcpy(buf, "g");
2584                   WARN("unknown format, c=%c, n=%d\n", type, count);
2585                }
2586             } else if (type == 'h') {
2587                /* gives us hours 1:00 -- 12:00 */
2588                sprintf(buf, dgfmt[count], (xtime->wHour-1)%12 +1);
2589             } else if (type == 'H') {
2590                /* 24-hour time */
2591                sprintf(buf, dgfmt[count], xtime->wHour);
2592             } else if ( type == 'm') {
2593                sprintf(buf, dgfmt[count], xtime->wMinute);
2594             } else if ( type == 's') {
2595                sprintf(buf, dgfmt[count], xtime->wSecond);
2596             } else if (type == 't') {
2597                if        (count == 1) {
2598                   sprintf(buf, "%c", (xtime->wHour < 12) ? 'A' : 'P');
2599                } else if (count == 2) {
2600                   /* sprintf(buf, "%s", (xtime->wHour < 12) ? "AM" : "PM"); */
2601                   GetLocaleInfoA(locale,
2602                                    (xtime->wHour<12) 
2603                                    ? LOCALE_S1159 : LOCALE_S2359,
2604                                    buf, sizeof(buf));
2605                }
2606             };
2607
2608             /* we need to check the next char in the format string 
2609                again, no matter what happened */
2610             inpos--;
2611             
2612             /* add the contents of buf to the output */
2613             buflen = strlen(buf);
2614             if (outpos + buflen < datelen) {
2615                date[outpos] = '\0'; /* for strcat to hook onto */
2616                  strcat(date, buf);
2617                outpos += buflen;
2618             } else {
2619                date[outpos] = '\0';
2620                strncat(date, buf, datelen - outpos);
2621                  date[datelen - 1] = '\0';
2622                  SetLastError(ERROR_INSUFFICIENT_BUFFER);
2623                WARN("insufficient buffer\n");
2624                  return 0;
2625             }
2626
2627             /* reset the variables we used to keep track of this item */
2628             count = 0;
2629             type = '\0';
2630          } else if (format[inpos] == '\0') {
2631             /* we can't check for this at the loop-head, because
2632                that breaks the printing of the last format-item */
2633             date[outpos] = '\0';
2634             break;
2635          } else if (count) {
2636             /* continuing a code for an item */
2637             count +=1;
2638             continue;
2639          } else if (strchr("hHmstyMdg", format[inpos])) {
2640             type = format[inpos];
2641             count = 1;
2642             continue;
2643          } else if (format[inpos] == '\'') {
2644             inquote = 1;
2645             continue;
2646        } else {
2647             date[outpos++] = format[inpos];
2648          }
2649       /* now deal with a possible buffer overflow */
2650       if (outpos >= datelen) {
2651        date[datelen - 1] = '\0';
2652        SetLastError(ERROR_INSUFFICIENT_BUFFER);
2653        return 0;
2654       }
2655    }
2656    
2657    if (Overflow) {
2658       SetLastError(ERROR_INSUFFICIENT_BUFFER);
2659    };
2660
2661    /* finish it off with a string terminator */
2662    outpos++;
2663    /* sanity check */
2664    if (outpos > datelen-1) outpos = datelen-1;
2665    date[outpos] = '\0';
2666    
2667    TRACE("OLE_GetFormatA returns string '%s', len %d\n",
2668                date, outpos);
2669    return outpos;
2670 }
2671
2672 /******************************************************************************
2673  * OLE_GetFormatW [INTERNAL]
2674  */
2675 static INT OLE_GetFormatW(LCID locale, DWORD flags, DWORD tflags,
2676                             LPSYSTEMTIME xtime,
2677                             LPCWSTR format,
2678                             LPWSTR output, INT outlen)
2679 {
2680    INT   inpos, outpos;
2681    int     count, type=0, inquote;
2682    int     Overflow; /* loop check */
2683    WCHAR   buf[40];
2684    int     buflen=0;
2685    WCHAR   arg0[] = {0}, arg1[] = {'%','d',0};
2686    WCHAR   arg2[] = {'%','0','2','d',0};
2687    WCHAR  *argarr[3];
2688    int     datevars=0, timevars=0;
2689
2690    argarr[0] = arg0;
2691    argarr[1] = arg1;
2692    argarr[2] = arg2;
2693
2694    /* make a debug report */
2695    TRACE("args: 0x%lx, 0x%lx, 0x%lx, time(d=%d,h=%d,m=%d,s=%d), fmt:%s (at %p), "
2696               "%p with max len %d\n",
2697          locale, flags, tflags,
2698          xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2699          debugstr_w(format), format, output, outlen);
2700    
2701    if(outlen == 0) {
2702      FIXME("outlen = 0, returning 255\n");
2703      return 255;
2704    }
2705
2706    /* initialize state variables */
2707    inpos = outpos = 0;
2708    count = 0;
2709    inquote = Overflow = 0;
2710    /* this is really just a sanity check */
2711    output[0] = buf[0] = 0;
2712    
2713    /* this loop is the core of the function */
2714    for (inpos = 0; /* we have several break points */ ; inpos++) {
2715       if (inquote) {
2716          if (format[inpos] == (WCHAR) '\'') {
2717             if (format[inpos+1] == '\'') {
2718                inpos++;
2719                output[outpos++] = '\'';
2720             } else {
2721                inquote = 0;
2722                continue;
2723             }
2724          } else if (format[inpos] == 0) {
2725             output[outpos++] = 0;
2726             if (outpos > outlen) Overflow = 1;
2727             break;  /*  normal exit (within a quote) */
2728          } else {
2729             output[outpos++] = format[inpos]; /* copy input */
2730             if (outpos > outlen) {
2731                Overflow = 1;
2732                output[outpos-1] = 0; 
2733                break;
2734             }
2735          }
2736       } else if (  (count && (format[inpos] != type))
2737                    || ( (count==4 && type =='y') ||
2738                         (count==4 && type =='M') ||
2739                         (count==4 && type =='d') ||
2740                         (count==2 && type =='g') ||
2741                         (count==2 && type =='h') ||
2742                         (count==2 && type =='H') ||
2743                         (count==2 && type =='m') ||
2744                         (count==2 && type =='s') ||
2745                         (count==2 && type =='t') )  ) {
2746          if        (type == 'd') {
2747             if        (count == 3) {
2748                GetLocaleInfoW(locale,
2749                              LOCALE_SDAYNAME1 + xtime->wDayOfWeek -1,
2750                              buf, sizeof(buf)/sizeof(WCHAR) );
2751             } else if (count == 3) {
2752                GetLocaleInfoW(locale,
2753                                 LOCALE_SABBREVDAYNAME1 +
2754                                 xtime->wDayOfWeek -1,
2755                                 buf, sizeof(buf)/sizeof(WCHAR) );
2756             } else {
2757                wsnprintfW(buf, 5, argarr[count], xtime->wDay );
2758             };
2759          } else if (type == 'M') {
2760             if        (count == 4) {
2761                GetLocaleInfoW(locale,  LOCALE_SMONTHNAME1 +
2762                                 xtime->wMonth -1, buf,
2763                                 sizeof(buf)/sizeof(WCHAR) );
2764             } else if (count == 3) {
2765                GetLocaleInfoW(locale,  LOCALE_SABBREVMONTHNAME1 +
2766                                 xtime->wMonth -1, buf,
2767                                 sizeof(buf)/sizeof(WCHAR) );
2768             } else {
2769                wsnprintfW(buf, 5, argarr[count], xtime->wMonth);
2770             }
2771          } else if (type == 'y') {
2772             if        (count == 4) {
2773                wsnprintfW(buf, 6, argarr[1] /* "%d" */,
2774                          xtime->wYear);
2775             } else if (count == 3) {
2776                lstrcpynAtoW(buf, "yyy", 5);
2777             } else {
2778                wsnprintfW(buf, 6, argarr[count],
2779                             xtime->wYear % 100);
2780             }
2781          } else if (type == 'g') {
2782             if        (count == 2) {
2783                FIXME("LOCALE_ICALENDARTYPE unimplemented\n");
2784                lstrcpynAtoW(buf, "AD", 5);
2785             } else {
2786                /* Win API sez we copy it verbatim */
2787                lstrcpynAtoW(buf, "g", 5);
2788             }
2789          } else if (type == 'h') {
2790             /* hours 1:00-12:00 --- is this right? */
2791             wsnprintfW(buf, 5, argarr[count], 
2792                          (xtime->wHour-1)%12 +1);
2793          } else if (type == 'H') {
2794             wsnprintfW(buf, 5, argarr[count], 
2795                          xtime->wHour);
2796          } else if (type == 'm' ) {
2797             wsnprintfW(buf, 5, argarr[count],
2798                          xtime->wMinute);
2799          } else if (type == 's' ) {
2800             wsnprintfW(buf, 5, argarr[count],
2801                          xtime->wSecond);
2802          } else if (type == 't') {
2803             GetLocaleInfoW(locale, (xtime->wHour < 12) ?
2804                              LOCALE_S1159 : LOCALE_S2359,
2805                              buf, sizeof(buf) );
2806             if        (count == 1) {
2807                buf[1] = 0;
2808             }
2809 }
2810
2811          /* no matter what happened,  we need to check this next 
2812             character the next time we loop through */
2813          inpos--;
2814
2815          /* cat buf onto the output */
2816          outlen = lstrlenW(buf);
2817          if (outpos + buflen < outlen) {
2818             lstrcpyW( output + outpos, buf );
2819             outpos += buflen;
2820          } else {
2821             lstrcpynW( output + outpos, buf, outlen - outpos );
2822             Overflow = 1;
2823             break; /* Abnormal exit */
2824          }
2825
2826          /* reset the variables we used this time */
2827          count = 0;
2828          type = '\0';
2829       } else if (format[inpos] == 0) {
2830          /* we can't check for this at the beginning,  because that 
2831          would keep us from printing a format spec that ended the 
2832          string */
2833          output[outpos] = 0;
2834          break;  /*  NORMAL EXIT  */
2835       } else if (count) {
2836          /* how we keep track of the middle of a format spec */
2837          count++;
2838          continue;
2839       } else if ( (datevars && (format[inpos]=='d' ||
2840                                 format[inpos]=='M' ||
2841                                 format[inpos]=='y' ||
2842                                 format[inpos]=='g')  ) ||
2843                   (timevars && (format[inpos]=='H' ||
2844                                 format[inpos]=='h' ||
2845                                 format[inpos]=='m' ||
2846                                 format[inpos]=='s' ||
2847                                 format[inpos]=='t') )    ) {
2848          type = format[inpos];
2849          count = 1;
2850          continue;
2851       } else if (format[inpos] == '\'') {
2852          inquote = 1;
2853          continue;
2854       } else {
2855          /* unquoted literals */
2856          output[outpos++] = format[inpos];
2857       }
2858    }
2859
2860    if (Overflow) {
2861       SetLastError(ERROR_INSUFFICIENT_BUFFER);
2862       WARN(" buffer overflow\n");
2863    };
2864
2865    /* final string terminator and sanity check */
2866    outpos++;
2867    if (outpos > outlen-1) outpos = outlen-1;
2868    output[outpos] = '0';
2869
2870    TRACE(" returning %s\n", debugstr_w(output));
2871         
2872    return (!Overflow) ? outlen : 0;
2873    
2874 }
2875
2876
2877 /******************************************************************************
2878  *              GetDateFormatA  [KERNEL32.310]
2879  * Makes an ASCII string of the date
2880  *
2881  * This function uses format to format the date,  or,  if format
2882  * is NULL, uses the default for the locale.  format is a string
2883  * of literal fields and characters as follows:
2884  *
2885  * - d    single-digit (no leading zero) day (of month)
2886  * - dd   two-digit day (of month)
2887  * - ddd  short day-of-week name
2888  * - dddd long day-of-week name
2889  * - M    single-digit month
2890  * - MM   two-digit month
2891  * - MMM  short month name
2892  * - MMMM full month name
2893  * - y    two-digit year, no leading 0
2894  * - yy   two-digit year
2895  * - yyyy four-digit year
2896  * - gg   era string
2897  *
2898  */
2899 INT WINAPI GetDateFormatA(LCID locale,DWORD flags,
2900                               LPSYSTEMTIME xtime,
2901                               LPCSTR format, LPSTR date,INT datelen) 
2902 {
2903    
2904   char format_buf[40];
2905   LPCSTR thisformat;
2906   SYSTEMTIME t;
2907   LPSYSTEMTIME thistime;
2908   LCID thislocale;
2909   INT ret;
2910
2911   TRACE("(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",
2912               locale,flags,xtime,format,date,datelen);
2913   
2914   if (!locale) {
2915      locale = LOCALE_SYSTEM_DEFAULT;
2916      };
2917   
2918   if (locale == LOCALE_SYSTEM_DEFAULT) {
2919      thislocale = GetSystemDefaultLCID();
2920   } else if (locale == LOCALE_USER_DEFAULT) {
2921      thislocale = GetUserDefaultLCID();
2922   } else {
2923      thislocale = locale;
2924    };
2925
2926   if (xtime == NULL) {
2927      GetSystemTime(&t);
2928      thistime = &t;
2929   } else {
2930      thistime = xtime;
2931   };
2932
2933   if (format == NULL) {
2934      GetLocaleInfoA(thislocale, ((flags&DATE_LONGDATE) 
2935                                    ? LOCALE_SLONGDATE
2936                                    : LOCALE_SSHORTDATE),
2937                       format_buf, sizeof(format_buf));
2938      thisformat = format_buf;
2939   } else {
2940      thisformat = format;
2941   };
2942
2943   
2944   ret = OLE_GetFormatA(thislocale, flags, 0, thistime, thisformat, 
2945                        date, datelen);
2946   
2947
2948    TRACE(
2949                "GetDateFormatA() returning %d, with data=%s\n",
2950                ret, date);
2951   return ret;
2952 }
2953
2954 /******************************************************************************
2955  *              GetDateFormatW  [KERNEL32.311]
2956  * Makes a Unicode string of the date
2957  *
2958  * Acts the same as GetDateFormatA(),  except that it's Unicode.
2959  * Accepts & returns sizes as counts of Unicode characters.
2960  *
2961  */
2962 INT WINAPI GetDateFormatW(LCID locale,DWORD flags,
2963                               LPSYSTEMTIME xtime,
2964                               LPCWSTR format,
2965                               LPWSTR date, INT datelen)
2966 {
2967    unsigned short datearr[] = {'1','9','9','4','-','1','-','1',0};
2968
2969    FIXME("STUB (should call OLE_GetFormatW)\n");   
2970    lstrcpynW(date, datearr, datelen);
2971    return (  datelen < 9) ? datelen : 9;
2972    
2973    
2974 }
2975
2976 /**************************************************************************
2977  *              EnumDateFormatsA        (KERNEL32.198)
2978  */
2979 BOOL WINAPI EnumDateFormatsA(
2980   DATEFMT_ENUMPROCA lpDateFmtEnumProc, LCID Locale,  DWORD dwFlags)
2981 {
2982   LCID Loc = GetUserDefaultLCID(); 
2983   if(!lpDateFmtEnumProc)
2984     {
2985       SetLastError(ERROR_INVALID_PARAMETER);
2986       return FALSE;
2987     }
2988
2989   switch( Loc )
2990  {
2991
2992    case 0x00000407:  /* (Loc,"de_DE") */
2993    {
2994     switch(dwFlags)
2995     {
2996       case DATE_SHORTDATE:
2997         if(!(*lpDateFmtEnumProc)("dd.MM.yy")) return TRUE;
2998         if(!(*lpDateFmtEnumProc)("d.M.yyyy")) return TRUE;
2999         if(!(*lpDateFmtEnumProc)("d.MM.yy")) return TRUE;
3000         if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
3001         return TRUE;
3002       case DATE_LONGDATE:
3003         if(!(*lpDateFmtEnumProc)("dddd,d. MMMM yyyy")) return TRUE;
3004         if(!(*lpDateFmtEnumProc)("d. MMMM yyyy")) return TRUE;
3005         if(!(*lpDateFmtEnumProc)("d. MMM yyyy")) return TRUE;
3006         return TRUE;
3007       default:
3008         FIXME("Unknown date format (%ld)\n", dwFlags); 
3009         SetLastError(ERROR_INVALID_PARAMETER);
3010         return FALSE;
3011      }
3012    }       
3013
3014    case 0x0000040c:  /* (Loc,"fr_FR") */
3015    {
3016     switch(dwFlags)
3017     {
3018       case DATE_SHORTDATE:
3019         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3020         if(!(*lpDateFmtEnumProc)("dd.MM.yy")) return TRUE;
3021         if(!(*lpDateFmtEnumProc)("dd-MM-yy")) return TRUE;
3022         if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
3023         return TRUE;
3024       case DATE_LONGDATE:
3025         if(!(*lpDateFmtEnumProc)("dddd d MMMM yyyy")) return TRUE;
3026         if(!(*lpDateFmtEnumProc)("d MMM yy")) return TRUE;
3027         if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
3028         return TRUE;
3029       default:
3030         FIXME("Unknown date format (%ld)\n", dwFlags); 
3031         SetLastError(ERROR_INVALID_PARAMETER);
3032         return FALSE;
3033      }
3034    }
3035
3036    case 0x00000c0c:  /* (Loc,"fr_CA") */
3037    {
3038     switch(dwFlags)
3039     {
3040       case DATE_SHORTDATE:
3041         if(!(*lpDateFmtEnumProc)("yy-MM-dd")) return TRUE;
3042         if(!(*lpDateFmtEnumProc)("dd-MM-yy")) return TRUE;
3043         if(!(*lpDateFmtEnumProc)("yy MM dd")) return TRUE;
3044         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3045         return TRUE;
3046       case DATE_LONGDATE:
3047         if(!(*lpDateFmtEnumProc)("d MMMM, yyyy")) return TRUE;
3048         if(!(*lpDateFmtEnumProc)("d MMM yyyy")) return TRUE;
3049         return TRUE;
3050       default:
3051         FIXME("Unknown date format (%ld)\n", dwFlags); 
3052         SetLastError(ERROR_INVALID_PARAMETER);
3053         return FALSE;
3054      }
3055    }
3056
3057    case 0x00000809:  /* (Loc,"en_UK") */ 
3058   {    
3059    switch(dwFlags)
3060     {
3061       case DATE_SHORTDATE:
3062         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3063         if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
3064         if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
3065         if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
3066         return TRUE;
3067       case DATE_LONGDATE:
3068         if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
3069         if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
3070         return TRUE;
3071       default:
3072         FIXME("Unknown date format (%ld)\n", dwFlags); 
3073         SetLastError(ERROR_INVALID_PARAMETER);
3074         return FALSE;
3075     }
3076   }
3077
3078    case 0x00000c09:  /* (Loc,"en_AU") */   
3079   {    
3080    switch(dwFlags)
3081     {
3082       case DATE_SHORTDATE:
3083         if(!(*lpDateFmtEnumProc)("d/MM/yy")) return TRUE;
3084         if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
3085         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3086         return TRUE;
3087       case DATE_LONGDATE:
3088         if(!(*lpDateFmtEnumProc)("dddd,d MMMM yyyy")) return TRUE;
3089         if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
3090         return TRUE;
3091       default:
3092         FIXME("Unknown date format (%ld)\n", dwFlags); 
3093         SetLastError(ERROR_INVALID_PARAMETER);
3094         return FALSE;
3095     }
3096   }
3097
3098    case 0x00001009:  /* (Loc,"en_CA") */ 
3099   {    
3100    switch(dwFlags)
3101     {
3102       case DATE_SHORTDATE:
3103         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3104         if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
3105         if(!(*lpDateFmtEnumProc)("yy-MM-dd")) return TRUE;
3106         if(!(*lpDateFmtEnumProc)("M/dd/yy")) return TRUE;
3107         return TRUE;
3108       case DATE_LONGDATE:
3109         if(!(*lpDateFmtEnumProc)("d-MMM-yy")) return TRUE;
3110         if(!(*lpDateFmtEnumProc)("MMMM d, yyyy")) return TRUE;
3111         return TRUE;
3112       default:
3113         FIXME("Unknown date format (%ld)\n", dwFlags); 
3114         SetLastError(ERROR_INVALID_PARAMETER);
3115         return FALSE;
3116     }
3117   }
3118
3119    case 0x00001409:  /* (Loc,"en_NZ") */ 
3120   {    
3121    switch(dwFlags)
3122     {
3123       case DATE_SHORTDATE:
3124         if(!(*lpDateFmtEnumProc)("d/MM/yy")) return TRUE;
3125         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3126         if(!(*lpDateFmtEnumProc)("d.MM.yy")) return TRUE;
3127         return TRUE;
3128       case DATE_LONGDATE:
3129         if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
3130         if(!(*lpDateFmtEnumProc)("dddd, d MMMM yyyy")) return TRUE;
3131         return TRUE;
3132       default:
3133         FIXME("Unknown date format (%ld)\n", dwFlags); 
3134         SetLastError(ERROR_INVALID_PARAMETER);
3135         return FALSE;
3136     }
3137   }
3138
3139    case 0x00001809:  /* (Loc,"en_IE") */   
3140   {    
3141    switch(dwFlags)
3142     {
3143       case DATE_SHORTDATE:
3144         if(!(*lpDateFmtEnumProc)("dd/MM/yy")) return TRUE;
3145         if(!(*lpDateFmtEnumProc)("d/M/yy")) return TRUE;
3146         if(!(*lpDateFmtEnumProc)("d.M.yy")) return TRUE;
3147         return TRUE;
3148       case DATE_LONGDATE:
3149         if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
3150         if(!(*lpDateFmtEnumProc)("d MMMM yyyy")) return TRUE;
3151         return TRUE;
3152       default:
3153         FIXME("Unknown date format (%ld)\n", dwFlags); 
3154         SetLastError(ERROR_INVALID_PARAMETER);
3155         return FALSE;
3156     }
3157   }
3158
3159    case 0x00001c09:  /* (Loc,"en_ZA") */   
3160   {    
3161    switch(dwFlags)
3162     {
3163       case DATE_SHORTDATE:
3164         if(!(*lpDateFmtEnumProc)("yy/MM/dd")) return TRUE;
3165         return TRUE;
3166       case DATE_LONGDATE:
3167         if(!(*lpDateFmtEnumProc)("dd MMMM yyyy")) return TRUE;
3168         return TRUE;
3169       default:
3170         FIXME("Unknown date format (%ld)\n", dwFlags); 
3171         SetLastError(ERROR_INVALID_PARAMETER);
3172         return FALSE;
3173     }
3174   }
3175
3176    case 0x00002009:  /* (Loc,"en_JM") */  
3177   {    
3178    switch(dwFlags)
3179     {
3180       case DATE_SHORTDATE:
3181         if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
3182         return TRUE;
3183       case DATE_LONGDATE:
3184         if(!(*lpDateFmtEnumProc)("dddd,MMMM dd,yyyy")) return TRUE;
3185         if(!(*lpDateFmtEnumProc)("MMMM dd,yyyy")) return TRUE;
3186         if(!(*lpDateFmtEnumProc)("dddd,dd MMMM,yyyy")) return TRUE;
3187         if(!(*lpDateFmtEnumProc)("dd MMMM,yyyy")) return TRUE;
3188         return TRUE;
3189       default:
3190         FIXME("Unknown date format (%ld)\n", dwFlags); 
3191         SetLastError(ERROR_INVALID_PARAMETER);
3192         return FALSE;
3193     }
3194   }
3195
3196    case 0x00002809:  /* (Loc,"en_BZ") */
3197    case 0x00002c09:  /* (Loc,"en_TT") */  
3198   {    
3199    switch(dwFlags)
3200     {
3201       case DATE_SHORTDATE:
3202         if(!(*lpDateFmtEnumProc)("dd/MM/yyyy")) return TRUE;
3203         return TRUE;
3204       case DATE_LONGDATE:
3205         if(!(*lpDateFmtEnumProc)("dddd,dd MMMM yyyy")) return TRUE;
3206         return TRUE;
3207       default:
3208         FIXME("Unknown date format (%ld)\n", dwFlags); 
3209         SetLastError(ERROR_INVALID_PARAMETER);
3210         return FALSE;
3211     }
3212   }
3213
3214    default:  /* default to US English "en_US" */
3215   {
3216    switch(dwFlags)
3217     {
3218       case DATE_SHORTDATE:
3219         if(!(*lpDateFmtEnumProc)("M/d/yy")) return TRUE;
3220         if(!(*lpDateFmtEnumProc)("M/d/yyyy")) return TRUE;
3221         if(!(*lpDateFmtEnumProc)("MM/dd/yy")) return TRUE;
3222         if(!(*lpDateFmtEnumProc)("MM/dd/yyyy")) return TRUE;
3223         if(!(*lpDateFmtEnumProc)("yy/MM/dd")) return TRUE;
3224         if(!(*lpDateFmtEnumProc)("dd-MMM-yy")) return TRUE;
3225         return TRUE;
3226       case DATE_LONGDATE:
3227         if(!(*lpDateFmtEnumProc)("dddd, MMMM dd, yyyy")) return TRUE;
3228         if(!(*lpDateFmtEnumProc)("MMMM dd, yyyy")) return TRUE;
3229         if(!(*lpDateFmtEnumProc)("dddd, dd MMMM, yyyy")) return TRUE;
3230         if(!(*lpDateFmtEnumProc)("dd MMMM, yyyy")) return TRUE;
3231         return TRUE;
3232       default:
3233         FIXME("Unknown date format (%ld)\n", dwFlags); 
3234         SetLastError(ERROR_INVALID_PARAMETER);
3235         return FALSE;
3236     }
3237   }
3238  }
3239 }
3240
3241 /**************************************************************************
3242  *              EnumDateFormatsW        (KERNEL32.199)
3243  */
3244 BOOL WINAPI EnumDateFormatsW(
3245   DATEFMT_ENUMPROCW lpDateFmtEnumProc, LCID Locale, DWORD dwFlags)
3246 {
3247   FIXME("(%p, %ld, %ld): stub\n", lpDateFmtEnumProc, Locale, dwFlags);
3248   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3249   return FALSE;
3250 }
3251
3252 /**************************************************************************
3253  *              EnumTimeFormatsA        (KERNEL32.210)
3254  */
3255 BOOL WINAPI EnumTimeFormatsA(
3256   TIMEFMT_ENUMPROCA lpTimeFmtEnumProc, LCID Locale, DWORD dwFlags)
3257 {
3258   LCID Loc = GetUserDefaultLCID(); 
3259   if(!lpTimeFmtEnumProc)
3260     {
3261       SetLastError(ERROR_INVALID_PARAMETER);
3262       return FALSE;
3263     }
3264   if(dwFlags)
3265     {
3266       FIXME("Unknown time format (%ld)\n", dwFlags); 
3267     }
3268
3269   switch( Loc )
3270  {
3271    case 0x00000407:  /* (Loc,"de_DE") */
3272    {
3273     if(!(*lpTimeFmtEnumProc)("HH.mm")) return TRUE; 
3274     if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
3275     if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
3276     if(!(*lpTimeFmtEnumProc)("H.mm")) return TRUE;
3277     if(!(*lpTimeFmtEnumProc)("H.mm'Uhr'")) return TRUE;
3278     return TRUE;
3279    }       
3280
3281    case 0x0000040c:  /* (Loc,"fr_FR") */
3282    case 0x00000c0c:  /* (Loc,"fr_CA") */
3283    {
3284     if(!(*lpTimeFmtEnumProc)("H:mm")) return TRUE;
3285     if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
3286     if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
3287     if(!(*lpTimeFmtEnumProc)("HH.mm")) return TRUE;
3288     if(!(*lpTimeFmtEnumProc)("HH'h'mm")) return TRUE;
3289     return TRUE;
3290    }
3291
3292    case 0x00000809:  /* (Loc,"en_UK") */
3293    case 0x00000c09:  /* (Loc,"en_AU") */ 
3294    case 0x00001409:  /* (Loc,"en_NZ") */
3295    case 0x00001809:  /* (Loc,"en_IE") */ 
3296    {
3297     if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
3298     if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
3299     if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
3300     return TRUE;
3301    }
3302
3303    case 0x00001c09:  /* (Loc,"en_ZA") */   
3304    case 0x00002809:  /* (Loc,"en_BZ") */
3305    case 0x00002c09:  /* (Loc,"en_TT") */ 
3306    {
3307     if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
3308     if(!(*lpTimeFmtEnumProc)("hh:mm:ss tt")) return TRUE; 
3309     return TRUE;  
3310    }
3311
3312    default:  /* default to US style "en_US" */   
3313    {
3314     if(!(*lpTimeFmtEnumProc)("h:mm:ss tt")) return TRUE;
3315     if(!(*lpTimeFmtEnumProc)("hh:mm:ss tt")) return TRUE;
3316     if(!(*lpTimeFmtEnumProc)("H:mm:ss")) return TRUE;
3317     if(!(*lpTimeFmtEnumProc)("HH:mm:ss")) return TRUE;
3318     return TRUE;
3319    }
3320  }
3321 }
3322
3323 /**************************************************************************
3324  *              EnumTimeFormatsW        (KERNEL32.211)
3325  */
3326 BOOL WINAPI EnumTimeFormatsW(
3327   TIMEFMT_ENUMPROCW lpTimeFmtEnumProc, LCID Locale, DWORD dwFlags)
3328 {
3329   FIXME("(%p,%ld,%ld): stub\n", lpTimeFmtEnumProc, Locale, dwFlags);
3330   SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
3331   return FALSE;
3332 }
3333
3334 /**************************************************************************
3335  *           This function is used just locally !
3336  *  Description: Inverts a string.
3337  */ 
3338 static void OLE_InvertString(char* string)
3339 {    
3340     char    sTmpArray[128];
3341     INT     counter, i = 0;
3342
3343     for (counter = strlen(string); counter > 0; counter--)
3344     {
3345         memcpy(sTmpArray + i, string + counter-1, 1);
3346         i++;
3347     }
3348     memcpy(sTmpArray + i, "\0", 1);
3349     strcpy(string, sTmpArray);
3350 }
3351
3352 /***************************************************************************************
3353  *           This function is used just locally !
3354  *  Description: Test if the given string (psNumber) is valid or not.
3355  *               The valid characters are the following:
3356  *               - Characters '0' through '9'. 
3357  *               - One decimal point (dot) if the number is a floating-point value. 
3358  *               - A minus sign in the first character position if the number is 
3359  *                 a negative value.
3360  *              If the function succeeds, psBefore/psAfter will point to the string
3361  *              on the right/left of the decimal symbol. pbNegative indicates if the 
3362  *              number is negative.
3363  */
3364 static INT OLE_GetNumberComponents(char* pInput, char* psBefore, char* psAfter, BOOL* pbNegative)
3365 {
3366 char    sNumberSet[] = "0123456789";
3367 BOOL    bInDecimal = FALSE;
3368
3369         /* Test if we do have a minus sign */
3370         if ( *pInput == '-' )
3371         {
3372                 *pbNegative = TRUE;
3373                 pInput++; /* Jump to the next character. */
3374         }
3375         
3376         while(*pInput != '\0')
3377         {
3378                 /* Do we have a valid numeric character */
3379                 if ( strchr(sNumberSet, *pInput) != NULL )
3380                 {
3381                         if (bInDecimal == TRUE)
3382                 *psAfter++ = *pInput;
3383                         else
3384                 *psBefore++ = *pInput;
3385                 }
3386                 else
3387                 {
3388                         /* Is this a decimal point (dot) */
3389                         if ( *pInput == '.' )
3390                         {
3391                                 /* Is it the first time we find it */
3392                                 if ((bInDecimal == FALSE))
3393                                         bInDecimal = TRUE;
3394                                 else
3395                                         return -1; /* ERROR: Invalid parameter */
3396                         }
3397                         else
3398                         {
3399                                 /* It's neither a numeric character, nor a decimal point.
3400                                  * Thus, return an error.
3401                  */
3402                                 return -1;
3403                         }
3404                 }
3405         pInput++;
3406         }
3407         
3408         /* Add an End of Line character to the output buffers */
3409         *psBefore = '\0';
3410         *psAfter = '\0';
3411
3412         return 0; 
3413 }
3414
3415 /**************************************************************************
3416  *           This function is used just locally !
3417  *  Description: A number could be formatted using different numbers 
3418  *               of "digits in group" (example: 4;3;2;0).
3419  *               The first parameter of this function is an array
3420  *               containing the rule to be used. It's format is the following:
3421  *               |NDG|DG1|DG2|...|0|
3422  *               where NDG is the number of used "digits in group" and DG1, DG2,
3423  *               are the corresponding "digits in group".
3424  *               Thus, this function returns the grouping value in the array
3425  *               pointed by the second parameter.
3426  */
3427 static INT OLE_GetGrouping(char* sRule, INT index)
3428 {
3429     char    sData[2], sRuleSize[2];
3430     INT     nData, nRuleSize;
3431
3432     memcpy(sRuleSize, sRule, 1);
3433     memcpy(sRuleSize+1, "\0", 1);
3434     nRuleSize = atoi(sRuleSize);
3435
3436     if (index > 0 && index < nRuleSize)
3437     {
3438         memcpy(sData, sRule+index, 1);
3439         memcpy(sData+1, "\0", 1);
3440         nData = atoi(sData);            
3441     }
3442         
3443     else
3444     {
3445         memcpy(sData, sRule+nRuleSize-1, 1);
3446         memcpy(sData+1, "\0", 1);
3447         nData = atoi(sData);            
3448     }
3449     
3450     return nData;
3451 }
3452
3453 /**************************************************************************
3454  *              GetNumberFormatA        (KERNEL32.355)
3455  */
3456 INT WINAPI GetNumberFormatA(LCID locale, DWORD dwflags,
3457                                LPCSTR lpvalue,   const NUMBERFMTA * lpFormat,
3458                                LPSTR lpNumberStr, int cchNumber)
3459 {
3460     char   sNumberDigits[3], sDecimalSymbol[5], sDigitsInGroup[11], sDigitGroupSymbol[5], sILZero[2];
3461     INT    nNumberDigits, nNumberDecimal, i, j, nCounter, nStep, nRuleIndex, nGrouping, nDigits, retVal, nLZ;
3462     char   sNumber[128], sDestination[128], sDigitsAfterDecimal[10], sDigitsBeforeDecimal[128];
3463     char   sRule[10], sSemiColumn[]=";", sBuffer[5], sNegNumber[2];    
3464     char   *pStr = NULL, *pTmpStr = NULL;
3465     LCID   systemDefaultLCID;
3466     BOOL   bNegative = FALSE;
3467     enum   Operations 
3468     {
3469         USE_PARAMETER,
3470         USE_LOCALEINFO,
3471         USE_SYSTEMDEFAULT,
3472         RETURN_ERROR
3473     } used_operation;
3474
3475     strncpy(sNumber, lpvalue, 128);
3476     sNumber[127] = '\0';
3477
3478     /* Make sure we have a valid input string, get the number
3479      * of digits before and after the decimal symbol, and check
3480      * if this is a negative number.
3481      */
3482     if ( OLE_GetNumberComponents(sNumber, sDigitsBeforeDecimal, sDigitsAfterDecimal, &bNegative) != -1)
3483     {
3484         nNumberDecimal = strlen(sDigitsBeforeDecimal);
3485         nDigits = strlen(sDigitsAfterDecimal);
3486     }
3487     else
3488     {
3489         SetLastError(ERROR_INVALID_PARAMETER);
3490         return 0;
3491     }
3492
3493     /* Which source will we use to format the string */
3494     used_operation = RETURN_ERROR;
3495     if (lpFormat != NULL)
3496     {
3497         if (dwflags == 0)
3498             used_operation = USE_PARAMETER; 
3499     }
3500     else
3501     {
3502         if (dwflags & LOCALE_NOUSEROVERRIDE)
3503             used_operation = USE_LOCALEINFO;
3504         else
3505             used_operation = USE_SYSTEMDEFAULT;
3506     }
3507
3508     /* Load the fields we need */
3509     switch(used_operation)
3510     {
3511     case USE_LOCALEINFO:
3512         GetLocaleInfoA(locale, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
3513         GetLocaleInfoA(locale, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
3514         GetLocaleInfoA(locale, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
3515         GetLocaleInfoA(locale, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
3516         GetLocaleInfoA(locale, LOCALE_ILZERO, sILZero, sizeof(sILZero));
3517         GetLocaleInfoA(locale, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
3518         break;
3519     case USE_PARAMETER:
3520         sprintf(sNumberDigits, "%d",lpFormat->NumDigits);
3521         strcpy(sDecimalSymbol, lpFormat->lpDecimalSep);
3522         sprintf(sDigitsInGroup, "%d;0",lpFormat->Grouping);
3523         strcpy(sDigitGroupSymbol, lpFormat->lpThousandSep);
3524         sprintf(sILZero, "%d",lpFormat->LeadingZero);
3525         sprintf(sNegNumber, "%d",lpFormat->NegativeOrder);
3526         break;
3527     case USE_SYSTEMDEFAULT:
3528         systemDefaultLCID = GetSystemDefaultLCID();
3529         GetLocaleInfoA(systemDefaultLCID, LOCALE_IDIGITS, sNumberDigits, sizeof(sNumberDigits));
3530         GetLocaleInfoA(systemDefaultLCID, LOCALE_SDECIMAL, sDecimalSymbol, sizeof(sDecimalSymbol));
3531         GetLocaleInfoA(systemDefaultLCID, LOCALE_SGROUPING, sDigitsInGroup, sizeof(sDigitsInGroup));
3532         GetLocaleInfoA(systemDefaultLCID, LOCALE_STHOUSAND, sDigitGroupSymbol, sizeof(sDigitGroupSymbol));
3533         GetLocaleInfoA(systemDefaultLCID, LOCALE_ILZERO, sILZero, sizeof(sILZero));
3534         GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGNUMBER, sNegNumber, sizeof(sNegNumber));
3535         break;
3536     default:
3537         SetLastError(ERROR_INVALID_PARAMETER);
3538         return 0;
3539     }
3540
3541     nNumberDigits = atoi(sNumberDigits);
3542     
3543     /* Remove the ";" */
3544     i=0;
3545     j = 1;
3546     for (nCounter=0; nCounter<strlen(sDigitsInGroup); nCounter++)
3547     {
3548         if ( memcmp(sDigitsInGroup + nCounter, sSemiColumn, 1) != 0 )
3549         {
3550             memcpy(sRule + j, sDigitsInGroup + nCounter, 1);
3551             i++;
3552             j++;
3553         }
3554     }
3555     sprintf(sBuffer, "%d", i);
3556     memcpy(sRule, sBuffer, 1); /* Number of digits in the groups ( used by OLE_GetGrouping() ) */
3557     memcpy(sRule + j, "\0", 1);
3558     
3559     /* First, format the digits before the decimal. */
3560     if ((nNumberDecimal>0) && (atoi(sDigitsBeforeDecimal) != 0)) 
3561     {
3562         /* Working on an inverted string is easier ! */
3563         OLE_InvertString(sDigitsBeforeDecimal);
3564
3565         nStep = nCounter = i = j = 0;
3566         nRuleIndex = 1;
3567         nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
3568         
3569         /* Here, we will loop until we reach the end of the string.
3570          * An internal counter (j) is used in order to know when to 
3571          * insert the "digit group symbol".
3572          */
3573         while (nNumberDecimal > 0)
3574         {
3575             i = nCounter + nStep;
3576             memcpy(sDestination + i, sDigitsBeforeDecimal + nCounter, 1);
3577             nCounter++;
3578             j++;
3579             if (j >= nGrouping)
3580             {
3581                 j = 0;
3582                 if (nRuleIndex < sRule[0])
3583                     nRuleIndex++;
3584                 nGrouping = OLE_GetGrouping(sRule, nRuleIndex);
3585                 memcpy(sDestination + i+1, sDigitGroupSymbol, strlen(sDigitGroupSymbol));
3586                 nStep+= strlen(sDigitGroupSymbol);
3587             }
3588
3589             nNumberDecimal--;
3590         }
3591
3592         memcpy(sDestination + i+1, "\0", 1);
3593         /* Get the string in the right order ! */
3594         OLE_InvertString(sDestination);
3595      }
3596      else
3597      {
3598         nLZ = atoi(sILZero);
3599         if (nLZ != 0)
3600         {        
3601             /* Use 0.xxx instead of .xxx */
3602             memcpy(sDestination, "0", 1);
3603             memcpy(sDestination+1, "\0", 1);
3604         }
3605         else
3606             memcpy(sDestination, "\0", 1);
3607
3608      }
3609
3610     /* Second, format the digits after the decimal. */
3611     j = 0;
3612     nCounter = nNumberDigits;
3613     if ( (nDigits>0) && (pStr = strstr (sNumber, ".")) )
3614     {
3615         i = strlen(sNumber) - strlen(pStr) + 1;        
3616         strncpy ( sDigitsAfterDecimal, sNumber + i, nNumberDigits);
3617         j = strlen(sDigitsAfterDecimal);
3618         if (j < nNumberDigits)
3619             nCounter = nNumberDigits-j;            
3620     }
3621     for (i=0;i<nCounter;i++)
3622          memcpy(sDigitsAfterDecimal+i+j, "0", 1);    
3623     memcpy(sDigitsAfterDecimal + nNumberDigits, "\0", 1);    
3624
3625     i = strlen(sDestination);
3626     j = strlen(sDigitsAfterDecimal);
3627     /* Finally, construct the resulting formatted string. */
3628         
3629     for (nCounter=0; nCounter<i; nCounter++)
3630         memcpy(sNumber + nCounter, sDestination + nCounter, 1);
3631        
3632     memcpy(sNumber + nCounter, sDecimalSymbol, strlen(sDecimalSymbol));
3633
3634     for (i=0; i<j; i++)
3635         memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), sDigitsAfterDecimal + i, 1);
3636     memcpy(sNumber + nCounter+i+strlen(sDecimalSymbol), "\0", 1);
3637         
3638     /* Is it a negative number */
3639     if (bNegative == TRUE)
3640     {
3641         i = atoi(sNegNumber);
3642         pStr = sDestination;
3643         pTmpStr = sNumber;
3644         switch (i)
3645         {          
3646         case 0:
3647             *pStr++ = '(';
3648             while (*sNumber != '\0')
3649                 *pStr++ =  *pTmpStr++;
3650             *pStr++ = ')';                
3651             break;  
3652         case 1:
3653             *pStr++ = '-';
3654             while (*pTmpStr != '\0')
3655                 *pStr++ =  *pTmpStr++;
3656             break;
3657         case 2:
3658             *pStr++ = '-';
3659             *pStr++ = ' ';
3660             while (*pTmpStr != '\0')
3661                 *pStr++ =  *pTmpStr++;
3662             break;
3663         case 3:
3664             while (*pTmpStr != '\0')
3665                 *pStr++ =  *pTmpStr++;
3666             *pStr++ = '-';
3667             break;
3668         case 4:
3669             while (*pTmpStr != '\0')
3670                 *pStr++ =  *pTmpStr++;
3671             *pStr++ = ' ';
3672             *pStr++ = '-';
3673             break;
3674         default:
3675             while (*pTmpStr != '\0')
3676                 *pStr++ =  *pTmpStr++;
3677             break;
3678         }
3679     }
3680     else
3681         strcpy(sDestination, sNumber);
3682
3683     /* If cchNumber is zero, then returns the number of bytes or characters 
3684      * required to hold the formatted number string
3685      */
3686     if (cchNumber==0)
3687         retVal = strlen(sDestination) + 1;
3688     else           
3689     {
3690         strncpy (lpNumberStr, sDestination, cchNumber-1);
3691         *(lpNumberStr+cchNumber-1) = '\0';   /* ensure we got a NULL at the end */
3692         retVal = strlen(lpNumberStr);
3693     }
3694           
3695     return retVal;
3696 }
3697
3698 /**************************************************************************
3699  *              GetNumberFormatW        (KERNEL32.xxx)
3700  */
3701 INT WINAPI GetNumberFormatW(LCID locale, DWORD dwflags,
3702                                LPCWSTR lpvalue,  const NUMBERFMTW * lpFormat,
3703                                LPWSTR lpNumberStr, int cchNumber)
3704 {
3705  FIXME("%s: stub, no reformating done\n",debugstr_w(lpvalue));
3706
3707  lstrcpynW( lpNumberStr, lpvalue, cchNumber );
3708  return cchNumber? lstrlenW( lpNumberStr ) : 0;
3709 }
3710
3711 /**************************************************************************
3712  *              GetCurrencyFormatA      (KERNEL32.302)
3713  */
3714 INT WINAPI GetCurrencyFormatA(LCID locale, DWORD dwflags,
3715                                LPCSTR lpvalue,   const CURRENCYFMTA * lpFormat,
3716                                LPSTR lpCurrencyStr, int cchCurrency)
3717 {
3718     UINT   nPosOrder, nNegOrder;
3719     INT    retVal;
3720     char   sDestination[128], sNegOrder[8], sPosOrder[8], sCurrencySymbol[8];
3721     char   *pDestination = sDestination;
3722     char   sNumberFormated[128];
3723     char   *pNumberFormated = sNumberFormated;
3724     LCID   systemDefaultLCID;
3725     BOOL   bIsPositive = FALSE, bValidFormat = FALSE;
3726     enum   Operations 
3727     {
3728         USE_PARAMETER,
3729         USE_LOCALEINFO,
3730         USE_SYSTEMDEFAULT,
3731         RETURN_ERROR
3732     } used_operation;
3733
3734     NUMBERFMTA  numberFmt;
3735
3736     /* Which source will we use to format the string */
3737     used_operation = RETURN_ERROR;
3738     if (lpFormat != NULL)
3739     {
3740         if (dwflags == 0)
3741             used_operation = USE_PARAMETER; 
3742     }
3743     else
3744     {
3745         if (dwflags & LOCALE_NOUSEROVERRIDE)
3746             used_operation = USE_LOCALEINFO;
3747         else
3748             used_operation = USE_SYSTEMDEFAULT;
3749     }
3750
3751     /* Load the fields we need */
3752     switch(used_operation)
3753     {
3754     case USE_LOCALEINFO:        
3755         /* Specific to CURRENCYFMTA*/
3756         GetLocaleInfoA(locale, LOCALE_INEGCURR, sNegOrder, sizeof(sNegOrder));
3757         GetLocaleInfoA(locale, LOCALE_ICURRENCY, sPosOrder, sizeof(sPosOrder));
3758         GetLocaleInfoA(locale, LOCALE_SCURRENCY, sCurrencySymbol, sizeof(sCurrencySymbol));
3759         
3760         nPosOrder = atoi(sPosOrder);
3761         nNegOrder = atoi(sNegOrder);
3762         break;
3763     case USE_PARAMETER:        
3764         /* Specific to CURRENCYFMTA*/
3765         nNegOrder = lpFormat->NegativeOrder;
3766         nPosOrder = lpFormat->PositiveOrder;
3767         strcpy(sCurrencySymbol, lpFormat->lpCurrencySymbol);
3768         break;
3769     case USE_SYSTEMDEFAULT:
3770         systemDefaultLCID = GetSystemDefaultLCID();        
3771         /* Specific to CURRENCYFMTA*/
3772         GetLocaleInfoA(systemDefaultLCID, LOCALE_INEGCURR, sNegOrder, sizeof(sNegOrder));
3773         GetLocaleInfoA(systemDefaultLCID, LOCALE_ICURRENCY, sPosOrder, sizeof(sPosOrder));
3774         GetLocaleInfoA(systemDefaultLCID, LOCALE_SCURRENCY, sCurrencySymbol, sizeof(sCurrencySymbol));
3775         
3776         nPosOrder = atoi(sPosOrder);
3777         nNegOrder = atoi(sNegOrder);
3778         break;
3779     default:
3780         SetLastError(ERROR_INVALID_PARAMETER);
3781         return 0;
3782     }
3783     
3784     /* Construct a temporary number format structure */
3785     if (lpFormat != NULL)
3786     {
3787         numberFmt.NumDigits     = lpFormat->NumDigits;
3788         numberFmt.LeadingZero   = lpFormat->LeadingZero;
3789         numberFmt.Grouping      = lpFormat->Grouping;
3790         numberFmt.NegativeOrder = 0;
3791         numberFmt.lpDecimalSep = lpFormat->lpDecimalSep;
3792         numberFmt.lpThousandSep = lpFormat->lpThousandSep;
3793         bValidFormat = TRUE;
3794     }
3795
3796     /* Make a call to GetNumberFormatA() */
3797     if (*lpvalue == '-')
3798     {
3799         bIsPositive = FALSE;
3800         retVal = GetNumberFormatA(locale,0,lpvalue+1,(bValidFormat)?&numberFmt:NULL,pNumberFormated,128);
3801     }
3802     else
3803     {
3804         bIsPositive = TRUE;
3805         retVal = GetNumberFormatA(locale,0,lpvalue,(bValidFormat)?&numberFmt:NULL,pNumberFormated,128);
3806     }
3807         
3808     if (retVal == 0)
3809         return 0;
3810
3811     /* construct the formatted string */
3812     if (bIsPositive)
3813     {
3814         switch (nPosOrder)
3815         {
3816         case 0:   /* Prefix, no separation */
3817             strcpy (pDestination, sCurrencySymbol);
3818             strcat (pDestination, pNumberFormated);
3819             break;
3820         case 1:   /* Suffix, no separation */
3821             strcpy (pDestination, pNumberFormated);
3822             strcat (pDestination, sCurrencySymbol);
3823             break;
3824         case 2:   /* Prefix, 1 char separation */
3825             strcpy (pDestination, sCurrencySymbol);
3826             strcat (pDestination, " ");
3827             strcat (pDestination, pNumberFormated);
3828             break;
3829         case 3:   /* Suffix, 1 char separation */
3830             strcpy (pDestination, pNumberFormated);
3831             strcat (pDestination, " ");
3832             strcat (pDestination, sCurrencySymbol);
3833             break;
3834         default:
3835             SetLastError(ERROR_INVALID_PARAMETER);
3836             return 0;
3837         }
3838     }
3839     else  /* negative number */
3840     {
3841         switch (nNegOrder)
3842         {
3843         case 0:   /* format: ($1.1) */
3844             strcpy (pDestination, "(");
3845             strcat (pDestination, sCurrencySymbol);
3846             strcat (pDestination, pNumberFormated);
3847             strcat (pDestination, ")");
3848             break;
3849         case 1:   /* format: -$1.1 */
3850             strcpy (pDestination, "-");
3851             strcat (pDestination, sCurrencySymbol);
3852             strcat (pDestination, pNumberFormated);
3853             break;
3854         case 2:   /* format: $-1.1 */
3855             strcpy (pDestination, sCurrencySymbol);
3856             strcat (pDestination, "-");
3857             strcat (pDestination, pNumberFormated);
3858             break;
3859         case 3:   /* format: $1.1- */
3860             strcpy (pDestination, sCurrencySymbol);
3861             strcat (pDestination, pNumberFormated);
3862             strcat (pDestination, "-");
3863             break;
3864         case 4:   /* format: (1.1$) */
3865             strcpy (pDestination, "(");
3866             strcat (pDestination, pNumberFormated);
3867             strcat (pDestination, sCurrencySymbol);
3868             strcat (pDestination, ")");
3869             break;
3870         case 5:   /* format: -1.1$ */
3871             strcpy (pDestination, "-");
3872             strcat (pDestination, pNumberFormated);
3873             strcat (pDestination, sCurrencySymbol);
3874             break;
3875         case 6:   /* format: 1.1-$ */
3876             strcpy (pDestination, pNumberFormated);
3877             strcat (pDestination, "-");
3878             strcat (pDestination, sCurrencySymbol);
3879             break;
3880         case 7:   /* format: 1.1$- */
3881             strcpy (pDestination, pNumberFormated);
3882             strcat (pDestination, sCurrencySymbol);
3883             strcat (pDestination, "-");
3884             break;
3885         case 8:   /* format: -1.1 $ */
3886             strcpy (pDestination, "-");
3887             strcat (pDestination, pNumberFormated);
3888             strcat (pDestination, " ");
3889             strcat (pDestination, sCurrencySymbol);
3890             break;
3891         case 9:   /* format: -$ 1.1 */
3892             strcpy (pDestination, "-");
3893             strcat (pDestination, sCurrencySymbol);
3894             strcat (pDestination, " ");
3895             strcat (pDestination, pNumberFormated);
3896             break;
3897         case 10:   /* format: 1.1 $- */
3898             strcpy (pDestination, pNumberFormated);
3899             strcat (pDestination, " ");
3900             strcat (pDestination, sCurrencySymbol);
3901             strcat (pDestination, "-");
3902             break;
3903         case 11:   /* format: $ 1.1- */
3904             strcpy (pDestination, sCurrencySymbol);
3905             strcat (pDestination, " ");
3906             strcat (pDestination, pNumberFormated);
3907             strcat (pDestination, "-");
3908             break;
3909         case 12:   /* format: $ -1.1 */
3910             strcpy (pDestination, sCurrencySymbol);
3911             strcat (pDestination, " ");
3912             strcat (pDestination, "-");
3913             strcat (pDestination, pNumberFormated);
3914             break;
3915         case 13:   /* format: 1.1- $ */
3916             strcpy (pDestination, pNumberFormated);
3917             strcat (pDestination, "-");
3918             strcat (pDestination, " ");
3919             strcat (pDestination, sCurrencySymbol);
3920             break;
3921         case 14:   /* format: ($ 1.1) */
3922             strcpy (pDestination, "("); 
3923             strcat (pDestination, sCurrencySymbol);
3924             strcat (pDestination, " ");
3925             strcat (pDestination, pNumberFormated);
3926             strcat (pDestination, ")");
3927             break;
3928         case 15:   /* format: (1.1 $) */
3929             strcpy (pDestination, "("); 
3930             strcat (pDestination, pNumberFormated);
3931             strcat (pDestination, " ");
3932             strcat (pDestination, sCurrencySymbol);
3933             strcat (pDestination, ")");
3934             break;
3935         default:
3936             SetLastError(ERROR_INVALID_PARAMETER);
3937             return 0;
3938         }
3939     }
3940
3941     if (cchCurrency == 0)
3942         return strlen(pDestination) + 1;
3943
3944     else
3945     {
3946         strncpy (lpCurrencyStr, pDestination, cchCurrency-1);
3947         *(lpCurrencyStr+cchCurrency-1) = '\0';   /* ensure we got a NULL at the end */
3948         return  strlen(lpCurrencyStr);
3949     }    
3950 }
3951
3952 /**************************************************************************
3953  *              GetCurrencyFormatW      (KERNEL32.303)
3954  */
3955 INT WINAPI GetCurrencyFormatW(LCID locale, DWORD dwflags,
3956                                LPCWSTR lpvalue,   const CURRENCYFMTW * lpFormat,
3957                                LPWSTR lpCurrencyStr, int cchCurrency)
3958 {
3959     FIXME("This API function is NOT implemented !\n");
3960     return 0;
3961 }
3962
3963 /******************************************************************************
3964  *              OLE2NLS_CheckLocale     [intern]
3965  */ 
3966 static LCID OLE2NLS_CheckLocale (LCID locale)
3967 {
3968         if (!locale) 
3969         { locale = LOCALE_SYSTEM_DEFAULT;
3970         }
3971   
3972         if (locale == LOCALE_SYSTEM_DEFAULT) 
3973         { return GetSystemDefaultLCID();
3974         } 
3975         else if (locale == LOCALE_USER_DEFAULT) 
3976         { return GetUserDefaultLCID();
3977         }
3978         else
3979         { return locale;
3980         }
3981 }
3982 /******************************************************************************
3983  *              GetTimeFormatA  [KERNEL32.422]
3984  * Makes an ASCII string of the time
3985  *
3986  * Formats date according to format,  or locale default if format is
3987  * NULL. The format consists of literal characters and fields as follows:
3988  *
3989  * h  hours with no leading zero (12-hour)
3990  * hh hours with full two digits
3991  * H  hours with no leading zero (24-hour)
3992  * HH hours with full two digits
3993  * m  minutes with no leading zero
3994  * mm minutes with full two digits
3995  * s  seconds with no leading zero
3996  * ss seconds with full two digits
3997  * t  time marker (A or P)
3998  * tt time marker (AM, PM)
3999  *
4000  */
4001 INT WINAPI 
4002 GetTimeFormatA(LCID locale,        /* in  */
4003                  DWORD flags,        /* in  */
4004                  LPSYSTEMTIME xtime, /* in  */ 
4005                  LPCSTR format,      /* in  */
4006                  LPSTR timestr,      /* out */
4007                  INT timelen       /* in  */) 
4008 { char format_buf[40];
4009   LPCSTR thisformat;
4010   SYSTEMTIME t;
4011   LPSYSTEMTIME thistime;
4012   LCID thislocale=0;
4013   DWORD thisflags=LOCALE_STIMEFORMAT; /* standart timeformat */
4014   INT ret;
4015     
4016   TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,xtime,format,timestr,timelen);
4017
4018   thislocale = OLE2NLS_CheckLocale ( locale );
4019
4020   if ( flags & (TIME_NOTIMEMARKER | TIME_FORCE24HOURFORMAT ))
4021   { FIXME("TIME_NOTIMEMARKER or TIME_FORCE24HOURFORMAT not implemented\n");
4022   }
4023   
4024   flags &= (TIME_NOSECONDS | TIME_NOMINUTESORSECONDS); /* mask for OLE_GetFormatA*/
4025
4026   if (format == NULL) 
4027   { if (flags & LOCALE_NOUSEROVERRIDE)  /*use system default*/
4028     { thislocale = GetSystemDefaultLCID();
4029     }
4030     GetLocaleInfoA(thislocale, thisflags, format_buf, sizeof(format_buf));
4031     thisformat = format_buf;
4032   }
4033   else 
4034   { thisformat = format;
4035   }
4036   
4037   if (xtime == NULL) /* NULL means use the current local time*/
4038   { GetLocalTime(&t);
4039     thistime = &t;
4040   } 
4041   else
4042   { thistime = xtime;
4043   }
4044   ret = OLE_GetFormatA(thislocale, thisflags, flags, thistime, thisformat,
4045                          timestr, timelen);
4046   return ret;
4047 }
4048
4049
4050 /******************************************************************************
4051  *              GetTimeFormatW  [KERNEL32.423]
4052  * Makes a Unicode string of the time
4053  */
4054 INT WINAPI 
4055 GetTimeFormatW(LCID locale,        /* in  */
4056                  DWORD flags,        /* in  */
4057                  LPSYSTEMTIME xtime, /* in  */ 
4058                  LPCWSTR format,     /* in  */
4059                  LPWSTR timestr,     /* out */
4060                  INT timelen       /* in  */) 
4061 {       WCHAR format_buf[40];
4062         LPCWSTR thisformat;
4063         SYSTEMTIME t;
4064         LPSYSTEMTIME thistime;
4065         LCID thislocale=0;
4066         DWORD thisflags=LOCALE_STIMEFORMAT; /* standart timeformat */
4067         INT ret;
4068             
4069         TRACE("GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,
4070         xtime,debugstr_w(format),timestr,timelen);
4071
4072         thislocale = OLE2NLS_CheckLocale ( locale );
4073
4074         if ( flags & (TIME_NOTIMEMARKER | TIME_FORCE24HOURFORMAT ))
4075         { FIXME("TIME_NOTIMEMARKER or TIME_FORCE24HOURFORMAT not implemented\n");
4076         }
4077   
4078         flags &= (TIME_NOSECONDS | TIME_NOMINUTESORSECONDS); /* mask for OLE_GetFormatA*/
4079
4080         if (format == NULL) 
4081         { if (flags & LOCALE_NOUSEROVERRIDE)  /*use system default*/
4082           { thislocale = GetSystemDefaultLCID();
4083           }
4084           GetLocaleInfoW(thislocale, thisflags, format_buf, 40);
4085           thisformat = format_buf;
4086         }         
4087         else 
4088         { thisformat = format;
4089         }
4090  
4091         if (xtime == NULL) /* NULL means use the current local time*/
4092         { GetSystemTime(&t);
4093           thistime = &t;
4094         } 
4095         else
4096         { thistime = xtime;
4097         }
4098
4099         ret = OLE_GetFormatW(thislocale, thisflags, flags, thistime, thisformat,
4100                          timestr, timelen);
4101         return ret;
4102 }
4103
4104 /******************************************************************************
4105  *              EnumCalendarInfoA       [KERNEL32.196]
4106  */
4107 BOOL WINAPI EnumCalendarInfoA(
4108         CALINFO_ENUMPROCA calinfoproc,LCID locale,CALID calendar,CALTYPE caltype
4109 ) {
4110         FIXME("(%p,0x%04lx,0x%08lx,0x%08lx),stub!\n",calinfoproc,locale,calendar,caltype);
4111         return FALSE;
4112 }