/*
 * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
 * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
 * PDC I/O Library (C) 1987 by J.A. Lydiatt.
 *
 * This code is freely redistributable upon the conditions that this
 * notice remains intact and that modified versions of this file not
 * be included as part of the PDC Software Distribution without the
 * express consent of the copyright holders.  No warrantee of any
 * kind is provided with this code.  For further information, contact:
 *
 *  PDC Software Distribution    Internet:                     BIX:
 *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
 *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
 */

/*
 * ctype.h - very efficient macros for classifying characters.
 * note, these do _not_ test that the parameter is in range.
 */

/*
 *  3.3.91 sjw; ensure only done once, fix toupper(), tolower() to
 *              only change when proper
 * 28.6.91 sjw; fix isgraph(), isascii(), iscsym(), toint(),
 *              toupper(), tolower() to evaluate argument once only
 *              NOTE the use of ({}) is a GNU extension.
 */

#ifndef __CTYPE_H__
#define __CTYPE_H__

extern char _ctype[256];

#define _UPPER       (1)
#define _LOWER       (1 << 1)
#define _HEXIT       (1 << 2)
#define _DIGIT       (1 << 3)
#define _SPACE       (1 << 4)
#define _CNTRL       (1 << 5)
#define _PUNCT       (1 << 6)
#define _OCTIT       (1 << 7)

/* standard */

#define isalnum(x)   (_ctype[(x)&0xff] & (_UPPER | _LOWER | _DIGIT))
#define isalpha(x)   (_ctype[(x)&0xff] & (_UPPER | _LOWER))
#define iscntrl(x)   (_ctype[(x)&0xff] & _CNTRL)
#define isdigit(x)   (_ctype[(x)&0xff] & _DIGIT)
#define isgraph(x) \
        ({int y = x; \
          (!(_ctype[(y)&0xff] & _CNTRL)) & ((y) != ' ');})
#define islower(x)   (_ctype[(x)&0xff] & _LOWER)
#define isprint(x)   (!(_ctype[(x)&0xff] & _CNTRL))
#define ispunct(x)   (_ctype[(x)&0xff] & _PUNCT)
#define isspace(x)   (_ctype[(x)&0xff] & _SPACE)
#define isupper(x)   (_ctype[(x)&0xff] & _UPPER)
#define isxdigit(x)  (_ctype[(x)&0xff] & _HEXIT)

#define tolower(x)   ({int y = x; isupper(y) ? y | 32 : y;})
#define toupper(x)   ({int y = x; islower(y) ? y & ~32 : y;})

/* extras */

#define iswhite(x)   (_ctype[(x)&0xff] & _SPACE)
#define isodigit(x)  (_ctype[(x)&0xff] & _OCTIT)

#define isascii(x)   ({int y = x; y >= 0 && y < 128;})
#define iscsym(x) \
        ({int y = x; \
          (_ctype[y & 0xff] & (_UPPER | _LOWER | _DIGIT)) \
          || (y == '_') \
          || (y == '$');})

#define tocntrl(x)  (((((x)+1)&~96)-1)&127)
#define toascii(x)  ((x) & 127)
#define toint(x) \
        ({int y = x; \
         (int)((_ctype[y&0xff]&_DIGIT)?(y-'0'):((y|32)-'a'+10));})

#endif /* __CTYPE_H__ */

