4 * Copyright 2000 Alexandre Julliard
14 #include "wine/unicode.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(string
);
19 /* current code pages */
20 static const union cptable
*ansi_cptable
;
21 static const union cptable
*oem_cptable
;
22 static const union cptable
*mac_cptable
;
24 /* retrieve a code page table from the locale info */
25 static const union cptable
*get_locale_cp( LCID lcid
, LCTYPE type
)
27 const union cptable
*table
= NULL
;
30 if (GetLocaleInfoA( lcid
, type
, buf
, sizeof(buf
) )) table
= cp_get_table( atoi(buf
) );
34 /* setup default codepage info before we can get at the locale stuff */
35 static void init_codepages(void)
37 ansi_cptable
= cp_get_table( 1252 );
38 oem_cptable
= cp_get_table( 437 );
39 mac_cptable
= cp_get_table( 10000 );
40 assert( ansi_cptable
);
41 assert( oem_cptable
);
42 assert( mac_cptable
);
45 /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
46 static const union cptable
*get_codepage_table( unsigned int codepage
)
48 const union cptable
*ret
= NULL
;
50 if (!ansi_cptable
) init_codepages();
54 case CP_ACP
: return ansi_cptable
;
55 case CP_OEMCP
: return oem_cptable
;
56 case CP_MACCP
: return mac_cptable
;
57 case CP_THREAD_ACP
: return get_locale_cp( GetThreadLocale(), LOCALE_IDEFAULTANSICODEPAGE
);
62 if (codepage
== ansi_cptable
->info
.codepage
) return ansi_cptable
;
63 if (codepage
== oem_cptable
->info
.codepage
) return oem_cptable
;
64 if (codepage
== mac_cptable
->info
.codepage
) return mac_cptable
;
65 ret
= cp_get_table( codepage
);
71 /* initialize default code pages from locale info */
72 /* FIXME: should be done in init_codepages, but it can't right now */
73 /* since it needs KERNEL32 to be loaded for the locale info. */
74 void CODEPAGE_Init(void)
76 const union cptable
*table
;
77 LCID lcid
= GetUserDefaultLCID();
79 if (!ansi_cptable
) init_codepages(); /* just in case */
81 if ((table
= get_locale_cp( lcid
, LOCALE_IDEFAULTANSICODEPAGE
))) ansi_cptable
= table
;
82 if ((table
= get_locale_cp( lcid
, LOCALE_IDEFAULTMACCODEPAGE
))) mac_cptable
= table
;
83 if ((table
= get_locale_cp( lcid
, LOCALE_IDEFAULTCODEPAGE
))) oem_cptable
= table
;
85 TRACE( "ansi=%03d oem=%03d mac=%03d\n", ansi_cptable
->info
.codepage
,
86 oem_cptable
->info
.codepage
, mac_cptable
->info
.codepage
);
89 /******************************************************************************
93 * Current ANSI code-page identifier, default if no current defined
95 UINT WINAPI
GetACP(void)
97 if (!ansi_cptable
) init_codepages();
98 return ansi_cptable
->info
.codepage
;
102 /***********************************************************************
103 * GetOEMCP (KERNEL32)
105 UINT WINAPI
GetOEMCP(void)
107 if (!oem_cptable
) init_codepages();
108 return oem_cptable
->info
.codepage
;
112 /***********************************************************************
113 * IsValidCodePage (KERNEL32)
115 BOOL WINAPI
IsValidCodePage( UINT codepage
)
117 return cp_get_table( codepage
) != NULL
;
121 /***********************************************************************
122 * IsDBCSLeadByteEx (KERNEL32)
124 BOOL WINAPI
IsDBCSLeadByteEx( UINT codepage
, BYTE testchar
)
126 const union cptable
*table
= get_codepage_table( codepage
);
127 return table
&& is_dbcs_leadbyte( table
, testchar
);
131 /***********************************************************************
132 * IsDBCSLeadByte (KERNEL32)
134 BOOL WINAPI
IsDBCSLeadByte( BYTE testchar
)
136 if (!ansi_cptable
) init_codepages();
137 return is_dbcs_leadbyte( ansi_cptable
, testchar
);
141 /***********************************************************************
142 * GetCPInfo (KERNEL32)
144 BOOL WINAPI
GetCPInfo( UINT codepage
, LPCPINFO cpinfo
)
146 const union cptable
*table
= get_codepage_table( codepage
);
150 SetLastError( ERROR_INVALID_PARAMETER
);
153 if (table
->info
.def_char
& 0xff00)
155 cpinfo
->DefaultChar
[0] = table
->info
.def_char
& 0xff00;
156 cpinfo
->DefaultChar
[1] = table
->info
.def_char
& 0x00ff;
160 cpinfo
->DefaultChar
[0] = table
->info
.def_char
& 0xff;
161 cpinfo
->DefaultChar
[1] = 0;
163 if ((cpinfo
->MaxCharSize
= table
->info
.char_size
) == 2)
164 memcpy( cpinfo
->LeadByte
, table
->dbcs
.lead_bytes
, sizeof(cpinfo
->LeadByte
) );
166 cpinfo
->LeadByte
[0] = cpinfo
->LeadByte
[1] = 0;
172 /***********************************************************************
173 * EnumSystemCodePagesA (KERNEL32)
175 BOOL WINAPI
EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum
, DWORD flags
)
177 const union cptable
*table
;
183 if (!(table
= cp_enum_table( index
++ ))) break;
184 sprintf( buffer
, "%d", table
->info
.codepage
);
185 if (!lpfnCodePageEnum( buffer
)) break;
191 /***********************************************************************
192 * EnumSystemCodePagesW (KERNEL32)
194 BOOL WINAPI
EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum
, DWORD flags
)
196 const union cptable
*table
;
197 WCHAR buffer
[10], *p
;
202 if (!(table
= cp_enum_table( index
++ ))) break;
203 p
= buffer
+ sizeof(buffer
)/sizeof(WCHAR
);
205 page
= table
->info
.codepage
;
208 *--p
= '0' + (page
% 10);
211 if (!lpfnCodePageEnum( p
)) break;
217 /***********************************************************************
218 * MultiByteToWideChar (KERNEL32)
221 * page [in] Codepage character set to convert from
222 * flags [in] Character mapping flags
223 * src [in] Source string buffer
224 * srclen [in] Length of source string buffer
225 * dst [in] Destination buffer
226 * dstlen [in] Length of destination buffer
229 * The returned length includes the null terminator character.
232 * Success: If dstlen > 0, number of characters written to destination
233 * buffer. If dstlen == 0, number of characters needed to do
235 * Failure: 0. Occurs if not enough space is available.
238 * ERROR_INSUFFICIENT_BUFFER
239 * ERROR_INVALID_PARAMETER
240 * ERROR_NO_UNICODE_TRANSLATION
243 INT WINAPI
MultiByteToWideChar( UINT page
, DWORD flags
, LPCSTR src
, INT srclen
,
244 LPWSTR dst
, INT dstlen
)
246 const union cptable
*table
;
249 if (!src
|| (!dst
&& dstlen
))
251 SetLastError( ERROR_INVALID_PARAMETER
);
255 if (srclen
== -1) srclen
= strlen(src
) + 1;
257 if (flags
& MB_COMPOSITE
) FIXME("MB_COMPOSITE not supported\n");
258 if (flags
& MB_USEGLYPHCHARS
) FIXME("MB_USEGLYPHCHARS not supported\n");
263 FIXME("UTF not supported\n");
264 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
267 ret
= utf8_mbstowcs( flags
, src
, srclen
, dst
, dstlen
);
270 if (!(table
= get_codepage_table( page
)))
272 SetLastError( ERROR_INVALID_PARAMETER
);
275 ret
= cp_mbstowcs( table
, flags
, src
, srclen
, dst
, dstlen
);
283 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER
); break;
284 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION
); break;
292 /***********************************************************************
293 * WideCharToMultiByte (KERNEL32)
296 * page [in] Codepage character set to convert to
297 * flags [in] Character mapping flags
298 * src [in] Source string buffer
299 * srclen [in] Length of source string buffer
300 * dst [in] Destination buffer
301 * dstlen [in] Length of destination buffer
302 * defchar [in] Default character to use for conversion if no exact
303 * conversion can be made
304 * used [out] Set if default character was used in the conversion
307 * The returned length includes the null terminator character.
310 * Success: If dstlen > 0, number of characters written to destination
311 * buffer. If dstlen == 0, number of characters needed to do
313 * Failure: 0. Occurs if not enough space is available.
316 * ERROR_INSUFFICIENT_BUFFER
317 * ERROR_INVALID_PARAMETER
319 INT WINAPI
WideCharToMultiByte( UINT page
, DWORD flags
, LPCWSTR src
, INT srclen
,
320 LPSTR dst
, INT dstlen
, LPCSTR defchar
, BOOL
*used
)
322 const union cptable
*table
;
325 if (!src
|| (!dst
&& dstlen
))
327 SetLastError( ERROR_INVALID_PARAMETER
);
331 if (srclen
== -1) srclen
= strlenW(src
) + 1;
333 /* if (flags & WC_COMPOSITECHECK) FIXME( "WC_COMPOSITECHECK (%lx) not supported\n", flags );*/
338 FIXME("UTF-7 not supported\n");
339 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
342 ret
= utf8_wcstombs( src
, srclen
, dst
, dstlen
);
345 if (!(table
= get_codepage_table( page
)))
347 SetLastError( ERROR_INVALID_PARAMETER
);
350 ret
= cp_wcstombs( table
, flags
, src
, srclen
, dst
, dstlen
,
351 defchar
, used
? &used_tmp
: NULL
);
352 if (used
) *used
= used_tmp
;
358 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
365 /******************************************************************************
366 * GetStringTypeW (KERNEL32)
369 BOOL WINAPI
GetStringTypeW( DWORD type
, LPCWSTR src
, INT count
, LPWORD chartype
)
371 if (count
== -1) count
= strlenW(src
) + 1;
375 while (count
--) *chartype
++ = get_char_typeW( *src
++ ) & 0xfff;
378 while (count
--) *chartype
++ = get_char_typeW( *src
++ ) >> 12;
381 FIXME("CT_CTYPE3 not supported.\n");
383 SetLastError( ERROR_INVALID_PARAMETER
);
390 /******************************************************************************
391 * GetStringTypeExW (KERNEL32)
393 BOOL WINAPI
GetStringTypeExW( LCID locale
, DWORD type
, LPCWSTR src
, INT count
, LPWORD chartype
)
395 /* locale is ignored for Unicode */
396 return GetStringTypeW( type
, src
, count
, chartype
);