perl5.001 patch.1c
[p5sagit/p5-mst-13.2.git] / handy.h
1 /*    handy.h
2  *
3  *    Copyright (c) 1991-1994, Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9
10 #if !defined(__STDC__)
11 #ifdef NULL
12 #undef NULL
13 #endif
14 #ifndef I286
15 #  define NULL 0
16 #else
17 #  define NULL 0L
18 #endif
19 #endif
20
21 #define Null(type) ((type)NULL)
22 #define Nullch Null(char*)
23 #define Nullfp Null(FILE*)
24 #define Nullsv Null(SV*)
25
26 /* bool is built-in for g++-2.6.3, which might be used for an extension.
27    gcc-2.6.2 under Linux defines _G_HAVE_BOOL to 0, and does not
28    define bool. */
29 #if !defined(HAS_BOOL) && !(_G_HAVE_BOOL)
30 #ifdef UTS
31 #define bool int
32 #else
33 #define bool char
34 #endif
35 #endif /* !defined(HAS_BOOL) && !(_G_HAVE_BOOL) */
36
37 #ifdef TRUE
38 #undef TRUE
39 #endif
40 #ifdef FALSE
41 #undef FALSE
42 #endif
43 #define TRUE (1)
44 #define FALSE (0)
45
46 typedef char            I8;
47 typedef unsigned char   U8;
48
49 typedef short           I16;
50 typedef unsigned short  U16;
51
52 #if BYTEORDER > 0x4321
53   typedef int           I32;
54   typedef unsigned int  U32;
55 #else
56   typedef long          I32;
57   typedef unsigned long U32;
58 #endif
59
60 #define Ctl(ch) (ch & 037)
61
62 #define strNE(s1,s2) (strcmp(s1,s2))
63 #define strEQ(s1,s2) (!strcmp(s1,s2))
64 #define strLT(s1,s2) (strcmp(s1,s2) < 0)
65 #define strLE(s1,s2) (strcmp(s1,s2) <= 0)
66 #define strGT(s1,s2) (strcmp(s1,s2) > 0)
67 #define strGE(s1,s2) (strcmp(s1,s2) >= 0)
68 #define strnNE(s1,s2,l) (strncmp(s1,s2,l))
69 #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
70
71 #ifdef HAS_SETLOCALE  /* XXX Is there a better test for this? */
72 #  ifndef CTYPE256
73 #    define CTYPE256
74 #  endif
75 #endif
76
77 #ifdef USE_NEXT_CTYPE 
78 #define isALNUM(c)   (NXIsAlpha((unsigned int)c) || NXIsDigit((unsigned int)c) || c == '_')
79 #define isIDFIRST(c) (NXIsAlpha((unsigned int)c) || c == '_')
80 #define isALPHA(c)   NXIsAlpha((unsigned int)c)
81 #define isSPACE(c)   NXIsSpace((unsigned int)c)
82 #define isDIGIT(c)   NXIsDigit((unsigned int)c)
83 #define isUPPER(c)   NXIsUpper((unsigned int)c)
84 #define isLOWER(c)   NXIsLower((unsigned int)c)
85 #define toUPPER(c)   NXToUpper((unsigned int)c)
86 #define toLOWER(c)   NXToLower((unsigned int)c)
87 #else /* USE_NEXT_CTYPE */
88 #if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
89 #define isALNUM(c)   (isalpha((unsigned char)(c)) || isdigit((unsigned char)(c)) || c == '_')
90 #define isIDFIRST(c) (isalpha((unsigned char)(c)) || (c) == '_')
91 #define isALPHA(c)   isalpha((unsigned char)(c))
92 #define isSPACE(c)   isspace((unsigned char)(c))
93 #define isDIGIT(c)   isdigit((unsigned char)(c))
94 #define isUPPER(c)   isupper((unsigned char)(c))
95 #define isLOWER(c)   islower((unsigned char)(c))
96 #define toUPPER(c)   toupper((unsigned char)(c))
97 #define toLOWER(c)   tolower((unsigned char)(c))
98 #else
99 #define isALNUM(c)   (isascii(c) && (isalpha(c) || isdigit(c) || c == '_'))
100 #define isIDFIRST(c) (isascii(c) && (isalpha(c) || (c) == '_'))
101 #define isALPHA(c)   (isascii(c) && isalpha(c))
102 #define isSPACE(c)   (isascii(c) && isspace(c))
103 #define isDIGIT(c)   (isascii(c) && isdigit(c))
104 #define isUPPER(c)   (isascii(c) && isupper(c))
105 #define isLOWER(c)   (isascii(c) && islower(c))
106 #define toUPPER(c)   toupper(c)
107 #define toLOWER(c)   tolower(c)
108 #endif
109 #endif /* USE_NEXT_CTYPE */
110
111 /* Line numbers are unsigned, 16 bits. */
112 typedef U16 line_t;
113 #ifdef lint
114 #define NOLINE ((line_t)0)
115 #else
116 #define NOLINE ((line_t) 65535)
117 #endif
118
119 #ifndef lint
120 #ifndef LEAKTEST
121 #ifndef safemalloc
122 char *safemalloc _((MEM_SIZE));
123 char *saferealloc _((char *, MEM_SIZE));
124 void safefree _((char *));
125 #endif
126 #ifndef MSDOS
127 #define New(x,v,n,t)  (v = (t*)safemalloc((MEM_SIZE)((n) * sizeof(t))))
128 #define Newc(x,v,n,t,c)  (v = (c*)safemalloc((MEM_SIZE)((n) * sizeof(t))))
129 #define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n) * sizeof(t)))), \
130     memzero((char*)(v), (n) * sizeof(t))
131 #define Renew(v,n,t) (v = (t*)saferealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
132 #define Renewc(v,n,t,c) (v = (c*)saferealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
133 #else
134 #define New(x,v,n,t)  (v = (t*)safemalloc(((unsigned long)(n) * sizeof(t))))
135 #define Newc(x,v,n,t,c)  (v = (c*)safemalloc(((unsigned long)(n) * sizeof(t))))
136 #define Newz(x,v,n,t) (v = (t*)safemalloc(((unsigned long)(n) * sizeof(t)))), \
137     memzero((char*)(v), (n) * sizeof(t))
138 #define Renew(v,n,t) (v = (t*)saferealloc((char*)(v),((unsigned long)(n)*sizeof(t))))
139 #define Renewc(v,n,t,c) (v = (c*)saferealloc((char*)(v),((unsigned long)(n)*sizeof(t))))
140 #endif /* MSDOS */
141 #define Safefree(d) safefree((char*)d)
142 #define NEWSV(x,len) newSV(len)
143 #else /* LEAKTEST */
144 char *safexmalloc();
145 char *safexrealloc();
146 void safexfree();
147 #define New(x,v,n,t)  (v = (t*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t))))
148 #define Newc(x,v,n,t,c)  (v = (c*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t))))
149 #define Newz(x,v,n,t) (v = (t*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t)))), \
150     memzero((char*)(v), (n) * sizeof(t))
151 #define Renew(v,n,t) (v = (t*)safexrealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
152 #define Renewc(v,n,t,c) (v = (c*)safexrealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
153 #define Safefree(d) safexfree((char*)d)
154 #define NEWSV(x,len) newSV(x,len)
155 #define MAXXCOUNT 1200
156 long xcount[MAXXCOUNT];
157 long lastxcount[MAXXCOUNT];
158 #endif /* LEAKTEST */
159 #define Move(s,d,n,t) (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
160 #define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
161 #define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t))
162 #else /* lint */
163 #define New(x,v,n,s) (v = Null(s *))
164 #define Newc(x,v,n,s,c) (v = Null(s *))
165 #define Newz(x,v,n,s) (v = Null(s *))
166 #define Renew(v,n,s) (v = Null(s *))
167 #define Move(s,d,n,t)
168 #define Copy(s,d,n,t)
169 #define Zero(d,n,t)
170 #define Safefree(d) d = d
171 #endif /* lint */
172
173 #ifdef USE_STRUCT_COPY
174 #define StructCopy(s,d,t) *((t*)(d)) = *((t*)(s))
175 #else
176 #define StructCopy(s,d,t) Copy(s,d,1,t)
177 #endif