source: trunk/src/kernel32/codepage.c@ 21916

Last change on this file since 21916 was 21916, checked in by dmik, 14 years ago

Merge branch gcc-kmk to trunk.

File size: 14.1 KB
Line 
1/* $Id: codepage.c,v 1.3 2003-07-31 15:59:49 sandervl Exp $
2 *
3 * Code page functions
4 *
5 * Based on Wine code (memory\codepage.c)
6 *
7 * Copyright 2000 Alexandre Julliard
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 */
12
13#include <assert.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include "winbase.h"
19#include "winerror.h"
20#include "winnls.h"
21#include "wine/unicode.h"
22#include "debugtools.h"
23
24#ifdef __WIN32OS2__
25#include <options.h>
26#include "codepage.h"
27
28#define DBG_LOCALLOG DBG_codepage
29#include "dbglocal.h"
30
31BOOL fIsDBCSEnv = FALSE;
32
33#endif
34
35DEFAULT_DEBUG_CHANNEL(string);
36
37/* current code pages */
38static const union cptable *ansi_cptable;
39static const union cptable *oem_cptable;
40static const union cptable *mac_cptable;
41
42/* retrieve a code page table from the locale info */
43static const union cptable *get_locale_cp( LCID lcid, LCTYPE type )
44{
45 const union cptable *table = NULL;
46 char buf[32];
47
48 if (GetLocaleInfoA( lcid, type, buf, sizeof(buf) )) table = cp_get_table( atoi(buf) );
49 return table;
50}
51
52/* setup default codepage info before we can get at the locale stuff */
53static void init_codepages(void)
54{
55 ansi_cptable = cp_get_table( 1252 );
56 oem_cptable = cp_get_table( 437 );
57 mac_cptable = cp_get_table( 10000 );
58 assert( ansi_cptable );
59 assert( oem_cptable );
60 assert( mac_cptable );
61}
62
63/* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
64static const union cptable *get_codepage_table( unsigned int codepage )
65{
66 const union cptable *ret = NULL;
67
68 if (!ansi_cptable) init_codepages();
69
70 switch(codepage)
71 {
72 case CP_ACP: return ansi_cptable;
73 case CP_OEMCP: return oem_cptable;
74 case CP_MACCP: return mac_cptable;
75 case CP_THREAD_ACP: return get_locale_cp( GetThreadLocale(), LOCALE_IDEFAULTANSICODEPAGE );
76 case CP_UTF7:
77 case CP_UTF8:
78 break;
79 default:
80 if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
81 if (codepage == oem_cptable->info.codepage) return oem_cptable;
82 if (codepage == mac_cptable->info.codepage) return mac_cptable;
83 ret = cp_get_table( codepage );
84 break;
85 }
86 return ret;
87}
88
89/* initialize default code pages from locale info */
90/* FIXME: should be done in init_codepages, but it can't right now */
91/* since it needs KERNEL32 to be loaded for the locale info. */
92void CODEPAGE_Init(void)
93{
94 const union cptable *table;
95 LCID lcid = GetUserDefaultLCID();
96
97 if (!ansi_cptable) init_codepages(); /* just in case */
98
99 if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTANSICODEPAGE )) != NULL) ansi_cptable = table;
100 if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTMACCODEPAGE )) != NULL) mac_cptable = table;
101 if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTCODEPAGE )) != NULL) oem_cptable = table;
102
103 TRACE( "ansi=%03d oem=%03d mac=%03d\n", ansi_cptable->info.codepage,
104 oem_cptable->info.codepage, mac_cptable->info.codepage );
105#ifdef __WIN32OS2__
106 dprintf(("Language %s", getenv("LANG")));
107
108 if(ansi_cptable->info.char_size == 2) {
109 fIsDBCSEnv = TRUE;
110 }
111#endif
112}
113
114#ifdef __WIN32OS2__
115ULONG GetDisplayCodepage()
116{
117 if (!ansi_cptable) CODEPAGE_Init();
118
119 return ansi_cptable->info.codepage;
120}
121
122ULONG GetWindowsCodepage()
123{
124 if (!ansi_cptable) CODEPAGE_Init();
125
126 return ansi_cptable->info.codepage;
127}
128
129#endif
130
131/******************************************************************************
132 * GetACP (KERNEL32)
133 *
134 * RETURNS
135 * Current ANSI code-page identifier, default if no current defined
136 */
137UINT WINAPI GetACP(void)
138{
139 if (!ansi_cptable) init_codepages();
140#ifdef __WIN32OS2__
141 dprintf(("GetACP %d", ansi_cptable->info.codepage));
142#endif
143 return ansi_cptable->info.codepage;
144}
145
146
147/***********************************************************************
148 * GetOEMCP (KERNEL32)
149 */
150UINT WINAPI GetOEMCP(void)
151{
152 if (!oem_cptable) init_codepages();
153#ifdef __WIN32OS2__
154 dprintf(("GetOEMCP %d", oem_cptable->info.codepage));
155#endif
156 return oem_cptable->info.codepage;
157}
158
159
160/***********************************************************************
161 * IsValidCodePage (KERNEL32)
162 */
163BOOL WINAPI IsValidCodePage( UINT codepage )
164{
165#ifdef __WIN32OS2__
166 dprintf(("IsValidCodePage %d", codepage));
167#endif
168 return cp_get_table( codepage ) != NULL;
169}
170
171
172/***********************************************************************
173 * IsDBCSLeadByteEx (KERNEL32)
174 */
175BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
176{
177 const union cptable *table = get_codepage_table( codepage );
178#ifdef __WIN32OS2__
179 dprintf2(("IsDBCSLeadByteEx %d %x", codepage, testchar));
180#endif
181 return table && is_dbcs_leadbyte( table, testchar );
182}
183
184
185/***********************************************************************
186 * IsDBCSLeadByte (KERNEL32)
187 */
188BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
189{
190 if (!ansi_cptable) init_codepages();
191 return is_dbcs_leadbyte( ansi_cptable, testchar );
192}
193
194
195/***********************************************************************
196 * GetCPInfo (KERNEL32)
197 */
198BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
199{
200 const union cptable *table = get_codepage_table( codepage );
201
202#ifdef __WIN32OS2__
203 dprintf(("GetCPInfo %d %x", codepage, cpinfo));
204#endif
205
206 if (!table)
207 {
208 SetLastError( ERROR_INVALID_PARAMETER );
209 return FALSE;
210 }
211 if (table->info.def_char & 0xff00)
212 {
213 cpinfo->DefaultChar[0] = (table->info.def_char & 0xff00) >> 8;
214 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
215 }
216 else
217 {
218 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
219 cpinfo->DefaultChar[1] = 0;
220 }
221 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
222 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
223 else
224 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
225
226 return TRUE;
227}
228
229
230/***********************************************************************
231 * EnumSystemCodePagesA (KERNEL32)
232 */
233BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
234{
235 const union cptable *table;
236 char buffer[10];
237 int index = 0;
238
239#ifdef __WIN32OS2__
240 dprintf(("EnumSystemCodePagesA %x %x", lpfnCodePageEnum, flags));
241#endif
242
243 for (;;)
244 {
245 if (!(table = cp_enum_table( index++ ))) break;
246 sprintf( buffer, "%d", table->info.codepage );
247 if (!lpfnCodePageEnum( buffer )) break;
248 }
249 return TRUE;
250}
251
252
253/***********************************************************************
254 * EnumSystemCodePagesW (KERNEL32)
255 */
256BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
257{
258 const union cptable *table;
259 WCHAR buffer[10], *p;
260 int page, index = 0;
261
262#ifdef __WIN32OS2__
263 dprintf(("EnumSystemCodePagesW %x %x", lpfnCodePageEnum, flags));
264#endif
265
266 for (;;)
267 {
268 if (!(table = cp_enum_table( index++ ))) break;
269 p = buffer + sizeof(buffer)/sizeof(WCHAR);
270 *--p = 0;
271 page = table->info.codepage;
272 do
273 {
274 *--p = '0' + (page % 10);
275 page /= 10;
276 } while( page );
277 if (!lpfnCodePageEnum( p )) break;
278 }
279 return TRUE;
280}
281
282
283/***********************************************************************
284 * MultiByteToWideChar (KERNEL32)
285 *
286 * PARAMS
287 * page [in] Codepage character set to convert from
288 * flags [in] Character mapping flags
289 * src [in] Source string buffer
290 * srclen [in] Length of source string buffer
291 * dst [in] Destination buffer
292 * dstlen [in] Length of destination buffer
293 *
294 * NOTES
295 * The returned length includes the null terminator character.
296 *
297 * RETURNS
298 * Success: If dstlen > 0, number of characters written to destination
299 * buffer. If dstlen == 0, number of characters needed to do
300 * conversion.
301 * Failure: 0. Occurs if not enough space is available.
302 *
303 * ERRORS
304 * ERROR_INSUFFICIENT_BUFFER
305 * ERROR_INVALID_PARAMETER
306 * ERROR_NO_UNICODE_TRANSLATION
307 *
308 */
309INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
310 LPWSTR dst, INT dstlen )
311{
312 const union cptable *table;
313 int ret;
314
315#ifdef __WIN32OS2__
316 dprintf2(("MultiByteToWideChar %d %x %x %d %x %d", page, flags, src, srclen, dst, dstlen));
317#endif
318
319 //Docs say source ptr can't be the same as destination (Windows ME, NT4-SP6)
320 if (!src || (!dst && dstlen) || ((void *)src == (void *)dst))
321 {
322 SetLastError( ERROR_INVALID_PARAMETER );
323 return 0;
324 }
325
326 //Even though the docs claim this only works for -1, testing shows it
327 //is done for any negative value (Windows ME, NT4-SP6)
328 if (srclen <= -1) srclen = strlen(src) + 1;
329
330 if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
331
332 switch(page)
333 {
334 case CP_UTF7:
335 FIXME("UTF not supported\n");
336 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
337 return 0;
338 case CP_UTF8:
339 ret = utf8_mbstowcs( flags, src, srclen, dst, dstlen );
340 break;
341 default:
342 if (!(table = get_codepage_table( page )))
343 {
344 SetLastError( ERROR_INVALID_PARAMETER );
345 return 0;
346 }
347 ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
348 break;
349 }
350
351 if (ret < 0)
352 {
353 switch(ret)
354 {
355 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
356 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
357 }
358 ret = 0;
359 }
360 return ret;
361}
362
363
364/***********************************************************************
365 * WideCharToMultiByte (KERNEL32)
366 *
367 * PARAMS
368 * page [in] Codepage character set to convert to
369 * flags [in] Character mapping flags
370 * src [in] Source string buffer
371 * srclen [in] Length of source string buffer
372 * dst [in] Destination buffer
373 * dstlen [in] Length of destination buffer
374 * defchar [in] Default character to use for conversion if no exact
375 * conversion can be made
376 * used [out] Set if default character was used in the conversion
377 *
378 * NOTES
379 * The returned length includes the null terminator character.
380 *
381 * RETURNS
382 * Success: If dstlen > 0, number of characters written to destination
383 * buffer. If dstlen == 0, number of characters needed to do
384 * conversion.
385 * Failure: 0. Occurs if not enough space is available.
386 *
387 * ERRORS
388 * ERROR_INSUFFICIENT_BUFFER
389 * ERROR_INVALID_PARAMETER
390 */
391INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
392 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
393{
394 const union cptable *table;
395 int ret, used_tmp;
396
397#ifdef __WIN32OS2__
398 dprintf2(("WideCharToMultiByte %d %x %x %d %x %d", page, flags, src, srclen, dst, dstlen));
399#endif
400
401 //Docs say source ptr can't be the same as destination (Windows ME, NT4-SP6)
402 if (!src || (!dst && dstlen) || ((void *)src == (void *)dst))
403 {
404 SetLastError( ERROR_INVALID_PARAMETER );
405 return 0;
406 }
407
408 //Even though the docs claim this only works for -1, testing shows it
409 //is done for any negative value (Windows ME, NT4 - SP6)
410 if (srclen <= -1) srclen = strlenW(src) + 1;
411
412 switch(page)
413 {
414 case CP_UTF7:
415 FIXME("UTF-7 not supported\n");
416 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
417 return 0;
418 case CP_UTF8:
419 ret = utf8_wcstombs( src, srclen, dst, dstlen );
420 break;
421 default:
422 if (!(table = get_codepage_table( page )))
423 {
424 SetLastError( ERROR_INVALID_PARAMETER );
425 return 0;
426 }
427 ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen,
428 defchar, used ? &used_tmp : NULL );
429 if (used) *used = used_tmp;
430 break;
431 }
432
433 if (ret == -1)
434 {
435 SetLastError( ERROR_INSUFFICIENT_BUFFER );
436 ret = 0;
437 }
438 return ret;
439}
440
441
442/******************************************************************************
443 * GetStringTypeW (KERNEL32)
444 *
445 */
446BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
447{
448#ifdef __WIN32OS2__
449 dprintf(("GetStringTypeW %x %x %d %x", type, src, count, chartype));
450#endif
451
452 if (count == -1) count = strlenW(src) + 1;
453 switch(type)
454 {
455 case CT_CTYPE1:
456 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
457 break;
458 case CT_CTYPE2:
459 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
460 break;
461 case CT_CTYPE3:
462 FIXME("CT_CTYPE3 not supported.\n");
463 default:
464 SetLastError( ERROR_INVALID_PARAMETER );
465 return FALSE;
466 }
467 return TRUE;
468}
469
470
471/******************************************************************************
472 * GetStringTypeExW (KERNEL32)
473 */
474BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
475{
476 /* locale is ignored for Unicode */
477 return GetStringTypeW( type, src, count, chartype );
478}
479
480/******************************************************************************
481 * GetStringTypeA [KERNEL32.@]
482 */
483BOOL WINAPI GetStringTypeA(LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype)
484{
485 char buf[20];
486 UINT cp;
487 INT countW;
488 LPWSTR srcW;
489 BOOL ret = FALSE;
490
491 if(count == -1) count = strlen(src) + 1;
492
493 if(!GetLocaleInfoA(locale, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_NOUSEROVERRIDE,
494 buf, sizeof(buf)))
495 {
496 FIXME("For locale %04lx using current ANSI code page\n", locale);
497 cp = GetACP();
498 }
499 else
500 cp = atoi(buf);
501
502 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
503 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))) != NULL)
504 {
505 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
506 ret = GetStringTypeW(type, srcW, count, chartype);
507 HeapFree(GetProcessHeap(), 0, srcW);
508 }
509 return ret;
510}
511
512/******************************************************************************
513 * GetStringTypeExA [KERNEL32.@]
514 */
515BOOL WINAPI GetStringTypeExA(LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype)
516{
517 return GetStringTypeA(locale, type, src, count, chartype);
518}
Note: See TracBrowser for help on using the repository browser.