source: trunk/src/kernel32/codepage.cpp@ 5564

Last change on this file since 5564 was 5564, checked in by sandervl, 24 years ago

removed wrong segment definition

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