Set ansi/oem/mac code pages from current locale.
[wine] / memory / codepage.c
1 /*
2  * Code page functions
3  *
4  * Copyright 2000 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include "winbase.h"
12 #include "winerror.h"
13 #include "winnls.h"
14 #include "wine/unicode.h"
15 #include "debugtools.h"
16
17 DEFAULT_DEBUG_CHANNEL(string);
18
19 /* current code pages */
20 static const union cptable *ansi_cptable;
21 static const union cptable *oem_cptable;
22 static const union cptable *mac_cptable;
23
24 /* retrieve a code page table from the locale info */
25 static const union cptable *get_locale_cp( LCID lcid, LCTYPE type )
26 {
27     const union cptable *table = NULL;
28     char buf[32];
29
30     if (GetLocaleInfoA( lcid, type, buf, sizeof(buf) )) table = cp_get_table( atoi(buf) );
31     return table;
32 }
33
34 /* setup default codepage info before we can get at the locale stuff */
35 static void init_codepages(void)
36 {
37     ansi_cptable = cp_get_table( 1252 );
38     oem_cptable  = cp_get_table( 437 );
39     mac_cptable  = cp_get_table( 10000 );
40     assert( ansi_cptable );
41     assert( oem_cptable );
42     assert( mac_cptable );
43 }
44
45 /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
46 static const union cptable *get_codepage_table( unsigned int codepage )
47 {
48     const union cptable *ret = NULL;
49
50     if (!ansi_cptable) init_codepages();
51
52     switch(codepage)
53     {
54     case CP_ACP:        return ansi_cptable;
55     case CP_OEMCP:      return oem_cptable;
56     case CP_MACCP:      return mac_cptable;
57     case CP_THREAD_ACP: return get_locale_cp( GetThreadLocale(), LOCALE_IDEFAULTANSICODEPAGE );
58     case CP_UTF7:
59     case CP_UTF8:
60         break;
61     default:
62         if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
63         if (codepage == oem_cptable->info.codepage) return oem_cptable;
64         if (codepage == mac_cptable->info.codepage) return mac_cptable;
65         ret = cp_get_table( codepage );
66         break;
67     }
68     return ret;
69 }
70
71 /* initialize default code pages from locale info */
72 /* FIXME: should be done in init_codepages, but it can't right now */
73 /* since it needs KERNEL32 to be loaded for the locale info. */
74 void CODEPAGE_Init(void)
75 {
76     const union cptable *table;
77     LCID lcid = GetUserDefaultLCID();
78
79     if (!ansi_cptable) init_codepages();  /* just in case */
80     
81     if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTANSICODEPAGE ))) ansi_cptable = table;
82     if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTMACCODEPAGE ))) mac_cptable = table;
83     if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTCODEPAGE ))) oem_cptable = table;
84
85     TRACE( "ansi=%03d oem=%03d mac=%03d\n", ansi_cptable->info.codepage,
86            oem_cptable->info.codepage, mac_cptable->info.codepage );
87 }
88
89 /******************************************************************************
90  *              GetACP   (KERNEL32)
91  *
92  * RETURNS
93  *    Current ANSI code-page identifier, default if no current defined
94  */
95 UINT WINAPI GetACP(void)
96 {
97     if (!ansi_cptable) init_codepages();
98     return ansi_cptable->info.codepage;
99 }
100
101
102 /***********************************************************************
103  *              GetOEMCP   (KERNEL32)
104  */
105 UINT WINAPI GetOEMCP(void)
106 {
107     if (!oem_cptable) init_codepages();
108     return oem_cptable->info.codepage;
109 }
110
111
112 /***********************************************************************
113  *           IsValidCodePage   (KERNEL32)
114  */
115 BOOL WINAPI IsValidCodePage( UINT codepage )
116 {
117     return cp_get_table( codepage ) != NULL;
118 }
119
120
121 /***********************************************************************
122  *           IsDBCSLeadByteEx   (KERNEL32)
123  */
124 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
125 {
126     const union cptable *table = get_codepage_table( codepage );
127     return table && is_dbcs_leadbyte( table, testchar );
128 }
129
130
131 /***********************************************************************
132  *           IsDBCSLeadByte   (KERNEL32)
133  */
134 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
135 {
136     if (!ansi_cptable) init_codepages();
137     return is_dbcs_leadbyte( ansi_cptable, testchar );
138 }
139
140
141 /***********************************************************************
142  *           GetCPInfo   (KERNEL32)
143  */
144 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
145 {
146     const union cptable *table = get_codepage_table( codepage );
147
148     if (!table) 
149     {
150         SetLastError( ERROR_INVALID_PARAMETER );
151         return FALSE;
152     }
153     if (table->info.def_char & 0xff00)
154     {
155         cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
156         cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
157     }
158     else
159     {
160         cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
161         cpinfo->DefaultChar[1] = 0;
162     }
163     if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
164         memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
165     else
166         cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
167
168     return TRUE;
169 }
170
171
172 /***********************************************************************
173  *              EnumSystemCodePagesA   (KERNEL32)
174  */
175 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
176 {
177     const union cptable *table;
178     char buffer[10];
179     int index = 0;
180
181     for (;;)
182     {
183         if (!(table = cp_enum_table( index++ ))) break;
184         sprintf( buffer, "%d", table->info.codepage );
185         if (!lpfnCodePageEnum( buffer )) break;
186     }
187     return TRUE;
188 }
189
190
191 /***********************************************************************
192  *              EnumSystemCodePagesW   (KERNEL32)
193  */
194 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
195 {
196     const union cptable *table;
197     WCHAR buffer[10], *p;
198     int page, index = 0;
199
200     for (;;)
201     {
202         if (!(table = cp_enum_table( index++ ))) break;
203         p = buffer + sizeof(buffer)/sizeof(WCHAR);
204         *--p = 0;
205         page = table->info.codepage;
206         do
207         {
208             *--p = '0' + (page % 10);
209             page /= 10;
210         } while( page );
211         if (!lpfnCodePageEnum( p )) break;
212     }
213     return TRUE;
214 }
215
216
217 /***********************************************************************
218  *              MultiByteToWideChar   (KERNEL32)
219  *
220  * PARAMS
221  *   page [in]    Codepage character set to convert from
222  *   flags [in]   Character mapping flags
223  *   src [in]     Source string buffer
224  *   srclen [in]  Length of source string buffer
225  *   dst [in]     Destination buffer
226  *   dstlen [in]  Length of destination buffer
227  *
228  * NOTES
229  *   The returned length includes the null terminator character.
230  *
231  * RETURNS
232  *   Success: If dstlen > 0, number of characters written to destination
233  *            buffer.  If dstlen == 0, number of characters needed to do
234  *            conversion.
235  *   Failure: 0. Occurs if not enough space is available.
236  *
237  * ERRORS
238  *   ERROR_INSUFFICIENT_BUFFER
239  *   ERROR_INVALID_PARAMETER
240  *   ERROR_NO_UNICODE_TRANSLATION
241  *
242  */
243 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
244                                 LPWSTR dst, INT dstlen )
245 {
246     const union cptable *table;
247     int ret;
248
249     if (srclen == -1) srclen = strlen(src) + 1;
250
251     if (page >= CP_UTF7)
252     {
253         FIXME("UTF not supported\n");
254         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
255         return 0;
256     }
257
258     if (!(table = get_codepage_table( page )))
259     {
260         SetLastError( ERROR_INVALID_PARAMETER );
261         return 0;
262     }
263
264     if (flags & MB_COMPOSITE) FIXME("MB_COMPOSITE not supported\n");
265     if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
266
267     ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
268
269     if (ret < 0)
270     {
271         switch(ret)
272         {
273         case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
274         case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
275         }
276         ret = 0;
277     }
278     return ret;
279 }
280
281
282 /***********************************************************************
283  *              WideCharToMultiByte   (KERNEL32)
284  *
285  * PARAMS
286  *   page [in]    Codepage character set to convert to
287  *   flags [in]   Character mapping flags
288  *   src [in]     Source string buffer
289  *   srclen [in]  Length of source string buffer
290  *   dst [in]     Destination buffer
291  *   dstlen [in]  Length of destination buffer
292  *   defchar [in] Default character to use for conversion if no exact
293  *                  conversion can be made
294  *   used [out]   Set if default character was used in the conversion
295  *
296  * NOTES
297  *   The returned length includes the null terminator character.
298  *
299  * RETURNS
300  *   Success: If dstlen > 0, number of characters written to destination
301  *            buffer.  If dstlen == 0, number of characters needed to do
302  *            conversion.
303  *   Failure: 0. Occurs if not enough space is available.
304  *
305  * ERRORS
306  *   ERROR_INSUFFICIENT_BUFFER
307  *   ERROR_INVALID_PARAMETER
308  */
309 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
310                                 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
311 {
312     const union cptable *table;
313     int ret, used_tmp;
314
315     if (srclen == -1) srclen = strlenW(src) + 1;
316
317     if (page >= CP_UTF7)
318     {
319         FIXME("UTF not supported\n");
320         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
321         return 0;
322     }
323
324     if (!(table = get_codepage_table( page )))
325     {
326         SetLastError( ERROR_INVALID_PARAMETER );
327         return 0;
328     }
329
330 /*    if (flags & WC_COMPOSITECHECK) FIXME( "WC_COMPOSITECHECK (%lx) not supported\n", flags );*/
331
332     ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen, defchar, used ? &used_tmp : NULL );
333     if (used) *used = used_tmp;
334
335     if (ret == -1)
336     {
337         SetLastError( ERROR_INSUFFICIENT_BUFFER );
338         ret = 0;
339     }
340     return ret;
341 }