1 /**
2  * \file h-basic.h
3  * \brief The lowest level header
4  *
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
6  *
7  * This software may be copied and distributed for educational, research,
8  * and not for profit purposes provided that this copyright and statement
9  * are included in all such copies.  Other copyrights may also apply.
10  */
11 
12 #ifndef INCLUDED_H_BASIC_H
13 #define INCLUDED_H_BASIC_H
14 
15 /**
16  * Include autoconf autodetections, otherwise try to autodetect ourselves
17  */
18 #ifdef HAVE_CONFIG_H
19 
20 # include "autoconf.h"
21 
22 #else
23 
24 /* Necessary? */
25 #ifdef NDS
26 # include <fat.h>
27 # include <unistd.h>
28 # include <reent.h>
29 # include <sys/iosupport.h>
30 # include <errno.h>
31 #endif
32 
33 /**
34  * Using C99, assume we have stdint and stdbool
35  */
36 # if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) \
37   || (defined(_MSC_VER) && _MSC_VER >= 1600L)
38 #  define HAVE_STDINT_H
39 # endif
40 
41 # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
42 #  define HAVE_STDbool_H
43 # endif
44 
45 /**
46  * Everyone except RISC OS has fcntl.h and sys/stat.h
47  */
48 #define HAVE_FCNTL_H
49 #define HAVE_STAT
50 
51 #endif /* HAVE_CONFIG_H */
52 
53 /**
54  * Extract the "WINDOWS" flag from the compiler
55  */
56 # if defined(_Windows) || defined(__WINDOWS__) || \
57      defined(__WIN32__) || defined(WIN32) || \
58      defined(__WINNT__) || defined(__NT__)
59 #  ifndef WINDOWS
60 #   define WINDOWS
61 #  endif
62 # endif
63 
64 /**
65  * Define UNIX if our OS is UNIXy
66  */
67 #if !defined(WINDOWS) && !defined(GAMEBOY) && !defined(NDS)
68 # define UNIX
69 
70 # ifndef HAVE_DIRENT_H
71 #  define HAVE_DIRENT_H
72 # endif
73 #endif
74 
75 /**
76  * Define SETGID if we are running as a central install on a multiuser
77  * system that has setgid support.
78  */
79 /* #define SETGID */
80 
81 
82 /**
83  * Every system seems to use its own symbol as a path separator.
84  *
85  * Default to the standard Unix slash, but attempt to change this
86  * for various other systems.  Note that any system that uses the
87  * "period" as a separator (i.e. RISCOS) will have to pretend that
88  * it uses the slash, and do its own mapping of period <-> slash.
89  *
90  * It is most definitely wrong to have such things here.  Platform-specific
91  * code should handle shifting Angband filenames to platform ones. XXX
92  */
93 #undef PATH_SEP
94 #define PATH_SEP "/"
95 #define PATH_SEPC '/'
96 
97 #ifdef WINDOWS
98 # undef PATH_SEP
99 # undef PATH_SEPC
100 # define PATH_SEP "\\"
101 # define PATH_SEPC '\\'
102 #endif
103 
104 
105 /**
106  * ------------------------------------------------------------------------
107  * Include the library header files
108  * ------------------------------------------------------------------------ */
109 
110 
111 /* Use various POSIX functions if available */
112 #undef _GNU_SOURCE
113 #define _GNU_SOURCE
114 
115 /** ANSI C headers **/
116 #include <ctype.h>
117 #include <stdbool.h>
118 #include <stdint.h>
119 #include <errno.h>
120 #include <limits.h>
121 #include <assert.h>
122 
123 #include <stdarg.h>
124 #include <stdio.h>
125 #include <stdlib.h>
126 #include <stddef.h>
127 #include <string.h>
128 #include <time.h>
129 
130 #include <wchar.h>
131 #include <wctype.h>
132 
133 /** POSIX headers **/
134 #ifdef UNIX
135 # include <pwd.h>
136 # include <sys/stat.h>
137 # include <unistd.h>
138 #endif
139 
140 
141 
142 /**
143  * ------------------------------------------------------------------------
144  * Define the basic game types
145  * ------------------------------------------------------------------------ */
146 
147 
148 /**
149  * errr is an error code
150  *
151  * A "byte" is an unsigned byte of memory.
152  * s16b/u16b are exactly 2 bytes (where possible)
153  * s32b/u32b are exactly 4 bytes (where possible)
154  */
155 
156 typedef int errr;
157 
158 /* Use guaranteed-size types */
159 typedef uint8_t byte;
160 
161 typedef uint16_t u16b;
162 typedef int16_t s16b;
163 
164 typedef uint32_t u32b;
165 typedef int32_t s32b;
166 
167 typedef uint64_t u64b;
168 typedef int64_t s64b;
169 
170 
171 /** Debugging macros ***/
172 
173 #define DSTRINGIFY(x) #x
174 #define DSTRING(x)    DSTRINGIFY(x)
175 #define DHERE         __FILE__ ":" DSTRING(__LINE__) ": "
176 
177 
178 /**
179  * ------------------------------------------------------------------------
180  * Basic math macros
181  * ------------------------------------------------------------------------ */
182 
183 
184 #undef MIN
185 #undef MAX
186 #undef ABS
187 #undef SGN
188 #undef CMP
189 
190 #define MIN(a,b)	(((a) > (b)) ? (b)  : (a))
191 #define MAX(a,b)	(((a) < (b)) ? (b)  : (a))
192 #define ABS(a)		(((a) < 0)   ? (-(a)) : (a))
193 #define SGN(a)		(((a) < 0)   ? (-1) : ((a) != 0))
194 #define CMP(a,b) ((a) < (b) ? -1 : ((b) < (a) ? 1 : 0))
195 
196 
197 /**
198  * ------------------------------------------------------------------------
199  * Useful fairly generic macros
200  * ------------------------------------------------------------------------ */
201 
202 
203 /**
204  * Given an array, determine how many elements are in it.
205  */
206 #define N_ELEMENTS(a) (sizeof(a) / sizeof((a)[0]))
207 
208 /**
209  * ------------------------------------------------------------------------
210  * Some hackish character manipulation
211  * ------------------------------------------------------------------------ */
212 
213 
214 /**
215  * Note that all "index" values must be "lowercase letters", while
216  * all "digits" must be "digits".
217  */
218 #define A2I(X)	((X) - 'a')
219 #define I2A(X)	((X) + 'a')
220 #define D2I(X)	((X) - '0')
221 #define I2D(X)	((X) + '0')
222 
223 #endif /* INCLUDED_H_BASIC_H */
224