Updated.
[wine] / dlls / msvcrt / ctype.c
1 /*
2  * msvcrt.dll ctype functions
3  *
4  * Copyright 2000 Jon Griffiths
5  */
6 #include "msvcrt.h"
7
8 DEFAULT_DEBUG_CHANNEL(msvcrt);
9
10 /* ASCII char classification table - binary compatible */
11 #define MSVCRT_UPPER            C1_UPPER
12 #define MSVCRT_LOWER            C1_LOWER
13 #define MSVCRT_DIGIT            C1_DIGIT
14 #define MSVCRT_SPACE            C1_SPACE
15 #define MSVCRT_PUNCT            C1_PUNCT
16 #define MSVCRT_CONTROL          C1_CNTRL
17 #define MSVCRT_BLANK            C1_BLANK
18 #define MSVCRT_HEX              C1_XDIGIT
19 #define MSVCRT_LEADBYTE  0x8000
20 #define MSVCRT_ALPHA    (C1_ALPHA|MSVCRT_UPPER|MSVCRT_LOWER)
21
22 #define _C_ MSVCRT_CONTROL
23 #define _S_ MSVCRT_SPACE
24 #define _P_ MSVCRT_PUNCT
25 #define _D_ MSVCRT_DIGIT
26 #define _H_ MSVCRT_HEX
27 #define _U_ MSVCRT_UPPER
28 #define _L_ MSVCRT_LOWER
29
30 WORD MSVCRT__ctype [257] = {
31   0, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _S_|_C_, _S_|_C_,
32   _S_|_C_, _S_|_C_, _S_|_C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_,
33   _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _S_|MSVCRT_BLANK,
34   _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_,
35   _P_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_,
36   _D_|_H_, _D_|_H_, _D_|_H_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _U_|_H_,
37   _U_|_H_, _U_|_H_, _U_|_H_, _U_|_H_, _U_|_H_, _U_, _U_, _U_, _U_, _U_,
38   _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_,
39   _U_, _P_, _P_, _P_, _P_, _P_, _P_, _L_|_H_, _L_|_H_, _L_|_H_, _L_|_H_,
40   _L_|_H_, _L_|_H_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_,
41   _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _P_, _P_, _P_, _P_,
42   _C_, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45   0, 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
48 };
49
50 /* Internal: Current ctype table for locale */
51 WORD MSVCRT_current_ctype[257];
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 = MSVCRT_current_ctype + 1;
59
60 /* mbctype data */
61 extern int MSVCRT___mb_cur_max;
62 extern LCID MSVCRT_current_lc_all_lcid;
63
64 /*********************************************************************
65  *              MSVCRT___p__pctype (MSVCRT.@)
66  */
67 WORD** MSVCRT___p__pctype(void)
68 {
69   return &MSVCRT__pctype;
70 }
71
72 /*********************************************************************
73  *              _isctype (MSVCRT.@)
74  */
75 int __cdecl MSVCRT__isctype(int c, int type)
76 {
77   if (c >= -1 && c <= 255)
78     return MSVCRT__pctype[c] & type;
79
80   if (MSVCRT___mb_cur_max != 1 && c > 0)
81   {
82     /* FIXME: Is there a faster way to do this? */
83     WORD typeInfo;
84     char convert[3], *pconv = convert;
85
86     if (MSVCRT__pctype[(UINT)c >> 8] & MSVCRT_LEADBYTE)
87       *pconv++ = (UINT)c >> 8;
88     *pconv++ = c & 0xff;
89     *pconv = 0;
90     /* FIXME: Use ctype LCID, not lc_all */
91     if (GetStringTypeExA(MSVCRT_current_lc_all_lcid, CT_CTYPE1,
92                          convert, convert[1] ? 2 : 1, &typeInfo))
93       return typeInfo & type;
94   }
95   return 0;
96 }
97
98 /*********************************************************************
99  *              isalnum (MSVCRT.@)
100  */
101 int __cdecl MSVCRT_isalnum(int c)
102 {
103   return MSVCRT__isctype( c,MSVCRT_ALPHA | MSVCRT_DIGIT );
104 }
105
106 /*********************************************************************
107  *              isalpha (MSVCRT.@)
108  */
109 int __cdecl MSVCRT_isalpha(int c)
110 {
111   return MSVCRT__isctype( c, MSVCRT_ALPHA );
112 }
113
114 /*********************************************************************
115  *              iscntrl (MSVCRT.@)
116  */
117 int __cdecl MSVCRT_iscntrl(int c)
118 {
119   return MSVCRT__isctype( c, MSVCRT_CONTROL );
120 }
121
122 /*********************************************************************
123  *              isdigit (MSVCRT.@)
124  */
125 int __cdecl MSVCRT_isdigit(int c)
126 {
127   return MSVCRT__isctype( c, MSVCRT_DIGIT );
128 }
129
130 /*********************************************************************
131  *              isgraph (MSVCRT.@)
132  */
133 int __cdecl MSVCRT_isgraph(int c)
134 {
135   return MSVCRT__isctype( c, MSVCRT_ALPHA | MSVCRT_DIGIT | MSVCRT_PUNCT );
136 }
137
138 /*********************************************************************
139  *              isleadbyte (MSVCRT.@)
140  */
141 int __cdecl MSVCRT_isleadbyte(int c)
142 {
143   return MSVCRT__isctype( c, MSVCRT_LEADBYTE );
144 }
145
146 /*********************************************************************
147  *              islower (MSVCRT.@)
148  */
149 int __cdecl MSVCRT_islower(int c)
150 {
151   return MSVCRT__isctype( c, MSVCRT_LOWER );
152 }
153
154 /*********************************************************************
155  *              isprint (MSVCRT.@)
156  */
157 int __cdecl MSVCRT_isprint(int c)
158 {
159   return MSVCRT__isctype( c, MSVCRT_ALPHA | MSVCRT_DIGIT |
160                           MSVCRT_BLANK | MSVCRT_PUNCT );
161 }
162
163 /*********************************************************************
164  *              ispunct (MSVCRT.@)
165  */
166 int __cdecl MSVCRT_ispunct(int c)
167 {
168   return MSVCRT__isctype( c, MSVCRT_PUNCT );
169 }
170
171 /*********************************************************************
172  *              isspace (MSVCRT.@)
173  */
174 int __cdecl MSVCRT_isspace(int c)
175 {
176   return MSVCRT__isctype( c, MSVCRT_SPACE );
177 }
178
179 /*********************************************************************
180  *              isupper (MSVCRT.@)
181  */
182 int __cdecl MSVCRT_isupper(int c)
183 {
184   return MSVCRT__isctype( c, MSVCRT_UPPER );
185 }
186
187 /*********************************************************************
188  *              isxdigit (MSVCRT.@)
189  */
190 int __cdecl MSVCRT_isxdigit(int c)
191 {
192   return MSVCRT__isctype( c, MSVCRT_HEX );
193 }
194
195 /*********************************************************************
196  *              __isascii (MSVCRT.@)
197  */
198 int __cdecl MSVCRT___isascii(int c)
199 {
200   return isascii((unsigned)c);
201 }
202
203 /*********************************************************************
204  *              __toascii (MSVCRT.@)
205  */
206 int __cdecl MSVCRT___toascii(int c)
207 {
208   return (unsigned)c & 0x7f;
209 }
210
211 /*********************************************************************
212  *              iswascii (MSVCRT.@)
213  *
214  */
215 int __cdecl MSVCRT_iswascii(WCHAR c)
216 {
217   return ((unsigned)c < 0x80);
218 }
219
220 /*********************************************************************
221  *              __iscsym (MSVCRT.@)
222  */
223 int __cdecl MSVCRT___iscsym(int c)
224 {
225   return (c < 127 && (isalnum(c) || c == '_'));
226 }
227
228 /*********************************************************************
229  *              __iscsymf (MSVCRT.@)
230  */
231 int __cdecl MSVCRT___iscsymf(int c)
232 {
233   return (c < 127 && (isalpha(c) || c == '_'));
234 }
235
236 /*********************************************************************
237  *              _toupper (MSVCRT.@)
238  */
239 int __cdecl MSVCRT__toupper(int c)
240 {
241     return c - 0x20;  /* sic */
242 }
243
244 /*********************************************************************
245  *              _tolower (MSVCRT.@)
246  */
247 int __cdecl MSVCRT__tolower(int c)
248 {
249     return c + 0x20;  /* sic */
250 }