[Win32SS]
[reactos.git] / reactos / win32ss / user / 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 /*
20 * PROJECT: ReactOS user32.dll
21 * FILE: win32ss/user/user32/windows/text.c
22 * PURPOSE: Input
23 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
24 * UPDATE HISTORY:
25 * 09-05-2001 CSH Created
26 */
27
28 /* INCLUDES ******************************************************************/
29
30 #include <user32.h>
31
32 #include <wine/debug.h>
33
34 /* FUNCTIONS *****************************************************************/
35
36 static WORD
37 GetC1Type(WCHAR Ch)
38 {
39 WORD CharType;
40
41 if (! GetStringTypeW(CT_CTYPE1, &Ch, 1, &CharType))
42 {
43 return 0;
44 }
45
46 return CharType;
47 }
48
49 /*
50 * @implemented
51 */
52 LPSTR
53 WINAPI
54 CharLowerA(LPSTR str)
55 {
56 if (!HIWORD(str))
57 {
58 char ch = LOWORD(str);
59 CharLowerBuffA( &ch, 1 );
60 return (LPSTR)(UINT_PTR)(BYTE)ch;
61 }
62
63 _SEH2_TRY
64 {
65 CharLowerBuffA( str, strlen(str) );
66 }
67 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
68 {
69 SetLastError( ERROR_INVALID_PARAMETER );
70 _SEH2_YIELD(return NULL);
71 }
72 _SEH2_END;
73
74 return str;
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)) return strlwrW(x);
121 else return (LPWSTR)((UINT_PTR)tolowerW(LOWORD(x)));
122 }
123
124 /*
125 * @implemented
126 */
127 LPWSTR
128 WINAPI
129 CharPrevW(LPCWSTR start, LPCWSTR x)
130 {
131 if (x > start) return (LPWSTR)(x-1);
132 else return (LPWSTR)x;
133 }
134
135 /*
136 * @implemented
137 */
138 LPSTR
139 WINAPI
140 CharNextA(LPCSTR ptr)
141 {
142 if (!*ptr) return (LPSTR)ptr;
143 if (IsDBCSLeadByte(ptr[0]) && ptr[1]) return (LPSTR)(ptr + 2);
144 return (LPSTR)(ptr + 1);
145 }
146
147 /*
148 * @implemented
149 */
150 LPSTR
151 WINAPI
152 CharNextExA(WORD codepage, LPCSTR ptr, DWORD flags)
153 {
154 if (!*ptr) return (LPSTR)ptr;
155 if (IsDBCSLeadByteEx(codepage, ptr[0]) && ptr[1]) return (LPSTR)(ptr + 2);
156 return (LPSTR)(ptr + 1);
157 }
158
159 /*
160 * @implemented
161 */
162 LPWSTR
163 WINAPI
164 CharNextW(LPCWSTR x)
165 {
166 if (*x) x++;
167 return (LPWSTR)x;
168 }
169
170 /*
171 * @implemented
172 */
173 LPSTR
174 WINAPI
175 CharPrevA(LPCSTR start, LPCSTR ptr)
176 {
177 while (*start && (start < ptr)) {
178 LPCSTR next = CharNextA(start);
179 if (next >= ptr) break;
180 start = next;
181 }
182 return (LPSTR)start;
183 }
184
185 /*
186 * @implemented
187 */
188 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
189 {
190 while (*start && (start < ptr))
191 {
192 LPCSTR next = CharNextExA( codepage, start, flags );
193 if (next > ptr) break;
194 start = next;
195 }
196 return (LPSTR)start;
197 }
198
199 /*
200 * @implemented
201 */
202 BOOL
203 WINAPI
204 CharToOemA(LPCSTR s, LPSTR d)
205 {
206 if (!s || !d) return FALSE;
207 return CharToOemBuffA(s, d, strlen(s) + 1);
208 }
209
210 /*
211 * @implemented
212 */
213 BOOL
214 WINAPI
215 CharToOemBuffA(LPCSTR s, LPSTR d, DWORD len)
216 {
217 WCHAR* bufW;
218
219 if ( !s || !d ) return FALSE;
220
221 bufW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
222 if (bufW) {
223 MultiByteToWideChar(CP_ACP, 0, s, len, bufW, len);
224 WideCharToMultiByte(CP_OEMCP, 0, bufW, len, d, len, NULL, NULL);
225 HeapFree(GetProcessHeap(), 0, bufW);
226 }
227 return TRUE;
228 }
229
230 /*
231 * @implemented
232 */
233 BOOL
234 WINAPI
235 CharToOemBuffW(LPCWSTR s, LPSTR d, DWORD len)
236 {
237 if (!s || !d)
238 return FALSE;
239 WideCharToMultiByte(CP_OEMCP, 0, s, len, d, len, NULL, NULL);
240 return TRUE;
241 }
242
243 /*
244 * @implemented
245 */
246 BOOL
247 WINAPI
248 CharToOemW(LPCWSTR s, LPSTR d)
249 {
250 if ( !s || !d ) return FALSE;
251 return CharToOemBuffW(s, d, wcslen(s) + 1);
252 }
253
254 /*
255 * @implemented
256 */
257 LPSTR WINAPI CharUpperA(LPSTR str)
258 {
259 if (!HIWORD(str))
260 {
261 char ch = LOWORD(str);
262 CharUpperBuffA( &ch, 1 );
263 return (LPSTR)(UINT_PTR)(BYTE)ch;
264 }
265
266 _SEH2_TRY
267 {
268 CharUpperBuffA( str, strlen(str) );
269 }
270 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
271 {
272 SetLastError( ERROR_INVALID_PARAMETER );
273 _SEH2_YIELD(return NULL);
274 }
275 _SEH2_END;
276
277 return str;
278 }
279
280 /*
281 * @implemented
282 */
283 DWORD
284 WINAPI
285 CharUpperBuffA(LPSTR str, DWORD len)
286 {
287 DWORD lenW;
288 WCHAR* strW;
289 if (!str) return 0; /* YES */
290
291 lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
292 strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
293 if (strW) {
294 MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
295 CharUpperBuffW(strW, lenW);
296 len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
297 HeapFree(GetProcessHeap(), 0, strW);
298 return len;
299 }
300 return 0;
301 }
302
303 /*
304 * @implemented
305 */
306 DWORD
307 WINAPI
308 CharUpperBuffW(LPWSTR str, DWORD len)
309 {
310 DWORD ret = len;
311 if (!str) return 0; /* YES */
312 for (; len; len--, str++) *str = towupper(*str);
313 return ret;
314 }
315
316 /*
317 * @implemented
318 */
319 LPWSTR
320 WINAPI
321 CharUpperW(LPWSTR x)
322 {
323 if (HIWORD(x)) return struprW(x);
324 else return (LPWSTR)((UINT_PTR)toupperW(LOWORD(x)));
325 }
326
327 /*
328 * @implemented
329 */
330 BOOL
331 WINAPI
332 IsCharAlphaA(CHAR Ch)
333 {
334 WCHAR WCh;
335
336 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1);
337 return IsCharAlphaW(WCh);
338 }
339
340 /*
341 * @implemented
342 */
343 BOOL
344 WINAPI
345 IsCharAlphaNumericA(CHAR Ch)
346 {
347 WCHAR WCh;
348
349 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1);
350 return IsCharAlphaNumericW(WCh);
351 }
352
353 /*
354 * @implemented
355 */
356 BOOL
357 WINAPI
358 IsCharAlphaNumericW(WCHAR Ch)
359 {
360 return (GetC1Type(Ch) & (C1_ALPHA|C1_DIGIT)) != 0;
361 }
362
363 /*
364 * @implemented
365 */
366 BOOL
367 WINAPI
368 IsCharAlphaW(WCHAR Ch)
369 {
370 return (GetC1Type(Ch) & C1_ALPHA) != 0;
371 }
372
373 /*
374 * @implemented
375 */
376 BOOL
377 WINAPI
378 IsCharLowerA(CHAR Ch)
379 {
380 WCHAR WCh;
381
382 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1);
383 return IsCharLowerW(WCh);
384 }
385
386 /*
387 * @implemented
388 */
389 BOOL
390 WINAPI
391 IsCharLowerW(WCHAR Ch)
392 {
393 return (GetC1Type(Ch) & C1_LOWER) != 0;
394 }
395
396 /*
397 * @implemented
398 */
399 BOOL
400 WINAPI
401 IsCharUpperA(CHAR Ch)
402 {
403 WCHAR WCh;
404
405 MultiByteToWideChar(CP_ACP, 0, &Ch, 1, &WCh, 1);
406 return IsCharUpperW(WCh);
407 }
408
409 /*
410 * @implemented
411 */
412 BOOL
413 WINAPI
414 IsCharUpperW(WCHAR Ch)
415 {
416 return (GetC1Type(Ch) & C1_UPPER) != 0;
417 }
418
419 /*
420 * @implemented
421 */
422 BOOL
423 WINAPI
424 OemToCharA(LPCSTR s, LPSTR d)
425 {
426 if ( !s || !d ) return FALSE;
427 return OemToCharBuffA(s, d, strlen(s) + 1);
428 }
429
430 /*
431 * @implemented
432 */
433 BOOL WINAPI OemToCharBuffA(LPCSTR s, LPSTR d, DWORD len)
434 {
435 WCHAR* bufW;
436
437 if ( !s || !d ) return FALSE;
438
439 bufW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
440 if (bufW) {
441 MultiByteToWideChar(CP_OEMCP, 0, s, len, bufW, len);
442 WideCharToMultiByte(CP_ACP, 0, bufW, len, d, len, NULL, NULL);
443 HeapFree(GetProcessHeap(), 0, bufW);
444 }
445 return TRUE;
446 }
447
448 /*
449 * @implemented
450 */
451 BOOL
452 WINAPI
453 OemToCharBuffW(LPCSTR s, LPWSTR d, DWORD len)
454 {
455 if ( !s || !d ) return FALSE;
456 MultiByteToWideChar(CP_OEMCP, 0, s, len, d, len);
457 return TRUE;
458 }
459
460 /*
461 * @implemented
462 */
463 BOOL WINAPI OemToCharW(LPCSTR s, LPWSTR d)
464 {
465 if ( !s || !d ) return FALSE;
466 return OemToCharBuffW(s, d, strlen(s) + 1);
467 }
468
469 /* EOF */