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
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 #ifndef CPL_BASE_H_INCLUDED
00104 #define CPL_BASE_H_INCLUDED
00105
00113
00114
00115
00116
00117 #ifdef macintosh
00118 # define macos_pre10
00119 #endif
00120
00121
00122
00123
00124 #if defined(_WIN32) && !defined(WIN32)
00125 # define WIN32
00126 #endif
00127
00128 #if defined(_WINDOWS) && !defined(WIN32)
00129 # define WIN32
00130 #endif
00131
00132 #include "cpl_config.h"
00133
00134
00135
00136
00137
00138
00139 #ifdef unix
00140 # undef WIN32
00141 #endif
00142
00143 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
00144 # define _LARGEFILE64_SOURCE 1
00145 #endif
00146
00147
00148
00149
00150
00151 #include <stdio.h>
00152 #include <stdlib.h>
00153 #include <math.h>
00154 #include <stdarg.h>
00155 #include <string.h>
00156 #include <ctype.h>
00157 #include <errno.h>
00158 #include <time.h>
00159
00160 #ifdef HAVE_LOCALE_H
00161 # include <locale.h>
00162 #endif
00163
00164 #ifdef _AIX
00165 # include <strings.h>
00166 #endif
00167
00168 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
00169 # define DBMALLOC
00170 # include <dbmalloc.h>
00171 #endif
00172
00173 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
00174 # define USE_DMALLOC
00175 # include <dmalloc.h>
00176 #endif
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 #if UINT_MAX == 65535
00187 typedef long GInt32;
00188 typedef unsigned long GUInt32;
00189 #else
00190 typedef int GInt32;
00191 typedef unsigned int GUInt32;
00192 #endif
00193
00194 typedef short GInt16;
00195 typedef unsigned short GUInt16;
00196 typedef unsigned char GByte;
00197 typedef int GBool;
00198
00199
00200
00201
00202
00203 #if defined(WIN32) && defined(_MSC_VER)
00204
00205 #define VSI_LARGE_API_SUPPORTED
00206 typedef __int64 GIntBig;
00207 typedef unsigned __int64 GUIntBig;
00208
00209 #elif HAVE_LONG_LONG
00210
00211 typedef long long GIntBig;
00212 typedef unsigned long long GUIntBig;
00213
00214 #else
00215
00216 typedef long GIntBig;
00217 typedef unsigned long GUIntBig;
00218
00219 #endif
00220
00221
00222
00223
00224 #ifdef __cplusplus
00225 # define CPL_C_START extern "C" {
00226 # define CPL_C_END }
00227 #else
00228 # define CPL_C_START
00229 # define CPL_C_END
00230 #endif
00231
00232 #ifndef CPL_DLL
00233 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
00234 # define CPL_DLL __declspec(dllexport)
00235 #else
00236 # define CPL_DLL
00237 #endif
00238 #endif
00239
00240
00241 #ifndef NULL
00242 # define NULL 0
00243 #endif
00244
00245 #ifndef FALSE
00246 # define FALSE 0
00247 #endif
00248
00249 #ifndef TRUE
00250 # define TRUE 1
00251 #endif
00252
00253 #ifndef MAX
00254 # define MIN(a,b) ((a<b) ? a : b)
00255 # define MAX(a,b) ((a>b) ? a : b)
00256 #endif
00257
00258 #ifndef ABS
00259 # define ABS(x) ((x<0) ? (-1*(x)) : x)
00260 #endif
00261
00262 #ifndef EQUAL
00263 #ifdef WIN32
00264 # define EQUALN(a,b,n) (strnicmp(a,b,n)==0)
00265 # define EQUAL(a,b) (stricmp(a,b)==0)
00266 #else
00267 # define EQUALN(a,b,n) (strncasecmp(a,b,n)==0)
00268 # define EQUAL(a,b) (strcasecmp(a,b)==0)
00269 #endif
00270 #endif
00271
00272 #ifdef macos_pre10
00273 int strcasecmp(char * str1, char * str2);
00274 int strncasecmp(char * str1, char * str2, int len);
00275 char * strdup (char *instr);
00276 #endif
00277
00278
00279
00280
00281
00282
00283
00284
00285 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
00286 # define CPL_MSB
00287 #endif
00288
00289 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
00290 #define CPL_LSB
00291 #endif
00292
00293 #if defined(CPL_LSB)
00294 # define CPL_IS_LSB 1
00295 #else
00296 # define CPL_IS_LSB 0
00297 #endif
00298
00299
00300
00301
00302
00303 #define CPL_SWAP16(x) \
00304 ((GUInt16)( \
00305 (((GUInt16)(x) & 0x00ffU) << 8) | \
00306 (((GUInt16)(x) & 0xff00U) >> 8) ))
00307
00308 #define CPL_SWAP16PTR(x) \
00309 { \
00310 GByte byTemp, *_pabyDataT = (GByte *) (x); \
00311 \
00312 byTemp = _pabyDataT[0]; \
00313 _pabyDataT[0] = _pabyDataT[1]; \
00314 _pabyDataT[1] = byTemp; \
00315 }
00316
00317 #define CPL_SWAP32(x) \
00318 ((GUInt32)( \
00319 (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
00320 (((GUInt32)(x) & (GUInt32)0x0000ff00UL) << 8) | \
00321 (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >> 8) | \
00322 (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
00323
00324 #define CPL_SWAP32PTR(x) \
00325 { \
00326 GByte byTemp, *_pabyDataT = (GByte *) (x); \
00327 \
00328 byTemp = _pabyDataT[0]; \
00329 _pabyDataT[0] = _pabyDataT[3]; \
00330 _pabyDataT[3] = byTemp; \
00331 byTemp = _pabyDataT[1]; \
00332 _pabyDataT[1] = _pabyDataT[2]; \
00333 _pabyDataT[2] = byTemp; \
00334 }
00335
00336 #define CPL_SWAP64PTR(x) \
00337 { \
00338 GByte byTemp, *_pabyDataT = (GByte *) (x); \
00339 \
00340 byTemp = _pabyDataT[0]; \
00341 _pabyDataT[0] = _pabyDataT[7]; \
00342 _pabyDataT[7] = byTemp; \
00343 byTemp = _pabyDataT[1]; \
00344 _pabyDataT[1] = _pabyDataT[6]; \
00345 _pabyDataT[6] = byTemp; \
00346 byTemp = _pabyDataT[2]; \
00347 _pabyDataT[2] = _pabyDataT[5]; \
00348 _pabyDataT[5] = byTemp; \
00349 byTemp = _pabyDataT[3]; \
00350 _pabyDataT[3] = _pabyDataT[4]; \
00351 _pabyDataT[4] = byTemp; \
00352 }
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
00372
00373 #ifdef CPL_MSB
00374 # define CPL_MSBWORD16(x) (x)
00375 # define CPL_LSBWORD16(x) CPL_SWAP16(x)
00376 # define CPL_MSBWORD32(x) (x)
00377 # define CPL_LSBWORD32(x) CPL_SWAP32(x)
00378 # define CPL_MSBPTR16(x)
00379 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
00380 # define CPL_MSBPTR32(x)
00381 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
00382 # define CPL_MSBPTR64(x)
00383 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
00384 #else
00385 # define CPL_LSBWORD16(x) (x)
00386 # define CPL_MSBWORD16(x) CPL_SWAP16(x)
00387 # define CPL_LSBWORD32(x) (x)
00388 # define CPL_MSBWORD32(x) CPL_SWAP32(x)
00389 # define CPL_LSBPTR16(x)
00390 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
00391 # define CPL_LSBPTR32(x)
00392 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
00393 # define CPL_LSBPTR64(x)
00394 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
00395 #endif
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405 #ifndef DISABLE_CVSID
00406 # define CPL_CVSID(string) static char cpl_cvsid[] = string; \
00407 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
00408 #else
00409 # define CPL_CVSID(string)
00410 #endif
00411
00412 #endif