GDAL

cpl_port.h
Go to the documentation of this file.
1 /******************************************************************************
2 * $Id$
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 spatialys.com>
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
32 #ifndef CPL_BASE_H_INCLUDED
33 #define CPL_BASE_H_INCLUDED
34
42 /* ==================================================================== */
43 /* We will use WIN32 as a standard windows define. */
44 /* ==================================================================== */
45 #if defined(_WIN32) && !defined(WIN32)
46 # define WIN32
47 #endif
48
49 #if defined(_WINDOWS) && !defined(WIN32)
50 # define WIN32
51 #endif
52
53 /* -------------------------------------------------------------------- */
54 /* The following apparently allow you to use strcpy() and other */
55 /* functions judged "unsafe" by microsoft in VS 8 (2005). */
56 /* -------------------------------------------------------------------- */
57 #ifdef _MSC_VER
58 # ifndef _CRT_SECURE_NO_DEPRECATE
59 # define _CRT_SECURE_NO_DEPRECATE
60 # endif
61 # ifndef _CRT_NONSTDC_NO_DEPRECATE
62 # define _CRT_NONSTDC_NO_DEPRECATE
63 # endif
64 #endif
65
66 #include "cpl_config.h"
67
68 /* ==================================================================== */
69 /* A few sanity checks, mainly to detect problems that sometimes */
70 /* arise with bad configured cross-compilation. */
71 /* ==================================================================== */
72
73 #if !defined(SIZEOF_INT) || SIZEOF_INT != 4
74 #error "Unexpected value for SIZEOF_INT"
75 #endif
76
77 #if !defined(SIZEOF_UNSIGNED_LONG) || (SIZEOF_UNSIGNED_LONG != 4 && SIZEOF_UNSIGNED_LONG != 8)
78 #error "Unexpected value for SIZEOF_UNSIGNED_LONG"
79 #endif
80
81 #if !defined(SIZEOF_VOIDP) || (SIZEOF_VOIDP != 4 && SIZEOF_VOIDP != 8)
82 #error "Unexpected value for SIZEOF_VOIDP"
83 #endif
84
85 /* ==================================================================== */
86 /* This will disable most WIN32 stuff in a Cygnus build which */
87 /* defines unix to 1. */
88 /* ==================================================================== */
89
90 #ifdef unix
91 # undef WIN32
92 #endif
93
95 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
96 # define _LARGEFILE64_SOURCE 1
97 #endif
98
99 /* ==================================================================== */
100 /* If iconv() is available use extended recoding module. */
101 /* Stub implementation is always compiled in, because it works */
102 /* faster than iconv() for encodings it supports. */
103 /* ==================================================================== */
104
105 #if defined(HAVE_ICONV)
106 # define CPL_RECODE_ICONV
107 #endif
108
109 #define CPL_RECODE_STUB
110
112 /* ==================================================================== */
113 /* MinGW stuff */
114 /* ==================================================================== */
115
116 /* We need __MSVCRT_VERSION__ >= 0x0700 to have "_aligned_malloc" */
117 /* Latest versions of mingw32 define it, but with older ones, */
118 /* we need to define it manually */
119 #if defined(__MINGW32__)
120 #ifndef __MSVCRT_VERSION__
121 #define __MSVCRT_VERSION__ 0x0700
122 #endif
123 #endif
124
125 /* Needed for std=c11 on Solaris to have strcasecmp() */
126 #if defined(GDAL_COMPILATION) && defined(__sun__) && (__STDC_VERSION__ + 0) >= 201112L && (_XOPEN_SOURCE + 0) < 600
127 #ifdef _XOPEN_SOURCE
128 #undef _XOPEN_SOURCE
129 #endif
130 #define _XOPEN_SOURCE 600
131 #endif
132
133 /* ==================================================================== */
134 /* Standard include files. */
135 /* ==================================================================== */
136
137 #include <stdio.h>
138 #include <stdlib.h>
139 #include <math.h>
140 #include <stdarg.h>
141 #include <string.h>
142 #include <ctype.h>
143 #include <limits.h>
144
145 #include <time.h>
146
147 #if defined(HAVE_ERRNO_H)
148 # include <errno.h>
149 #endif
150
151 #ifdef HAVE_LOCALE_H
152 # include <locale.h>
153 #endif
154
155 #ifdef HAVE_DIRECT_H
156 # include <direct.h>
157 #endif
158
159 #if !defined(WIN32)
160 # include <strings.h>
161 #endif
162
163 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
164 # define DBMALLOC
165 # include <dbmalloc.h>
166 #endif
167
168 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
169 # define USE_DMALLOC
170 # include <dmalloc.h>
171 #endif
172
173 /* ==================================================================== */
174 /* Base portability stuff ... this stuff may need to be */
175 /* modified for new platforms. */
176 /* ==================================================================== */
177
178 /* -------------------------------------------------------------------- */
179 /* Which versions of C++ are available. */
180 /* -------------------------------------------------------------------- */
181
182 /* MSVC fails to define a decent value of __cplusplus. Try to target VS2015*/
183 /* as a minimum */
184
185 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
186 # if !(__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900))
187 # error Must have C++11 or newer.
188 # endif
189 # if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)
190 # define HAVE_CXX14 1
191 # endif
192 # if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
193 # define HAVE_CXX17 1
194 # endif
195 #endif /* __cplusplus */
196
197 /*---------------------------------------------------------------------
198 * types for 16 and 32 bits integers, etc...
199 *--------------------------------------------------------------------*/
200 #if UINT_MAX == 65535
201 typedef long GInt32;
202 typedef unsigned long GUInt32;
203 #else
204
205 typedef int GInt32;
207 typedef unsigned int GUInt32;
208 #endif
209
211 typedef short GInt16;
213 typedef unsigned short GUInt16;
215 typedef unsigned char GByte;
216 /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
217 /* in include/poppler/goo/gtypes.h */
218 #ifndef CPL_GBOOL_DEFINED
219
220 #define CPL_GBOOL_DEFINED
221
223 typedef int GBool;
224 #endif
225
227 #ifdef __cplusplus
228 #define CPL_STATIC_CAST(type, expr) static_cast<type>(expr)
229 #define CPL_REINTERPRET_CAST(type, expr) reinterpret_cast<type>(expr)
230 #else
231 #define CPL_STATIC_CAST(type, expr) ((type)(expr))
232 #define CPL_REINTERPRET_CAST(type, expr) ((type)(expr))
233 #endif
234
236 /* -------------------------------------------------------------------- */
237 /* 64bit support */
238 /* -------------------------------------------------------------------- */
239
240 #if HAVE_LONG_LONG
241
244 typedef long long GIntBig;
247 typedef unsigned long long GUIntBig;
248
250 #define GINTBIG_MIN (CPL_STATIC_CAST(GIntBig, 0x80000000) << 32)
251
252 #define GINTBIG_MAX ((CPL_STATIC_CAST(GIntBig, 0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
253
254 #define GUINTBIG_MAX ((CPL_STATIC_CAST(GUIntBig, 0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
255
257 #define CPL_HAS_GINT64 1
258
260 /* Note: we might want to use instead int64_t / uint64_t if they are available */
261
263 typedef GIntBig GInt64;
265 typedef GUIntBig GUInt64;
266
268 #define GINT64_MIN GINTBIG_MIN
269
270 #define GINT64_MAX GINTBIG_MAX
271
272 #define GUINT64_MAX GUINTBIG_MAX
273
274 #else
275
276 #error "64bit integer support required"
277
278 #endif
279
280 #if SIZEOF_VOIDP == 8
281
282 typedef GIntBig GPtrDiff_t;
283 #else
284
285 typedef int GPtrDiff_t;
286 #endif
287
288 #ifdef GDAL_COMPILATION
289 #if HAVE_UINTPTR_T
290 #include <stdint.h>
291 typedef uintptr_t GUIntptr_t;
292 #elif SIZEOF_VOIDP == 8
293 typedef GUIntBig GUIntptr_t;
294 #else
295 typedef unsigned int GUIntptr_t;
296 #endif
297
298 #define CPL_IS_ALIGNED(ptr, quant) ((CPL_REINTERPRET_CAST(GUIntptr_t, CPL_STATIC_CAST(const void*, ptr)) % (quant)) == 0)
299
300 #endif
301
302 #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
303 #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
304 #elif HAVE_LONG_LONG
305
306 #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
307 #else
308 #define CPL_FRMT_GB_WITHOUT_PREFIX "l"
309 #endif
310
312 #define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
313
314 #define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
315
317 #ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
318 #define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
319 #else
320 #define CPL_INT64_FITS_ON_INT32(x) (CPL_STATIC_CAST(GIntBig, CPL_STATIC_CAST(int, x)) == (x))
321 #endif
322
324 /* ==================================================================== */
325 /* Other standard services. */
326 /* ==================================================================== */
327 #ifdef __cplusplus
328
329 # define CPL_C_START extern "C" {
330
331 # define CPL_C_END }
332 #else
333 # define CPL_C_START
334 # define CPL_C_END
335 #endif
336
337 #ifndef CPL_DLL
338 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
339 # ifdef GDAL_COMPILATION
340 # define CPL_DLL __declspec(dllexport)
341 # else
342 # define CPL_DLL
343 # endif
344 # define CPL_INTERNAL
345 #else
346 # if defined(USE_GCC_VISIBILITY_FLAG)
347 # define CPL_DLL __attribute__ ((visibility("default")))
348 # if !defined(__MINGW32__)
349 # define CPL_INTERNAL __attribute__((visibility("hidden")))
350 # else
351 # define CPL_INTERNAL
352 # endif
353 # else
354 # define CPL_DLL
355 # define CPL_INTERNAL
356 # endif
357 #endif
358
359 // Marker for unstable API
360 #define CPL_UNSTABLE_API CPL_DLL
361
362 #endif
363
365 /* Should optional (normally private) interfaces be exported? */
366 #ifdef CPL_OPTIONAL_APIS
367 # define CPL_ODLL CPL_DLL
368 #else
369 # define CPL_ODLL
370 #endif
371
373 #ifndef CPL_STDCALL
374 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
375 # define CPL_STDCALL __stdcall
376 #else
377 # define CPL_STDCALL
378 #endif
379 #endif
380
382 #ifdef _MSC_VER
383 # define FORCE_CDECL __cdecl
384 #else
385 # define FORCE_CDECL
386 #endif
387
390 /* TODO : support for other compilers needed */
391 #if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
392 #define HAS_CPL_INLINE 1
393 #define CPL_INLINE __inline
394 #elif defined(__SUNPRO_CC)
395 #define HAS_CPL_INLINE 1
396 #define CPL_INLINE inline
397 #else
398 #define CPL_INLINE
399 #endif
400
402 #ifndef MAX
403
404 # define MIN(a,b) (((a)<(b)) ? (a) : (b))
405
406 # define MAX(a,b) (((a)>(b)) ? (a) : (b))
407 #endif
408
409 #ifndef ABS
410
411 # define ABS(x) (((x)<0) ? (-1*(x)) : (x))
412 #endif
413
414 #ifndef M_PI
415
416 # define M_PI 3.14159265358979323846
417 /* 3.1415926535897932384626433832795 */
418 #endif
419
420 /* -------------------------------------------------------------------- */
421 /* Macro to test equality of two floating point values. */
422 /* We use fabs() function instead of ABS() macro to avoid side */
423 /* effects. */
424 /* -------------------------------------------------------------------- */
426 #ifndef CPLIsEqual
427 # define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
428 #endif
429
431 /* -------------------------------------------------------------------- */
432 /* Provide macros for case insensitive string comparisons. */
433 /* -------------------------------------------------------------------- */
434 #ifndef EQUAL
435
436 #if defined(AFL_FRIENDLY) && defined(__GNUC__)
437
438 static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
439 __attribute__((always_inline));
440
441 static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
442 {
443 const unsigned char* bptr1 = (const unsigned char*)ptr1;
444 const unsigned char* bptr2 = (const unsigned char*)ptr2;
445 while( len-- )
446 {
447 unsigned char b1 = *(bptr1++);
448 unsigned char b2 = *(bptr2++);
449 if( b1 != b2 ) return b1 - b2;
450 }
451 return 0;
452 }
453
454 static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
455 __attribute__((always_inline));
456
457 static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
458 {
459 const unsigned char* usptr1 = (const unsigned char*)ptr1;
460 const unsigned char* usptr2 = (const unsigned char*)ptr2;
461 while( 1 )
462 {
463 unsigned char ch1 = *(usptr1++);
464 unsigned char ch2 = *(usptr2++);
465 if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
466 }
467 }
468
469 static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
470 __attribute__((always_inline));
471
472 static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
473 {
474 const unsigned char* usptr1 = (const unsigned char*)ptr1;
475 const unsigned char* usptr2 = (const unsigned char*)ptr2;
476 while( len -- )
477 {
478 unsigned char ch1 = *(usptr1++);
479 unsigned char ch2 = *(usptr2++);
480 if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
481 }
482 return 0;
483 }
484
485 static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
486 __attribute__((always_inline));
487
488 static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
489 {
490 const unsigned char* usptr1 = (const unsigned char*)ptr1;
491 const unsigned char* usptr2 = (const unsigned char*)ptr2;
492 while( 1 )
493 {
494 unsigned char ch1 = *(usptr1++);
495 unsigned char ch2 = *(usptr2++);
496 ch1 = (unsigned char)toupper(ch1);
497 ch2 = (unsigned char)toupper(ch2);
498 if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
499 }
500 }
501
502 static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
503 __attribute__((always_inline));
504
505 static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
506 {
507 const unsigned char* usptr1 = (const unsigned char*)ptr1;
508 const unsigned char* usptr2 = (const unsigned char*)ptr2;
509 while( len-- )
510 {
511 unsigned char ch1 = *(usptr1++);
512 unsigned char ch2 = *(usptr2++);
513 ch1 = (unsigned char)toupper(ch1);
514 ch2 = (unsigned char)toupper(ch2);
515 if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
516 }
517 return 0;
518 }
519
520 static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
521 __attribute__((always_inline));
522
523 static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
524 {
525 const char* ptr_haystack = haystack;
526 while( 1 )
527 {
528 const char* ptr_haystack2 = ptr_haystack;
529 const char* ptr_needle = needle;
530 while( 1 )
531 {
532 char ch1 = *(ptr_haystack2++);
533 char ch2 = *(ptr_needle++);
534 if( ch2 == 0 )
535 return (char*)ptr_haystack;
536 if( ch1 != ch2 )
537 break;
538 }
539 if( *ptr_haystack == 0 )
540 return NULL;
541 ptr_haystack ++;
542 }
543 }
544
545 #undef strcmp
546 #undef strncmp
547 #define memcmp CPL_afl_friendly_memcmp
548 #define strcmp CPL_afl_friendly_strcmp
549 #define strncmp CPL_afl_friendly_strncmp
550 #define strcasecmp CPL_afl_friendly_strcasecmp
551 #define strncasecmp CPL_afl_friendly_strncasecmp
552 #define strstr CPL_afl_friendly_strstr
553
554 #endif /* defined(AFL_FRIENDLY) && defined(__GNUC__) */
555
556 # if defined(WIN32)
557 # define STRCASECMP(a,b) (_stricmp(a,b))
558 # define STRNCASECMP(a,b,n) (_strnicmp(a,b,n))
559 # else
560
561 # define STRCASECMP(a,b) (strcasecmp(a,b))
562
563 # define STRNCASECMP(a,b,n) (strncasecmp(a,b,n))
564 # endif
565
566 # define EQUALN(a,b,n) (STRNCASECMP(a,b,n)==0)
567
568 # define EQUAL(a,b) (STRCASECMP(a,b)==0)
569 #endif
570
571 /*---------------------------------------------------------------------
572 * Does a string "a" start with string "b". Search is case-sensitive or,
573 * with CI, it is a case-insensitive comparison.
574 *--------------------------------------------------------------------- */
575 #ifndef STARTS_WITH_CI
576
577 #define STARTS_WITH(a,b) (strncmp(a,b,strlen(b)) == 0)
578
579 #define STARTS_WITH_CI(a,b) EQUALN(a,b,strlen(b))
580 #endif
581
583 #ifndef CPL_THREADLOCAL
584 # define CPL_THREADLOCAL
585 #endif
586
588 /* -------------------------------------------------------------------- */
589 /* Handle isnan() and isinf(). Note that isinf() and isnan() */
590 /* are supposed to be macros according to C99, defined in math.h */
591 /* Some systems (i.e. Tru64) don't have isinf() at all, so if */
592 /* the macro is not defined we just assume nothing is infinite. */
593 /* This may mean we have no real CPLIsInf() on systems with isinf()*/
594 /* function but no corresponding macro, but I can live with */
595 /* that since it isn't that important a test. */
596 /* -------------------------------------------------------------------- */
597 #ifdef _MSC_VER
598 # include <float.h>
599 # define CPLIsNan(x) _isnan(x)
600 # define CPLIsInf(x) (!_isnan(x) && !_finite(x))
601 # define CPLIsFinite(x) _finite(x)
602 #elif defined(__GNUC__) && ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 4 ) )
603 /* When including <cmath> in C++11 the isnan() macro is undefined, so that */
604 /* std::isnan() can work (#6489). This is a GCC specific workaround for now. */
605 # define CPLIsNan(x) __builtin_isnan(x)
606 # define CPLIsInf(x) __builtin_isinf(x)
607 # define CPLIsFinite(x) __builtin_isfinite(x)
608 #elif defined(__cplusplus) && defined(HAVE_STD_IS_NAN) && HAVE_STD_IS_NAN
609 extern "C++" {
610 #ifndef DOXYGEN_SKIP
611 #include <cmath>
612 #endif
613 static inline int CPLIsNan(float f) { return std::isnan(f); }
614 static inline int CPLIsNan(double f) { return std::isnan(f); }
615 static inline int CPLIsInf(float f) { return std::isinf(f); }
616 static inline int CPLIsInf(double f) { return std::isinf(f); }
617 static inline int CPLIsFinite(float f) { return std::isfinite(f); }
618 static inline int CPLIsFinite(double f) { return std::isfinite(f); }
619 }
620 #else
621
622 #if defined(__cplusplus) && defined(__GNUC__) && defined(__linux) && !defined(__ANDROID__) && !defined(CPL_SUPRESS_CPLUSPLUS)
623 /* so to not get warning about conversion from double to float with */
624 /* gcc -Wfloat-conversion when using isnan()/isinf() macros */
625 extern "C++" {
626 static inline int CPLIsNan(float f) { return __isnanf(f); }
627 static inline int CPLIsNan(double f) { return __isnan(f); }
628 static inline int CPLIsInf(float f) { return __isinff(f); }
629 static inline int CPLIsInf(double f) { return __isinf(f); }
630 static inline int CPLIsFinite(float f) { return !__isnanf(f) && !__isinff(f); }
631 static inline int CPLIsFinite(double f) { return !__isnan(f) && !__isinf(f); }
632 }
633 #else
634 # define CPLIsNan(x) isnan(x)
635 # if defined(isinf) || defined(__FreeBSD__)
636
637 # define CPLIsInf(x) isinf(x)
638
639 # define CPLIsFinite(x) (!isnan(x) && !isinf(x))
640 # elif defined(__sun__)
641 # include <ieeefp.h>
642 # define CPLIsInf(x) (!finite(x) && !isnan(x))
643 # define CPLIsFinite(x) finite(x)
644 # else
645 # define CPLIsInf(x) (0)
646 # define CPLIsFinite(x) (!isnan(x))
647 # endif
648 #endif
649 #endif
650
652 /*---------------------------------------------------------------------
653 * CPL_LSB and CPL_MSB
654 * Only one of these 2 macros should be defined and specifies the byte
655 * ordering for the current platform.
656 * This should be defined in the Makefile, but if it is not then
657 * the default is CPL_LSB (Intel ordering, LSB first).
658 *--------------------------------------------------------------------*/
659 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
660 # define CPL_MSB
661 #endif
662
663 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
664 #define CPL_LSB
665 #endif
666
667 #if defined(CPL_LSB)
668 # define CPL_IS_LSB 1
669 #else
670 # define CPL_IS_LSB 0
671 #endif
672
674 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
675
677 extern "C++" {
678
679 template <bool b> struct CPLStaticAssert {};
680 template<> struct CPLStaticAssert<true>
681 {
682 static void my_function() {}
683 };
684
685 } /* extern "C++" */
686
687 #define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
688 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
689
690 #else /* __cplusplus */
691
692 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
693
694 #endif /* __cplusplus */
695
697 /*---------------------------------------------------------------------
698 * Little endian <==> big endian byte swap macros.
699 *--------------------------------------------------------------------*/
700
702 #define CPL_SWAP16(x) CPL_STATIC_CAST(GUInt16, (CPL_STATIC_CAST(GUInt16, x) << 8) | (CPL_STATIC_CAST(GUInt16, x) >> 8) )
703
704 #if defined(HAVE_GCC_BSWAP)
705
706 #define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, __builtin_bswap32(CPL_STATIC_CAST(GUInt32, x)))
707
708 #define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, __builtin_bswap64(CPL_STATIC_CAST(GUInt64, x)))
709 #elif defined(_MSC_VER)
710 #define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, _byteswap_ulong(CPL_STATIC_CAST(GUInt32, x)))
711 #define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, _byteswap_uint64(CPL_STATIC_CAST(GUInt64, x)))
712 #else
713
714 #define CPL_SWAP32(x) \
715 CPL_STATIC_CAST(GUInt32, \
716 ((CPL_STATIC_CAST(GUInt32, x) & 0x000000ffU) << 24) | \
717 ((CPL_STATIC_CAST(GUInt32, x) & 0x0000ff00U) << 8) | \
718 ((CPL_STATIC_CAST(GUInt32, x) & 0x00ff0000U) >> 8) | \
719 ((CPL_STATIC_CAST(GUInt32, x) & 0xff000000U) >> 24) )
720
722 #define CPL_SWAP64(x) \
723 ((CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, x))) << 32) | \
724 (CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, CPL_STATIC_CAST(GUInt64, x) >> 32)))))
725
726 #endif
727
729 #define CPL_SWAP16PTR(x) \
730 do { \
731 GUInt16 _n16; \
732 void* _lx = x; \
733 memcpy(&_n16, _lx, 2); \
734 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2); \
735 _n16 = CPL_SWAP16(_n16); \
736 memcpy(_lx, &_n16, 2); \
737 } while(0)
738
740 #define CPL_SWAP32PTR(x) \
741 do { \
742 GUInt32 _n32; \
743 void* _lx = x; \
744 memcpy(&_n32, _lx, 4); \
745 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
746 _n32 = CPL_SWAP32(_n32); \
747 memcpy(_lx, &_n32, 4); \
748 } while(0)
749
751 #define CPL_SWAP64PTR(x) \
752 do { \
753 GUInt64 _n64; \
754 void* _lx = x; \
755 memcpy(&_n64, _lx, 8); \
756 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
757 _n64 = CPL_SWAP64(_n64); \
758 memcpy(_lx, &_n64, 8); \
759 } while(0)
760
762 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
763
764 #ifdef CPL_MSB
765 # define CPL_MSBWORD16(x) (x)
766 # define CPL_LSBWORD16(x) CPL_SWAP16(x)
767 # define CPL_MSBWORD32(x) (x)
768 # define CPL_LSBWORD32(x) CPL_SWAP32(x)
769 # define CPL_MSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
770 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
771 # define CPL_MSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
772 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
773 # define CPL_MSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
774 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
775 #else
776
777 # define CPL_LSBWORD16(x) (x)
778
779 # define CPL_MSBWORD16(x) CPL_SWAP16(x)
780
781 # define CPL_LSBWORD32(x) (x)
782
783 # define CPL_MSBWORD32(x) CPL_SWAP32(x)
784
785 # define CPL_LSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
786
787 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
788
789 # define CPL_LSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
790
791 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
792
793 # define CPL_LSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
794
795 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
796 #endif
797
801 #define CPL_LSBINT16PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8))
802
806 #define CPL_LSBINT32PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8) | \
807 (*((CPL_REINTERPRET_CAST(const GByte*, x))+2) << 16) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+3) << 24))
808
810 #define CPL_LSBSINT16PTR(x) CPL_STATIC_CAST(GInt16,CPL_LSBINT16PTR(x))
811
813 #define CPL_LSBUINT16PTR(x) CPL_STATIC_CAST(GUInt16, CPL_LSBINT16PTR(x))
814
816 #define CPL_LSBSINT32PTR(x) CPL_STATIC_CAST(GInt32, CPL_LSBINT32PTR(x))
817
819 #define CPL_LSBUINT32PTR(x) CPL_STATIC_CAST(GUInt32, CPL_LSBINT32PTR(x))
820
822 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
823 #ifndef UNREFERENCED_PARAM
824 # ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
825 # define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
826 # else
827 # define UNREFERENCED_PARAM(param) ((void)param)
828 # endif /* UNREFERENCED_PARAMETER */
829 #endif /* UNREFERENCED_PARAM */
830
832 /***********************************************************************
833 * Define CPL_CVSID() macro. It can be disabled during a build by
834 * defining DISABLE_CVSID in the compiler options.
835 *
836 * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
837 * being unused.
838 */
839
841 #ifndef DISABLE_CVSID
842 #if defined(__GNUC__) && __GNUC__ >= 4
843 # define CPL_CVSID(string) static const char cpl_cvsid[] __attribute__((used)) = string;
844 #else
845 # define CPL_CVSID(string) static const char cpl_cvsid[] = string; \
846 static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
847 #endif
848 #else
849 # define CPL_CVSID(string)
850 #endif
851
853 /* We exclude mingw64 4.6 which seems to be broken regarding this */
854 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
855
856 # define CPL_NULL_TERMINATED __attribute__((__sentinel__))
857 #else
858
859 # define CPL_NULL_TERMINATED
860 #endif
861
862 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
863
864 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
865
866 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
867 #else
868
869 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
870
871 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )
872 #endif
873
874 #if defined(_MSC_VER) && (defined(GDAL_COMPILATION) || defined(CPL_ENABLE_MSVC_ANNOTATIONS))
875 #include <sal.h>
878 # define CPL_FORMAT_STRING(arg) _Printf_format_string_ arg
879
881 # define CPL_SCANF_FORMAT_STRING(arg) _Scanf_format_string_ arg
882 #else
883
884 # define CPL_FORMAT_STRING(arg) arg
885
886 # define CPL_SCANF_FORMAT_STRING(arg) arg
887 #endif /* defined(_MSC_VER) && defined(GDAL_COMPILATION) */
888
889 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
890
891 #define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
892 #else
893
894 #define CPL_WARN_UNUSED_RESULT
895 #endif
896
897 #if defined(__GNUC__) && __GNUC__ >= 4
898
899 # define CPL_UNUSED __attribute((__unused__))
900 #else
901 /* TODO: add cases for other compilers */
903 # define CPL_UNUSED
904 #endif
905
906 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
907
908 #define CPL_NO_RETURN __attribute__((noreturn))
909 #else
910
911 #define CPL_NO_RETURN
912 #endif
913
915 /* Clang __has_attribute */
916 #ifndef __has_attribute
917 #define __has_attribute(x) 0 // Compatibility with non-clang compilers.
918 #endif
919
922 #if ((defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || __has_attribute(returns_nonnull)) && !defined(DOXYGEN_SKIP) && !defined(__INTEL_COMPILER)
923
924 # define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
925 #else
926
927 # define CPL_RETURNS_NONNULL
928 #endif
929
930 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
931
932 #define CPL_RESTRICT __restrict__
933 #else
934
935 #define CPL_RESTRICT
936 #endif
937
938 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
939
942 # define CPL_OVERRIDE override
943
945 # define CPL_FINAL final
946
948 # define CPL_NON_FINAL
949
955 # define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
956 ClassName( const ClassName & ) = delete; \
957 ClassName &operator=( const ClassName & ) = delete;
958
959 #endif /* __cplusplus */
960
961 #if !defined(DOXYGEN_SKIP) && !defined(CPL_WARN_DEPRECATED)
962 #if defined(__has_extension)
963 #if __has_extension(attribute_deprecated_with_message)
964 /* Clang extension */
965 #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated(x)))
966 #else
967 #define CPL_WARN_DEPRECATED(x)
968 #endif
969 #elif defined(__GNUC__)
970 #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated))
971 #else
972 #define CPL_WARN_DEPRECATED(x)
973 #endif
974 #endif
975
976 #if !defined(_MSC_VER) && !defined(__APPLE__) && !defined(_FORTIFY_SOURCE)
977 CPL_C_START
978 # if defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF)
979 int vsnprintf(char *str, size_t size, const char* fmt, va_list args)
980 CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");
981 int snprintf(char *str, size_t size, const char* fmt, ...)
982 CPL_PRINT_FUNC_FORMAT(3,4)
983 CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
984 int sprintf(char *str, const char* fmt, ...)
985 CPL_PRINT_FUNC_FORMAT(2, 3)
986 CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
987 # elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
988 int sprintf(char *str, const char* fmt, ...)
989 CPL_PRINT_FUNC_FORMAT(2, 3)
990 CPL_WARN_DEPRECATED("Use snprintf() or CPLsnprintf() instead");
991 # endif /* defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF) */
992 CPL_C_END
993 #endif /* !defined(_MSC_VER) && !defined(__APPLE__) */
994
995 #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
996
997 #define CPL_CPU_REQUIRES_ALIGNED_ACCESS
998
999 #endif
1000
1001 #if defined(__cplusplus)
1002 #ifndef CPPCHECK
1003
1004 #define CPL_ARRAYSIZE(array) \
1005 ((sizeof(array) / sizeof(*(array))) / \
1006 static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
1007 #else
1008 /* simplified version for cppcheck */
1009 #define CPL_ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
1010 #endif
1011
1012 extern "C++" {
1013 template<class T> static void CPL_IGNORE_RET_VAL(const T&) {}
1014 inline static bool CPL_TO_BOOL(int x) { return x != 0; }
1015 } /* extern "C++" */
1016
1017 #endif /* __cplusplus */
1018
1019 #if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (defined(__clang__) && __clang_major__ >= 3)) && !defined(_MSC_VER))
1020 #define HAVE_GCC_DIAGNOSTIC_PUSH
1021 #endif
1022
1023 #if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && !defined(_MSC_VER))
1024 #define HAVE_GCC_SYSTEM_HEADER
1025 #endif
1026
1027 #if ((defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))) || __GNUC__ >= 7)
1028
1029 # define CPL_FALLTHROUGH [[clang::fallthrough]];
1030 #else
1031
1032 # define CPL_FALLTHROUGH
1033 #endif
1034
1036 // Define DEBUG_BOOL to compile in "MSVC mode", ie error out when
1037 // a integer is assigned to a bool
1038 // WARNING: use only at compilation time, since it is know to not work
1039 // at runtime for unknown reasons (crash in MongoDB driver for example)
1040 #if defined(__cplusplus) && defined(DEBUG_BOOL) && !defined(DO_NOT_USE_DEBUG_BOOL) && !defined(CPL_SUPRESS_CPLUSPLUS)
1041 extern "C++" {
1042 class MSVCPedanticBool
1043 {
1044
1045 friend bool operator== (const bool& one, const MSVCPedanticBool& other);
1046 friend bool operator!= (const bool& one, const MSVCPedanticBool& other);
1047
1048 bool b;
1049 MSVCPedanticBool(int bIn);
1050
1051 public:
1052 /* b not initialized on purpose in default ctor to flag use. */
1053 /* cppcheck-suppress uninitMemberVar */
1054 MSVCPedanticBool() {}
1055 MSVCPedanticBool(bool bIn) : b(bIn) {}
1056 MSVCPedanticBool(const MSVCPedanticBool& other) : b(other.b) {}
1057
1058 MSVCPedanticBool& operator= (const MSVCPedanticBool& other) { b = other.b; return *this; }
1059 MSVCPedanticBool& operator&= (const MSVCPedanticBool& other) { b &= other.b; return *this; }
1060 MSVCPedanticBool& operator|= (const MSVCPedanticBool& other) { b |= other.b; return *this; }
1061
1062 bool operator== (const bool& other) const { return b == other; }
1063 bool operator!= (const bool& other) const { return b != other; }
1064 bool operator< (const bool& other) const { return b < other; }
1065 bool operator== (const MSVCPedanticBool& other) const { return b == other.b; }
1066 bool operator!= (const MSVCPedanticBool& other) const { return b != other.b; }
1067 bool operator< (const MSVCPedanticBool& other) const { return b < other.b; }
1068
1069 bool operator! () const { return !b; }
1070 operator bool() const { return b; }
1071 operator int() const { return b; }
1072 operator GIntBig() const { return b; }
1073 };
1074
1075 inline bool operator== (const bool& one, const MSVCPedanticBool& other) { return one == other.b; }
1076 inline bool operator!= (const bool& one, const MSVCPedanticBool& other) { return one != other.b; }
1077
1078 /* We must include all C++ stuff before to avoid issues with templates that use bool */
1079 #include <vector>
1080 #include <map>
1081 #include <set>
1082 #include <string>
1083 #include <cstddef>
1084 #include <limits>
1085 #include <sstream>
1086 #include <fstream>
1087 #include <algorithm>
1088 #include <functional>
1089 #include <memory>
1090 #include <queue>
1091 #include <mutex>
1092 #include <unordered_map>
1093 #include <thread>
1094 #include <unordered_set>
1095 #include <complex>
1096 #include <iomanip>
1097
1098 } /* extern C++ */
1099
1100 #undef FALSE
1101 #define FALSE false
1102 #undef TRUE
1103 #define TRUE true
1104
1105 /* In the very few cases we really need a "simple" type, fallback to bool */
1106 #define EMULATED_BOOL int
1107
1108 /* Use our class instead of bool */
1109 #define bool MSVCPedanticBool
1110
1111 /* "volatile bool" with the below substitution doesn't really work. */
1112 /* Just for the sake of the debug, we don't really need volatile */
1113 #define VOLATILE_BOOL bool
1114
1115 #else /* defined(__cplusplus) && defined(DEBUG_BOOL) */
1116
1117 #ifndef FALSE
1118 # define FALSE 0
1119 #endif
1120
1121 #ifndef TRUE
1122 # define TRUE 1
1123 #endif
1124
1125 #define EMULATED_BOOL bool
1126 #define VOLATILE_BOOL volatile bool
1127
1128 #endif /* defined(__cplusplus) && defined(DEBUG_BOOL) */
1129
1130 #if __clang_major__ >= 4 || (__clang_major__ == 3 && __clang_minor__ >= 8)
1131 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
1132 #else
1133 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1134 #endif
1135
1136 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && defined(GDAL_COMPILATION)
1137 extern "C++" {
1138 template<class C, class A, class B>
1139 CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1140 inline C CPLUnsanitizedAdd(A a, B b)
1141 {
1142 return a + b;
1143 }
1144 }
1145 #endif
1146
1150 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
1151 #define CPL_NULLPTR nullptr
1152 #else
1153 #define CPL_NULLPTR NULL
1154 #endif
1155
1157 /* This typedef is for C functions that take char** as argument, but */
1158 /* with the semantics of a const list. In C, char** is not implicitly cast to */
1159 /* const char* const*, contrary to C++. So when seen for C++, it is OK */
1160 /* to expose the prototypes as const char* const*, but for C we keep the */
1161 /* historical definition to avoid warnings. */
1162 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && !defined(DOXYGEN_SKIP)
1163
1165 typedef const char* const* CSLConstList;
1166 #else
1167
1169 typedef char** CSLConstList;
1170 #endif
1171
1172 #endif /* ndef CPL_BASE_H_INCLUDED */
CPL_PRINT_FUNC_FORMAT
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:869
GByte
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:215
GUInt64
GUIntBig GUInt64
Unsigned 64 bit integer type.
Definition: cpl_port.h:265
GInt16
short GInt16
Int16 type.
Definition: cpl_port.h:211
GPtrDiff_t
GIntBig GPtrDiff_t
Integer type large enough to hold the difference between 2 addresses.
Definition: cpl_port.h:282
CPLsnprintf
int CPLsnprintf(char *str, size_t size, const char *fmt,...)
snprintf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition: cpl_string.cpp:1339
GInt64
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:263
CPL_C_START
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:329
CSLConstList
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1169
GUIntBig
unsigned long long GUIntBig
Large unsigned integer type (generally 64-bit unsigned integer type).
Definition: cpl_port.h:247
CPL_C_END
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:331
GIntBig
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:244
GBool
int GBool
Type for boolean values (alias to int)
Definition: cpl_port.h:223
GUInt16
unsigned short GUInt16
Unsigned int16 type.
Definition: cpl_port.h:213
GInt32
int GInt32
Int32 type.
Definition: cpl_port.h:205
GUInt32
unsigned int GUInt32
Unsigned int32 type.
Definition: cpl_port.h:207