cpl_port.h

Go to the documentation of this file.
00001 /******************************************************************************
00002  * $Id: cpl_port.h 23431 2011-11-27 15:02:24Z rouault $
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 
00041 /* ==================================================================== */
00042 /*      We will use macos_pre10 to indicate compilation with MacOS      */
00043 /*      versions before MacOS X.                                        */
00044 /* ==================================================================== */
00045 #ifdef macintosh
00046 #  define macos_pre10
00047 #endif
00048 
00049 /* ==================================================================== */
00050 /*      We will use WIN32 as a standard windows define.                 */
00051 /* ==================================================================== */
00052 #if defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE)
00053 #  define WIN32
00054 #endif
00055 
00056 #if defined(_WINDOWS) && !defined(WIN32) && !defined(_WIN32_WCE)
00057 #  define WIN32
00058 #endif
00059 
00060 /* ==================================================================== */
00061 /*      We will use WIN32CE as a standard Windows CE (Mobile) define.   */
00062 /* ==================================================================== */
00063 #if defined(_WIN32_WCE)
00064 #  define WIN32CE
00065 #endif
00066 
00067 /* -------------------------------------------------------------------- */
00068 /*      The following apparently allow you to use strcpy() and other    */
00069 /*      functions judged "unsafe" by microsoft in VS 8 (2005).          */
00070 /* -------------------------------------------------------------------- */
00071 #ifdef _MSC_VER
00072 #  ifndef _CRT_SECURE_NO_DEPRECATE
00073 #    define _CRT_SECURE_NO_DEPRECATE
00074 #  endif
00075 #  ifndef _CRT_NONSTDC_NO_DEPRECATE
00076 #    define _CRT_NONSTDC_NO_DEPRECATE
00077 #  endif
00078 #  ifdef MSVC_USE_VLD
00079 #    include <vld.h>
00080 #  endif
00081 #endif
00082 
00083 #include "cpl_config.h"
00084 
00085 /* ==================================================================== */
00086 /*      This will disable most WIN32 stuff in a Cygnus build which      */
00087 /*      defines unix to 1.                                              */
00088 /* ==================================================================== */
00089 
00090 #ifdef unix
00091 #  undef WIN32
00092 #  undef WIN32CE
00093 #endif
00094 
00095 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
00096 #  define _LARGEFILE64_SOURCE 1
00097 #endif
00098 
00099 /* ==================================================================== */
00100 /*      If iconv() is available use extended recoding module.           */
00101 /*      Stub implementation is always compiled in, because it works     */
00102 /*      faster than iconv() for encodings it supports.                  */
00103 /* ==================================================================== */
00104 
00105 #if defined(HAVE_ICONV)
00106 #  define CPL_RECODE_ICONV
00107 #endif
00108 
00109 #define CPL_RECODE_STUB
00110 
00111 /* ==================================================================== */
00112 /*      MinGW stuff                                                     */
00113 /* ==================================================================== */
00114 
00115 /* We need __MSVCRT_VERSION__ >= 0x0601 to have "struct __stat64" */
00116 /* Latest versions of mingw32 define it, but with older ones, */
00117 /* we need to define it manually */
00118 #if defined(__MINGW32__)
00119 #ifndef __MSVCRT_VERSION__
00120 #define __MSVCRT_VERSION__ 0x0601
00121 #endif
00122 #endif
00123 
00124 /* ==================================================================== */
00125 /*      Standard include files.                                         */
00126 /* ==================================================================== */
00127 
00128 #include <stdio.h>
00129 #include <stdlib.h>
00130 #include <math.h>
00131 #include <stdarg.h>
00132 #include <string.h>
00133 #include <ctype.h>
00134 #include <limits.h>
00135 
00136 #if !defined(WIN32CE)
00137 #  include <time.h>
00138 #else
00139 #  include <wce_time.h>
00140 #  include <wce_errno.h>
00141 #endif
00142 
00143 
00144 #if defined(HAVE_ERRNO_H)
00145 #  include <errno.h>
00146 #endif 
00147 
00148 #ifdef HAVE_LOCALE_H
00149 #  include <locale.h>
00150 #endif
00151 
00152 #ifdef HAVE_DIRECT_H
00153 #  include <direct.h>
00154 #endif
00155 
00156 #ifdef _AIX
00157 #  include <strings.h>
00158 #endif
00159 
00160 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
00161 #  define DBMALLOC
00162 #  include <dbmalloc.h>
00163 #endif
00164 
00165 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
00166 #  define USE_DMALLOC
00167 #  include <dmalloc.h>
00168 #endif
00169 
00170 /* ==================================================================== */
00171 /*      Base portability stuff ... this stuff may need to be            */
00172 /*      modified for new platforms.                                     */
00173 /* ==================================================================== */
00174 
00175 /*---------------------------------------------------------------------
00176  *        types for 16 and 32 bits integers, etc...
00177  *--------------------------------------------------------------------*/
00178 #if UINT_MAX == 65535
00179 typedef long            GInt32;
00180 typedef unsigned long   GUInt32;
00181 #else
00182 typedef int             GInt32;
00183 typedef unsigned int    GUInt32;
00184 #endif
00185 
00186 typedef short           GInt16;
00187 typedef unsigned short  GUInt16;
00188 typedef unsigned char   GByte;
00189 /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
00190 /* in include/poppler/goo/gtypes.h */
00191 #ifndef CPL_GBOOL_DEFINED
00192 #define CPL_GBOOL_DEFINED
00193 typedef int             GBool;
00194 #endif
00195 
00196 /* -------------------------------------------------------------------- */
00197 /*      64bit support                                                   */
00198 /* -------------------------------------------------------------------- */
00199 
00200 #if defined(WIN32) && defined(_MSC_VER)
00201 
00202 #define VSI_LARGE_API_SUPPORTED
00203 typedef __int64          GIntBig;
00204 typedef unsigned __int64 GUIntBig;
00205 
00206 #elif HAVE_LONG_LONG
00207 
00208 typedef long long        GIntBig;
00209 typedef unsigned long long GUIntBig;
00210 
00211 #else
00212 
00213 typedef long             GIntBig;
00214 typedef unsigned long    GUIntBig;
00215 
00216 #endif
00217 
00218 #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
00219   #define CPL_FRMT_GB_WITHOUT_PREFIX     "I64"
00220 #elif HAVE_LONG_LONG
00221   #define CPL_FRMT_GB_WITHOUT_PREFIX     "ll"
00222 #else
00223   #define CPL_FRMT_GB_WITHOUT_PREFIX     "l"
00224 #endif
00225 
00226 #define CPL_FRMT_GIB     "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
00227 #define CPL_FRMT_GUIB    "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
00228 
00229 /* Workaround VC6 bug */
00230 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
00231 #define GUINTBIG_TO_DOUBLE(x) (double)(GIntBig)(x)
00232 #else
00233 #define GUINTBIG_TO_DOUBLE(x) (double)(x)
00234 #endif
00235 
00236 /* ==================================================================== */
00237 /*      Other standard services.                                        */
00238 /* ==================================================================== */
00239 #ifdef __cplusplus
00240 #  define CPL_C_START           extern "C" {
00241 #  define CPL_C_END             }
00242 #else
00243 #  define CPL_C_START
00244 #  define CPL_C_END
00245 #endif
00246 
00247 #ifndef CPL_DLL
00248 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
00249 #  define CPL_DLL     __declspec(dllexport)
00250 #else
00251 #  if defined(USE_GCC_VISIBILITY_FLAG)
00252 #    define CPL_DLL     __attribute__ ((visibility("default")))
00253 #  else
00254 #    define CPL_DLL
00255 #  endif
00256 #endif
00257 #endif
00258 
00259 /* Should optional (normally private) interfaces be exported? */
00260 #ifdef CPL_OPTIONAL_APIS
00261 #  define CPL_ODLL CPL_DLL
00262 #else
00263 #  define CPL_ODLL
00264 #endif
00265 
00266 #ifndef CPL_STDCALL
00267 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
00268 #  define CPL_STDCALL     __stdcall
00269 #else
00270 #  define CPL_STDCALL
00271 #endif
00272 #endif
00273 
00274 #ifdef _MSC_VER
00275 #  define FORCE_CDECL  __cdecl
00276 #else
00277 #  define FORCE_CDECL 
00278 #endif
00279 
00280 /* TODO : support for other compilers needed */
00281 #if defined(__GNUC__) || defined(_MSC_VER)
00282 #define HAS_CPL_INLINE  1
00283 #define CPL_INLINE __inline
00284 #elif defined(__SUNPRO_CC)
00285 #define HAS_CPL_INLINE  1
00286 #define CPL_INLINE inline
00287 #else
00288 #define CPL_INLINE
00289 #endif
00290 
00291 #ifndef NULL
00292 #  define NULL  0
00293 #endif
00294 
00295 #ifndef FALSE
00296 #  define FALSE 0
00297 #endif
00298 
00299 #ifndef TRUE
00300 #  define TRUE  1
00301 #endif
00302 
00303 #ifndef MAX
00304 #  define MIN(a,b)      ((a<b) ? a : b)
00305 #  define MAX(a,b)      ((a>b) ? a : b)
00306 #endif
00307 
00308 #ifndef ABS
00309 #  define ABS(x)        ((x<0) ? (-1*(x)) : x)
00310 #endif
00311 
00312 /* -------------------------------------------------------------------- */
00313 /*      Macro to test equality of two floating point values.            */
00314 /*      We use fabs() function instead of ABS() macro to avoid side     */
00315 /*      effects.                                                        */
00316 /* -------------------------------------------------------------------- */
00317 #ifndef CPLIsEqual
00318 #  define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
00319 #endif
00320 
00321 /* -------------------------------------------------------------------- */
00322 /*      Provide macros for case insensitive string comparisons.         */
00323 /* -------------------------------------------------------------------- */
00324 #ifndef EQUAL
00325 #  if defined(WIN32) || defined(WIN32CE)
00326 #    define STRCASECMP(a,b)         (stricmp(a,b))
00327 #    define STRNCASECMP(a,b,n)      (strnicmp(a,b,n))
00328 #  else
00329 #    define STRCASECMP(a,b)         (strcasecmp(a,b))
00330 #    define STRNCASECMP(a,b,n)      (strncasecmp(a,b,n))
00331 #  endif
00332 #  define EQUALN(a,b,n)           (STRNCASECMP(a,b,n)==0)
00333 #  define EQUAL(a,b)              (STRCASECMP(a,b)==0)
00334 #endif
00335 
00336 #ifdef macos_pre10
00337 int strcasecmp(char * str1, char * str2);
00338 int strncasecmp(char * str1, char * str2, int len);
00339 char * strdup (char *instr);
00340 #endif
00341 
00342 #ifndef CPL_THREADLOCAL
00343 #  define CPL_THREADLOCAL
00344 #endif
00345 
00346 /* -------------------------------------------------------------------- */
00347 /*      Handle isnan() and isinf().  Note that isinf() and isnan()      */
00348 /*      are supposed to be macros according to C99, defined in math.h   */
00349 /*      Some systems (ie. Tru64) don't have isinf() at all, so if       */
00350 /*      the macro is not defined we just assume nothing is infinite.    */
00351 /*      This may mean we have no real CPLIsInf() on systems with isinf()*/
00352 /*      function but no corresponding macro, but I can live with        */
00353 /*      that since it isn't that important a test.                      */
00354 /* -------------------------------------------------------------------- */
00355 #ifdef _MSC_VER
00356 #  include <float.h>
00357 #  define CPLIsNan(x) _isnan(x)
00358 #  define CPLIsInf(x) (!_isnan(x) && !_finite(x))
00359 #  define CPLIsFinite(x) _finite(x)
00360 #else
00361 #  define CPLIsNan(x) isnan(x)
00362 #  ifdef isinf 
00363 #    define CPLIsInf(x) isinf(x)
00364 #    define CPLIsFinite(x) (!isnan(x) && !isinf(x))
00365 #  else
00366 #    define CPLIsInf(x)    FALSE
00367 #    define CPLIsFinite(x) (!isnan(x))
00368 #  endif
00369 #endif
00370 
00371 /*---------------------------------------------------------------------
00372  *                         CPL_LSB and CPL_MSB
00373  * Only one of these 2 macros should be defined and specifies the byte 
00374  * ordering for the current platform.  
00375  * This should be defined in the Makefile, but if it is not then
00376  * the default is CPL_LSB (Intel ordering, LSB first).
00377  *--------------------------------------------------------------------*/
00378 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
00379 #  define CPL_MSB
00380 #endif
00381 
00382 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
00383 #define CPL_LSB
00384 #endif
00385 
00386 #if defined(CPL_LSB)
00387 #  define CPL_IS_LSB 1
00388 #else
00389 #  define CPL_IS_LSB 0
00390 #endif
00391 
00392 /*---------------------------------------------------------------------
00393  *        Little endian <==> big endian byte swap macros.
00394  *--------------------------------------------------------------------*/
00395 
00396 #define CPL_SWAP16(x) \
00397         ((GUInt16)( \
00398             (((GUInt16)(x) & 0x00ffU) << 8) | \
00399             (((GUInt16)(x) & 0xff00U) >> 8) ))
00400 
00401 #define CPL_SWAP16PTR(x) \
00402 {                                                                 \
00403     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00404                                                                   \
00405     byTemp = _pabyDataT[0];                                       \
00406     _pabyDataT[0] = _pabyDataT[1];                                \
00407     _pabyDataT[1] = byTemp;                                       \
00408 }                                                                    
00409                                                             
00410 #define CPL_SWAP32(x) \
00411         ((GUInt32)( \
00412             (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
00413             (((GUInt32)(x) & (GUInt32)0x0000ff00UL) <<  8) | \
00414             (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >>  8) | \
00415             (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
00416 
00417 #define CPL_SWAP32PTR(x) \
00418 {                                                                 \
00419     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00420                                                                   \
00421     byTemp = _pabyDataT[0];                                       \
00422     _pabyDataT[0] = _pabyDataT[3];                                \
00423     _pabyDataT[3] = byTemp;                                       \
00424     byTemp = _pabyDataT[1];                                       \
00425     _pabyDataT[1] = _pabyDataT[2];                                \
00426     _pabyDataT[2] = byTemp;                                       \
00427 }                                                                    
00428                                                             
00429 #define CPL_SWAP64PTR(x) \
00430 {                                                                 \
00431     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00432                                                                   \
00433     byTemp = _pabyDataT[0];                                       \
00434     _pabyDataT[0] = _pabyDataT[7];                                \
00435     _pabyDataT[7] = byTemp;                                       \
00436     byTemp = _pabyDataT[1];                                       \
00437     _pabyDataT[1] = _pabyDataT[6];                                \
00438     _pabyDataT[6] = byTemp;                                       \
00439     byTemp = _pabyDataT[2];                                       \
00440     _pabyDataT[2] = _pabyDataT[5];                                \
00441     _pabyDataT[5] = byTemp;                                       \
00442     byTemp = _pabyDataT[3];                                       \
00443     _pabyDataT[3] = _pabyDataT[4];                                \
00444     _pabyDataT[4] = byTemp;                                       \
00445 }                                                                    
00446                                                             
00447 
00448 /* Until we have a safe 64 bits integer data type defined, we'll replace
00449  * this version of the CPL_SWAP64() macro with a less efficient one.
00450  */
00451 /*
00452 #define CPL_SWAP64(x) \
00453         ((uint64)( \
00454             (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
00455             (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
00456             (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
00457             (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \
00458             (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \
00459             (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
00460             (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
00461             (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
00462 */
00463 
00464 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
00465 
00466 #ifdef CPL_MSB
00467 #  define CPL_MSBWORD16(x)      (x)
00468 #  define CPL_LSBWORD16(x)      CPL_SWAP16(x)
00469 #  define CPL_MSBWORD32(x)      (x)
00470 #  define CPL_LSBWORD32(x)      CPL_SWAP32(x)
00471 #  define CPL_MSBPTR16(x)       
00472 #  define CPL_LSBPTR16(x)       CPL_SWAP16PTR(x)
00473 #  define CPL_MSBPTR32(x)       
00474 #  define CPL_LSBPTR32(x)       CPL_SWAP32PTR(x)
00475 #  define CPL_MSBPTR64(x)       
00476 #  define CPL_LSBPTR64(x)       CPL_SWAP64PTR(x)
00477 #else
00478 #  define CPL_LSBWORD16(x)      (x)
00479 #  define CPL_MSBWORD16(x)      CPL_SWAP16(x)
00480 #  define CPL_LSBWORD32(x)      (x)
00481 #  define CPL_MSBWORD32(x)      CPL_SWAP32(x)
00482 #  define CPL_LSBPTR16(x)       
00483 #  define CPL_MSBPTR16(x)       CPL_SWAP16PTR(x)
00484 #  define CPL_LSBPTR32(x)       
00485 #  define CPL_MSBPTR32(x)       CPL_SWAP32PTR(x)
00486 #  define CPL_LSBPTR64(x)       
00487 #  define CPL_MSBPTR64(x)       CPL_SWAP64PTR(x)
00488 #endif
00489 
00491 #define CPL_LSBINT16PTR(x)    ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8))
00492 
00494 #define CPL_LSBINT32PTR(x)    ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8) | \
00495                               ((*(GByte*)((x)+2)) << 16) | ((*(GByte*)((x)+3)) << 24))
00496 
00497 
00498 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
00499 #ifndef UNREFERENCED_PARAM 
00500 #  ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
00501 #    define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
00502 #  else
00503 #    define UNREFERENCED_PARAM(param) ((void)param)
00504 #  endif /* UNREFERENCED_PARAMETER */
00505 #endif /* UNREFERENCED_PARAM */
00506 
00507 /***********************************************************************
00508  * Define CPL_CVSID() macro.  It can be disabled during a build by
00509  * defining DISABLE_CPLID in the compiler options.
00510  *
00511  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
00512  * being unused.
00513  */
00514 
00515 #ifndef DISABLE_CVSID
00516 #if defined(__GNUC__) && __GNUC__ >= 4
00517 #  define CPL_CVSID(string)     static char cpl_cvsid[] __attribute__((used)) = string;
00518 #else
00519 #  define CPL_CVSID(string)     static char cpl_cvsid[] = string; \
00520 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
00521 #endif
00522 #else
00523 #  define CPL_CVSID(string)
00524 #endif
00525 
00526 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
00527 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )  __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00528 #else
00529 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
00530 #endif
00531 
00532 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
00533 #define CPL_WARN_UNUSED_RESULT                        __attribute__((warn_unused_result))
00534 #else
00535 #define CPL_WARN_UNUSED_RESULT
00536 #endif
00537 
00538 
00539 #endif /* ndef CPL_BASE_H_INCLUDED */

Generated for GDAL by doxygen 1.6.1.