cpl_port.h

Go to the documentation of this file.
00001 /******************************************************************************
00002  * $Id: cpl_port.h 13724 2008-02-07 20:02:52Z warmerdam $
00003  *
00004  * Project:  CPL - Common Portability Library
00005  * Author:   Frank Warmerdam, warmerdam@pobox.com
00006  * Purpose:  Include file providing low level portability services for CPL.  
00007  *           This should be the first include file for any CPL based code.  
00008  *
00009  ******************************************************************************
00010  * Copyright (c) 1998, 2005, Frank Warmerdam <warmerdam@pobox.com>
00011  *
00012  * Permission is hereby granted, free of charge, to any person obtaining a
00013  * copy of this software and associated documentation files (the "Software"),
00014  * to deal in the Software without restriction, including without limitation
00015  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00016  * and/or sell copies of the Software, and to permit persons to whom the
00017  * Software is furnished to do so, subject to the following conditions:
00018  *
00019  * The above copyright notice and this permission notice shall be included
00020  * in all copies or substantial portions of the Software.
00021  *
00022  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00023  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00025  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00027  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00028  * DEALINGS IN THE SOFTWARE.
00029  ****************************************************************************/
00030 
00031 #ifndef CPL_BASE_H_INCLUDED
00032 #define CPL_BASE_H_INCLUDED
00033 
00034 /* Remove annoying warnings Microsoft Visual C++ */
00035 #if defined(_MSC_VER)
00036 #  pragma warning(disable:4251 4275 4786)
00037 #endif
00038 
00046 /* ==================================================================== */
00047 /*      We will use macos_pre10 to indicate compilation with MacOS      */
00048 /*      versions before MacOS X.                                        */
00049 /* ==================================================================== */
00050 #ifdef macintosh
00051 #  define macos_pre10
00052 #endif
00053 
00054 /* ==================================================================== */
00055 /*      We will use WIN32 as a standard windows define.                 */
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 /*      We will use WIN32CE as a standard Windows CE (Mobile) define.   */
00067 /* ==================================================================== */
00068 #if defined(_WIN32_WCE)
00069 #  define WIN32CE
00070 #endif
00071 
00072 /* -------------------------------------------------------------------- */
00073 /*      The following apparently allow you to use strcpy() and other    */
00074 /*      functions judged "unsafe" by microsoft in VS 8 (2005).          */
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 /*      This will disable most WIN32 stuff in a Cygnus build which      */
00093 /*      defines unix to 1.                                              */
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 /*      Standard include files.                                         */
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 /*      Base portability stuff ... this stuff may need to be            */
00153 /*      modified for new platforms.                                     */
00154 /* ==================================================================== */
00155 
00156 /*---------------------------------------------------------------------
00157  *        types for 16 and 32 bits integers, etc...
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 /*      64bit support                                                   */
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 /*      Other standard services.                                        */
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 /* Should optional (normally private) interfaces be exported? */
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 #ifndef NULL
00239 #  define NULL  0
00240 #endif
00241 
00242 #ifndef FALSE
00243 #  define FALSE 0
00244 #endif
00245 
00246 #ifndef TRUE
00247 #  define TRUE  1
00248 #endif
00249 
00250 #ifndef MAX
00251 #  define MIN(a,b)      ((a<b) ? a : b)
00252 #  define MAX(a,b)      ((a>b) ? a : b)
00253 #endif
00254 
00255 #ifndef ABS
00256 #  define ABS(x)        ((x<0) ? (-1*(x)) : x)
00257 #endif
00258 
00259 /* -------------------------------------------------------------------- */
00260 /*      Macro to test equality of two floating point values.            */
00261 /*      We use fabs() function instead of ABS() macro to avoid side     */
00262 /*      effects.                                                        */
00263 /* -------------------------------------------------------------------- */
00264 #ifndef CPLIsEqual
00265 #  define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
00266 #endif
00267 
00268 #ifndef EQUAL
00269 #if defined(WIN32) || defined(WIN32CE)
00270 #  define EQUALN(a,b,n)           (strnicmp(a,b,n)==0)
00271 #  define EQUAL(a,b)              (stricmp(a,b)==0)
00272 #else
00273 #  define EQUALN(a,b,n)           (strncasecmp(a,b,n)==0)
00274 #  define EQUAL(a,b)              (strcasecmp(a,b)==0)
00275 #endif
00276 #endif
00277 
00278 #ifdef macos_pre10
00279 int strcasecmp(char * str1, char * str2);
00280 int strncasecmp(char * str1, char * str2, int len);
00281 char * strdup (char *instr);
00282 #endif
00283 
00284 #ifndef CPL_THREADLOCAL 
00285 #  define CPL_THREADLOCAL 
00286 #endif
00287 
00288 /* -------------------------------------------------------------------- */
00289 /*      Handle isnan() and isinf().  Note that isinf() and isnan()      */
00290 /*      are supposed to be macros according to C99, defined in math.h   */
00291 /*      Some systems (ie. Tru64) don't have isinf() at all, so if       */
00292 /*      the macro is not defined we just assume nothing is infinite.    */
00293 /*      This may mean we have no real CPLIsInf() on systems with isinf()*/
00294 /*      function but no corresponding macro, but I can live with        */
00295 /*      that since it isn't that important a test.                      */
00296 /* -------------------------------------------------------------------- */
00297 #ifdef _MSC_VER
00298 #  include <float.h>
00299 #  define CPLIsNan(x) _isnan(x)
00300 #  define CPLIsInf(x) (!_isnan(x) && !_finite(x))
00301 #  define CPLIsFinite(x) _finite(x)
00302 #else
00303 #  define CPLIsNan(x) isnan(x)
00304 #  ifdef isinf 
00305 #    define CPLIsInf(x) isinf(x)
00306 #    define CPLIsFinite(x) (!isnan(x) && !isinf(x))
00307 #  else
00308 #    define CPLIsInf(x)    FALSE
00309 #    define CPLIsFinite(x) (!isnan(x))
00310 #  endif
00311 #endif
00312 
00313 /*---------------------------------------------------------------------
00314  *                         CPL_LSB and CPL_MSB
00315  * Only one of these 2 macros should be defined and specifies the byte 
00316  * ordering for the current platform.  
00317  * This should be defined in the Makefile, but if it is not then
00318  * the default is CPL_LSB (Intel ordering, LSB first).
00319  *--------------------------------------------------------------------*/
00320 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
00321 #  define CPL_MSB
00322 #endif
00323 
00324 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
00325 #define CPL_LSB
00326 #endif
00327 
00328 #if defined(CPL_LSB)
00329 #  define CPL_IS_LSB 1
00330 #else
00331 #  define CPL_IS_LSB 0
00332 #endif
00333 
00334 /*---------------------------------------------------------------------
00335  *        Little endian <==> big endian byte swap macros.
00336  *--------------------------------------------------------------------*/
00337 
00338 #define CPL_SWAP16(x) \
00339         ((GUInt16)( \
00340             (((GUInt16)(x) & 0x00ffU) << 8) | \
00341             (((GUInt16)(x) & 0xff00U) >> 8) ))
00342 
00343 #define CPL_SWAP16PTR(x) \
00344 {                                                                 \
00345     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00346                                                                   \
00347     byTemp = _pabyDataT[0];                                       \
00348     _pabyDataT[0] = _pabyDataT[1];                                \
00349     _pabyDataT[1] = byTemp;                                       \
00350 }                                                                    
00351                                                             
00352 #define CPL_SWAP32(x) \
00353         ((GUInt32)( \
00354             (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
00355             (((GUInt32)(x) & (GUInt32)0x0000ff00UL) <<  8) | \
00356             (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >>  8) | \
00357             (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
00358 
00359 #define CPL_SWAP32PTR(x) \
00360 {                                                                 \
00361     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00362                                                                   \
00363     byTemp = _pabyDataT[0];                                       \
00364     _pabyDataT[0] = _pabyDataT[3];                                \
00365     _pabyDataT[3] = byTemp;                                       \
00366     byTemp = _pabyDataT[1];                                       \
00367     _pabyDataT[1] = _pabyDataT[2];                                \
00368     _pabyDataT[2] = byTemp;                                       \
00369 }                                                                    
00370                                                             
00371 #define CPL_SWAP64PTR(x) \
00372 {                                                                 \
00373     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00374                                                                   \
00375     byTemp = _pabyDataT[0];                                       \
00376     _pabyDataT[0] = _pabyDataT[7];                                \
00377     _pabyDataT[7] = byTemp;                                       \
00378     byTemp = _pabyDataT[1];                                       \
00379     _pabyDataT[1] = _pabyDataT[6];                                \
00380     _pabyDataT[6] = byTemp;                                       \
00381     byTemp = _pabyDataT[2];                                       \
00382     _pabyDataT[2] = _pabyDataT[5];                                \
00383     _pabyDataT[5] = byTemp;                                       \
00384     byTemp = _pabyDataT[3];                                       \
00385     _pabyDataT[3] = _pabyDataT[4];                                \
00386     _pabyDataT[4] = byTemp;                                       \
00387 }                                                                    
00388                                                             
00389 
00390 /* Until we have a safe 64 bits integer data type defined, we'll replace
00391  * this version of the CPL_SWAP64() macro with a less efficient one.
00392  */
00393 /*
00394 #define CPL_SWAP64(x) \
00395         ((uint64)( \
00396             (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
00397             (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
00398             (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
00399             (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \
00400             (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \
00401             (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
00402             (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
00403             (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
00404 */
00405 
00406 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
00407 
00408 #ifdef CPL_MSB
00409 #  define CPL_MSBWORD16(x)      (x)
00410 #  define CPL_LSBWORD16(x)      CPL_SWAP16(x)
00411 #  define CPL_MSBWORD32(x)      (x)
00412 #  define CPL_LSBWORD32(x)      CPL_SWAP32(x)
00413 #  define CPL_MSBPTR16(x)       
00414 #  define CPL_LSBPTR16(x)       CPL_SWAP16PTR(x)
00415 #  define CPL_MSBPTR32(x)       
00416 #  define CPL_LSBPTR32(x)       CPL_SWAP32PTR(x)
00417 #  define CPL_MSBPTR64(x)       
00418 #  define CPL_LSBPTR64(x)       CPL_SWAP64PTR(x)
00419 #else
00420 #  define CPL_LSBWORD16(x)      (x)
00421 #  define CPL_MSBWORD16(x)      CPL_SWAP16(x)
00422 #  define CPL_LSBWORD32(x)      (x)
00423 #  define CPL_MSBWORD32(x)      CPL_SWAP32(x)
00424 #  define CPL_LSBPTR16(x)       
00425 #  define CPL_MSBPTR16(x)       CPL_SWAP16PTR(x)
00426 #  define CPL_LSBPTR32(x)       
00427 #  define CPL_MSBPTR32(x)       CPL_SWAP32PTR(x)
00428 #  define CPL_LSBPTR64(x)       
00429 #  define CPL_MSBPTR64(x)       CPL_SWAP64PTR(x)
00430 #endif
00431 
00432 /***********************************************************************
00433  * Define CPL_CVSID() macro.  It can be disabled during a build by
00434  * defining DISABLE_CPLID in the compiler options.
00435  *
00436  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
00437  * being unused.
00438  */
00439 
00440 #ifndef DISABLE_CVSID
00441 #  define CPL_CVSID(string)     static char cpl_cvsid[] = string; \
00442 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
00443 #else
00444 #  define CPL_CVSID(string)
00445 #endif
00446 
00447 #endif /* ndef CPL_BASE_H_INCLUDED */

Generated for GDAL by doxygen 1.5.5.