msvcrt: Fix __pctype_func implementation.
[wine/multimedia.git] / dlls / msvcrt / ctype.c
blob5573608fe4048085d2a0dde4147608eac91a6c11
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 * __pctype_func (MSVCRT.@)
64 const unsigned short* CDECL MSVCRT___pctype_func(void)
66 return get_locale()->locinfo->pctype;
69 /*********************************************************************
70 * _isctype_l (MSVCRT.@)
72 int CDECL _isctype_l(int c, int type, MSVCRT__locale_t locale)
74 if(!locale)
75 locale = get_locale();
77 if (c >= -1 && c <= 255)
78 return locale->locinfo->pctype[c] & type;
80 if (locale->locinfo->mb_cur_max != 1 && c > 0)
82 /* FIXME: Is there a faster way to do this? */
83 WORD typeInfo;
84 char convert[3], *pconv = convert;
86 if (locale->locinfo->pctype[(UINT)c >> 8] & MSVCRT__LEADBYTE)
87 *pconv++ = (UINT)c >> 8;
88 *pconv++ = c & 0xff;
89 *pconv = 0;
91 if (GetStringTypeExA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE],
92 CT_CTYPE1, convert, convert[1] ? 2 : 1, &typeInfo))
93 return typeInfo & type;
95 return 0;
98 /*********************************************************************
99 * _isctype (MSVCRT.@)
101 int CDECL _isctype(int c, int type)
103 return _isctype_l(c, type, NULL);
106 /*********************************************************************
107 * _isalnum_l (MSVCRT.@)
109 int CDECL MSVCRT__isalnum_l(int c, MSVCRT__locale_t locale)
111 return _isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT, locale );
114 /*********************************************************************
115 * isalnum (MSVCRT.@)
117 int CDECL MSVCRT_isalnum(int c)
119 return _isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT );
122 /*********************************************************************
123 * _isalpha_l (MSVCRT.@)
125 int CDECL MSVCRT__isalpha_l(int c, MSVCRT__locale_t locale)
127 return _isctype_l( c, MSVCRT__ALPHA, locale );
130 /*********************************************************************
131 * isalpha (MSVCRT.@)
133 int CDECL MSVCRT_isalpha(int c)
135 return _isctype( c, MSVCRT__ALPHA );
138 /*********************************************************************
139 * _iscntrl_l (MSVCRT.@)
141 int CDECL MSVCRT__iscntrl_l(int c, MSVCRT__locale_t locale)
143 return _isctype_l( c, MSVCRT__CONTROL, locale );
146 /*********************************************************************
147 * iscntrl (MSVCRT.@)
149 int CDECL MSVCRT_iscntrl(int c)
151 return _isctype( c, MSVCRT__CONTROL );
154 /*********************************************************************
155 * _isdigit_l (MSVCRT.@)
157 int CDECL MSVCRT__isdigit_l(int c, MSVCRT__locale_t locale)
159 return _isctype_l( c, MSVCRT__DIGIT, locale );
162 /*********************************************************************
163 * isdigit (MSVCRT.@)
165 int CDECL MSVCRT_isdigit(int c)
167 return _isctype( c, MSVCRT__DIGIT );
170 /*********************************************************************
171 * _isgraph_l (MSVCRT.@)
173 int CDECL MSVCRT__isgraph_l(int c, MSVCRT__locale_t locale)
175 return _isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__PUNCT, locale );
178 /*********************************************************************
179 * isgraph (MSVCRT.@)
181 int CDECL MSVCRT_isgraph(int c)
183 return _isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__PUNCT );
186 /*********************************************************************
187 * _isleadbyte_l (MSVCRT.@)
189 int CDECL MSVCRT__isleadbyte_l(int c, MSVCRT__locale_t locale)
191 return _isctype_l( c, MSVCRT__LEADBYTE, locale );
194 /*********************************************************************
195 * isleadbyte (MSVCRT.@)
197 int CDECL MSVCRT_isleadbyte(int c)
199 return _isctype( c, MSVCRT__LEADBYTE );
202 /*********************************************************************
203 * _islower_l (MSVCRT.@)
205 int CDECL MSVCRT__islower_l(int c, MSVCRT__locale_t locale)
207 return _isctype_l( c, MSVCRT__LOWER, locale );
210 /*********************************************************************
211 * islower (MSVCRT.@)
213 int CDECL MSVCRT_islower(int c)
215 return _isctype( c, MSVCRT__LOWER );
218 /*********************************************************************
219 * _isprint_l (MSVCRT.@)
221 int CDECL MSVCRT__isprint_l(int c, MSVCRT__locale_t locale)
223 return _isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__BLANK | MSVCRT__PUNCT, locale );
226 /*********************************************************************
227 * isprint (MSVCRT.@)
229 int CDECL MSVCRT_isprint(int c)
231 return _isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__BLANK | MSVCRT__PUNCT );
234 /*********************************************************************
235 * ispunct (MSVCRT.@)
237 int CDECL MSVCRT_ispunct(int c)
239 return _isctype( c, MSVCRT__PUNCT );
242 /*********************************************************************
243 * _isspace_l (MSVCRT.@)
245 int CDECL MSVCRT__isspace_l(int c, MSVCRT__locale_t locale)
247 return _isctype_l( c, MSVCRT__SPACE, locale );
250 /*********************************************************************
251 * isspace (MSVCRT.@)
253 int CDECL MSVCRT_isspace(int c)
255 return _isctype( c, MSVCRT__SPACE );
258 /*********************************************************************
259 * _isupper_l (MSVCRT.@)
261 int CDECL MSVCRT__isupper_l(int c, MSVCRT__locale_t locale)
263 return _isctype_l( c, MSVCRT__UPPER, locale );
266 /*********************************************************************
267 * isupper (MSVCRT.@)
269 int CDECL MSVCRT_isupper(int c)
271 return _isctype( c, MSVCRT__UPPER );
274 /*********************************************************************
275 * _isxdigit_l (MSVCRT.@)
277 int CDECL MSVCRT__isxdigit_l(int c, MSVCRT__locale_t locale)
279 return _isctype_l( c, MSVCRT__HEX, locale );
282 /*********************************************************************
283 * isxdigit (MSVCRT.@)
285 int CDECL MSVCRT_isxdigit(int c)
287 return _isctype( c, MSVCRT__HEX );
290 /*********************************************************************
291 * __isascii (MSVCRT.@)
293 int CDECL MSVCRT___isascii(int c)
295 return isascii((unsigned)c);
298 /*********************************************************************
299 * __toascii (MSVCRT.@)
301 int CDECL MSVCRT___toascii(int c)
303 return (unsigned)c & 0x7f;
306 /*********************************************************************
307 * iswascii (MSVCRT.@)
310 int CDECL MSVCRT_iswascii(MSVCRT_wchar_t c)
312 return ((unsigned)c < 0x80);
315 /*********************************************************************
316 * __iscsym (MSVCRT.@)
318 int CDECL MSVCRT___iscsym(int c)
320 return (c < 127 && (isalnum(c) || c == '_'));
323 /*********************************************************************
324 * __iscsymf (MSVCRT.@)
326 int CDECL MSVCRT___iscsymf(int c)
328 return (c < 127 && (isalpha(c) || c == '_'));
331 /*********************************************************************
332 * _toupper_l (MSVCRT.@)
334 int CDECL MSVCRT__toupper_l(int c, MSVCRT__locale_t locale)
336 if(!locale)
337 locale = get_locale();
339 if(c < 256)
340 return locale->locinfo->pcumap[c];
342 if(locale->locinfo->pctype[(c>>8)&255] & MSVCRT__LEADBYTE)
344 WCHAR wide, upper;
345 char str[2], *p = str;
346 *p++ = (c>>8) & 255;
347 *p++ = c & 255;
349 if(!MultiByteToWideChar(locale->locinfo->lc_codepage,
350 MB_ERR_INVALID_CHARS, str, 2, &wide, 1))
351 return c;
353 upper = toupperW(wide);
354 if(upper == wide)
355 return c;
357 WideCharToMultiByte(locale->locinfo->lc_codepage, 0,
358 &upper, 1, str, 2, NULL, NULL);
360 return str[0] + (str[1]<<8);
363 return c;
366 /*********************************************************************
367 * toupper (MSVCRT.@)
369 int CDECL MSVCRT_toupper(int c)
371 return MSVCRT__toupper_l(c, NULL);
374 /*********************************************************************
375 * _toupper (MSVCRT.@)
377 int CDECL MSVCRT__toupper(int c)
379 return c - 0x20; /* sic */
382 /*********************************************************************
383 * _tolower_l (MSVCRT.@)
385 int CDECL MSVCRT__tolower_l(int c, MSVCRT__locale_t locale)
387 if(!locale)
388 locale = get_locale();
390 if(c < 256)
391 return locale->locinfo->pclmap[c];
393 if(locale->locinfo->pctype[(c>>8)&255] & MSVCRT__LEADBYTE)
395 WCHAR wide, upper;
396 char str[2], *p = str;
397 *p++ = (c>>8) & 255;
398 *p++ = c & 255;
400 if(!MultiByteToWideChar(locale->locinfo->lc_codepage,
401 MB_ERR_INVALID_CHARS, str, 2, &wide, 1))
402 return c;
404 upper = tolowerW(wide);
405 if(upper == wide)
406 return c;
408 WideCharToMultiByte(locale->locinfo->lc_codepage, 0,
409 &upper, 1, str, 2, NULL, NULL);
411 return str[0] + (str[1]<<8);
414 return c;
417 /*********************************************************************
418 * tolower (MSVCRT.@)
420 int CDECL MSVCRT_tolower(int c)
422 return MSVCRT__tolower_l(c, NULL);
425 /*********************************************************************
426 * _tolower (MSVCRT.@)
428 int CDECL MSVCRT__tolower(int c)
430 return c + 0x20; /* sic */