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

Last change on this file since 22017 was 22017, checked in by abwillis, 13 years ago

ADD GetCPInfoExA and GetCPInfoExW part of ticket #25.

File size: 17.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 *
198 * Get information about a code page.
199 *
200 * PARAMS
201 * codepage [I] Code page number
202 * cpinfo [O] Destination for code page information
203 *
204 * RETURNS
205 * Success: TRUE. cpinfo is updated with the information about codepage.
206 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
207 */
208BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
209{
210 const union cptable *table;
211
212#ifdef __WIN32OS2__
213 dprintf(("GetCPInfo %d %x", codepage, cpinfo));
214#endif
215
216 if (!cpinfo)
217 {
218 SetLastError( ERROR_INVALID_PARAMETER );
219 return FALSE;
220 }
221
222 if (!(table = get_codepage_table( codepage )))
223 {
224 switch(codepage)
225 {
226 case CP_UTF7:
227 case CP_UTF8:
228 cpinfo->DefaultChar[0] = 0x3f;
229 cpinfo->DefaultChar[1] = 0;
230 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
231 cpinfo->MaxCharSize = (codepage == CP_UTF7) ? 5 : 4;
232 return TRUE;
233 }
234
235 SetLastError( ERROR_INVALID_PARAMETER );
236 return FALSE;
237 }
238 if (table->info.def_char & 0xff00)
239 {
240 cpinfo->DefaultChar[0] = (table->info.def_char & 0xff00) >> 8;
241 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
242 }
243 else
244 {
245 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
246 cpinfo->DefaultChar[1] = 0;
247 }
248 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
249 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
250 else
251 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
252
253 return TRUE;
254}
255
256/***********************************************************************
257 * GetCPInfoExA (KERNEL32.@)
258 *
259 * Get extended information about a code page.
260 *
261 * PARAMS
262 * codepage [I] Code page number
263 * dwFlags [I] Reserved, must to 0.
264 * cpinfo [O] Destination for code page information
265 *
266 * RETURNS
267 * Success: TRUE. cpinfo is updated with the information about codepage.
268 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
269 */
270BOOL WINAPI GetCPInfoExA( UINT codepage, DWORD dwFlags, LPCPINFOEXA cpinfo )
271{
272 CPINFOEXW cpinfoW;
273
274 if (!GetCPInfoExW( codepage, dwFlags, &cpinfoW ))
275 return FALSE;
276
277 /* the layout is the same except for CodePageName */
278 memcpy(cpinfo, &cpinfoW, sizeof(CPINFOEXA));
279 WideCharToMultiByte(CP_ACP, 0, cpinfoW.CodePageName, -1, cpinfo->CodePageName, sizeof(cpinfo->CodePageName), NULL, NULL);
280 return TRUE;
281}
282
283/***********************************************************************
284 * GetCPInfoExW (KERNEL32.@)
285 *
286 * Unicode version of GetCPInfoExA.
287 */
288BOOL WINAPI GetCPInfoExW( UINT codepage, DWORD dwFlags, LPCPINFOEXW cpinfo )
289{
290 if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
291 return FALSE;
292
293 switch(codepage)
294 {
295 case CP_UTF7:
296 {
297 static const WCHAR utf7[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','7',')',0};
298
299 cpinfo->CodePage = CP_UTF7;
300 cpinfo->UnicodeDefaultChar = 0x3f;
301 strcpyW(cpinfo->CodePageName, utf7);
302 break;
303 }
304
305 case CP_UTF8:
306 {
307 static const WCHAR utf8[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','8',')',0};
308
309 cpinfo->CodePage = CP_UTF8;
310 cpinfo->UnicodeDefaultChar = 0x3f;
311 strcpyW(cpinfo->CodePageName, utf8);
312 break;
313 }
314
315 default:
316 {
317 const union cptable *table = get_codepage_table( codepage );
318
319 cpinfo->CodePage = table->info.codepage;
320 cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
321 MultiByteToWideChar( CP_ACP, 0, table->info.name, -1, cpinfo->CodePageName,
322 sizeof(cpinfo->CodePageName)/sizeof(WCHAR));
323 break;
324 }
325 }
326 return TRUE;
327}
328
329
330/***********************************************************************
331 * EnumSystemCodePagesA (KERNEL32)
332 */
333BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
334{
335 const union cptable *table;
336 char buffer[10];
337 int index = 0;
338
339#ifdef __WIN32OS2__
340 dprintf(("EnumSystemCodePagesA %x %x", lpfnCodePageEnum, flags));
341#endif
342
343 for (;;)
344 {
345 if (!(table = cp_enum_table( index++ ))) break;
346 sprintf( buffer, "%d", table->info.codepage );
347 if (!lpfnCodePageEnum( buffer )) break;
348 }
349 return TRUE;
350}
351
352
353/***********************************************************************
354 * EnumSystemCodePagesW (KERNEL32)
355 */
356BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
357{
358 const union cptable *table;
359 WCHAR buffer[10], *p;
360 int page, index = 0;
361
362#ifdef __WIN32OS2__
363 dprintf(("EnumSystemCodePagesW %x %x", lpfnCodePageEnum, flags));
364#endif
365
366 for (;;)
367 {
368 if (!(table = cp_enum_table( index++ ))) break;
369 p = buffer + sizeof(buffer)/sizeof(WCHAR);
370 *--p = 0;
371 page = table->info.codepage;
372 do
373 {
374 *--p = '0' + (page % 10);
375 page /= 10;
376 } while( page );
377 if (!lpfnCodePageEnum( p )) break;
378 }
379 return TRUE;
380}
381
382
383/***********************************************************************
384 * MultiByteToWideChar (KERNEL32)
385 *
386 * PARAMS
387 * page [in] Codepage character set to convert from
388 * flags [in] Character mapping flags
389 * src [in] Source string buffer
390 * srclen [in] Length of source string buffer
391 * dst [in] Destination buffer
392 * dstlen [in] Length of destination buffer
393 *
394 * NOTES
395 * The returned length includes the null terminator character.
396 *
397 * RETURNS
398 * Success: If dstlen > 0, number of characters written to destination
399 * buffer. If dstlen == 0, number of characters needed to do
400 * conversion.
401 * Failure: 0. Occurs if not enough space is available.
402 *
403 * ERRORS
404 * ERROR_INSUFFICIENT_BUFFER
405 * ERROR_INVALID_PARAMETER
406 * ERROR_NO_UNICODE_TRANSLATION
407 *
408 */
409INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
410 LPWSTR dst, INT dstlen )
411{
412 const union cptable *table;
413 int ret;
414
415#ifdef __WIN32OS2__
416 dprintf2(("MultiByteToWideChar %d %x %x %d %x %d", page, flags, src, srclen, dst, dstlen));
417#endif
418
419 //Docs say source ptr can't be the same as destination (Windows ME, NT4-SP6)
420 if (!src || (!dst && dstlen) || ((void *)src == (void *)dst))
421 {
422 SetLastError( ERROR_INVALID_PARAMETER );
423 return 0;
424 }
425
426 //Even though the docs claim this only works for -1, testing shows it
427 //is done for any negative value (Windows ME, NT4-SP6)
428 if (srclen <= -1) srclen = strlen(src) + 1;
429
430 if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
431
432 switch(page)
433 {
434 case CP_UTF7:
435 FIXME("UTF not supported\n");
436 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
437 return 0;
438 case CP_UTF8:
439 ret = utf8_mbstowcs( flags, src, srclen, dst, dstlen );
440 break;
441 default:
442 if (!(table = get_codepage_table( page )))
443 {
444 SetLastError( ERROR_INVALID_PARAMETER );
445 return 0;
446 }
447 ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
448 break;
449 }
450
451 if (ret < 0)
452 {
453 switch(ret)
454 {
455 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
456 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
457 }
458 ret = 0;
459 }
460 return ret;
461}
462
463
464/***********************************************************************
465 * WideCharToMultiByte (KERNEL32)
466 *
467 * PARAMS
468 * page [in] Codepage character set to convert to
469 * flags [in] Character mapping flags
470 * src [in] Source string buffer
471 * srclen [in] Length of source string buffer
472 * dst [in] Destination buffer
473 * dstlen [in] Length of destination buffer
474 * defchar [in] Default character to use for conversion if no exact
475 * conversion can be made
476 * used [out] Set if default character was used in the conversion
477 *
478 * NOTES
479 * The returned length includes the null terminator character.
480 *
481 * RETURNS
482 * Success: If dstlen > 0, number of characters written to destination
483 * buffer. If dstlen == 0, number of characters needed to do
484 * conversion.
485 * Failure: 0. Occurs if not enough space is available.
486 *
487 * ERRORS
488 * ERROR_INSUFFICIENT_BUFFER
489 * ERROR_INVALID_PARAMETER
490 */
491INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
492 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
493{
494 const union cptable *table;
495 int ret, used_tmp;
496
497#ifdef __WIN32OS2__
498 dprintf2(("WideCharToMultiByte %d %x %x %d %x %d", page, flags, src, srclen, dst, dstlen));
499#endif
500
501 //Docs say source ptr can't be the same as destination (Windows ME, NT4-SP6)
502 if (!src || (!dst && dstlen) || ((void *)src == (void *)dst))
503 {
504 SetLastError( ERROR_INVALID_PARAMETER );
505 return 0;
506 }
507
508 //Even though the docs claim this only works for -1, testing shows it
509 //is done for any negative value (Windows ME, NT4 - SP6)
510 if (srclen <= -1) srclen = strlenW(src) + 1;
511
512 switch(page)
513 {
514 case CP_UTF7:
515 FIXME("UTF-7 not supported\n");
516 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
517 return 0;
518 case CP_UTF8:
519 ret = utf8_wcstombs( src, srclen, dst, dstlen );
520 break;
521 default:
522 if (!(table = get_codepage_table( page )))
523 {
524 SetLastError( ERROR_INVALID_PARAMETER );
525 return 0;
526 }
527 ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen,
528 defchar, used ? &used_tmp : NULL );
529 if (used) *used = used_tmp;
530 break;
531 }
532
533 if (ret == -1)
534 {
535 SetLastError( ERROR_INSUFFICIENT_BUFFER );
536 ret = 0;
537 }
538 return ret;
539}
540
541
542/******************************************************************************
543 * GetStringTypeW (KERNEL32)
544 *
545 */
546BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
547{
548#ifdef __WIN32OS2__
549 dprintf(("GetStringTypeW %x %x %d %x", type, src, count, chartype));
550#endif
551
552 if (count == -1) count = strlenW(src) + 1;
553 switch(type)
554 {
555 case CT_CTYPE1:
556 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
557 break;
558 case CT_CTYPE2:
559 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
560 break;
561 case CT_CTYPE3:
562 FIXME("CT_CTYPE3 not supported.\n");
563 default:
564 SetLastError( ERROR_INVALID_PARAMETER );
565 return FALSE;
566 }
567 return TRUE;
568}
569
570
571/******************************************************************************
572 * GetStringTypeExW (KERNEL32)
573 */
574BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
575{
576 /* locale is ignored for Unicode */
577 return GetStringTypeW( type, src, count, chartype );
578}
579
580/******************************************************************************
581 * GetStringTypeA [KERNEL32.@]
582 */
583BOOL WINAPI GetStringTypeA(LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype)
584{
585 char buf[20];
586 UINT cp;
587 INT countW;
588 LPWSTR srcW;
589 BOOL ret = FALSE;
590
591 if(count == -1) count = strlen(src) + 1;
592
593 if(!GetLocaleInfoA(locale, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_NOUSEROVERRIDE,
594 buf, sizeof(buf)))
595 {
596 FIXME("For locale %04lx using current ANSI code page\n", locale);
597 cp = GetACP();
598 }
599 else
600 cp = atoi(buf);
601
602 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
603 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))) != NULL)
604 {
605 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
606 ret = GetStringTypeW(type, srcW, count, chartype);
607 HeapFree(GetProcessHeap(), 0, srcW);
608 }
609 return ret;
610}
611
612/******************************************************************************
613 * GetStringTypeExA [KERNEL32.@]
614 */
615BOOL WINAPI GetStringTypeExA(LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype)
616{
617 return GetStringTypeA(locale, type, src, count, chartype);
618}
Note: See TracBrowser for help on using the repository browser.