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 4127 4100)
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 #include "cpl_config.h"
00089
00090
00091
00092
00093
00094
00095 #ifdef unix
00096 # undef WIN32
00097 # undef WIN32CE
00098 #endif
00099
00100 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
00101 # define _LARGEFILE64_SOURCE 1
00102 #endif
00103
00104
00105
00106
00107
00108 #include <stdio.h>
00109 #include <stdlib.h>
00110 #include <math.h>
00111 #include <stdarg.h>
00112 #include <string.h>
00113 #include <ctype.h>
00114 #include <limits.h>
00115
00116 #if !defined(WIN32CE)
00117 # include <time.h>
00118 #else
00119 # include <wce_time.h>
00120 # include <wce_errno.h>
00121 #endif
00122
00123
00124 #if defined(HAVE_ERRNO_H)
00125 # include <errno.h>
00126 #endif
00127
00128 #ifdef HAVE_LOCALE_H
00129 # include <locale.h>
00130 #endif
00131
00132 #ifdef HAVE_DIRECT_H
00133 # include <direct.h>
00134 #endif
00135
00136 #ifdef _AIX
00137 # include <strings.h>
00138 #endif
00139
00140 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
00141 # define DBMALLOC
00142 # include <dbmalloc.h>
00143 #endif
00144
00145 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
00146 # define USE_DMALLOC
00147 # include <dmalloc.h>
00148 #endif
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 #if UINT_MAX == 65535
00159 typedef long GInt32;
00160 typedef unsigned long GUInt32;
00161 #else
00162 typedef int GInt32;
00163 typedef unsigned int GUInt32;
00164 #endif
00165
00166 typedef short GInt16;
00167 typedef unsigned short GUInt16;
00168 typedef unsigned char GByte;
00169
00170
00171 #ifndef CPL_GBOOL_DEFINED
00172 #define CPL_GBOOL_DEFINED
00173 typedef int GBool;
00174 #endif
00175
00176
00177
00178
00179
00180 #if defined(WIN32) && defined(_MSC_VER)
00181
00182 #define VSI_LARGE_API_SUPPORTED
00183 typedef __int64 GIntBig;
00184 typedef unsigned __int64 GUIntBig;
00185
00186 #elif HAVE_LONG_LONG
00187
00188 typedef long long GIntBig;
00189 typedef unsigned long long GUIntBig;
00190
00191 #else
00192
00193 typedef long GIntBig;
00194 typedef unsigned long GUIntBig;
00195
00196 #endif
00197
00198 #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
00199 #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
00200 #elif HAVE_LONG_LONG
00201 #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
00202 #else
00203 #define CPL_FRMT_GB_WITHOUT_PREFIX "l"
00204 #endif
00205
00206 #define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
00207 #define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
00208
00209
00210 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
00211 #define GUINTBIG_TO_DOUBLE(x) (double)(GIntBig)(x)
00212 #else
00213 #define GUINTBIG_TO_DOUBLE(x) (double)(x)
00214 #endif
00215
00216
00217
00218
00219 #ifdef __cplusplus
00220 # define CPL_C_START extern "C" {
00221 # define CPL_C_END }
00222 #else
00223 # define CPL_C_START
00224 # define CPL_C_END
00225 #endif
00226
00227 #ifndef CPL_DLL
00228 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
00229 # define CPL_DLL __declspec(dllexport)
00230 #else
00231 # if defined(USE_GCC_VISIBILITY_FLAG)
00232 # define CPL_DLL __attribute__ ((visibility("default")))
00233 # else
00234 # define CPL_DLL
00235 # endif
00236 #endif
00237 #endif
00238
00239
00240 #ifdef CPL_OPTIONAL_APIS
00241 # define CPL_ODLL CPL_DLL
00242 #else
00243 # define CPL_ODLL
00244 #endif
00245
00246 #ifndef CPL_STDCALL
00247 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
00248 # define CPL_STDCALL __stdcall
00249 #else
00250 # define CPL_STDCALL
00251 #endif
00252 #endif
00253
00254 #ifdef _MSC_VER
00255 # define FORCE_CDECL __cdecl
00256 #else
00257 # define FORCE_CDECL
00258 #endif
00259
00260
00261 #if defined(__GNUC__) || defined(_MSC_VER)
00262 #define HAS_CPL_INLINE 1
00263 #define CPL_INLINE __inline
00264 #elif defined(__SUNPRO_CC)
00265 #define HAS_CPL_INLINE 1
00266 #define CPL_INLINE inline
00267 #else
00268 #define CPL_INLINE
00269 #endif
00270
00271 #ifndef NULL
00272 # define NULL 0
00273 #endif
00274
00275 #ifndef FALSE
00276 # define FALSE 0
00277 #endif
00278
00279 #ifndef TRUE
00280 # define TRUE 1
00281 #endif
00282
00283 #ifndef MAX
00284 # define MIN(a,b) ((a<b) ? a : b)
00285 # define MAX(a,b) ((a>b) ? a : b)
00286 #endif
00287
00288 #ifndef ABS
00289 # define ABS(x) ((x<0) ? (-1*(x)) : x)
00290 #endif
00291
00292
00293
00294
00295
00296
00297 #ifndef CPLIsEqual
00298 # define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
00299 #endif
00300
00301 #ifndef EQUAL
00302 #if defined(WIN32) || defined(WIN32CE)
00303 # define EQUALN(a,b,n) (strnicmp(a,b,n)==0)
00304 # define EQUAL(a,b) (stricmp(a,b)==0)
00305 #else
00306 # define EQUALN(a,b,n) (strncasecmp(a,b,n)==0)
00307 # define EQUAL(a,b) (strcasecmp(a,b)==0)
00308 #endif
00309 #endif
00310
00311 #ifdef macos_pre10
00312 int strcasecmp(char * str1, char * str2);
00313 int strncasecmp(char * str1, char * str2, int len);
00314 char * strdup (char *instr);
00315 #endif
00316
00317 #ifndef CPL_THREADLOCAL
00318 # define CPL_THREADLOCAL
00319 #endif
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 #ifdef _MSC_VER
00331 # include <float.h>
00332 # define CPLIsNan(x) _isnan(x)
00333 # define CPLIsInf(x) (!_isnan(x) && !_finite(x))
00334 # define CPLIsFinite(x) _finite(x)
00335 #else
00336 # define CPLIsNan(x) isnan(x)
00337 # ifdef isinf
00338 # define CPLIsInf(x) isinf(x)
00339 # define CPLIsFinite(x) (!isnan(x) && !isinf(x))
00340 # else
00341 # define CPLIsInf(x) FALSE
00342 # define CPLIsFinite(x) (!isnan(x))
00343 # endif
00344 #endif
00345
00346
00347
00348
00349
00350
00351
00352
00353 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
00354 # define CPL_MSB
00355 #endif
00356
00357 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
00358 #define CPL_LSB
00359 #endif
00360
00361 #if defined(CPL_LSB)
00362 # define CPL_IS_LSB 1
00363 #else
00364 # define CPL_IS_LSB 0
00365 #endif
00366
00367
00368
00369
00370
00371 #define CPL_SWAP16(x) \
00372 ((GUInt16)( \
00373 (((GUInt16)(x) & 0x00ffU) << 8) | \
00374 (((GUInt16)(x) & 0xff00U) >> 8) ))
00375
00376 #define CPL_SWAP16PTR(x) \
00377 { \
00378 GByte byTemp, *_pabyDataT = (GByte *) (x); \
00379 \
00380 byTemp = _pabyDataT[0]; \
00381 _pabyDataT[0] = _pabyDataT[1]; \
00382 _pabyDataT[1] = byTemp; \
00383 }
00384
00385 #define CPL_SWAP32(x) \
00386 ((GUInt32)( \
00387 (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
00388 (((GUInt32)(x) & (GUInt32)0x0000ff00UL) << 8) | \
00389 (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >> 8) | \
00390 (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
00391
00392 #define CPL_SWAP32PTR(x) \
00393 { \
00394 GByte byTemp, *_pabyDataT = (GByte *) (x); \
00395 \
00396 byTemp = _pabyDataT[0]; \
00397 _pabyDataT[0] = _pabyDataT[3]; \
00398 _pabyDataT[3] = byTemp; \
00399 byTemp = _pabyDataT[1]; \
00400 _pabyDataT[1] = _pabyDataT[2]; \
00401 _pabyDataT[2] = byTemp; \
00402 }
00403
00404 #define CPL_SWAP64PTR(x) \
00405 { \
00406 GByte byTemp, *_pabyDataT = (GByte *) (x); \
00407 \
00408 byTemp = _pabyDataT[0]; \
00409 _pabyDataT[0] = _pabyDataT[7]; \
00410 _pabyDataT[7] = byTemp; \
00411 byTemp = _pabyDataT[1]; \
00412 _pabyDataT[1] = _pabyDataT[6]; \
00413 _pabyDataT[6] = byTemp; \
00414 byTemp = _pabyDataT[2]; \
00415 _pabyDataT[2] = _pabyDataT[5]; \
00416 _pabyDataT[5] = byTemp; \
00417 byTemp = _pabyDataT[3]; \
00418 _pabyDataT[3] = _pabyDataT[4]; \
00419 _pabyDataT[4] = byTemp; \
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
00440
00441 #ifdef CPL_MSB
00442 # define CPL_MSBWORD16(x) (x)
00443 # define CPL_LSBWORD16(x) CPL_SWAP16(x)
00444 # define CPL_MSBWORD32(x) (x)
00445 # define CPL_LSBWORD32(x) CPL_SWAP32(x)
00446 # define CPL_MSBPTR16(x)
00447 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
00448 # define CPL_MSBPTR32(x)
00449 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
00450 # define CPL_MSBPTR64(x)
00451 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
00452 #else
00453 # define CPL_LSBWORD16(x) (x)
00454 # define CPL_MSBWORD16(x) CPL_SWAP16(x)
00455 # define CPL_LSBWORD32(x) (x)
00456 # define CPL_MSBWORD32(x) CPL_SWAP32(x)
00457 # define CPL_LSBPTR16(x)
00458 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
00459 # define CPL_LSBPTR32(x)
00460 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
00461 # define CPL_LSBPTR64(x)
00462 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
00463 #endif
00464
00466 #define CPL_LSBINT16PTR(x) ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8))
00467
00469 #define CPL_LSBINT32PTR(x) ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8) | \
00470 ((*(GByte*)((x)+2)) << 16) | ((*(GByte*)((x)+3)) << 24))
00471
00472
00473
00474 #ifndef UNREFERENCED_PARAM
00475 # ifdef UNREFERENCED_PARAMETER
00476 # define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
00477 # else
00478 # define UNREFERENCED_PARAM(param) ((void)param)
00479 # endif
00480 #endif
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490 #ifndef DISABLE_CVSID
00491 #if defined(__GNUC__) && __GNUC__ >= 4
00492 # define CPL_CVSID(string) static char cpl_cvsid[] __attribute__((used)) = string;
00493 #else
00494 # define CPL_CVSID(string) static char cpl_cvsid[] = string; \
00495 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
00496 #endif
00497 #else
00498 # define CPL_CVSID(string)
00499 #endif
00500
00501 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
00502 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00503 #else
00504 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
00505 #endif
00506
00507 #endif