1 /*
2     SDL - Simple DirectMedia Layer
3     Copyright (C) 1997-2012 Sam Lantinga
4 
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9 
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Lesser General Public License for more details.
14 
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 
19     Sam Lantinga
20     slouken@libsdl.org
21 */
22 
23 /** @file SDL_stdinc.h
24  *  This is a general header that includes C language support
25  */
26 
27 #ifndef _LRSDL_stdinc_h
28 #define _LRSDL_stdinc_h
29 
30 #include "LRSDL_config.h"
31 
32 
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
35 #endif
36 #ifdef HAVE_STDIO_H
37 #include <stdio.h>
38 #endif
39 #if defined(STDC_HEADERS)
40 # include <stdlib.h>
41 # include <stddef.h>
42 # include <stdarg.h>
43 #else
44 # if defined(HAVE_STDLIB_H)
45 #  include <stdlib.h>
46 # elif defined(HAVE_MALLOC_H)
47 #  include <malloc.h>
48 # endif
49 # if defined(HAVE_STDDEF_H)
50 #  include <stddef.h>
51 # endif
52 # if defined(HAVE_STDARG_H)
53 #  include <stdarg.h>
54 # endif
55 #endif
56 #ifdef HAVE_STRING_H
57 # if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
58 #  include <memory.h>
59 # endif
60 # include <string.h>
61 #endif
62 #include <stdint.h>
63 #ifdef HAVE_CTYPE_H
64 # include <ctype.h>
65 #endif
66 
67 /** The number of elements in an array */
68 #define SDL_arraysize(array)	(sizeof(array)/sizeof(array[0]))
69 #define SDL_TABLESIZE(table)	SDL_arraysize(table)
70 
71 /* Use proper C++ casts when compiled as C++ to be compatible with the option
72  -Wold-style-cast of GCC (and -Werror=old-style-cast in GCC 4.2 and above. */
73 #ifdef __cplusplus
74 #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression)
75 #define SDL_static_cast(type, expression) static_cast<type>(expression)
76 #else
77 #define SDL_reinterpret_cast(type, expression) ((type)(expression))
78 #define SDL_static_cast(type, expression) ((type)(expression))
79 #endif
80 
81 /** @name Basic data types */
82 /*@{*/
83 typedef enum {
84 	LRSDL_FALSE = 0,
85 	LRSDL_TRUE  = 1
86 } LRSDL_bool;
87 
88 typedef int8_t		Sint8;
89 typedef uint8_t		Uint8;
90 typedef int16_t		Sint16;
91 typedef uint16_t	Uint16;
92 typedef int32_t		Sint32;
93 typedef uint32_t	Uint32;
94 
95 #ifdef SDL_HAS_64BIT_TYPE
96 typedef int64_t		Sint64;
97 #ifndef SYMBIAN32_GCCE
98 typedef uint64_t	Uint64;
99 #endif
100 #else
101 /* This is really just a hack to prevent the compiler from complaining */
102 typedef struct {
103 	Uint32 hi;
104 	Uint32 lo;
105 } Uint64, Sint64;
106 #endif
107 
108 #include "begin_code.h"
109 /* Set up for C function definitions, even when using C++ */
110 #ifdef __cplusplus
111 extern "C" {
112 #endif
113 
114 #define SDL_malloc	malloc
115 #define SDL_calloc	calloc
116 #define SDL_realloc	realloc
117 #define SDL_free	free
118 
119 #if defined(HAVE_ALLOCA) && !defined(alloca)
120 # if defined(__GNUC__)
121 #  define alloca __builtin_alloca
122 # elif defined(_MSC_VER)
123 #  include <malloc.h>
124 #  define alloca _alloca
125 # elif defined(__WATCOMC__)
126 #  include <malloc.h>
127 # elif defined(__BORLANDC__)
128 #  include <malloc.h>
129 # elif defined(__DMC__)
130 #  include <stdlib.h>
131 # elif defined(__AIX__)
132   #pragma alloca
133 # elif defined(__MRC__)
134    void *alloca (unsigned);
135 # else
136    char *alloca ();
137 # endif
138 #endif
139 #ifdef HAVE_ALLOCA
140 #define SDL_stack_alloc(type, count)    (type*)alloca(sizeof(type)*(count))
141 #define SDL_stack_free(data)
142 #else
143 #define SDL_stack_alloc(type, count)    (type*)SDL_malloc(sizeof(type)*(count))
144 #define SDL_stack_free(data)            SDL_free(data)
145 #endif
146 
147 #ifdef HAVE_GETENV
148 #define SDL_getenv	getenv
149 #else
150 extern DECLSPEC char * SDLCALL SDL_getenv(const char *name);
151 #endif
152 
153 #ifdef HAVE_PUTENV
154 #define SDL_putenv	putenv
155 #else
156 extern DECLSPEC int SDLCALL SDL_putenv(const char *variable);
157 #endif
158 
159 #ifdef HAVE_QSORT
160 #define SDL_qsort	qsort
161 #else
162 extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size,
163            int (*compare)(const void *, const void *));
164 #endif
165 
166 #ifdef HAVE_ABS
167 #define SDL_abs		abs
168 #else
169 #define SDL_abs(X)	((X) < 0 ? -(X) : (X))
170 #endif
171 
172 #define SDL_min(x, y)	(((x) < (y)) ? (x) : (y))
173 #define SDL_max(x, y)	(((x) > (y)) ? (x) : (y))
174 
175 #ifdef HAVE_CTYPE_H
176 #define SDL_isdigit(X)  isdigit(X)
177 #define SDL_isspace(X)  isspace(X)
178 #define SDL_toupper(X)  toupper(X)
179 #define SDL_tolower(X)  tolower(X)
180 #else
181 #define SDL_isdigit(X)  (((X) >= '0') && ((X) <= '9'))
182 #define SDL_isspace(X)  (((X) == ' ') || ((X) == '\t') || ((X) == '\r') || ((X) == '\n'))
183 #define SDL_toupper(X)  (((X) >= 'a') && ((X) <= 'z') ? ('A'+((X)-'a')) : (X))
184 #define SDL_tolower(X)  (((X) >= 'A') && ((X) <= 'Z') ? ('a'+((X)-'A')) : (X))
185 #endif
186 
187 #define SDL_memset      memset
188 
189 #ifndef SDL_memset4
190 #define SDL_memset4(dst, val, len)		\
191 do {						\
192 	unsigned _count = (len);		\
193 	unsigned _n = (_count + 3) / 4;		\
194 	Uint32 *_p = SDL_static_cast(Uint32 *, dst);	\
195 	Uint32 _val = (val);			\
196 	if (len == 0) break;			\
197         switch (_count % 4) {			\
198         case 0: do {    *_p++ = _val;		\
199         case 3:         *_p++ = _val;		\
200         case 2:         *_p++ = _val;		\
201         case 1:         *_p++ = _val;		\
202 		} while ( --_n );		\
203 	}					\
204 } while(0)
205 #endif
206 
207 #define SDL_memcpy      memcpy
208 
209 #ifndef SDL_memcpy4
210 #define SDL_memcpy4(dst, src, len)	SDL_memcpy(dst, src, (len) << 2)
211 #endif
212 
213 #ifndef SDL_revcpy
214 extern DECLSPEC void * SDLCALL SDL_revcpy(void *dst, const void *src, size_t len);
215 #endif
216 
217 #define SDL_memmove     memmove
218 
219 #define SDL_memcmp      memcmp
220 
221 #define SDL_strlen      strlen
222 
223 #ifdef HAVE_STRLCAT
224 #define SDL_strlcat    strlcat
225 #else
226 extern DECLSPEC size_t SDLCALL SDL_strlcat(char *dst, const char *src, size_t maxlen);
227 #endif
228 
229 #ifdef HAVE_STRDUP
230 #define SDL_strdup     strdup
231 #else
232 extern DECLSPEC char * SDLCALL SDL_strdup(const char *string);
233 #endif
234 
235 #ifdef HAVE__STRREV
236 #define SDL_strrev      _strrev
237 #else
238 extern DECLSPEC char * SDLCALL SDL_strrev(char *string);
239 #endif
240 
241 #ifdef HAVE__STRUPR
242 #define SDL_strupr      _strupr
243 #else
244 extern DECLSPEC char * SDLCALL SDL_strupr(char *string);
245 #endif
246 
247 #ifdef HAVE__STRLWR
248 #define SDL_strlwr      _strlwr
249 #else
250 extern DECLSPEC char * SDLCALL SDL_strlwr(char *string);
251 #endif
252 
253 #ifdef HAVE_STRCHR
254 #define SDL_strchr      strchr
255 #elif defined(HAVE_INDEX)
256 #define SDL_strchr      index
257 #else
258 extern DECLSPEC char * SDLCALL SDL_strchr(const char *string, int c);
259 #endif
260 
261 #ifdef HAVE_STRRCHR
262 #define SDL_strrchr     strrchr
263 #elif defined(HAVE_RINDEX)
264 #define SDL_strrchr     rindex
265 #else
266 extern DECLSPEC char * SDLCALL SDL_strrchr(const char *string, int c);
267 #endif
268 
269 #ifdef HAVE_STRSTR
270 #define SDL_strstr      strstr
271 #else
272 extern DECLSPEC char * SDLCALL SDL_strstr(const char *haystack, const char *needle);
273 #endif
274 
275 #ifdef HAVE_ITOA
276 #define SDL_itoa        itoa
277 #else
278 #define SDL_itoa(value, string, radix)	SDL_ltoa((long)value, string, radix)
279 #endif
280 
281 #ifdef HAVE__LTOA
282 #define SDL_ltoa        _ltoa
283 #else
284 extern DECLSPEC char * SDLCALL SDL_ltoa(long value, char *string, int radix);
285 #endif
286 
287 #ifdef HAVE__UITOA
288 #define SDL_uitoa       _uitoa
289 #else
290 #define SDL_uitoa(value, string, radix)	SDL_ultoa((long)value, string, radix)
291 #endif
292 
293 #ifdef HAVE__ULTOA
294 #define SDL_ultoa       _ultoa
295 #else
296 extern DECLSPEC char * SDLCALL SDL_ultoa(unsigned long value, char *string, int radix);
297 #endif
298 
299 #ifdef HAVE_STRTOL
300 #define SDL_strtol      strtol
301 #else
302 extern DECLSPEC long SDLCALL SDL_strtol(const char *string, char **endp, int base);
303 #endif
304 
305 #ifdef HAVE_STRTOUL
306 #define SDL_strtoul      strtoul
307 #else
308 extern DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *string, char **endp, int base);
309 #endif
310 
311 #ifdef SDL_HAS_64BIT_TYPE
312 
313 #ifdef HAVE__I64TOA
314 #define SDL_lltoa       _i64toa
315 #else
316 extern DECLSPEC char* SDLCALL SDL_lltoa(Sint64 value, char *string, int radix);
317 #endif
318 
319 #ifdef HAVE__UI64TOA
320 #define SDL_ulltoa      _ui64toa
321 #else
322 extern DECLSPEC char* SDLCALL SDL_ulltoa(Uint64 value, char *string, int radix);
323 #endif
324 
325 #ifdef HAVE_STRTOLL
326 #define SDL_strtoll     strtoll
327 #else
328 extern DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *string, char **endp, int base);
329 #endif
330 
331 #ifdef HAVE_STRTOULL
332 #define SDL_strtoull     strtoull
333 #else
334 extern DECLSPEC Uint64 SDLCALL SDL_strtoull(const char *string, char **endp, int base);
335 #endif
336 
337 #endif /* SDL_HAS_64BIT_TYPE */
338 
339 #ifdef HAVE_STRTOD
340 #define SDL_strtod      strtod
341 #else
342 extern DECLSPEC double SDLCALL SDL_strtod(const char *string, char **endp);
343 #endif
344 
345 #ifdef HAVE_ATOI
346 #define SDL_atoi        atoi
347 #else
348 #define SDL_atoi(X)     SDL_strtol(X, NULL, 0)
349 #endif
350 
351 #ifdef HAVE_ATOF
352 #define SDL_atof        atof
353 #else
354 #define SDL_atof(X)     SDL_strtod(X, NULL)
355 #endif
356 
357 #ifdef HAVE_STRCMP
358 #define SDL_strcmp      strcmp
359 #else
360 extern DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
361 #endif
362 
363 #ifdef HAVE_STRNCMP
364 #define SDL_strncmp     strncmp
365 #else
366 extern DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
367 #endif
368 
369 #ifdef HAVE_STRCASECMP
370 #define SDL_strcasecmp  strcasecmp
371 #elif defined(HAVE__STRICMP)
372 #define SDL_strcasecmp  _stricmp
373 #else
374 extern DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
375 #endif
376 
377 #ifdef HAVE_STRNCASECMP
378 #define SDL_strncasecmp strncasecmp
379 #elif defined(HAVE__STRNICMP)
380 #define SDL_strncasecmp _strnicmp
381 #else
382 extern DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen);
383 #endif
384 
385 #ifdef HAVE_SSCANF
386 #define SDL_sscanf      sscanf
387 #else
388 extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, const char *fmt, ...);
389 #endif
390 
391 #if defined(HAVE_SNPRINTF) && !defined(_WIN32)
392 #define SDL_snprintf    snprintf
393 #elif defined(HAVE_SNPRINTF) && defined(_WIN32)
394 #define SDL_snprintf _snprintf
395 #else
396 extern DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...);
397 #endif
398 
399 #ifdef HAVE_VSNPRINTF
400 #define SDL_vsnprintf   vsnprintf
401 #else
402 extern DECLSPEC int SDLCALL SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap);
403 #endif
404 
405 /* Ends C function definitions when using C++ */
406 #ifdef __cplusplus
407 }
408 #endif
409 #include "close_code.h"
410 
411 #endif /* _SDL_stdinc_h */
412