msvcrt: Use per thread locale in many functions.
[wine] / dlls / msvcrt / ctype.c
1 /*
2  * msvcrt.dll ctype functions
3  *
4  * Copyright 2000 Jon Griffiths
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "msvcrt.h"
22 #include "winnls.h"
23
24 /* Some abbreviations to make the following table readable */
25 #define _C_ MSVCRT__CONTROL
26 #define _S_ MSVCRT__SPACE
27 #define _P_ MSVCRT__PUNCT
28 #define _D_ MSVCRT__DIGIT
29 #define _H_ MSVCRT__HEX
30 #define _U_ MSVCRT__UPPER
31 #define _L_ MSVCRT__LOWER
32
33 WORD MSVCRT__ctype [257] = {
34   0, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _S_|_C_, _S_|_C_,
35   _S_|_C_, _S_|_C_, _S_|_C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_,
36   _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _S_|MSVCRT__BLANK,
37   _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_,
38   _P_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_,
39   _D_|_H_, _D_|_H_, _D_|_H_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _U_|_H_,
40   _U_|_H_, _U_|_H_, _U_|_H_, _U_|_H_, _U_|_H_, _U_, _U_, _U_, _U_, _U_,
41   _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_,
42   _U_, _P_, _P_, _P_, _P_, _P_, _P_, _L_|_H_, _L_|_H_, _L_|_H_, _L_|_H_,
43   _L_|_H_, _L_|_H_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_,
44   _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _P_, _P_, _P_, _P_,
45   _C_, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46   0, 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
51 };
52
53 /* pctype is used by macros in the Win32 headers. It must point
54  * To a table of flags exactly like ctype. To allow locale
55  * changes to affect ctypes (i.e. isleadbyte), we use a second table
56  * and update its flags whenever the current locale changes.
57  */
58 WORD* MSVCRT__pctype;
59
60 /*********************************************************************
61  *              __pctype_func (MSVCRT.@)
62  */
63 WORD** CDECL MSVCRT___pctype_func(void)
64 {
65   return &get_locale()->locinfo->pctype;
66 }
67
68 /*********************************************************************
69  *              _isctype (MSVCRT.@)
70  */
71 int CDECL _isctype(int c, int type)
72 {
73   MSVCRT__locale_t locale = get_locale();
74
75   if (c >= -1 && c <= 255)
76     return locale->locinfo->pctype[c] & type;
77
78   if (locale->locinfo->mb_cur_max != 1 && c > 0)
79   {
80     /* FIXME: Is there a faster way to do this? */
81     WORD typeInfo;
82     char convert[3], *pconv = convert;
83
84     if (locale->locinfo->pctype[(UINT)c >> 8] & MSVCRT__LEADBYTE)
85       *pconv++ = (UINT)c >> 8;
86     *pconv++ = c & 0xff;
87     *pconv = 0;
88     /* FIXME: Use ctype LCID, not lc_all */
89     if (GetStringTypeExA(get_locale()->locinfo->lc_handle[MSVCRT_LC_CTYPE],
90                 CT_CTYPE1, convert, convert[1] ? 2 : 1, &typeInfo))
91       return typeInfo & type;
92   }
93   return 0;
94 }
95
96 /*********************************************************************
97  *              isalnum (MSVCRT.@)
98  */
99 int CDECL MSVCRT_isalnum(int c)
100 {
101   return _isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT );
102 }
103
104 /*********************************************************************
105  *              isalpha (MSVCRT.@)
106  */
107 int CDECL MSVCRT_isalpha(int c)
108 {
109   return _isctype( c, MSVCRT__ALPHA );
110 }
111
112 /*********************************************************************
113  *              iscntrl (MSVCRT.@)
114  */
115 int CDECL MSVCRT_iscntrl(int c)
116 {
117   return _isctype( c, MSVCRT__CONTROL );
118 }
119
120 /*********************************************************************
121  *              isdigit (MSVCRT.@)
122  */
123 int CDECL MSVCRT_isdigit(int c)
124 {
125   return _isctype( c, MSVCRT__DIGIT );
126 }
127
128 /*********************************************************************
129  *              isgraph (MSVCRT.@)
130  */
131 int CDECL MSVCRT_isgraph(int c)
132 {
133   return _isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__PUNCT );
134 }
135
136 /*********************************************************************
137  *              isleadbyte (MSVCRT.@)
138  */
139 int CDECL MSVCRT_isleadbyte(int c)
140 {
141   return _isctype( c, MSVCRT__LEADBYTE );
142 }
143
144 /*********************************************************************
145  *              islower (MSVCRT.@)
146  */
147 int CDECL MSVCRT_islower(int c)
148 {
149   return _isctype( c, MSVCRT__LOWER );
150 }
151
152 /*********************************************************************
153  *              isprint (MSVCRT.@)
154  */
155 int CDECL MSVCRT_isprint(int c)
156 {
157   return _isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__BLANK | MSVCRT__PUNCT );
158 }
159
160 /*********************************************************************
161  *              ispunct (MSVCRT.@)
162  */
163 int CDECL MSVCRT_ispunct(int c)
164 {
165   return _isctype( c, MSVCRT__PUNCT );
166 }
167
168 /*********************************************************************
169  *              isspace (MSVCRT.@)
170  */
171 int CDECL MSVCRT_isspace(int c)
172 {
173   return _isctype( c, MSVCRT__SPACE );
174 }
175
176 /*********************************************************************
177  *              isupper (MSVCRT.@)
178  */
179 int CDECL MSVCRT_isupper(int c)
180 {
181   return _isctype( c, MSVCRT__UPPER );
182 }
183
184 /*********************************************************************
185  *              isxdigit (MSVCRT.@)
186  */
187 int CDECL MSVCRT_isxdigit(int c)
188 {
189   return _isctype( c, MSVCRT__HEX );
190 }
191
192 /*********************************************************************
193  *              __isascii (MSVCRT.@)
194  */
195 int CDECL MSVCRT___isascii(int c)
196 {
197   return isascii((unsigned)c);
198 }
199
200 /*********************************************************************
201  *              __toascii (MSVCRT.@)
202  */
203 int CDECL MSVCRT___toascii(int c)
204 {
205   return (unsigned)c & 0x7f;
206 }
207
208 /*********************************************************************
209  *              iswascii (MSVCRT.@)
210  *
211  */
212 int CDECL MSVCRT_iswascii(MSVCRT_wchar_t c)
213 {
214   return ((unsigned)c < 0x80);
215 }
216
217 /*********************************************************************
218  *              __iscsym (MSVCRT.@)
219  */
220 int CDECL MSVCRT___iscsym(int c)
221 {
222   return (c < 127 && (isalnum(c) || c == '_'));
223 }
224
225 /*********************************************************************
226  *              __iscsymf (MSVCRT.@)
227  */
228 int CDECL MSVCRT___iscsymf(int c)
229 {
230   return (c < 127 && (isalpha(c) || c == '_'));
231 }
232
233 /*********************************************************************
234  *              _toupper (MSVCRT.@)
235  */
236 int CDECL MSVCRT__toupper(int c)
237 {
238     return c - 0x20;  /* sic */
239 }
240
241 /*********************************************************************
242  *              _tolower (MSVCRT.@)
243  */
244 int CDECL MSVCRT__tolower(int c)
245 {
246     return c + 0x20;  /* sic */
247 }