1 /*
2     SDL - Simple DirectMedia Layer
3     Copyright (C) 1997-2006 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 /* This is a general header that includes C language support */
24 
25 #ifndef _SDL_stdinc_h
26 #define _SDL_stdinc_h
27 
28 #include "SDL_config.h"
29 
30 
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
34 #ifdef HAVE_STDIO_H
35 #include <stdio.h>
36 #endif
37 #if defined(STDC_HEADERS)
38 # include <stdlib.h>
39 # include <stddef.h>
40 # include <stdarg.h>
41 #else
42 # if defined(HAVE_STDLIB_H)
43 #  include <stdlib.h>
44 # elif defined(HAVE_MALLOC_H)
45 #  include <malloc.h>
46 # endif
47 # if defined(HAVE_STDDEF_H)
48 #  include <stddef.h>
49 # endif
50 # if defined(HAVE_STDARG_H)
51 #  include <stdarg.h>
52 # endif
53 #endif
54 #ifdef HAVE_STRING_H
55 # if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
56 #  include <memory.h>
57 # endif
58 # include <string.h>
59 #endif
60 #ifdef HAVE_STRINGS_H
61 # include <strings.h>
62 #endif
63 #if defined(HAVE_INTTYPES_H)
64 # include <inttypes.h>
65 #elif defined(HAVE_STDINT_H)
66 # include <stdint.h>
67 #endif
68 #ifdef HAVE_CTYPE_H
69 # include <ctype.h>
70 #endif
71 #ifdef HAVE_ICONV_H
72 # include <iconv.h>
73 #endif
74 
75 /* The number of elements in an array */
76 #define SDL_arraysize(array)	(sizeof(array)/sizeof(array[0]))
77 #define SDL_TABLESIZE(table)	SDL_arraysize(table)
78 
79 /* Basic data types */
80 typedef enum SDL_bool {
81 	SDL_FALSE = 0,
82 	SDL_TRUE  = 1
83 } SDL_bool;
84 
85 typedef int8_t		Sint8;
86 typedef uint8_t		Uint8;
87 typedef int16_t		Sint16;
88 typedef uint16_t	Uint16;
89 typedef int32_t		Sint32;
90 typedef uint32_t	Uint32;
91 
92 #ifdef SDL_HAS_64BIT_TYPE
93 typedef int64_t		Sint64;
94 #ifndef SYMBIAN32_GCCE
95 typedef uint64_t	Uint64;
96 #endif
97 #else
98 /* This is really just a hack to prevent the compiler from complaining */
99 typedef struct {
100 	Uint32 hi;
101 	Uint32 lo;
102 } Uint64, Sint64;
103 #endif
104 
105 /* Make sure the types really have the right sizes */
106 #define SDL_COMPILE_TIME_ASSERT(name, x)               \
107        typedef int SDL_dummy_ ## name[(x) * 2 - 1]
108 
109 SDL_COMPILE_TIME_ASSERT(uint8, sizeof(Uint8) == 1);
110 SDL_COMPILE_TIME_ASSERT(sint8, sizeof(Sint8) == 1);
111 SDL_COMPILE_TIME_ASSERT(uint16, sizeof(Uint16) == 2);
112 SDL_COMPILE_TIME_ASSERT(sint16, sizeof(Sint16) == 2);
113 SDL_COMPILE_TIME_ASSERT(uint32, sizeof(Uint32) == 4);
114 SDL_COMPILE_TIME_ASSERT(sint32, sizeof(Sint32) == 4);
115 SDL_COMPILE_TIME_ASSERT(uint64, sizeof(Uint64) == 8);
116 SDL_COMPILE_TIME_ASSERT(sint64, sizeof(Sint64) == 8);
117 
118 /* Check to make sure enums are the size of ints, for structure packing.
119    For both Watcom C/C++ and Borland C/C++ the compiler option that makes
120    enums having the size of an int must be enabled.
121    This is "-b" for Borland C/C++ and "-ei" for Watcom C/C++ (v11).
122 */
123 /* Enable enums always int in CodeWarrior (for MPW use "-enum int") */
124 #ifdef __MWERKS__
125 #pragma enumsalwaysint on
126 #endif
127 
128 typedef enum {
129 	DUMMY_ENUM_VALUE
130 } SDL_DUMMY_ENUM;
131 
132 #ifndef __NDS__
133 SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
134 #endif
135 
136 
137 #include "begin_code.h"
138 /* Set up for C function definitions, even when using C++ */
139 #ifdef __cplusplus
140 extern "C" {
141 #endif
142 
143 #ifdef HAVE_MALLOC
144 #define SDL_malloc	malloc
145 #else
146 extern DECLSPEC void * SDLCALL SDL_malloc(size_t size);
147 #endif
148 
149 #ifdef HAVE_CALLOC
150 #define SDL_calloc	calloc
151 #else
152 extern DECLSPEC void * SDLCALL SDL_calloc(size_t nmemb, size_t size);
153 #endif
154 
155 #ifdef HAVE_REALLOC
156 #define SDL_realloc	realloc
157 #else
158 extern DECLSPEC void * SDLCALL SDL_realloc(void *mem, size_t size);
159 #endif
160 
161 #ifdef HAVE_FREE
162 #define SDL_free	free
163 #else
164 extern DECLSPEC void SDLCALL SDL_free(void *mem);
165 #endif
166 
167 #if defined(HAVE_ALLOCA) && !defined(alloca)
168 # if defined(HAVE_ALLOCA_H)
169 #  include <alloca.h>
170 # elif defined(__GNUC__)
171 #  define alloca __builtin_alloca
172 # elif defined(_MSC_VER)
173 #  include <malloc.h>
174 #  define alloca _alloca
175 # elif defined(__WATCOMC__)
176 #  include <malloc.h>
177 # elif defined(__BORLANDC__)
178 #  include <malloc.h>
179 # elif defined(__DMC__)
180 #  include <stdlib.h>
181 # elif defined(__AIX__)
182   #pragma alloca
183 # elif defined(__MRC__)
184    void *alloca (unsigned);
185 # else
186    char *alloca ();
187 # endif
188 #endif
189 #ifdef HAVE_ALLOCA
190 #define SDL_stack_alloc(type, count)    (type*)alloca(sizeof(type)*(count))
191 #define SDL_stack_free(data)
192 #else
193 #define SDL_stack_alloc(type, count)    (type*)SDL_malloc(sizeof(type)*(count))
194 #define SDL_stack_free(data)            SDL_free(data)
195 #endif
196 
197 #ifdef HAVE_GETENV
198 #define SDL_getenv	getenv
199 #else
200 extern DECLSPEC char * SDLCALL SDL_getenv(const char *name);
201 #endif
202 
203 #ifdef HAVE_PUTENV
204 #define SDL_putenv	putenv
205 #else
206 extern DECLSPEC int SDLCALL SDL_putenv(const char *variable);
207 #endif
208 
209 #ifdef HAVE_QSORT
210 #define SDL_qsort	qsort
211 #else
212 extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size,
213            int (*compare)(const void *, const void *));
214 #endif
215 
216 #ifdef HAVE_ABS
217 #define SDL_abs		abs
218 #else
219 #define SDL_abs(X)	((X) < 0 ? -(X) : (X))
220 #endif
221 
222 #define SDL_min(x, y)	(((x) < (y)) ? (x) : (y))
223 #define SDL_max(x, y)	(((x) > (y)) ? (x) : (y))
224 
225 #ifdef HAVE_CTYPE_H
226 #define SDL_isdigit(X)  isdigit(X)
227 #define SDL_isspace(X)  isspace(X)
228 #define SDL_toupper(X)  toupper(X)
229 #define SDL_tolower(X)  tolower(X)
230 #else
231 #define SDL_isdigit(X)  (((X) >= '0') && ((X) <= '9'))
232 #define SDL_isspace(X)  (((X) == ' ') || ((X) == '\t') || ((X) == '\r') || ((X) == '\n'))
233 #define SDL_toupper(X)  (((X) >= 'a') && ((X) <= 'z') ? ('A'+((X)-'a')) : (X))
234 #define SDL_tolower(X)  (((X) >= 'A') && ((X) <= 'Z') ? ('a'+((X)-'A')) : (X))
235 #endif
236 
237 #ifdef HAVE_MEMSET
238 #define SDL_memset      memset
239 #else
240 extern DECLSPEC void * SDLCALL SDL_memset(void *dst, int c, size_t len);
241 #endif
242 
243 #if defined(__GNUC__) && defined(i386)
244 #define SDL_memset4(dst, val, len)				\
245 do {								\
246 	int u0, u1, u2;						\
247 	__asm__ __volatile__ (					\
248 		"cld\n\t"					\
249 		"rep ; stosl\n\t"				\
250 		: "=&D" (u0), "=&a" (u1), "=&c" (u2)		\
251 		: "0" (dst), "1" (val), "2" ((Uint32)(len))	\
252 		: "memory" );					\
253 } while(0)
254 #endif
255 #ifndef SDL_memset4
256 #define SDL_memset4(dst, val, len)		\
257 do {						\
258 	unsigned _count = (len);		\
259 	unsigned _n = (_count + 3) / 4;		\
260 	Uint32 *_p = (Uint32 *)(dst);		\
261 	Uint32 _val = (val);			\
262         switch (_count % 4) {			\
263         case 0: do {    *_p++ = _val;		\
264         case 3:         *_p++ = _val;		\
265         case 2:         *_p++ = _val;		\
266         case 1:         *_p++ = _val;		\
267 		} while ( --_n );		\
268 	}					\
269 } while(0)
270 #endif
271 
272 /* We can count on memcpy existing on Mac OS X and being well-tuned. */
273 #if defined(__MACH__) && defined(__APPLE__)
274 #define SDL_memcpy(dst, src, len) memcpy(dst, src, len)
275 #elif defined(__GNUC__) && defined(i386)
276 #define SDL_memcpy(dst, src, len)					  \
277 do {									  \
278 	int u0, u1, u2;						  	  \
279 	__asm__ __volatile__ (						  \
280 		"cld\n\t"						  \
281 		"rep ; movsl\n\t"					  \
282 		"testb $2,%b4\n\t"					  \
283 		"je 1f\n\t"						  \
284 		"movsw\n"						  \
285 		"1:\ttestb $1,%b4\n\t"					  \
286 		"je 2f\n\t"						  \
287 		"movsb\n"						  \
288 		"2:"							  \
289 		: "=&c" (u0), "=&D" (u1), "=&S" (u2)			  \
290 		: "0" ((unsigned)(len)/4), "q" (len), "1" (dst),"2" (src) \
291 		: "memory" );						  \
292 } while(0)
293 #endif
294 #ifndef SDL_memcpy
295 #ifdef HAVE_MEMCPY
296 #define SDL_memcpy      memcpy
297 #elif defined(HAVE_BCOPY)
298 #define SDL_memcpy(d, s, n)	bcopy((s), (d), (n))
299 #else
300 extern DECLSPEC void * SDLCALL SDL_memcpy(void *dst, const void *src, size_t len);
301 #endif
302 #endif
303 
304 /* We can count on memcpy existing on Mac OS X and being well-tuned. */
305 #if defined(__MACH__) && defined(__APPLE__)
306 #define SDL_memcpy4(dst, src, len) memcpy(dst, src, (len)*4)
307 #elif defined(__GNUC__) && defined(i386)
308 #define SDL_memcpy4(dst, src, len)				\
309 do {								\
310 	int ecx, edi, esi;					\
311 	__asm__ __volatile__ (					\
312 		"cld\n\t"					\
313 		"rep ; movsl"					\
314 		: "=&c" (ecx), "=&D" (edi), "=&S" (esi)		\
315 		: "0" ((unsigned)(len)), "1" (dst), "2" (src)	\
316 		: "memory" );					\
317 } while(0)
318 #endif
319 #ifndef SDL_memcpy4
320 #define SDL_memcpy4(dst, src, len)	SDL_memcpy(dst, src, (len) << 2)
321 #endif
322 
323 #if defined(__GNUC__) && defined(i386)
324 #define SDL_revcpy(dst, src, len)			\
325 do {							\
326 	int u0, u1, u2;					\
327 	char *dstp = (char *)(dst);			\
328 	char *srcp = (char *)(src);			\
329 	int n = (len);					\
330 	if ( n >= 4 ) {					\
331 	__asm__ __volatile__ (				\
332 		"std\n\t"				\
333 		"rep ; movsl\n\t"			\
334 		: "=&c" (u0), "=&D" (u1), "=&S" (u2)	\
335 		: "0" (n >> 2),				\
336 		  "1" (dstp+(n-4)), "2" (srcp+(n-4))	\
337 		: "memory" );				\
338 	}						\
339 	switch (n & 3) {				\
340 		case 3: dstp[2] = srcp[2];		\
341 		case 2: dstp[1] = srcp[1];		\
342 		case 1: dstp[0] = srcp[0];		\
343 			break;				\
344 		default:				\
345 			break;				\
346 	}						\
347 } while(0)
348 #endif
349 #ifndef SDL_revcpy
350 extern DECLSPEC void * SDLCALL SDL_revcpy(void *dst, const void *src, size_t len);
351 #endif
352 
353 #ifdef HAVE_MEMMOVE
354 #define SDL_memmove     memmove
355 #elif defined(HAVE_BCOPY)
356 #define SDL_memmove(d, s, n)	bcopy((s), (d), (n))
357 #else
358 #define SDL_memmove(dst, src, len)			\
359 do {							\
360 	if ( dst < src ) {				\
361 		SDL_memcpy(dst, src, len);		\
362 	} else {					\
363 		SDL_revcpy(dst, src, len);		\
364 	}						\
365 } while(0)
366 #endif
367 
368 #ifdef HAVE_MEMCMP
369 #define SDL_memcmp      memcmp
370 #else
371 extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
372 #endif
373 
374 #ifdef HAVE_STRLEN
375 #define SDL_strlen      strlen
376 #else
377 extern DECLSPEC size_t SDLCALL SDL_strlen(const char *string);
378 #endif
379 
380 #ifdef HAVE_STRLCPY
381 #define SDL_strlcpy     strlcpy
382 #else
383 extern DECLSPEC size_t SDLCALL SDL_strlcpy(char *dst, const char *src, size_t maxlen);
384 #endif
385 
386 #ifdef HAVE_STRLCAT
387 #define SDL_strlcat    strlcat
388 #else
389 extern DECLSPEC size_t SDLCALL SDL_strlcat(char *dst, const char *src, size_t maxlen);
390 #endif
391 
392 #ifdef HAVE_STRDUP
393 #define SDL_strdup     strdup
394 #else
395 extern DECLSPEC char * SDLCALL SDL_strdup(const char *string);
396 #endif
397 
398 #ifdef HAVE__STRREV
399 #define SDL_strrev      _strrev
400 #else
401 extern DECLSPEC char * SDLCALL SDL_strrev(char *string);
402 #endif
403 
404 #ifdef HAVE__STRUPR
405 #define SDL_strupr      _strupr
406 #else
407 extern DECLSPEC char * SDLCALL SDL_strupr(char *string);
408 #endif
409 
410 #ifdef HAVE__STRLWR
411 #define SDL_strlwr      _strlwr
412 #else
413 extern DECLSPEC char * SDLCALL SDL_strlwr(char *string);
414 #endif
415 
416 #ifdef HAVE_STRCHR
417 #define SDL_strchr      strchr
418 #elif defined(HAVE_INDEX)
419 #define SDL_strchr      index
420 #else
421 extern DECLSPEC char * SDLCALL SDL_strchr(const char *string, int c);
422 #endif
423 
424 #ifdef HAVE_STRRCHR
425 #define SDL_strrchr     strrchr
426 #elif defined(HAVE_RINDEX)
427 #define SDL_strrchr     rindex
428 #else
429 extern DECLSPEC char * SDLCALL SDL_strrchr(const char *string, int c);
430 #endif
431 
432 #ifdef HAVE_STRSTR
433 #define SDL_strstr      strstr
434 #else
435 extern DECLSPEC char * SDLCALL SDL_strstr(const char *haystack, const char *needle);
436 #endif
437 
438 #ifdef HAVE_ITOA
439 #define SDL_itoa        itoa
440 #else
441 #define SDL_itoa(value, string, radix)	SDL_ltoa((long)value, string, radix)
442 #endif
443 
444 #ifdef HAVE__LTOA
445 #define SDL_ltoa        _ltoa
446 #else
447 extern DECLSPEC char * SDLCALL SDL_ltoa(long value, char *string, int radix);
448 #endif
449 
450 #ifdef HAVE__UITOA
451 #define SDL_uitoa       _uitoa
452 #else
453 #define SDL_uitoa(value, string, radix)	SDL_ultoa((long)value, string, radix)
454 #endif
455 
456 #ifdef HAVE__ULTOA
457 #define SDL_ultoa       _ultoa
458 #else
459 extern DECLSPEC char * SDLCALL SDL_ultoa(unsigned long value, char *string, int radix);
460 #endif
461 
462 #ifdef HAVE_STRTOL
463 #define SDL_strtol      strtol
464 #else
465 extern DECLSPEC long SDLCALL SDL_strtol(const char *string, char **endp, int base);
466 #endif
467 
468 #ifdef HAVE_STRTOUL
469 #define SDL_strtoul      strtoul
470 #else
471 extern DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *string, char **endp, int base);
472 #endif
473 
474 #ifdef SDL_HAS_64BIT_TYPE
475 
476 #ifdef HAVE__I64TOA
477 #define SDL_lltoa       _i64toa
478 #else
479 extern DECLSPEC char* SDLCALL SDL_lltoa(Sint64 value, char *string, int radix);
480 #endif
481 
482 #ifdef HAVE__UI64TOA
483 #define SDL_ulltoa      _ui64toa
484 #else
485 extern DECLSPEC char* SDLCALL SDL_ulltoa(Uint64 value, char *string, int radix);
486 #endif
487 
488 #ifdef HAVE_STRTOLL
489 #define SDL_strtoll     strtoll
490 #else
491 extern DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *string, char **endp, int base);
492 #endif
493 
494 #ifdef HAVE_STRTOULL
495 #define SDL_strtoull     strtoull
496 #else
497 extern DECLSPEC Uint64 SDLCALL SDL_strtoull(const char *string, char **endp, int base);
498 #endif
499 
500 #endif /* SDL_HAS_64BIT_TYPE */
501 
502 #ifdef HAVE_STRTOD
503 #define SDL_strtod      strtod
504 #else
505 extern DECLSPEC double SDLCALL SDL_strtod(const char *string, char **endp);
506 #endif
507 
508 #ifdef HAVE_ATOI
509 #define SDL_atoi        atoi
510 #else
511 #define SDL_atoi(X)     SDL_strtol(X, NULL, 0)
512 #endif
513 
514 #ifdef HAVE_ATOF
515 #define SDL_atof        atof
516 #else
517 #define SDL_atof(X)     SDL_strtod(X, NULL)
518 #endif
519 
520 #ifdef HAVE_STRCMP
521 #define SDL_strcmp      strcmp
522 #else
523 extern DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
524 #endif
525 
526 #ifdef HAVE_STRNCMP
527 #define SDL_strncmp     strncmp
528 #else
529 extern DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
530 #endif
531 
532 #ifdef HAVE_STRCASECMP
533 #define SDL_strcasecmp  strcasecmp
534 #elif defined(HAVE__STRICMP)
535 #define SDL_strcasecmp  _stricmp
536 #else
537 extern DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
538 #endif
539 
540 #ifdef HAVE_STRNCASECMP
541 #define SDL_strncasecmp strncasecmp
542 #elif defined(HAVE__STRNICMP)
543 #define SDL_strncasecmp _strnicmp
544 #else
545 extern DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen);
546 #endif
547 
548 #ifdef HAVE_SSCANF
549 #define SDL_sscanf      sscanf
550 #else
551 extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, const char *fmt, ...);
552 #endif
553 
554 #ifdef HAVE_SNPRINTF
555 #define SDL_snprintf    snprintf
556 #else
557 extern DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...);
558 #endif
559 
560 #ifdef HAVE_VSNPRINTF
561 #define SDL_vsnprintf   vsnprintf
562 #else
563 extern DECLSPEC int SDLCALL SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap);
564 #endif
565 
566 /* The SDL implementation of iconv() returns these error codes */
567 #define SDL_ICONV_ERROR		(size_t)-1
568 #define SDL_ICONV_E2BIG		(size_t)-2
569 #define SDL_ICONV_EILSEQ	(size_t)-3
570 #define SDL_ICONV_EINVAL	(size_t)-4
571 
572 #ifdef HAVE_ICONV
573 #define SDL_iconv_t     iconv_t
574 #define SDL_iconv_open  iconv_open
575 #define SDL_iconv_close iconv_close
576 #else
577 typedef struct _SDL_iconv_t *SDL_iconv_t;
578 extern DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode, const char *fromcode);
579 extern DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
580 #endif
581 extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
582 /* This function converts a string between encodings in one pass, returning a
583    string that must be freed with SDL_free() or NULL on error.
584 */
585 extern DECLSPEC char * SDLCALL SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t inbytesleft);
586 #define SDL_iconv_utf8_locale(S)	SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1)
587 #define SDL_iconv_utf8_ucs2(S)		(Uint16 *)SDL_iconv_string("UCS-2", "UTF-8", S, SDL_strlen(S)+1)
588 #define SDL_iconv_utf8_ucs4(S)		(Uint32 *)SDL_iconv_string("UCS-4", "UTF-8", S, SDL_strlen(S)+1)
589 
590 /* Ends C function definitions when using C++ */
591 #ifdef __cplusplus
592 }
593 #endif
594 #include "close_code.h"
595 
596 #endif /* _SDL_stdinc_h */
597