branch update for HEAD-2003021201
[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 LPSTR
59 WINAPI
60 CharLowerA(LPSTR x)
61 {
62     if (!HIWORD(x)) return (LPSTR)tolower((char)(int)x);
63 /*
64     __TRY
65     {
66         LPSTR s = x;
67         while (*s)
68         {
69             *s=tolower(*s);
70             s++;
71         }
72     }
73     __EXCEPT(page_fault)
74     {
75         SetLastError( ERROR_INVALID_PARAMETER );
76         return NULL;
77     }
78     __ENDTRY
79  */
80     return x;
81 }
82
83 //DWORD STDCALL CharLowerBuffA(LPSTR lpsz, DWORD cchLength)
84 DWORD
85 WINAPI
86 CharLowerBuffA(LPSTR str, DWORD len)
87 {
88     DWORD lenW;
89     WCHAR *strW;
90     if (!str) return 0; /* YES */
91
92     lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
93     strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
94     if (strW) {
95         MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
96         CharLowerBuffW(strW, lenW);
97         len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
98         HeapFree(GetProcessHeap(), 0, strW);
99         return len;
100     }
101     return 0;
102 }
103
104 //DWORD STDCALL CharLowerBuffW(LPWSTR lpsz, DWORD cchLength)
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 //LPWSTR STDCALL CharLowerW(LPWSTR lpsz)
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 //LPWSTR STDCALL CharPrevW(LPCWSTR lpszStart, LPCWSTR lpszCurrent)
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 //LPSTR STDCALL CharNextA(LPCSTR lpsz)
137 LPSTR
138 WINAPI
139 CharNextA(LPCSTR ptr)
140 {
141     if (!*ptr) return (LPSTR)ptr;
142     if (IsDBCSLeadByte(ptr[0]) && ptr[1]) return (LPSTR)(ptr + 2);
143     return (LPSTR)(ptr + 1);
144 }
145
146 //LPSTR STDCALL CharNextExA(WORD CodePage, LPCSTR lpCurrentChar, DWORD dwFlags)
147 LPSTR
148 WINAPI
149 CharNextExA(WORD codepage, LPCSTR ptr, DWORD flags)
150 {
151     if (!*ptr) return (LPSTR)ptr;
152     if (IsDBCSLeadByteEx(codepage, ptr[0]) && ptr[1]) return (LPSTR)(ptr + 2);
153     return (LPSTR)(ptr + 1);
154 }
155
156 //LPWSTR STDCALL CharNextW(LPCWSTR lpsz)
157 LPWSTR
158 WINAPI
159 CharNextW(LPCWSTR x)
160 {
161     if (*x) x++;
162     return (LPWSTR)x;
163 }
164
165 //LPSTR STDCALL CharPrevA(LPCSTR lpszStart, LPCSTR lpszCurrent)
166 LPSTR
167 WINAPI
168 CharPrevA(LPCSTR start, LPCSTR ptr)
169 {
170     while (*start && (start < ptr)) {
171         LPCSTR next = CharNextA(start);
172         if (next >= ptr) break;
173         start = next;
174     }
175     return (LPSTR)start;
176 }
177
178 //LPSTR STDCALL CharPrevExA(WORD CodePage, LPCSTR lpStart, LPCSTR lpCurrentChar, DWORD dwFlags)
179 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
180 {
181     while (*start && (start < ptr))
182     {
183         LPCSTR next = CharNextExA( codepage, start, flags );
184         if (next > ptr) break;
185         start = next;
186     }
187     return (LPSTR)start;
188 }
189
190 //WINBOOL STDCALL CharToOemA(LPCSTR lpszSrc, LPSTR lpszDst)
191 BOOL
192 WINAPI
193 CharToOemA(LPCSTR s, LPSTR d)
194 {
195     if (!s || !d) return TRUE;
196     return CharToOemBuffA(s, d, strlen(s) + 1);
197 }
198
199 //WINBOOL STDCALL CharToOemBuffA(LPCSTR lpszSrc, LPSTR lpszDst, DWORD cchDstLength)
200 BOOL
201 WINAPI
202 CharToOemBuffA(LPCSTR s, LPSTR d, DWORD len)
203 {
204     WCHAR* bufW;
205
206     bufW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
207     if (bufW) {
208             MultiByteToWideChar(CP_ACP, 0, s, len, bufW, len);
209         WideCharToMultiByte(CP_OEMCP, 0, bufW, len, d, len, NULL, NULL);
210             HeapFree(GetProcessHeap(), 0, bufW);
211     }
212     return TRUE;
213 }
214
215 //WINBOOL STDCALL CharToOemBuffW(LPCWSTR lpszSrc, LPSTR lpszDst, DWORD cchDstLength)
216 BOOL
217 WINAPI
218 CharToOemBuffW(LPCWSTR s, LPSTR d, DWORD len)
219 {
220     if (!s || !d)
221         return TRUE;
222     WideCharToMultiByte(CP_OEMCP, 0, s, len, d, len, NULL, NULL);
223     return TRUE;
224 }
225
226 //WINBOOL STDCALL CharToOemW(LPCWSTR lpszSrc, LPSTR lpszDst)
227 BOOL
228 WINAPI
229 CharToOemW(LPCWSTR s, LPSTR d)
230 {
231     return CharToOemBuffW(s, d, wcslen(s) + 1);
232 }
233
234 //LPSTR STDCALL CharUpperA(LPSTR lpsz)
235 LPSTR WINAPI CharUpperA(LPSTR x)
236 {
237     if (!HIWORD(x)) return (LPSTR)toupper((char)(int)x);
238 /*
239     __TRY
240     {
241         LPSTR s = x;
242         while (*s)
243         {
244             *s=toupper(*s);
245             s++;
246         }
247     }
248     __EXCEPT(page_fault)
249     {
250         SetLastError( ERROR_INVALID_PARAMETER );
251         return NULL;
252     }
253     __ENDTRY
254     return x;
255  */
256   return (LPSTR)NULL;
257 }
258
259 //DWORD STDCALL CharUpperBuffA(LPSTR lpsz, DWORD cchLength)
260 DWORD
261 WINAPI
262 CharUpperBuffA(LPSTR str, DWORD len)
263 {
264     DWORD lenW;
265     WCHAR* strW;
266     if (!str) return 0; /* YES */
267
268     lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
269     strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
270     if (strW) {
271         MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
272         CharUpperBuffW(strW, lenW);
273         len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
274         HeapFree(GetProcessHeap(), 0, strW);
275         return len;
276     }
277     return 0;
278 }
279
280 //DWORD STDCALL CharUpperBuffW(LPWSTR lpsz, DWORD cchLength)
281 DWORD
282 WINAPI
283 CharUpperBuffW(LPWSTR str, DWORD len)
284 {
285     DWORD ret = len;
286     if (!str) return 0; /* YES */
287     for (; len; len--, str++) *str = towupper(*str);
288     return ret;
289 }
290
291 //LPWSTR STDCALL CharUpperW(LPWSTR lpsz)
292 LPWSTR
293 WINAPI
294 CharUpperW(LPWSTR x)
295 {
296     if (HIWORD(x)) return _wcsupr(x);
297     else return (LPWSTR)(UINT)towlower((WORD)(((DWORD)(x)) & 0xFFFF));
298 }
299
300 //WINBOOL STDCALL IsCharAlphaA(CHAR ch)
301 BOOL
302 WINAPI
303 IsCharAlphaA(CHAR x)
304 {
305     WCHAR wch;
306     MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
307     return IsCharAlphaW(wch);
308 }
309
310 const char IsCharAlphaNumericA_lookup_table[] = { 
311     0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0xff,  0x03,
312     0xfe,  0xff,  0xff,  0x07,  0xfe,  0xff,  0xff,  0x07,
313     0x08,  0x54,  0x00,  0xd4,  0x00,  0x00,  0x0c,  0x02,
314     0xff,  0xff,  0x7f,  0xff,  0xff,  0xff,  0x7f,  0xff
315 };
316
317 WINBOOL
318 STDCALL
319 IsCharAlphaNumericA(CHAR ch)
320 {
321 //    return (IsCharAlphaNumericA_lookup_table[ch / 8] & (1 << (ch % 8))) ? 1 : 0;
322
323     WCHAR wch;
324     MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
325     return IsCharAlphaNumericW(wch);
326   //return FALSE;
327 }
328
329 WINBOOL
330 STDCALL
331 IsCharAlphaNumericW(WCHAR ch)
332 {
333     return (get_char_typeW(ch) & (C1_ALPHA|C1_DIGIT)) != 0;
334 //  return FALSE;
335 }
336
337 //WINBOOL STDCALL IsCharAlphaW(WCHAR ch)
338 BOOL
339 WINAPI
340 IsCharAlphaW(WCHAR x)
341 {
342     return (get_char_typeW(x) & C1_ALPHA) != 0;
343 }
344
345 //WINBOOL STDCALL IsCharLowerA(CHAR ch)
346 BOOL
347 WINAPI
348 IsCharLowerA(CHAR x)
349 {
350     WCHAR wch;
351     MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
352     return IsCharLowerW(wch);
353 }
354
355 //WINBOOL STDCALL IsCharLowerW(WCHAR ch)
356 BOOL
357 WINAPI
358 IsCharLowerW(WCHAR x)
359 {
360     return (get_char_typeW(x) & C1_LOWER) != 0;
361 }
362
363 //WINBOOL STDCALL IsCharUpperA(CHAR ch)
364 BOOL
365 WINAPI
366 IsCharUpperA(CHAR x)
367 {
368     WCHAR wch;
369     MultiByteToWideChar(CP_ACP, 0, &x, 1, &wch, 1);
370     return IsCharUpperW(wch);
371 }
372
373 //WINBOOL STDCALL IsCharUpperW(WCHAR ch)
374 BOOL
375 WINAPI
376 IsCharUpperW(WCHAR x)
377 {
378     return (get_char_typeW(x) & C1_UPPER) != 0;
379 }
380
381 //WINBOOL STDCALL OemToCharA(LPCSTR lpszSrc, LPSTR lpszDst)
382 BOOL
383 WINAPI
384 OemToCharA(LPCSTR s, LPSTR d)
385 {
386     return OemToCharBuffA(s, d, strlen(s) + 1);
387 }
388
389 //WINBOOL STDCALL OemToCharBuffA(LPCSTR lpszSrc, LPSTR lpszDst, DWORD cchDstLength)
390 BOOL WINAPI OemToCharBuffA(LPCSTR s, LPSTR d, DWORD len)
391 {
392     WCHAR* bufW;
393
394     bufW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
395     if (bufW) {
396         MultiByteToWideChar(CP_OEMCP, 0, s, len, bufW, len);
397             WideCharToMultiByte(CP_ACP, 0, bufW, len, d, len, NULL, NULL);
398             HeapFree(GetProcessHeap(), 0, bufW);
399     }
400     return TRUE;
401 }
402
403 //WINBOOL STDCALL OemToCharBuffW(LPCSTR lpszSrc, LPWSTR lpszDst, DWORD cchDstLength)
404 BOOL
405 WINAPI
406 OemToCharBuffW(LPCSTR s, LPWSTR d, DWORD len)
407 {
408     MultiByteToWideChar(CP_OEMCP, 0, s, len, d, len);
409     return TRUE;
410 }
411
412 //WINBOOL STDCALL OemToCharW(LPCSTR lpszSrc, LPWSTR lpszDst)
413 BOOL WINAPI OemToCharW(LPCSTR s, LPWSTR d)
414 {
415     return OemToCharBuffW(s, d, strlen(s) + 1);
416 }