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