perl 5.0 alpha 9
[p5sagit/p5-mst-13.2.git] / handy.h
1 /* $RCSfile: handy.h,v $$Revision: 4.1 $$Date: 92/08/07 18:21:46 $
2  *
3  *    Copyright (c) 1991, 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  * $Log:        handy.h,v $
9  * Revision 4.1  92/08/07  18:21:46  lwall
10  * 
11  * Revision 4.0.1.4  92/06/08  13:23:17  lwall
12  * patch20: isascii() may now be supplied by a library routine
13  * patch20: Perl now distinguishes overlapped copies from non-overlapped
14  * 
15  * Revision 4.0.1.3  91/11/05  22:54:26  lwall
16  * patch11: erratum
17  * 
18  * Revision 4.0.1.2  91/11/05  17:23:38  lwall
19  * patch11: prepared for ctype implementations that don't define isascii()
20  * 
21  * Revision 4.0.1.1  91/06/07  11:09:56  lwall
22  * patch4: new copyright notice
23  * 
24  * Revision 4.0  91/03/20  01:22:15  lwall
25  * 4.0 baseline.
26  * 
27  */
28
29 #if !defined(__STDC__)
30 #ifdef NULL
31 #undef NULL
32 #endif
33 #ifndef I286
34 #  define NULL 0
35 #else
36 #  define NULL 0L
37 #endif
38 #endif
39
40 #define Null(type) ((type)NULL)
41 #define Nullch Null(char*)
42 #define Nullfp Null(FILE*)
43 #define Nullsv Null(SV*)
44
45 #ifdef UTS
46 #define bool int
47 #else
48 #define bool char
49 #endif
50
51 #ifdef TRUE
52 #undef TRUE
53 #endif
54 #ifdef FALSE
55 #undef FALSE
56 #endif
57 #define TRUE (1)
58 #define FALSE (0)
59
60 #ifdef UNICOS
61 #define I8      char
62 #define U8      unsigned char
63 #define I16     short
64 #define U16     unsigned short
65 #define I32     int
66 #define U32     unsigned int
67
68 #else
69
70 typedef char            I8;
71 typedef unsigned char   U8;
72
73 typedef short           I16;
74 typedef unsigned short  U16;
75
76 #if INTSIZE == 4
77   typedef int           I32;
78   typedef unsigned int  U32;
79 #else
80   typedef long          I32;
81   typedef unsigned long U32;
82 #endif
83
84 #endif /* UNICOS */
85
86 #define Ctl(ch) (ch & 037)
87
88 #define strNE(s1,s2) (strcmp(s1,s2))
89 #define strEQ(s1,s2) (!strcmp(s1,s2))
90 #define strLT(s1,s2) (strcmp(s1,s2) < 0)
91 #define strLE(s1,s2) (strcmp(s1,s2) <= 0)
92 #define strGT(s1,s2) (strcmp(s1,s2) > 0)
93 #define strGE(s1,s2) (strcmp(s1,s2) >= 0)
94 #define strnNE(s1,s2,l) (strncmp(s1,s2,l))
95 #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
96
97 #ifdef HAS_SETLOCALE  /* XXX Is there a better test for this? */
98 #  ifndef CTYPE256
99 #    define CTYPE256
100 #  endif
101 #endif
102
103 #if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
104 #define isALNUM(c)   (isalpha((unsigned char)(c)) || isdigit((unsigned char)(c)) || c == '_')
105 #define isIDFIRST(c) (isalpha((unsigned char)(c)) || (c) == '_')
106 #define isALPHA(c)   isalpha((unsigned char)(c))
107 #define isSPACE(c)   isspace((unsigned char)(c))
108 #define isDIGIT(c)   isdigit((unsigned char)(c))
109 #define isUPPER(c)   isupper((unsigned char)(c))
110 #define isLOWER(c)   islower((unsigned char)(c))
111 #else
112 #define isALNUM(c)   (isascii(c) && (isalpha(c) || isdigit(c) || c == '_'))
113 #define isIDFIRST(c) (isascii(c) && (isalpha(c) || (c) == '_'))
114 #define isALPHA(c)   (isascii(c) && isalpha(c))
115 #define isSPACE(c)   (isascii(c) && isspace(c))
116 #define isDIGIT(c)   (isascii(c) && isdigit(c))
117 #define isUPPER(c)   (isascii(c) && isupper(c))
118 #define isLOWER(c)   (isascii(c) && islower(c))
119 #endif
120
121 /* Line numbers are unsigned, 16 bits. */
122 typedef U16 line_t;
123 #ifdef lint
124 #define NOLINE ((line_t)0)
125 #else
126 #define NOLINE ((line_t) 65535)
127 #endif
128
129 #ifndef lint
130 #ifndef LEAKTEST
131 #ifndef safemalloc
132 char *safemalloc();
133 char *saferealloc();
134 void safefree();
135 #endif
136 #ifndef MSDOS
137 #define New(x,v,n,t)  (v = (t*)safemalloc((MEM_SIZE)((n) * sizeof(t))))
138 #define Newc(x,v,n,t,c)  (v = (c*)safemalloc((MEM_SIZE)((n) * sizeof(t))))
139 #define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n) * sizeof(t)))), \
140     memzero((char*)(v), (n) * sizeof(t))
141 #define Renew(v,n,t) (v = (t*)saferealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
142 #define Renewc(v,n,t,c) (v = (c*)saferealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
143 #else
144 #define New(x,v,n,t)  (v = (t*)safemalloc(((unsigned long)(n) * sizeof(t))))
145 #define Newc(x,v,n,t,c)  (v = (c*)safemalloc(((unsigned long)(n) * sizeof(t))))
146 #define Newz(x,v,n,t) (v = (t*)safemalloc(((unsigned long)(n) * sizeof(t)))), \
147     memzero((char*)(v), (n) * sizeof(t))
148 #define Renew(v,n,t) (v = (t*)saferealloc((char*)(v),((unsigned long)(n)*sizeof(t))))
149 #define Renewc(v,n,t,c) (v = (c*)saferealloc((char*)(v),((unsigned long)(n)*sizeof(t))))
150 #endif /* MSDOS */
151 #define Safefree(d) safefree((char*)d)
152 #define NEWSV(x,len) newSV(len)
153 #else /* LEAKTEST */
154 char *safexmalloc();
155 char *safexrealloc();
156 void safexfree();
157 #define New(x,v,n,t)  (v = (t*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t))))
158 #define Newc(x,v,n,t,c)  (v = (c*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t))))
159 #define Newz(x,v,n,t) (v = (t*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t)))), \
160     memzero((char*)(v), (n) * sizeof(t))
161 #define Renew(v,n,t) (v = (t*)safexrealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
162 #define Renewc(v,n,t,c) (v = (c*)safexrealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
163 #define Safefree(d) safexfree((char*)d)
164 #define NEWSV(x,len) newSV(x,len)
165 #define MAXXCOUNT 1200
166 long xcount[MAXXCOUNT];
167 long lastxcount[MAXXCOUNT];
168 #endif /* LEAKTEST */
169 #define Move(s,d,n,t) (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
170 #define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
171 #define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t))
172 #else /* lint */
173 #define New(x,v,n,s) (v = Null(s *))
174 #define Newc(x,v,n,s,c) (v = Null(s *))
175 #define Newz(x,v,n,s) (v = Null(s *))
176 #define Renew(v,n,s) (v = Null(s *))
177 #define Move(s,d,n,t)
178 #define Copy(s,d,n,t)
179 #define Zero(d,n,t)
180 #define Safefree(d) d = d
181 #endif /* lint */
182
183 #ifdef USE_STRUCT_COPY
184 #define StructCopy(s,d,t) *((t*)(d)) = *((t*)(s))
185 #else
186 #define StructCopy(s,d,t) Copy(s,d,1,t)
187 #endif