#ifndef _CTYPE_H
#define _CTYPE_H
/**
 *
 * This header file defines various ASCII character manipulation macros.
 * If you want the 'function' forms undef the relevant name.
 *
 *       isalpha(c)    non-zero if c is alpha
 *       isupper(c)    non-zero if c is upper case
 *       islower(c)    non-zero if c is lower case
 *       isdigit(c)    non-zero if c is a digit (0 to 9)
 *       isxdigit(c)   non-zero if c is a hexadecimal digit (0 to 9, A to F,
 *                   a to f)
 *       isspace(c)    non-zero if c is white space
 *       ispunct(c)    non-zero if c is punctuation
 *       isalnum(c)    non-zero if c is alpha or digit
 *       isprint(c)    non-zero if c is printable (including blank)
 *       isgraph(c)    non-zero if c is graphic (excluding blank)
 *       iscntrl(c)    non-zero if c is control character
 *       isascii(c)    non-zero if c is ASCII
 *       iscsym(c)     non-zero if valid character for C symbols
 *       iscsymf(c)    non-zero if valid first character for C symbols
 * 
 * AMENDMENT HISTORY
 *~~~~~~~~~~~~~~~~~~
 *   25 Jan 94   DJW  - Only define the macro versions of toupper() and
 *                      tolower() when not in ANSI mode as they evaluate
 *                      their arguments more than once.
 *                      Reported by Richard Kettlewell 
 *
 *  28 May 94   DJW   - Changed the '_ctype' variable to be a character
 *                      pointer to the _ctype table values.  This removes
 *                      the need to adjust the offset by 1 in parameters,
 *                      and also allows for locale hanbling to be implemented.
 *                    - Changed to iscsym(), iscsymf(), tolower() and toupper()
 *                      macros to use an intermediate work area to avoid
 *                      double evalutaion of arguments.
 *                    - Added _tolower() and _toupper() macros defined by POSIX
 *                    - Added explicit casts to 'int' to most macro expansions
 *
 *  11 Mar 95   DJW   - Added casts to 'unsigned char' for many of the constant
 *                      values as this allows the compiler to generate more
 *                      effecient code.
**/

#ifndef _TYPES_H
#include <sys/types.h>
#endif

#ifdef __STDC__
#define _P_(params) params
#else
#define _P_(params) ()
#endif

/*
 *  Give prototypes for library versions
 *  NOTE.  It is important that the prototypes
 *         preceed the macro definitions.
 */
int isalpha     _P_((int));
int isalnum     _P_((int));
int isascii     _P_((int));
int iscntrl     _P_((int));
int iscsym      _P_((int));
int iscsymf     _P_((int));
int isdigit     _P_((int));
int isgraph     _P_((int));
int islower     _P_((int));
int isprint     _P_((int));
int ispunct     _P_((int));
int isspace     _P_((int));
int isupper     _P_((int));
int isxdigit    _P_((int));
int toascii     _P_((int));
int tolower     _P_((int));
int toupper     _P_((int));

/*
 *  Now the more common macro definitions of these routines
 */
#define _U 1          /* upper case flag */
#define _L 2          /* lower case flag */
#define _N 4          /* number flag */
#define _S 8          /* space flag */
#define _P 16         /* punctuation flag */
#define _C 32         /* control character flag */
#define _B 64         /* blank flag */
#define _X 128        /* hexadecimal flag */

extern unsigned char * _ctype;   /* pointer to character type table */
extern int  _ctypetemp; /* temporary storage area used by macros */

#define isalpha(c)      (_ctype[(int)(c)] & (unsigned char)(_U|_L))
#define isupper(c)      (_ctype[(int)(c)] & (unsigned char)_U)
#define islower(c)      (_ctype[(int)(c)] & (unsigned char)_L)
#define isdigit(c)      (_ctype[(int)(c)] & (unsigned char)_N)
#define isxdigit(c)     (_ctype[(int)(c)] & (unsigned char)_X)
#define isspace(c)      (_ctype[(int)(c)] & (unsigned char)_S)
#define ispunct(c)      (_ctype[(int)(c)] & (unsigned char)_P)
#define isalnum(c)      (_ctype[(int)(c)] & (unsigned char)(_U|_L|_N))
#define isprint(c)      (_ctype[(int)(c)] & (unsigned char)(_P|_U|_L|_N|_B))
#define isgraph(c)      (_ctype[(int)(c)] & (unsigned char)(_P|_U|_L|_N))
#define iscntrl(c)      (_ctype[(int)(c)] & (unsigned char)_C)
#define isascii(c)      ((unsigned)(c)<=127)

#define toascii(c)      ((int)(c)&127)

/*
 *  The following are defined in POSIX as always being macros
 */
#define _toupper(c)    (_ctypetemp=(int)(c),islower(_ctypetemp)?((_ctypetemp)-('a'-'A')):(_ctypetemp))
#define _tolower(c)    (_ctypetemp=(int)(c),isupper(_ctypetemp)?((_ctypetemp)+('a'-'A')):(_ctypetemp))

/*
 *  Macros which evaluate there argiments more than once
 *  need to use an intermediate temporary storage area.
 */
#define iscsym(c)       (_ctypetemp=(int)(c),isalnum(_ctypetemp)||(((_ctypetemp)&127)==0x5f))
#define iscsymf(c)      (_ctypetemp=(int)(c),isalpha(_ctypetemp)||(((_ctypetemp)&127)==0x5f))

#define toupper(c)     _toupper(c)
#define tolower(c)     _tolower(c)


#undef _P_

#endif  /* _CTYPE_H */

