cpl_port.h

Go to the documentation of this file.
00001 /******************************************************************************
00002  * $Id: cpl_port.h 34135 2016-04-27 21:44:07Z 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  * Copyright (c) 2008-2013, Even Rouault <even dot rouault at mines-paris dot org>
00012  *
00013  * Permission is hereby granted, free of charge, to any person obtaining a
00014  * copy of this software and associated documentation files (the "Software"),
00015  * to deal in the Software without restriction, including without limitation
00016  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00017  * and/or sell copies of the Software, and to permit persons to whom the
00018  * Software is furnished to do so, subject to the following conditions:
00019  *
00020  * The above copyright notice and this permission notice shall be included
00021  * in all copies or substantial portions of the Software.
00022  *
00023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00024  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00025  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00026  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00027  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00028  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00029  * DEALINGS IN THE SOFTWARE.
00030  ****************************************************************************/
00031 
00032 #ifndef CPL_BASE_H_INCLUDED
00033 #define CPL_BASE_H_INCLUDED
00034 
00042 /* ==================================================================== */
00043 /*      We will use WIN32 as a standard windows define.                 */
00044 /* ==================================================================== */
00045 #if defined(_WIN32) && !defined(WIN32)
00046 #  define WIN32
00047 #endif
00048 
00049 #if defined(_WINDOWS) && !defined(WIN32)
00050 #  define WIN32
00051 #endif
00052 
00053 /* -------------------------------------------------------------------- */
00054 /*      The following apparently allow you to use strcpy() and other    */
00055 /*      functions judged "unsafe" by microsoft in VS 8 (2005).          */
00056 /* -------------------------------------------------------------------- */
00057 #ifdef _MSC_VER
00058 #  ifndef _CRT_SECURE_NO_DEPRECATE
00059 #    define _CRT_SECURE_NO_DEPRECATE
00060 #  endif
00061 #  ifndef _CRT_NONSTDC_NO_DEPRECATE
00062 #    define _CRT_NONSTDC_NO_DEPRECATE
00063 #  endif
00064 #endif
00065 
00066 #include "cpl_config.h"
00067 
00068 /* ==================================================================== */
00069 /*      A few sanity checks, mainly to detect problems that sometimes   */
00070 /*      arise with bad configured cross-compilation.                    */
00071 /* ==================================================================== */
00072 
00073 #if !defined(SIZEOF_INT) || SIZEOF_INT != 4
00074 #error "Unexpected value for SIZEOF_INT"
00075 #endif
00076 
00077 #if !defined(SIZEOF_UNSIGNED_LONG) || (SIZEOF_UNSIGNED_LONG != 4 && SIZEOF_UNSIGNED_LONG != 8)
00078 #error "Unexpected value for SIZEOF_UNSIGNED_LONG"
00079 #endif
00080 
00081 #if !defined(SIZEOF_VOIDP) || (SIZEOF_VOIDP != 4 && SIZEOF_VOIDP != 8)
00082 #error "Unexpected value for SIZEOF_VOIDP"
00083 #endif
00084 
00085 
00086 /* ==================================================================== */
00087 /*      This will disable most WIN32 stuff in a Cygnus build which      */
00088 /*      defines unix to 1.                                              */
00089 /* ==================================================================== */
00090 
00091 #ifdef unix
00092 #  undef WIN32
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 #include <time.h>
00137 
00138 #if defined(HAVE_ERRNO_H)
00139 #  include <errno.h>
00140 #endif
00141 
00142 #ifdef HAVE_LOCALE_H
00143 #  include <locale.h>
00144 #endif
00145 
00146 #ifdef HAVE_DIRECT_H
00147 #  include <direct.h>
00148 #endif
00149 
00150 #if !defined(WIN32)
00151 #  include <strings.h>
00152 #endif
00153 
00154 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
00155 #  define DBMALLOC
00156 #  include <dbmalloc.h>
00157 #endif
00158 
00159 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
00160 #  define USE_DMALLOC
00161 #  include <dmalloc.h>
00162 #endif
00163 
00164 /* ==================================================================== */
00165 /*      Base portability stuff ... this stuff may need to be            */
00166 /*      modified for new platforms.                                     */
00167 /* ==================================================================== */
00168 
00169 /* -------------------------------------------------------------------- */
00170 /*      Which versions of C++ are available.                            */
00171 /* -------------------------------------------------------------------- */
00172 
00173 #ifdef __cplusplus
00174 #  if __cplusplus >= 201103L
00175 #    define HAVE_CXX11 1
00176 #  endif
00177 /* TODO(schwehr): What are the correct tests for C++ 14 and 17? */
00178 #endif  /* __cpluscplus */
00179 
00180 /*---------------------------------------------------------------------
00181  *        types for 16 and 32 bits integers, etc...
00182  *--------------------------------------------------------------------*/
00183 #if UINT_MAX == 65535
00184 typedef long            GInt32;
00185 typedef unsigned long   GUInt32;
00186 #else
00187 typedef int             GInt32;
00188 typedef unsigned int    GUInt32;
00189 #endif
00190 
00191 typedef short           GInt16;
00192 typedef unsigned short  GUInt16;
00193 typedef unsigned char   GByte;
00194 /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
00195 /* in include/poppler/goo/gtypes.h */
00196 #ifndef CPL_GBOOL_DEFINED
00197 #define CPL_GBOOL_DEFINED
00198 typedef int             GBool;
00199 #endif
00200 
00201 /* -------------------------------------------------------------------- */
00202 /*      64bit support                                                   */
00203 /* -------------------------------------------------------------------- */
00204 
00205 #if defined(WIN32) && defined(_MSC_VER)
00206 
00207 #define VSI_LARGE_API_SUPPORTED
00208 typedef __int64          GIntBig;
00209 typedef unsigned __int64 GUIntBig;
00210 
00211 #define GINTBIG_MIN     ((GIntBig)(0x80000000) << 32)
00212 #define GINTBIG_MAX     (((GIntBig)(0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
00213 #define GUINTBIG_MAX     (((GUIntBig)(0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
00214 
00215 #elif HAVE_LONG_LONG
00216 
00217 typedef long long        GIntBig;
00218 typedef unsigned long long GUIntBig;
00219 
00220 #define GINTBIG_MIN     ((GIntBig)(0x80000000) << 32)
00221 #define GINTBIG_MAX     (((GIntBig)(0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
00222 #define GUINTBIG_MAX     (((GUIntBig)(0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
00223 
00224 #else
00225 
00226 typedef long             GIntBig;
00227 typedef unsigned long    GUIntBig;
00228 
00229 #define GINTBIG_MIN     INT_MIN
00230 #define GINTBIG_MAX     INT_MAX
00231 #define GUINTBIG_MAX     UINT_MAX
00232 #endif
00233 
00234 #if SIZEOF_VOIDP == 8
00235 typedef GIntBig          GPtrDiff_t;
00236 #else
00237 typedef int              GPtrDiff_t;
00238 #endif
00239 
00240 #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
00241   #define CPL_FRMT_GB_WITHOUT_PREFIX     "I64"
00242 #elif HAVE_LONG_LONG
00243   #define CPL_FRMT_GB_WITHOUT_PREFIX     "ll"
00244 #else
00245   #define CPL_FRMT_GB_WITHOUT_PREFIX     "l"
00246 #endif
00247 
00248 #define CPL_FRMT_GIB     "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
00249 #define CPL_FRMT_GUIB    "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
00250 
00251 /* Workaround VC6 bug */
00252 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
00253 #define GUINTBIG_TO_DOUBLE(x) (double)(GIntBig)(x)
00254 #else
00255 #define GUINTBIG_TO_DOUBLE(x) (double)(x)
00256 #endif
00257 
00258 #ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
00259 #define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
00260 #else
00261 #define CPL_INT64_FITS_ON_INT32(x) (((GIntBig)(int)(x)) == (x))
00262 #endif
00263 
00264 /* ==================================================================== */
00265 /*      Other standard services.                                        */
00266 /* ==================================================================== */
00267 #ifdef __cplusplus
00268 #  define CPL_C_START           extern "C" {
00269 #  define CPL_C_END             }
00270 #else
00271 #  define CPL_C_START
00272 #  define CPL_C_END
00273 #endif
00274 
00275 #ifndef CPL_DLL
00276 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
00277 #  define CPL_DLL     __declspec(dllexport)
00278 #else
00279 #  if defined(USE_GCC_VISIBILITY_FLAG)
00280 #    define CPL_DLL     __attribute__ ((visibility("default")))
00281 #  else
00282 #    define CPL_DLL
00283 #  endif
00284 #endif
00285 #endif
00286 
00287 /* Should optional (normally private) interfaces be exported? */
00288 #ifdef CPL_OPTIONAL_APIS
00289 #  define CPL_ODLL CPL_DLL
00290 #else
00291 #  define CPL_ODLL
00292 #endif
00293 
00294 #ifndef CPL_STDCALL
00295 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
00296 #  define CPL_STDCALL     __stdcall
00297 #else
00298 #  define CPL_STDCALL
00299 #endif
00300 #endif
00301 
00302 #ifdef _MSC_VER
00303 #  define FORCE_CDECL  __cdecl
00304 #else
00305 #  define FORCE_CDECL
00306 #endif
00307 
00308 /* TODO : support for other compilers needed */
00309 #if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
00310 #define HAS_CPL_INLINE  1
00311 #define CPL_INLINE __inline
00312 #elif defined(__SUNPRO_CC)
00313 #define HAS_CPL_INLINE  1
00314 #define CPL_INLINE inline
00315 #else
00316 #define CPL_INLINE
00317 #endif
00318 
00319 // Define NULL_AS_NULLPTR together with -std=c++11 -Wzero-as-null-pointer-constant with GCC
00320 // to detect misuses of NULL
00321 #if defined(NULL_AS_NULLPTR) && HAVE_CXX11
00322 
00323 #ifdef __GNUC__
00324 // We need to include all that bunch of system headers, otherwise
00325 // as they include <stddef.h> with __need_NULL, this overrides our #define NULL nullptr
00326 // with #define NULL __null
00327 #include <locale.h>
00328 #include <unistd.h>
00329 #include <sys/types.h>
00330 #ifdef HAVE_ICONV
00331 #include <iconv.h>
00332 #endif
00333 #ifdef HAVE_MMAP
00334 #include <sys/mman.h>
00335 #endif
00336 #include <signal.h>
00337 #ifndef _WIN32
00338 #include <dlfcn.h>
00339 #include <netdb.h>
00340 #include <fcntl.h>
00341 #endif
00342 
00343 extern "C++" {
00344 #include <string>
00345 #include <cstdio>
00346 #include <cstdlib>
00347 #include <cstring>
00348 #include <cstddef>
00349 #include <ostream>
00350 #include <iostream>
00351 #include <sstream>
00352 }
00353 #endif /* __GNUC__ */
00354 
00355 #undef NULL
00356 #define NULL nullptr
00357 #else /* defined(NULL_AS_NULLPTR) && HAVE_CXX11 */
00358 #ifndef NULL
00359 #  define NULL  0
00360 #endif
00361 #endif /* defined(NULL_AS_NULLPTR) && HAVE_CXX11 */
00362 
00363 
00364 #ifndef MAX
00365 #  define MIN(a,b)      ((a<b) ? a : b)
00366 #  define MAX(a,b)      ((a>b) ? a : b)
00367 #endif
00368 
00369 #ifndef ABS
00370 #  define ABS(x)        ((x<0) ? (-1*(x)) : x)
00371 #endif
00372 
00373 #ifndef M_PI
00374 # define M_PI           3.14159265358979323846
00375 /* 3.1415926535897932384626433832795 */
00376 #endif
00377 
00378 /* -------------------------------------------------------------------- */
00379 /*      Macro to test equality of two floating point values.            */
00380 /*      We use fabs() function instead of ABS() macro to avoid side     */
00381 /*      effects.                                                        */
00382 /* -------------------------------------------------------------------- */
00383 #ifndef CPLIsEqual
00384 #  define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
00385 #endif
00386 
00387 /* -------------------------------------------------------------------- */
00388 /*      Provide macros for case insensitive string comparisons.         */
00389 /* -------------------------------------------------------------------- */
00390 #ifndef EQUAL
00391 
00392 #if defined(AFL_FRIENDLY) && defined(__GNUC__)
00393 
00394 static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
00395         __attribute__((always_inline));
00396 
00397 static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
00398 {
00399     const unsigned char* bptr1 = (const unsigned char*)ptr1;
00400     const unsigned char* bptr2 = (const unsigned char*)ptr2;
00401     while( len-- )
00402     {
00403         unsigned char b1 = *(bptr1++);
00404         unsigned char b2 = *(bptr2++);
00405         if( b1 != b2 ) return b1 - b2;
00406     }
00407     return 0;
00408 }
00409 
00410 static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
00411         __attribute__((always_inline));
00412 
00413 static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
00414 {
00415     const unsigned char* usptr1 = (const unsigned char*)ptr1;
00416     const unsigned char* usptr2 = (const unsigned char*)ptr2;
00417     while( 1 )
00418     {
00419         unsigned char ch1 = *(usptr1++);
00420         unsigned char ch2 = *(usptr2++);
00421         if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
00422     }
00423 }
00424 
00425 static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
00426         __attribute__((always_inline));
00427 
00428 static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
00429 {
00430     const unsigned char* usptr1 = (const unsigned char*)ptr1;
00431     const unsigned char* usptr2 = (const unsigned char*)ptr2;
00432     while( len -- )
00433     {
00434         unsigned char ch1 = *(usptr1++);
00435         unsigned char ch2 = *(usptr2++);
00436         if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
00437     }
00438     return 0;
00439 }
00440 
00441 static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
00442         __attribute__((always_inline));
00443 
00444 static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
00445 {
00446     const unsigned char* usptr1 = (const unsigned char*)ptr1;
00447     const unsigned char* usptr2 = (const unsigned char*)ptr2;
00448     while( 1 )
00449     {
00450         unsigned char ch1 = *(usptr1++);
00451         unsigned char ch2 = *(usptr2++);
00452         ch1 = (unsigned char)toupper(ch1);
00453         ch2 = (unsigned char)toupper(ch2);
00454         if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
00455     }
00456 }
00457 
00458 static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
00459         __attribute__((always_inline));
00460 
00461 static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
00462 {
00463     const unsigned char* usptr1 = (const unsigned char*)ptr1;
00464     const unsigned char* usptr2 = (const unsigned char*)ptr2;
00465     while( len-- )
00466     {
00467         unsigned char ch1 = *(usptr1++);
00468         unsigned char ch2 = *(usptr2++);
00469         ch1 = (unsigned char)toupper(ch1);
00470         ch2 = (unsigned char)toupper(ch2);
00471         if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
00472     }
00473     return 0;
00474 }
00475 
00476 static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
00477         __attribute__((always_inline));
00478 
00479 static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
00480 {
00481     const char* ptr_haystack = haystack;
00482     while( 1 )
00483     {
00484         const char* ptr_haystack2 = ptr_haystack;
00485         const char* ptr_needle = needle;
00486         while( 1 )
00487         {
00488             char ch1 = *(ptr_haystack2++);
00489             char ch2 = *(ptr_needle++);
00490             if( ch2 == 0 )
00491                 return (char*)ptr_haystack;
00492             if( ch1 != ch2 )
00493                 break;
00494         }
00495         if( *ptr_haystack == 0 )
00496             return NULL;
00497         ptr_haystack ++;
00498     }
00499 }
00500 
00501 #undef strcmp
00502 #undef strncmp
00503 #define memcmp CPL_afl_friendly_memcmp
00504 #define strcmp CPL_afl_friendly_strcmp
00505 #define strncmp CPL_afl_friendly_strncmp
00506 #define strcasecmp CPL_afl_friendly_strcasecmp
00507 #define strncasecmp CPL_afl_friendly_strncasecmp
00508 #define strstr CPL_afl_friendly_strstr
00509 
00510 #endif /* defined(AFL_FRIENDLY) && defined(__GNUC__) */
00511 
00512 #  if defined(WIN32)
00513 #    define STRCASECMP(a,b)         (stricmp(a,b))
00514 #    define STRNCASECMP(a,b,n)      (strnicmp(a,b,n))
00515 #  else
00516 #    define STRCASECMP(a,b)         (strcasecmp(a,b))
00517 #    define STRNCASECMP(a,b,n)      (strncasecmp(a,b,n))
00518 #  endif
00519 #  define EQUALN(a,b,n)           (STRNCASECMP(a,b,n)==0)
00520 #  define EQUAL(a,b)              (STRCASECMP(a,b)==0)
00521 #endif
00522 
00523 /*---------------------------------------------------------------------
00524  * Does a string "a" start with string "b".  Search is case-sensitive or,
00525  * with CI, it is a case-insensitive comparison.
00526  *--------------------------------------------------------------------- */
00527 #ifndef STARTS_WITH_CI
00528 #define STARTS_WITH(a,b)               (strncmp(a,b,strlen(b)) == 0)
00529 #define STARTS_WITH_CI(a,b)            EQUALN(a,b,strlen(b))
00530 #endif
00531 
00532 #ifndef CPL_THREADLOCAL
00533 #  define CPL_THREADLOCAL
00534 #endif
00535 
00536 /* -------------------------------------------------------------------- */
00537 /*      Handle isnan() and isinf().  Note that isinf() and isnan()      */
00538 /*      are supposed to be macros according to C99, defined in math.h   */
00539 /*      Some systems (i.e. Tru64) don't have isinf() at all, so if      */
00540 /*      the macro is not defined we just assume nothing is infinite.    */
00541 /*      This may mean we have no real CPLIsInf() on systems with isinf()*/
00542 /*      function but no corresponding macro, but I can live with        */
00543 /*      that since it isn't that important a test.                      */
00544 /* -------------------------------------------------------------------- */
00545 #ifdef _MSC_VER
00546 #  include <float.h>
00547 #  define CPLIsNan(x) _isnan(x)
00548 #  define CPLIsInf(x) (!_isnan(x) && !_finite(x))
00549 #  define CPLIsFinite(x) _finite(x)
00550 #elif defined(HAVE_CXX11) && HAVE_CXX11 && defined(__GNUC__)
00551 /* When including <cmath> in C++11 the isnan() macro is undefined, so that */
00552 /* std::isnan() can work (#6489). This is a GCC specific workaround for now. */
00553 #  define CPLIsNan(x)    __builtin_isnan(x)
00554 #  define CPLIsInf(x)    __builtin_isinf(x)
00555 #  define CPLIsFinite(x) __builtin_isfinite(x)
00556 #else
00557 #  define CPLIsNan(x) isnan(x)
00558 #  ifdef isinf
00559 #    define CPLIsInf(x) isinf(x)
00560 #    define CPLIsFinite(x) (!isnan(x) && !isinf(x))
00561 #  elif defined(__sun__)
00562 #    include <ieeefp.h>
00563 #    define CPLIsInf(x)    (!finite(x) && !isnan(x))
00564 #    define CPLIsFinite(x) finite(x)
00565 #  else
00566 #    define CPLIsInf(x)    (0)
00567 #    define CPLIsFinite(x) (!isnan(x))
00568 #  endif
00569 #endif
00570 
00571 /*---------------------------------------------------------------------
00572  *                         CPL_LSB and CPL_MSB
00573  * Only one of these 2 macros should be defined and specifies the byte
00574  * ordering for the current platform.
00575  * This should be defined in the Makefile, but if it is not then
00576  * the default is CPL_LSB (Intel ordering, LSB first).
00577  *--------------------------------------------------------------------*/
00578 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
00579 #  define CPL_MSB
00580 #endif
00581 
00582 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
00583 #define CPL_LSB
00584 #endif
00585 
00586 #if defined(CPL_LSB)
00587 #  define CPL_IS_LSB 1
00588 #else
00589 #  define CPL_IS_LSB 0
00590 #endif
00591 
00592 #ifdef __cplusplus
00593 
00594 extern "C++" {
00595 
00596 template <bool b> struct CPLStaticAssert {};
00597 template<> struct CPLStaticAssert<true>
00598 {
00599     static void my_function() {}
00600 };
00601 
00602 } /* extern "C++" */
00603 
00604 #define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
00605 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
00606 
00607 #else  /* __cplusplus */
00608 
00609 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
00610 
00611 #endif  /* __cplusplus */
00612 
00613 /*---------------------------------------------------------------------
00614  *        Little endian <==> big endian byte swap macros.
00615  *--------------------------------------------------------------------*/
00616 
00617 #define CPL_SWAP16(x) \
00618         ((GUInt16)( \
00619             (((GUInt16)(x) & 0x00ffU) << 8) | \
00620             (((GUInt16)(x) & 0xff00U) >> 8) ))
00621 
00622 #define CPL_SWAP16PTR(x) \
00623 {                                                                 \
00624     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00625     CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2); \
00626                                                                   \
00627     byTemp = _pabyDataT[0];                                       \
00628     _pabyDataT[0] = _pabyDataT[1];                                \
00629     _pabyDataT[1] = byTemp;                                       \
00630 }
00631 
00632 #define CPL_SWAP32(x) \
00633         ((GUInt32)( \
00634             (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
00635             (((GUInt32)(x) & (GUInt32)0x0000ff00UL) <<  8) | \
00636             (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >>  8) | \
00637             (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
00638 
00639 #define CPL_SWAP32PTR(x) \
00640 {                                                                 \
00641     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00642     CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4);  \
00643                                                                   \
00644     byTemp = _pabyDataT[0];                                       \
00645     _pabyDataT[0] = _pabyDataT[3];                                \
00646     _pabyDataT[3] = byTemp;                                       \
00647     byTemp = _pabyDataT[1];                                       \
00648     _pabyDataT[1] = _pabyDataT[2];                                \
00649     _pabyDataT[2] = byTemp;                                       \
00650 }
00651 
00652 #define CPL_SWAP64PTR(x) \
00653 {                                                                 \
00654     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00655     CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
00656                                                                   \
00657     byTemp = _pabyDataT[0];                                       \
00658     _pabyDataT[0] = _pabyDataT[7];                                \
00659     _pabyDataT[7] = byTemp;                                       \
00660     byTemp = _pabyDataT[1];                                       \
00661     _pabyDataT[1] = _pabyDataT[6];                                \
00662     _pabyDataT[6] = byTemp;                                       \
00663     byTemp = _pabyDataT[2];                                       \
00664     _pabyDataT[2] = _pabyDataT[5];                                \
00665     _pabyDataT[5] = byTemp;                                       \
00666     byTemp = _pabyDataT[3];                                       \
00667     _pabyDataT[3] = _pabyDataT[4];                                \
00668     _pabyDataT[4] = byTemp;                                       \
00669 }
00670 
00671 
00672 /* Until we have a safe 64 bits integer data type defined, we'll replace
00673  * this version of the CPL_SWAP64() macro with a less efficient one.
00674  */
00675 /*
00676 #define CPL_SWAP64(x) \
00677         ((uint64)( \
00678             (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
00679             (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
00680             (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
00681             (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \
00682             (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \
00683             (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
00684             (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
00685             (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
00686 */
00687 
00688 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
00689 
00690 #ifdef CPL_MSB
00691 #  define CPL_MSBWORD16(x)      (x)
00692 #  define CPL_LSBWORD16(x)      CPL_SWAP16(x)
00693 #  define CPL_MSBWORD32(x)      (x)
00694 #  define CPL_LSBWORD32(x)      CPL_SWAP32(x)
00695 #  define CPL_MSBPTR16(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
00696 #  define CPL_LSBPTR16(x)       CPL_SWAP16PTR(x)
00697 #  define CPL_MSBPTR32(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
00698 #  define CPL_LSBPTR32(x)       CPL_SWAP32PTR(x)
00699 #  define CPL_MSBPTR64(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
00700 #  define CPL_LSBPTR64(x)       CPL_SWAP64PTR(x)
00701 #else
00702 #  define CPL_LSBWORD16(x)      (x)
00703 #  define CPL_MSBWORD16(x)      CPL_SWAP16(x)
00704 #  define CPL_LSBWORD32(x)      (x)
00705 #  define CPL_MSBWORD32(x)      CPL_SWAP32(x)
00706 #  define CPL_LSBPTR16(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
00707 #  define CPL_MSBPTR16(x)       CPL_SWAP16PTR(x)
00708 #  define CPL_LSBPTR32(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
00709 #  define CPL_MSBPTR32(x)       CPL_SWAP32PTR(x)
00710 #  define CPL_LSBPTR64(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
00711 #  define CPL_MSBPTR64(x)       CPL_SWAP64PTR(x)
00712 #endif
00713 
00715 #define CPL_LSBINT16PTR(x)    ((*(GByte*)(x)) | (*(((GByte*)(x))+1) << 8))
00716 
00718 #define CPL_LSBINT32PTR(x)    ((*(GByte*)(x)) | (*(((GByte*)(x))+1) << 8) | \
00719                               (*(((GByte*)(x))+2) << 16) | (*(((GByte*)(x))+3) << 24))
00720 
00722 #define CPL_LSBSINT16PTR(x) ((GInt16) CPL_LSBINT16PTR(x))
00723 
00725 #define CPL_LSBUINT16PTR(x) ((GUInt16)CPL_LSBINT16PTR(x))
00726 
00728 #define CPL_LSBSINT32PTR(x) ((GInt32) CPL_LSBINT32PTR(x))
00729 
00731 #define CPL_LSBUINT32PTR(x) ((GUInt32)CPL_LSBINT32PTR(x))
00732 
00733 
00734 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
00735 #ifndef UNREFERENCED_PARAM
00736 #  ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
00737 #    define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
00738 #  else
00739 #    define UNREFERENCED_PARAM(param) ((void)param)
00740 #  endif /* UNREFERENCED_PARAMETER */
00741 #endif /* UNREFERENCED_PARAM */
00742 
00743 /***********************************************************************
00744  * Define CPL_CVSID() macro.  It can be disabled during a build by
00745  * defining DISABLE_CVSID in the compiler options.
00746  *
00747  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
00748  * being unused.
00749  */
00750 
00751 #ifndef DISABLE_CVSID
00752 #if defined(__GNUC__) && __GNUC__ >= 4
00753 #  define CPL_CVSID(string)     static const char cpl_cvsid[] __attribute__((used)) = string;
00754 #else
00755 #  define CPL_CVSID(string)     static const char cpl_cvsid[] = string; \
00756 static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
00757 #endif
00758 #else
00759 #  define CPL_CVSID(string)
00760 #endif
00761 
00762 /* Null terminated variadic */
00763 /* We exclude mingw64 4.6 which seems to be broken regarding this */
00764 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
00765 #   define CPL_NULL_TERMINATED     __attribute__((__sentinel__))
00766 #else
00767 #   define CPL_NULL_TERMINATED
00768 #endif
00769 
00770 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
00771 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )  __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00772 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )  __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
00773 #else
00774 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
00775 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )
00776 #endif
00777 
00778 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
00779 #define CPL_WARN_UNUSED_RESULT                        __attribute__((warn_unused_result))
00780 #else
00781 #define CPL_WARN_UNUSED_RESULT
00782 #endif
00783 
00784 #if defined(__GNUC__) && __GNUC__ >= 4
00785 #  define CPL_UNUSED __attribute((__unused__))
00786 #else
00787 /* TODO: add cases for other compilers */
00788 #  define CPL_UNUSED
00789 #endif
00790 
00791 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
00792 #define CPL_NO_RETURN                                __attribute__((noreturn))
00793 #else
00794 #define CPL_NO_RETURN
00795 #endif
00796 
00797 /* Clang __has_attribute */
00798 #ifndef __has_attribute
00799   #define __has_attribute(x) 0  // Compatibility with non-clang compilers.
00800 #endif
00801 
00802 #if ((defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || __has_attribute(returns_nonnull)) && !defined(DOXYGEN_SKIP)
00803 #  define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
00804 #else
00805 #  define CPL_RETURNS_NONNULL
00806 #endif
00807 
00808 
00809 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
00810 #define CPL_RESTRICT __restrict__
00811 #else
00812 #define CPL_RESTRICT
00813 #endif
00814 
00815 /* Helper to remove the copy and assignment constructors so that the compiler
00816    will not generate the default versions.
00817 
00818    Must be placed in the private section of a class and should be at the end.
00819 */
00820 #ifdef __cplusplus
00821 
00822 #if HAVE_CXX11
00823 #  define CPL_FINAL final
00824 #  define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
00825     ClassName( const ClassName & ) = delete; \
00826     ClassName &operator=( const ClassName & ) = delete;
00827 #else
00828 #  define CPL_FINAL
00829 #  define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
00830     ClassName( const ClassName & ); \
00831     ClassName &operator=( const ClassName & );
00832 #endif  /* HAVE_CXX11 */
00833 
00834 #endif /* __cplusplus */
00835 
00836 #if !defined(DOXYGEN_SKIP)
00837 #if defined(__has_extension)
00838   #if __has_extension(attribute_deprecated_with_message)
00839     /* Clang extension */
00840     #define CPL_WARN_DEPRECATED(x)                       __attribute__ ((deprecated(x)))
00841   #else
00842     #define CPL_WARN_DEPRECATED(x)
00843   #endif
00844 #elif defined(__GNUC__)
00845     #define CPL_WARN_DEPRECATED(x)                       __attribute__ ((deprecated))
00846 #else
00847   #define CPL_WARN_DEPRECATED(x)
00848 #endif
00849 #endif
00850 
00851 #if !defined(_MSC_VER) && !defined(__APPLE__)
00852 CPL_C_START
00853 #ifdef WARN_STANDARD_PRINTF
00854 int vsnprintf(char *str, size_t size, const char* fmt, va_list args) CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");
00855 int snprintf(char *str, size_t size, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(3,4) CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
00856 int sprintf(char *str, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
00857 #elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
00858 int sprintf(char *str, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_DEPRECATED("Use snprintf() or CPLsnprintf() instead");
00859 #endif
00860 CPL_C_END
00861 #endif /* !defined(_MSC_VER) && !defined(__APPLE__) */
00862 
00863 #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
00864 #define CPL_CPU_REQUIRES_ALIGNED_ACCESS
00865 #define CPL_IS_DOUBLE_A_INT(d)  ( (d) >= INT_MIN && (d) <= INT_MAX && (double)(int)(d) == (d) )
00866 #else
00867 /* This is technically unspecified behaviour if the double is out of range, but works OK on x86 */
00868 #define CPL_IS_DOUBLE_A_INT(d)  ( (double)(int)(d) == (d) )
00869 #endif
00870 
00871 #ifdef __cplusplus
00872 /* The size of C style arrays. */
00873 #define CPL_ARRAYSIZE(array) \
00874   ((sizeof(array) / sizeof(*(array))) / \
00875   static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
00876 
00877 extern "C++" {
00878 template<class T> static void CPL_IGNORE_RET_VAL(T) {}
00879 inline static bool CPL_TO_BOOL(int x) { return x != 0; }
00880 } /* extern "C++" */
00881 
00882 #endif  /* __cplusplus */
00883 
00884 #if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (defined(__clang__) && __clang_major__ >= 3)) && !defined(_MSC_VER))
00885 #define HAVE_GCC_DIAGNOSTIC_PUSH
00886 #endif
00887 
00888 #if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && !defined(_MSC_VER))
00889 #define HAVE_GCC_SYSTEM_HEADER
00890 #endif
00891 
00892 #if defined(__clang__)
00893 #  define CPL_FALLTHROUGH [[clang::fallthrough]];
00894 #else
00895 #  define CPL_FALLTHROUGH
00896 #endif
00897 
00898 // Define DEBUG_BOOL to compile in "MSVC mode", ie error out when
00899 // a integer is assigned to a bool
00900 // WARNING: use only at compilation time, since it is know to not work
00901 //  at runtime for unknown reasons (crash in MongoDB driver for example)
00902 #if defined(__cplusplus) && defined(DEBUG_BOOL) && !defined(DO_NOT_USE_DEBUG_BOOL)
00903 extern "C++" {
00904 class MSVCPedanticBool
00905 {
00906 
00907         friend bool operator== (const bool& one, const MSVCPedanticBool& other);
00908         friend bool operator!= (const bool& one, const MSVCPedanticBool& other);
00909 
00910         bool b;
00911         MSVCPedanticBool(int bIn);
00912 
00913     public:
00914         /* b not initialized on purpose in default ctor to flag use. */
00915         /* cppcheck-suppress uninitMemberVar */
00916         MSVCPedanticBool() {}
00917         MSVCPedanticBool(bool bIn) : b(bIn) {}
00918         MSVCPedanticBool(const MSVCPedanticBool& other) : b(other.b) {}
00919 
00920         MSVCPedanticBool& operator= (const MSVCPedanticBool& other) { b = other.b; return *this; }
00921         MSVCPedanticBool& operator&= (const MSVCPedanticBool& other) { b &= other.b; return *this; }
00922         MSVCPedanticBool& operator|= (const MSVCPedanticBool& other) { b |= other.b; return *this; }
00923 
00924         bool operator== (const bool& other) const { return b == other; }
00925         bool operator!= (const bool& other) const { return b != other; }
00926         bool operator== (const MSVCPedanticBool& other) const { return b == other.b; }
00927         bool operator!= (const MSVCPedanticBool& other) const { return b != other.b; }
00928 
00929         bool operator! () const { return !b; }
00930         operator bool() const { return b; }
00931         operator int() const { return b; }
00932 };
00933 
00934 inline bool operator== (const bool& one, const MSVCPedanticBool& other) { return one == other.b; }
00935 inline bool operator!= (const bool& one, const MSVCPedanticBool& other) { return one != other.b; }
00936 
00937 /* We must include all C++ stuff before to avoid issues with templates that use bool */
00938 #include <vector>
00939 #include <map>
00940 #include <set>
00941 #include <string>
00942 #include <cstddef>
00943 #include <limits>
00944 #include <sstream>
00945 #include <fstream>
00946 #include <algorithm>
00947 
00948 } /* extern C++ */
00949 
00950 #undef FALSE
00951 #define FALSE false
00952 #undef TRUE
00953 #define TRUE true
00954 
00955 /* In the very few cases we really need a "simple" type, fallback to bool */
00956 #define EMULATED_BOOL int
00957 
00958 /* Use our class instead of bool */
00959 #define bool MSVCPedanticBool
00960 
00961 /* "volatile bool" with the below substitution doesn't really work. */
00962 /* Just for the sake of the debug, we don't really need volatile */
00963 #define VOLATILE_BOOL bool
00964 
00965 #else /* defined(__cplusplus) && defined(DEBUG_BOOL) */
00966 
00967 #ifndef FALSE
00968 #  define FALSE 0
00969 #endif
00970 
00971 #ifndef TRUE
00972 #  define TRUE 1
00973 #endif
00974 
00975 #define EMULATED_BOOL bool
00976 #define VOLATILE_BOOL volatile bool
00977 
00978 #endif /* defined(__cplusplus) && defined(DEBUG_BOOL) */
00979 
00980 #endif /* ndef CPL_BASE_H_INCLUDED */

Generated for GDAL by doxygen 1.6.1.