comctl32/monthcal: After notification safely free buffer allocated on heap.
[wine/multimedia.git] / dlls / msvcrt / ctype.c
blob6bf4948acc71de9377e404f44e7d3ac26b4b43ce
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 /* pctype is used by macros in the Win32 headers. It must point
55 * To a table of flags exactly like ctype. To allow locale
56 * changes to affect ctypes (i.e. isleadbyte), we use a second table
57 * and update its flags whenever the current locale changes.
59 unsigned short *MSVCRT__pctype = NULL;
61 /*********************************************************************
62 * __p__pctype (MSVCRT.@)
64 unsigned short** CDECL MSVCRT___p__pctype(void)
66 return &get_locinfo()->pctype;
69 /*********************************************************************
70 * __pctype_func (MSVCRT.@)
72 const unsigned short* CDECL MSVCRT___pctype_func(void)
74 return get_locinfo()->pctype;
77 /*********************************************************************
78 * _isctype_l (MSVCRT.@)
80 int CDECL MSVCRT__isctype_l(int c, int type, MSVCRT__locale_t locale)
82 MSVCRT_pthreadlocinfo locinfo;
84 if(!locale)
85 locinfo = get_locinfo();
86 else
87 locinfo = locale->locinfo;
89 if (c >= -1 && c <= 255)
90 return locinfo->pctype[c] & type;
92 if (locinfo->mb_cur_max != 1 && c > 0)
94 /* FIXME: Is there a faster way to do this? */
95 WORD typeInfo;
96 char convert[3], *pconv = convert;
98 if (locinfo->pctype[(UINT)c >> 8] & MSVCRT__LEADBYTE)
99 *pconv++ = (UINT)c >> 8;
100 *pconv++ = c & 0xff;
101 *pconv = 0;
103 if (GetStringTypeExA(locinfo->lc_handle[MSVCRT_LC_CTYPE],
104 CT_CTYPE1, convert, convert[1] ? 2 : 1, &typeInfo))
105 return typeInfo & type;
107 return 0;
110 /*********************************************************************
111 * _isctype (MSVCRT.@)
113 int CDECL MSVCRT__isctype(int c, int type)
115 return MSVCRT__isctype_l(c, type, NULL);
118 /*********************************************************************
119 * _isalnum_l (MSVCRT.@)
121 int CDECL MSVCRT__isalnum_l(int c, MSVCRT__locale_t locale)
123 return MSVCRT__isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT, locale );
126 /*********************************************************************
127 * isalnum (MSVCRT.@)
129 int CDECL MSVCRT_isalnum(int c)
131 return MSVCRT__isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT );
134 /*********************************************************************
135 * _isalpha_l (MSVCRT.@)
137 int CDECL MSVCRT__isalpha_l(int c, MSVCRT__locale_t locale)
139 return MSVCRT__isctype_l( c, MSVCRT__ALPHA, locale );
142 /*********************************************************************
143 * isalpha (MSVCRT.@)
145 int CDECL MSVCRT_isalpha(int c)
147 return MSVCRT__isctype( c, MSVCRT__ALPHA );
150 /*********************************************************************
151 * _iscntrl_l (MSVCRT.@)
153 int CDECL MSVCRT__iscntrl_l(int c, MSVCRT__locale_t locale)
155 return MSVCRT__isctype_l( c, MSVCRT__CONTROL, locale );
158 /*********************************************************************
159 * iscntrl (MSVCRT.@)
161 int CDECL MSVCRT_iscntrl(int c)
163 return MSVCRT__isctype( c, MSVCRT__CONTROL );
166 /*********************************************************************
167 * _isdigit_l (MSVCRT.@)
169 int CDECL MSVCRT__isdigit_l(int c, MSVCRT__locale_t locale)
171 return MSVCRT__isctype_l( c, MSVCRT__DIGIT, locale );
174 /*********************************************************************
175 * isdigit (MSVCRT.@)
177 int CDECL MSVCRT_isdigit(int c)
179 return MSVCRT__isctype( c, MSVCRT__DIGIT );
182 /*********************************************************************
183 * _isgraph_l (MSVCRT.@)
185 int CDECL MSVCRT__isgraph_l(int c, MSVCRT__locale_t locale)
187 return MSVCRT__isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__PUNCT, locale );
190 /*********************************************************************
191 * isgraph (MSVCRT.@)
193 int CDECL MSVCRT_isgraph(int c)
195 return MSVCRT__isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__PUNCT );
198 /*********************************************************************
199 * _isleadbyte_l (MSVCRT.@)
201 int CDECL MSVCRT__isleadbyte_l(int c, MSVCRT__locale_t locale)
203 return MSVCRT__isctype_l( c, MSVCRT__LEADBYTE, locale );
206 /*********************************************************************
207 * isleadbyte (MSVCRT.@)
209 int CDECL MSVCRT_isleadbyte(int c)
211 return MSVCRT__isctype( c, MSVCRT__LEADBYTE );
214 /*********************************************************************
215 * _islower_l (MSVCRT.@)
217 int CDECL MSVCRT__islower_l(int c, MSVCRT__locale_t locale)
219 return MSVCRT__isctype_l( c, MSVCRT__LOWER, locale );
222 /*********************************************************************
223 * islower (MSVCRT.@)
225 int CDECL MSVCRT_islower(int c)
227 return MSVCRT__isctype( c, MSVCRT__LOWER );
230 /*********************************************************************
231 * _isprint_l (MSVCRT.@)
233 int CDECL MSVCRT__isprint_l(int c, MSVCRT__locale_t locale)
235 return MSVCRT__isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__BLANK | MSVCRT__PUNCT, locale );
238 /*********************************************************************
239 * isprint (MSVCRT.@)
241 int CDECL MSVCRT_isprint(int c)
243 return MSVCRT__isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__BLANK | MSVCRT__PUNCT );
246 /*********************************************************************
247 * ispunct (MSVCRT.@)
249 int CDECL MSVCRT_ispunct(int c)
251 return MSVCRT__isctype( c, MSVCRT__PUNCT );
254 /*********************************************************************
255 * _isspace_l (MSVCRT.@)
257 int CDECL MSVCRT__isspace_l(int c, MSVCRT__locale_t locale)
259 return MSVCRT__isctype_l( c, MSVCRT__SPACE, locale );
262 /*********************************************************************
263 * isspace (MSVCRT.@)
265 int CDECL MSVCRT_isspace(int c)
267 return MSVCRT__isctype( c, MSVCRT__SPACE );
270 /*********************************************************************
271 * _isupper_l (MSVCRT.@)
273 int CDECL MSVCRT__isupper_l(int c, MSVCRT__locale_t locale)
275 return MSVCRT__isctype_l( c, MSVCRT__UPPER, locale );
278 /*********************************************************************
279 * isupper (MSVCRT.@)
281 int CDECL MSVCRT_isupper(int c)
283 return MSVCRT__isctype( c, MSVCRT__UPPER );
286 /*********************************************************************
287 * _isxdigit_l (MSVCRT.@)
289 int CDECL MSVCRT__isxdigit_l(int c, MSVCRT__locale_t locale)
291 return MSVCRT__isctype_l( c, MSVCRT__HEX, locale );
294 /*********************************************************************
295 * isxdigit (MSVCRT.@)
297 int CDECL MSVCRT_isxdigit(int c)
299 return MSVCRT__isctype( c, MSVCRT__HEX );
302 /*********************************************************************
303 * __isascii (MSVCRT.@)
305 int CDECL MSVCRT___isascii(int c)
307 return isascii((unsigned)c);
310 /*********************************************************************
311 * __toascii (MSVCRT.@)
313 int CDECL MSVCRT___toascii(int c)
315 return (unsigned)c & 0x7f;
318 /*********************************************************************
319 * iswascii (MSVCRT.@)
322 int CDECL MSVCRT_iswascii(MSVCRT_wchar_t c)
324 return ((unsigned)c < 0x80);
327 /*********************************************************************
328 * __iscsym (MSVCRT.@)
330 int CDECL MSVCRT___iscsym(int c)
332 return (c < 127 && (isalnum(c) || c == '_'));
335 /*********************************************************************
336 * __iscsymf (MSVCRT.@)
338 int CDECL MSVCRT___iscsymf(int c)
340 return (c < 127 && (isalpha(c) || c == '_'));
343 /*********************************************************************
344 * _toupper_l (MSVCRT.@)
346 int CDECL MSVCRT__toupper_l(int c, MSVCRT__locale_t locale)
348 MSVCRT_pthreadlocinfo locinfo;
350 if(!locale)
351 locinfo = get_locinfo();
352 else
353 locinfo = locale->locinfo;
355 if(c < 256)
356 return locinfo->pcumap[(unsigned char)c];
358 if(locinfo->pctype[(c>>8)&255] & MSVCRT__LEADBYTE)
360 WCHAR wide, upper;
361 char str[2], *p = str;
362 *p++ = (c>>8) & 255;
363 *p++ = c & 255;
365 if(!MultiByteToWideChar(locinfo->lc_codepage,
366 MB_ERR_INVALID_CHARS, str, 2, &wide, 1))
367 return c;
369 upper = toupperW(wide);
370 if(upper == wide)
371 return c;
373 WideCharToMultiByte(locinfo->lc_codepage, 0,
374 &upper, 1, str, 2, NULL, NULL);
376 return str[0] + (str[1]<<8);
379 return c;
382 /*********************************************************************
383 * toupper (MSVCRT.@)
385 int CDECL MSVCRT_toupper(int c)
387 return MSVCRT__toupper_l(c, NULL);
390 /*********************************************************************
391 * _toupper (MSVCRT.@)
393 int CDECL MSVCRT__toupper(int c)
395 return c - 0x20; /* sic */
398 /*********************************************************************
399 * _tolower_l (MSVCRT.@)
401 int CDECL MSVCRT__tolower_l(int c, MSVCRT__locale_t locale)
403 MSVCRT_pthreadlocinfo locinfo;
405 if(!locale)
406 locinfo = get_locinfo();
407 else
408 locinfo = locale->locinfo;
410 if(c < 256)
411 return locinfo->pclmap[(unsigned char)c];
413 if(locinfo->pctype[(c>>8)&255] & MSVCRT__LEADBYTE)
415 WCHAR wide, upper;
416 char str[2], *p = str;
417 *p++ = (c>>8) & 255;
418 *p++ = c & 255;
420 if(!MultiByteToWideChar(locinfo->lc_codepage,
421 MB_ERR_INVALID_CHARS, str, 2, &wide, 1))
422 return c;
424 upper = tolowerW(wide);
425 if(upper == wide)
426 return c;
428 WideCharToMultiByte(locinfo->lc_codepage, 0,
429 &upper, 1, str, 2, NULL, NULL);
431 return str[0] + (str[1]<<8);
434 return c;
437 /*********************************************************************
438 * tolower (MSVCRT.@)
440 int CDECL MSVCRT_tolower(int c)
442 return MSVCRT__tolower_l(c, NULL);
445 /*********************************************************************
446 * _tolower (MSVCRT.@)
448 int CDECL MSVCRT__tolower(int c)
450 return c + 0x20; /* sic */