- Create KD branch. All debugging support is removed in this branch (no symbols,...
[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 x)
56 {
57 if (!HIWORD(x)) return (LPSTR)tolower((char)(int)x);
58 CharLowerBuffA(x, strlen(x));
59 /*
60 __TRY
61 {
62 LPSTR s = x;
63 while (*s)
64 {
65 *s=tolower(*s);
66 s++;
67 }
68 }
69 __EXCEPT(page_fault)
70 {
71 SetLastError( ERROR_INVALID_PARAMETER );
72 return NULL;
73 }
74 __ENDTRY
75 */
76 return x;
77 }
78
79 /*
80 * @implemented
81 */
82 DWORD
83 WINAPI
84 CharLowerBuffA(LPSTR str, DWORD len)
85 {
86 DWORD lenW;
87 WCHAR *strW;
88 if (!str) return 0; /* YES */
89
90 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
91 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
92 if (strW) {
93 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
94 CharLowerBuffW(strW, lenW);
95 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
96 HeapFree(GetProcessHeap(), 0, strW);
97 return len;
98 }
99 return 0;
100 }
101
102 /*
103 * @implemented
104 */
105 DWORD
106 WINAPI
107 CharLowerBuffW(LPWSTR str, DWORD len)
108 {
109 DWORD ret = len;
110 if (!str) return 0; /* YES */
111 for (; len; len--, str++) *str = towlower(*str);
112 return ret;
113 }
114
115 /*
116 * @implemented
117 */
118 LPWSTR
119 WINAPI
120 CharLowerW(LPWSTR x)
121 {
122 if (HIWORD(x)) {
123 return _wcslwr(x);
124 } else {
125 return (LPWSTR)(INT)towlower((WORD)(((DWORD)(x)) & 0xFFFF));
126 }
127 }
128
129 /*
130 * @implemented
131 */
132 LPWSTR
133 WINAPI
134 CharPrevW(LPCWSTR start, LPCWSTR x)
135 {
136 if (x > start) return (LPWSTR)(x-1);
137 else return (LPWSTR)x;
138 }
139
140 /*
141 * @implemented
142 */
143 LPSTR
144 WINAPI
145 CharNextA(LPCSTR ptr)
146 {
147 if (!*ptr) return (LPSTR)ptr;
148 if (IsDBCSLeadByte(ptr[0]) && ptr[1]) return (LPSTR)(ptr + 2);
149 return (LPSTR)(ptr + 1);
150 }
151
152 /*
153 * @implemented
154 */
155 LPSTR
156 WINAPI
157 CharNextExA(WORD codepage, LPCSTR ptr, DWORD flags)
158 {
159 if (!*ptr) return (LPSTR)ptr;
160 if (IsDBCSLeadByteEx(codepage, ptr[0]) && ptr[1]) return (LPSTR)(ptr + 2);
161 return (LPSTR)(ptr + 1);
162 }
163
164 /*
165 * @implemented
166 */
167 LPWSTR
168 WINAPI
169 CharNextW(LPCWSTR x)
170 {
171 if (*x) x++;
172 return (LPWSTR)x;
173 }
174
175 /*
176 * @implemented
177 */
178 LPSTR
179 WINAPI
180 CharPrevA(LPCSTR start, LPCSTR ptr)
181 {
182 while (*start && (start < ptr)) {
183 LPCSTR next = CharNextA(start);
184 if (next >= ptr) break;
185 start = next;
186 }
187 return (LPSTR)start;
188 }
189
190 /*
191 * @implemented
192 */
193 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
194 {
195 while (*start && (start < ptr))
196 {
197 LPCSTR next = CharNextExA( codepage, start, flags );
198 if (next > ptr) break;
199 start = next;
200 }
201 return (LPSTR)start;
202 }
203
204 /*
205 * @implemented
206 */
207 BOOL
208 WINAPI
209 CharToOemA(LPCSTR s, LPSTR d)
210 {
211 if (!s || !d) return TRUE;
212 return CharToOemBuffA(s, d, strlen(s) + 1);
213 }
214
215 /*
216 * @implemented
217 */
218 BOOL
219 WINAPI
220 CharToOemBuffA(LPCSTR s, LPSTR d, DWORD len)
221 {
222 WCHAR* bufW;
223
224 bufW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
225 if (bufW) {
226 MultiByteToWideChar(CP_ACP, 0, s, len, bufW, len);
227 WideCharToMultiByte(CP_OEMCP, 0, bufW, len, d, len, NULL, NULL);
228 HeapFree(GetProcessHeap(), 0, bufW);
229 }
230 return TRUE;
231 }
232
233 /*
234 * @implemented
235 */
236 BOOL
237 WINAPI
238 CharToOemBuffW(LPCWSTR s, LPSTR d, DWORD len)
239 {
240 if (!s || !d)
241 return TRUE;
242 WideCharToMultiByte(CP_OEMCP, 0, s, len, d, len, NULL, NULL);
243 return TRUE;
244 }
245
246 /*
247 * @implemented
248 */
249 BOOL
250 WINAPI
251 CharToOemW(LPCWSTR s, LPSTR d)
252 {
253 return CharToOemBuffW(s, d, wcslen(s) + 1);
254 }
255
256 /*
257 * @implemented
258 */
259 LPSTR WINAPI CharUpperA(LPSTR x)
260 {
261 if (!HIWORD(x)) return (LPSTR)toupper((char)(int)x);
262 CharUpperBuffA(x, strlen(x));
263 return x;
264 }
265
266 /*
267 * @implemented
268 */
269 DWORD
270 WINAPI
271 CharUpperBuffA(LPSTR str, DWORD len)
272 {
273 DWORD lenW;
274 WCHAR* strW;
275 if (!str) return 0; /* YES */
276
277 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
278 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
279 if (strW) {
280 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
281 CharUpperBuffW(strW, lenW);
282 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
283 HeapFree(GetProcessHeap(), 0, strW);
284 return len;
285 }
286 return 0;
287 }
288
289 /*
290 * @implemented
291 */
292 DWORD
293 WINAPI
294 CharUpperBuffW(LPWSTR str, DWORD len)
295 {
296 DWORD ret = len;
297 if (!str) return 0; /* YES */
298 for (; len; len--, str++) *str = towupper(*str);
299 return ret;
300 }
301
302 /*
303 * @implemented
304 */
305 LPWSTR
306 WINAPI
307 CharUpperW(LPWSTR x)
308 {
309 if (HIWORD(x)) return _wcsupr(x);
310 else return (LPWSTR)(UINT)towlower((WORD)(((DWORD)(x)) & 0xFFFF));
311 }
312
313 /*
314 * @implemented
315 */
316 BOOL
317 WINAPI
318 IsCharAlphaA(CHAR Ch)
319 {
320 WCHAR WCh;
321
322 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1);
323 return IsCharAlphaW(WCh);
324 }
325
326 /*
327 * @implemented
328 */
329 BOOL
330 STDCALL
331 IsCharAlphaNumericA(CHAR Ch)
332 {
333 WCHAR WCh;
334
335 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1);
336 return IsCharAlphaNumericW(WCh);
337 }
338
339 /*
340 * @implemented
341 */
342 BOOL
343 STDCALL
344 IsCharAlphaNumericW(WCHAR Ch)
345 {
346 return (GetC1Type(Ch) & (C1_ALPHA|C1_DIGIT)) != 0;
347 }
348
349 /*
350 * @implemented
351 */
352 BOOL
353 WINAPI
354 IsCharAlphaW(WCHAR Ch)
355 {
356 return (GetC1Type(Ch) & C1_ALPHA) != 0;
357 }
358
359 /*
360 * @implemented
361 */
362 BOOL
363 WINAPI
364 IsCharLowerA(CHAR Ch)
365 {
366 WCHAR WCh;
367
368 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1);
369 return IsCharLowerW(WCh);
370 }
371
372 /*
373 * @implemented
374 */
375 BOOL
376 WINAPI
377 IsCharLowerW(WCHAR Ch)
378 {
379 return (GetC1Type(Ch) & C1_LOWER) != 0;
380 }
381
382 /*
383 * @implemented
384 */
385 BOOL
386 WINAPI
387 IsCharUpperA(CHAR Ch)
388 {
389 WCHAR WCh;
390
391 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1);
392 return IsCharUpperW(WCh);
393 }
394
395 /*
396 * @implemented
397 */
398 BOOL
399 WINAPI
400 IsCharUpperW(WCHAR Ch)
401 {
402 return (GetC1Type(Ch) & C1_UPPER) != 0;
403 }
404
405 /*
406 * @implemented
407 */
408 BOOL
409 WINAPI
410 OemToCharA(LPCSTR s, LPSTR d)
411 {
412 return OemToCharBuffA(s, d, strlen(s) + 1);
413 }
414
415 /*
416 * @implemented
417 */
418 BOOL WINAPI OemToCharBuffA(LPCSTR s, LPSTR d, DWORD len)
419 {
420 WCHAR* bufW;
421
422 bufW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
423 if (bufW) {
424 MultiByteToWideChar(CP_OEMCP, 0, s, len, bufW, len);
425 WideCharToMultiByte(CP_ACP, 0, bufW, len, d, len, NULL, NULL);
426 HeapFree(GetProcessHeap(), 0, bufW);
427 }
428 return TRUE;
429 }
430
431 /*
432 * @implemented
433 */
434 BOOL
435 WINAPI
436 OemToCharBuffW(LPCSTR s, LPWSTR d, DWORD len)
437 {
438 MultiByteToWideChar(CP_OEMCP, 0, s, len, d, len);
439 return TRUE;
440 }
441
442 /*
443 * @implemented
444 */
445 BOOL WINAPI OemToCharW(LPCSTR s, LPWSTR d)
446 {
447 return OemToCharBuffW(s, d, strlen(s) + 1);
448 }
449
450 /* EOF */