00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef CPL_BASE_H_INCLUDED
00032 #define CPL_BASE_H_INCLUDED
00033
00034
00035 #if defined(_MSC_VER)
00036 # pragma warning(disable:4251 4275 4786)
00037 #endif
00038
00046
00047
00048
00049
00050 #ifdef macintosh
00051 # define macos_pre10
00052 #endif
00053
00054
00055
00056
00057 #if defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE)
00058 # define WIN32
00059 #endif
00060
00061 #if defined(_WINDOWS) && !defined(WIN32) && !defined(_WIN32_WCE)
00062 # define WIN32
00063 #endif
00064
00065
00066
00067
00068 #if defined(_WIN32_WCE)
00069 # define WIN32CE
00070 #endif
00071
00072
00073
00074
00075
00076 #ifdef _MSC_VER
00077 # ifndef _CRT_SECURE_NO_DEPRECATE
00078 # define _CRT_SECURE_NO_DEPRECATE
00079 # endif
00080 # ifndef _CRT_NONSTDC_NO_DEPRECATE
00081 # define _CRT_NONSTDC_NO_DEPRECATE
00082 # endif
00083 # ifdef MSVC_USE_VLD
00084 # include <vld.h>
00085 # endif
00086 #endif
00087
00088
00089 #include "cpl_config.h"
00090
00091
00092
00093
00094
00095
00096 #ifdef unix
00097 # undef WIN32
00098 # undef WIN32CE
00099 #endif
00100
00101 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
00102 # define _LARGEFILE64_SOURCE 1
00103 #endif
00104
00105
00106
00107
00108
00109 #include <stdio.h>
00110 #include <stdlib.h>
00111 #include <math.h>
00112 #include <stdarg.h>
00113 #include <string.h>
00114 #include <ctype.h>
00115 #include <limits.h>
00116
00117 #if !defined(WIN32CE)
00118 # include <time.h>
00119 #else
00120 # include <wce_time.h>
00121 # include <wce_errno.h>
00122 #endif
00123
00124
00125 #if defined(HAVE_ERRNO_H)
00126 # include <errno.h>
00127 #endif
00128
00129 #ifdef HAVE_LOCALE_H
00130 # include <locale.h>
00131 #endif
00132
00133 #ifdef HAVE_DIRECT_H
00134 # include <direct.h>
00135 #endif
00136
00137 #ifdef _AIX
00138 # include <strings.h>
00139 #endif
00140
00141 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
00142 # define DBMALLOC
00143 # include <dbmalloc.h>
00144 #endif
00145
00146 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
00147 # define USE_DMALLOC
00148 # include <dmalloc.h>
00149 #endif
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 #if UINT_MAX == 65535
00160 typedef long GInt32;
00161 typedef unsigned long GUInt32;
00162 #else
00163 typedef int GInt32;
00164 typedef unsigned int GUInt32;
00165 #endif
00166
00167 typedef short GInt16;
00168 typedef unsigned short GUInt16;
00169 typedef unsigned char GByte;
00170 typedef int GBool;
00171
00172
00173
00174
00175
00176 #if defined(WIN32) && defined(_MSC_VER)
00177
00178 #define VSI_LARGE_API_SUPPORTED
00179 typedef __int64 GIntBig;
00180 typedef unsigned __int64 GUIntBig;
00181
00182 #elif HAVE_LONG_LONG
00183
00184 typedef long long GIntBig;
00185 typedef unsigned long long GUIntBig;
00186
00187 #else
00188
00189 typedef long GIntBig;
00190 typedef unsigned long GUIntBig;
00191
00192 #endif
00193
00194
00195
00196
00197 #ifdef __cplusplus
00198 # define CPL_C_START extern "C" {
00199 # define CPL_C_END }
00200 #else
00201 # define CPL_C_START
00202 # define CPL_C_END
00203 #endif
00204
00205 #ifndef CPL_DLL
00206 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
00207 # define CPL_DLL __declspec(dllexport)
00208 #else
00209 # if defined(USE_GCC_VISIBILITY_FLAG)
00210 # define CPL_DLL __attribute__ ((visibility("default")))
00211 # else
00212 # define CPL_DLL
00213 # endif
00214 #endif
00215 #endif
00216
00217
00218 #ifdef CPL_OPTIONAL_APIS
00219 # define CPL_ODLL CPL_DLL
00220 #else
00221 # define CPL_ODLL
00222 #endif
00223
00224 #ifndef CPL_STDCALL
00225 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
00226 # define CPL_STDCALL __stdcall
00227 #else
00228 # define CPL_STDCALL
00229 #endif
00230 #endif
00231
00232 #ifdef _MSC_VER
00233 # define FORCE_CDECL __cdecl
00234 #else
00235 # define FORCE_CDECL
00236 #endif
00237
00238
00239 #if defined(__GNUC__)
00240 #define CPL_INLINE inline
00241 #else
00242 #define CPL_INLINE
00243 #endif
00244
00245 #ifndef NULL
00246 # define NULL 0
00247 #endif
00248
00249 #ifndef FALSE
00250 # define FALSE 0
00251 #endif
00252
00253 #ifndef TRUE
00254 # define TRUE 1
00255 #endif
00256
00257 #ifndef MAX
00258 # define MIN(a,b) ((a<b) ? a : b)
00259 # define MAX(a,b) ((a>b) ? a : b)
00260 #endif
00261
00262 #ifndef ABS
00263 # define ABS(x) ((x<0) ? (-1*(x)) : x)
00264 #endif
00265
00266
00267
00268
00269
00270
00271 #ifndef CPLIsEqual
00272 # define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
00273 #endif
00274
00275 #ifndef EQUAL
00276 #if defined(WIN32) || defined(WIN32CE)
00277 # define EQUALN(a,b,n) (strnicmp(a,b,n)==0)
00278 # define EQUAL(a,b) (stricmp(a,b)==0)
00279 #else
00280 # define EQUALN(a,b,n) (strncasecmp(a,b,n)==0)
00281 # define EQUAL(a,b) (strcasecmp(a,b)==0)
00282 #endif
00283 #endif
00284
00285 #ifdef macos_pre10
00286 int strcasecmp(char * str1, char * str2);
00287 int strncasecmp(char * str1, char * str2, int len);
00288 char * strdup (char *instr);
00289 #endif
00290
00291 #ifndef CPL_THREADLOCAL
00292 # define CPL_THREADLOCAL
00293 #endif
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 #ifdef _MSC_VER
00305 # include <float.h>
00306 # define CPLIsNan(x) _isnan(x)
00307 # define CPLIsInf(x) (!_isnan(x) && !_finite(x))
00308 # define CPLIsFinite(x) _finite(x)
00309 #else
00310 # define CPLIsNan(x) isnan(x)
00311 # ifdef isinf
00312 # define CPLIsInf(x) isinf(x)
00313 # define CPLIsFinite(x) (!isnan(x) && !isinf(x))
00314 # else
00315 # define CPLIsInf(x) FALSE
00316 # define CPLIsFinite(x) (!isnan(x))
00317 # endif
00318 #endif
00319
00320
00321
00322
00323
00324
00325
00326
00327 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
00328 # define CPL_MSB
00329 #endif
00330
00331 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
00332 #define CPL_LSB
00333 #endif
00334
00335 #if defined(CPL_LSB)
00336 # define CPL_IS_LSB 1
00337 #else
00338 # define CPL_IS_LSB 0
00339 #endif
00340
00341
00342
00343
00344
00345 #define CPL_SWAP16(x) \
00346 ((GUInt16)( \
00347 (((GUInt16)(x) & 0x00ffU) << 8) | \
00348 (((GUInt16)(x) & 0xff00U) >> 8) ))
00349
00350 #define CPL_SWAP16PTR(x) \
00351 { \
00352 GByte byTemp, *_pabyDataT = (GByte *) (x); \
00353 \
00354 byTemp = _pabyDataT[0]; \
00355 _pabyDataT[0] = _pabyDataT[1]; \
00356 _pabyDataT[1] = byTemp; \
00357 }
00358
00359 #define CPL_SWAP32(x) \
00360 ((GUInt32)( \
00361 (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
00362 (((GUInt32)(x) & (GUInt32)0x0000ff00UL) << 8) | \
00363 (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >> 8) | \
00364 (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
00365
00366 #define CPL_SWAP32PTR(x) \
00367 { \
00368 GByte byTemp, *_pabyDataT = (GByte *) (x); \
00369 \
00370 byTemp = _pabyDataT[0]; \
00371 _pabyDataT[0] = _pabyDataT[3]; \
00372 _pabyDataT[3] = byTemp; \
00373 byTemp = _pabyDataT[1]; \
00374 _pabyDataT[1] = _pabyDataT[2]; \
00375 _pabyDataT[2] = byTemp; \
00376 }
00377
00378 #define CPL_SWAP64PTR(x) \
00379 { \
00380 GByte byTemp, *_pabyDataT = (GByte *) (x); \
00381 \
00382 byTemp = _pabyDataT[0]; \
00383 _pabyDataT[0] = _pabyDataT[7]; \
00384 _pabyDataT[7] = byTemp; \
00385 byTemp = _pabyDataT[1]; \
00386 _pabyDataT[1] = _pabyDataT[6]; \
00387 _pabyDataT[6] = byTemp; \
00388 byTemp = _pabyDataT[2]; \
00389 _pabyDataT[2] = _pabyDataT[5]; \
00390 _pabyDataT[5] = byTemp; \
00391 byTemp = _pabyDataT[3]; \
00392 _pabyDataT[3] = _pabyDataT[4]; \
00393 _pabyDataT[4] = byTemp; \
00394 }
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
00414
00415 #ifdef CPL_MSB
00416 # define CPL_MSBWORD16(x) (x)
00417 # define CPL_LSBWORD16(x) CPL_SWAP16(x)
00418 # define CPL_MSBWORD32(x) (x)
00419 # define CPL_LSBWORD32(x) CPL_SWAP32(x)
00420 # define CPL_MSBPTR16(x)
00421 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
00422 # define CPL_MSBPTR32(x)
00423 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
00424 # define CPL_MSBPTR64(x)
00425 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
00426 #else
00427 # define CPL_LSBWORD16(x) (x)
00428 # define CPL_MSBWORD16(x) CPL_SWAP16(x)
00429 # define CPL_LSBWORD32(x) (x)
00430 # define CPL_MSBWORD32(x) CPL_SWAP32(x)
00431 # define CPL_LSBPTR16(x)
00432 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
00433 # define CPL_LSBPTR32(x)
00434 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
00435 # define CPL_LSBPTR64(x)
00436 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
00437 #endif
00438
00440 #define CPL_LSBINT16PTR(x) ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8))
00441
00443 #define CPL_LSBINT32PTR(x) ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8) | \
00444 ((*(GByte*)((x)+2)) << 16) | ((*(GByte*)((x)+1)) << 24))
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455 #ifndef DISABLE_CVSID
00456 # define CPL_CVSID(string) static char cpl_cvsid[] = string; \
00457 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
00458 #else
00459 # define CPL_CVSID(string)
00460 #endif
00461
00462 #if defined(__GNUC__) && __GNUC__ >= 3
00463 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00464 #else
00465 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
00466 #endif
00467
00468 #endif