00001 /****************************************************************************** 00002 * $Id: cpl_port.h,v 1.40 2005/03/11 14:59:07 fwarmerdam Exp $ 00003 * 00004 * Project: CPL - Common Portability Library 00005 * Author: Frank Warmerdam, warmerdam@pobox.com 00006 * Purpose: 00007 * Include file providing low level portability services for CPL. This 00008 * should be the first include file for any CPL based code. It provides the 00009 * following: 00010 * 00011 * o Includes some standard system include files, such as stdio, and stdlib. 00012 * 00013 * o Defines CPL_C_START, CPL_C_END macros. 00014 * 00015 * o Ensures that some other standard macros like NULL are defined. 00016 * 00017 * o Defines some portability stuff like CPL_MSB, or CPL_LSB. 00018 * 00019 * o Ensures that core types such as GBool, GInt32, GInt16, GUInt32, 00020 * GUInt16, and GByte are defined. 00021 * 00022 ****************************************************************************** 00023 * Copyright (c) 1998, Frank Warmerdam 00024 * 00025 * Permission is hereby granted, free of charge, to any person obtaining a 00026 * copy of this software and associated documentation files (the "Software"), 00027 * to deal in the Software without restriction, including without limitation 00028 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00029 * and/or sell copies of the Software, and to permit persons to whom the 00030 * Software is furnished to do so, subject to the following conditions: 00031 * 00032 * The above copyright notice and this permission notice shall be included 00033 * in all copies or substantial portions of the Software. 00034 * 00035 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00036 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00037 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00038 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00039 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00040 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00041 * DEALINGS IN THE SOFTWARE. 00042 ****************************************************************************** 00043 * 00044 * $Log: cpl_port.h,v $ 00045 * Revision 1.40 2005/03/11 14:59:07 fwarmerdam 00046 * Default to assuming nothing is infinite if isinf() macro not defined. 00047 * Per http://bugzilla.remotesensing.org/show_bug.cgi?id=795 00048 * 00049 * Revision 1.39 2005/03/01 21:22:07 fwarmerdam 00050 * added CPLIsFinite() 00051 * 00052 * Revision 1.38 2005/03/01 20:44:38 fwarmerdam 00053 * Check for _MSC_VER instead of WIN32. 00054 * 00055 * Revision 1.37 2005/03/01 19:57:55 fwarmerdam 00056 * Added CPLIsNan and CPLIsInf macros. 00057 * 00058 * Revision 1.36 2004/01/06 21:42:32 warmerda 00059 * "Fix" for bug 455 related to CPL_IS_LSB macro. 00060 * 00061 * Revision 1.35 2003/12/11 03:16:02 warmerda 00062 * Added CPL_IS_LSB macro with value 0 (MSB) or 1 (LSB). 00063 * 00064 * Revision 1.34 2003/09/08 11:11:05 dron 00065 * Include time.h and locale.h. 00066 * 00067 * Revision 1.33 2003/05/12 14:52:56 warmerda 00068 * Use _MSC_VER to test for Microsoft Visual C++ compiler. 00069 * 00070 * Revision 1.32 2002/10/24 20:24:40 warmerda 00071 * avoid using variable names likely to conflict in macros 00072 * 00073 * Revision 1.31 2002/07/15 13:31:46 warmerda 00074 * CPL_SWAPDOUBLE had alignment problem, use CPL_SWAP64PTR 00075 * 00076 * Revision 1.30 2002/04/18 18:55:06 dron 00077 * added <ctype.h> at the list of standard include files 00078 * 00079 * Revision 1.29 2002/01/17 01:40:27 warmerda 00080 * added _LARGEFILE64_SOURCE support 00081 * 00082 * Revision 1.28 2001/08/30 21:20:49 warmerda 00083 * expand tabs 00084 * 00085 * Revision 1.27 2001/07/18 04:00:49 warmerda 00086 * added CPL_CVSID 00087 * 00088 * Revision 1.26 2001/06/21 21:17:26 warmerda 00089 * added irix 64bit file api support 00090 * 00091 * Revision 1.25 2001/04/30 18:18:38 warmerda 00092 * added macos support, standard header 00093 * 00094 * Revision 1.24 2001/01/19 21:16:41 warmerda 00095 * expanded tabs 00096 * 00097 * Revision 1.23 2001/01/13 04:06:39 warmerda 00098 * added strings.h on AIX as per patch from Dale. 00099 * 00100 * Revision 1.22 2001/01/03 16:18:07 warmerda 00101 * added GUIntBig 00102 * 00103 * Revision 1.21 2000/10/20 04:20:33 warmerda 00104 * added SWAP16PTR macros 00105 * 00106 * Revision 1.20 2000/10/13 17:32:42 warmerda 00107 * check for unix instead of IGNORE_WIN32 00108 * 00109 * Revision 1.19 2000/09/25 19:58:43 warmerda 00110 * ensure win32 doesn't get defined in Cygnus builds 00111 * 00112 * Revision 1.18 2000/07/20 13:15:03 warmerda 00113 * don't redeclare CPL_DLL 00114 */ 00115 00116 #ifndef CPL_BASE_H_INCLUDED 00117 #define CPL_BASE_H_INCLUDED 00118 00126 /* ==================================================================== */ 00127 /* We will use macos_pre10 to indicate compilation with MacOS */ 00128 /* versions before MacOS X. */ 00129 /* ==================================================================== */ 00130 #ifdef macintosh 00131 # define macos_pre10 00132 #endif 00133 00134 /* ==================================================================== */ 00135 /* We will use WIN32 as a standard windows define. */ 00136 /* ==================================================================== */ 00137 #if defined(_WIN32) && !defined(WIN32) 00138 # define WIN32 00139 #endif 00140 00141 #if defined(_WINDOWS) && !defined(WIN32) 00142 # define WIN32 00143 #endif 00144 00145 #include "cpl_config.h" 00146 00147 /* ==================================================================== */ 00148 /* This will disable most WIN32 stuff in a Cygnus build which */ 00149 /* defines unix to 1. */ 00150 /* ==================================================================== */ 00151 00152 #ifdef unix 00153 # undef WIN32 00154 #endif 00155 00156 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE) 00157 # define _LARGEFILE64_SOURCE 1 00158 #endif 00159 00160 /* ==================================================================== */ 00161 /* Standard include files. */ 00162 /* ==================================================================== */ 00163 00164 #include <stdio.h> 00165 #include <stdlib.h> 00166 #include <math.h> 00167 #include <stdarg.h> 00168 #include <string.h> 00169 #include <ctype.h> 00170 #include <errno.h> 00171 #include <time.h> 00172 00173 #ifdef HAVE_LOCALE_H 00174 # include <locale.h> 00175 #endif 00176 00177 #ifdef _AIX 00178 # include <strings.h> 00179 #endif 00180 00181 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG) 00182 # define DBMALLOC 00183 # include <dbmalloc.h> 00184 #endif 00185 00186 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H) 00187 # define USE_DMALLOC 00188 # include <dmalloc.h> 00189 #endif 00190 00191 /* ==================================================================== */ 00192 /* Base portability stuff ... this stuff may need to be */ 00193 /* modified for new platforms. */ 00194 /* ==================================================================== */ 00195 00196 /*--------------------------------------------------------------------- 00197 * types for 16 and 32 bits integers, etc... 00198 *--------------------------------------------------------------------*/ 00199 #if UINT_MAX == 65535 00200 typedef long GInt32; 00201 typedef unsigned long GUInt32; 00202 #else 00203 typedef int GInt32; 00204 typedef unsigned int GUInt32; 00205 #endif 00206 00207 typedef short GInt16; 00208 typedef unsigned short GUInt16; 00209 typedef unsigned char GByte; 00210 typedef int GBool; 00211 00212 /* -------------------------------------------------------------------- */ 00213 /* 64bit support */ 00214 /* -------------------------------------------------------------------- */ 00215 00216 #if defined(WIN32) && defined(_MSC_VER) 00217 00218 #define VSI_LARGE_API_SUPPORTED 00219 typedef __int64 GIntBig; 00220 typedef unsigned __int64 GUIntBig; 00221 00222 #elif HAVE_LONG_LONG 00223 00224 typedef long long GIntBig; 00225 typedef unsigned long long GUIntBig; 00226 00227 #else 00228 00229 typedef long GIntBig; 00230 typedef unsigned long GUIntBig; 00231 00232 #endif 00233 00234 /* ==================================================================== */ 00235 /* Other standard services. */ 00236 /* ==================================================================== */ 00237 #ifdef __cplusplus 00238 # define CPL_C_START extern "C" { 00239 # define CPL_C_END } 00240 #else 00241 # define CPL_C_START 00242 # define CPL_C_END 00243 #endif 00244 00245 #ifndef CPL_DLL 00246 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL) 00247 # define CPL_DLL __declspec(dllexport) 00248 #else 00249 # define CPL_DLL 00250 #endif 00251 #endif 00252 00253 00254 #ifndef NULL 00255 # define NULL 0 00256 #endif 00257 00258 #ifndef FALSE 00259 # define FALSE 0 00260 #endif 00261 00262 #ifndef TRUE 00263 # define TRUE 1 00264 #endif 00265 00266 #ifndef MAX 00267 # define MIN(a,b) ((a<b) ? a : b) 00268 # define MAX(a,b) ((a>b) ? a : b) 00269 #endif 00270 00271 #ifndef ABS 00272 # define ABS(x) ((x<0) ? (-1*(x)) : x) 00273 #endif 00274 00275 #ifndef EQUAL 00276 #ifdef WIN32 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 /* -------------------------------------------------------------------- */ 00292 /* Handle isnan() and isinf(). Note that isinf() and isnan() */ 00293 /* are supposed to be macros according to C99. Some systems */ 00294 /* (ie. Tru64) don't have isinf() at all, so if the macro is */ 00295 /* not defined we just assume nothing is infinite. This may */ 00296 /* mean we have no real CPLIsInf() on systems with an isinf() */ 00297 /* function but no corresponding macro, but I can live with */ 00298 /* that since it isn't that important a test. */ 00299 /* -------------------------------------------------------------------- */ 00300 #ifdef _MSC_VER 00301 # define CPLIsNan(x) _isnan(x) 00302 # define CPLIsInf(x) (!_isnan(x) && !_finite(x)) 00303 # define CPLIsFinite(x) _finite(x) 00304 #else 00305 # define CPLIsNan(x) isnan(x) 00306 # ifdef isinf 00307 # define CPLIsInf(x) isinf(x) 00308 # define CPLIsFinite(x) (!isnan(x) && !isinf(x)) 00309 # else 00310 # define CPLIsInf(x) FALSE 00311 # define CPLIsFinite(x) (!isnan(x)) 00312 # endif 00313 #endif 00314 00315 /*--------------------------------------------------------------------- 00316 * CPL_LSB and CPL_MSB 00317 * Only one of these 2 macros should be defined and specifies the byte 00318 * ordering for the current platform. 00319 * This should be defined in the Makefile, but if it is not then 00320 * the default is CPL_LSB (Intel ordering, LSB first). 00321 *--------------------------------------------------------------------*/ 00322 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB) 00323 # define CPL_MSB 00324 #endif 00325 00326 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) ) 00327 #define CPL_LSB 00328 #endif 00329 00330 #if defined(CPL_LSB) 00331 # define CPL_IS_LSB 1 00332 #else 00333 # define CPL_IS_LSB 0 00334 #endif 00335 00336 /*--------------------------------------------------------------------- 00337 * Little endian <==> big endian byte swap macros. 00338 *--------------------------------------------------------------------*/ 00339 00340 #define CPL_SWAP16(x) \ 00341 ((GUInt16)( \ 00342 (((GUInt16)(x) & 0x00ffU) << 8) | \ 00343 (((GUInt16)(x) & 0xff00U) >> 8) )) 00344 00345 #define CPL_SWAP16PTR(x) \ 00346 { \ 00347 GByte byTemp, *_pabyDataT = (GByte *) (x); \ 00348 \ 00349 byTemp = _pabyDataT[0]; \ 00350 _pabyDataT[0] = _pabyDataT[1]; \ 00351 _pabyDataT[1] = byTemp; \ 00352 } 00353 00354 #define CPL_SWAP32(x) \ 00355 ((GUInt32)( \ 00356 (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \ 00357 (((GUInt32)(x) & (GUInt32)0x0000ff00UL) << 8) | \ 00358 (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >> 8) | \ 00359 (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) )) 00360 00361 #define CPL_SWAP32PTR(x) \ 00362 { \ 00363 GByte byTemp, *_pabyDataT = (GByte *) (x); \ 00364 \ 00365 byTemp = _pabyDataT[0]; \ 00366 _pabyDataT[0] = _pabyDataT[3]; \ 00367 _pabyDataT[3] = byTemp; \ 00368 byTemp = _pabyDataT[1]; \ 00369 _pabyDataT[1] = _pabyDataT[2]; \ 00370 _pabyDataT[2] = byTemp; \ 00371 } 00372 00373 #define CPL_SWAP64PTR(x) \ 00374 { \ 00375 GByte byTemp, *_pabyDataT = (GByte *) (x); \ 00376 \ 00377 byTemp = _pabyDataT[0]; \ 00378 _pabyDataT[0] = _pabyDataT[7]; \ 00379 _pabyDataT[7] = byTemp; \ 00380 byTemp = _pabyDataT[1]; \ 00381 _pabyDataT[1] = _pabyDataT[6]; \ 00382 _pabyDataT[6] = byTemp; \ 00383 byTemp = _pabyDataT[2]; \ 00384 _pabyDataT[2] = _pabyDataT[5]; \ 00385 _pabyDataT[5] = byTemp; \ 00386 byTemp = _pabyDataT[3]; \ 00387 _pabyDataT[3] = _pabyDataT[4]; \ 00388 _pabyDataT[4] = byTemp; \ 00389 } 00390 00391 00392 /* Until we have a safe 64 bits integer data type defined, we'll replace 00393 m * this version of the CPL_SWAP64() macro with a less efficient one. 00394 */ 00395 /* 00396 #define CPL_SWAP64(x) \ 00397 ((uint64)( \ 00398 (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \ 00399 (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \ 00400 (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \ 00401 (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \ 00402 (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \ 00403 (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \ 00404 (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \ 00405 (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) )) 00406 */ 00407 00408 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p) 00409 00410 #ifdef CPL_MSB 00411 # define CPL_MSBWORD16(x) (x) 00412 # define CPL_LSBWORD16(x) CPL_SWAP16(x) 00413 # define CPL_MSBWORD32(x) (x) 00414 # define CPL_LSBWORD32(x) CPL_SWAP32(x) 00415 # define CPL_MSBPTR16(x) 00416 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x) 00417 # define CPL_MSBPTR32(x) 00418 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x) 00419 # define CPL_MSBPTR64(x) 00420 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x) 00421 #else 00422 # define CPL_LSBWORD16(x) (x) 00423 # define CPL_MSBWORD16(x) CPL_SWAP16(x) 00424 # define CPL_LSBWORD32(x) (x) 00425 # define CPL_MSBWORD32(x) CPL_SWAP32(x) 00426 # define CPL_LSBPTR16(x) 00427 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x) 00428 # define CPL_LSBPTR32(x) 00429 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x) 00430 # define CPL_LSBPTR64(x) 00431 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x) 00432 #endif 00433 00434 /*********************************************************************** 00435 * Define CPL_CVSID() macro. It can be disabled during a build by 00436 * defining DISABLE_CPLID in the compiler options. 00437 * 00438 * The cvsid_aw() function is just there to prevent reports of cpl_cvsid() 00439 * being unused. 00440 */ 00441 00442 #ifndef DISABLE_CVSID 00443 # define CPL_CVSID(string) static char cpl_cvsid[] = string; \ 00444 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); } 00445 #else 00446 # define CPL_CVSID(string) 00447 #endif 00448 00449 #endif /* ndef CPL_BASE_H_INCLUDED */