include/SDL_string.h
author Sam Lantinga <slouken@libsdl.org>
Tue, 07 Feb 2006 18:36:54 +0000
changeset 1345 7f32b9bede06
parent 1341 d02b552e5304
child 1353 7ba544e2888d
permissions -rw-r--r--
Fixes for Visual C++ 6.0
     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 file contains portable string manipulation functions for SDL */
    24 
    25 #ifndef _SDL_string_h
    26 #define _SDL_string_h
    27 
    28 #include "SDL_config.h"
    29 
    30 #ifdef HAVE_STDIO_H
    31 #include <stdio.h>	/* For snprintf() and friends */
    32 #endif
    33 
    34 #ifdef HAVE_STRING_H
    35 #include <string.h>
    36 #endif
    37 
    38 #include "SDL_types.h"
    39 #include "SDL_stdarg.h"
    40 
    41 #include "begin_code.h"
    42 /* Set up for C function definitions, even when using C++ */
    43 #ifdef __cplusplus
    44 extern "C" {
    45 #endif
    46 
    47 #ifdef HAVE_MEMSET
    48 #define SDL_memset      memset
    49 #else
    50 extern DECLSPEC void * SDLCALL SDL_memset(void *dst, int c, size_t len);
    51 #endif
    52 
    53 #if defined(__GNUC__) && defined(i386)
    54 #define SDL_memset4(dst, val, len)				\
    55 do {								\
    56 	int u0, u1, u2;						\
    57 	__asm__ __volatile__ (					\
    58 		"cld\n\t"					\
    59 		"rep ; stosl\n\t"				\
    60 		: "=&D" (u0), "=&a" (u1), "=&c" (u2)		\
    61 		: "0" (dst), "1" (val), "2" ((Uint32)(len))	\
    62 		: "memory" );					\
    63 } while(0)
    64 #endif
    65 #ifndef SDL_memset4
    66 #define SDL_memset4(dst, val, len)		\
    67 do {						\
    68 	unsigned _count = (len);		\
    69 	unsigned _n = (_count + 3) / 4;		\
    70 	Uint32 *_p = (Uint32 *)(dst);		\
    71 	Uint32 _val = (val);			\
    72         switch (_count % 4) {			\
    73         case 0: do {    *_p++ = _val;		\
    74         case 3:         *_p++ = _val;		\
    75         case 2:         *_p++ = _val;		\
    76         case 1:         *_p++ = _val;		\
    77 		} while ( --_n );		\
    78 	}					\
    79 } while(0)
    80 #endif
    81 
    82 #if defined(__GNUC__) && defined(i386)
    83 #define SDL_memcpy(dst, src, len)					  \
    84 do {									  \
    85 	int u0, u1, u2;						  	  \
    86 	__asm__ __volatile__ (						  \
    87 		"cld\n\t"						  \
    88 		"rep ; movsl\n\t"					  \
    89 		"testb $2,%b4\n\t"					  \
    90 		"je 1f\n\t"						  \
    91 		"movsw\n"						  \
    92 		"1:\ttestb $1,%b4\n\t"					  \
    93 		"je 2f\n\t"						  \
    94 		"movsb\n"						  \
    95 		"2:"							  \
    96 		: "=&c" (u0), "=&D" (u1), "=&S" (u2)			  \
    97 		: "0" ((unsigned)(len)/4), "q" (len), "1" (dst),"2" (src) \
    98 		: "memory" );						  \
    99 } while(0)
   100 #endif
   101 #ifndef SDL_memcpy
   102 #ifdef HAVE_MEMCPY
   103 #define SDL_memcpy      memcpy
   104 #else
   105 extern DECLSPEC void * SDLCALL SDL_memcpy(void *dst, const void *src, size_t len);
   106 #endif
   107 #endif
   108 
   109 #if defined(__GNUC__) && defined(i386)
   110 #define SDL_memcpy4(dst, src, len)				\
   111 do {								\
   112 	int ecx, edi, esi;					\
   113 	__asm__ __volatile__ (					\
   114 		"cld\n\t"					\
   115 		"rep ; movsl"					\
   116 		: "=&c" (ecx), "=&D" (edi), "=&S" (esi)		\
   117 		: "0" ((unsigned)(len)), "1" (dst), "2" (src)	\
   118 		: "memory" );					\
   119 } while(0)
   120 #endif
   121 #ifndef SDL_memcpy4
   122 #define SDL_memcpy4(dst, src, len)	SDL_memcpy(dst, src, (len) << 2)
   123 #endif
   124 
   125 #if defined(__GNUC__) && defined(i386)
   126 #define SDL_revcpy(dst, src, len)			\
   127 do {							\
   128 	int u0, u1, u2;					\
   129 	char *dstp = (char *)(dst);			\
   130 	char *srcp = (char *)(src);			\
   131 	int n = (len);					\
   132 	if ( n >= 4 ) {					\
   133 	__asm__ __volatile__ (				\
   134 		"std\n\t"				\
   135 		"rep ; movsl\n\t"			\
   136 		: "=&c" (u0), "=&D" (u1), "=&S" (u2)	\
   137 		: "0" (n >> 2),				\
   138 		  "1" (dstp+(n-4)), "2" (srcp+(n-4))	\
   139 		: "memory" );				\
   140 	}						\
   141 	switch (n & 3) {				\
   142 		case 3: dstp[2] = srcp[2];		\
   143 		case 2: dstp[1] = srcp[1];		\
   144 		case 1: dstp[0] = srcp[0];		\
   145 			break;				\
   146 		default:				\
   147 			break;				\
   148 	}						\
   149 } while(0)
   150 #endif
   151 #ifndef SDL_revcpy
   152 extern DECLSPEC void * SDLCALL SDL_revcpy(void *dst, const void *src, size_t len);
   153 #endif
   154 
   155 #ifdef HAVE_MEMMOVE
   156 #define SDL_memmove     memmove
   157 #else
   158 #define SDL_memmove(dst, src, len)			\
   159 do {							\
   160 	if ( dst < src ) {				\
   161 		SDL_memcpy(dst, src, len);		\
   162 	} else {					\
   163 		SDL_revcpy(dst, src, len);		\
   164 	}						\
   165 } while(0)
   166 #endif
   167 
   168 #ifdef HAVE_MEMCMP
   169 #define SDL_memcmp      memcmp
   170 #else
   171 extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
   172 #endif
   173 
   174 #ifdef HAVE_STRLEN
   175 #define SDL_strlen      strlen
   176 #else
   177 extern DECLSPEC size_t SDLCALL SDL_strlen(const char *string);
   178 #endif
   179 
   180 #ifdef HAVE_STRCPY
   181 #define SDL_strcpy     strcpy
   182 #else
   183 extern DECLSPEC char * SDLCALL SDL_strcpy(char *dst, const char *src);
   184 #endif
   185 
   186 #ifdef HAVE_STRNCPY
   187 #define SDL_strncpy     strncpy
   188 #else
   189 extern DECLSPEC char * SDLCALL SDL_strncpy(char *dst, const char *src, size_t maxlen);
   190 #endif
   191 
   192 #ifdef HAVE_STRCAT
   193 #define SDL_strcat     strcat
   194 #else
   195 #define SDL_strcat(dst, src)    (SDL_strcpy(dst+SDL_strlen(dst), src), dst)
   196 #endif
   197 
   198 #ifdef HAVE_STRNCAT
   199 #define SDL_strncat    strncat
   200 #else
   201 #define SDL_strncat(dst, src, n) (SDL_strncpy(dst+SDL_strlen(dst), src, n), dst)
   202 #endif
   203 
   204 #ifdef HAVE_STRDUP
   205 #define SDL_strdup     strdup
   206 #else
   207 extern DECLSPEC char * SDLCALL SDL_strdup(const char *string);
   208 #endif
   209 
   210 #ifdef HAVE__STRREV
   211 #define SDL_strrev      _strrev
   212 #else
   213 extern DECLSPEC char * SDLCALL SDL_strrev(char *string);
   214 #endif
   215 
   216 #ifdef HAVE__STRUPR
   217 #define SDL_strupr      _strupr
   218 #else
   219 extern DECLSPEC char * SDLCALL SDL_strupr(char *string);
   220 #endif
   221 
   222 #ifdef HAVE__STRLWR
   223 #define SDL_strlwr      _strlwr
   224 #else
   225 extern DECLSPEC char * SDLCALL SDL_strlwr(char *string);
   226 #endif
   227 
   228 #ifdef HAVE_STRCHR
   229 #define SDL_strchr      strchr
   230 #else
   231 extern DECLSPEC char * SDLCALL SDL_strchr(const char *string, int c);
   232 #endif
   233 
   234 #ifdef HAVE_STRRCHR
   235 #define SDL_strrchr     strrchr
   236 #else
   237 extern DECLSPEC char * SDLCALL SDL_strrchr(const char *string, int c);
   238 #endif
   239 
   240 #ifdef HAVE_STRSTR
   241 #define SDL_strstr      strstr
   242 #else
   243 extern DECLSPEC char * SDLCALL SDL_strstr(const char *haystack, const char *needle);
   244 #endif
   245 
   246 #ifdef HAVE_ITOA
   247 #define SDL_itoa        itoa
   248 #else
   249 #define SDL_itoa(value, string, radix)	SDL_ltoa((long)value, string, radix)
   250 #endif
   251 
   252 #ifdef HAVE__LTOA
   253 #define SDL_ltoa        _ltoa
   254 #else
   255 extern DECLSPEC char * SDLCALL SDL_ltoa(long value, char *string, int radix);
   256 #endif
   257 
   258 #ifdef HAVE__UITOA
   259 #define SDL_uitoa       _uitoa
   260 #else
   261 #define SDL_uitoa(value, string, radix)	SDL_ultoa((long)value, string, radix)
   262 #endif
   263 
   264 #ifdef HAVE__ULTOA
   265 #define SDL_ultoa       _ultoa
   266 #else
   267 extern DECLSPEC char * SDLCALL SDL_ultoa(unsigned long value, char *string, int radix);
   268 #endif
   269 
   270 #ifdef HAVE_STRTOL
   271 #define SDL_strtol      strtol
   272 #else
   273 extern DECLSPEC long SDLCALL SDL_strtol(const char *string, char **endp, int base);
   274 #endif
   275 
   276 #ifdef SDL_HAS_64BIT_TYPE
   277 
   278 #ifdef HAVE__I64TOA
   279 #define SDL_lltoa       _i64toa
   280 #else
   281 extern DECLSPEC char* SDLCALL SDL_lltoa(Sint64 value, char *string, int radix);
   282 #endif
   283 
   284 #ifdef HAVE__UI64TOA
   285 #define SDL_ulltoa      _ui64toa
   286 #else
   287 extern DECLSPEC char* SDLCALL SDL_ulltoa(Uint64 value, char *string, int radix);
   288 #endif
   289 
   290 #ifdef HAVE_STRTOLL
   291 #define SDL_strtoll     strtoll
   292 #else
   293 extern DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *string, char **endp, int base);
   294 #endif
   295 
   296 #endif /* SDL_HAS_64BIT_TYPE */
   297 
   298 #ifdef HAVE_STRTOD
   299 #define SDL_strtod      strtod
   300 #else
   301 extern DECLSPEC double SDLCALL SDL_strtod(const char *string, char **endp);
   302 #endif
   303 
   304 #ifdef HAVE_ATOI
   305 #define SDL_atoi        atoi
   306 #else
   307 #define SDL_atoi(X)     SDL_strtol(X, NULL, 0)
   308 #endif
   309 
   310 #ifdef HAVE_ATOF
   311 #define SDL_atof        atof
   312 #else
   313 #define SDL_atof(X)     SDL_strtod(X, NULL)
   314 #endif
   315 
   316 #ifdef HAVE_STRCMP
   317 #define SDL_strcmp      strcmp
   318 #else
   319 extern DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
   320 #endif
   321 
   322 #ifdef HAVE_STRNCMP
   323 #define SDL_strncmp     strncmp
   324 #else
   325 extern DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
   326 #endif
   327 
   328 #if defined(HAVE_STRICMP) && !defined(HAVE_STRCASECMP)
   329 #define strcasecmp      stricmp
   330 #define HAVE_STRCASECMP
   331 #endif
   332 #ifdef HAVE_STRCASECMP
   333 #define SDL_strcasecmp  strcasecmp
   334 #else
   335 extern DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
   336 #endif
   337 
   338 #ifdef HAVE_SSCANF
   339 #define SDL_sscanf      sscanf
   340 #else
   341 extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, const char *fmt, ...);
   342 #endif
   343 
   344 #ifdef HAVE_SNPRINTF
   345 #define SDL_snprintf    snprintf
   346 #else
   347 extern DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...);
   348 #endif
   349 
   350 #ifdef HAVE_VSNPRINTF
   351 #define SDL_vsnprintf   vsnprintf
   352 #else
   353 extern DECLSPEC int SDLCALL SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap);
   354 #endif
   355 
   356 /* Ends C function definitions when using C++ */
   357 #ifdef __cplusplus
   358 }
   359 #endif
   360 #include "close_code.h"
   361 
   362 #endif /* _SDL_string_h */