Sync to trunk head (r42241)
[reactos.git] / reactos / dll / win32 / user32 / windows / text.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id$
20 *
21 * PROJECT: ReactOS user32.dll
22 * FILE: lib/user32/windows/input.c
23 * PURPOSE: Input
24 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
25 * UPDATE HISTORY:
26 * 09-05-2001 CSH Created
27 */
28
29 /* INCLUDES ******************************************************************/
30
31 #include <user32.h>
32
33 #include <wine/debug.h>
34
35 /* FUNCTIONS *****************************************************************/
36
37 static WORD
38 GetC1Type(WCHAR Ch)
39 {
40 WORD CharType;
41
42 if (! GetStringTypeW(CT_CTYPE1, &Ch, 1, &CharType))
43 {
44 return 0;
45 }
46
47 return CharType;
48 }
49
50 /*
51 * @implemented
52 */
53 LPSTR
54 WINAPI
55 CharLowerA(LPSTR str)
56 {
57 if (!HIWORD(str))
58 {
59 char ch = LOWORD(str);
60 CharLowerBuffA( &ch, 1 );
61 return (LPSTR)(UINT_PTR)(BYTE)ch;
62 }
63
64 _SEH2_TRY
65 {
66 CharLowerBuffA( str, strlen(str) );
67 }
68 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
69 {
70 SetLastError( ERROR_INVALID_PARAMETER );
71 return NULL;
72 }
73 _SEH2_END;
74
75 return str;
76 }
77
78 /*
79 * @implemented
80 */
81 DWORD
82 WINAPI
83 CharLowerBuffA(LPSTR str, DWORD len)
84 {
85 DWORD lenW;
86 WCHAR *strW;
87 if (!str) return 0; /* YES */
88
89 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
90 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
91 if (strW) {
92 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
93 CharLowerBuffW(strW, lenW);
94 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
95 HeapFree(GetProcessHeap(), 0, strW);
96 return len;
97 }
98 return 0;
99 }
100
101 /*
102 * @implemented
103 */
104 DWORD
105 WINAPI
106 CharLowerBuffW(LPWSTR str, DWORD len)
107 {
108 DWORD ret = len;
109 if (!str) return 0; /* YES */
110 for (; len; len--, str++) *str = towlower(*str);
111 return ret;
112 }
113
114 /*
115 * @implemented
116 */
117 LPWSTR
118 WINAPI
119 CharLowerW(LPWSTR x)
120 {
121 if (HIWORD(x)) return strlwrW(x);
122 else return (LPWSTR)((UINT_PTR)tolowerW(LOWORD(x)));
123 }
124
125 /*
126 * @implemented
127 */
128 LPWSTR
129 WINAPI
130 CharPrevW(LPCWSTR start, LPCWSTR x)
131 {
132 if (x > start) return (LPWSTR)(x-1);
133 else return (LPWSTR)x;
134 }
135
136 /*
137 * @implemented
138 */
139 LPSTR
140 WINAPI
141 CharNextA(LPCSTR ptr)
142 {
143 if (!*ptr) return (LPSTR)ptr;
144 if (IsDBCSLeadByte(ptr[0]) && ptr[1]) return (LPSTR)(ptr + 2);
145 return (LPSTR)(ptr + 1);
146 }
147
148 /*
149 * @implemented
150 */
151 LPSTR
152 WINAPI
153 CharNextExA(WORD codepage, LPCSTR ptr, DWORD flags)
154 {
155 if (!*ptr) return (LPSTR)ptr;
156 if (IsDBCSLeadByteEx(codepage, ptr[0]) && ptr[1]) return (LPSTR)(ptr + 2);
157 return (LPSTR)(ptr + 1);
158 }
159
160 /*
161 * @implemented
162 */
163 LPWSTR
164 WINAPI
165 CharNextW(LPCWSTR x)
166 {
167 if (*x) x++;
168 return (LPWSTR)x;
169 }
170
171 /*
172 * @implemented
173 */
174 LPSTR
175 WINAPI
176 CharPrevA(LPCSTR start, LPCSTR ptr)
177 {
178 while (*start && (start < ptr)) {
179 LPCSTR next = CharNextA(start);
180 if (next >= ptr) break;
181 start = next;
182 }
183 return (LPSTR)start;
184 }
185
186 /*
187 * @implemented
188 */
189 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
190 {
191 while (*start && (start < ptr))
192 {
193 LPCSTR next = CharNextExA( codepage, start, flags );
194 if (next > ptr) break;
195 start = next;
196 }
197 return (LPSTR)start;
198 }
199
200 /*
201 * @implemented
202 */
203 BOOL
204 WINAPI
205 CharToOemA(LPCSTR s, LPSTR d)
206 {
207 if (!s || !d) return TRUE;
208 return CharToOemBuffA(s, d, strlen(s) + 1);
209 }
210
211 /*
212 * @implemented
213 */
214 BOOL
215 WINAPI
216 CharToOemBuffA(LPCSTR s, LPSTR d, DWORD len)
217 {
218 WCHAR* bufW;
219
220 bufW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
221 if (bufW) {
222 MultiByteToWideChar(CP_ACP, 0, s, len, bufW, len);
223 WideCharToMultiByte(CP_OEMCP, 0, bufW, len, d, len, NULL, NULL);
224 HeapFree(GetProcessHeap(), 0, bufW);
225 }
226 return TRUE;
227 }
228
229 /*
230 * @implemented
231 */
232 BOOL
233 WINAPI
234 CharToOemBuffW(LPCWSTR s, LPSTR d, DWORD len)
235 {
236 if (!s || !d)
237 return TRUE;
238 WideCharToMultiByte(CP_OEMCP, 0, s, len, d, len, NULL, NULL);
239 return TRUE;
240 }
241
242 /*
243 * @implemented
244 */
245 BOOL
246 WINAPI
247 CharToOemW(LPCWSTR s, LPSTR d)
248 {
249 return CharToOemBuffW(s, d, wcslen(s) + 1);
250 }
251
252 /*
253 * @implemented
254 */
255 LPSTR WINAPI CharUpperA(LPSTR str)
256 {
257 if (!HIWORD(str))
258 {
259 char ch = LOWORD(str);
260 CharUpperBuffA( &ch, 1 );
261 return (LPSTR)(UINT_PTR)(BYTE)ch;
262 }
263
264 _SEH2_TRY
265 {
266 CharUpperBuffA( str, strlen(str) );
267 }
268 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
269 {
270 SetLastError( ERROR_INVALID_PARAMETER );
271 return NULL;
272 }
273 _SEH2_END;
274
275 return str;
276 }
277
278 /*
279 * @implemented
280 */
281 DWORD
282 WINAPI
283 CharUpperBuffA(LPSTR str, DWORD len)
284 {
285 DWORD lenW;
286 WCHAR* strW;
287 if (!str) return 0; /* YES */
288
289 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
290 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
291 if (strW) {
292 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
293 CharUpperBuffW(strW, lenW);
294 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
295 HeapFree(GetProcessHeap(), 0, strW);
296 return len;
297 }
298 return 0;
299 }
300
301 /*
302 * @implemented
303 */
304 DWORD
305 WINAPI
306 CharUpperBuffW(LPWSTR str, DWORD len)
307 {
308 DWORD ret = len;
309 if (!str) return 0; /* YES */
310 for (; len; len--, str++) *str = towupper(*str);
311 return ret;
312 }
313
314 /*
315 * @implemented
316 */
317 LPWSTR
318 WINAPI
319 CharUpperW(LPWSTR x)
320 {
321 if (HIWORD(x)) return struprW(x);
322 else return (LPWSTR)((UINT_PTR)toupperW(LOWORD(x)));
323 }
324
325 /*
326 * @implemented
327 */
328 BOOL
329 WINAPI
330 IsCharAlphaA(CHAR Ch)
331 {
332 WCHAR WCh;
333
334 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1);
335 return IsCharAlphaW(WCh);
336 }
337
338 /*
339 * @implemented
340 */
341 BOOL
342 WINAPI
343 IsCharAlphaNumericA(CHAR Ch)
344 {
345 WCHAR WCh;
346
347 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1);
348 return IsCharAlphaNumericW(WCh);
349 }
350
351 /*
352 * @implemented
353 */
354 BOOL
355 WINAPI
356 IsCharAlphaNumericW(WCHAR Ch)
357 {
358 return (GetC1Type(Ch) & (C1_ALPHA|C1_DIGIT)) != 0;
359 }
360
361 /*
362 * @implemented
363 */
364 BOOL
365 WINAPI
366 IsCharAlphaW(WCHAR Ch)
367 {
368 return (GetC1Type(Ch) & C1_ALPHA) != 0;
369 }
370
371 /*
372 * @implemented
373 */
374 BOOL
375 WINAPI
376 IsCharLowerA(CHAR Ch)
377 {
378 WCHAR WCh;
379
380 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1);
381 return IsCharLowerW(WCh);
382 }
383
384 /*
385 * @implemented
386 */
387 BOOL
388 WINAPI
389 IsCharLowerW(WCHAR Ch)
390 {
391 return (GetC1Type(Ch) & C1_LOWER) != 0;
392 }
393
394 /*
395 * @implemented
396 */
397 BOOL
398 WINAPI
399 IsCharUpperA(CHAR Ch)
400 {
401 WCHAR WCh;
402
403 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1);
404 return IsCharUpperW(WCh);
405 }
406
407 /*
408 * @implemented
409 */
410 BOOL
411 WINAPI
412 IsCharUpperW(WCHAR Ch)
413 {
414 return (GetC1Type(Ch) & C1_UPPER) != 0;
415 }
416
417 /*
418 * @implemented
419 */
420 BOOL
421 WINAPI
422 OemToCharA(LPCSTR s, LPSTR d)
423 {
424 return OemToCharBuffA(s, d, strlen(s) + 1);
425 }
426
427 /*
428 * @implemented
429 */
430 BOOL WINAPI OemToCharBuffA(LPCSTR s, LPSTR d, DWORD len)
431 {
432 WCHAR* bufW;
433
434 bufW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
435 if (bufW) {
436 MultiByteToWideChar(CP_OEMCP, 0, s, len, bufW, len);
437 WideCharToMultiByte(CP_ACP, 0, bufW, len, d, len, NULL, NULL);
438 HeapFree(GetProcessHeap(), 0, bufW);
439 }
440 return TRUE;
441 }
442
443 /*
444 * @implemented
445 */
446 BOOL
447 WINAPI
448 OemToCharBuffW(LPCSTR s, LPWSTR d, DWORD len)
449 {
450 MultiByteToWideChar(CP_OEMCP, 0, s, len, d, len);
451 return TRUE;
452 }
453
454 /*
455 * @implemented
456 */
457 BOOL WINAPI OemToCharW(LPCSTR s, LPWSTR d)
458 {
459 return OemToCharBuffW(s, d, strlen(s) + 1);
460 }
461
462 /* EOF */