Release 980712
[wine] / ole / ole2nls.c
1 /*
2  *      OLE2NLS library
3  *
4  *      Copyright 1995  Martin von Loewis
5  *      Copyright 1998  David Lee Lambert
6  */
7
8 #include <string.h>
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include "windows.h"
12 #include "heap.h"
13 #include "ole.h"
14 #include "options.h"
15 #include "winnls.h"
16 #include "winreg.h"
17 #include "winerror.h"
18 #include "debug.h"
19
20 /* Locale name to id map. used by EnumSystemLocales, GetLocalInfoA 
21  * MUST contain all #defines from winnls.h
22  * last entry has NULL name, 0 id.
23  */ 
24 #define LOCALE_ENTRY(x) {#x,LOCALE_##x}
25 static struct tagLOCALE_NAME2ID {
26         char    *name;
27         DWORD   id;
28 } locale_name2id[]= {
29         LOCALE_ENTRY(ILANGUAGE),
30         LOCALE_ENTRY(SLANGUAGE),
31         LOCALE_ENTRY(SENGLANGUAGE),
32         LOCALE_ENTRY(SABBREVLANGNAME),
33         LOCALE_ENTRY(SNATIVELANGNAME),
34         LOCALE_ENTRY(ICOUNTRY),
35         LOCALE_ENTRY(SCOUNTRY),
36         LOCALE_ENTRY(SENGCOUNTRY),
37         LOCALE_ENTRY(SABBREVCTRYNAME),
38         LOCALE_ENTRY(SNATIVECTRYNAME),
39         LOCALE_ENTRY(IDEFAULTLANGUAGE),
40         LOCALE_ENTRY(IDEFAULTCOUNTRY),
41         LOCALE_ENTRY(IDEFAULTCODEPAGE),
42         LOCALE_ENTRY(IDEFAULTANSICODEPAGE),
43         LOCALE_ENTRY(SLIST),
44         LOCALE_ENTRY(IMEASURE),
45         LOCALE_ENTRY(SDECIMAL),
46         LOCALE_ENTRY(STHOUSAND),
47         LOCALE_ENTRY(SGROUPING),
48         LOCALE_ENTRY(IDIGITS),
49         LOCALE_ENTRY(ILZERO),
50         LOCALE_ENTRY(INEGNUMBER),
51         LOCALE_ENTRY(SNATIVEDIGITS),
52         LOCALE_ENTRY(SCURRENCY),
53         LOCALE_ENTRY(SINTLSYMBOL),
54         LOCALE_ENTRY(SMONDECIMALSEP),
55         LOCALE_ENTRY(SMONTHOUSANDSEP),
56         LOCALE_ENTRY(SMONGROUPING),
57         LOCALE_ENTRY(ICURRDIGITS),
58         LOCALE_ENTRY(IINTLCURRDIGITS),
59         LOCALE_ENTRY(ICURRENCY),
60         LOCALE_ENTRY(INEGCURR),
61         LOCALE_ENTRY(SDATE),
62         LOCALE_ENTRY(STIME),
63         LOCALE_ENTRY(SSHORTDATE),
64         LOCALE_ENTRY(SLONGDATE),
65         LOCALE_ENTRY(STIMEFORMAT),
66         LOCALE_ENTRY(IDATE),
67         LOCALE_ENTRY(ILDATE),
68         LOCALE_ENTRY(ITIME),
69         LOCALE_ENTRY(ITIMEMARKPOSN),
70         LOCALE_ENTRY(ICENTURY),
71         LOCALE_ENTRY(ITLZERO),
72         LOCALE_ENTRY(IDAYLZERO),
73         LOCALE_ENTRY(IMONLZERO),
74         LOCALE_ENTRY(S1159),
75         LOCALE_ENTRY(S2359),
76         LOCALE_ENTRY(ICALENDARTYPE),
77         LOCALE_ENTRY(IOPTIONALCALENDAR),
78         LOCALE_ENTRY(IFIRSTDAYOFWEEK),
79         LOCALE_ENTRY(IFIRSTWEEKOFYEAR),
80         LOCALE_ENTRY(SDAYNAME1),
81         LOCALE_ENTRY(SDAYNAME2),
82         LOCALE_ENTRY(SDAYNAME3),
83         LOCALE_ENTRY(SDAYNAME4),
84         LOCALE_ENTRY(SDAYNAME5),
85         LOCALE_ENTRY(SDAYNAME6),
86         LOCALE_ENTRY(SDAYNAME7),
87         LOCALE_ENTRY(SABBREVDAYNAME1),
88         LOCALE_ENTRY(SABBREVDAYNAME2),
89         LOCALE_ENTRY(SABBREVDAYNAME3),
90         LOCALE_ENTRY(SABBREVDAYNAME4),
91         LOCALE_ENTRY(SABBREVDAYNAME5),
92         LOCALE_ENTRY(SABBREVDAYNAME6),
93         LOCALE_ENTRY(SABBREVDAYNAME7),
94         LOCALE_ENTRY(SMONTHNAME1),
95         LOCALE_ENTRY(SMONTHNAME2),
96         LOCALE_ENTRY(SMONTHNAME3),
97         LOCALE_ENTRY(SMONTHNAME4),
98         LOCALE_ENTRY(SMONTHNAME5),
99         LOCALE_ENTRY(SMONTHNAME6),
100         LOCALE_ENTRY(SMONTHNAME7),
101         LOCALE_ENTRY(SMONTHNAME8),
102         LOCALE_ENTRY(SMONTHNAME9),
103         LOCALE_ENTRY(SMONTHNAME10),
104         LOCALE_ENTRY(SMONTHNAME11),
105         LOCALE_ENTRY(SMONTHNAME12),
106         LOCALE_ENTRY(SMONTHNAME13),
107         LOCALE_ENTRY(SABBREVMONTHNAME1),
108         LOCALE_ENTRY(SABBREVMONTHNAME2),
109         LOCALE_ENTRY(SABBREVMONTHNAME3),
110         LOCALE_ENTRY(SABBREVMONTHNAME4),
111         LOCALE_ENTRY(SABBREVMONTHNAME5),
112         LOCALE_ENTRY(SABBREVMONTHNAME6),
113         LOCALE_ENTRY(SABBREVMONTHNAME7),
114         LOCALE_ENTRY(SABBREVMONTHNAME8),
115         LOCALE_ENTRY(SABBREVMONTHNAME9),
116         LOCALE_ENTRY(SABBREVMONTHNAME10),
117         LOCALE_ENTRY(SABBREVMONTHNAME11),
118         LOCALE_ENTRY(SABBREVMONTHNAME12),
119         LOCALE_ENTRY(SABBREVMONTHNAME13),
120         LOCALE_ENTRY(SPOSITIVESIGN),
121         LOCALE_ENTRY(SNEGATIVESIGN),
122         LOCALE_ENTRY(IPOSSIGNPOSN),
123         LOCALE_ENTRY(INEGSIGNPOSN),
124         LOCALE_ENTRY(IPOSSYMPRECEDES),
125         LOCALE_ENTRY(IPOSSEPBYSPACE),
126         LOCALE_ENTRY(INEGSYMPRECEDES),
127         LOCALE_ENTRY(INEGSEPBYSPACE),
128 /*      LOCALE_ENTRY(FONTSIGNATURE),*/
129         {NULL,0},
130 };
131
132 const struct map_lcid2str {
133         LCID            langid;
134         const char      *langname;
135 } languages[]={
136         {0x0401,"Arabisch"},
137         {0x0402,"Bulgarisch"},
138         {0x0403,"Katalanisch"},
139         {0x0404,"Traditionales Chinesisch"},
140         {0x0405,"Tschecisch"},
141         {0x0406,"Dänisch"},
142         {0x0407,"Deutsch"},
143         {0x0408,"Griechisch"},
144         {0x0409,"Amerikanisches Englisch"},
145         {0x040A,"Kastilisches Spanisch"},
146         {0x040B,"Finnisch"},
147         {0x040C,"Franzvsisch"},
148         {0x040D,"Hebrdisch"},
149         {0x040E,"Ungarisch"},
150         {0x040F,"Isldndisch"},
151         {0x0410,"Italienisch"},
152         {0x0411,"Japanisch"},
153         {0x0412,"Koreanisch"},
154         {0x0413,"Niederldndisch"},
155         {0x0414,"Norwegisch-Bokmal"},
156         {0x0415,"Polnisch"},
157         {0x0416,"Brasilianisches Portugiesisch"},
158         {0x0417,"Rdtoromanisch"},
159         {0x0418,"Rumdnisch"},
160         {0x0419,"Russisch"},
161         {0x041A,"Kroatoserbisch (lateinisch)"},
162         {0x041B,"Slowenisch"},
163         {0x041C,"Albanisch"},
164         {0x041D,"Schwedisch"},
165         {0x041E,"Thai"},
166         {0x041F,"T|rkisch"},
167         {0x0420,"Urdu"},
168         {0x0421,"Bahasa"},
169         {0x0804,"Vereinfachtes Chinesisch"},
170         {0x0807,"Schweizerdeutsch"},
171         {0x0809,"Britisches Englisch"},
172         {0x080A,"Mexikanisches Spanisch"},
173         {0x080C,"Belgisches Franzvsisch"},
174         {0x0810,"Schweizerisches Italienisch"},
175         {0x0813,"Belgisches Niederldndisch"},
176         {0x0814,"Norgwegisch-Nynorsk"},
177         {0x0816,"Portugiesisch"},
178         {0x081A,"Serbokratisch (kyrillisch)"},
179         {0x0C1C,"Kanadisches Franzvsisch"},
180         {0x100C,"Schweizerisches Franzvsisch"},
181         {0x0000,"Unbekannt"},
182 };
183
184 /***********************************************************************
185  *           GetUserDefaultLCID       (OLE2NLS.1)
186  */
187 LCID WINAPI GetUserDefaultLCID()
188 {
189 /* Default sorting, neutral sublanguage */
190     switch(Options.language)
191     {
192  /* case LANG_Bu: return 0x02; */      /* Bulgarian */
193  /* case LANG_Ch: return 0x04; */      /* Chinese */
194     case LANG_Cz: return 0x05;         /* Czech */
195     case LANG_Da: return 0x06;         /* Danish */
196     case LANG_De: return 0x07;         /* German */
197  /* case LANG_Gr: return 0x08; */      /* Greek */
198     case LANG_En: return 0x09;         /* English */
199     case LANG_Es: return 0x0a;         /* Spanish */
200     case LANG_Fi: return 0x0b;         /* Finnish */
201     case LANG_Fr: return 0x0c;         /* French */
202     case LANG_Hu: return 0x0e;         /* Hungarian */
203  /* case LANG_Ic: return 0x0f; */      /* Icelandic */
204     case LANG_It: return 0x10;         /* Italian */
205  /* case LANG_Jp: return 0x11; */      /* Japanese */
206     case LANG_Ko: return 0x12;         /* Korean */
207  /* case LANG_Du: return 0x13; */      /* Dutch */
208     case LANG_No: return 0x14;         /* Norwegian */
209     case LANG_Pl: return 0x15;         /* Polish */
210     case LANG_Po: return 0x16;         /* Portuguese */
211  /* case LANG_Ro: return 0x18; */      /* Romanian */
212  /* case LANG_Ru: return 0x19; */      /* Russian */
213  /* case LANG_Cr: return 0x1a; */      /* Croatian */
214  /* case LANG_Sl: return 0x1b; */      /* Slovak */
215     case LANG_Sw: return 0x1d;         /* Swedish */
216  /* case LANG_Tu: return 0x1f; */      /* Turkish */
217  /* case LANG_Sv: return 0x24; */      /* Slovenian */
218     case LANG_Eo: return 0x25;         /* Esperanto (not official) */
219     case LANG_Ca: return 0x26;         /* Catalan */
220
221     default:
222         return 0x00;                   /* Neutral language */
223     }
224 }
225
226 /***********************************************************************
227  *         GetSystemDefaultLCID       (OLE2NLS.2)
228  */
229 LCID WINAPI GetSystemDefaultLCID()
230 {
231         return GetUserDefaultLCID();
232 }
233
234 /***********************************************************************
235  *         GetUserDefaultLangID       (OLE2NLS.3)
236  */
237 LANGID WINAPI GetUserDefaultLangID()
238 {
239         return (WORD)GetUserDefaultLCID();
240 }
241
242 /***********************************************************************
243  *         GetSystemDefaultLangID     (OLE2NLS.4)
244  */
245 LANGID WINAPI GetSystemDefaultLangID()
246 {
247         return GetUserDefaultLangID();
248 }
249
250 /***********************************************************************
251  *         GetLocaleInfoA             (OLE2NLS.5)
252  * Is the last parameter really WORD for Win16?
253  */
254 INT16 WINAPI GetLocaleInfo16(LCID lcid,LCTYPE LCType,LPSTR buf,INT16 len)
255 {
256         return GetLocaleInfo32A(lcid,LCType,buf,len);
257 }
258
259 INT32 WINAPI GetLocaleInfo32A(LCID lcid,LCTYPE LCType,LPSTR buf,INT32 len)
260 {
261         char    *retString;
262         int     found,i;
263
264         TRACE(ole,"(%8lX,%8lX,%p,%4X)\n",
265                         lcid,LCType,buf,len);
266
267         LCType &= ~(LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP);
268
269         /* As an option, we could obtain the value from win.ini.
270            This would not match the Wine compile-time option.
271            Also, not all identifiers are available from win.ini */
272         retString=0;
273         /* If we are through all of this, retLen should not be zero anymore.
274            If it is, the value is not supported */
275         i=0;
276         while (locale_name2id[i].name!=NULL) {
277                 if (LCType == locale_name2id[i].id) {
278                         retString = locale_name2id[i].name;
279                         break;
280                 }
281                 i++;
282         }
283         if (!retString) {
284                 WARN(ole,"Unkown LC type %lX\n",LCType);
285                 return 0;
286         }
287
288 #define LOCVAL(type,value) case type:retString=value;break;
289
290 /* Now, the language specific definitions. They don't have to be
291    complete */
292     found=1;
293     switch(Options.language)
294     {
295     case LANG_De:
296         switch (LCType) {
297 #include "nls/deu.nls"
298         default: found=0;break;
299         }
300     break;  /* LANG(De) */
301
302     case LANG_Da:
303         switch (LCType) {
304 #include "nls/dnk.nls"
305         default: found=0;break;
306         }
307     break; /* LANG(Da) */
308
309     case LANG_En:
310         switch (LCType) {
311 #include "nls/enu.nls"
312         default: found=0;break;
313         }
314     break;  /* LANG(En) */
315
316     case LANG_Eo:
317         switch (LCType) {
318 #include "nls/esperanto.nls"
319         default: found=0;break;
320         }
321     break;  /* LANG(Eo) */
322
323     case LANG_Fi:
324         switch (LCType) {
325 #include "nls/fin.nls"
326         default: found=0;break;
327         }
328     break;  /* LANG(Fi) */
329
330     case LANG_It:
331         switch (LCType) {
332 #include "nls/ita.nls"
333         default: found=0;break;
334         }
335     break;  /* LANG(It) */
336
337     case 0x0809:
338         switch (LCType) {
339 #include "nls/eng.nls"
340         default: found=0;break;
341         }
342     break; /* LANG(0x0809) (U.K. English) */
343
344     case LANG_Ko: /* string using codepage 949 */
345         switch (LCType) {
346 #include "nls/kor.nls"
347         default: found=0;break;
348         }
349     break;  /* LANG(Ko) */
350
351     case LANG_Hu:
352         switch (LCType) {
353 #include "nls/hun.nls"
354         default: found=0;break;
355         }
356     break;  /* LANG(En) */
357
358     case LANG_Pl:
359         switch (LCType) {
360 #include "nls/plk.nls"
361         default: found=0;break;
362         }
363     break;  /* LANG(Pl) */
364
365     case LANG_Po:
366         switch (LCType) {
367 #include "nls/ptb.nls"
368         default: found=0;break;
369         }
370     break; /* LANG(Po) */
371
372     case LANG_Sw:
373         switch (LCType) {
374 #include "nls/sve.nls"
375         default: found=0;break;
376         }
377     break; /* LANG(Sw) */
378
379 /*Insert other languages here*/
380
381     default:
382         found=0;
383         break;
384     }  /* switch */
385
386         if(!found) {
387                 ERR(ole,"'%s' not supported for your language.\n",
388                         retString);
389                 retString = "<WINE-NLS-unknown>";
390                 /*return 0;*/
391         }
392         if (buf)
393                 lstrcpyn32A(buf,retString,len);
394         return strlen(retString)+1;
395 }
396
397 /***********************************************************************
398  *         GetLocaleInfo32W             (KERNEL32.230)
399  * Is the last parameter really WORD for Win16?
400  */
401 INT32 WINAPI GetLocaleInfo32W(LCID lcid,LCTYPE LCType,LPWSTR wbuf,INT32 len)
402 {
403         LPSTR abuf = (LPSTR)HeapAlloc(GetProcessHeap(),0,len);
404
405         INT32 n = GetLocaleInfo32A(lcid, LCType, abuf, len);
406         if (wbuf)
407                 lstrcpynAtoW(wbuf,abuf,len);
408         HeapFree(GetProcessHeap(),0,abuf);
409         return n;
410 }
411
412 /***********************************************************************
413  *           SetLocaleInfoA       [KERNEL32.499]
414  */
415 BOOL16 WINAPI SetLocaleInfoA(DWORD lcid, DWORD lctype, LPCSTR data)
416 {
417     FIXME(ole,"(%ld,%ld,%s): stub\n",lcid,lctype,data);
418     return TRUE;
419 }
420
421 /***********************************************************************
422  *           IsValidLocale       [KERNEL32.361]
423  */
424 BOOL32 WINAPI IsValidLocale(LCID lcid,DWORD flags)
425 {
426         /* we support ANY language. Well, at least say that...*/
427         return TRUE;
428 }
429
430 /***********************************************************************
431  *              EnumSystemLocales32W                [KERNEL32.93]
432  */
433 BOOL32 WINAPI EnumSystemLocales32W( LOCALE_ENUMPROC32W lpfnLocaleEnum,
434                                     DWORD flags )
435 {
436         int     i;
437         BOOL32  ret;
438         WCHAR   buffer[200];
439         HKEY    xhkey;
440
441         TRACE(win32,"(%p,%08lx)\n",lpfnLocaleEnum,flags );
442         /* see if we can reuse the Win95 registry entries.... */
443         if (ERROR_SUCCESS==RegOpenKey32A(HKEY_LOCAL_MACHINE,"\\System\\CurrentControlSet\\control\\Nls\\Locale\\",&xhkey)) {
444                 i=0;
445                 while (1) {
446                         if (ERROR_SUCCESS!=RegEnumKey32W(xhkey,i,buffer,sizeof(buffer)))
447                                 break;
448                         if (!lpfnLocaleEnum(buffer))
449                                 break;
450                         i++;
451                 }
452                 RegCloseKey(xhkey);
453                 return TRUE;
454         }
455
456         i=0;
457         while (languages[i].langname!=NULL)
458         {
459             LPWSTR cp;
460             char   xbuffer[10];
461         
462             sprintf(xbuffer,"%08lx",(DWORD)languages[i].langid);
463
464             cp = HEAP_strdupAtoW( GetProcessHeap(), 0, xbuffer );
465             ret = lpfnLocaleEnum(cp);
466             HeapFree( GetProcessHeap(), 0, cp );
467             if (!ret) break;
468             i++;
469         }
470         return TRUE;
471 }
472
473 /***********************************************************************
474  *              EnumSystemLocales32A                [KERNEL32.92]
475  */
476 BOOL32 WINAPI EnumSystemLocales32A(LOCALE_ENUMPROC32A lpfnLocaleEnum,
477                                    DWORD flags)
478 {
479         int     i;
480         CHAR    buffer[200];
481         HKEY    xhkey;
482
483         TRACE(win32,"(%p,%08lx)\n",
484                 lpfnLocaleEnum,flags
485         );
486         if (ERROR_SUCCESS==RegOpenKey32A(HKEY_LOCAL_MACHINE,"\\System\\CurrentControlSet\\control\\Nls\\Locale\\",&xhkey)) {
487                 i=0;
488                 while (1) {
489                         if (ERROR_SUCCESS!=RegEnumKey32A(xhkey,i,buffer,sizeof(buffer)))
490                                 break;
491                         if (!lpfnLocaleEnum(buffer))
492                                 break;
493                         i++;
494                 }
495                 RegCloseKey(xhkey);
496                 return TRUE;
497         }
498         i=0;
499         while (languages[i].langname!=NULL) {
500                 sprintf(buffer,"%08lx",(DWORD)languages[i].langid);
501                 if (!lpfnLocaleEnum(buffer))
502                         break;
503                 i++;
504         }
505         return TRUE;
506 }
507
508 static const unsigned char CT_CType2_LUT[] = {
509   C2_NOTAPPLICABLE, /*   -   0 */
510   C2_NOTAPPLICABLE, /*   -   1 */
511   C2_NOTAPPLICABLE, /*   -   2 */
512   C2_NOTAPPLICABLE, /*   -   3 */
513   C2_NOTAPPLICABLE, /*   -   4 */
514   C2_NOTAPPLICABLE, /*   -   5 */
515   C2_NOTAPPLICABLE, /*   -   6 */
516   C2_NOTAPPLICABLE, /*   -   7 */
517   C2_NOTAPPLICABLE, /*   -   8 */
518   C2_SEGMENTSEPARATOR, /*   -   9 */
519   C2_NOTAPPLICABLE, /*   -  10 */
520   C2_NOTAPPLICABLE, /*   -  11 */
521   C2_NOTAPPLICABLE, /*   -  12 */
522   C2_NOTAPPLICABLE, /*   -  13 */
523   C2_NOTAPPLICABLE, /*   -  14 */
524   C2_NOTAPPLICABLE, /*   -  15 */
525   C2_NOTAPPLICABLE, /*   -  16 */
526   C2_NOTAPPLICABLE, /*   -  17 */
527   C2_NOTAPPLICABLE, /*   -  18 */
528   C2_NOTAPPLICABLE, /*   -  19 */
529   C2_NOTAPPLICABLE, /*   -  20 */
530   C2_NOTAPPLICABLE, /*   -  21 */
531   C2_NOTAPPLICABLE, /*   -  22 */
532   C2_NOTAPPLICABLE, /*   -  23 */
533   C2_NOTAPPLICABLE, /*   -  24 */
534   C2_NOTAPPLICABLE, /*   -  25 */
535   C2_NOTAPPLICABLE, /*   -  26 */
536   C2_NOTAPPLICABLE, /*   -  27 */
537   C2_NOTAPPLICABLE, /*   -  28 */
538   C2_NOTAPPLICABLE, /*   -  29 */
539   C2_NOTAPPLICABLE, /*   -  30 */
540   C2_NOTAPPLICABLE, /*   -  31 */
541   C2_WHITESPACE, /*   -  32 */
542   C2_OTHERNEUTRAL, /* ! -  33 */
543   C2_OTHERNEUTRAL, /* " -  34 */ /* " */
544   C2_EUROPETERMINATOR, /* # -  35 */
545   C2_EUROPETERMINATOR, /* $ -  36 */
546   C2_EUROPETERMINATOR, /* % -  37 */
547   C2_LEFTTORIGHT, /* & -  38 */
548   C2_OTHERNEUTRAL, /* ' -  39 */
549   C2_OTHERNEUTRAL, /* ( -  40 */
550   C2_OTHERNEUTRAL, /* ) -  41 */
551   C2_OTHERNEUTRAL, /* * -  42 */
552   C2_EUROPETERMINATOR, /* + -  43 */
553   C2_COMMONSEPARATOR, /* , -  44 */
554   C2_EUROPETERMINATOR, /* - -  45 */
555   C2_EUROPESEPARATOR, /* . -  46 */
556   C2_EUROPESEPARATOR, /* / -  47 */
557   C2_EUROPENUMBER, /* 0 -  48 */
558   C2_EUROPENUMBER, /* 1 -  49 */
559   C2_EUROPENUMBER, /* 2 -  50 */
560   C2_EUROPENUMBER, /* 3 -  51 */
561   C2_EUROPENUMBER, /* 4 -  52 */
562   C2_EUROPENUMBER, /* 5 -  53 */
563   C2_EUROPENUMBER, /* 6 -  54 */
564   C2_EUROPENUMBER, /* 7 -  55 */
565   C2_EUROPENUMBER, /* 8 -  56 */
566   C2_EUROPENUMBER, /* 9 -  57 */
567   C2_COMMONSEPARATOR, /* : -  58 */
568   C2_OTHERNEUTRAL, /* ; -  59 */
569   C2_OTHERNEUTRAL, /* < -  60 */
570   C2_OTHERNEUTRAL, /* = -  61 */
571   C2_OTHERNEUTRAL, /* > -  62 */
572   C2_OTHERNEUTRAL, /* ? -  63 */
573   C2_LEFTTORIGHT, /* @ -  64 */
574   C2_LEFTTORIGHT, /* A -  65 */
575   C2_LEFTTORIGHT, /* B -  66 */
576   C2_LEFTTORIGHT, /* C -  67 */
577   C2_LEFTTORIGHT, /* D -  68 */
578   C2_LEFTTORIGHT, /* E -  69 */
579   C2_LEFTTORIGHT, /* F -  70 */
580   C2_LEFTTORIGHT, /* G -  71 */
581   C2_LEFTTORIGHT, /* H -  72 */
582   C2_LEFTTORIGHT, /* I -  73 */
583   C2_LEFTTORIGHT, /* J -  74 */
584   C2_LEFTTORIGHT, /* K -  75 */
585   C2_LEFTTORIGHT, /* L -  76 */
586   C2_LEFTTORIGHT, /* M -  77 */
587   C2_LEFTTORIGHT, /* N -  78 */
588   C2_LEFTTORIGHT, /* O -  79 */
589   C2_LEFTTORIGHT, /* P -  80 */
590   C2_LEFTTORIGHT, /* Q -  81 */
591   C2_LEFTTORIGHT, /* R -  82 */
592   C2_LEFTTORIGHT, /* S -  83 */
593   C2_LEFTTORIGHT, /* T -  84 */
594   C2_LEFTTORIGHT, /* U -  85 */
595   C2_LEFTTORIGHT, /* V -  86 */
596   C2_LEFTTORIGHT, /* W -  87 */
597   C2_LEFTTORIGHT, /* X -  88 */
598   C2_LEFTTORIGHT, /* Y -  89 */
599   C2_LEFTTORIGHT, /* Z -  90 */
600   C2_OTHERNEUTRAL, /* [ -  91 */
601   C2_OTHERNEUTRAL, /* \ -  92 */
602   C2_OTHERNEUTRAL, /* ] -  93 */
603   C2_OTHERNEUTRAL, /* ^ -  94 */
604   C2_OTHERNEUTRAL, /* _ -  95 */
605   C2_OTHERNEUTRAL, /* ` -  96 */
606   C2_LEFTTORIGHT, /* a -  97 */
607   C2_LEFTTORIGHT, /* b -  98 */
608   C2_LEFTTORIGHT, /* c -  99 */
609   C2_LEFTTORIGHT, /* d - 100 */
610   C2_LEFTTORIGHT, /* e - 101 */
611   C2_LEFTTORIGHT, /* f - 102 */
612   C2_LEFTTORIGHT, /* g - 103 */
613   C2_LEFTTORIGHT, /* h - 104 */
614   C2_LEFTTORIGHT, /* i - 105 */
615   C2_LEFTTORIGHT, /* j - 106 */
616   C2_LEFTTORIGHT, /* k - 107 */
617   C2_LEFTTORIGHT, /* l - 108 */
618   C2_LEFTTORIGHT, /* m - 109 */
619   C2_LEFTTORIGHT, /* n - 110 */
620   C2_LEFTTORIGHT, /* o - 111 */
621   C2_LEFTTORIGHT, /* p - 112 */
622   C2_LEFTTORIGHT, /* q - 113 */
623   C2_LEFTTORIGHT, /* r - 114 */
624   C2_LEFTTORIGHT, /* s - 115 */
625   C2_LEFTTORIGHT, /* t - 116 */
626   C2_LEFTTORIGHT, /* u - 117 */
627   C2_LEFTTORIGHT, /* v - 118 */
628   C2_LEFTTORIGHT, /* w - 119 */
629   C2_LEFTTORIGHT, /* x - 120 */
630   C2_LEFTTORIGHT, /* y - 121 */
631   C2_LEFTTORIGHT, /* z - 122 */
632   C2_OTHERNEUTRAL, /* { - 123 */
633   C2_OTHERNEUTRAL, /* | - 124 */
634   C2_OTHERNEUTRAL, /* } - 125 */
635   C2_OTHERNEUTRAL, /* ~ - 126 */
636   C2_NOTAPPLICABLE, /* \7f - 127 */
637   C2_NOTAPPLICABLE, /* \80 - 128 */
638   C2_NOTAPPLICABLE, /* \81 - 129 */
639   C2_OTHERNEUTRAL, /* \82 - 130 */
640   C2_LEFTTORIGHT, /* \83 - 131 */
641   C2_OTHERNEUTRAL, /* \84 - 132 */
642   C2_OTHERNEUTRAL, /* \85 - 133 */
643   C2_OTHERNEUTRAL, /* \86 - 134 */
644   C2_OTHERNEUTRAL, /* \87 - 135 */
645   C2_LEFTTORIGHT, /* \88 - 136 */
646   C2_EUROPETERMINATOR, /* \89 - 137 */
647   C2_LEFTTORIGHT, /* \8a - 138 */
648   C2_OTHERNEUTRAL, /* \8b - 139 */
649   C2_LEFTTORIGHT, /* \8c - 140 */
650   C2_NOTAPPLICABLE, /* \8d - 141 */
651   C2_NOTAPPLICABLE, /* \8e - 142 */
652   C2_NOTAPPLICABLE, /* \8f - 143 */
653   C2_NOTAPPLICABLE, /* \90 - 144 */
654   C2_OTHERNEUTRAL, /* \91 - 145 */
655   C2_OTHERNEUTRAL, /* \92 - 146 */
656   C2_OTHERNEUTRAL, /* \93 - 147 */
657   C2_OTHERNEUTRAL, /* \94 - 148 */
658   C2_OTHERNEUTRAL, /* \95 - 149 */
659   C2_OTHERNEUTRAL, /* \96 - 150 */
660   C2_OTHERNEUTRAL, /* \97 - 151 */
661   C2_LEFTTORIGHT, /* \98 - 152 */
662   C2_OTHERNEUTRAL, /* \99 - 153 */
663   C2_LEFTTORIGHT, /* \9a - 154 */
664   C2_OTHERNEUTRAL, /* \9b - 155 */
665   C2_LEFTTORIGHT, /* \9c - 156 */
666   C2_NOTAPPLICABLE, /* \9d - 157 */
667   C2_NOTAPPLICABLE, /* \9e - 158 */
668   C2_LEFTTORIGHT, /* \9f - 159 */
669   C2_WHITESPACE, /*   - 160 */
670   C2_OTHERNEUTRAL, /* ¡ - 161 */
671   C2_EUROPETERMINATOR, /* ¢ - 162 */
672   C2_EUROPETERMINATOR, /* £ - 163 */
673   C2_EUROPETERMINATOR, /* ¤ - 164 */
674   C2_EUROPETERMINATOR, /* ¥ - 165 */
675   C2_OTHERNEUTRAL, /* ¦ - 166 */
676   C2_OTHERNEUTRAL, /* § - 167 */
677   C2_OTHERNEUTRAL, /* ¨ - 168 */
678   C2_OTHERNEUTRAL, /* © - 169 */
679   C2_OTHERNEUTRAL, /* ª - 170 */
680   C2_OTHERNEUTRAL, /* « - 171 */
681   C2_OTHERNEUTRAL, /* ¬ - 172 */
682   C2_OTHERNEUTRAL, /* ­ - 173 */
683   C2_OTHERNEUTRAL, /* ® - 174 */
684   C2_OTHERNEUTRAL, /* ¯ - 175 */
685   C2_EUROPETERMINATOR, /* ° - 176 */
686   C2_EUROPETERMINATOR, /* ± - 177 */
687   C2_EUROPENUMBER, /* ² - 178 */
688   C2_EUROPENUMBER, /* ³ - 179 */
689   C2_OTHERNEUTRAL, /* ´ - 180 */
690   C2_OTHERNEUTRAL, /* µ - 181 */
691   C2_OTHERNEUTRAL, /* ¶ - 182 */
692   C2_OTHERNEUTRAL, /* · - 183 */
693   C2_OTHERNEUTRAL, /* ¸ - 184 */
694   C2_EUROPENUMBER, /* ¹ - 185 */
695   C2_OTHERNEUTRAL, /* º - 186 */
696   C2_OTHERNEUTRAL, /* » - 187 */
697   C2_OTHERNEUTRAL, /* ¼ - 188 */
698   C2_OTHERNEUTRAL, /* ½ - 189 */
699   C2_OTHERNEUTRAL, /* ¾ - 190 */
700   C2_OTHERNEUTRAL, /* ¿ - 191 */
701   C2_LEFTTORIGHT, /* À - 192 */
702   C2_LEFTTORIGHT, /* Á - 193 */
703   C2_LEFTTORIGHT, /* Â - 194 */
704   C2_LEFTTORIGHT, /* Ã - 195 */
705   C2_LEFTTORIGHT, /* Ä - 196 */
706   C2_LEFTTORIGHT, /* Å - 197 */
707   C2_LEFTTORIGHT, /* Æ - 198 */
708   C2_LEFTTORIGHT, /* Ç - 199 */
709   C2_LEFTTORIGHT, /* È - 200 */
710   C2_LEFTTORIGHT, /* É - 201 */
711   C2_LEFTTORIGHT, /* Ê - 202 */
712   C2_LEFTTORIGHT, /* Ë - 203 */
713   C2_LEFTTORIGHT, /* Ì - 204 */
714   C2_LEFTTORIGHT, /* Í - 205 */
715   C2_LEFTTORIGHT, /* Î - 206 */
716   C2_LEFTTORIGHT, /* Ï - 207 */
717   C2_LEFTTORIGHT, /* Ð - 208 */
718   C2_LEFTTORIGHT, /* Ñ - 209 */
719   C2_LEFTTORIGHT, /* Ò - 210 */
720   C2_LEFTTORIGHT, /* Ó - 211 */
721   C2_LEFTTORIGHT, /* Ô - 212 */
722   C2_LEFTTORIGHT, /* Õ - 213 */
723   C2_LEFTTORIGHT, /* Ö - 214 */
724   C2_OTHERNEUTRAL, /* × - 215 */
725   C2_LEFTTORIGHT, /* Ø - 216 */
726   C2_LEFTTORIGHT, /* Ù - 217 */
727   C2_LEFTTORIGHT, /* Ú - 218 */
728   C2_LEFTTORIGHT, /* Û - 219 */
729   C2_LEFTTORIGHT, /* Ü - 220 */
730   C2_LEFTTORIGHT, /* Ý - 221 */
731   C2_LEFTTORIGHT, /* Þ - 222 */
732   C2_LEFTTORIGHT, /* ß - 223 */
733   C2_LEFTTORIGHT, /* à - 224 */
734   C2_LEFTTORIGHT, /* á - 225 */
735   C2_LEFTTORIGHT, /* â - 226 */
736   C2_LEFTTORIGHT, /* ã - 227 */
737   C2_LEFTTORIGHT, /* ä - 228 */
738   C2_LEFTTORIGHT, /* å - 229 */
739   C2_LEFTTORIGHT, /* æ - 230 */
740   C2_LEFTTORIGHT, /* ç - 231 */
741   C2_LEFTTORIGHT, /* è - 232 */
742   C2_LEFTTORIGHT, /* é - 233 */
743   C2_LEFTTORIGHT, /* ê - 234 */
744   C2_LEFTTORIGHT, /* ë - 235 */
745   C2_LEFTTORIGHT, /* ì - 236 */
746   C2_LEFTTORIGHT, /* í - 237 */
747   C2_LEFTTORIGHT, /* î - 238 */
748   C2_LEFTTORIGHT, /* ï - 239 */
749   C2_LEFTTORIGHT, /* ð - 240 */
750   C2_LEFTTORIGHT, /* ñ - 241 */
751   C2_LEFTTORIGHT, /* ò - 242 */
752   C2_LEFTTORIGHT, /* ó - 243 */
753   C2_LEFTTORIGHT, /* ô - 244 */
754   C2_LEFTTORIGHT, /* õ - 245 */
755   C2_LEFTTORIGHT, /* ö - 246 */
756   C2_OTHERNEUTRAL, /* ÷ - 247 */
757   C2_LEFTTORIGHT, /* ø - 248 */
758   C2_LEFTTORIGHT, /* ù - 249 */
759   C2_LEFTTORIGHT, /* ú - 250 */
760   C2_LEFTTORIGHT, /* û - 251 */
761   C2_LEFTTORIGHT, /* ü - 252 */
762   C2_LEFTTORIGHT, /* ý - 253 */
763   C2_LEFTTORIGHT, /* þ - 254 */
764   C2_LEFTTORIGHT /* ÿ - 255 */
765 };
766
767 static const WORD CT_CType3_LUT[] = { 
768   0x0000, /*   -   0 */
769   0x0000, /*   -   1 */
770   0x0000, /*   -   2 */
771   0x0000, /*   -   3 */
772   0x0000, /*   -   4 */
773   0x0000, /*   -   5 */
774   0x0000, /*   -   6 */
775   0x0000, /*   -   7 */
776   0x0000, /*   -   8 */
777   0x0008, /*   -   9 */
778   0x0008, /*   -  10 */
779   0x0008, /*   -  11 */
780   0x0008, /*   -  12 */
781   0x0008, /*   -  13 */
782   0x0000, /*   -  14 */
783   0x0000, /*   -  15 */
784   0x0000, /*   -  16 */
785   0x0000, /*   -  17 */
786   0x0000, /*   -  18 */
787   0x0000, /*   -  19 */
788   0x0000, /*   -  20 */
789   0x0000, /*   -  21 */
790   0x0000, /*   -  22 */
791   0x0000, /*   -  23 */
792   0x0000, /*   -  24 */
793   0x0000, /*   -  25 */
794   0x0000, /*   -  26 */
795   0x0000, /*   -  27 */
796   0x0000, /*   -  28 */
797   0x0000, /*   -  29 */
798   0x0000, /*   -  30 */
799   0x0000, /*   -  31 */
800   0x0048, /*   -  32 */
801   0x0048, /* ! -  33 */
802   0x0448, /* " -  34 */ /* " */
803   0x0048, /* # -  35 */
804   0x0448, /* $ -  36 */
805   0x0048, /* % -  37 */
806   0x0048, /* & -  38 */
807   0x0440, /* ' -  39 */
808   0x0048, /* ( -  40 */
809   0x0048, /* ) -  41 */
810   0x0048, /* * -  42 */
811   0x0048, /* + -  43 */
812   0x0048, /* , -  44 */
813   0x0440, /* - -  45 */
814   0x0048, /* . -  46 */
815   0x0448, /* / -  47 */
816   0x0040, /* 0 -  48 */
817   0x0040, /* 1 -  49 */
818   0x0040, /* 2 -  50 */
819   0x0040, /* 3 -  51 */
820   0x0040, /* 4 -  52 */
821   0x0040, /* 5 -  53 */
822   0x0040, /* 6 -  54 */
823   0x0040, /* 7 -  55 */
824   0x0040, /* 8 -  56 */
825   0x0040, /* 9 -  57 */
826   0x0048, /* : -  58 */
827   0x0048, /* ; -  59 */
828   0x0048, /* < -  60 */
829   0x0448, /* = -  61 */
830   0x0048, /* > -  62 */
831   0x0048, /* ? -  63 */
832   0x0448, /* @ -  64 */
833   0x8040, /* A -  65 */
834   0x8040, /* B -  66 */
835   0x8040, /* C -  67 */
836   0x8040, /* D -  68 */
837   0x8040, /* E -  69 */
838   0x8040, /* F -  70 */
839   0x8040, /* G -  71 */
840   0x8040, /* H -  72 */
841   0x8040, /* I -  73 */
842   0x8040, /* J -  74 */
843   0x8040, /* K -  75 */
844   0x8040, /* L -  76 */
845   0x8040, /* M -  77 */
846   0x8040, /* N -  78 */
847   0x8040, /* O -  79 */
848   0x8040, /* P -  80 */
849   0x8040, /* Q -  81 */
850   0x8040, /* R -  82 */
851   0x8040, /* S -  83 */
852   0x8040, /* T -  84 */
853   0x8040, /* U -  85 */
854   0x8040, /* V -  86 */
855   0x8040, /* W -  87 */
856   0x8040, /* X -  88 */
857   0x8040, /* Y -  89 */
858   0x8040, /* Z -  90 */
859   0x0048, /* [ -  91 */
860   0x0448, /* \ -  92 */
861   0x0048, /* ] -  93 */
862   0x0448, /* ^ -  94 */
863   0x0448, /* _ -  95 */
864   0x0448, /* ` -  96 */
865   0x8040, /* a -  97 */
866   0x8040, /* b -  98 */
867   0x8040, /* c -  99 */
868   0x8040, /* d - 100 */
869   0x8040, /* e - 101 */
870   0x8040, /* f - 102 */
871   0x8040, /* g - 103 */
872   0x8040, /* h - 104 */
873   0x8040, /* i - 105 */
874   0x8040, /* j - 106 */
875   0x8040, /* k - 107 */
876   0x8040, /* l - 108 */
877   0x8040, /* m - 109 */
878   0x8040, /* n - 110 */
879   0x8040, /* o - 111 */
880   0x8040, /* p - 112 */
881   0x8040, /* q - 113 */
882   0x8040, /* r - 114 */
883   0x8040, /* s - 115 */
884   0x8040, /* t - 116 */
885   0x8040, /* u - 117 */
886   0x8040, /* v - 118 */
887   0x8040, /* w - 119 */
888   0x8040, /* x - 120 */
889   0x8040, /* y - 121 */
890   0x8040, /* z - 122 */
891   0x0048, /* { - 123 */
892   0x0048, /* | - 124 */
893   0x0048, /* } - 125 */
894   0x0448, /* ~ - 126 */
895   0x0000, /* \7f - 127 */
896   0x0000, /* \80 - 128 */
897   0x0000, /* \81 - 129 */
898   0x0008, /* \82 - 130 */
899   0x8000, /* \83 - 131 */
900   0x0008, /* \84 - 132 */
901   0x0008, /* \85 - 133 */
902   0x0008, /* \86 - 134 */
903   0x0008, /* \87 - 135 */
904   0x0001, /* \88 - 136 */
905   0x0008, /* \89 - 137 */
906   0x8003, /* \8a - 138 */
907   0x0008, /* \8b - 139 */
908   0x8000, /* \8c - 140 */
909   0x0000, /* \8d - 141 */
910   0x0000, /* \8e - 142 */
911   0x0000, /* \8f - 143 */
912   0x0000, /* \90 - 144 */
913   0x0088, /* \91 - 145 */
914   0x0088, /* \92 - 146 */
915   0x0088, /* \93 - 147 */
916   0x0088, /* \94 - 148 */
917   0x0008, /* \95 - 149 */
918   0x0400, /* \96 - 150 */
919   0x0400, /* \97 - 151 */
920   0x0408, /* \98 - 152 */
921   0x0000, /* \99 - 153 */
922   0x8003, /* \9a - 154 */
923   0x0008, /* \9b - 155 */
924   0x8000, /* \9c - 156 */
925   0x0000, /* \9d - 157 */
926   0x0000, /* \9e - 158 */
927   0x8003, /* \9f - 159 */
928   0x0008, /*   - 160 */
929   0x0008, /* ¡ - 161 */
930   0x0048, /* ¢ - 162 */
931   0x0048, /* £ - 163 */
932   0x0008, /* ¤ - 164 */
933   0x0048, /* ¥ - 165 */
934   0x0048, /* ¦ - 166 */
935   0x0008, /* § - 167 */
936   0x0408, /* ¨ - 168 */
937   0x0008, /* © - 169 */
938   0x0400, /* ª - 170 */
939   0x0008, /* « - 171 */
940   0x0048, /* ¬ - 172 */
941   0x0408, /* ­ - 173 */
942   0x0008, /* ® - 174 */
943   0x0448, /* ¯ - 175 */
944   0x0008, /* ° - 176 */
945   0x0008, /* ± - 177 */
946   0x0000, /* ² - 178 */
947   0x0000, /* ³ - 179 */
948   0x0408, /* ´ - 180 */
949   0x0008, /* µ - 181 */
950   0x0008, /* ¶ - 182 */
951   0x0008, /* · - 183 */
952   0x0408, /* ¸ - 184 */
953   0x0000, /* ¹ - 185 */
954   0x0400, /* º - 186 */
955   0x0008, /* » - 187 */
956   0x0000, /* ¼ - 188 */
957   0x0000, /* ½ - 189 */
958   0x0000, /* ¾ - 190 */
959   0x0008, /* ¿ - 191 */
960   0x8003, /* À - 192 */
961   0x8003, /* Á - 193 */
962   0x8003, /* Â - 194 */
963   0x8003, /* Ã - 195 */
964   0x8003, /* Ä - 196 */
965   0x8003, /* Å - 197 */
966   0x8000, /* Æ - 198 */
967   0x8003, /* Ç - 199 */
968   0x8003, /* È - 200 */
969   0x8003, /* É - 201 */
970   0x8003, /* Ê - 202 */
971   0x8003, /* Ë - 203 */
972   0x8003, /* Ì - 204 */
973   0x8003, /* Í - 205 */
974   0x8003, /* Î - 206 */
975   0x8003, /* Ï - 207 */
976   0x8000, /* Ð - 208 */
977   0x8003, /* Ñ - 209 */
978   0x8003, /* Ò - 210 */
979   0x8003, /* Ó - 211 */
980   0x8003, /* Ô - 212 */
981   0x8003, /* Õ - 213 */
982   0x8003, /* Ö - 214 */
983   0x0008, /* × - 215 */
984   0x8003, /* Ø - 216 */
985   0x8003, /* Ù - 217 */
986   0x8003, /* Ú - 218 */
987   0x8003, /* Û - 219 */
988   0x8003, /* Ü - 220 */
989   0x8003, /* Ý - 221 */
990   0x8000, /* Þ - 222 */
991   0x8000, /* ß - 223 */
992   0x8003, /* à - 224 */
993   0x8003, /* á - 225 */
994   0x8003, /* â - 226 */
995   0x8003, /* ã - 227 */
996   0x8003, /* ä - 228 */
997   0x8003, /* å - 229 */
998   0x8000, /* æ - 230 */
999   0x8003, /* ç - 231 */
1000   0x8003, /* è - 232 */
1001   0x8003, /* é - 233 */
1002   0x8003, /* ê - 234 */
1003   0x8003, /* ë - 235 */
1004   0x8003, /* ì - 236 */
1005   0x8003, /* í - 237 */
1006   0x8003, /* î - 238 */
1007   0x8003, /* ï - 239 */
1008   0x8000, /* ð - 240 */
1009   0x8003, /* ñ - 241 */
1010   0x8003, /* ò - 242 */
1011   0x8003, /* ó - 243 */
1012   0x8003, /* ô - 244 */
1013   0x8003, /* õ - 245 */
1014   0x8003, /* ö - 246 */
1015   0x0008, /* ÷ - 247 */
1016   0x8003, /* ø - 248 */
1017   0x8003, /* ù - 249 */
1018   0x8003, /* ú - 250 */
1019   0x8003, /* û - 251 */
1020   0x8003, /* ü - 252 */
1021   0x8003, /* ý - 253 */
1022   0x8000, /* þ - 254 */
1023   0x8003  /* ÿ - 255 */
1024 };
1025
1026 /***********************************************************************
1027  *              GetStringTypeA                [OLE2NLS.7]
1028  */
1029 BOOL16 WINAPI GetStringType16(LCID locale,DWORD dwInfoType,LPCSTR src,
1030                               INT16 cchSrc,LPWORD chartype)
1031 {
1032         return GetStringTypeEx32A(locale,dwInfoType,src,cchSrc,chartype);
1033 }
1034 /***********************************************************************
1035  *              GetStringTypeA                [KERNEL32.277]
1036  */
1037 BOOL32 WINAPI GetStringType32A(LCID locale,DWORD dwInfoType,LPCSTR src,
1038                                INT32 cchSrc,LPWORD chartype)
1039 {
1040         return GetStringTypeEx32A(locale,dwInfoType,src,cchSrc,chartype);
1041 }
1042
1043 /***********************************************************************
1044  *              GetStringTypeExA                [KERNEL32.276]
1045  *
1046  * FIXME: Ignores the locale.
1047  */
1048 BOOL32 WINAPI GetStringTypeEx32A(LCID locale,DWORD dwInfoType,LPCSTR src,
1049                                  INT32 cchSrc,LPWORD chartype)
1050 {
1051         int     i;
1052         
1053         if ((src==NULL) || (chartype==NULL) || (src==(LPSTR)chartype))
1054         {
1055           SetLastError(ERROR_INVALID_PARAMETER);
1056           return FALSE;
1057         }
1058
1059         if (cchSrc==-1)
1060           cchSrc=lstrlen32A(src)+1;
1061           
1062         switch (dwInfoType) {
1063         case CT_CTYPE1:
1064           for (i=0;i<cchSrc;i++) 
1065           {
1066             chartype[i] = 0;
1067             if (isdigit(src[i])) chartype[i]|=C1_DIGIT;
1068             if (isalpha(src[i])) chartype[i]|=C1_ALPHA;
1069             if (islower(src[i])) chartype[i]|=C1_LOWER;
1070             if (isupper(src[i])) chartype[i]|=C1_UPPER;
1071             if (isspace(src[i])) chartype[i]|=C1_SPACE;
1072             if (ispunct(src[i])) chartype[i]|=C1_PUNCT;
1073             if (iscntrl(src[i])) chartype[i]|=C1_CNTRL;
1074 /* FIXME: isblank() is a GNU extension */
1075 /*              if (isblank(src[i])) chartype[i]|=C1_BLANK; */
1076             if ((src[i] == ' ') || (src[i] == '\t')) chartype[i]|=C1_BLANK;
1077             /* C1_XDIGIT */
1078         }
1079         return TRUE;
1080
1081         case CT_CTYPE2:
1082           for (i=0;i<cchSrc;i++) 
1083           {
1084             chartype[i]=(WORD)CT_CType2_LUT[i];
1085           }
1086           return TRUE;
1087
1088         case CT_CTYPE3:
1089           for (i=0;i<cchSrc;i++) 
1090           {
1091             chartype[i]=CT_CType3_LUT[i];
1092           }
1093           return TRUE;
1094
1095         default:
1096           ERR(ole,"Unknown dwInfoType:%ld\n",dwInfoType);
1097           return FALSE;
1098         }
1099 }
1100
1101 /*********************************************************************
1102  *  GetStringTypeW [KERNEL32.279]
1103  *
1104  * NOTES
1105  * Yes, this is missing LCID locale. MS fault.
1106  */
1107 BOOL32 WINAPI GetStringType32W(DWORD dwInfoType,LPCWSTR src,INT32 cchSrc,
1108                                LPWORD chartype)
1109 {
1110         return GetStringTypeEx32W(0/*defaultlocale*/,dwInfoType,src,cchSrc,chartype);
1111 }
1112
1113 /*********************************************************************
1114  *   GetStringTypeW   [KERNEL32.278]
1115  * FIXME: unicode chars are assumed chars
1116  */
1117 BOOL32 WINAPI GetStringTypeEx32W(LCID locale,DWORD dwInfoType,LPCWSTR src,
1118                                  INT32 cchSrc,LPWORD chartype)
1119 {
1120         int     i;
1121
1122
1123         if (cchSrc==-1)
1124           cchSrc=lstrlen32W(src)+1;
1125         
1126         switch (dwInfoType) {
1127         case CT_CTYPE2:
1128                 FIXME(ole,"CT_CTYPE2 not supported.\n");
1129                 return FALSE;
1130         case CT_CTYPE3:
1131                 FIXME(ole,"CT_CTYPE3 not supported.\n");
1132                 return FALSE;
1133         default:break;
1134         }
1135         for (i=0;i<cchSrc;i++) {
1136                 chartype[i] = 0;
1137                 if (isdigit(src[i])) chartype[i]|=C1_DIGIT;
1138                 if (isalpha(src[i])) chartype[i]|=C1_ALPHA;
1139                 if (islower(src[i])) chartype[i]|=C1_LOWER;
1140                 if (isupper(src[i])) chartype[i]|=C1_UPPER;
1141                 if (isspace(src[i])) chartype[i]|=C1_SPACE;
1142                 if (ispunct(src[i])) chartype[i]|=C1_PUNCT;
1143                 if (iscntrl(src[i])) chartype[i]|=C1_CNTRL;
1144 /* FIXME: isblank() is a GNU extension */
1145 /*              if (isblank(src[i])) chartype[i]|=C1_BLANK; */
1146                 if ((src[i] == ' ') || (src[i] == '\t')) chartype[i]|=C1_BLANK;
1147                 /* C1_XDIGIT */
1148         }
1149         return TRUE;
1150 }
1151
1152 /*****************************************************************
1153  * VerLanguageName16   [VER.10] 
1154  */
1155 DWORD WINAPI VerLanguageName16(UINT16 langid,LPSTR langname,UINT16 langnamelen)
1156 {
1157         int     i;
1158         DWORD   result;
1159         char    buffer[80];
1160
1161         TRACE(ver,"(%d,%p,%d)\n",langid,langname,langnamelen);
1162         /* First, check \System\CurrentControlSet\control\Nls\Locale\<langid>
1163          * from the registry. 
1164          */
1165         sprintf(buffer,
1166                 "\\System\\CurrentControlSet\\control\\Nls\\Locale\\%08x",
1167                 langid);
1168         result = RegQueryValue16(HKEY_LOCAL_MACHINE, buffer, langname,
1169                                 (LPDWORD)&langnamelen);
1170         if (result == ERROR_SUCCESS) {
1171                 langname[langnamelen-1]='\0';
1172                 return langnamelen;
1173         }
1174         /* if that fails, use the internal table */
1175         for (i=0;languages[i].langid!=0;i++)
1176                 if (langid==languages[i].langid)
1177                         break;
1178         strncpy(langname,languages[i].langname,langnamelen);
1179         langname[langnamelen-1]='\0';
1180         return strlen(languages[i].langname);
1181 }
1182
1183 /*****************************************************************
1184  * VerLanguageName32A                           [VERSION.9] 
1185  */
1186 DWORD WINAPI VerLanguageName32A(UINT32 langid,LPSTR langname,
1187                                 UINT32 langnamelen)
1188 {
1189         return VerLanguageName16(langid,langname,langnamelen);
1190 }
1191
1192 /*****************************************************************
1193  * VerLanguageName32W                           [VERSION.10] 
1194  */
1195 DWORD WINAPI VerLanguageName32W(UINT32 langid,LPWSTR langname,
1196                                 UINT32 langnamelen)
1197 {
1198         int     i;
1199         LPWSTR  keyname;
1200         DWORD   result;
1201         char    buffer[80];
1202
1203         /* First, check \System\CurrentControlSet\control\Nls\Locale\<langid>
1204          * from the registry. 
1205          */
1206         sprintf(buffer,
1207                 "\\System\\CurrentControlSet\\control\\Nls\\Locale\\%08x",
1208                 langid);
1209         keyname = HEAP_strdupAtoW( GetProcessHeap(), 0, buffer );
1210         result = RegQueryValue32W(HKEY_LOCAL_MACHINE, keyname, langname,
1211                                 (LPDWORD)&langnamelen);
1212         HeapFree( GetProcessHeap(), 0, keyname );
1213         if (result != ERROR_SUCCESS) {
1214                 /* if that fails, use the internal table */
1215                 for (i=0;languages[i].langid!=0;i++)
1216                         if (langid==languages[i].langid)
1217                                 break;
1218                 lstrcpyAtoW( langname, languages[i].langname );
1219                 langnamelen = strlen(languages[i].langname);
1220                 /* same as strlenW(langname); */
1221         }
1222         return langnamelen;
1223 }
1224  
1225 static int is_punctuation(unsigned char c) 
1226 {
1227   /* punctuation characters are :
1228      1-8, 14-31, 39, 45, 127-129, 141-144, 150-151, 157-158, 173 
1229
1230      "punctuation character" in this context is a character which is 
1231      considered "less important" during word sort comparison.
1232      See LCMapString for the precise definition of "less important". */
1233   if (c>=141)
1234   {
1235     if (c<=151)
1236       return (c<=144) || (c>=150);
1237     if (c<=158)
1238       return (c>=157);
1239     return (c==173);
1240   }
1241   if (c>=127)
1242     return (c<=129);
1243   if (c>=14)
1244     return (c<=31) || (c==39) || (c==45);
1245   return (c<=8);
1246 }
1247
1248 static int identity(int c)
1249 {
1250   return c;
1251 }
1252
1253 static const unsigned char LCM_Unicode_LUT[] = {
1254   6      ,   3, /*   -   1 */  
1255   6      ,   4, /*   -   2 */  
1256   6      ,   5, /*   -   3 */  
1257   6      ,   6, /*   -   4 */  
1258   6      ,   7, /*   -   5 */  
1259   6      ,   8, /*   -   6 */  
1260   6      ,   9, /*   -   7 */  
1261   6      ,  10, /*   -   8 */  
1262   7      ,   5, /*   -   9 */  
1263   7      ,   6, /*   -  10 */  
1264   7      ,   7, /*   -  11 */  
1265   7      ,   8, /*   -  12 */  
1266   7      ,   9, /*   -  13 */  
1267   6      ,  11, /*   -  14 */  
1268   6      ,  12, /*   -  15 */  
1269   6      ,  13, /*   -  16 */  
1270   6      ,  14, /*   -  17 */  
1271   6      ,  15, /*   -  18 */  
1272   6      ,  16, /*   -  19 */  
1273   6      ,  17, /*   -  20 */  
1274   6      ,  18, /*   -  21 */  
1275   6      ,  19, /*   -  22 */  
1276   6      ,  20, /*   -  23 */  
1277   6      ,  21, /*   -  24 */  
1278   6      ,  22, /*   -  25 */  
1279   6      ,  23, /*   -  26 */  
1280   6      ,  24, /*   -  27 */  
1281   6      ,  25, /*   -  28 */  
1282   6      ,  26, /*   -  29 */  
1283   6      ,  27, /*   -  30 */  
1284   6      ,  28, /*   -  31 */  
1285   7      ,   2, /*   -  32 */
1286   7      ,  28, /* ! -  33 */
1287   7      ,  29, /* " -  34 */ /* " */
1288   7      ,  31, /* # -  35 */
1289   7      ,  33, /* $ -  36 */
1290   7      ,  35, /* % -  37 */
1291   7      ,  37, /* & -  38 */
1292   6      , 128, /* ' -  39 */
1293   7      ,  39, /* ( -  40 */
1294   7      ,  42, /* ) -  41 */
1295   7      ,  45, /* * -  42 */
1296   8      ,   3, /* + -  43 */
1297   7      ,  47, /* , -  44 */
1298   6      , 130, /* - -  45 */
1299   7      ,  51, /* . -  46 */
1300   7      ,  53, /* / -  47 */
1301  12      ,   3, /* 0 -  48 */
1302  12      ,  33, /* 1 -  49 */
1303  12      ,  51, /* 2 -  50 */
1304  12      ,  70, /* 3 -  51 */
1305  12      ,  88, /* 4 -  52 */
1306  12      , 106, /* 5 -  53 */
1307  12      , 125, /* 6 -  54 */
1308  12      , 144, /* 7 -  55 */
1309  12      , 162, /* 8 -  56 */
1310  12      , 180, /* 9 -  57 */
1311   7      ,  55, /* : -  58 */
1312   7      ,  58, /* ; -  59 */
1313   8      ,  14, /* < -  60 */
1314   8      ,  18, /* = -  61 */
1315   8      ,  20, /* > -  62 */
1316   7      ,  60, /* ? -  63 */
1317   7      ,  62, /* @ -  64 */
1318  14      ,   2, /* A -  65 */
1319  14      ,   9, /* B -  66 */
1320  14      ,  10, /* C -  67 */
1321  14      ,  26, /* D -  68 */
1322  14      ,  33, /* E -  69 */
1323  14      ,  35, /* F -  70 */
1324  14      ,  37, /* G -  71 */
1325  14      ,  44, /* H -  72 */
1326  14      ,  50, /* I -  73 */
1327  14      ,  53, /* J -  74 */
1328  14      ,  54, /* K -  75 */
1329  14      ,  72, /* L -  76 */
1330  14      ,  81, /* M -  77 */
1331  14      , 112, /* N -  78 */
1332  14      , 124, /* O -  79 */
1333  14      , 126, /* P -  80 */
1334  14      , 137, /* Q -  81 */
1335  14      , 138, /* R -  82 */
1336  14      , 145, /* S -  83 */
1337  14      , 153, /* T -  84 */
1338  14      , 159, /* U -  85 */
1339  14      , 162, /* V -  86 */
1340  14      , 164, /* W -  87 */
1341  14      , 166, /* X -  88 */
1342  14      , 167, /* Y -  89 */
1343  14      , 169, /* Z -  90 */
1344   7      ,  63, /* [ -  91 */
1345   7      ,  65, /* \ -  92 */
1346   7      ,  66, /* ] -  93 */
1347   7      ,  67, /* ^ -  94 */
1348   7      ,  68, /* _ -  95 */
1349   7      ,  72, /* ` -  96 */
1350  14      ,   2, /* a -  97 */
1351  14      ,   9, /* b -  98 */
1352  14      ,  10, /* c -  99 */
1353  14      ,  26, /* d - 100 */
1354  14      ,  33, /* e - 101 */
1355  14      ,  35, /* f - 102 */
1356  14      ,  37, /* g - 103 */
1357  14      ,  44, /* h - 104 */
1358  14      ,  50, /* i - 105 */
1359  14      ,  53, /* j - 106 */
1360  14      ,  54, /* k - 107 */
1361  14      ,  72, /* l - 108 */
1362  14      ,  81, /* m - 109 */
1363  14      , 112, /* n - 110 */
1364  14      , 124, /* o - 111 */
1365  14      , 126, /* p - 112 */
1366  14      , 137, /* q - 113 */
1367  14      , 138, /* r - 114 */
1368  14      , 145, /* s - 115 */
1369  14      , 153, /* t - 116 */
1370  14      , 159, /* u - 117 */
1371  14      , 162, /* v - 118 */
1372  14      , 164, /* w - 119 */
1373  14      , 166, /* x - 120 */
1374  14      , 167, /* y - 121 */
1375  14      , 169, /* z - 122 */
1376   7      ,  74, /* { - 123 */
1377   7      ,  76, /* | - 124 */
1378   7      ,  78, /* } - 125 */
1379   7      ,  80, /* ~ - 126 */
1380   6      ,  29, /* \7f - 127 */
1381   6      ,  30, /* \80 - 128 */
1382   6      ,  31, /* \81 - 129 */
1383   7      , 123, /* \82 - 130 */
1384  14      ,  35, /* \83 - 131 */
1385   7      , 127, /* \84 - 132 */
1386  10      ,  21, /* \85 - 133 */
1387  10      ,  15, /* \86 - 134 */
1388  10      ,  16, /* \87 - 135 */
1389   7      ,  67, /* \88 - 136 */
1390  10      ,  22, /* \89 - 137 */
1391  14      , 145, /* \8a - 138 */
1392   7      , 136, /* \8b - 139 */
1393  14 + 16 , 124, /* \8c - 140 */
1394   6      ,  43, /* \8d - 141 */
1395   6      ,  44, /* \8e - 142 */
1396   6      ,  45, /* \8f - 143 */
1397   6      ,  46, /* \90 - 144 */
1398   7      , 121, /* \91 - 145 */
1399   7      , 122, /* \92 - 146 */
1400   7      , 125, /* \93 - 147 */
1401   7      , 126, /* \94 - 148 */
1402  10      ,  17, /* \95 - 149 */
1403   6      , 137, /* \96 - 150 */
1404   6      , 139, /* \97 - 151 */
1405   7      ,  93, /* \98 - 152 */
1406  14      , 156, /* \99 - 153 */
1407  14      , 145, /* \9a - 154 */
1408   7      , 137, /* \9b - 155 */
1409  14 + 16 , 124, /* \9c - 156 */
1410   6      ,  59, /* \9d - 157 */
1411   6      ,  60, /* \9e - 158 */
1412  14      , 167, /* \9f - 159 */
1413   7      ,   4, /*   - 160 */
1414   7      ,  81, /* ¡ - 161 */
1415  10      ,   2, /* ¢ - 162 */
1416  10      ,   3, /* £ - 163 */
1417  10      ,   4, /* ¤ - 164 */
1418  10      ,   5, /* ¥ - 165 */
1419   7      ,  82, /* ¦ - 166 */
1420  10      ,   6, /* § - 167 */
1421   7      ,  83, /* ¨ - 168 */
1422  10      ,   7, /* © - 169 */
1423  14      ,   2, /* ª - 170 */
1424   8      ,  24, /* « - 171 */
1425  10      ,   8, /* ¬ - 172 */
1426   6      , 131, /* ­ - 173 */
1427  10      ,   9, /* ® - 174 */
1428   7      ,  84, /* ¯ - 175 */
1429  10      ,  10, /* ° - 176 */
1430   8      ,  23, /* ± - 177 */
1431  12      ,  51, /* ² - 178 */
1432  12      ,  70, /* ³ - 179 */
1433   7      ,  85, /* ´ - 180 */
1434  10      ,  11, /* µ - 181 */
1435  10      ,  12, /* ¶ - 182 */
1436  10      ,  13, /* · - 183 */
1437   7      ,  86, /* ¸ - 184 */
1438  12      ,  33, /* ¹ - 185 */
1439  14      , 124, /* º - 186 */
1440   8      ,  26, /* » - 187 */
1441  12      ,  21, /* ¼ - 188 */
1442  12      ,  25, /* ½ - 189 */
1443  12      ,  29, /* ¾ - 190 */
1444   7      ,  87, /* ¿ - 191 */
1445  14      ,   2, /* À - 192 */
1446  14      ,   2, /* Á - 193 */
1447  14      ,   2, /* Â - 194 */
1448  14      ,   2, /* Ã - 195 */
1449  14      ,   2, /* Ä - 196 */
1450  14      ,   2, /* Å - 197 */
1451  14 + 16 ,   2, /* Æ - 198 */
1452  14      ,  10, /* Ç - 199 */
1453  14      ,  33, /* È - 200 */
1454  14      ,  33, /* É - 201 */
1455  14      ,  33, /* Ê - 202 */
1456  14      ,  33, /* Ë - 203 */
1457  14      ,  50, /* Ì - 204 */
1458  14      ,  50, /* Í - 205 */
1459  14      ,  50, /* Î - 206 */
1460  14      ,  50, /* Ï - 207 */
1461  14      ,  26, /* Ð - 208 */
1462  14      , 112, /* Ñ - 209 */
1463  14      , 124, /* Ò - 210 */
1464  14      , 124, /* Ó - 211 */
1465  14      , 124, /* Ô - 212 */
1466  14      , 124, /* Õ - 213 */
1467  14      , 124, /* Ö - 214 */
1468   8      ,  28, /* × - 215 */
1469  14      , 124, /* Ø - 216 */
1470  14      , 159, /* Ù - 217 */
1471  14      , 159, /* Ú - 218 */
1472  14      , 159, /* Û - 219 */
1473  14      , 159, /* Ü - 220 */
1474  14      , 167, /* Ý - 221 */
1475  14 + 32 , 153, /* Þ - 222 */
1476  14 + 48 , 145, /* ß - 223 */
1477  14      ,   2, /* à - 224 */
1478  14      ,   2, /* á - 225 */
1479  14      ,   2, /* â - 226 */
1480  14      ,   2, /* ã - 227 */
1481  14      ,   2, /* ä - 228 */
1482  14      ,   2, /* å - 229 */
1483  14 + 16 ,   2, /* æ - 230 */
1484  14      ,  10, /* ç - 231 */
1485  14      ,  33, /* è - 232 */
1486  14      ,  33, /* é - 233 */
1487  14      ,  33, /* ê - 234 */
1488  14      ,  33, /* ë - 235 */
1489  14      ,  50, /* ì - 236 */
1490  14      ,  50, /* í - 237 */
1491  14      ,  50, /* î - 238 */
1492  14      ,  50, /* ï - 239 */
1493  14      ,  26, /* ð - 240 */
1494  14      , 112, /* ñ - 241 */
1495  14      , 124, /* ò - 242 */
1496  14      , 124, /* ó - 243 */
1497  14      , 124, /* ô - 244 */
1498  14      , 124, /* õ - 245 */
1499  14      , 124, /* ö - 246 */
1500   8      ,  29, /* ÷ - 247 */
1501  14      , 124, /* ø - 248 */
1502  14      , 159, /* ù - 249 */
1503  14      , 159, /* ú - 250 */
1504  14      , 159, /* û - 251 */
1505  14      , 159, /* ü - 252 */
1506  14      , 167, /* ý - 253 */
1507  14 + 32 , 153, /* þ - 254 */
1508  14      , 167  /* ÿ - 255 */ };
1509
1510 static const unsigned char LCM_Unicode_LUT_2[] = { 33, 44, 145 };
1511
1512 #define LCM_Diacritic_Start 131
1513
1514 static const unsigned char LCM_Diacritic_LUT[] = { 
1515 123,  /* \83 - 131 */
1516   2,  /* \84 - 132 */
1517   2,  /* \85 - 133 */
1518   2,  /* \86 - 134 */
1519   2,  /* \87 - 135 */
1520   3,  /* \88 - 136 */
1521   2,  /* \89 - 137 */
1522  20,  /* \8a - 138 */
1523   2,  /* \8b - 139 */
1524   2,  /* \8c - 140 */
1525   2,  /* \8d - 141 */
1526   2,  /* \8e - 142 */
1527   2,  /* \8f - 143 */
1528   2,  /* \90 - 144 */
1529   2,  /* \91 - 145 */
1530   2,  /* \92 - 146 */
1531   2,  /* \93 - 147 */
1532   2,  /* \94 - 148 */
1533   2,  /* \95 - 149 */
1534   2,  /* \96 - 150 */
1535   2,  /* \97 - 151 */
1536   2,  /* \98 - 152 */
1537   2,  /* \99 - 153 */
1538  20,  /* \9a - 154 */
1539   2,  /* \9b - 155 */
1540   2,  /* \9c - 156 */
1541   2,  /* \9d - 157 */
1542   2,  /* \9e - 158 */
1543  19,  /* \9f - 159 */
1544   2,  /*   - 160 */
1545   2,  /* ¡ - 161 */
1546   2,  /* ¢ - 162 */
1547   2,  /* £ - 163 */
1548   2,  /* ¤ - 164 */
1549   2,  /* ¥ - 165 */
1550   2,  /* ¦ - 166 */
1551   2,  /* § - 167 */
1552   2,  /* ¨ - 168 */
1553   2,  /* © - 169 */
1554   3,  /* ª - 170 */
1555   2,  /* « - 171 */
1556   2,  /* ¬ - 172 */
1557   2,  /* ­ - 173 */
1558   2,  /* ® - 174 */
1559   2,  /* ¯ - 175 */
1560   2,  /* ° - 176 */
1561   2,  /* ± - 177 */
1562   2,  /* ² - 178 */
1563   2,  /* ³ - 179 */
1564   2,  /* ´ - 180 */
1565   2,  /* µ - 181 */
1566   2,  /* ¶ - 182 */
1567   2,  /* · - 183 */
1568   2,  /* ¸ - 184 */
1569   2,  /* ¹ - 185 */
1570   3,  /* º - 186 */
1571   2,  /* » - 187 */
1572   2,  /* ¼ - 188 */
1573   2,  /* ½ - 189 */
1574   2,  /* ¾ - 190 */
1575   2,  /* ¿ - 191 */
1576  15,  /* À - 192 */
1577  14,  /* Á - 193 */
1578  18,  /* Â - 194 */
1579  25,  /* Ã - 195 */
1580  19,  /* Ä - 196 */
1581  26,  /* Å - 197 */
1582   2,  /* Æ - 198 */
1583  28,  /* Ç - 199 */
1584  15,  /* È - 200 */
1585  14,  /* É - 201 */
1586  18,  /* Ê - 202 */
1587  19,  /* Ë - 203 */
1588  15,  /* Ì - 204 */
1589  14,  /* Í - 205 */
1590  18,  /* Î - 206 */
1591  19,  /* Ï - 207 */
1592 104,  /* Ð - 208 */
1593  25,  /* Ñ - 209 */
1594  15,  /* Ò - 210 */
1595  14,  /* Ó - 211 */
1596  18,  /* Ô - 212 */
1597  25,  /* Õ - 213 */
1598  19,  /* Ö - 214 */
1599   2,  /* × - 215 */
1600  33,  /* Ø - 216 */
1601  15,  /* Ù - 217 */
1602  14,  /* Ú - 218 */
1603  18,  /* Û - 219 */
1604  19,  /* Ü - 220 */
1605  14,  /* Ý - 221 */
1606   2,  /* Þ - 222 */
1607   2,  /* ß - 223 */
1608  15,  /* à - 224 */
1609  14,  /* á - 225 */
1610  18,  /* â - 226 */
1611  25,  /* ã - 227 */
1612  19,  /* ä - 228 */
1613  26,  /* å - 229 */
1614   2,  /* æ - 230 */
1615  28,  /* ç - 231 */
1616  15,  /* è - 232 */
1617  14,  /* é - 233 */
1618  18,  /* ê - 234 */
1619  19,  /* ë - 235 */
1620  15,  /* ì - 236 */
1621  14,  /* í - 237 */
1622  18,  /* î - 238 */
1623  19,  /* ï - 239 */
1624 104,  /* ð - 240 */
1625  25,  /* ñ - 241 */
1626  15,  /* ò - 242 */
1627  14,  /* ó - 243 */
1628  18,  /* ô - 244 */
1629  25,  /* õ - 245 */
1630  19,  /* ö - 246 */
1631   2,  /* ÷ - 247 */
1632  33,  /* ø - 248 */
1633  15,  /* ù - 249 */
1634  14,  /* ú - 250 */
1635  18,  /* û - 251 */
1636  19,  /* ü - 252 */
1637  14,  /* ý - 253 */
1638   2,  /* þ - 254 */
1639  19,  /* ÿ - 255 */
1640 } ;
1641
1642 /*************************************************************************
1643  * Convert a string, or generate a sort key from it.
1644  *
1645  * If (mapflags & LCMAP_SORTKEY), the function will generate
1646  * a sort key for the source string.  Else, it will convert it
1647  * accordingly to the flags LCMAP_UPPERCASE, LCMAP_LOWERCASE,...
1648  *
1649  * RETURNS
1650  *    Error : 0.
1651  *    Success : length of the result string.
1652  *
1653  * REMARKS
1654  *    If called with scrlen = -1, the function will compute the length
1655  *      of the 0-terminated string strsrc by itself.      
1656  * 
1657  *    If called with dstlen = 0, returns the buffer length that 
1658  *      would be required.
1659  */
1660 INT32 WINAPI LCMapString32A(
1661         LCID lcid /* locale identifier created with MAKELCID; 
1662                      LOCALE_SYSTEM_DEFAULT and LOCALE_USER_DEFAULT are predefined
1663                      values. */,
1664         DWORD mapflags /* flags */,
1665         LPCSTR srcstr  /* source buffer */,
1666         INT32 srclen   /* source length */,
1667         LPSTR dststr   /* destination buffer */,
1668         INT32 dstlen   /* destination buffer length */) 
1669 {
1670   int i;
1671
1672   TRACE(string,"(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1673         lcid,mapflags,srcstr,srclen,dststr,dstlen);
1674
1675   if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
1676   {
1677     SetLastError(ERROR_INVALID_PARAMETER);
1678     return 0;
1679   }
1680   if (srclen==-1) 
1681     srclen = lstrlen32A(srcstr) + 1 ;    /* (include final '\0') */
1682
1683   if (mapflags & ~ ( LCMAP_UPPERCASE | LCMAP_LOWERCASE | LCMAP_SORTKEY |
1684                      NORM_IGNORECASE | NORM_IGNORENONSPACE | SORT_STRINGSORT) )
1685   {
1686     FIXME(string,"(0x%04lx,0x%08lx,%p,%d,%p,%d): "
1687           "unimplemented flags: 0x%08lx\n",
1688           lcid,mapflags,srcstr,srclen,dststr,dstlen,mapflags);
1689   }
1690   
1691   if ( !(mapflags & LCMAP_SORTKEY) )
1692   {
1693     int (*f)(int)=identity; 
1694
1695     if (dstlen==0)
1696       return srclen;  /* dstlen=0 means "do nothing but return required length" */
1697     if (dstlen<srclen)
1698       srclen=dstlen;  /* No, this case is not an error under Windows 95.
1699                          And no '\0' gets written. */
1700     if (mapflags & LCMAP_UPPERCASE)
1701       f = toupper;
1702     else if (mapflags & LCMAP_LOWERCASE)
1703       f = tolower;
1704     for (i=0; i < srclen; i++)
1705       dststr[i] = (CHAR) f(srcstr[i]);
1706     return srclen;
1707   }
1708
1709   /* else ... (mapflags & LCMAP_SORTKEY)  */
1710   {
1711     int unicode_len=0;
1712     int case_len=0;
1713     int diacritic_len=0;
1714     int delayed_punctuation_len=0;
1715     char *case_component;
1716     char *diacritic_component;
1717     char *delayed_punctuation_component;
1718     int room,count;
1719     int flag_stringsort = mapflags & SORT_STRINGSORT;
1720
1721     /* compute how much room we will need */
1722     for (i=0;i<srclen;i++)
1723     {
1724       int ofs;
1725       unsigned char source_char = srcstr[i];
1726       if (source_char!='\0') 
1727       {
1728         if (flag_stringsort || !is_punctuation(source_char))
1729         {
1730           unicode_len++;
1731           if ( LCM_Unicode_LUT[-2+2*source_char] & ~15 )
1732             unicode_len++;             /* double letter */
1733         }
1734         else
1735         {
1736           delayed_punctuation_len++;
1737         }         
1738       }
1739           
1740       if (isupper(source_char))
1741         case_len=unicode_len; 
1742
1743       ofs = source_char - LCM_Diacritic_Start;
1744       if ((ofs>=0) && (LCM_Diacritic_LUT[ofs]!=2))
1745         diacritic_len=unicode_len;
1746     }
1747
1748     if (mapflags & NORM_IGNORECASE)
1749       case_len=0;                   
1750     if (mapflags & NORM_IGNORENONSPACE)
1751       diacritic_len=0;
1752
1753     room =  2 * unicode_len              /* "unicode" component */
1754       +     diacritic_len                /* "diacritic" component */
1755       +     case_len                     /* "case" component */
1756       +     4 * delayed_punctuation_len  /* punctuation in word sort mode */
1757       +     4                            /* four '\1' separators */
1758       +     1  ;                         /* terminal '\0' */
1759     if (dstlen==0)
1760       return room;      
1761     else if (dstlen<room)
1762     {
1763       SetLastError(ERROR_INSUFFICIENT_BUFFER);
1764       return 0;
1765     }
1766
1767     /*FIXME the Pointercheck should not be nessesary */
1768     if (IsBadWritePtr32 (dststr,room))
1769     { ERR (string,"bad destination buffer (dststr) : %p,%d\n",dststr,dstlen);
1770       SetLastError(ERROR_INSUFFICIENT_BUFFER);
1771       return 0;
1772     }
1773
1774     /* locate each component, write separators */
1775     diacritic_component = dststr + 2*unicode_len ;
1776     *diacritic_component++ = '\1'; 
1777     case_component = diacritic_component + diacritic_len ;
1778     *case_component++ = '\1'; 
1779     delayed_punctuation_component = case_component + case_len ;
1780     *delayed_punctuation_component++ = '\1';
1781     *delayed_punctuation_component++ = '\1';
1782
1783     /* read source string char by char, write 
1784        corresponding weight in each component. */
1785     for (i=0,count=0;i<srclen;i++)
1786     {
1787       unsigned char source_char=srcstr[i];
1788       if (source_char!='\0') 
1789       {
1790         int type,longcode;
1791         type = LCM_Unicode_LUT[-2+2*source_char];
1792         longcode = type >> 4;
1793         type &= 15;
1794         if (!flag_stringsort && is_punctuation(source_char)) 
1795         {
1796           UINT16 encrypted_location = (1<<15) + 7 + 4*count;
1797           *delayed_punctuation_component++ = (unsigned char) (encrypted_location>>8);
1798           *delayed_punctuation_component++ = (unsigned char) (encrypted_location&255);
1799                      /* big-endian is used here because it lets string comparison be
1800                         compatible with numerical comparison */
1801
1802           *delayed_punctuation_component++ = type;
1803           *delayed_punctuation_component++ = LCM_Unicode_LUT[-1+2*source_char];  
1804                      /* assumption : a punctuation character is never a 
1805                         double or accented letter */
1806         }
1807         else
1808         {
1809           dststr[2*count] = type;
1810           dststr[2*count+1] = LCM_Unicode_LUT[-1+2*source_char];  
1811           if (longcode)
1812           {
1813             if (count<case_len)
1814               case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
1815             if (count<diacritic_len)
1816               diacritic_component[count] = 2; /* assumption: a double letter
1817                                                  is never accented */
1818             count++;
1819             
1820             dststr[2*count] = type;
1821             dststr[2*count+1] = *(LCM_Unicode_LUT_2 - 1 + longcode); 
1822             /* 16 in the first column of LCM_Unicode_LUT  -->  longcode = 1 
1823                32 in the first column of LCM_Unicode_LUT  -->  longcode = 2 
1824                48 in the first column of LCM_Unicode_LUT  -->  longcode = 3 */
1825           }
1826
1827           if (count<case_len)
1828             case_component[count] = ( isupper(source_char) ? 18 : 2 ) ;
1829           if (count<diacritic_len)
1830           {
1831             int ofs = source_char - LCM_Diacritic_Start;
1832             diacritic_component[count] = (ofs>=0 ? LCM_Diacritic_LUT[ofs] : 2);
1833           }
1834           count++;
1835         }
1836       }
1837     }
1838     dststr[room-1] = '\0';
1839     return room;
1840   }
1841 }
1842                      
1843 INT32 WINAPI LCMapString32W(
1844         LCID lcid,DWORD mapflags,LPCWSTR srcstr,INT32 srclen,LPWSTR dststr,
1845         INT32 dstlen)
1846 {
1847   int i;
1848  
1849   TRACE(string,"(0x%04lx,0x%08lx,%p,%d,%p,%d)\n",
1850         lcid,mapflags,srcstr,srclen,dststr,dstlen);
1851
1852   if ( ((dstlen!=0) && (dststr==NULL)) || (srcstr==NULL) )
1853   {
1854     SetLastError(ERROR_INVALID_PARAMETER);
1855     return 0;
1856   }
1857   if (srclen==-1) 
1858     srclen = lstrlen32W(srcstr)+1;
1859   if (mapflags & LCMAP_SORTKEY) 
1860   {
1861     FIXME(string,"(0x%04lx,0x%08lx,%p,%d,%p,%d): "
1862           "unimplemented flags: 0x%08lx\n",
1863           lcid,mapflags,srcstr,srclen,dststr,dstlen,mapflags);
1864     return 0;
1865   }
1866   else
1867   {
1868     int (*f)(int)=identity; 
1869
1870     if (dstlen==0)
1871       return srclen;  
1872     if (dstlen<srclen)
1873       return 0;       
1874     if (mapflags & LCMAP_UPPERCASE)
1875       f = toupper;
1876     else if (mapflags & LCMAP_LOWERCASE)
1877       f = tolower;
1878     for (i=0; i < srclen; i++)
1879       dststr[i] = (WCHAR) f(srcstr[i]);
1880     return srclen;
1881   }
1882 }
1883
1884 /***********************************************************************
1885  *           CompareString16       (OLE2NLS.8)
1886  */
1887 UINT16 WINAPI CompareString16(DWORD lcid,DWORD fdwStyle,
1888                               LPCSTR s1,DWORD l1,LPCSTR s2,DWORD l2)
1889 {
1890         return (UINT16)CompareString32A(lcid,fdwStyle,s1,l1,s2,l2);
1891 }
1892
1893 /***********************************************************************
1894  *           CompareString32A   (KERNEL32.29)
1895  * This implementation ignores the locale
1896  * FIXME
1897  * Moreover it is quite inefficient. FIXME too!
1898  */
1899 UINT32 WINAPI CompareString32A(DWORD lcid, DWORD fdwStyle, 
1900                                LPCSTR s1, DWORD l1, LPCSTR s2,DWORD l2)
1901 {
1902   int mapstring_flags;
1903   int len1,len2;
1904   int result;
1905   LPSTR sk1,sk2;
1906   TRACE(ole,"%s and %s\n",
1907         debugstr_a (s1), debugstr_a (s2));
1908
1909   if ( (s1==NULL) || (s2==NULL) )
1910   {    
1911     SetLastError(ERROR_INVALID_PARAMETER);
1912     return 0;
1913   }
1914
1915   if(fdwStyle & NORM_IGNORESYMBOLS)
1916     FIXME(ole, "IGNORESYMBOLS not supported\n");
1917         
1918   mapstring_flags = LCMAP_SORTKEY | fdwStyle ;
1919   len1 = LCMapString32A(lcid,mapstring_flags,s1,l1,NULL,0);
1920   len2 = LCMapString32A(lcid,mapstring_flags,s2,l2,NULL,0);
1921
1922   if ((len1==0)||(len2==0))
1923     return 0;     /* something wrong happened */
1924
1925   sk1 = (LPSTR)HeapAlloc(GetProcessHeap(),0,len1);
1926   sk2 = (LPSTR)HeapAlloc(GetProcessHeap(),0,len2);
1927   if ( (!LCMapString32A(lcid,mapstring_flags,s1,l1,sk1,len1))
1928          || (!LCMapString32A(lcid,mapstring_flags,s2,l2,sk2,len2)) )
1929   { ERR(ole,"Bug in LCmapString32A.\n");
1930     result = 0;
1931   }
1932   else
1933   { result = strcmp(sk1,sk2)+2;
1934   }
1935   HeapFree(GetProcessHeap(),0,sk1);
1936   HeapFree(GetProcessHeap(),0,sk2);
1937   return result;
1938 }
1939
1940 /***********************************************************************
1941  *           CompareString32W       (KERNEL32.30)
1942  * This implementation ignores the locale
1943  * FIXME :  Does only string sort.  Should
1944  * be reimplemented the same way as CompareString32A.
1945  */
1946 UINT32 WINAPI CompareString32W(DWORD lcid, DWORD fdwStyle, 
1947                                LPCWSTR s1, DWORD l1, LPCWSTR s2,DWORD l2)
1948 {
1949         int len,ret;
1950         if(fdwStyle & NORM_IGNORENONSPACE)
1951                 FIXME(ole,"IGNORENONSPACE not supprted\n");
1952         if(fdwStyle & NORM_IGNORESYMBOLS)
1953                 FIXME(ole,"IGNORESYMBOLS not supported\n");
1954
1955         /* Is strcmp defaulting to string sort or to word sort?? */
1956         /* FIXME: Handle NORM_STRINGSORT */
1957         l1 = (l1==-1)?lstrlen32W(s1):l1;
1958         l2 = (l2==-1)?lstrlen32W(s2):l2;
1959         len = l1<l2 ? l1:l2;
1960         ret = (fdwStyle & NORM_IGNORECASE) ?
1961                 lstrncmpi32W(s1,s2,len) : lstrncmp32W(s1,s2,len);
1962         /* not equal, return 1 or 3 */
1963         if(ret!=0) return ret+2;
1964         /* same len, return 2 */
1965         if(l1==l2) return 2;
1966         /* the longer one is lexically greater */
1967         return (l1<l2)? 1 : 3;
1968 }
1969
1970 /*****************************************************************
1971  *
1972  *  OLE_GetFormatA() [internal]
1973
1974
1975  This function implements stuff for GetDateFormat() and 
1976  GetTimeFormat().
1977
1978   d    single-digit (no leading zero) day (of month)
1979   dd   two-digit day (of month)
1980   ddd  short day-of-week name
1981   dddd long day-of-week name
1982   M    single-digit month
1983   MM   two-digit month
1984   MMM  short month name
1985   MMMM full month name
1986   y    two-digit year, no leading 0
1987   yy   two-digit year
1988   yyyy four-digit year
1989   gg   era string
1990   h    hours with no leading zero (12-hour)
1991   hh   hours with full two digits
1992   H    hours with no leading zero (24-hour)
1993   HH   hours with full two digits
1994   m    minutes with no leading zero
1995   mm   minutes with full two digits
1996   s    seconds with no leading zero
1997   ss   seconds with full two digits
1998   t    time marker (A or P)
1999   tt   time marker (AM, PM)
2000   ''   used to quote literal characters
2001   ''   (within a quoted string) indicates a literal '
2002
2003  These functions REQUIRE valid locale, date,  and format. 
2004
2005  */
2006
2007 INT32 WINAPI OLE_GetFormatA(LCID locale,
2008                             DWORD flags,
2009                             LPSYSTEMTIME xtime,
2010                             LPCSTR format, 
2011                             LPSTR date, INT32 datelen)
2012 {
2013    INT32 inpos, outpos;
2014    int count, type, inquote, Overflow;
2015    char buf[40];
2016    int buflen;
2017
2018    const char * _dgfmt[] = { "%d", "%02d" };
2019    const char ** dgfmt = _dgfmt - 1; 
2020
2021    /* report, for debugging */
2022    TRACE(ole, "func(%8lx,%8lx, time(d=%d,h=%d,m=%d,s=%d), fmt:\'%s\' (at %p), %p (%9s), len=%d)\n", locale, flags,
2023          xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2024          format, format, date, date, datelen);
2025   
2026    /* initalize state variables and output buffer */
2027    inpos = outpos = 0;
2028    count = 0; inquote = 0; Overflow = 0;
2029    type = '\0';
2030    date[0] = buf[0] = '\0';
2031       
2032    for (inpos = 0;; inpos++) {
2033       /* TRACE(ole, "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]); */
2034       if (inquote) {
2035          if (format[inpos] == '\'') {
2036             if (format[inpos+1] == '\'') {
2037                inpos += 1;
2038                date[outpos++] = '\'';
2039             } else {
2040                inquote = 0;
2041                continue; /* we did nothing to the output */
2042             }
2043          } else if (format[inpos] == '\0') {
2044             date[outpos++] = '\0';
2045             if (outpos > datelen) Overflow = 1;
2046             break;
2047          } else {
2048             date[outpos++] = format[inpos];
2049             if (outpos > datelen) {
2050                Overflow = 1;
2051                date[outpos-1] = '\0'; /* this is the last place where
2052                                          it's safe to write */
2053                break;
2054             }
2055          }
2056       } else if (  (count && (format[inpos] != type))
2057                    || count == 4
2058                    || (count == 2 && strchr("ghHmst", type)) )
2059        {
2060             if         (type == 'd') {
2061                if        (count == 4) {
2062                   GetLocaleInfo32A(locale,
2063                                    LOCALE_SDAYNAME1
2064                                    + xtime->wDayOfWeek - 1,
2065                                    buf, sizeof(buf));
2066                } else if (count == 3) {
2067                            GetLocaleInfo32A(locale, 
2068                                             LOCALE_SABBREVDAYNAME1 
2069                                             + xtime->wDayOfWeek - 1,
2070                                             buf, sizeof(buf));
2071                       } else {
2072                   sprintf(buf, dgfmt[count], xtime->wDay);
2073                }
2074             } else if (type == 'M') {
2075                if (count == 3) {
2076                   GetLocaleInfo32A(locale, 
2077                                    LOCALE_SABBREVMONTHNAME1
2078                                    + xtime->wMonth - 1,
2079                                    buf, sizeof(buf));
2080                } else if (count == 4) {
2081                   GetLocaleInfo32A(locale,
2082                                    LOCALE_SMONTHNAME1
2083                                    + xtime->wMonth - 1,
2084                                    buf, sizeof(buf));
2085                  } else {
2086                   sprintf(buf, dgfmt[count], xtime->wMonth);
2087                }
2088             } else if (type == 'y') {
2089                if (count == 4) {
2090                       sprintf(buf, "%d", xtime->wYear);
2091                } else if (count == 3) {
2092                   strcpy(buf, "yyy");
2093                   WARN(ole,
2094                               "unknown format,\
2095 c=%c, n=%d\n",  type, count);
2096                  } else {
2097                   sprintf(buf, dgfmt[count], xtime->wYear % 100);
2098                }
2099             } else if (type == 'g') {
2100                if        (count == 2) {
2101                   FIXME(ole, "LOCALE_ICALENDARTYPE unimp.\n");
2102                   strcpy(buf, "AD");
2103             } else {
2104                   strcpy(buf, "g");
2105                   WARN(ole,
2106                                "unknown format, \
2107 c=%c, n=%d\n", type, count);
2108                }
2109             } else if (type == 'h') {
2110                /* gives us hours 1:00 -- 12:00 */
2111                sprintf(buf, dgfmt[count], (xtime->wHour-1)%12 +1);
2112             } else if (type == 'H') {
2113                /* 24-hour time */
2114                sprintf(buf, dgfmt[count], xtime->wHour);
2115             } else if (type == 'm') {
2116                sprintf(buf, dgfmt[count], xtime->wMinute);
2117             } else if (type == 's') {
2118                sprintf(buf, dgfmt[count], xtime->wSecond);
2119             } else if (type == 't') {
2120                if        (count == 1) {
2121                   sprintf(buf, "%c", (xtime->wHour < 12) ? 'A' : 'P');
2122                } else if (count == 2) {
2123                   /* sprintf(buf, "%s", (xtime->wHour < 12) ? "AM" : "PM"); */
2124                   GetLocaleInfo32A(locale,
2125                                    (xtime->wHour<12) 
2126                                    ? LOCALE_S1159 : LOCALE_S2359,
2127                                    buf, sizeof(buf));
2128                }
2129             };
2130
2131             /* we need to check the next char in the format string 
2132                again, no matter what happened */
2133             inpos--;
2134             
2135             /* add the contents of buf to the output */
2136             buflen = strlen(buf);
2137             if (outpos + buflen < datelen) {
2138                date[outpos] = '\0'; /* for strcat to hook onto */
2139                  strcat(date, buf);
2140                outpos += buflen;
2141             } else {
2142                date[outpos] = '\0';
2143                strncat(date, buf, datelen - outpos);
2144                  date[datelen - 1] = '\0';
2145                  SetLastError(ERROR_INSUFFICIENT_BUFFER);
2146                WARN(ole, "insufficient buffer\n");
2147                  return 0;
2148             }
2149
2150             /* reset the variables we used to keep track of this item */
2151             count = 0;
2152             type = '\0';
2153          } else if (format[inpos] == '\0') {
2154             /* we can't check for this at the loop-head, because
2155                that breaks the printing of the last format-item */
2156             date[outpos] = '\0';
2157             break;
2158          } else if (count) {
2159             /* continuing a code for an item */
2160             count +=1;
2161             continue;
2162          } else if (strchr("hHmstyMdg", format[inpos])) {
2163             type = format[inpos];
2164             count = 1;
2165             continue;
2166          } else if (format[inpos] == '\'') {
2167             inquote = 1;
2168             continue;
2169        } else {
2170             date[outpos++] = format[inpos];
2171          }
2172       /* now deal with a possible buffer overflow */
2173       if (outpos >= datelen) {
2174        date[datelen - 1] = '\0';
2175        SetLastError(ERROR_INSUFFICIENT_BUFFER);
2176        return 0;
2177       }
2178    }
2179    
2180    if (Overflow) {
2181       SetLastError(ERROR_INSUFFICIENT_BUFFER);
2182    };
2183
2184    /* finish it off with a string terminator */
2185    outpos++;
2186    /* sanity check */
2187    if (outpos > datelen-1) outpos = datelen-1;
2188    date[outpos] = '\0';
2189    
2190    TRACE(ole, "OLE_GetFormatA returns string '%s', len %d\n",
2191                date, outpos);
2192    return outpos;
2193 }
2194
2195 /**************************************************************
2196  * OLE_GetFormatW  [internal]  
2197  */
2198 INT32 WINAPI OLE_GetFormatW(LCID locale, DWORD flags,
2199                             LPSYSTEMTIME xtime,
2200                             LPCWSTR format,
2201                             LPWSTR output, INT32 outlen)
2202 {
2203    INT32   inpos, outpos;
2204    int     count, type=0, inquote;
2205    int     Overflow; /* loop check */
2206    int usedate, usetime;
2207    WCHAR   buf[40];
2208    int     buflen=0;
2209    char    abuf[40];
2210    WCHAR   arg0[] = {0}, arg1[] = {'%','d',0};
2211    WCHAR   arg2[] = {'%','0','2','d',0};
2212    WCHAR  *argarr[] = {arg0, arg1, arg2};
2213    int     datevars=0, timevars=0;
2214
2215    /* make a debug report */
2216    lstrcpynWtoA(abuf, format, sizeof(format));
2217    TRACE(ole, "args: %8lx, %8lx, time(d=%d,h=%d,m=%d,s=%d), fmt:\'%s\' (at %p), %p with max len %d\n",
2218          locale, flags, 
2219          xtime->wDay, xtime->wHour, xtime->wMinute, xtime->wSecond,
2220          abuf, format, output, outlen);
2221    
2222
2223    /* initialize state variables */
2224    inpos = outpos = 0;
2225    count = 0;
2226    inquote = Overflow = 0;
2227    /* this is really just a sanity check */
2228    output[0] = buf[0] = 0;
2229    abuf[0] = '\0';
2230    /* for compatibility with official Windows behavior */
2231    usedate = flags & DATE_DATEVARSONLY;
2232    usetime = flags & TIME_TIMEVARSONLY;
2233    
2234    /* this loop is the core of the function */
2235    for (inpos = 0; /* we have several break points */ ; inpos++) {
2236       if (inquote) {
2237          if (format[inpos] == (WCHAR) '\'') {
2238             if (format[inpos+1] == '\'') {
2239                inpos++;
2240                output[outpos++] = '\'';
2241             } else {
2242                inquote = 0;
2243                continue;
2244             }
2245          } else if (format[inpos] == 0) {
2246             output[outpos++] = 0;
2247             if (outpos > outlen) Overflow = 1;
2248             break;  /*  normal exit (within a quote) */
2249          } else {
2250             output[outpos++] = format[inpos]; /* copy input */
2251             if (outpos > outlen) {
2252                Overflow = 1;
2253                output[outpos-1] = 0; 
2254                break;
2255             }
2256          }
2257       } else if (  (count && (format[inpos] != type))
2258                    || ( (count==4 && type =='y') ||
2259                         (count==4 && type =='M') ||
2260                         (count==4 && type =='d') ||
2261                         (count==2 && type =='g') ||
2262                         (count==2 && type =='h') ||
2263                         (count==2 && type =='H') ||
2264                         (count==2 && type =='m') ||
2265                         (count==2 && type =='s') ||
2266                         (count==2 && type =='t') )  ) {
2267          if        (type == 'd') {
2268             if        (count == 3) {
2269                GetLocaleInfo32W(locale,
2270                              LOCALE_SDAYNAME1 + xtime->wDayOfWeek -1,
2271                              buf, sizeof(buf)/sizeof(WCHAR) );
2272             } else if (count == 3) {
2273                GetLocaleInfo32W(locale,
2274                                 LOCALE_SABBREVDAYNAME1 +
2275                                 xtime->wDayOfWeek -1,
2276                                 buf, sizeof(buf)/sizeof(WCHAR) );
2277             } else {
2278                wsnprintf32W(buf, 5, argarr[count], xtime->wDay );
2279             };
2280          } else if (type == 'M') {
2281             if        (count == 4) {
2282                GetLocaleInfo32W(locale,  LOCALE_SMONTHNAME1 +
2283                                 xtime->wMonth -1, buf,
2284                                 sizeof(buf)/sizeof(WCHAR) );
2285             } else if (count == 3) {
2286                GetLocaleInfo32W(locale,  LOCALE_SABBREVMONTHNAME1 +
2287                                 xtime->wMonth -1, buf,
2288                                 sizeof(buf)/sizeof(WCHAR) );
2289             } else {
2290                wsnprintf32W(buf, 5, argarr[count], xtime->wMonth);
2291             }
2292          } else if (type == 'y') {
2293             if        (count == 4) {
2294                wsnprintf32W(buf, 6, argarr[1] /* "%d" */,
2295                          xtime->wYear);
2296             } else if (count == 3) {
2297                lstrcpynAtoW(buf, "yyy", 5);
2298             } else {
2299                wsnprintf32W(buf, 6, argarr[count],
2300                             xtime->wYear % 100);
2301             }
2302          } else if (type == 'g') {
2303             if        (count == 2) {
2304                FIXME(ole, "LOCALE_ICALENDARTYPE unimplemented\n");
2305                lstrcpynAtoW(buf, "AD", 5);
2306             } else {
2307                /* Win API sez we copy it verbatim */
2308                lstrcpynAtoW(buf, "g", 5);
2309             }
2310          } else if (type == 'h') {
2311             /* hours 1:00-12:00 --- is this right? */
2312             wsnprintf32W(buf, 5, argarr[count], 
2313                          (xtime->wHour-1)%12 +1);
2314          } else if (type == 'H') {
2315             wsnprintf32W(buf, 5, argarr[count], 
2316                          xtime->wHour);
2317          } else if (type == 'm') {
2318             wsnprintf32W(buf, 5, argarr[count],
2319                          xtime->wMinute);
2320          } else if (type == 's') {
2321             wsnprintf32W(buf, 5, argarr[count],
2322                          xtime->wSecond);
2323          } else if (type == 't') {
2324             GetLocaleInfo32W(locale, (xtime->wHour < 12) ?
2325                              LOCALE_S1159 : LOCALE_S2359,
2326                              buf, sizeof(buf) );
2327             if        (count == 1) {
2328                buf[1] = 0;
2329             }
2330 }
2331
2332          /* no matter what happened,  we need to check this next 
2333             character the next time we loop through */
2334          inpos--;
2335
2336          /* cat buf onto the output */
2337          outlen = lstrlen32W(buf);
2338          if (outpos + buflen < outlen) {
2339             output[outpos] = 0;  /* a "hook" for strcat */
2340             lstrcat32W(output, buf);
2341             outpos += buflen;
2342          } else {
2343             output[outpos] = 0;
2344             lstrcatn32W(output, buf, outlen - outpos);
2345             output[outlen - 1] = 0;
2346             Overflow = 1;
2347             break; /* Abnormal exit */
2348          }
2349
2350          /* reset the variables we used this time */
2351          count = 0;
2352          type = '\0';
2353       } else if (format[inpos] == 0) {
2354          /* we can't check for this at the beginning,  because that 
2355          would keep us from printing a format spec that ended the 
2356          string */
2357          output[outpos] = 0;
2358          break;  /*  NORMAL EXIT  */
2359       } else if (count) {
2360          /* how we keep track of the middle of a format spec */
2361          count++;
2362          continue;
2363       } else if ( (datevars && (format[inpos]=='d' ||
2364                                 format[inpos]=='M' ||
2365                                 format[inpos]=='y' ||
2366                                 format[inpos]=='g')  ) ||
2367                   (timevars && (format[inpos]=='H' ||
2368                                 format[inpos]=='h' ||
2369                                 format[inpos]=='m' ||
2370                                 format[inpos]=='s' ||
2371                                 format[inpos]=='t') )    ) {
2372          type = format[inpos];
2373          count = 1;
2374          continue;
2375       } else if (format[inpos] == '\'') {
2376          inquote = 1;
2377          continue;
2378       } else {
2379          /* unquoted literals */
2380          output[outpos++] = format[inpos];
2381       }
2382    }
2383
2384    if (Overflow) {
2385       SetLastError(ERROR_INSUFFICIENT_BUFFER);
2386       WARN(ole, " buffer overflow\n");
2387    };
2388
2389    /* final string terminator and sanity check */
2390    outpos++;
2391    if (outpos > outlen-1) outpos = outlen-1;
2392    output[outpos] = '0';
2393
2394    lstrcpynWtoA(abuf, output, sizeof(abuf) );
2395    TRACE(ole, " returning string \'%s\'\n", abuf);
2396         
2397    return (!Overflow) ? outlen : 0;
2398    
2399 }
2400
2401
2402 /*****************************************************************
2403  *  GetDateFormat32A() [KERNEL32.310] Makes an ASCII string of the date
2404  *
2405  * This function uses format to format the date,  or,  if format
2406  * is NULL, uses the default for the locale.  format is a string
2407  * of literal fields and characters as follows:
2408  *
2409  * - d    single-digit (no leading zero) day (of month)
2410  * - dd   two-digit day (of month)
2411  * - ddd  short day-of-week name
2412  * - dddd long day-of-week name
2413  * - M    single-digit month
2414  * - MM   two-digit month
2415  * - MMM  short month name
2416  * - MMMM full month name
2417  * - y    two-digit year, no leading 0
2418  * - yy   two-digit year
2419  * - yyyy four-digit year
2420  * - gg   era string
2421  *
2422  */
2423
2424 INT32 WINAPI GetDateFormat32A(LCID locale,DWORD flags,
2425                               LPSYSTEMTIME xtime,
2426                               LPCSTR format, LPSTR date,INT32 datelen) 
2427 {
2428    
2429   char format_buf[40];
2430   LPCSTR thisformat;
2431   SYSTEMTIME t;
2432   LPSYSTEMTIME thistime;
2433   LCID thislocale;
2434
2435   INT32 ret;
2436
2437   TRACE(ole,"(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",
2438               locale,flags,xtime,format,date,datelen);
2439   
2440   if (!locale) {
2441      locale = LOCALE_SYSTEM_DEFAULT;
2442      };
2443   
2444   if (locale == LOCALE_SYSTEM_DEFAULT) {
2445      thislocale = GetSystemDefaultLCID();
2446   } else if (locale == LOCALE_USER_DEFAULT) {
2447      thislocale = GetUserDefaultLCID();
2448   } else {
2449      thislocale = locale;
2450    };
2451
2452   if (xtime == NULL) {
2453      GetSystemTime(&t);
2454      thistime = &t;
2455   } else {
2456      thistime = xtime;
2457   };
2458
2459   if (format == NULL) {
2460      GetLocaleInfo32A(thislocale, ((flags&DATE_LONGDATE) 
2461                                    ? LOCALE_SLONGDATE
2462                                    : LOCALE_SSHORTDATE),
2463                       format_buf, sizeof(format_buf));
2464      thisformat = format_buf;
2465   } else {
2466      thisformat = format;
2467   };
2468
2469   
2470   ret = OLE_GetFormatA(thislocale, flags, thistime, thisformat, 
2471                        date, datelen);
2472   
2473
2474    TRACE(ole, 
2475                "GetDateFormat32A() returning %d, with data=%s\n",
2476                ret, date);
2477   return ret;
2478 }
2479
2480 /* ****************************************************************
2481  * GetDateFormat32W() [KERNEL32.311] Makes a Unicode string of the date
2482  *
2483  * Acts the same as GetDateFormat32A(),  except that it's Unicode.
2484  * Accepts & returns sizes as counts of Unicode characters.
2485  *
2486  */
2487 INT32 WINAPI GetDateFormat32W(LCID locale,DWORD flags,
2488                               LPSYSTEMTIME xtime,
2489                               LPCWSTR format,
2490                               LPWSTR date, INT32 datelen)
2491 {
2492    short datearr[] = {'1','9','9','4','-','1','-','1',0};
2493
2494    FIXME(ole, "STUB (should call OLE_GetFormatW)\n");   
2495    lstrcpyn32W(date, datearr, datelen);
2496    return (  datelen < 9) ? datelen : 9;
2497    
2498    
2499 }
2500
2501
2502 /**************************************************************************
2503  *              GetNumberFormat32A      (KERNEL32.355)
2504  * NOTE: type of lpFormat should be CONST NUMBERFORMAT 
2505  */
2506 INT32 WINAPI GetNumberFormat32A(LCID locale, DWORD dwflags,
2507                                LPCSTR lpvalue,  char *lpFormat,
2508                                LPSTR lpNumberStr, int cchNumber)
2509 {
2510  int n;
2511
2512  FIXME(file,"%s: stub, no reformating done\n",lpvalue);
2513
2514  n = strlen(lpvalue);
2515  if (cchNumber) { 
2516    strncpy(lpNumberStr,lpvalue,cchNumber);
2517    if (cchNumber <= n) {
2518      lpNumberStr[cchNumber-1] = 0;
2519      n = cchNumber-1;
2520    }
2521  }
2522  return n;
2523 }
2524  
2525
2526
2527 /*****************************************************************
2528  *
2529  * GetTimeFormat32A() [KERNEL32.422] Makes an ASCII string of the time
2530  *
2531  * Formats date according to format,  or locale default if format is
2532  * NULL. The format consists of literal characters and fields as follows:
2533  *
2534  * h  hours with no leading zero (12-hour)
2535  * hh hours with full two digits
2536  * H  hours with no leading zero (24-hour)
2537  * HH hours with full two digits
2538  * m  minutes with no leading zero
2539  * mm minutes with full two digits
2540  * s  seconds with no leading zero
2541  * ss seconds with full two digits
2542  * t  time marker (A or P)
2543  * tt time marker (AM, PM)
2544  *
2545  */
2546
2547 INT32 WINAPI 
2548 GetTimeFormat32A(LCID locale,        /* in  */
2549                  DWORD flags,        /* in  */
2550                  LPSYSTEMTIME xtime, /* in  */ 
2551                  LPCSTR format,      /* in  */
2552                  LPSTR timestr,      /* out */
2553                  INT32 timelen       /* in  */) 
2554 { char format_buf[40];
2555   LPCSTR thisformat;
2556   SYSTEMTIME t;
2557   LPSYSTEMTIME thistime;
2558   LCID thislocale=0;
2559   DWORD thisflags=LOCALE_STIMEFORMAT; /* standart timeformat */;
2560   
2561   TRACE(ole,"GetTimeFormat(0x%04lx,0x%08lx,%p,%s,%p,%d)\n",locale,flags,xtime,format,timestr,timelen);
2562   if (!locale) 
2563   { locale = LOCALE_SYSTEM_DEFAULT;
2564   }
2565   
2566   if (locale == LOCALE_SYSTEM_DEFAULT) 
2567   { thislocale = GetSystemDefaultLCID();
2568   } 
2569   else if (locale == LOCALE_USER_DEFAULT) 
2570   { thislocale = GetUserDefaultLCID();
2571         }
2572   else
2573   { thislocale = locale;
2574   }
2575   if (format == NULL) 
2576   { if (flags&LOCALE_NOUSEROVERRIDE)  /*use system default*/
2577     { thislocale = GetSystemDefaultLCID();
2578     }
2579     switch (flags)
2580     { case TIME_NOSECONDS:
2581         FIXME(ole,"TIME_NOSECONDS not yet implemented");
2582         break;
2583       case TIME_NOMINUTESORSECONDS:
2584         FIXME(ole,"TIME_NOMINUTESORSECONDS not yet implemented");
2585        break;
2586     }   
2587     GetLocaleInfo32A(thislocale, thisflags, format_buf,sizeof(format_buf));
2588     thisformat = format_buf;
2589   }
2590   else 
2591   { thisformat = format;
2592   }
2593   
2594   if (!locale) 
2595   {  locale = GetSystemDefaultLCID();
2596   }
2597   if (xtime == NULL) /* NULL means use the current local time*/
2598   { GetSystemTime(&t);
2599     thistime = &t;
2600   } 
2601   else
2602   { thistime = xtime;
2603   }
2604   return OLE_GetFormatA(thislocale, thisflags, thistime, thisformat,timestr, timelen);
2605 }
2606
2607
2608 /* ****************************************************************
2609  *  GetTimeFormat32W()  [KERNEL32.423] Makes a Unicode string of the time
2610  * 
2611  *
2612  */
2613
2614 INT32 WINAPI 
2615 GetTimeFormat32W(LCID locale,DWORD flags,
2616                               LPSYSTEMTIME xtime,
2617                               LPCWSTR format, 
2618                               LPWSTR timestr,INT32 timelen) 
2619 {
2620    char debugbuf[40];
2621    WCHAR buf[20];
2622    LPCWSTR realformat=0;
2623    SYSTEMTIME t;
2624    LPSYSTEMTIME realtime;
2625    WCHAR * fmt_buf = NULL;
2626    int fmt_buf_size = 0; /* units of WCHARs */
2627    INT32 retval;
2628    
2629    lstrcpynWtoA(debugbuf, format, (sizeof(buf))/2);
2630    TRACE(ole, "GetTimeFormatW len %d flags 0x%lX format >%s<\n",
2631                 timelen, flags, debugbuf);
2632
2633    /* Enforce the Windows behavior */
2634    flags |= ~LOCALE_TIMEDATEBOTH;
2635    flags |= TIME_TIMEVARSONLY;
2636
2637    /* take care of the format or locale not being given */
2638    if (format) {
2639       realformat = format;
2640    } else if (locale) {
2641       /* allocate memory */
2642       retval = ERROR_INSUFFICIENT_BUFFER;
2643       fmt_buf = malloc((fmt_buf_size+=7) * sizeof(WCHAR));
2644       if (!fmt_buf)
2645         goto out_nomem;
2646
2647       while(!GetLocaleInfo32W(locale, LOCALE_STIMEFORMAT,
2648                               fmt_buf, fmt_buf_size)) {
2649         retval = ERROR_OUTOFMEMORY;
2650         fmt_buf = realloc(fmt_buf, 
2651                            (fmt_buf_size += 10) * sizeof(WCHAR));
2652         if (!fmt_buf)
2653                 goto out_nomem;
2654       }
2655       realformat = fmt_buf;
2656    } else {
2657       FIXME(ole,  "caller gave no locale,  no format;  what should we do?\n");
2658       SetLastError(ERROR_BAD_FORMAT);
2659    }
2660    if (!locale)
2661       locale = GetSystemDefaultLCID();
2662
2663    realtime = xtime;
2664    if (!realtime) {
2665       realtime = &t;
2666       GetSystemTime(realtime);
2667    }
2668
2669    retval = OLE_GetFormatW(locale, flags, realtime, realformat, timestr,  timelen);
2670    if (fmt_buf)
2671         free(fmt_buf);
2672    return retval;
2673
2674 out_nomem:
2675         SetLastError(retval);
2676         WARN(ole, "could not allocate %d chars of memory\n", fmt_buf_size); 
2677         return 0;
2678 }