4 * Copyright 2000 Alexandre Julliard
13 #include "wine/unicode.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(string);
18 /* current code pages */
19 static unsigned int ansi_cp = 1252; /* Windows 3.1 ISO Latin */
20 static unsigned int oem_cp = 437; /* MS-DOS United States */
21 static unsigned int mac_cp = 10000; /* Mac Roman */
23 static const union cptable *ansi_cptable;
24 static const union cptable *oem_cptable;
25 static const union cptable *mac_cptable;
28 /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
29 static const union cptable *get_codepage_table( unsigned int codepage )
31 const union cptable *ret = NULL;
33 if (!ansi_cptable) /* initialize them */
35 /* FIXME: should load from the registry */
36 ansi_cptable = cp_get_table( ansi_cp );
37 oem_cptable = cp_get_table( oem_cp );
38 mac_cptable = cp_get_table( mac_cp );
39 assert( ansi_cptable );
40 assert( oem_cptable );
41 assert( mac_cptable );
46 case CP_ACP: return ansi_cptable;
47 case CP_OEMCP: return oem_cptable;
48 case CP_MACCP: return mac_cptable;
49 case CP_THREAD_ACP: return ansi_cptable; /* FIXME */
54 if (codepage == ansi_cp) return ansi_cptable;
55 if (codepage == oem_cp) return oem_cptable;
56 if (codepage == mac_cp) return mac_cptable;
57 ret = cp_get_table( codepage );
63 /******************************************************************************
67 * Current ANSI code-page identifier, default if no current defined
69 UINT WINAPI GetACP(void)
75 /***********************************************************************
78 UINT WINAPI GetOEMCP(void)
84 /***********************************************************************
85 * IsValidCodePage (KERNEL32)
87 BOOL WINAPI IsValidCodePage( UINT codepage )
89 return cp_get_table( codepage ) != NULL;
93 /***********************************************************************
94 * IsDBCSLeadByteEx (KERNEL32)
96 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
98 const union cptable *table = get_codepage_table( codepage );
99 return table && is_dbcs_leadbyte( table, testchar );
103 /***********************************************************************
104 * IsDBCSLeadByte (KERNEL32)
106 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
108 return is_dbcs_leadbyte( ansi_cptable, testchar );
112 /***********************************************************************
113 * GetCPInfo (KERNEL32)
115 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
117 const union cptable *table = get_codepage_table( codepage );
121 SetLastError( ERROR_INVALID_PARAMETER );
124 if (table->info.def_char & 0xff00)
126 cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
127 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
131 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
132 cpinfo->DefaultChar[1] = 0;
134 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
135 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
137 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
143 /***********************************************************************
144 * EnumSystemCodePagesA (KERNEL32)
146 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
148 const union cptable *table;
154 if (!(table = cp_enum_table( index++ ))) break;
155 sprintf( buffer, "%d", table->info.codepage );
156 if (!lpfnCodePageEnum( buffer )) break;
162 /***********************************************************************
163 * EnumSystemCodePagesW (KERNEL32)
165 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
167 const union cptable *table;
168 WCHAR buffer[10], *p;
173 if (!(table = cp_enum_table( index++ ))) break;
174 p = buffer + sizeof(buffer)/sizeof(WCHAR);
176 page = table->info.codepage;
179 *--p = '0' + (page % 10);
182 if (!lpfnCodePageEnum( p )) break;
188 /***********************************************************************
189 * MultiByteToWideChar (KERNEL32)
192 * page [in] Codepage character set to convert from
193 * flags [in] Character mapping flags
194 * src [in] Source string buffer
195 * srclen [in] Length of source string buffer
196 * dst [in] Destination buffer
197 * dstlen [in] Length of destination buffer
200 * The returned length includes the null terminator character.
203 * Success: If dstlen > 0, number of characters written to destination
204 * buffer. If dstlen == 0, number of characters needed to do
206 * Failure: 0. Occurs if not enough space is available.
209 * ERROR_INSUFFICIENT_BUFFER
210 * ERROR_INVALID_PARAMETER
211 * ERROR_NO_UNICODE_TRANSLATION
214 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
215 LPWSTR dst, INT dstlen )
217 const union cptable *table;
220 if (srclen == -1) srclen = strlen(src) + 1;
224 FIXME("UTF not supported\n");
225 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
229 if (!(table = get_codepage_table( page )))
231 SetLastError( ERROR_INVALID_PARAMETER );
235 if (flags & MB_COMPOSITE) FIXME("MB_COMPOSITE not supported\n");
236 if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
238 ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
244 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
245 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
253 /***********************************************************************
254 * WideCharToMultiByte (KERNEL32)
257 * page [in] Codepage character set to convert to
258 * flags [in] Character mapping flags
259 * src [in] Source string buffer
260 * srclen [in] Length of source string buffer
261 * dst [in] Destination buffer
262 * dstlen [in] Length of destination buffer
263 * defchar [in] Default character to use for conversion if no exact
264 * conversion can be made
265 * used [out] Set if default character was used in the conversion
268 * The returned length includes the null terminator character.
271 * Success: If dstlen > 0, number of characters written to destination
272 * buffer. If dstlen == 0, number of characters needed to do
274 * Failure: 0. Occurs if not enough space is available.
277 * ERROR_INSUFFICIENT_BUFFER
278 * ERROR_INVALID_PARAMETER
280 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
281 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
283 const union cptable *table;
286 if (srclen == -1) srclen = strlenW(src) + 1;
290 FIXME("UTF not supported\n");
291 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
295 if (!(table = get_codepage_table( page )))
297 SetLastError( ERROR_INVALID_PARAMETER );
301 /* if (flags & WC_COMPOSITECHECK) FIXME( "WC_COMPOSITECHECK (%lx) not supported\n", flags );*/
303 ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen, defchar, used ? &used_tmp : NULL );
304 if (used) *used = used_tmp;
308 SetLastError( ERROR_INSUFFICIENT_BUFFER );