ctype.c 12 KB
Newer Older
1 2 3 4
/*
 * msvcrt.dll ctype functions
 *
 * Copyright 2000 Jon Griffiths
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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/unicode.h"
24

25
/* Some abbreviations to make the following table readable */
26 27 28 29 30 31 32
#define _C_ MSVCRT__CONTROL
#define _S_ MSVCRT__SPACE
#define _P_ MSVCRT__PUNCT
#define _D_ MSVCRT__DIGIT
#define _H_ MSVCRT__HEX
#define _U_ MSVCRT__UPPER
#define _L_ MSVCRT__LOWER
33 34 35 36

WORD MSVCRT__ctype [257] = {
  0, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _C_, _S_|_C_, _S_|_C_,
  _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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _P_,
  _P_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_, _D_|_H_,
  _D_|_H_, _D_|_H_, _D_|_H_, _P_, _P_, _P_, _P_, _P_, _P_, _P_, _U_|_H_,
  _U_|_H_, _U_|_H_, _U_|_H_, _U_|_H_, _U_|_H_, _U_, _U_, _U_, _U_, _U_,
  _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_, _U_,
  _U_, _P_, _P_, _P_, _P_, _P_, _P_, _L_|_H_, _L_|_H_, _L_|_H_, _L_|_H_,
  _L_|_H_, _L_|_H_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_,
  _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _L_, _P_, _P_, _P_, _P_,
  _C_, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

54 55 56 57 58 59 60 61
/*********************************************************************
 *		__p__pctype (MSVCRT.@)
 */
unsigned short** CDECL MSVCRT___p__pctype(void)
{
    return &get_locinfo()->pctype;
}

62
/*********************************************************************
63
 *		__pctype_func (MSVCRT.@)
64
 */
65
const unsigned short* CDECL MSVCRT___pctype_func(void)
66
{
67
    return get_locinfo()->pctype;
68 69 70
}

/*********************************************************************
71
 *		_isctype_l (MSVCRT.@)
72
 */
73
int CDECL MSVCRT__isctype_l(int c, int type, MSVCRT__locale_t locale)
74
{
75 76
  MSVCRT_pthreadlocinfo locinfo;

77
  if(!locale)
78 79 80
    locinfo = get_locinfo();
  else
    locinfo = locale->locinfo;
81

82
  if (c >= -1 && c <= 255)
83
    return locinfo->pctype[c] & type;
84

85
  if (locinfo->mb_cur_max != 1 && c > 0)
86 87 88 89 90
  {
    /* FIXME: Is there a faster way to do this? */
    WORD typeInfo;
    char convert[3], *pconv = convert;

91
    if (locinfo->pctype[(UINT)c >> 8] & MSVCRT__LEADBYTE)
92 93 94
      *pconv++ = (UINT)c >> 8;
    *pconv++ = c & 0xff;
    *pconv = 0;
95

96
    if (GetStringTypeExA(locinfo->lc_handle[MSVCRT_LC_CTYPE],
97
                CT_CTYPE1, convert, convert[1] ? 2 : 1, &typeInfo))
98 99 100 101 102
      return typeInfo & type;
  }
  return 0;
}

103 104 105
/*********************************************************************
 *              _isctype (MSVCRT.@)
 */
106
int CDECL MSVCRT__isctype(int c, int type)
107
{
108
    return MSVCRT__isctype_l(c, type, NULL);
109 110
}

111 112 113 114 115
/*********************************************************************
 *		_isalnum_l (MSVCRT.@)
 */
int CDECL MSVCRT__isalnum_l(int c, MSVCRT__locale_t locale)
{
116
  return MSVCRT__isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT, locale );
117 118
}

119 120 121
/*********************************************************************
 *		isalnum (MSVCRT.@)
 */
122
int CDECL MSVCRT_isalnum(int c)
123
{
124
  return MSVCRT__isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT );
125 126
}

127 128 129 130 131
/*********************************************************************
 *		_isalpha_l (MSVCRT.@)
 */
int CDECL MSVCRT__isalpha_l(int c, MSVCRT__locale_t locale)
{
132
  return MSVCRT__isctype_l( c, MSVCRT__ALPHA, locale );
133 134
}

135 136 137
/*********************************************************************
 *		isalpha (MSVCRT.@)
 */
138
int CDECL MSVCRT_isalpha(int c)
139
{
140
  return MSVCRT__isctype( c, MSVCRT__ALPHA );
141 142
}

143 144 145 146 147
/*********************************************************************
 *		_iscntrl_l (MSVCRT.@)
 */
int CDECL MSVCRT__iscntrl_l(int c, MSVCRT__locale_t locale)
{
148
  return MSVCRT__isctype_l( c, MSVCRT__CONTROL, locale );
149 150
}

151 152 153
/*********************************************************************
 *		iscntrl (MSVCRT.@)
 */
154
int CDECL MSVCRT_iscntrl(int c)
155
{
156
  return MSVCRT__isctype( c, MSVCRT__CONTROL );
157 158
}

159 160 161 162 163
/*********************************************************************
 *		_isdigit_l (MSVCRT.@)
 */
int CDECL MSVCRT__isdigit_l(int c, MSVCRT__locale_t locale)
{
164
  return MSVCRT__isctype_l( c, MSVCRT__DIGIT, locale );
165 166
}

167 168 169
/*********************************************************************
 *		isdigit (MSVCRT.@)
 */
170
int CDECL MSVCRT_isdigit(int c)
171
{
172
  return MSVCRT__isctype( c, MSVCRT__DIGIT );
173 174
}

175 176 177 178 179
/*********************************************************************
 *		_isgraph_l (MSVCRT.@)
 */
int CDECL MSVCRT__isgraph_l(int c, MSVCRT__locale_t locale)
{
180
  return MSVCRT__isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__PUNCT, locale );
181 182
}

183 184 185
/*********************************************************************
 *		isgraph (MSVCRT.@)
 */
186
int CDECL MSVCRT_isgraph(int c)
187
{
188
  return MSVCRT__isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__PUNCT );
189 190
}

191 192 193 194 195
/*********************************************************************
 *		_isleadbyte_l (MSVCRT.@)
 */
int CDECL MSVCRT__isleadbyte_l(int c, MSVCRT__locale_t locale)
{
196
  return MSVCRT__isctype_l( c, MSVCRT__LEADBYTE, locale );
197 198
}

199 200 201
/*********************************************************************
 *		isleadbyte (MSVCRT.@)
 */
202
int CDECL MSVCRT_isleadbyte(int c)
203
{
204
  return MSVCRT__isctype( c, MSVCRT__LEADBYTE );
205 206
}

207 208 209 210 211
/*********************************************************************
 *		_islower_l (MSVCRT.@)
 */
int CDECL MSVCRT__islower_l(int c, MSVCRT__locale_t locale)
{
212
  return MSVCRT__isctype_l( c, MSVCRT__LOWER, locale );
213 214
}

215 216 217
/*********************************************************************
 *		islower (MSVCRT.@)
 */
218
int CDECL MSVCRT_islower(int c)
219
{
220
  return MSVCRT__isctype( c, MSVCRT__LOWER );
221 222
}

223 224 225 226 227
/*********************************************************************
 *		_isprint_l (MSVCRT.@)
 */
int CDECL MSVCRT__isprint_l(int c, MSVCRT__locale_t locale)
{
228
  return MSVCRT__isctype_l( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__BLANK | MSVCRT__PUNCT, locale );
229 230
}

231 232 233
/*********************************************************************
 *		isprint (MSVCRT.@)
 */
234
int CDECL MSVCRT_isprint(int c)
235
{
236
  return MSVCRT__isctype( c, MSVCRT__ALPHA | MSVCRT__DIGIT | MSVCRT__BLANK | MSVCRT__PUNCT );
237 238 239 240 241
}

/*********************************************************************
 *		ispunct (MSVCRT.@)
 */
242
int CDECL MSVCRT_ispunct(int c)
243
{
244
  return MSVCRT__isctype( c, MSVCRT__PUNCT );
245 246
}

247 248 249 250 251
/*********************************************************************
 *		_isspace_l (MSVCRT.@)
 */
int CDECL MSVCRT__isspace_l(int c, MSVCRT__locale_t locale)
{
252
  return MSVCRT__isctype_l( c, MSVCRT__SPACE, locale );
253 254
}

255 256 257
/*********************************************************************
 *		isspace (MSVCRT.@)
 */
258
int CDECL MSVCRT_isspace(int c)
259
{
260
  return MSVCRT__isctype( c, MSVCRT__SPACE );
261 262
}

263 264 265 266 267
/*********************************************************************
 *		_isupper_l (MSVCRT.@)
 */
int CDECL MSVCRT__isupper_l(int c, MSVCRT__locale_t locale)
{
268
  return MSVCRT__isctype_l( c, MSVCRT__UPPER, locale );
269 270
}

271 272 273
/*********************************************************************
 *		isupper (MSVCRT.@)
 */
274
int CDECL MSVCRT_isupper(int c)
275
{
276
  return MSVCRT__isctype( c, MSVCRT__UPPER );
277 278
}

279 280 281 282 283
/*********************************************************************
 *		_isxdigit_l (MSVCRT.@)
 */
int CDECL MSVCRT__isxdigit_l(int c, MSVCRT__locale_t locale)
{
284
  return MSVCRT__isctype_l( c, MSVCRT__HEX, locale );
285 286
}

287 288 289
/*********************************************************************
 *		isxdigit (MSVCRT.@)
 */
290
int CDECL MSVCRT_isxdigit(int c)
291
{
292
  return MSVCRT__isctype( c, MSVCRT__HEX );
293 294 295 296 297
}

/*********************************************************************
 *		__isascii (MSVCRT.@)
 */
298
int CDECL MSVCRT___isascii(int c)
299 300 301 302 303 304 305
{
  return isascii((unsigned)c);
}

/*********************************************************************
 *		__toascii (MSVCRT.@)
 */
306
int CDECL MSVCRT___toascii(int c)
307 308 309 310 311 312 313 314
{
  return (unsigned)c & 0x7f;
}

/*********************************************************************
 *		iswascii (MSVCRT.@)
 *
 */
315
int CDECL MSVCRT_iswascii(MSVCRT_wchar_t c)
316 317 318 319 320 321 322
{
  return ((unsigned)c < 0x80);
}

/*********************************************************************
 *		__iscsym (MSVCRT.@)
 */
323
int CDECL MSVCRT___iscsym(int c)
324 325 326 327 328 329 330
{
  return (c < 127 && (isalnum(c) || c == '_'));
}

/*********************************************************************
 *		__iscsymf (MSVCRT.@)
 */
331
int CDECL MSVCRT___iscsymf(int c)
332 333 334 335
{
  return (c < 127 && (isalpha(c) || c == '_'));
}

336 337 338 339 340
/*********************************************************************
 *		_toupper_l (MSVCRT.@)
 */
int CDECL MSVCRT__toupper_l(int c, MSVCRT__locale_t locale)
{
341
    MSVCRT_pthreadlocinfo locinfo;
342 343
    unsigned char str[2], *p = str;
    WCHAR wide, upper;
344

345
    if(!locale)
346 347 348
        locinfo = get_locinfo();
    else
        locinfo = locale->locinfo;
349

350 351
    if((unsigned)c < 256)
        return locinfo->pcumap[c];
352

353
    if(locinfo->pctype[(c>>8)&255] & MSVCRT__LEADBYTE)
354
        *p++ = (c>>8) & 255;
355 356 357 358 359
    else {
        *MSVCRT__errno() = MSVCRT_EILSEQ;
        str[1] = 0;
    }
    *p++ = c & 255;
360

361 362 363
    if(!MultiByteToWideChar(locinfo->lc_codepage,
                MB_ERR_INVALID_CHARS, (char*)str, p-str, &wide, 1))
        return c;
364

365 366 367
    upper = toupperW(wide);
    if(upper == wide)
        return str[0] + (str[1]<<8);
368

369 370 371 372 373 374 375
    switch(WideCharToMultiByte(locinfo->lc_codepage, 0,
                &upper, 1, (char*)str, 2, NULL, NULL)) {
    case 0:
        return c;
    case 1:
        return str[0];
    default:
376 377 378 379 380 381 382 383 384 385 386 387
        return str[0] + (str[1]<<8);
    }
}

/*********************************************************************
 *		toupper (MSVCRT.@)
 */
int CDECL MSVCRT_toupper(int c)
{
    return MSVCRT__toupper_l(c, NULL);
}

388 389 390
/*********************************************************************
 *		_toupper (MSVCRT.@)
 */
391
int CDECL MSVCRT__toupper(int c)
392
{
393
    return c - 0x20;  /* sic */
394 395
}

396 397 398 399 400
/*********************************************************************
 *              _tolower_l (MSVCRT.@)
 */
int CDECL MSVCRT__tolower_l(int c, MSVCRT__locale_t locale)
{
401
    MSVCRT_pthreadlocinfo locinfo;
402 403
    unsigned char str[2], *p = str;
    WCHAR wide, lower;
404

405
    if(!locale)
406 407 408
        locinfo = get_locinfo();
    else
        locinfo = locale->locinfo;
409

410 411
    if((unsigned)c < 256)
        return locinfo->pclmap[c];
412

413
    if(locinfo->pctype[(c>>8)&255] & MSVCRT__LEADBYTE)
414
        *p++ = (c>>8) & 255;
415 416 417 418 419
    else {
        *MSVCRT__errno() = MSVCRT_EILSEQ;
        str[1] = 0;
    }
    *p++ = c & 255;
420

421 422 423
    if(!MultiByteToWideChar(locinfo->lc_codepage,
                MB_ERR_INVALID_CHARS, (char*)str, p-str, &wide, 1))
        return c;
424

425 426 427
    lower = tolowerW(wide);
    if(lower == wide)
        return str[0] + (str[1]<<8);
428

429 430 431 432 433 434 435
    switch(WideCharToMultiByte(locinfo->lc_codepage, 0,
                &lower, 1, (char*)str, 2, NULL, NULL)) {
    case 0:
        return c;
    case 1:
        return str[0];
    default:
436 437 438 439 440 441 442 443 444 445 446 447
        return str[0] + (str[1]<<8);
    }
}

/*********************************************************************
 *              tolower (MSVCRT.@)
 */
int CDECL MSVCRT_tolower(int c)
{
        return MSVCRT__tolower_l(c, NULL);
}

448 449 450
/*********************************************************************
 *		_tolower (MSVCRT.@)
 */
451
int CDECL MSVCRT__tolower(int c)
452
{
453
    return c + 0x20;  /* sic */
454
}