1 /* $Id: mbchars.c,v 1.1 2002/12/06 13:14:14 robd Exp $
7 /**********************************************************************
12 * TRUE if CodePage is installed in the system.
17 IsInstalledCP(UINT CodePage
)
24 /**********************************************************************
26 * MultiByteToWideChar@24
30 * CP_ACP ANSI code page
31 * CP_MACCP Macintosh code page
32 * CP_OEMCP OEM code page
33 * (UINT) Any installed code page
38 * MB_ERR_INVALID_CHARS
45 * Size of MultiByteStr, or -1 if MultiByteStr is
52 * Size (in WCHAR unit) of WideCharStr, or 0
53 * if the caller just wants to know how large
54 * WideCharStr should be for a successful
58 * 0 on error; otherwise the number of WCHAR written
59 * in the WideCharStr buffer.
62 * A raw converter for now. It assumes lpMultiByteStr is
63 * NEVER multi-byte (that is each input character is
64 * 8-bit ASCII) and is ALWAYS NULL terminated.
65 * FIXME-FIXME-FIXME-FIXME
72 LPCSTR lpMultiByteStr
,
77 int InStringLength
= 0;
83 * Check the parameters.
85 if (/* --- CODE PAGE --- */
86 ( (CP_ACP
!= CodePage
)
87 && (CP_MACCP
!= CodePage
)
88 && (CP_OEMCP
!= CodePage
)
89 && (FALSE
== IsInstalledCP(CodePage
)) )
91 || (dwFlags
& ~(MB_PRECOMPOSED
| MB_COMPOSITE
|
92 MB_ERR_INVALID_CHARS
| MB_USEGLYPHCHARS
))
93 /* --- INPUT BUFFER --- */
94 || (NULL
== lpMultiByteStr
) )
96 SetLastError (ERROR_INVALID_PARAMETER
);
100 * Compute the input buffer length.
102 if (-1 == cchMultiByte
)
104 InStringLength
= lstrlen(lpMultiByteStr
) + 1;
108 InStringLength
= cchMultiByte
;
111 * Does caller query for output
114 if (0 == cchWideChar
)
116 SetLastError(ERROR_SUCCESS
);
117 return InStringLength
;
120 * Is space provided for the translated
123 if (cchWideChar
< InStringLength
)
125 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
129 * Raw 8- to 16-bit conversion.
131 for (cchConverted
= 0,
132 r
= (PCHAR
)lpMultiByteStr
,
133 w
= (PWCHAR
)lpWideCharStr
;
135 cchConverted
< InStringLength
;
144 * Return how many characters we
145 * wrote in the output buffer.
147 SetLastError(ERROR_SUCCESS
);
152 /**********************************************************************
154 * WideCharToMultiByte@32
156 * Not yet implemented complete (without NLS so far)
160 * CP_ACP ANSI code page
161 * CP_MACCP Macintosh code page
162 * CP_OEMCP OEM code page
163 * CP_SYMBOL Symbol code page (42)
164 * CP_THREAD_ACP Current thread's ANSI code page
165 * CP_UTF7 Translate using UTF-7
166 * CP_UTF8 Translate using UTF-8
167 * (UINT) Any installed code page
170 * WC_NO_BEST_FIT_CHARS
171 * WC_COMPOSITECHECK Convert composite characters to precomposed characters.
172 * WC_DISCARDNS Discard nonspacing characters during conversion.
173 * WC_SEPCHARS Generate separate characters during conversion. This is the default conversion behavior.
174 * WC_DEFAULTCHAR Replace exceptions with the default character during conversion.
177 * Points to the wide-character string to be converted.
180 * Size (in WCHAR unit) of WideCharStr, or 0
181 * if the caller just wants to know how large
182 * WideCharStr should be for a successful
185 * Points to the buffer to receive the translated string.
187 * Specifies the size in bytes of the buffer pointed to by the
188 * lpMultiByteStr parameter. If this value is zero, the function
189 * returns the number of bytes required for the buffer.
191 * Points to the character used if a wide character cannot be
192 * represented in the specified code page. If this parameter is
193 * NULL, a system default value is used.
196 * Points to a flag that indicates whether a default character was used.
197 * This parameter may be NULL.
198 FIXME: allways set to FALSE.
203 * 0 on error; otherwise the number of bytes written
204 * in the lpMultiByteStr buffer. Or the number of
205 * bytes needed for the lpMultiByteStr buffer if cchMultiByte is zero.
208 * A raw converter for now. It just cuts off the upper 9 Bit.
209 * So the MBCS-string does not contain any LeadCharacters
210 * FIXME - FIXME - FIXME - FIXME
218 LPCWSTR lpWideCharStr
,
220 LPSTR lpMultiByteStr
,
222 LPCSTR lpDefaultChar
,
223 LPBOOL lpUsedDefaultChar
226 int wi
, di
; // wide counter, dbcs byte count
229 * Check the parameters.
231 if ( /* --- CODE PAGE --- */
232 ( (CP_ACP
!= CodePage
)
233 && (CP_MACCP
!= CodePage
)
234 && (CP_OEMCP
!= CodePage
)
235 && (CP_SYMBOL
!= CodePage
)
236 && (CP_THREAD_ACP
!= CodePage
)
237 && (CP_UTF7
!= CodePage
)
238 && (CP_UTF8
!= CodePage
)
239 && (FALSE
== IsInstalledCP (CodePage
))
242 || (dwFlags
& ~(/*WC_NO_BEST_FIT_CHARS
243 |*/ WC_COMPOSITECHECK
249 /* --- INPUT BUFFER --- */
250 || (NULL
== lpWideCharStr
)
253 SetLastError(ERROR_INVALID_PARAMETER
);
257 // for now, make no difference but only convert cut the characters to 7Bit
258 if (cchWideChar
== -1) // assume its a 0-terminated str
259 { // and determine its length
260 for (cchWideChar
=0; lpWideCharStr
[cchWideChar
]!=0; cchWideChar
++)
264 // user wants to determine needed space
265 if (cchMultiByte
== 0)
267 SetLastError(ERROR_SUCCESS
);
268 return cchWideChar
; // FIXME: determine correct.
270 // the lpWideCharStr is cchWideChar characters long.
271 for (wi
=0, di
=0; wi
<cchWideChar
&& di
<cchMultiByte
; ++wi
, ++di
)
273 // Flag and a not displayable char FIXME
274 /*if( (dwFlags&WC_NO_BEST_FIT_CHARS) && (lpWideCharStr[wi] >127) )
277 *lpUsedDefaultChar = TRUE;
281 // just cut off the upper 9 Bit, since vals>=128 mean LeadByte.
282 lpMultiByteStr
[di
] = lpWideCharStr
[wi
] & 0x007F;
284 // has MultiByte exceeded but Wide is still in the string?
285 if (wi
< cchWideChar
&& di
>= cchMultiByte
)
287 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
290 // else return # of bytes wirtten to MBCSbuffer (di)
291 SetLastError(ERROR_SUCCESS
);
292 // FIXME: move that elsewhere
293 if (lpUsedDefaultChar
!= NULL
) *lpUsedDefaultChar
= FALSE
;