root/trunk/src/h-basic.h

Revision 164, 5.5 kB (checked in by takkaria, 3 years ago)

Make a start at fixing #153.

Line 
1 /*
2  * File: h-basic.h
3  *
4  * The most basic "include" file.
5  */
6
7 #ifndef INCLUDED_H_BASIC_H
8 #define INCLUDED_H_BASIC_H
9
10 #ifdef HAVE_CONFIG_H
11 #include "autoconf.h"
12 #endif /* HAVE_CONFIG_H */
13
14
15 /*** Autodetect platform ***/
16
17 /*
18  * Extract the "RISCOS" flag from the compiler
19  */
20 #ifdef __riscos
21 # ifndef RISCOS
22 define RISCOS
23 # endif
24 #endif
25
26
27 /*
28  * Extract the "WINDOWS" flag from the compiler
29  */
30 #if defined(_Windows) || defined(__WINDOWS__) || \
31     defined(__WIN32__) || defined(WIN32) || \
32     defined(__WINNT__) || defined(__NT__)
33 # ifndef WINDOWS
34 define WINDOWS
35 # endif
36 #endif
37
38
39 /*
40  * OPTION: set "SET_UID" if the machine is a "multi-user" machine.
41  *
42  * This option is used to verify the use of "uids" and "gids" for
43  * various "Unix" calls, and of "pids" for getting a random seed,
44  * and of the "umask()" call for various reasons, and to guess if
45  * the "kill()" function is available, and for permission to use
46  * functions to extract user names and expand "tildes" in filenames.
47  * It is also used for "locking" and "unlocking" the score file.
48  * Basically, SET_UID should *only* be set for "Unix" machines.
49  */
50 #if !defined(MACH_O_CARBON) && !defined(WINDOWS) && \
51     !defined(RISCOS) && !defined(GAMEBOY)
52 # define SET_UID
53 #endif
54
55
56 /*
57  * Every system seems to use its own symbol as a path separator.
58  *
59  * Default to the standard Unix slash, but attempt to change this
60  * for various other systems.  Note that any system that uses the
61  * "period" as a separator (i.e. RISCOS) will have to pretend that
62  * it uses the slash, and do its own mapping of period <-> slash.
63  *
64  * It is most definitely wrong to have such things here.  Platform-specific
65  * code should handle shifting Angband filenames to platform ones. XXX
66  */
67 #undef PATH_SEP
68 #define PATH_SEP "/"
69
70 #ifdef WINDOWS
71 # undef PATH_SEP
72 # define PATH_SEP "\\"
73 #endif
74
75
76 /*
77  * Mac support
78  */
79 #ifdef MACH_O_CARBON
80
81 /* OS X uses filetypes when creating files. */
82 # define FILE_TYPE_TEXT 'TEXT'
83 # define FILE_TYPE_DATA 'DATA'
84 # define FILE_TYPE_SAVE 'SAVE'
85 # define FILE_TYPE(X) (_ftype = (X))
86
87 #else
88
89 # define FILE_TYPE(X) ((void)0)
90
91 #endif
92
93
94
95 /*** Include the library header files ***/
96
97 /** ANSI C headers **/
98
99 #include <ctype.h>
100 #include <errno.h>
101 #include <limits.h>
102 #include <assert.h>
103
104 #include <stdarg.h>
105 #include <stdio.h>
106 #include <stdlib.h>
107 #include <string.h>
108 #include <time.h>
109
110 /** POSIX headers **/
111
112 #if defined(SET_UID) || defined(MACH_O_CARBON)
113 # include <pwd.h>
114 # include <sys/stat.h>
115 # include <unistd.h>
116 #endif
117
118 #ifdef SET_UID
119 # include <sys/types.h>
120 #endif
121
122 #if defined(__DJGPP__) || defined(__MWERKS__)
123 #include <unistd.h>
124 #endif
125
126 /** Other headers **/
127
128 #if defined(MACINTOSH) && defined(__MWERKS__)
129 # include <unix.h>
130 #endif
131
132 #if defined(WINDOWS) || defined(MSDOS) || defined(USE_EMX)
133 # include <io.h>
134 #endif
135
136
137
138 /*** Define the basic game types ***/
139
140 /*
141  * cptr is a shortcut type for "const char *".  XXX
142  * errr is an error code
143  *
144  * A "byte" is an unsigned byte of memory.
145  * s16b/u16b are exactly 2 bytes (where possible)
146  * s32b/u32b are exactly 4 bytes (where possible)
147  *
148  * We define a "bool" as a char.  We should really be able to use the C89 types
149  * where available. XXX
150  */
151
152 /* C++ defines its own bool type, so we hack around it */
153 #undef bool
154 #define bool bool_hack
155
156
157 typedef const char *cptr;
158 typedef int errr;
159
160
161 #if defined(HAVE_STDBOOL_H)
162
163   #include <stdbool.h>
164
165   #define TRUE  true
166   #define FALSE false
167
168 #else
169
170   typedef char bool;
171
172   #undef TRUE
173   #undef FALSE
174
175   #define TRUE   1
176   #define FALSE  0
177
178 #endif
179
180
181 /* C99/stdint.h provide guaranteed-size ints */
182 #if (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) || defined HAVE_STDINT_H
183
184   /* Use guaranteed-size types */
185   #include <stdint.h>
186
187   typedef uint8_t byte;
188
189   typedef uint16_t u16b;
190   typedef int16_t s16b;
191
192   typedef uint32_t u32b;
193   typedef int32_t s32b;
194
195 #define MAX_UCHAR               UINT8_MAX
196 #define MAX_SHORT               INT16_MAX
197
198 #else /* __STDC__ */
199
200   /* Try hacks instead (not guaranteed to work) */
201   typedef unsigned char byte;
202   typedef signed short s16b;
203   typedef unsigned short u16b;
204
205 #define MAX_UCHAR               UCHAR_MAX
206 #define MAX_SHORT               32767
207
208   /* Detect >32-bit longs */
209   #if (UINT_MAX == 0xFFFFFFFFUL) && (ULONG_MAX > 0xFFFFFFFFUL)
210     typedef signed int s32b;
211     typedef unsigned int u32b;
212   #else
213     typedef signed long s32b;
214     typedef unsigned long u32b;
215   #endif /* L64 */
216
217 #endif /* __STDC__ */
218
219
220
221 /*** Simple constants ***/
222
223 /* Define NULL */
224 #ifndef NULL
225 # define NULL ((void*)0)
226 #endif
227
228
229
230 /*** Basic math macros ***/
231
232 #undef MIN
233 #undef MAX
234 #undef ABS
235 #undef SGN
236
237 #define MIN(a,b)        (((a) > (b)) ? (b)  : (a))
238 #define MAX(a,b)        (((a) < (b)) ? (b)  : (a))
239 #define ABS(a)          (((a) < 0)   ? (-(a)) : (a))
240 #define SGN(a)          (((a) < 0)   ? (-1) : ((a) != 0))
241
242
243 /*** Useful array length macro ***/
244
245 /*
246  * Given an array, determine how many elements are in the array.
247  */
248 #define N_ELEMENTS(a) (sizeof(a) / sizeof((a)[0]))
249
250
251 /*** Some hackish character manipulation ***/
252
253 /*
254  * Note that all "index" values must be "lowercase letters", while
255  * all "digits" must be "digits".  Control characters can be made
256  * from any legal characters.  XXX XXX XXX
257  */
258 #define A2I(X)  ((X) - 'a')
259 #define I2A(X)  ((X) + 'a')
260 #define D2I(X)  ((X) - '0')
261 #define I2D(X)  ((X) + '0')
262 #define KTRL(X) ((X) & 0x1F)
263 #define UN_KTRL(X)      ((X) + 64)
264 #define ESCAPE  '\033'
265
266
267 /*
268  * System-independent definitions for the arrow keys.
269  */
270 #define ARROW_DOWN      '\x8A'
271 #define ARROW_LEFT      '\x8B'
272 #define ARROW_RIGHT     '\x8C'
273 #define ARROW_UP        '\x8D'
274
275 /* Analogous to isdigit() etc in ctypes */
276 #define isarrow(c)      ((c >= ARROW_DOWN) && (c <= ARROW_UP))
277
278
279 #endif
280
281
Note: See TracBrowser for help on using the browser.