update for HEAD-2003091401
[reactos.git] / lib / 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 #define __NTAPP__
32
33 #include <windows.h>
34 #include <user32.h>
35
36 //#include <kernel32/winnls.h>
37 #include <ntos/rtl.h>
38
39 #include <debug.h>
40
41
42 const unsigned short wctype_table[] =
43 {
44 };
45
46 /* the character type contains the C1_* flags in the low 12 bits */
47 /* and the C2_* type in the high 4 bits */
48 static inline unsigned short get_char_typeW(WCHAR ch)
49 {
50     extern const unsigned short wctype_table[];
51     return wctype_table[wctype_table[ch >> 8] + (ch & 0xff)];
52 }
53
54
55 /* FUNCTIONS *****************************************************************/
56
57 //LPSTR STDCALL CharLowerA(LPSTR lpsz)
58 /*
59  * @implemented
60  */
61 LPSTR
62 WINAPI
63 CharLowerA(LPSTR x)
64 {
65     if (!HIWORD(x)) return (LPSTR)tolower((char)(int)x);
66 /*
67     __TRY
68     {
69         LPSTR s = x;
70         while (*s)
71         {
72             *s=tolower(*s);
73             s++;
74         }
75     }
76     __EXCEPT(page_fault)
77     {
78         SetLastError( ERROR_INVALID_PARAMETER );
79         return NULL;
80     }
81     __ENDTRY
82  */
83     return x;
84 }
85
86 //DWORD STDCALL CharLowerBuffA(LPSTR lpsz, DWORD cchLength)
87 /*
88  * @implemented
89  */
90 DWORD
91 WINAPI
92 CharLowerBuffA(LPSTR str, DWORD len)
93 {
94     DWORD lenW;
95     WCHAR *strW;
96     if (!str) return 0; /* YES */
97
98     lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
99     strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
100     if (strW) {
101         MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
102         CharLowerBuffW(strW, lenW);
103         len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
104         HeapFree(GetProcessHeap(), 0, strW);
105         return len;
106     }
107     return 0;
108 }
109
110 //DWORD STDCALL CharLowerBuffW(LPWSTR lpsz, DWORD cchLength)
111 /*
112  * @implemented
113  */
114 DWORD
115 WINAPI
116 CharLowerBuffW(LPWSTR str, DWORD len)
117 {
118     DWORD ret = len;
119     if (!str) return 0; /* YES */
120     for (; len; len--, str++) *str = towlower(*str);
121     return ret;
122 }
123
124 //LPWSTR STDCALL CharLowerW(LPWSTR lpsz)
125 /*
126  * @implemented
127  */
128 LPWSTR
129 WINAPI
130 CharLowerW(LPWSTR x)
131 {
132     if (HIWORD(x)) {
133         return _wcslwr(x);
134     } else {
135         return (LPWSTR)(INT)towlower((WORD)(((DWORD)(x)) & 0xFFFF));
136     }
137 }
138
139 //LPWSTR STDCALL CharPrevW(LPCWSTR lpszStart, LPCWSTR lpszCurrent)
140 /*
141  * @implemented
142  */
143 LPWSTR
144 WINAPI
145 CharPrevW(LPCWSTR start, LPCWSTR x)
146 {
147     if (x > start) return (LPWSTR)(x-1);
148     else return (LPWSTR)x;
149 }
150
151 //LPSTR STDCALL CharNextA(LPCSTR lpsz)
152 /*
153  * @implemented
154  */
155 LPSTR
156 WINAPI
157 CharNextA(LPCSTR ptr)
158 {
159     if (!*ptr) return (LPSTR)ptr;
160     if (IsDBCSLeadByte(ptr[0]) && ptr[1]) return (LPSTR)(ptr + 2);
161     return (LPSTR)(ptr + 1);
162 }
163
164 //LPSTR STDCALL CharNextExA(WORD CodePage, LPCSTR lpCurrentChar, DWORD dwFlags)
165 /*
166  * @implemented
167  */
168 LPSTR
169 WINAPI
170 CharNextExA(WORD codepage, LPCSTR ptr, DWORD flags)
171 {
172     if (!*ptr) return (LPSTR)ptr;
173     if (IsDBCSLeadByteEx(codepage, ptr[0]) && ptr[1]) return (LPSTR)(ptr + 2);
174     return (LPSTR)(ptr + 1);
175 }
176
177 //LPWSTR STDCALL CharNextW(LPCWSTR lpsz)
178 /*
179  * @implemented
180  */
181 LPWSTR
182 WINAPI
183 CharNextW(LPCWSTR x)
184 {
185     if (*x) x++;
186     return (LPWSTR)x;
187 }
188
189 //LPSTR STDCALL CharPrevA(LPCSTR lpszStart, LPCSTR lpszCurrent)
190 /*
191  * @implemented
192  */
193 LPSTR
194 WINAPI
195 CharPrevA(LPCSTR start, LPCSTR ptr)
196 {
197     while (*start && (start < ptr)) {
198         LPCSTR next = CharNextA(start);
199         if (next >= ptr) break;
200         start = next;
201     }
202     return (LPSTR)start;
203 }
204
205 //LPSTR STDCALL CharPrevExA(WORD CodePage, LPCSTR lpStart, LPCSTR lpCurrentChar, DWORD dwFlags)
206 /*
207  * @implemented
208  */
209 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
210 {
211     while (*start && (start < ptr))
212     {
213         LPCSTR next = CharNextExA( codepage, start, flags );
214         if (next > ptr) break;
215         start = next;
216     }
217     return (LPSTR)start;
218 }
219
220 //WINBOOL STDCALL CharToOemA(LPCSTR lpszSrc, LPSTR lpszDst)
221 /*
222  * @implemented
223  */
224 BOOL
225 WINAPI
226 CharToOemA(LPCSTR s, LPSTR d)
227 {
228     if (!s || !d) return TRUE;
229     return CharToOemBuffA(s, d, strlen(s) + 1);
230 }
231
232 //WINBOOL STDCALL CharToOemBuffA(LPCSTR lpszSrc, LPSTR lpszDst, DWORD cchDstLength)
233 /*
234  * @implemented
235  */
236 BOOL
237 WINAPI
238 CharToOemBuffA(LPCSTR s, LPSTR d, DWORD len)
239 {
240     WCHAR* bufW;
241
242     bufW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
243     if (bufW) {
244             MultiByteToWideChar(CP_ACP, 0, s, len, bufW, len);
245         WideCharToMultiByte(CP_OEMCP, 0, bufW, len, d, len, NULL, NULL);
246             HeapFree(GetProcessHeap(), 0, bufW);
247     }
248     return TRUE;
249 }
250
251 //WINBOOL STDCALL CharToOemBuffW(LPCWSTR lpszSrc, LPSTR lpszDst, DWORD cchDstLength)
252 /*
253  * @implemented
254  */
255 BOOL
256 WINAPI
257 CharToOemBuffW(LPCWSTR s, LPSTR d, DWORD len)
258 {
259     if (!s || !d)
260         return TRUE;
261     WideCharToMultiByte(CP_OEMCP, 0, s, len, d, len, NULL, NULL);
262     return TRUE;
263 }
264
265 //WINBOOL STDCALL CharToOemW(LPCWSTR lpszSrc, LPSTR lpszDst)
266 /*
267  * @implemented
268  */
269 BOOL
270 WINAPI
271 CharToOemW(LPCWSTR s, LPSTR d)
272 {
273     return CharToOemBuffW(s, d, wcslen(s) + 1);
274 }
275
276 //LPSTR STDCALL CharUpperA(LPSTR lpsz)
277 /*
278  * @implemented
279  */
280 LPSTR WINAPI CharUpperA(LPSTR x)
281 {
282     if (!HIWORD(x)) return (LPSTR)toupper((char)(int)x);
283 /*
284     __TRY
285     {
286         LPSTR s = x;
287         while (*s)
288         {
289             *s=toupper(*s);
290             s++;
291         }
292     }
293     __EXCEPT(page_fault)
294     {
295         SetLastError( ERROR_INVALID_PARAMETER );
296         return NULL;
297     }
298     __ENDTRY
299     return x;
300  */
301   return (LPSTR)NULL;
302 }
303
304 //DWORD STDCALL CharUpperBuffA(LPSTR lpsz, DWORD cchLength)
305 /*
306  * @implemented
307  */
308 DWORD
309 WINAPI
310 CharUpperBuffA(LPSTR str, DWORD len)
311 {
312     DWORD lenW;
313     WCHAR* strW;
314     if (!str) return 0; /* YES */
315
316     lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
317     strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
318     if (strW) {
319         MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
320         CharUpperBuffW(strW, lenW);
321         len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
322         HeapFree(GetProcessHeap(), 0, strW);
323         return len;
324     }
325     return 0;
326 }
327
328 //DWORD STDCALL CharUpperBuffW(LPWSTR lpsz, DWORD cchLength)
329 /*
330  * @implemented
331  */
332 DWORD
333 WINAPI
334 CharUpperBuffW(LPWSTR str, DWORD len)
335 {
336     DWORD ret = len;
337     if (!str) return 0; /* YES */
338     for (; len; len--, str++) *str = towupper(*str);
339     return ret;
340 }
341
342 //LPWSTR STDCALL CharUpperW(LPWSTR lpsz)
343 /*
344  * @implemented
345  */
346 LPWSTR
347 WINAPI
348 CharUpperW(LPWSTR x)
349 {
350     if (HIWORD(x)) return _wcsupr(x);
351     else return (LPWSTR)(UINT)towlower((WORD)(((DWORD)(x)) & 0xFFFF));
352 }
353
354 //WINBOOL STDCALL IsCharAlphaA(CHAR ch)
355 /*
356  * @implemented
357  */
358 BOOL
359 WINAPI
360 IsCharAlphaA(CHAR x)
361 {
362     WCHAR wch;
363     MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
364     return IsCharAlphaW(wch);
365 }
366
367 const char IsCharAlphaNumericA_lookup_table[] = { 
368     0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0xff,  0x03,
369     0xfe,  0xff,  0xff,  0x07,  0xfe,  0xff,  0xff,  0x07,
370     0x08,  0x54,  0x00,  0xd4,  0x00,  0x00,  0x0c,  0x02,
371     0xff,  0xff,  0x7f,  0xff,  0xff,  0xff,  0x7f,  0xff
372 };
373
374 /*
375  * @implemented
376  */
377 WINBOOL
378 STDCALL
379 IsCharAlphaNumericA(CHAR ch)
380 {
381 //    return (IsCharAlphaNumericA_lookup_table[ch / 8] & (1 << (ch % 8))) ? 1 : 0;
382
383     WCHAR wch;
384     MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
385     return IsCharAlphaNumericW(wch);
386   //return FALSE;
387 }
388
389 /*
390  * @implemented
391  */
392 WINBOOL
393 STDCALL
394 IsCharAlphaNumericW(WCHAR ch)
395 {
396     return (get_char_typeW(ch) & (C1_ALPHA|C1_DIGIT)) != 0;
397 //  return FALSE;
398 }
399
400 //WINBOOL STDCALL IsCharAlphaW(WCHAR ch)
401 /*
402  * @implemented
403  */
404 BOOL
405 WINAPI
406 IsCharAlphaW(WCHAR x)
407 {
408     return (get_char_typeW(x) & C1_ALPHA) != 0;
409 }
410
411 //WINBOOL STDCALL IsCharLowerA(CHAR ch)
412 /*
413  * @implemented
414  */
415 BOOL
416 WINAPI
417 IsCharLowerA(CHAR x)
418 {
419     WCHAR wch;
420     MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
421     return IsCharLowerW(wch);
422 }
423
424 //WINBOOL STDCALL IsCharLowerW(WCHAR ch)
425 /*
426  * @implemented
427  */
428 BOOL
429 WINAPI
430 IsCharLowerW(WCHAR x)
431 {
432     return (get_char_typeW(x) & C1_LOWER) != 0;
433 }
434
435 //WINBOOL STDCALL IsCharUpperA(CHAR ch)
436 /*
437  * @implemented
438  */
439 BOOL
440 WINAPI
441 IsCharUpperA(CHAR x)
442 {
443     WCHAR wch;
444     MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
445     return IsCharUpperW(wch);
446 }
447
448 //WINBOOL STDCALL IsCharUpperW(WCHAR ch)
449 /*
450  * @implemented
451  */
452 BOOL
453 WINAPI
454 IsCharUpperW(WCHAR x)
455 {
456     return (get_char_typeW(x) & C1_UPPER) != 0;
457 }
458
459 //WINBOOL STDCALL OemToCharA(LPCSTR lpszSrc, LPSTR lpszDst)
460 /*
461  * @implemented
462  */
463 BOOL
464 WINAPI
465 OemToCharA(LPCSTR s, LPSTR d)
466 {
467     return OemToCharBuffA(s, d, strlen(s) + 1);
468 }
469
470 //WINBOOL STDCALL OemToCharBuffA(LPCSTR lpszSrc, LPSTR lpszDst, DWORD cchDstLength)
471 /*
472  * @implemented
473  */
474 BOOL WINAPI OemToCharBuffA(LPCSTR s, LPSTR d, DWORD len)
475 {
476     WCHAR* bufW;
477
478     bufW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
479     if (bufW) {
480         MultiByteToWideChar(CP_OEMCP, 0, s, len, bufW, len);
481             WideCharToMultiByte(CP_ACP, 0, bufW, len, d, len, NULL, NULL);
482             HeapFree(GetProcessHeap(), 0, bufW);
483     }
484     return TRUE;
485 }
486
487 //WINBOOL STDCALL OemToCharBuffW(LPCSTR lpszSrc, LPWSTR lpszDst, DWORD cchDstLength)
488 /*
489  * @implemented
490  */
491 BOOL
492 WINAPI
493 OemToCharBuffW(LPCSTR s, LPWSTR d, DWORD len)
494 {
495     MultiByteToWideChar(CP_OEMCP, 0, s, len, d, len);
496     return TRUE;
497 }
498
499 //WINBOOL STDCALL OemToCharW(LPCSTR lpszSrc, LPWSTR lpszDst)
500 /*
501  * @implemented
502  */
503 BOOL WINAPI OemToCharW(LPCSTR s, LPWSTR d)
504 {
505     return OemToCharBuffW(s, d, strlen(s) + 1);
506 }