Removed code for handling ${VAR} constructs in .ini files.
[wine.git] / memory / codepage.c
blob88aa7ce3902ddf5262e8d89b77a5d99f7752562f
1 /*
2 * Code page functions
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "winnls.h"
29 #include "wine/unicode.h"
30 #include "thread.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(string);
35 /* current code pages */
36 static const union cptable *ansi_cptable;
37 static const union cptable *oem_cptable;
38 static const union cptable *mac_cptable;
39 static const union cptable *unix_cptable; /* NULL if UTF8 */
40 static LCID default_lcid = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
42 /* setup default codepage info before we can get at the locale stuff */
43 static void init_codepages(void)
45 ansi_cptable = wine_cp_get_table( 1252 );
46 oem_cptable = wine_cp_get_table( 437 );
47 mac_cptable = wine_cp_get_table( 10000 );
48 unix_cptable = wine_cp_get_table( 28591 );
49 assert( ansi_cptable );
50 assert( oem_cptable );
51 assert( mac_cptable );
52 assert( unix_cptable );
55 /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
56 static const union cptable *get_codepage_table( unsigned int codepage )
58 const union cptable *ret = NULL;
60 if (!ansi_cptable) init_codepages();
62 switch(codepage)
64 case CP_ACP:
65 return ansi_cptable;
66 case CP_OEMCP:
67 return oem_cptable;
68 case CP_MACCP:
69 return mac_cptable;
70 case CP_UTF7:
71 case CP_UTF8:
72 break;
73 case CP_THREAD_ACP:
74 if (!(codepage = NtCurrentTeb()->code_page)) return ansi_cptable;
75 /* fall through */
76 default:
77 if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
78 if (codepage == oem_cptable->info.codepage) return oem_cptable;
79 if (codepage == mac_cptable->info.codepage) return mac_cptable;
80 ret = wine_cp_get_table( codepage );
81 break;
83 return ret;
86 /* initialize default code pages from locale info */
87 /* FIXME: should be done in init_codepages, but it can't right now */
88 /* since it needs KERNEL32 to be loaded for the locale info. */
89 void CODEPAGE_Init( UINT ansi_cp, UINT oem_cp, UINT mac_cp, UINT unix_cp, LCID lcid )
91 extern void __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp );
92 const union cptable *table;
94 default_lcid = lcid;
95 if (!ansi_cptable) init_codepages(); /* just in case */
97 if ((table = wine_cp_get_table( ansi_cp ))) ansi_cptable = table;
98 if ((table = wine_cp_get_table( oem_cp ))) oem_cptable = table;
99 if ((table = wine_cp_get_table( mac_cp ))) mac_cptable = table;
100 if (unix_cp == CP_UTF8)
101 unix_cptable = NULL;
102 else if ((table = wine_cp_get_table( unix_cp )))
103 unix_cptable = table;
105 __wine_init_codepages( ansi_cptable, oem_cptable );
107 TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
108 ansi_cptable->info.codepage, oem_cptable->info.codepage,
109 mac_cptable->info.codepage, unix_cp );
112 /******************************************************************************
113 * GetACP (KERNEL32.@)
115 * RETURNS
116 * Current ANSI code-page identifier, default if no current defined
118 UINT WINAPI GetACP(void)
120 if (!ansi_cptable) return 1252;
121 return ansi_cptable->info.codepage;
125 /***********************************************************************
126 * GetOEMCP (KERNEL32.@)
128 UINT WINAPI GetOEMCP(void)
130 if (!oem_cptable) return 437;
131 return oem_cptable->info.codepage;
135 /***********************************************************************
136 * IsValidCodePage (KERNEL32.@)
138 BOOL WINAPI IsValidCodePage( UINT codepage )
140 switch(codepage) {
141 case CP_SYMBOL:
142 return FALSE;
143 case CP_UTF7:
144 case CP_UTF8:
145 return TRUE;
146 default:
147 return wine_cp_get_table( codepage ) != NULL;
152 /***********************************************************************
153 * GetUserDefaultLangID (KERNEL32.@)
155 LANGID WINAPI GetUserDefaultLangID(void)
157 return LANGIDFROMLCID(default_lcid);
161 /***********************************************************************
162 * GetSystemDefaultLangID (KERNEL32.@)
164 LANGID WINAPI GetSystemDefaultLangID(void)
166 return GetUserDefaultLangID();
170 /***********************************************************************
171 * GetUserDefaultLCID (KERNEL32.@)
173 LCID WINAPI GetUserDefaultLCID(void)
175 return default_lcid;
179 /***********************************************************************
180 * GetSystemDefaultLCID (KERNEL32.@)
182 LCID WINAPI GetSystemDefaultLCID(void)
184 return GetUserDefaultLCID();
188 /***********************************************************************
189 * GetUserDefaultUILanguage (KERNEL32.@)
191 LANGID WINAPI GetUserDefaultUILanguage(void)
193 return GetUserDefaultLangID();
197 /***********************************************************************
198 * GetSystemDefaultUILanguage (KERNEL32.@)
200 LANGID WINAPI GetSystemDefaultUILanguage(void)
202 return GetSystemDefaultLangID();
206 /***********************************************************************
207 * IsDBCSLeadByteEx (KERNEL32.@)
209 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
211 const union cptable *table = get_codepage_table( codepage );
212 return table && is_dbcs_leadbyte( table, testchar );
216 /***********************************************************************
217 * IsDBCSLeadByte (KERNEL32.@)
218 * IsDBCSLeadByte (KERNEL.207)
220 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
222 if (!ansi_cptable) return FALSE;
223 return is_dbcs_leadbyte( ansi_cptable, testchar );
227 /***********************************************************************
228 * GetCPInfo (KERNEL32.@)
230 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
232 const union cptable *table = get_codepage_table( codepage );
234 if (!table)
236 SetLastError( ERROR_INVALID_PARAMETER );
237 return FALSE;
239 if (table->info.def_char & 0xff00)
241 cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
242 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
244 else
246 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
247 cpinfo->DefaultChar[1] = 0;
249 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
250 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
251 else
252 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
254 return TRUE;
258 /***********************************************************************
259 * EnumSystemCodePagesA (KERNEL32.@)
261 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
263 const union cptable *table;
264 char buffer[10];
265 int index = 0;
267 for (;;)
269 if (!(table = wine_cp_enum_table( index++ ))) break;
270 sprintf( buffer, "%d", table->info.codepage );
271 if (!lpfnCodePageEnum( buffer )) break;
273 return TRUE;
277 /***********************************************************************
278 * EnumSystemCodePagesW (KERNEL32.@)
280 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
282 const union cptable *table;
283 WCHAR buffer[10], *p;
284 int page, index = 0;
286 for (;;)
288 if (!(table = wine_cp_enum_table( index++ ))) break;
289 p = buffer + sizeof(buffer)/sizeof(WCHAR);
290 *--p = 0;
291 page = table->info.codepage;
294 *--p = '0' + (page % 10);
295 page /= 10;
296 } while( page );
297 if (!lpfnCodePageEnum( p )) break;
299 return TRUE;
303 /***********************************************************************
304 * MultiByteToWideChar (KERNEL32.@)
306 * PARAMS
307 * page [in] Codepage character set to convert from
308 * flags [in] Character mapping flags
309 * src [in] Source string buffer
310 * srclen [in] Length of source string buffer
311 * dst [in] Destination buffer
312 * dstlen [in] Length of destination buffer
314 * NOTES
315 * The returned length includes the null terminator character.
317 * RETURNS
318 * Success: If dstlen > 0, number of characters written to destination
319 * buffer. If dstlen == 0, number of characters needed to do
320 * conversion.
321 * Failure: 0. Occurs if not enough space is available.
323 * ERRORS
324 * ERROR_INSUFFICIENT_BUFFER
325 * ERROR_INVALID_PARAMETER
326 * ERROR_NO_UNICODE_TRANSLATION
329 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
330 LPWSTR dst, INT dstlen )
332 const union cptable *table;
333 int ret;
335 if (!src || (!dst && dstlen))
337 SetLastError( ERROR_INVALID_PARAMETER );
338 return 0;
341 if (srclen < 0) srclen = strlen(src) + 1;
343 if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
345 switch(page)
347 case CP_UTF7:
348 FIXME("UTF-7 not supported\n");
349 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
350 return 0;
351 case CP_UNIXCP:
352 if (unix_cptable)
354 ret = wine_cp_mbstowcs( unix_cptable, flags, src, srclen, dst, dstlen );
355 break;
357 /* fall through */
358 case CP_UTF8:
359 ret = wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
360 break;
361 default:
362 if (!(table = get_codepage_table( page )))
364 SetLastError( ERROR_INVALID_PARAMETER );
365 return 0;
367 ret = wine_cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
368 break;
371 if (ret < 0)
373 switch(ret)
375 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
376 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
378 ret = 0;
380 return ret;
384 /***********************************************************************
385 * WideCharToMultiByte (KERNEL32.@)
387 * PARAMS
388 * page [in] Codepage character set to convert to
389 * flags [in] Character mapping flags
390 * src [in] Source string buffer
391 * srclen [in] Length of source string buffer
392 * dst [in] Destination buffer
393 * dstlen [in] Length of destination buffer
394 * defchar [in] Default character to use for conversion if no exact
395 * conversion can be made
396 * used [out] Set if default character was used in the conversion
398 * NOTES
399 * The returned length includes the null terminator character.
401 * RETURNS
402 * Success: If dstlen > 0, number of characters written to destination
403 * buffer. If dstlen == 0, number of characters needed to do
404 * conversion.
405 * Failure: 0. Occurs if not enough space is available.
407 * ERRORS
408 * ERROR_INSUFFICIENT_BUFFER
409 * ERROR_INVALID_PARAMETER
411 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
412 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
414 const union cptable *table;
415 int ret, used_tmp;
417 if (!src || (!dst && dstlen))
419 SetLastError( ERROR_INVALID_PARAMETER );
420 return 0;
423 if (srclen < 0) srclen = strlenW(src) + 1;
425 switch(page)
427 case CP_UTF7:
428 FIXME("UTF-7 not supported\n");
429 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
430 return 0;
431 case CP_UNIXCP:
432 if (unix_cptable)
434 ret = wine_cp_wcstombs( unix_cptable, flags, src, srclen, dst, dstlen,
435 defchar, used ? &used_tmp : NULL );
436 break;
438 /* fall through */
439 case CP_UTF8:
440 ret = wine_utf8_wcstombs( src, srclen, dst, dstlen );
441 break;
442 default:
443 if (!(table = get_codepage_table( page )))
445 SetLastError( ERROR_INVALID_PARAMETER );
446 return 0;
448 ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
449 defchar, used ? &used_tmp : NULL );
450 if (used) *used = used_tmp;
451 break;
454 if (ret == -1)
456 SetLastError( ERROR_INSUFFICIENT_BUFFER );
457 ret = 0;
459 return ret;