gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1198 rindas
44KB

  1. /******************************************************************************
  2. * $Id: cpl_port.h 7d34df879ac151e8cc377ecd5d0107c579b0c0a2 2019-04-25 12:22:24 +0200 Even Rouault $
  3. *
  4. * Project: CPL - Common Portability Library
  5. * Author: Frank Warmerdam, warmerdam@pobox.com
  6. * Purpose: Include file providing low level portability services for CPL.
  7. * This should be the first include file for any CPL based code.
  8. *
  9. ******************************************************************************
  10. * Copyright (c) 1998, 2005, Frank Warmerdam <warmerdam@pobox.com>
  11. * Copyright (c) 2008-2013, Even Rouault <even dot rouault at mines-paris dot org>
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included
  21. * in all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  24. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. * DEALINGS IN THE SOFTWARE.
  30. ****************************************************************************/
  31. #ifndef CPL_BASE_H_INCLUDED
  32. #define CPL_BASE_H_INCLUDED
  33. /**
  34. * \file cpl_port.h
  35. *
  36. * Core portability definitions for CPL.
  37. *
  38. */
  39. /* ==================================================================== */
  40. /* We will use WIN32 as a standard windows define. */
  41. /* ==================================================================== */
  42. #if defined(_WIN32) && !defined(WIN32)
  43. # define WIN32
  44. #endif
  45. #if defined(_WINDOWS) && !defined(WIN32)
  46. # define WIN32
  47. #endif
  48. /* -------------------------------------------------------------------- */
  49. /* The following apparently allow you to use strcpy() and other */
  50. /* functions judged "unsafe" by microsoft in VS 8 (2005). */
  51. /* -------------------------------------------------------------------- */
  52. #ifdef _MSC_VER
  53. # ifndef _CRT_SECURE_NO_DEPRECATE
  54. # define _CRT_SECURE_NO_DEPRECATE
  55. # endif
  56. # ifndef _CRT_NONSTDC_NO_DEPRECATE
  57. # define _CRT_NONSTDC_NO_DEPRECATE
  58. # endif
  59. #endif
  60. #include "cpl_config.h"
  61. /* ==================================================================== */
  62. /* A few sanity checks, mainly to detect problems that sometimes */
  63. /* arise with bad configured cross-compilation. */
  64. /* ==================================================================== */
  65. #if !defined(SIZEOF_INT) || SIZEOF_INT != 4
  66. #error "Unexpected value for SIZEOF_INT"
  67. #endif
  68. #if !defined(SIZEOF_UNSIGNED_LONG) || (SIZEOF_UNSIGNED_LONG != 4 && SIZEOF_UNSIGNED_LONG != 8)
  69. #error "Unexpected value for SIZEOF_UNSIGNED_LONG"
  70. #endif
  71. #if !defined(SIZEOF_VOIDP) || (SIZEOF_VOIDP != 4 && SIZEOF_VOIDP != 8)
  72. #error "Unexpected value for SIZEOF_VOIDP"
  73. #endif
  74. /* ==================================================================== */
  75. /* This will disable most WIN32 stuff in a Cygnus build which */
  76. /* defines unix to 1. */
  77. /* ==================================================================== */
  78. #ifdef unix
  79. # undef WIN32
  80. #endif
  81. /*! @cond Doxygen_Suppress */
  82. #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
  83. # define _LARGEFILE64_SOURCE 1
  84. #endif
  85. /* ==================================================================== */
  86. /* If iconv() is available use extended recoding module. */
  87. /* Stub implementation is always compiled in, because it works */
  88. /* faster than iconv() for encodings it supports. */
  89. /* ==================================================================== */
  90. #if defined(HAVE_ICONV)
  91. # define CPL_RECODE_ICONV
  92. #endif
  93. #define CPL_RECODE_STUB
  94. /*! @endcond */
  95. /* ==================================================================== */
  96. /* MinGW stuff */
  97. /* ==================================================================== */
  98. /* We need __MSVCRT_VERSION__ >= 0x0700 to have "_aligned_malloc" */
  99. /* Latest versions of mingw32 define it, but with older ones, */
  100. /* we need to define it manually */
  101. #if defined(__MINGW32__)
  102. #ifndef __MSVCRT_VERSION__
  103. #define __MSVCRT_VERSION__ 0x0700
  104. #endif
  105. #endif
  106. /* Needed for std=c11 on Solaris to have strcasecmp() */
  107. #if defined(GDAL_COMPILATION) && defined(__sun__) && (__STDC_VERSION__ + 0) >= 201112L && (_XOPEN_SOURCE + 0) < 600
  108. #ifdef _XOPEN_SOURCE
  109. #undef _XOPEN_SOURCE
  110. #endif
  111. #define _XOPEN_SOURCE 600
  112. #endif
  113. /* ==================================================================== */
  114. /* Standard include files. */
  115. /* ==================================================================== */
  116. #include <stdio.h>
  117. #include <stdlib.h>
  118. #include <math.h>
  119. #include <stdarg.h>
  120. #include <string.h>
  121. #include <ctype.h>
  122. #include <limits.h>
  123. #include <time.h>
  124. #if defined(HAVE_ERRNO_H)
  125. # include <errno.h>
  126. #endif
  127. #ifdef HAVE_LOCALE_H
  128. # include <locale.h>
  129. #endif
  130. #ifdef HAVE_DIRECT_H
  131. # include <direct.h>
  132. #endif
  133. #if !defined(WIN32)
  134. # include <strings.h>
  135. #endif
  136. #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
  137. # define DBMALLOC
  138. # include <dbmalloc.h>
  139. #endif
  140. #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
  141. # define USE_DMALLOC
  142. # include <dmalloc.h>
  143. #endif
  144. /* ==================================================================== */
  145. /* Base portability stuff ... this stuff may need to be */
  146. /* modified for new platforms. */
  147. /* ==================================================================== */
  148. /* -------------------------------------------------------------------- */
  149. /* Which versions of C++ are available. */
  150. /* -------------------------------------------------------------------- */
  151. /* MSVC fails to define a decent value of __cplusplus. Try to target VS2015*/
  152. /* as a minimum */
  153. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  154. # if !(__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900))
  155. # error Must have C++11 or newer.
  156. # endif
  157. # if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)
  158. # define HAVE_CXX14 1
  159. # endif
  160. # if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  161. # define HAVE_CXX17 1
  162. # endif
  163. #endif /* __cplusplus */
  164. /*---------------------------------------------------------------------
  165. * types for 16 and 32 bits integers, etc...
  166. *--------------------------------------------------------------------*/
  167. #if UINT_MAX == 65535
  168. typedef long GInt32;
  169. typedef unsigned long GUInt32;
  170. #else
  171. /** Int32 type */
  172. typedef int GInt32;
  173. /** Unsigned int32 type */
  174. typedef unsigned int GUInt32;
  175. #endif
  176. /** Int16 type */
  177. typedef short GInt16;
  178. /** Unsigned int16 type */
  179. typedef unsigned short GUInt16;
  180. /** Unsigned byte type */
  181. typedef unsigned char GByte;
  182. /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
  183. /* in include/poppler/goo/gtypes.h */
  184. #ifndef CPL_GBOOL_DEFINED
  185. /*! @cond Doxygen_Suppress */
  186. #define CPL_GBOOL_DEFINED
  187. /*! @endcond */
  188. /** Type for boolean values (alias to int) */
  189. typedef int GBool;
  190. #endif
  191. /*! @cond Doxygen_Suppress */
  192. #ifdef __cplusplus
  193. #define CPL_STATIC_CAST(type, expr) static_cast<type>(expr)
  194. #define CPL_REINTERPRET_CAST(type, expr) reinterpret_cast<type>(expr)
  195. #else
  196. #define CPL_STATIC_CAST(type, expr) ((type)(expr))
  197. #define CPL_REINTERPRET_CAST(type, expr) ((type)(expr))
  198. #endif
  199. /*! @endcond */
  200. /* -------------------------------------------------------------------- */
  201. /* 64bit support */
  202. /* -------------------------------------------------------------------- */
  203. #if defined(WIN32) && defined(_MSC_VER)
  204. #define VSI_LARGE_API_SUPPORTED
  205. #endif
  206. #if HAVE_LONG_LONG
  207. /** Large signed integer type (generally 64-bit integer type).
  208. * Use GInt64 when exactly 64 bit is needed */
  209. typedef long long GIntBig;
  210. /** Large unsigned integer type (generally 64-bit unsigned integer type).
  211. * Use GUInt64 when exactly 64 bit is needed */
  212. typedef unsigned long long GUIntBig;
  213. /** Minimum GIntBig value */
  214. #define GINTBIG_MIN (CPL_STATIC_CAST(GIntBig, 0x80000000) << 32)
  215. /** Maximum GIntBig value */
  216. #define GINTBIG_MAX ((CPL_STATIC_CAST(GIntBig, 0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
  217. /** Maximum GUIntBig value */
  218. #define GUINTBIG_MAX ((CPL_STATIC_CAST(GUIntBig, 0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
  219. /*! @cond Doxygen_Suppress */
  220. #define CPL_HAS_GINT64 1
  221. /*! @endcond */
  222. /* Note: we might want to use instead int64_t / uint64_t if they are available */
  223. /** Signed 64 bit integer type */
  224. typedef GIntBig GInt64;
  225. /** Unsigned 64 bit integer type */
  226. typedef GUIntBig GUInt64;
  227. /** Minimum GInt64 value */
  228. #define GINT64_MIN GINTBIG_MIN
  229. /** Maximum GInt64 value */
  230. #define GINT64_MAX GINTBIG_MAX
  231. /** Minimum GUInt64 value */
  232. #define GUINT64_MAX GUINTBIG_MAX
  233. #else
  234. #error "64bit integer support required"
  235. #endif
  236. #if SIZEOF_VOIDP == 8
  237. /** Integer type large enough to hold the difference between 2 addresses */
  238. typedef GIntBig GPtrDiff_t;
  239. #else
  240. /** Integer type large enough to hold the difference between 2 addresses */
  241. typedef int GPtrDiff_t;
  242. #endif
  243. #ifdef GDAL_COMPILATION
  244. #if HAVE_UINTPTR_T
  245. #include <stdint.h>
  246. typedef uintptr_t GUIntptr_t;
  247. #elif SIZEOF_VOIDP == 8
  248. typedef GUIntBig GUIntptr_t;
  249. #else
  250. typedef unsigned int GUIntptr_t;
  251. #endif
  252. #define CPL_IS_ALIGNED(ptr, quant) ((CPL_REINTERPRET_CAST(GUIntptr_t, CPL_STATIC_CAST(const void*, ptr)) % (quant)) == 0)
  253. #endif
  254. #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
  255. #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
  256. #elif HAVE_LONG_LONG
  257. /** Printf formatting suffix for GIntBig */
  258. #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
  259. #else
  260. #define CPL_FRMT_GB_WITHOUT_PREFIX "l"
  261. #endif
  262. /** Printf formatting for GIntBig */
  263. #define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
  264. /** Printf formatting for GUIntBig */
  265. #define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
  266. /*! @cond Doxygen_Suppress */
  267. #define GUINTBIG_TO_DOUBLE(x) CPL_STATIC_CAST(double, x)
  268. /*! @endcond */
  269. /*! @cond Doxygen_Suppress */
  270. #ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
  271. #define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
  272. #else
  273. #define CPL_INT64_FITS_ON_INT32(x) (CPL_STATIC_CAST(GIntBig, CPL_STATIC_CAST(int, x)) == (x))
  274. #endif
  275. /*! @endcond */
  276. /* ==================================================================== */
  277. /* Other standard services. */
  278. /* ==================================================================== */
  279. #ifdef __cplusplus
  280. /** Macro to start a block of C symbols */
  281. # define CPL_C_START extern "C" {
  282. /** Macro to end a block of C symbols */
  283. # define CPL_C_END }
  284. #else
  285. # define CPL_C_START
  286. # define CPL_C_END
  287. #endif
  288. #ifndef CPL_DLL
  289. #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
  290. # define CPL_DLL __declspec(dllexport)
  291. # define CPL_INTERNAL
  292. #else
  293. # if defined(USE_GCC_VISIBILITY_FLAG)
  294. # define CPL_DLL __attribute__ ((visibility("default")))
  295. # if !defined(__MINGW32__)
  296. # define CPL_INTERNAL __attribute__((visibility("hidden")))
  297. # else
  298. # define CPL_INTERNAL
  299. # endif
  300. # else
  301. # define CPL_DLL
  302. # define CPL_INTERNAL
  303. # endif
  304. #endif
  305. #endif
  306. /*! @cond Doxygen_Suppress */
  307. /* Should optional (normally private) interfaces be exported? */
  308. #ifdef CPL_OPTIONAL_APIS
  309. # define CPL_ODLL CPL_DLL
  310. #else
  311. # define CPL_ODLL
  312. #endif
  313. /*! @endcond */
  314. #ifndef CPL_STDCALL
  315. #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
  316. # define CPL_STDCALL __stdcall
  317. #else
  318. # define CPL_STDCALL
  319. #endif
  320. #endif
  321. /*! @cond Doxygen_Suppress */
  322. #ifdef _MSC_VER
  323. # define FORCE_CDECL __cdecl
  324. #else
  325. # define FORCE_CDECL
  326. #endif
  327. /*! @endcond */
  328. /*! @cond Doxygen_Suppress */
  329. /* TODO : support for other compilers needed */
  330. #if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
  331. #define HAS_CPL_INLINE 1
  332. #define CPL_INLINE __inline
  333. #elif defined(__SUNPRO_CC)
  334. #define HAS_CPL_INLINE 1
  335. #define CPL_INLINE inline
  336. #else
  337. #define CPL_INLINE
  338. #endif
  339. /*! @endcond*/
  340. #ifndef MAX
  341. /** Macro to compute the minimum of 2 values */
  342. # define MIN(a,b) (((a)<(b)) ? (a) : (b))
  343. /** Macro to compute the maximum of 2 values */
  344. # define MAX(a,b) (((a)>(b)) ? (a) : (b))
  345. #endif
  346. #ifndef ABS
  347. /** Macro to compute the absolute value */
  348. # define ABS(x) (((x)<0) ? (-1*(x)) : (x))
  349. #endif
  350. #ifndef M_PI
  351. /** PI definition */
  352. # define M_PI 3.14159265358979323846
  353. /* 3.1415926535897932384626433832795 */
  354. #endif
  355. /* -------------------------------------------------------------------- */
  356. /* Macro to test equality of two floating point values. */
  357. /* We use fabs() function instead of ABS() macro to avoid side */
  358. /* effects. */
  359. /* -------------------------------------------------------------------- */
  360. /*! @cond Doxygen_Suppress */
  361. #ifndef CPLIsEqual
  362. # define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
  363. #endif
  364. /*! @endcond */
  365. /* -------------------------------------------------------------------- */
  366. /* Provide macros for case insensitive string comparisons. */
  367. /* -------------------------------------------------------------------- */
  368. #ifndef EQUAL
  369. #if defined(AFL_FRIENDLY) && defined(__GNUC__)
  370. static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
  371. __attribute__((always_inline));
  372. static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
  373. {
  374. const unsigned char* bptr1 = (const unsigned char*)ptr1;
  375. const unsigned char* bptr2 = (const unsigned char*)ptr2;
  376. while( len-- )
  377. {
  378. unsigned char b1 = *(bptr1++);
  379. unsigned char b2 = *(bptr2++);
  380. if( b1 != b2 ) return b1 - b2;
  381. }
  382. return 0;
  383. }
  384. static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
  385. __attribute__((always_inline));
  386. static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
  387. {
  388. const unsigned char* usptr1 = (const unsigned char*)ptr1;
  389. const unsigned char* usptr2 = (const unsigned char*)ptr2;
  390. while( 1 )
  391. {
  392. unsigned char ch1 = *(usptr1++);
  393. unsigned char ch2 = *(usptr2++);
  394. if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
  395. }
  396. }
  397. static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
  398. __attribute__((always_inline));
  399. static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
  400. {
  401. const unsigned char* usptr1 = (const unsigned char*)ptr1;
  402. const unsigned char* usptr2 = (const unsigned char*)ptr2;
  403. while( len -- )
  404. {
  405. unsigned char ch1 = *(usptr1++);
  406. unsigned char ch2 = *(usptr2++);
  407. if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
  408. }
  409. return 0;
  410. }
  411. static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
  412. __attribute__((always_inline));
  413. static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
  414. {
  415. const unsigned char* usptr1 = (const unsigned char*)ptr1;
  416. const unsigned char* usptr2 = (const unsigned char*)ptr2;
  417. while( 1 )
  418. {
  419. unsigned char ch1 = *(usptr1++);
  420. unsigned char ch2 = *(usptr2++);
  421. ch1 = (unsigned char)toupper(ch1);
  422. ch2 = (unsigned char)toupper(ch2);
  423. if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
  424. }
  425. }
  426. static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
  427. __attribute__((always_inline));
  428. static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
  429. {
  430. const unsigned char* usptr1 = (const unsigned char*)ptr1;
  431. const unsigned char* usptr2 = (const unsigned char*)ptr2;
  432. while( len-- )
  433. {
  434. unsigned char ch1 = *(usptr1++);
  435. unsigned char ch2 = *(usptr2++);
  436. ch1 = (unsigned char)toupper(ch1);
  437. ch2 = (unsigned char)toupper(ch2);
  438. if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
  439. }
  440. return 0;
  441. }
  442. static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
  443. __attribute__((always_inline));
  444. static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
  445. {
  446. const char* ptr_haystack = haystack;
  447. while( 1 )
  448. {
  449. const char* ptr_haystack2 = ptr_haystack;
  450. const char* ptr_needle = needle;
  451. while( 1 )
  452. {
  453. char ch1 = *(ptr_haystack2++);
  454. char ch2 = *(ptr_needle++);
  455. if( ch2 == 0 )
  456. return (char*)ptr_haystack;
  457. if( ch1 != ch2 )
  458. break;
  459. }
  460. if( *ptr_haystack == 0 )
  461. return NULL;
  462. ptr_haystack ++;
  463. }
  464. }
  465. #undef strcmp
  466. #undef strncmp
  467. #define memcmp CPL_afl_friendly_memcmp
  468. #define strcmp CPL_afl_friendly_strcmp
  469. #define strncmp CPL_afl_friendly_strncmp
  470. #define strcasecmp CPL_afl_friendly_strcasecmp
  471. #define strncasecmp CPL_afl_friendly_strncasecmp
  472. #define strstr CPL_afl_friendly_strstr
  473. #endif /* defined(AFL_FRIENDLY) && defined(__GNUC__) */
  474. # if defined(WIN32)
  475. # define STRCASECMP(a,b) (stricmp(a,b))
  476. # define STRNCASECMP(a,b,n) (strnicmp(a,b,n))
  477. # else
  478. /** Alias for strcasecmp() */
  479. # define STRCASECMP(a,b) (strcasecmp(a,b))
  480. /** Alias for strncasecmp() */
  481. # define STRNCASECMP(a,b,n) (strncasecmp(a,b,n))
  482. # endif
  483. /** Alias for strncasecmp() == 0 */
  484. # define EQUALN(a,b,n) (STRNCASECMP(a,b,n)==0)
  485. /** Alias for strcasecmp() == 0 */
  486. # define EQUAL(a,b) (STRCASECMP(a,b)==0)
  487. #endif
  488. /*---------------------------------------------------------------------
  489. * Does a string "a" start with string "b". Search is case-sensitive or,
  490. * with CI, it is a case-insensitive comparison.
  491. *--------------------------------------------------------------------- */
  492. #ifndef STARTS_WITH_CI
  493. /** Returns whether a starts with b */
  494. #define STARTS_WITH(a,b) (strncmp(a,b,strlen(b)) == 0)
  495. /** Returns whether a starts with b (case insensitive comparison) */
  496. #define STARTS_WITH_CI(a,b) EQUALN(a,b,strlen(b))
  497. #endif
  498. /*! @cond Doxygen_Suppress */
  499. #ifndef CPL_THREADLOCAL
  500. # define CPL_THREADLOCAL
  501. #endif
  502. /*! @endcond */
  503. /* -------------------------------------------------------------------- */
  504. /* Handle isnan() and isinf(). Note that isinf() and isnan() */
  505. /* are supposed to be macros according to C99, defined in math.h */
  506. /* Some systems (i.e. Tru64) don't have isinf() at all, so if */
  507. /* the macro is not defined we just assume nothing is infinite. */
  508. /* This may mean we have no real CPLIsInf() on systems with isinf()*/
  509. /* function but no corresponding macro, but I can live with */
  510. /* that since it isn't that important a test. */
  511. /* -------------------------------------------------------------------- */
  512. #ifdef _MSC_VER
  513. # include <float.h>
  514. # define CPLIsNan(x) _isnan(x)
  515. # define CPLIsInf(x) (!_isnan(x) && !_finite(x))
  516. # define CPLIsFinite(x) _finite(x)
  517. #elif defined(__GNUC__) && ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 4 ) )
  518. /* When including <cmath> in C++11 the isnan() macro is undefined, so that */
  519. /* std::isnan() can work (#6489). This is a GCC specific workaround for now. */
  520. # define CPLIsNan(x) __builtin_isnan(x)
  521. # define CPLIsInf(x) __builtin_isinf(x)
  522. # define CPLIsFinite(x) __builtin_isfinite(x)
  523. #elif defined(__cplusplus) && defined(HAVE_STD_IS_NAN) && HAVE_STD_IS_NAN
  524. extern "C++" {
  525. #ifndef DOXYGEN_SKIP
  526. #include <cmath>
  527. #endif
  528. static inline int CPLIsNan(float f) { return std::isnan(f); }
  529. static inline int CPLIsNan(double f) { return std::isnan(f); }
  530. static inline int CPLIsInf(float f) { return std::isinf(f); }
  531. static inline int CPLIsInf(double f) { return std::isinf(f); }
  532. static inline int CPLIsFinite(float f) { return std::isfinite(f); }
  533. static inline int CPLIsFinite(double f) { return std::isfinite(f); }
  534. }
  535. #else
  536. /** Return whether a floating-pointer number is NaN */
  537. #if defined(__cplusplus) && defined(__GNUC__) && defined(__linux) && !defined(__ANDROID__) && !defined(CPL_SUPRESS_CPLUSPLUS)
  538. /* so to not get warning about conversion from double to float with */
  539. /* gcc -Wfloat-conversion when using isnan()/isinf() macros */
  540. extern "C++" {
  541. static inline int CPLIsNan(float f) { return __isnanf(f); }
  542. static inline int CPLIsNan(double f) { return __isnan(f); }
  543. static inline int CPLIsInf(float f) { return __isinff(f); }
  544. static inline int CPLIsInf(double f) { return __isinf(f); }
  545. static inline int CPLIsFinite(float f) { return !__isnanf(f) && !__isinff(f); }
  546. static inline int CPLIsFinite(double f) { return !__isnan(f) && !__isinf(f); }
  547. }
  548. #else
  549. # define CPLIsNan(x) isnan(x)
  550. # if defined(isinf) || defined(__FreeBSD__)
  551. /** Return whether a floating-pointer number is +/- infinty */
  552. # define CPLIsInf(x) isinf(x)
  553. /** Return whether a floating-pointer number is finite */
  554. # define CPLIsFinite(x) (!isnan(x) && !isinf(x))
  555. # elif defined(__sun__)
  556. # include <ieeefp.h>
  557. # define CPLIsInf(x) (!finite(x) && !isnan(x))
  558. # define CPLIsFinite(x) finite(x)
  559. # else
  560. # define CPLIsInf(x) (0)
  561. # define CPLIsFinite(x) (!isnan(x))
  562. # endif
  563. #endif
  564. #endif
  565. /*! @cond Doxygen_Suppress */
  566. /*---------------------------------------------------------------------
  567. * CPL_LSB and CPL_MSB
  568. * Only one of these 2 macros should be defined and specifies the byte
  569. * ordering for the current platform.
  570. * This should be defined in the Makefile, but if it is not then
  571. * the default is CPL_LSB (Intel ordering, LSB first).
  572. *--------------------------------------------------------------------*/
  573. #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
  574. # define CPL_MSB
  575. #endif
  576. #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
  577. #define CPL_LSB
  578. #endif
  579. #if defined(CPL_LSB)
  580. # define CPL_IS_LSB 1
  581. #else
  582. # define CPL_IS_LSB 0
  583. #endif
  584. /*! @endcond */
  585. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  586. /*! @cond Doxygen_Suppress */
  587. extern "C++" {
  588. template <bool b> struct CPLStaticAssert {};
  589. template<> struct CPLStaticAssert<true>
  590. {
  591. static void my_function() {}
  592. };
  593. } /* extern "C++" */
  594. #define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
  595. #define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
  596. #else /* __cplusplus */
  597. #define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
  598. #endif /* __cplusplus */
  599. /*! @endcond */
  600. /*---------------------------------------------------------------------
  601. * Little endian <==> big endian byte swap macros.
  602. *--------------------------------------------------------------------*/
  603. /** Byte-swap a 16bit unsigned integer */
  604. #define CPL_SWAP16(x) CPL_STATIC_CAST(GUInt16, (CPL_STATIC_CAST(GUInt16, x) << 8) | (CPL_STATIC_CAST(GUInt16, x) >> 8) )
  605. #if defined(HAVE_GCC_BSWAP) && (defined(__i386__) || defined(__x86_64__))
  606. /* Could potentially be extended to other architectures but must be checked */
  607. /* that the intrinsic is indeed efficient */
  608. /* GCC (at least 4.6 or above) need that include */
  609. #include <x86intrin.h>
  610. /** Byte-swap a 32bit unsigned integer */
  611. #define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, __builtin_bswap32(CPL_STATIC_CAST(GUInt32, x)))
  612. /** Byte-swap a 64bit unsigned integer */
  613. #define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, __builtin_bswap64(CPL_STATIC_CAST(GUInt64, x)))
  614. #elif defined(_MSC_VER)
  615. #define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, _byteswap_ulong(CPL_STATIC_CAST(GUInt32, x)))
  616. #define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, _byteswap_uint64(CPL_STATIC_CAST(GUInt64, x)))
  617. #else
  618. /** Byte-swap a 32bit unsigned integer */
  619. #define CPL_SWAP32(x) \
  620. CPL_STATIC_CAST(GUInt32, \
  621. ((CPL_STATIC_CAST(GUInt32, x) & 0x000000ffU) << 24) | \
  622. ((CPL_STATIC_CAST(GUInt32, x) & 0x0000ff00U) << 8) | \
  623. ((CPL_STATIC_CAST(GUInt32, x) & 0x00ff0000U) >> 8) | \
  624. ((CPL_STATIC_CAST(GUInt32, x) & 0xff000000U) >> 24) )
  625. /** Byte-swap a 64bit unsigned integer */
  626. #define CPL_SWAP64(x) \
  627. ((CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, x))) << 32) | \
  628. (CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, CPL_STATIC_CAST(GUInt64, x) >> 32)))))
  629. #endif
  630. /** Byte-swap a 16 bit pointer */
  631. #define CPL_SWAP16PTR(x) \
  632. { \
  633. GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
  634. CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2); \
  635. \
  636. byTemp = _pabyDataT[0]; \
  637. _pabyDataT[0] = _pabyDataT[1]; \
  638. _pabyDataT[1] = byTemp; \
  639. }
  640. #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
  641. /** Byte-swap a 32 bit pointer */
  642. #define CPL_SWAP32PTR(x) \
  643. { \
  644. GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
  645. CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
  646. \
  647. byTemp = _pabyDataT[0]; \
  648. _pabyDataT[0] = _pabyDataT[3]; \
  649. _pabyDataT[3] = byTemp; \
  650. byTemp = _pabyDataT[1]; \
  651. _pabyDataT[1] = _pabyDataT[2]; \
  652. _pabyDataT[2] = byTemp; \
  653. }
  654. /** Byte-swap a 64 bit pointer */
  655. #define CPL_SWAP64PTR(x) \
  656. { \
  657. GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
  658. CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
  659. \
  660. byTemp = _pabyDataT[0]; \
  661. _pabyDataT[0] = _pabyDataT[7]; \
  662. _pabyDataT[7] = byTemp; \
  663. byTemp = _pabyDataT[1]; \
  664. _pabyDataT[1] = _pabyDataT[6]; \
  665. _pabyDataT[6] = byTemp; \
  666. byTemp = _pabyDataT[2]; \
  667. _pabyDataT[2] = _pabyDataT[5]; \
  668. _pabyDataT[5] = byTemp; \
  669. byTemp = _pabyDataT[3]; \
  670. _pabyDataT[3] = _pabyDataT[4]; \
  671. _pabyDataT[4] = byTemp; \
  672. }
  673. #else
  674. /** Byte-swap a 32 bit pointer */
  675. #define CPL_SWAP32PTR(x) \
  676. { \
  677. GUInt32 _n32; \
  678. void* _lx = x; \
  679. memcpy(&_n32, _lx, 4); \
  680. CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
  681. _n32 = CPL_SWAP32(_n32); \
  682. memcpy(_lx, &_n32, 4); \
  683. }
  684. /** Byte-swap a 64 bit pointer */
  685. #define CPL_SWAP64PTR(x) \
  686. { \
  687. GUInt64 _n64; \
  688. void* _lx = x; \
  689. memcpy(&_n64, _lx, 8); \
  690. CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
  691. _n64 = CPL_SWAP64(_n64); \
  692. memcpy(_lx, &_n64, 8); \
  693. }
  694. #endif
  695. /** Byte-swap a 64 bit pointer */
  696. #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
  697. #ifdef CPL_MSB
  698. # define CPL_MSBWORD16(x) (x)
  699. # define CPL_LSBWORD16(x) CPL_SWAP16(x)
  700. # define CPL_MSBWORD32(x) (x)
  701. # define CPL_LSBWORD32(x) CPL_SWAP32(x)
  702. # define CPL_MSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
  703. # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
  704. # define CPL_MSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
  705. # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
  706. # define CPL_MSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
  707. # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
  708. #else
  709. /** Return a 16bit word from a originally LSB ordered word */
  710. # define CPL_LSBWORD16(x) (x)
  711. /** Return a 16bit word from a originally MSB ordered word */
  712. # define CPL_MSBWORD16(x) CPL_SWAP16(x)
  713. /** Return a 32bit word from a originally LSB ordered word */
  714. # define CPL_LSBWORD32(x) (x)
  715. /** Return a 32bit word from a originally MSB ordered word */
  716. # define CPL_MSBWORD32(x) CPL_SWAP32(x)
  717. /** Byte-swap if necessary a 16bit word at the location pointed from a originally LSB ordered pointer */
  718. # define CPL_LSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
  719. /** Byte-swap if necessary a 16bit word at the location pointed from a originally MSB ordered pointer */
  720. # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
  721. /** Byte-swap if necessary a 32bit word at the location pointed from a originally LSB ordered pointer */
  722. # define CPL_LSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
  723. /** Byte-swap if necessary a 32bit word at the location pointed from a originally MSB ordered pointer */
  724. # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
  725. /** Byte-swap if necessary a 64bit word at the location pointed from a originally LSB ordered pointer */
  726. # define CPL_LSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
  727. /** Byte-swap if necessary a 64bit word at the location pointed from a originally MSB ordered pointer */
  728. # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
  729. #endif
  730. /** Return a Int16 from the 2 bytes ordered in LSB order at address x.
  731. * @deprecated Use rather CPL_LSBSINT16PTR or CPL_LSBUINT16PTR for explicit
  732. * signedness. */
  733. #define CPL_LSBINT16PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8))
  734. /** Return a Int32 from the 4 bytes ordered in LSB order at address x.
  735. * @deprecated Use rather CPL_LSBSINT32PTR or CPL_LSBUINT32PTR for explicit
  736. * signedness. */
  737. #define CPL_LSBINT32PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8) | \
  738. (*((CPL_REINTERPRET_CAST(const GByte*, x))+2) << 16) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+3) << 24))
  739. /** Return a signed Int16 from the 2 bytes ordered in LSB order at address x */
  740. #define CPL_LSBSINT16PTR(x) CPL_STATIC_CAST(GInt16,CPL_LSBINT16PTR(x))
  741. /** Return a unsigned Int16 from the 2 bytes ordered in LSB order at address x */
  742. #define CPL_LSBUINT16PTR(x) CPL_STATIC_CAST(GUInt16, CPL_LSBINT16PTR(x))
  743. /** Return a signed Int32 from the 4 bytes ordered in LSB order at address x */
  744. #define CPL_LSBSINT32PTR(x) CPL_STATIC_CAST(GInt32, CPL_LSBINT32PTR(x))
  745. /** Return a unsigned Int32 from the 4 bytes ordered in LSB order at address x */
  746. #define CPL_LSBUINT32PTR(x) CPL_STATIC_CAST(GUInt32, CPL_LSBINT32PTR(x))
  747. /*! @cond Doxygen_Suppress */
  748. /* Utility macro to explicitly mark intentionally unreferenced parameters. */
  749. #ifndef UNREFERENCED_PARAM
  750. # ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
  751. # define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
  752. # else
  753. # define UNREFERENCED_PARAM(param) ((void)param)
  754. # endif /* UNREFERENCED_PARAMETER */
  755. #endif /* UNREFERENCED_PARAM */
  756. /*! @endcond */
  757. /***********************************************************************
  758. * Define CPL_CVSID() macro. It can be disabled during a build by
  759. * defining DISABLE_CVSID in the compiler options.
  760. *
  761. * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
  762. * being unused.
  763. */
  764. /*! @cond Doxygen_Suppress */
  765. #ifndef DISABLE_CVSID
  766. #if defined(__GNUC__) && __GNUC__ >= 4
  767. # define CPL_CVSID(string) static const char cpl_cvsid[] __attribute__((used)) = string;
  768. #else
  769. # define CPL_CVSID(string) static const char cpl_cvsid[] = string; \
  770. static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
  771. #endif
  772. #else
  773. # define CPL_CVSID(string)
  774. #endif
  775. /*! @endcond */
  776. /* We exclude mingw64 4.6 which seems to be broken regarding this */
  777. #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
  778. /** Null terminated variadic */
  779. # define CPL_NULL_TERMINATED __attribute__((__sentinel__))
  780. #else
  781. /** Null terminated variadic */
  782. # define CPL_NULL_TERMINATED
  783. #endif
  784. #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
  785. /** Tag a function to have printf() formatting */
  786. #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
  787. /** Tag a function to have scanf() formatting */
  788. #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
  789. #else
  790. /** Tag a function to have printf() formatting */
  791. #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
  792. /** Tag a function to have scanf() formatting */
  793. #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )
  794. #endif
  795. #if defined(_MSC_VER) && (defined(GDAL_COMPILATION) || defined(CPL_ENABLE_MSVC_ANNOTATIONS))
  796. #include <sal.h>
  797. /** Macro into which to wrap the format argument of a printf-like function.
  798. * Only used if ANALYZE=1 is specified to nmake */
  799. # define CPL_FORMAT_STRING(arg) _Printf_format_string_ arg
  800. /** Macro into which to wrap the format argument of a sscanf-like function.
  801. * Only used if ANALYZE=1 is specified to nmake */
  802. # define CPL_SCANF_FORMAT_STRING(arg) _Scanf_format_string_ arg
  803. #else
  804. /** Macro into which to wrap the format argument of a printf-like function */
  805. # define CPL_FORMAT_STRING(arg) arg
  806. /** Macro into which to wrap the format argument of a sscanf-like function. */
  807. # define CPL_SCANF_FORMAT_STRING(arg) arg
  808. #endif /* defined(_MSC_VER) && defined(GDAL_COMPILATION) */
  809. #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
  810. /** Qualifier to warn when the return value of a function is not used */
  811. #define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  812. #else
  813. /** Qualifier to warn when the return value of a function is not used */
  814. #define CPL_WARN_UNUSED_RESULT
  815. #endif
  816. #if defined(__GNUC__) && __GNUC__ >= 4
  817. /** Qualifier for an argument that is unused */
  818. # define CPL_UNUSED __attribute((__unused__))
  819. #else
  820. /* TODO: add cases for other compilers */
  821. /** Qualifier for an argument that is unused */
  822. # define CPL_UNUSED
  823. #endif
  824. #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
  825. /** Qualifier for a function that does not return at all (terminates the process) */
  826. #define CPL_NO_RETURN __attribute__((noreturn))
  827. #else
  828. /** Qualifier for a function that does not return at all (terminates the process) */
  829. #define CPL_NO_RETURN
  830. #endif
  831. /*! @cond Doxygen_Suppress */
  832. /* Clang __has_attribute */
  833. #ifndef __has_attribute
  834. #define __has_attribute(x) 0 // Compatibility with non-clang compilers.
  835. #endif
  836. /*! @endcond */
  837. #if ((defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || __has_attribute(returns_nonnull)) && !defined(DOXYGEN_SKIP) && !defined(__INTEL_COMPILER)
  838. /** Qualifier for a function that does not return NULL */
  839. # define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
  840. #else
  841. /** Qualifier for a function that does not return NULL */
  842. # define CPL_RETURNS_NONNULL
  843. #endif
  844. #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
  845. /** restrict keyword to declare that pointers do not alias */
  846. #define CPL_RESTRICT __restrict__
  847. #else
  848. /** restrict keyword to declare that pointers do not alias */
  849. #define CPL_RESTRICT
  850. #endif
  851. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  852. /** To be used in public headers only. For non-public headers or .cpp files,
  853. * use override directly. */
  854. # define CPL_OVERRIDE override
  855. /** C++11 final qualifier */
  856. # define CPL_FINAL final
  857. /** Helper to remove the copy and assignment constructors so that the compiler
  858. will not generate the default versions.
  859. Must be placed in the private section of a class and should be at the end.
  860. */
  861. # define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
  862. ClassName( const ClassName & ) = delete; \
  863. ClassName &operator=( const ClassName & ) = delete;
  864. #endif /* __cplusplus */
  865. #if !defined(DOXYGEN_SKIP)
  866. #if defined(__has_extension)
  867. #if __has_extension(attribute_deprecated_with_message)
  868. /* Clang extension */
  869. #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated(x)))
  870. #else
  871. #define CPL_WARN_DEPRECATED(x)
  872. #endif
  873. #elif defined(__GNUC__)
  874. #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated))
  875. #else
  876. #define CPL_WARN_DEPRECATED(x)
  877. #endif
  878. #endif
  879. #if !defined(_MSC_VER) && !defined(__APPLE__) && !defined(_FORTIFY_SOURCE)
  880. CPL_C_START
  881. # if defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF)
  882. int vsnprintf(char *str, size_t size, const char* fmt, va_list args)
  883. CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");
  884. int snprintf(char *str, size_t size, const char* fmt, ...)
  885. CPL_PRINT_FUNC_FORMAT(3,4)
  886. CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
  887. int sprintf(char *str, const char* fmt, ...)
  888. CPL_PRINT_FUNC_FORMAT(2, 3)
  889. CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
  890. # elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
  891. int sprintf(char *str, const char* fmt, ...)
  892. CPL_PRINT_FUNC_FORMAT(2, 3)
  893. CPL_WARN_DEPRECATED("Use snprintf() or CPLsnprintf() instead");
  894. # endif /* defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF) */
  895. CPL_C_END
  896. #endif /* !defined(_MSC_VER) && !defined(__APPLE__) */
  897. #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
  898. /*! @cond Doxygen_Suppress */
  899. #define CPL_CPU_REQUIRES_ALIGNED_ACCESS
  900. /*! @endcond */
  901. #endif
  902. #if defined(__cplusplus)
  903. /** Returns the size of C style arrays. */
  904. #define CPL_ARRAYSIZE(array) \
  905. ((sizeof(array) / sizeof(*(array))) / \
  906. static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
  907. extern "C++" {
  908. template<class T> static void CPL_IGNORE_RET_VAL(T) {}
  909. inline static bool CPL_TO_BOOL(int x) { return x != 0; }
  910. } /* extern "C++" */
  911. #endif /* __cplusplus */
  912. #if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (defined(__clang__) && __clang_major__ >= 3)) && !defined(_MSC_VER))
  913. #define HAVE_GCC_DIAGNOSTIC_PUSH
  914. #endif
  915. #if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && !defined(_MSC_VER))
  916. #define HAVE_GCC_SYSTEM_HEADER
  917. #endif
  918. #if ((defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))) || __GNUC__ >= 7)
  919. /** Macro for fallthrough in a switch case construct */
  920. # define CPL_FALLTHROUGH [[clang::fallthrough]];
  921. #else
  922. /** Macro for fallthrough in a switch case construct */
  923. # define CPL_FALLTHROUGH
  924. #endif
  925. /*! @cond Doxygen_Suppress */
  926. // Define DEBUG_BOOL to compile in "MSVC mode", ie error out when
  927. // a integer is assigned to a bool
  928. // WARNING: use only at compilation time, since it is know to not work
  929. // at runtime for unknown reasons (crash in MongoDB driver for example)
  930. #if defined(__cplusplus) && defined(DEBUG_BOOL) && !defined(DO_NOT_USE_DEBUG_BOOL) && !defined(CPL_SUPRESS_CPLUSPLUS)
  931. extern "C++" {
  932. class MSVCPedanticBool
  933. {
  934. friend bool operator== (const bool& one, const MSVCPedanticBool& other);
  935. friend bool operator!= (const bool& one, const MSVCPedanticBool& other);
  936. bool b;
  937. MSVCPedanticBool(int bIn);
  938. public:
  939. /* b not initialized on purpose in default ctor to flag use. */
  940. /* cppcheck-suppress uninitMemberVar */
  941. MSVCPedanticBool() {}
  942. MSVCPedanticBool(bool bIn) : b(bIn) {}
  943. MSVCPedanticBool(const MSVCPedanticBool& other) : b(other.b) {}
  944. MSVCPedanticBool& operator= (const MSVCPedanticBool& other) { b = other.b; return *this; }
  945. MSVCPedanticBool& operator&= (const MSVCPedanticBool& other) { b &= other.b; return *this; }
  946. MSVCPedanticBool& operator|= (const MSVCPedanticBool& other) { b |= other.b; return *this; }
  947. bool operator== (const bool& other) const { return b == other; }
  948. bool operator!= (const bool& other) const { return b != other; }
  949. bool operator< (const bool& other) const { return b < other; }
  950. bool operator== (const MSVCPedanticBool& other) const { return b == other.b; }
  951. bool operator!= (const MSVCPedanticBool& other) const { return b != other.b; }
  952. bool operator< (const MSVCPedanticBool& other) const { return b < other.b; }
  953. bool operator! () const { return !b; }
  954. operator bool() const { return b; }
  955. operator int() const { return b; }
  956. operator GIntBig() const { return b; }
  957. };
  958. inline bool operator== (const bool& one, const MSVCPedanticBool& other) { return one == other.b; }
  959. inline bool operator!= (const bool& one, const MSVCPedanticBool& other) { return one != other.b; }
  960. /* We must include all C++ stuff before to avoid issues with templates that use bool */
  961. #include <vector>
  962. #include <map>
  963. #include <set>
  964. #include <string>
  965. #include <cstddef>
  966. #include <limits>
  967. #include <sstream>
  968. #include <fstream>
  969. #include <algorithm>
  970. #include <functional>
  971. #include <memory>
  972. #include <queue>
  973. #include <mutex>
  974. #include <unordered_map>
  975. #include <thread>
  976. #include <unordered_set>
  977. #include <complex>
  978. #include <iomanip>
  979. } /* extern C++ */
  980. #undef FALSE
  981. #define FALSE false
  982. #undef TRUE
  983. #define TRUE true
  984. /* In the very few cases we really need a "simple" type, fallback to bool */
  985. #define EMULATED_BOOL int
  986. /* Use our class instead of bool */
  987. #define bool MSVCPedanticBool
  988. /* "volatile bool" with the below substitution doesn't really work. */
  989. /* Just for the sake of the debug, we don't really need volatile */
  990. #define VOLATILE_BOOL bool
  991. #else /* defined(__cplusplus) && defined(DEBUG_BOOL) */
  992. #ifndef FALSE
  993. # define FALSE 0
  994. #endif
  995. #ifndef TRUE
  996. # define TRUE 1
  997. #endif
  998. #define EMULATED_BOOL bool
  999. #define VOLATILE_BOOL volatile bool
  1000. #endif /* defined(__cplusplus) && defined(DEBUG_BOOL) */
  1001. #if __clang_major__ >= 4 || (__clang_major__ == 3 && __clang_minor__ >= 8)
  1002. #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
  1003. #else
  1004. #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  1005. #endif
  1006. /*! @endcond */
  1007. /*! @cond Doxygen_Suppress */
  1008. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  1009. #define CPL_NULLPTR nullptr
  1010. #else
  1011. #define CPL_NULLPTR NULL
  1012. #endif
  1013. /*! @endcond */
  1014. /* This typedef is for C functions that take char** as argument, but */
  1015. /* with the semantics of a const list. In C, char** is not implicitly cast to */
  1016. /* const char* const*, contrary to C++. So when seen for C++, it is OK */
  1017. /* to expose the prototyes as const char* const*, but for C we keep the */
  1018. /* historical definition to avoid warnings. */
  1019. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && !defined(DOXYGEN_SKIP)
  1020. /** Type of a constant null-terminated list of nul terminated strings.
  1021. * Seen as char** from C and const char* const* from C++ */
  1022. typedef const char* const* CSLConstList;
  1023. #else
  1024. /** Type of a constant null-terminated list of nul terminated strings.
  1025. * Seen as char** from C and const char* const* from C++ */
  1026. typedef char** CSLConstList;
  1027. #endif
  1028. #endif /* ndef CPL_BASE_H_INCLUDED */
上海开阖软件有限公司 沪ICP备12045867号-1