kernel32/tests: Ensure that pipe tests read the full pipe content.
[wine/wine-gecko.git] / dlls / msvcrt / ctype.c
blob8b9d14c4bbfd0f4777cbbfbc68d9f09e3b5bc32c
1 /*
2 * msvcrt.dll ctype functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "msvcrt.h"
22 #include "winnls.h"
23 #include "wine/unicode.h"
25 /* Some abbreviations to make the following table readable */
26 #define _C_ MSVCRT__CONTROL
27 #define _S_ MSVCRT__SPACE
28 #define _P_ MSVCRT__PUNCT
29 #define _D_ MSVCRT__DIGIT
30 #define _H_ MSVCRT__HEX
31 #define _U_ MSVCRT__UPPER
32 #define _L_ MSVCRT__LOWER
34 WORD MSVCRT__ctype [257] = {
35 0, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _S_|_C_, _S_|_C_,
36 _S_|_C_, _S_|_C_, _S_|_C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_,
37 _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _S_|MSVCRT__BLANK,
38 _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_,
39 _P_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_,
40 _D_|_H_, _D_|_H_, _D_|_H_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _U_|_H_,
41 _U_|_H_, _U_|_H_, _U_|_H_, _U_|_H_, _U_|_H_, _U_, _U_, _U_, _U_, _U_,
42 _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_,
43 _U_, _P_, _P_, _P_, _P_, _P_, _P_, _L_|_H_, _L_|_H_, _L_|_H_, _L_|_H_,
44 _L_|_H_, _L_|_H_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_,
45 _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _P_, _P_, _P_, _P_,
46 _C_, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
54 /*********************************************************************
55 * __p__pctype (MSVCRT.@)
57 unsigned short** CDECL MSVCRT___p__pctype(void)
59 return &get_locinfo()->pctype;
62 /*********************************************************************
63 * __pctype_func (MSVCRT.@)
65 const unsigned short* CDECL MSVCRT___pctype_func(void)
67 return get_locinfo()->pctype;
70 /*********************************************************************
71 * _isctype_l (MSVCRT.@)
73 int CDECL MSVCRT__isctype_l(int c, int type, MSVCRT__locale_t locale)
75 MSVCRT_pthreadlocinfo locinfo;
77 if(!locale)
78 locinfo = get_locinfo();
79 else
80 locinfo = locale->locinfo;
82 if (c >= -1 && c <= 255)
83 return locinfo->pctype[c] & type;
85 if (locinfo->mb_cur_max != 1 && c > 0)
87 /* FIXME: Is there a faster way to do this? */
88 WORD typeInfo;
89 char convert[3], *pconv = convert;
91 if (locinfo->pctype[(UINT)c >> 8] & MSVCRT__LEADBYTE)
92 *pconv++ = (UINT)c >> 8;
93 *pconv++ = c & 0xff;
94 *pconv = 0;
96 if (GetStringTypeExA(locinfo->lc_handle[MSVCRT_LC_CTYPE],
97 CT_CTYPE1, convert, convert[1] ? 2 : 1, &typeInfo))
98 return typeInfo & type;
100 return 0;
103 /*********************************************************************
104 * _isctype (MSVCRT.@)
106 int CDECL MSVCRT__isctype(int c, int type)
108 return MSVCRT__isctype_l(c, type, NULL);
111 /*********************************************************************
112 * _isalnum_l (MSVCRT.@)
114 int CDECL MSVCRT__isalnum_l(int c, MSVCRT__locale_t locale)
116 return MSVCRT__isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT, locale );
119 /*********************************************************************
120 * isalnum (MSVCRT.@)
122 int CDECL MSVCRT_isalnum(int c)
124 return MSVCRT__isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT );
127 /*********************************************************************
128 * _isalpha_l (MSVCRT.@)
130 int CDECL MSVCRT__isalpha_l(int c, MSVCRT__locale_t locale)
132 return MSVCRT__isctype_l( c, MSVCRT__ALPHA, locale );
135 /*********************************************************************
136 * isalpha (MSVCRT.@)
138 int CDECL MSVCRT_isalpha(int c)
140 return MSVCRT__isctype( c, MSVCRT__ALPHA );
143 /*********************************************************************
144 * _iscntrl_l (MSVCRT.@)
146 int CDECL MSVCRT__iscntrl_l(int c, MSVCRT__locale_t locale)
148 return MSVCRT__isctype_l( c, MSVCRT__CONTROL, locale );
151 /*********************************************************************
152 * iscntrl (MSVCRT.@)
154 int CDECL MSVCRT_iscntrl(int c)
156 return MSVCRT__isctype( c, MSVCRT__CONTROL );
159 /*********************************************************************
160 * _isdigit_l (MSVCRT.@)
162 int CDECL MSVCRT__isdigit_l(int c, MSVCRT__locale_t locale)
164 return MSVCRT__isctype_l( c, MSVCRT__DIGIT, locale );
167 /*********************************************************************
168 * isdigit (MSVCRT.@)
170 int CDECL MSVCRT_isdigit(int c)
172 return MSVCRT__isctype( c, MSVCRT__DIGIT );
175 /*********************************************************************
176 * _isgraph_l (MSVCRT.@)
178 int CDECL MSVCRT__isgraph_l(int c, MSVCRT__locale_t locale)
180 return MSVCRT__isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__PUNCT, locale );
183 /*********************************************************************
184 * isgraph (MSVCRT.@)
186 int CDECL MSVCRT_isgraph(int c)
188 return MSVCRT__isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__PUNCT );
191 /*********************************************************************
192 * _isleadbyte_l (MSVCRT.@)
194 int CDECL MSVCRT__isleadbyte_l(int c, MSVCRT__locale_t locale)
196 return MSVCRT__isctype_l( c, MSVCRT__LEADBYTE, locale );
199 /*********************************************************************
200 * isleadbyte (MSVCRT.@)
202 int CDECL MSVCRT_isleadbyte(int c)
204 return MSVCRT__isctype( c, MSVCRT__LEADBYTE );
207 /*********************************************************************
208 * _islower_l (MSVCRT.@)
210 int CDECL MSVCRT__islower_l(int c, MSVCRT__locale_t locale)
212 return MSVCRT__isctype_l( c, MSVCRT__LOWER, locale );
215 /*********************************************************************
216 * islower (MSVCRT.@)
218 int CDECL MSVCRT_islower(int c)
220 return MSVCRT__isctype( c, MSVCRT__LOWER );
223 /*********************************************************************
224 * _isprint_l (MSVCRT.@)
226 int CDECL MSVCRT__isprint_l(int c, MSVCRT__locale_t locale)
228 return MSVCRT__isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__BLANK | MSVCRT__PUNCT, locale );
231 /*********************************************************************
232 * isprint (MSVCRT.@)
234 int CDECL MSVCRT_isprint(int c)
236 return MSVCRT__isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__BLANK | MSVCRT__PUNCT );
239 /*********************************************************************
240 * ispunct (MSVCRT.@)
242 int CDECL MSVCRT_ispunct(int c)
244 return MSVCRT__isctype( c, MSVCRT__PUNCT );
247 /*********************************************************************
248 * _isspace_l (MSVCRT.@)
250 int CDECL MSVCRT__isspace_l(int c, MSVCRT__locale_t locale)
252 return MSVCRT__isctype_l( c, MSVCRT__SPACE, locale );
255 /*********************************************************************
256 * isspace (MSVCRT.@)
258 int CDECL MSVCRT_isspace(int c)
260 return MSVCRT__isctype( c, MSVCRT__SPACE );
263 /*********************************************************************
264 * _isupper_l (MSVCRT.@)
266 int CDECL MSVCRT__isupper_l(int c, MSVCRT__locale_t locale)
268 return MSVCRT__isctype_l( c, MSVCRT__UPPER, locale );
271 /*********************************************************************
272 * isupper (MSVCRT.@)
274 int CDECL MSVCRT_isupper(int c)
276 return MSVCRT__isctype( c, MSVCRT__UPPER );
279 /*********************************************************************
280 * _isxdigit_l (MSVCRT.@)
282 int CDECL MSVCRT__isxdigit_l(int c, MSVCRT__locale_t locale)
284 return MSVCRT__isctype_l( c, MSVCRT__HEX, locale );
287 /*********************************************************************
288 * isxdigit (MSVCRT.@)
290 int CDECL MSVCRT_isxdigit(int c)
292 return MSVCRT__isctype( c, MSVCRT__HEX );
295 /*********************************************************************
296 * __isascii (MSVCRT.@)
298 int CDECL MSVCRT___isascii(int c)
300 return isascii((unsigned)c);
303 /*********************************************************************
304 * __toascii (MSVCRT.@)
306 int CDECL MSVCRT___toascii(int c)
308 return (unsigned)c & 0x7f;
311 /*********************************************************************
312 * iswascii (MSVCRT.@)
315 int CDECL MSVCRT_iswascii(MSVCRT_wchar_t c)
317 return ((unsigned)c < 0x80);
320 /*********************************************************************
321 * __iscsym (MSVCRT.@)
323 int CDECL MSVCRT___iscsym(int c)
325 return (c < 127 && (isalnum(c) || c == '_'));
328 /*********************************************************************
329 * __iscsymf (MSVCRT.@)
331 int CDECL MSVCRT___iscsymf(int c)
333 return (c < 127 && (isalpha(c) || c == '_'));
336 /*********************************************************************
337 * _toupper_l (MSVCRT.@)
339 int CDECL MSVCRT__toupper_l(int c, MSVCRT__locale_t locale)
341 MSVCRT_pthreadlocinfo locinfo;
342 unsigned char str[2], *p = str;
343 WCHAR wide, upper;
345 if(!locale)
346 locinfo = get_locinfo();
347 else
348 locinfo = locale->locinfo;
350 if((unsigned)c < 256)
351 return locinfo->pcumap[c];
353 if(locinfo->pctype[(c>>8)&255] & MSVCRT__LEADBYTE)
354 *p++ = (c>>8) & 255;
355 else {
356 *MSVCRT__errno() = MSVCRT_EILSEQ;
357 str[1] = 0;
359 *p++ = c & 255;
361 if(!MultiByteToWideChar(locinfo->lc_codepage,
362 MB_ERR_INVALID_CHARS, (char*)str, p-str, &wide, 1))
363 return c;
365 upper = toupperW(wide);
366 if(upper == wide)
367 return str[0] + (str[1]<<8);
369 switch(WideCharToMultiByte(locinfo->lc_codepage, 0,
370 &upper, 1, (char*)str, 2, NULL, NULL)) {
371 case 0:
372 return c;
373 case 1:
374 return str[0];
375 default:
376 return str[0] + (str[1]<<8);
380 /*********************************************************************
381 * toupper (MSVCRT.@)
383 int CDECL MSVCRT_toupper(int c)
385 return MSVCRT__toupper_l(c, NULL);
388 /*********************************************************************
389 * _toupper (MSVCRT.@)
391 int CDECL MSVCRT__toupper(int c)
393 return c - 0x20; /* sic */
396 /*********************************************************************
397 * _tolower_l (MSVCRT.@)
399 int CDECL MSVCRT__tolower_l(int c, MSVCRT__locale_t locale)
401 MSVCRT_pthreadlocinfo locinfo;
402 unsigned char str[2], *p = str;
403 WCHAR wide, lower;
405 if(!locale)
406 locinfo = get_locinfo();
407 else
408 locinfo = locale->locinfo;
410 if((unsigned)c < 256)
411 return locinfo->pclmap[c];
413 if(locinfo->pctype[(c>>8)&255] & MSVCRT__LEADBYTE)
414 *p++ = (c>>8) & 255;
415 else {
416 *MSVCRT__errno() = MSVCRT_EILSEQ;
417 str[1] = 0;
419 *p++ = c & 255;
421 if(!MultiByteToWideChar(locinfo->lc_codepage,
422 MB_ERR_INVALID_CHARS, (char*)str, p-str, &wide, 1))
423 return c;
425 lower = tolowerW(wide);
426 if(lower == wide)
427 return str[0] + (str[1]<<8);
429 switch(WideCharToMultiByte(locinfo->lc_codepage, 0,
430 &lower, 1, (char*)str, 2, NULL, NULL)) {
431 case 0:
432 return c;
433 case 1:
434 return str[0];
435 default:
436 return str[0] + (str[1]<<8);
440 /*********************************************************************
441 * tolower (MSVCRT.@)
443 int CDECL MSVCRT_tolower(int c)
445 return MSVCRT__tolower_l(c, NULL);
448 /*********************************************************************
449 * _tolower (MSVCRT.@)
451 int CDECL MSVCRT__tolower(int c)
453 return c + 0x20; /* sic */