00001 /****************************************************************************** 00002 * $Id: cpl_port.h,v 1.45 2005/06/15 09:47:40 dron 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.45 2005/06/15 09:47:40 dron 00046 * Fixed typo. 00047 * 00048 * Revision 1.44 2005/06/15 09:11:58 dron 00049 * Added CPLIsEqual() macro. 00050 * 00051 * Revision 1.43 2005/05/23 03:57:08 fwarmerdam 00052 * added default definition of CPL_THREADLOCAL 00053 * 00054 * Revision 1.42 2005/04/04 15:22:36 fwarmerdam 00055 * added CPL_STDCALL declaration 00056 * 00057 * Revision 1.41 2005/03/17 04:20:24 fwarmerdam 00058 * added FORCE_CDECL 00059 * 00060 * Revision 1.40 2005/03/11 14:59:07 fwarmerdam 00061 * Default to assuming nothing is infinite if isinf() macro not defined. 00062 * Per http://bugzilla.remotesensing.org/show_bug.cgi?id=795 00063 * 00064 * Revision 1.39 2005/03/01 21:22:07 fwarmerdam 00065 * added CPLIsFinite() 00066 * 00067 * Revision 1.38 2005/03/01 20:44:38 fwarmerdam 00068 * Check for _MSC_VER instead of WIN32. 00069 * 00070 * Revision 1.37 2005/03/01 19:57:55 fwarmerdam 00071 * Added CPLIsNan and CPLIsInf macros. 00072 * 00073 * Revision 1.36 2004/01/06 21:42:32 warmerda 00074 * "Fix" for bug 455 related to CPL_IS_LSB macro. 00075 * 00076 * Revision 1.35 2003/12/11 03:16:02 warmerda 00077 * Added CPL_IS_LSB macro with value 0 (MSB) or 1 (LSB). 00078 * 00079 * Revision 1.34 2003/09/08 11:11:05 dron 00080 * Include time.h and locale.h. 00081 * 00082 * Revision 1.33 2003/05/12 14:52:56 warmerda 00083 * Use _MSC_VER to test for Microsoft Visual C++ compiler. 00084 * 00085 * Revision 1.32 2002/10/24 20:24:40 warmerda 00086 * avoid using variable names likely to conflict in macros 00087 * 00088 * Revision 1.31 2002/07/15 13:31:46 warmerda 00089 * CPL_SWAPDOUBLE had alignment problem, use CPL_SWAP64PTR 00090 * 00091 * Revision 1.30 2002/04/18 18:55:06 dron 00092 * added <ctype.h> at the list of standard include files 00093 * 00094 * Revision 1.29 2002/01/17 01:40:27 warmerda 00095 * added _LARGEFILE64_SOURCE support 00096 * 00097 * Revision 1.28 2001/08/30 21:20:49 warmerda 00098 * expand tabs 00099 * 00100 * Revision 1.27 2001/07/18 04:00:49 warmerda 00101 * added CPL_CVSID 00102 * 00103 * Revision 1.26 2001/06/21 21:17:26 warmerda 00104 * added irix 64bit file api support 00105 * 00106 * Revision 1.25 2001/04/30 18:18:38 warmerda 00107 * added macos support, standard header 00108 * 00109 * Revision 1.24 2001/01/19 21:16:41 warmerda 00110 * expanded tabs 00111 * 00112 * Revision 1.23 2001/01/13 04:06:39 warmerda 00113 * added strings.h on AIX as per patch from Dale. 00114 * 00115 * Revision 1.22 2001/01/03 16:18:07 warmerda 00116 * added GUIntBig 00117 * 00118 * Revision 1.21 2000/10/20 04:20:33 warmerda 00119 * added SWAP16PTR macros 00120 * 00121 * Revision 1.20 2000/10/13 17:32:42 warmerda 00122 * check for unix instead of IGNORE_WIN32 00123 * 00124 * Revision 1.19 2000/09/25 19:58:43 warmerda 00125 * ensure win32 doesn't get defined in Cygnus builds 00126 * 00127 * Revision 1.18 2000/07/20 13:15:03 warmerda 00128 * don't redeclare CPL_DLL 00129 */ 00130 00131 #ifndef CPL_BASE_H_INCLUDED 00132 #define CPL_BASE_H_INCLUDED 00133 00141 /* ==================================================================== */ 00142 /* We will use macos_pre10 to indicate compilation with MacOS */ 00143 /* versions before MacOS X. */ 00144 /* ==================================================================== */ 00145 #ifdef macintosh 00146 # define macos_pre10 00147 #endif 00148 00149 /* ==================================================================== */ 00150 /* We will use WIN32 as a standard windows define. */ 00151 /* ==================================================================== */ 00152 #if defined(_WIN32) && !defined(WIN32) 00153 # define WIN32 00154 #endif 00155 00156 #if defined(_WINDOWS) && !defined(WIN32) 00157 # define WIN32 00158 #endif 00159 00160 #include "cpl_config.h" 00161 00162 /* ==================================================================== */ 00163 /* This will disable most WIN32 stuff in a Cygnus build which */ 00164 /* defines unix to 1. */ 00165 /* ==================================================================== */ 00166 00167 #ifdef unix 00168 # undef WIN32 00169 #endif 00170 00171 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE) 00172 # define _LARGEFILE64_SOURCE 1 00173 #endif 00174 00175 /* ==================================================================== */ 00176 /* Standard include files. */ 00177 /* ==================================================================== */ 00178 00179 #include <stdio.h> 00180 #include <stdlib.h> 00181 #include <math.h> 00182 #include <stdarg.h> 00183 #include <string.h> 00184 #include <ctype.h> 00185 #include <errno.h> 00186 #include <time.h> 00187 00188 #ifdef HAVE_LOCALE_H 00189 # include <locale.h> 00190 #endif 00191 00192 #ifdef _AIX 00193 # include <strings.h> 00194 #endif 00195 00196 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG) 00197 # define DBMALLOC 00198 # include <dbmalloc.h> 00199 #endif 00200 00201 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H) 00202 # define USE_DMALLOC 00203 # include <dmalloc.h> 00204 #endif 00205 00206 /* ==================================================================== */ 00207 /* Base portability stuff ... this stuff may need to be */ 00208 /* modified for new platforms. */ 00209 /* ==================================================================== */ 00210 00211 /*--------------------------------------------------------------------- 00212 * types for 16 and 32 bits integers, etc... 00213 *--------------------------------------------------------------------*/ 00214 #if UINT_MAX == 65535 00215 typedef long GInt32; 00216 typedef unsigned long GUInt32; 00217 #else 00218 typedef int GInt32; 00219 typedef unsigned int GUInt32; 00220 #endif 00221 00222 typedef short GInt16; 00223 typedef unsigned short GUInt16; 00224 typedef unsigned char GByte; 00225 typedef int GBool; 00226 00227 /* -------------------------------------------------------------------- */ 00228 /* 64bit support */ 00229 /* -------------------------------------------------------------------- */ 00230 00231 #if defined(WIN32) && defined(_MSC_VER) 00232 00233 #define VSI_LARGE_API_SUPPORTED 00234 typedef __int64 GIntBig; 00235 typedef unsigned __int64 GUIntBig; 00236 00237 #elif HAVE_LONG_LONG 00238 00239 typedef long long GIntBig; 00240 typedef unsigned long long GUIntBig; 00241 00242 #else 00243 00244 typedef long GIntBig; 00245 typedef unsigned long GUIntBig; 00246 00247 #endif 00248 00249 /* ==================================================================== */ 00250 /* Other standard services. */ 00251 /* ==================================================================== */ 00252 #ifdef __cplusplus 00253 # define CPL_C_START extern "C" { 00254 # define CPL_C_END } 00255 #else 00256 # define CPL_C_START 00257 # define CPL_C_END 00258 #endif 00259 00260 #ifndef CPL_DLL 00261 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL) 00262 # define CPL_DLL __declspec(dllexport) 00263 #else 00264 # define CPL_DLL 00265 #endif 00266 #endif 00267 00268 #ifndef CPL_STDCALL 00269 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL) 00270 # define CPL_STDCALL __stdcall 00271 #else 00272 # define CPL_STDCALL 00273 #endif 00274 #endif 00275 00276 #ifdef _MSC_VER 00277 # define FORCE_CDECL __cdecl 00278 #else 00279 # define FORCE_CDECL 00280 #endif 00281 00282 #ifndef NULL 00283 # define NULL 0 00284 #endif 00285 00286 #ifndef FALSE 00287 # define FALSE 0 00288 #endif 00289 00290 #ifndef TRUE 00291 # define TRUE 1 00292 #endif 00293 00294 #ifndef MAX 00295 # define MIN(a,b) ((a<b) ? a : b) 00296 # define MAX(a,b) ((a>b) ? a : b) 00297 #endif 00298 00299 #ifndef ABS 00300 # define ABS(x) ((x<0) ? (-1*(x)) : x) 00301 #endif 00302 00303 /* -------------------------------------------------------------------- */ 00304 /* Macro to test equality of two floating point values. */ 00305 /* We use fabs() function instead of ABS() macro to avoid side */ 00306 /* effects. */ 00307 /* -------------------------------------------------------------------- */ 00308 #ifndef CPLIsEqual 00309 # define CPLIsEqual(x,y) (fabs(fabs(x) - fabs(y)) < 0.0000000000001 ? 1 : 0) 00310 #endif 00311 00312 #ifndef EQUAL 00313 #ifdef WIN32 00314 # define EQUALN(a,b,n) (strnicmp(a,b,n)==0) 00315 # define EQUAL(a,b) (stricmp(a,b)==0) 00316 #else 00317 # define EQUALN(a,b,n) (strncasecmp(a,b,n)==0) 00318 # define EQUAL(a,b) (strcasecmp(a,b)==0) 00319 #endif 00320 #endif 00321 00322 #ifdef macos_pre10 00323 int strcasecmp(char * str1, char * str2); 00324 int strncasecmp(char * str1, char * str2, int len); 00325 char * strdup (char *instr); 00326 #endif 00327 00328 #ifndef CPL_THREADLOCAL 00329 # define CPL_THREADLOCAL 00330 #endif 00331 00332 /* -------------------------------------------------------------------- */ 00333 /* Handle isnan() and isinf(). Note that isinf() and isnan() */ 00334 /* are supposed to be macros according to C99. Some systems */ 00335 /* (ie. Tru64) don't have isinf() at all, so if the macro is */ 00336 /* not defined we just assume nothing is infinite. This may */ 00337 /* mean we have no real CPLIsInf() on systems with an isinf() */ 00338 /* function but no corresponding macro, but I can live with */ 00339 /* that since it isn't that important a test. */ 00340 /* -------------------------------------------------------------------- */ 00341 #ifdef _MSC_VER 00342 # define CPLIsNan(x) _isnan(x) 00343 # define CPLIsInf(x) (!_isnan(x) && !_finite(x)) 00344 # define CPLIsFinite(x) _finite(x) 00345 #else 00346 # define CPLIsNan(x) isnan(x) 00347 # ifdef isinf 00348 # define CPLIsInf(x) isinf(x) 00349 # define CPLIsFinite(x) (!isnan(x) && !isinf(x)) 00350 # else 00351 # define CPLIsInf(x) FALSE 00352 # define CPLIsFinite(x) (!isnan(x)) 00353 # endif 00354 #endif 00355 00356 /*--------------------------------------------------------------------- 00357 * CPL_LSB and CPL_MSB 00358 * Only one of these 2 macros should be defined and specifies the byte 00359 * ordering for the current platform. 00360 * This should be defined in the Makefile, but if it is not then 00361 * the default is CPL_LSB (Intel ordering, LSB first). 00362 *--------------------------------------------------------------------*/ 00363 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB) 00364 # define CPL_MSB 00365 #endif 00366 00367 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) ) 00368 #define CPL_LSB 00369 #endif 00370 00371 #if defined(CPL_LSB) 00372 # define CPL_IS_LSB 1 00373 #else 00374 # define CPL_IS_LSB 0 00375 #endif 00376 00377 /*--------------------------------------------------------------------- 00378 * Little endian <==> big endian byte swap macros. 00379 *--------------------------------------------------------------------*/ 00380 00381 #define CPL_SWAP16(x) \ 00382 ((GUInt16)( \ 00383 (((GUInt16)(x) & 0x00ffU) << 8) | \ 00384 (((GUInt16)(x) & 0xff00U) >> 8) )) 00385 00386 #define CPL_SWAP16PTR(x) \ 00387 { \ 00388 GByte byTemp, *_pabyDataT = (GByte *) (x); \ 00389 \ 00390 byTemp = _pabyDataT[0]; \ 00391 _pabyDataT[0] = _pabyDataT[1]; \ 00392 _pabyDataT[1] = byTemp; \ 00393 } 00394 00395 #define CPL_SWAP32(x) \ 00396 ((GUInt32)( \ 00397 (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \ 00398 (((GUInt32)(x) & (GUInt32)0x0000ff00UL) << 8) | \ 00399 (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >> 8) | \ 00400 (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) )) 00401 00402 #define CPL_SWAP32PTR(x) \ 00403 { \ 00404 GByte byTemp, *_pabyDataT = (GByte *) (x); \ 00405 \ 00406 byTemp = _pabyDataT[0]; \ 00407 _pabyDataT[0] = _pabyDataT[3]; \ 00408 _pabyDataT[3] = byTemp; \ 00409 byTemp = _pabyDataT[1]; \ 00410 _pabyDataT[1] = _pabyDataT[2]; \ 00411 _pabyDataT[2] = byTemp; \ 00412 } 00413 00414 #define CPL_SWAP64PTR(x) \ 00415 { \ 00416 GByte byTemp, *_pabyDataT = (GByte *) (x); \ 00417 \ 00418 byTemp = _pabyDataT[0]; \ 00419 _pabyDataT[0] = _pabyDataT[7]; \ 00420 _pabyDataT[7] = byTemp; \ 00421 byTemp = _pabyDataT[1]; \ 00422 _pabyDataT[1] = _pabyDataT[6]; \ 00423 _pabyDataT[6] = byTemp; \ 00424 byTemp = _pabyDataT[2]; \ 00425 _pabyDataT[2] = _pabyDataT[5]; \ 00426 _pabyDataT[5] = byTemp; \ 00427 byTemp = _pabyDataT[3]; \ 00428 _pabyDataT[3] = _pabyDataT[4]; \ 00429 _pabyDataT[4] = byTemp; \ 00430 } 00431 00432 00433 /* Until we have a safe 64 bits integer data type defined, we'll replace 00434 m * this version of the CPL_SWAP64() macro with a less efficient one. 00435 */ 00436 /* 00437 #define CPL_SWAP64(x) \ 00438 ((uint64)( \ 00439 (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \ 00440 (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \ 00441 (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \ 00442 (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \ 00443 (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \ 00444 (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \ 00445 (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \ 00446 (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) )) 00447 */ 00448 00449 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p) 00450 00451 #ifdef CPL_MSB 00452 # define CPL_MSBWORD16(x) (x) 00453 # define CPL_LSBWORD16(x) CPL_SWAP16(x) 00454 # define CPL_MSBWORD32(x) (x) 00455 # define CPL_LSBWORD32(x) CPL_SWAP32(x) 00456 # define CPL_MSBPTR16(x) 00457 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x) 00458 # define CPL_MSBPTR32(x) 00459 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x) 00460 # define CPL_MSBPTR64(x) 00461 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x) 00462 #else 00463 # define CPL_LSBWORD16(x) (x) 00464 # define CPL_MSBWORD16(x) CPL_SWAP16(x) 00465 # define CPL_LSBWORD32(x) (x) 00466 # define CPL_MSBWORD32(x) CPL_SWAP32(x) 00467 # define CPL_LSBPTR16(x) 00468 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x) 00469 # define CPL_LSBPTR32(x) 00470 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x) 00471 # define CPL_LSBPTR64(x) 00472 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x) 00473 #endif 00474 00475 /*********************************************************************** 00476 * Define CPL_CVSID() macro. It can be disabled during a build by 00477 * defining DISABLE_CPLID in the compiler options. 00478 * 00479 * The cvsid_aw() function is just there to prevent reports of cpl_cvsid() 00480 * being unused. 00481 */ 00482 00483 #ifndef DISABLE_CVSID 00484 # define CPL_CVSID(string) static char cpl_cvsid[] = string; \ 00485 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); } 00486 #else 00487 # define CPL_CVSID(string) 00488 #endif 00489 00490 #endif /* ndef CPL_BASE_H_INCLUDED */