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