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