1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 /**
23  *  \file SDL_stdinc.h
24  *
25  *  This is a general header that includes C language support.
26  */
27 
28 #ifndef _SDL_stdinc_h
29 #define _SDL_stdinc_h
30 
31 #include "SDL_config.h"
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 <stdlib.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 #ifdef HAVE_STRINGS_H
63 # include <strings.h>
64 #endif
65 #if defined(HAVE_INTTYPES_H)
66 # include <inttypes.h>
67 #elif defined(HAVE_STDINT_H)
68 # include <stdint.h>
69 #endif
70 #ifdef HAVE_CTYPE_H
71 # include <ctype.h>
72 #endif
73 #ifdef HAVE_MATH_H
74 # include <math.h>
75 #endif
76 #if defined(HAVE_ICONV) && defined(HAVE_ICONV_H)
77 # include <iconv.h>
78 #endif
79 
80 /**
81  *  The number of elements in an array.
82  */
83 #define SDL_arraysize(array)    (sizeof(array)/sizeof(array[0]))
84 #define SDL_TABLESIZE(table)    SDL_arraysize(table)
85 
86 /**
87  *  \name Cast operators
88  *
89  *  Use proper C++ casts when compiled as C++ to be compatible with the option
90  *  -Wold-style-cast of GCC (and -Werror=old-style-cast in GCC 4.2 and above).
91  */
92 /* @{ */
93 #ifdef __cplusplus
94 #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression)
95 #define SDL_static_cast(type, expression) static_cast<type>(expression)
96 #define SDL_const_cast(type, expression) const_cast<type>(expression)
97 #else
98 #define SDL_reinterpret_cast(type, expression) ((type)(expression))
99 #define SDL_static_cast(type, expression) ((type)(expression))
100 #define SDL_const_cast(type, expression) ((type)(expression))
101 #endif
102 /* @} *//* Cast operators */
103 
104 /* Define a four character code as a Uint32 */
105 #define SDL_FOURCC(A, B, C, D) \
106     ((SDL_static_cast(Uint32, SDL_static_cast(Uint8, (A))) << 0) | \
107      (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (B))) << 8) | \
108      (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (C))) << 16) | \
109      (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (D))) << 24))
110 
111 /**
112  *  \name Basic data types
113  */
114 /* @{ */
115 
116 typedef enum
117 {
118     SDL_FALSE = 0,
119     SDL_TRUE = 1
120 } SDL_bool;
121 
122 /**
123  * \brief A signed 8-bit integer type.
124  */
125 typedef int8_t Sint8;
126 /**
127  * \brief An unsigned 8-bit integer type.
128  */
129 typedef uint8_t Uint8;
130 /**
131  * \brief A signed 16-bit integer type.
132  */
133 typedef int16_t Sint16;
134 /**
135  * \brief An unsigned 16-bit integer type.
136  */
137 typedef uint16_t Uint16;
138 /**
139  * \brief A signed 32-bit integer type.
140  */
141 typedef int32_t Sint32;
142 /**
143  * \brief An unsigned 32-bit integer type.
144  */
145 typedef uint32_t Uint32;
146 
147 /**
148  * \brief A signed 64-bit integer type.
149  */
150 typedef int64_t Sint64;
151 /**
152  * \brief An unsigned 64-bit integer type.
153  */
154 typedef uint64_t Uint64;
155 
156 /* @} *//* Basic data types */
157 
158 
159 #define SDL_COMPILE_TIME_ASSERT(name, x)               \
160        typedef int SDL_dummy_ ## name[(x) * 2 - 1]
161 /** \cond */
162 #ifndef DOXYGEN_SHOULD_IGNORE_THIS
163 SDL_COMPILE_TIME_ASSERT(uint8, sizeof(Uint8) == 1);
164 SDL_COMPILE_TIME_ASSERT(sint8, sizeof(Sint8) == 1);
165 SDL_COMPILE_TIME_ASSERT(uint16, sizeof(Uint16) == 2);
166 SDL_COMPILE_TIME_ASSERT(sint16, sizeof(Sint16) == 2);
167 SDL_COMPILE_TIME_ASSERT(uint32, sizeof(Uint32) == 4);
168 SDL_COMPILE_TIME_ASSERT(sint32, sizeof(Sint32) == 4);
169 SDL_COMPILE_TIME_ASSERT(uint64, sizeof(Uint64) == 8);
170 SDL_COMPILE_TIME_ASSERT(sint64, sizeof(Sint64) == 8);
171 #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
172 /** \endcond */
173 
174 /* Check to make sure enums are the size of ints, for structure packing.
175    For both Watcom C/C++ and Borland C/C++ the compiler option that makes
176    enums having the size of an int must be enabled.
177    This is "-b" for Borland C/C++ and "-ei" for Watcom C/C++ (v11).
178 */
179 
180 /** \cond */
181 #ifndef DOXYGEN_SHOULD_IGNORE_THIS
182 #if !defined(__ANDROID__)
183    /* TODO: include/SDL_stdinc.h:174: error: size of array 'SDL_dummy_enum' is negative */
184 typedef enum
185 {
186     DUMMY_ENUM_VALUE
187 } SDL_DUMMY_ENUM;
188 
189 SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
190 #endif
191 #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
192 /** \endcond */
193 
194 #include "begin_code.h"
195 /* Set up for C function definitions, even when using C++ */
196 #ifdef __cplusplus
197 extern "C" {
198 #endif
199 
200 #if defined(HAVE_ALLOCA) && !defined(alloca)
201 # if defined(HAVE_ALLOCA_H)
202 #  include <alloca.h>
203 # elif defined(__GNUC__)
204 #  define alloca __builtin_alloca
205 # elif defined(_MSC_VER)
206 #  include <stdlib.h>
207 #  define alloca _alloca
208 # elif defined(__WATCOMC__)
209 #  include <stdlib.h>
210 # elif defined(__BORLANDC__)
211 #  include <stdlib.h>
212 # elif defined(__DMC__)
213 #  include <stdlib.h>
214 # elif defined(__AIX__)
215 #pragma alloca
216 # elif defined(__MRC__)
217 void *alloca(unsigned);
218 # else
219 char *alloca();
220 # endif
221 #endif
222 #ifdef HAVE_ALLOCA
223 #define SDL_stack_alloc(type, count)    (type*)alloca(sizeof(type)*(count))
224 #define SDL_stack_free(data)
225 #else
226 #define SDL_stack_alloc(type, count)    (type*)SDL_malloc(sizeof(type)*(count))
227 #define SDL_stack_free(data)            SDL_free(data)
228 #endif
229 
230 extern DECLSPEC void *SDLCALL SDL_malloc(size_t size);
231 extern DECLSPEC void *SDLCALL SDL_calloc(size_t nmemb, size_t size);
232 extern DECLSPEC void *SDLCALL SDL_realloc(void *mem, size_t size);
233 extern DECLSPEC void SDLCALL SDL_free(void *mem);
234 
235 extern DECLSPEC char *SDLCALL SDL_getenv(const char *name);
236 extern DECLSPEC int SDLCALL SDL_setenv(const char *name, const char *value, int overwrite);
237 
238 extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, int (*compare) (const void *, const void *));
239 
240 extern DECLSPEC int SDLCALL SDL_abs(int x);
241 
242 /* !!! FIXME: these have side effects. You probably shouldn't use them. */
243 /* !!! FIXME: Maybe we do forceinline functions of SDL_mini, SDL_minf, etc? */
244 #define SDL_min(x, y) (((x) < (y)) ? (x) : (y))
245 #define SDL_max(x, y) (((x) > (y)) ? (x) : (y))
246 
247 extern DECLSPEC int SDLCALL SDL_isdigit(int x);
248 extern DECLSPEC int SDLCALL SDL_isspace(int x);
249 extern DECLSPEC int SDLCALL SDL_toupper(int x);
250 extern DECLSPEC int SDLCALL SDL_tolower(int x);
251 
252 extern DECLSPEC void *SDLCALL SDL_memset(void *dst, int c, size_t len);
253 
254 #define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x)))
255 #define SDL_zerop(x) SDL_memset((x), 0, sizeof(*(x)))
256 
257 /* Note that memset() is a byte assignment and this is a 32-bit assignment, so they're not directly equivalent. */
SDL_memset4(void * dst,int val,size_t dwords)258 SDL_FORCE_INLINE void SDL_memset4(void *dst, int val, size_t dwords)
259 {
260 #if defined(__GNUC__) && defined(i386)
261     int u0, u1, u2;
262     __asm__ __volatile__ (
263         "cld \n\t"
264         "rep ; stosl \n\t"
265         : "=&D" (u0), "=&a" (u1), "=&c" (u2)
266         : "0" (dst), "1" (val), "2" (SDL_static_cast(Uint32, dwords))
267         : "memory"
268     );
269 #else
270     size_t _n = (dwords + 3) / 4;
271     Uint32 *_p = SDL_static_cast(Uint32 *, dst);
272     Uint32 _val = (val);
273     if (dwords == 0)
274         return;
275     switch (dwords % 4)
276     {
277         case 0: do {    *_p++ = _val;
278         case 3:         *_p++ = _val;
279         case 2:         *_p++ = _val;
280         case 1:         *_p++ = _val;
281         } while ( --_n );
282     }
283 #endif
284 }
285 
286 
287 extern DECLSPEC void *SDLCALL SDL_memcpy(void *dst, const void *src, size_t len);
288 
SDL_memcpy4(void * dst,const void * src,size_t dwords)289 SDL_FORCE_INLINE void *SDL_memcpy4(void *dst, const void *src, size_t dwords)
290 {
291     return SDL_memcpy(dst, src, dwords * 4);
292 }
293 
294 extern DECLSPEC void *SDLCALL SDL_memmove(void *dst, const void *src, size_t len);
295 extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
296 
297 extern DECLSPEC size_t SDLCALL SDL_wcslen(const wchar_t *wstr);
298 extern DECLSPEC size_t SDLCALL SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen);
299 extern DECLSPEC size_t SDLCALL SDL_wcslcat(wchar_t *dst, const wchar_t *src, size_t maxlen);
300 
301 extern DECLSPEC size_t SDLCALL SDL_strlen(const char *str);
302 extern DECLSPEC size_t SDLCALL SDL_strlcpy(char *dst, const char *src, size_t maxlen);
303 extern DECLSPEC size_t SDLCALL SDL_utf8strlcpy(char *dst, const char *src, size_t dst_bytes);
304 extern DECLSPEC size_t SDLCALL SDL_strlcat(char *dst, const char *src, size_t maxlen);
305 extern DECLSPEC char *SDLCALL SDL_strdup(const char *str);
306 extern DECLSPEC char *SDLCALL SDL_strrev(char *str);
307 extern DECLSPEC char *SDLCALL SDL_strupr(char *str);
308 extern DECLSPEC char *SDLCALL SDL_strlwr(char *str);
309 extern DECLSPEC char *SDLCALL SDL_strchr(const char *str, int c);
310 extern DECLSPEC char *SDLCALL SDL_strrchr(const char *str, int c);
311 extern DECLSPEC char *SDLCALL SDL_strstr(const char *haystack, const char *needle);
312 
313 extern DECLSPEC char *SDLCALL SDL_itoa(int value, char *str, int radix);
314 extern DECLSPEC char *SDLCALL SDL_uitoa(unsigned int value, char *str, int radix);
315 extern DECLSPEC char *SDLCALL SDL_ltoa(long value, char *str, int radix);
316 extern DECLSPEC char *SDLCALL SDL_ultoa(unsigned long value, char *str, int radix);
317 extern DECLSPEC char *SDLCALL SDL_lltoa(Sint64 value, char *str, int radix);
318 extern DECLSPEC char *SDLCALL SDL_ulltoa(Uint64 value, char *str, int radix);
319 
320 extern DECLSPEC int SDLCALL SDL_atoi(const char *str);
321 extern DECLSPEC double SDLCALL SDL_atof(const char *str);
322 extern DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int base);
323 extern DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base);
324 extern DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *str, char **endp, int base);
325 extern DECLSPEC Uint64 SDLCALL SDL_strtoull(const char *str, char **endp, int base);
326 extern DECLSPEC double SDLCALL SDL_strtod(const char *str, char **endp);
327 
328 extern DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
329 extern DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
330 extern DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
331 extern DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t len);
332 
333 extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, const char *fmt, ...);
334 extern DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...);
335 extern DECLSPEC int SDLCALL SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap);
336 
337 #ifndef HAVE_M_PI
338 #ifndef M_PI
339 #define M_PI    3.14159265358979323846264338327950288   /* pi */
340 #endif
341 #endif
342 
343 extern DECLSPEC double SDLCALL SDL_atan(double x);
344 extern DECLSPEC double SDLCALL SDL_atan2(double x, double y);
345 extern DECLSPEC double SDLCALL SDL_ceil(double x);
346 extern DECLSPEC double SDLCALL SDL_copysign(double x, double y);
347 extern DECLSPEC double SDLCALL SDL_cos(double x);
348 extern DECLSPEC float SDLCALL SDL_cosf(float x);
349 extern DECLSPEC double SDLCALL SDL_fabs(double x);
350 extern DECLSPEC double SDLCALL SDL_floor(double x);
351 extern DECLSPEC double SDLCALL SDL_log(double x);
352 extern DECLSPEC double SDLCALL SDL_pow(double x, double y);
353 extern DECLSPEC double SDLCALL SDL_scalbn(double x, int n);
354 extern DECLSPEC double SDLCALL SDL_sin(double x);
355 extern DECLSPEC float SDLCALL SDL_sinf(float x);
356 extern DECLSPEC double SDLCALL SDL_sqrt(double x);
357 
358 /* The SDL implementation of iconv() returns these error codes */
359 #define SDL_ICONV_ERROR     (size_t)-1
360 #define SDL_ICONV_E2BIG     (size_t)-2
361 #define SDL_ICONV_EILSEQ    (size_t)-3
362 #define SDL_ICONV_EINVAL    (size_t)-4
363 
364 /* SDL_iconv_* are now always real symbols/types, not macros or inlined. */
365 typedef struct _SDL_iconv_t *SDL_iconv_t;
366 extern DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode,
367                                                    const char *fromcode);
368 extern DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
369 extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf,
370                                          size_t * inbytesleft, char **outbuf,
371                                          size_t * outbytesleft);
372 /**
373  *  This function converts a string between encodings in one pass, returning a
374  *  string that must be freed with SDL_free() or NULL on error.
375  */
376 extern DECLSPEC char *SDLCALL SDL_iconv_string(const char *tocode,
377                                                const char *fromcode,
378                                                const char *inbuf,
379                                                size_t inbytesleft);
380 #define SDL_iconv_utf8_locale(S)    SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1)
381 #define SDL_iconv_utf8_ucs2(S)      (Uint16 *)SDL_iconv_string("UCS-2-INTERNAL", "UTF-8", S, SDL_strlen(S)+1)
382 #define SDL_iconv_utf8_ucs4(S)      (Uint32 *)SDL_iconv_string("UCS-4-INTERNAL", "UTF-8", S, SDL_strlen(S)+1)
383 
384 /* Ends C function definitions when using C++ */
385 #ifdef __cplusplus
386 }
387 #endif
388 #include "close_code.h"
389 
390 #endif /* _SDL_stdinc_h */
391 
392 /* vi: set ts=4 sw=4 expandtab: */
393