This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Updated.
[perl5.git] / handy.h
... / ...
CommitLineData
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 If the extension includes <_G_config.h> before this file then
28 _G_HAVE_BOOL will be properly set. If, however, the extension includes
29 this file first, then you will have to manually set -DHAS_BOOL in
30 your command line to avoid a conflict.
31*/
32#ifdef _G_HAVE_BOOL
33# if _G_HAVE_BOOL
34# ifndef HAS_BOOL
35# define HAS_BOOL 1
36# endif
37# endif
38#endif
39
40#ifndef HAS_BOOL
41# ifdef UTS
42# define bool int
43# else
44# define bool char
45# endif
46#endif
47
48#ifdef TRUE
49#undef TRUE
50#endif
51#ifdef FALSE
52#undef FALSE
53#endif
54#define TRUE (1)
55#define FALSE (0)
56
57typedef char I8;
58typedef unsigned char U8;
59
60typedef short I16;
61typedef unsigned short U16;
62
63#if BYTEORDER > 0x4321
64 typedef int I32;
65 typedef unsigned int U32;
66#else
67 typedef long I32;
68 typedef unsigned long U32;
69#endif
70
71#define Ctl(ch) (ch & 037)
72
73#define strNE(s1,s2) (strcmp(s1,s2))
74#define strEQ(s1,s2) (!strcmp(s1,s2))
75#define strLT(s1,s2) (strcmp(s1,s2) < 0)
76#define strLE(s1,s2) (strcmp(s1,s2) <= 0)
77#define strGT(s1,s2) (strcmp(s1,s2) > 0)
78#define strGE(s1,s2) (strcmp(s1,s2) >= 0)
79#define strnNE(s1,s2,l) (strncmp(s1,s2,l))
80#define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
81
82#ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
83# ifndef CTYPE256
84# define CTYPE256
85# endif
86#endif
87
88#ifdef USE_NEXT_CTYPE
89#define isALNUM(c) (NXIsAlpha((unsigned int)c) || NXIsDigit((unsigned int)c) || c == '_')
90#define isIDFIRST(c) (NXIsAlpha((unsigned int)c) || c == '_')
91#define isALPHA(c) NXIsAlpha((unsigned int)c)
92#define isSPACE(c) NXIsSpace((unsigned int)c)
93#define isDIGIT(c) NXIsDigit((unsigned int)c)
94#define isUPPER(c) NXIsUpper((unsigned int)c)
95#define isLOWER(c) NXIsLower((unsigned int)c)
96#define toUPPER(c) NXToUpper((unsigned int)c)
97#define toLOWER(c) NXToLower((unsigned int)c)
98#else /* USE_NEXT_CTYPE */
99#if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
100#define isALNUM(c) (isalpha((unsigned char)(c)) || isdigit((unsigned char)(c)) || c == '_')
101#define isIDFIRST(c) (isalpha((unsigned char)(c)) || (c) == '_')
102#define isALPHA(c) isalpha((unsigned char)(c))
103#define isSPACE(c) isspace((unsigned char)(c))
104#define isDIGIT(c) isdigit((unsigned char)(c))
105#define isUPPER(c) isupper((unsigned char)(c))
106#define isLOWER(c) islower((unsigned char)(c))
107#define toUPPER(c) toupper((unsigned char)(c))
108#define toLOWER(c) tolower((unsigned char)(c))
109#else
110#define isALNUM(c) (isascii(c) && (isalpha(c) || isdigit(c) || c == '_'))
111#define isIDFIRST(c) (isascii(c) && (isalpha(c) || (c) == '_'))
112#define isALPHA(c) (isascii(c) && isalpha(c))
113#define isSPACE(c) (isascii(c) && isspace(c))
114#define isDIGIT(c) (isascii(c) && isdigit(c))
115#define isUPPER(c) (isascii(c) && isupper(c))
116#define isLOWER(c) (isascii(c) && islower(c))
117#define toUPPER(c) toupper(c)
118#define toLOWER(c) tolower(c)
119#endif
120#endif /* USE_NEXT_CTYPE */
121
122/* Line numbers are unsigned, 16 bits. */
123typedef U16 line_t;
124#ifdef lint
125#define NOLINE ((line_t)0)
126#else
127#define NOLINE ((line_t) 65535)
128#endif
129
130#ifndef lint
131#ifndef LEAKTEST
132#ifndef safemalloc
133char *safemalloc _((MEM_SIZE));
134char *saferealloc _((char *, MEM_SIZE));
135void safefree _((char *));
136#endif
137#ifndef MSDOS
138#define New(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n) * sizeof(t))))
139#define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n) * sizeof(t))))
140#define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n) * sizeof(t)))), \
141 memzero((char*)(v), (n) * sizeof(t))
142#define Renew(v,n,t) (v = (t*)saferealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
143#define Renewc(v,n,t,c) (v = (c*)saferealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
144#else
145#define New(x,v,n,t) (v = (t*)safemalloc(((unsigned long)(n) * sizeof(t))))
146#define Newc(x,v,n,t,c) (v = (c*)safemalloc(((unsigned long)(n) * sizeof(t))))
147#define Newz(x,v,n,t) (v = (t*)safemalloc(((unsigned long)(n) * sizeof(t)))), \
148 memzero((char*)(v), (n) * sizeof(t))
149#define Renew(v,n,t) (v = (t*)saferealloc((char*)(v),((unsigned long)(n)*sizeof(t))))
150#define Renewc(v,n,t,c) (v = (c*)saferealloc((char*)(v),((unsigned long)(n)*sizeof(t))))
151#endif /* MSDOS */
152#define Safefree(d) safefree((char*)d)
153#define NEWSV(x,len) newSV(len)
154#else /* LEAKTEST */
155char *safexmalloc();
156char *safexrealloc();
157void safexfree();
158#define New(x,v,n,t) (v = (t*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t))))
159#define Newc(x,v,n,t,c) (v = (c*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t))))
160#define Newz(x,v,n,t) (v = (t*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t)))), \
161 memzero((char*)(v), (n) * sizeof(t))
162#define Renew(v,n,t) (v = (t*)safexrealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
163#define Renewc(v,n,t,c) (v = (c*)safexrealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
164#define Safefree(d) safexfree((char*)d)
165#define NEWSV(x,len) newSV(x,len)
166#define MAXXCOUNT 1200
167long xcount[MAXXCOUNT];
168long lastxcount[MAXXCOUNT];
169#endif /* LEAKTEST */
170#define Move(s,d,n,t) (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
171#define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
172#define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t))
173#else /* lint */
174#define New(x,v,n,s) (v = Null(s *))
175#define Newc(x,v,n,s,c) (v = Null(s *))
176#define Newz(x,v,n,s) (v = Null(s *))
177#define Renew(v,n,s) (v = Null(s *))
178#define Move(s,d,n,t)
179#define Copy(s,d,n,t)
180#define Zero(d,n,t)
181#define Safefree(d) d = d
182#endif /* lint */
183
184#ifdef USE_STRUCT_COPY
185#define StructCopy(s,d,t) *((t*)(d)) = *((t*)(s))
186#else
187#define StructCopy(s,d,t) Copy(s,d,1,t)
188#endif