Release 970914
[wine] / win32 / code_page.c
1 /*
2  * Win32 kernel functions
3  *
4  * Copyright 1995 Martin von Loewis and Cameron Heide
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "windows.h"
10 #include "winerror.h"
11 #include "winnls.h"
12 #include "heap.h"
13 #include "stddebug.h"
14 #include "debug.h"
15
16
17 /***********************************************************************
18  *           GetACP               (KERNEL32.148)
19  */
20 UINT32 WINAPI GetACP(void)
21 {
22     return 1252;    /* Windows 3.1 ISO Latin */
23 }
24
25 /***********************************************************************
26  *           GetCPInfo            (KERNEL32.154)
27  */
28 BOOL32 WINAPI GetCPInfo( UINT32 codepage, LPCPINFO cpinfo )
29 {
30     cpinfo->DefaultChar[0] = '?';
31     switch (codepage)
32     {
33     case 932 : /* Shift JIS (japan) */
34         cpinfo->MaxCharSize = 2;
35         cpinfo->LeadByte[0]= 0x81; cpinfo->LeadByte[1] = 0x9F;
36         cpinfo->LeadByte[2]= 0xE0; cpinfo->LeadByte[3] = 0xFC;
37         cpinfo->LeadByte[4]= 0x00; cpinfo->LeadByte[5] = 0x00;
38         break;
39     case 936 : /* GB2312 (Chinese) */
40     case 949 : /* KSC5601-1987 (Korean) */
41     case 950 : /* BIG5 (Chinese) */
42         cpinfo->MaxCharSize = 2;
43         cpinfo->LeadByte[0]= 0x81; cpinfo->LeadByte[1] = 0xFE;
44         cpinfo->LeadByte[2]= 0x00; cpinfo->LeadByte[3] = 0x00;
45         break;
46     case 1361 : /* Johab (Korean) */
47         cpinfo->MaxCharSize = 2;
48         cpinfo->LeadByte[0]= 0x84; cpinfo->LeadByte[1] = 0xD3;
49         cpinfo->LeadByte[2]= 0xD8; cpinfo->LeadByte[3] = 0xDE;
50         cpinfo->LeadByte[4]= 0xE0; cpinfo->LeadByte[5] = 0xF9;
51         cpinfo->LeadByte[6]= 0x00; cpinfo->LeadByte[7] = 0x00;
52         break;
53     default :
54         cpinfo->MaxCharSize = 1;
55         cpinfo->LeadByte[0]= 0x00; cpinfo->LeadByte[1] = 0x00;
56         break;
57     }
58     return 1;
59 }
60
61 /***********************************************************************
62  *              GetOEMCP                (KERNEL32.248)
63  */
64 UINT32 WINAPI GetOEMCP(void)
65 {
66     return 437;    /* MS-DOS United States */
67 }
68
69 /***********************************************************************
70  *           IsValidCodePage   (KERNEL32.360)
71  */
72 BOOL32 WINAPI IsValidCodePage(UINT32 CodePage)
73 {
74     switch ( CodePage )
75     {
76     case 1252 :
77     case 437 :
78         return TRUE;
79     default :
80         return FALSE;
81     }
82 }
83
84
85 /***********************************************************************
86  *              MultiByteToWideChar                (KERNEL32.392)
87  */
88 int WINAPI MultiByteToWideChar(UINT32 page, DWORD flags, char *src, int srclen,
89                                WCHAR *dst, int dstlen)
90 {
91     if (srclen == -1)
92          srclen = lstrlen32A(src);
93     if (!dst)
94          return srclen*2;
95
96     lstrcpynAtoW(dst,src,srclen); /* FIXME */
97     return srclen*2;
98 }
99
100 int WINAPI WideCharToMultiByte(UINT32 page, DWORD flags, WCHAR *src, int srclen,
101                                char *dst, int dstlen, char* defchar, BOOL32 *used)
102 {
103         int count = 0;
104         int dont_copy= (dstlen==0);
105         if(page!=GetACP() && page!=CP_OEMCP && page!=CP_ACP)
106                 fprintf(stdnimp,"Conversion in CP %d not supported\n",page);
107 #if 0
108         if(flags)
109                 fprintf(stdnimp,"WideCharToMultiByte flags %lx not supported\n",flags);
110 #endif
111         if(used)
112                 *used=0;
113         while(srclen && (dont_copy || dstlen))
114         {
115                 if(!dont_copy){
116                         if(*src<256)
117                                 *dst = *src;
118                         else
119                         {
120                                 /* FIXME: Is this correct ?*/
121                                 if(flags & WC_DEFAULTCHAR){
122                                         *dst = defchar ? *defchar : '?';
123                                         if(used)*used=1;
124                                 }
125                         }
126                         dstlen--;
127                         dst++;
128                 }
129                 count++;
130                 if(!*src)
131                         break;
132                 if(srclen!=-1)srclen--;
133                 src++;
134         }
135         return count;
136 }
137
138
139 /***********************************************************************
140  *           IsDBCSLeadByteEx   (KERNEL32.359)
141  */
142 BOOL32 WINAPI IsDBCSLeadByteEx( UINT32 codepage, BYTE testchar )
143 {
144     CPINFO cpinfo;
145     int i;
146
147     GetCPInfo(codepage, &cpinfo);
148     for (i = 0 ; i < sizeof(cpinfo.LeadByte)/sizeof(cpinfo.LeadByte[0]); i+=2)
149     {
150         if (cpinfo.LeadByte[i] == 0)
151             return FALSE;
152         if (cpinfo.LeadByte[i] <= testchar && testchar <= cpinfo.LeadByte[i+1])
153             return TRUE;
154     }
155     return FALSE;
156 }
157
158
159 /***********************************************************************
160  *           IsDBCSLeadByte16   (KERNEL.207)
161  */
162 BOOL16 WINAPI IsDBCSLeadByte16( BYTE testchar )
163 {
164     return IsDBCSLeadByteEx(GetACP(), testchar);
165 }
166
167
168 /***********************************************************************
169  *           IsDBCSLeadByte32   (KERNEL32.358)
170  */
171 BOOL32 WINAPI IsDBCSLeadByte32( BYTE testchar )
172 {
173     return IsDBCSLeadByteEx(GetACP(), testchar);
174 }
175
176
177 /***********************************************************************
178  *              EnumSystemCodePages32A                (KERNEL32.92)
179  */
180 BOOL32 WINAPI EnumSystemCodePages32A(CODEPAGE_ENUMPROC32A lpfnCodePageEnum,DWORD flags)
181 {
182         dprintf_win32(stddeb,"EnumSystemCodePages32A(%p,%08lx)\n",
183                 lpfnCodePageEnum,flags
184         );
185         lpfnCodePageEnum("437");
186         return TRUE;
187 }
188
189 /***********************************************************************
190  *              EnumSystemCodePages32W                (KERNEL32.93)
191  */
192 BOOL32 WINAPI EnumSystemCodePages32W( CODEPAGE_ENUMPROC32W lpfnCodePageEnum,
193                                       DWORD flags)
194 {
195     WCHAR       *cp;
196     dprintf_win32(stddeb,"EnumSystemCodePages32W(%p,%08lx)\n",
197                   lpfnCodePageEnum,flags );
198
199     cp = HEAP_strdupAtoW( GetProcessHeap(), 0, "437" );
200     lpfnCodePageEnum(cp);
201     HeapFree( GetProcessHeap(), 0, cp );
202     return TRUE;
203 }