Back to index

cell-binutils  2.17cvs20070401
safe-ctype.h
Go to the documentation of this file.
00001 /* <ctype.h> replacement macros.
00002 
00003    Copyright (C) 2000, 2001 Free Software Foundation, Inc.
00004    Contributed by Zack Weinberg <zackw@stanford.edu>.
00005 
00006 This file is part of the libiberty library.
00007 Libiberty is free software; you can redistribute it and/or
00008 modify it under the terms of the GNU Library General Public
00009 License as published by the Free Software Foundation; either
00010 version 2 of the License, or (at your option) any later version.
00011 
00012 Libiberty is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 Library General Public License for more details.
00016 
00017 You should have received a copy of the GNU Library General Public
00018 License along with libiberty; see the file COPYING.LIB.  If
00019 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00020 Boston, MA 02110-1301, USA.  */
00021 
00022 /* This is a compatible replacement of the standard C library's <ctype.h>
00023    with the following properties:
00024 
00025    - Implements all isxxx() macros required by C99.
00026    - Also implements some character classes useful when
00027      parsing C-like languages.
00028    - Does not change behavior depending on the current locale.
00029    - Behaves properly for all values in the range of a signed or
00030      unsigned char.
00031 
00032    To avoid conflicts, this header defines the isxxx functions in upper
00033    case, e.g. ISALPHA not isalpha.  */
00034 
00035 #ifndef SAFE_CTYPE_H
00036 #define SAFE_CTYPE_H
00037 
00038 #ifdef isalpha
00039  #error "safe-ctype.h and ctype.h may not be used simultaneously"
00040 #endif
00041 
00042 /* Determine host character set.  */
00043 #define HOST_CHARSET_UNKNOWN 0
00044 #define HOST_CHARSET_ASCII   1
00045 #define HOST_CHARSET_EBCDIC  2
00046 
00047 #if  '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 \
00048    && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21
00049 #  define HOST_CHARSET HOST_CHARSET_ASCII
00050 #else
00051 # if '\n' == 0x15 && ' ' == 0x40 && '0' == 0xF0 \
00052    && 'A' == 0xC1 && 'a' == 0x81 && '!' == 0x5A
00053 #  define HOST_CHARSET HOST_CHARSET_EBCDIC
00054 # else
00055 #  define HOST_CHARSET HOST_CHARSET_UNKNOWN
00056 # endif
00057 #endif
00058 
00059 /* Categories.  */
00060 
00061 enum {
00062   /* In C99 */
00063   _sch_isblank  = 0x0001,   /* space \t */
00064   _sch_iscntrl  = 0x0002,   /* nonprinting characters */
00065   _sch_isdigit  = 0x0004,   /* 0-9 */
00066   _sch_islower  = 0x0008,   /* a-z */
00067   _sch_isprint  = 0x0010,   /* any printing character including ' ' */
00068   _sch_ispunct  = 0x0020,   /* all punctuation */
00069   _sch_isspace  = 0x0040,   /* space \t \n \r \f \v */
00070   _sch_isupper  = 0x0080,   /* A-Z */
00071   _sch_isxdigit = 0x0100,   /* 0-9A-Fa-f */
00072 
00073   /* Extra categories useful to cpplib.  */
00074   _sch_isidst = 0x0200,     /* A-Za-z_ */
00075   _sch_isvsp    = 0x0400,   /* \n \r */
00076   _sch_isnvsp   = 0x0800,   /* space \t \f \v \0 */
00077 
00078   /* Combinations of the above.  */
00079   _sch_isalpha  = _sch_isupper|_sch_islower,     /* A-Za-z */
00080   _sch_isalnum  = _sch_isalpha|_sch_isdigit,     /* A-Za-z0-9 */
00081   _sch_isidnum  = _sch_isidst|_sch_isdigit,      /* A-Za-z0-9_ */
00082   _sch_isgraph  = _sch_isalnum|_sch_ispunct,     /* isprint and not space */
00083   _sch_iscppsp  = _sch_isvsp|_sch_isnvsp, /* isspace + \0 */
00084   _sch_isbasic  = _sch_isprint|_sch_iscppsp     /* basic charset of ISO C
00085                                              (plus ` and @)  */
00086 };
00087 
00088 /* Character classification.  */
00089 extern const unsigned short _sch_istable[256];
00090 
00091 #define _sch_test(c, bit) (_sch_istable[(c) & 0xff] & (unsigned short)(bit))
00092 
00093 #define ISALPHA(c)  _sch_test(c, _sch_isalpha)
00094 #define ISALNUM(c)  _sch_test(c, _sch_isalnum)
00095 #define ISBLANK(c)  _sch_test(c, _sch_isblank)
00096 #define ISCNTRL(c)  _sch_test(c, _sch_iscntrl)
00097 #define ISDIGIT(c)  _sch_test(c, _sch_isdigit)
00098 #define ISGRAPH(c)  _sch_test(c, _sch_isgraph)
00099 #define ISLOWER(c)  _sch_test(c, _sch_islower)
00100 #define ISPRINT(c)  _sch_test(c, _sch_isprint)
00101 #define ISPUNCT(c)  _sch_test(c, _sch_ispunct)
00102 #define ISSPACE(c)  _sch_test(c, _sch_isspace)
00103 #define ISUPPER(c)  _sch_test(c, _sch_isupper)
00104 #define ISXDIGIT(c) _sch_test(c, _sch_isxdigit)
00105 
00106 #define ISIDNUM(c)   _sch_test(c, _sch_isidnum)
00107 #define ISIDST(c)    _sch_test(c, _sch_isidst)
00108 #define IS_ISOBASIC(c)      _sch_test(c, _sch_isbasic)
00109 #define IS_VSPACE(c) _sch_test(c, _sch_isvsp)
00110 #define IS_NVSPACE(c)       _sch_test(c, _sch_isnvsp)
00111 #define IS_SPACE_OR_NUL(c)  _sch_test(c, _sch_iscppsp)
00112 
00113 /* Character transformation.  */
00114 extern const unsigned char  _sch_toupper[256];
00115 extern const unsigned char  _sch_tolower[256];
00116 #define TOUPPER(c) _sch_toupper[(c) & 0xff]
00117 #define TOLOWER(c) _sch_tolower[(c) & 0xff]
00118 
00119 #endif /* SAFE_CTYPE_H */