source: trunk/tools/wrc/u/codepage.c@ 10367

Last change on this file since 10367 was 8938, checked in by sandervl, 23 years ago

PF: Unicode update

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