summaryrefslogtreecommitdiffstats
path: root/src/lib/ppc405lib/ctype.h
blob: 1c5f815c55a37ac817de1124266f0d18742d4a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/lib/ppc405lib/ctype.h $                                   */
/*                                                                        */
/* OpenPOWER OnChipController Project                                     */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2016                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#ifndef __CTYPE_H__
#define __CTYPE_H__

/// \file ctype.h
/// \brief Replacement for <ctype.h>
///
/// The Gnu <ctype.h> requires some locale information to be defined.  We
/// avoid this overhead and implement the simple functions, simply assuming
/// standard 8-bit ASCII. The standard requires that these be defined as entry
/// points, but may be defined as macros, therefore all macros defined by
/// ctype.h are also be replicated in \c ctype.c

// The reference for which characters are included in each set was:
//
//     http://www.cplusplus.com/reference/clibrary/cctype/
//
// Note that no code was copied from the above or any other published
// description of the ctype.h functionality.

// To keep space to a minimum we encode the 8 common types directly into
// an 8-bit mask. Other types take a little longer to compute.

#define _CTYPE_CNTRL  0x01
#define _CTYPE_SPACE  0x02
#define _CTYPE_PRINT  0x04
#define _CTYPE_PUNCT  0x08
#define _CTYPE_UPPER  0x10
#define _CTYPE_LOWER  0x20
#define _CTYPE_DIGIT  0x40
#define _CTYPE_XDIGIT 0x80

#ifndef __ASSEMBLER__

#include "stdint.h"

/// \bug <ctype.h> can not include <stdio.h> to get the definition of
/// EOF. This is because it causes conflicts withe the pore_inline* code which
/// is portable (and ported) to Linux and PHYP. We need to go back through the
/// way the includes are done in SSX and this library and fix this. We should
/// have redefined <stdio.h> rather than creating a new "ssx_io.h"

#define _CTYPE_EOF -1

// Note that in all of the type macros, 'c' is an unsigned char.

extern const uint8_t _ctype[256];

#define _CTYPE_ISLOWER(c) (_ctype[c] & _CTYPE_LOWER)

#define _CTYPE_ISUPPER(c) (_ctype[c] & _CTYPE_UPPER)

#define _CTYPE_ISALPHA(c) (_CTYPE_ISUPPER(c) || _CTYPE_ISLOWER(c))

#define _CTYPE_ISDIGIT(c) (_ctype[c] & _CTYPE_DIGIT)

#define _CTYPE_ISALNUM(c) (_CTYPE_ISALPHA(c) || _CTYPE_ISDIGIT(c))

#define _CTYPE_ISXDIGIT(c) (_ctype[c] & _CTYPE_XDIGIT)

#define _CTYPE_ISCNTRL(c) (_ctype[c] & _CTYPE_CNTRL)

#define _CTYPE_ISSPACE(c) (_ctype[c] & _CTYPE_SPACE)

#define _CTYPE_ISPRINT(c) (_ctype[c] & _CTYPE_PRINT)

#define _CTYPE_ISGRAPH(c) (_CTYPE_ISPRINT(c) && ((c) != 0x20))

#define _CTYPE_ISPUNCT(c) (_ctype[c] & _CTYPE_PUNCT)

#define _CTYPE_TOUPPER(c) (islower(c) ? ((c) + ('A' - 'a')) : (c))

#define _CTYPE_TOLOWER(c) (isupper(c) ? ((c) - ('A' - 'a')) : (c))

// When #include'ed into ctype.c, the non-inline forms of the functions are
// created.  
//
// Note that the specification requires that 'c' "must have the value of an
// unsigned char or EOF". The specification also stipulates that "The values
// returned are non-zero if the character c falls into the tested class, and a
// zero value if not."

#ifdef __CTYPE_C__
#define _CTYPE_EXTERN_INLINE
#else
#define _CTYPE_EXTERN_INLINE extern inline
#endif

#define _CTYPE_PREDICATE(predicate, def)                \
    _CTYPE_EXTERN_INLINE int predicate(int c) {         \
        return ((c == _CTYPE_EOF) ?                     \
                0 : _CTYPE_##def((unsigned char)c));    \
    }

#define _CTYPE_FUNCTION(function, def)                          \
    _CTYPE_EXTERN_INLINE int function(int c) {                  \
        return ((c == _CTYPE_EOF) ?                             \
                _CTYPE_EOF : _CTYPE_##def((unsigned char)c));   \
    }

_CTYPE_PREDICATE(islower,  ISLOWER)
_CTYPE_PREDICATE(isupper,  ISUPPER)
_CTYPE_PREDICATE(isalpha,  ISALPHA)
_CTYPE_PREDICATE(isdigit,  ISDIGIT)
_CTYPE_PREDICATE(isalnum,  ISALNUM)
_CTYPE_PREDICATE(isxdigit, ISXDIGIT)
_CTYPE_PREDICATE(iscntrl,  ISCNTRL)
_CTYPE_PREDICATE(isspace,  ISSPACE)
_CTYPE_PREDICATE(isprint,  ISPRINT)
_CTYPE_PREDICATE(isgraph,  ISGRAPH)
_CTYPE_PREDICATE(ispunct,  ISPUNCT)

_CTYPE_FUNCTION(tolower, TOLOWER)
_CTYPE_FUNCTION(toupper, TOUPPER)

#endif // __ASSEMBLER__

#endif  /* __CTYPE_H__ */
OpenPOWER on IntegriCloud