1 /* Converted to D from C:/devLibs/gdal21/include/cpl_port.h by htod */
2 module gdal.cpl.port;
3 /******************************************************************************
4  * $Id: cpl_port.h 33907 2016-04-07 00:37:06Z goatbar $
5  *
6  * Project:  CPL - Common Portability Library
7  * Author:   Frank Warmerdam, warmerdam@pobox.com
8  * Purpose:  Include file providing low level portability services for CPL.
9  *           This should be the first include file for any CPL based code.
10  *
11  ******************************************************************************
12  * Copyright (c) 1998, 2005, Frank Warmerdam <warmerdam@pobox.com>
13  * Copyright (c) 2008-2013, Even Rouault <even dot rouault at mines-paris dot org>
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal in the Software without restriction, including without limitation
18  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19  * and/or sell copies of the Software, and to permit persons to whom the
20  * Software is furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included
23  * in all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31  * DEALINGS IN THE SOFTWARE.
32  ****************************************************************************/
33 
34 /**
35  * \file cpl_port.h
36  *
37  * Core portability definitions for CPL.
38  *
39  */
40 
41 //C     #include "cpl_config.h"
42 // needed? import cpl_config;
43 
44 
45 /* ==================================================================== */
46 /*      Standard include files.                                         */
47 /* ==================================================================== */
48 
49 //C     #include <stdio.h>
50 // needed? import std.c.stdio;
51 //C     #include <stdlib.h>
52 // needed? import std.c.stdlib;
53 //C     #include <math.h>
54 // needed? import std.c.math;
55 //C     #include <stdarg.h>
56 // needed? import std.c.stdarg;
57 //C     #include <string.h>
58 // needed? import std.c.string;
59 //C     #include <ctype.h>
60 // needed? import std.c.ctype;
61 //C     #include <limits.h>
62 import core.stdc.limits : INT_MAX, INT_MIN, UINT_MAX;
63 
64 //C     #include <time.h>
65 // needed? import std.c.time;
66 
67 //C     #if defined(HAVE_ERRNO_H)
68 //C     #  include <errno.h>
69 // needed? import std.c.errno;
70 //C     #endif
71 
72 //C     #ifdef HAVE_LOCALE_H
73 //C     #  include <locale.h>
74 // needed? import std.c.locale;
75 //C     #endif
76 
77 //C     #ifdef HAVE_DIRECT_H
78 //C     #  include <direct.h>
79 // needed? import std.c.direct;
80 //C     #endif
81 
82 //C     #if !defined(WIN32)
83 //C     #  include <strings.h>
84 //C     #endif
85 
86 //C     #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
87 //C     #  define DBMALLOC
88 //C     #  include <dbmalloc.h>
89 //C     #endif
90 
91 //C     #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
92 //C     #  define USE_DMALLOC
93 //C     #  include <dmalloc.h>
94 //C     #endif
95 
96 /* ==================================================================== */
97 /*      Base portability stuff ... this stuff may need to be            */
98 /*      modified for new platforms.                                     */
99 /* ==================================================================== */
100 
101 /* -------------------------------------------------------------------- */
102 /*      Which versions of C++ are available.                            */
103 /* -------------------------------------------------------------------- */
104 
105 //C     #ifdef __cplusplus
106 //C     #  if __cplusplus >= 201103L
107 //C     #    define HAVE_CXX11 1
108 //C     #  endif
109 /* TODO(schwehr): What are the correct tests for C++ 14 and 17? */
110 //C     #endif  /* __cpluscplus */
111 
112 /*---------------------------------------------------------------------
113  *        types for 16 and 32 bits integers, etc...
114  *--------------------------------------------------------------------*/
115 //C     #if UINT_MAX == 65535
116 //C     typedef long            GInt32;
117 //C     typedef unsigned long   GUInt32;
118 //C     #else
119 //C     typedef int             GInt32;
120 extern (C):
121 alias int GInt32;
122 //C     typedef unsigned int    GUInt32;
123 alias uint GUInt32;
124 //C     #endif
125 
126 //C     typedef short           GInt16;
127 alias short GInt16;
128 //C     typedef unsigned short  GUInt16;
129 alias ushort GUInt16;
130 //C     typedef unsigned char   GByte;
131 alias ubyte GByte;
132 /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
133 /* in include/poppler/goo/gtypes.h */
134 //C     #ifndef CPL_GBOOL_DEFINED
135 //C     #define CPL_GBOOL_DEFINED
136 //C     typedef int             GBool;
137 alias int GBool;
138 //C     #endif
139 
140 /* -------------------------------------------------------------------- */
141 /*      64bit support                                                   */
142 /* -------------------------------------------------------------------- */
143 
144 //C     #if defined(WIN32) && defined(_MSC_VER)
145 
146 //C     #define VSI_LARGE_API_SUPPORTED
147 //C     typedef __int64          GIntBig;
148 //C     typedef unsigned __int64 GUIntBig;
149 
150 //C     #define GINTBIG_MIN     ((GIntBig)(0x80000000) << 32)
151 //C     #define GINTBIG_MAX     (((GIntBig)(0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
152 //C     #define GUINTBIG_MAX     (((GUIntBig)(0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
153 
154 //C     #elif HAVE_LONG_LONG
155 
156 //C     typedef long long        GIntBig;
157 //C     typedef unsigned long long GUIntBig;
158 
159 //C     #define GINTBIG_MIN     ((GIntBig)(0x80000000) << 32)
160 //C     #define GINTBIG_MAX     (((GIntBig)(0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
161 //C     #define GUINTBIG_MAX     (((GUIntBig)(0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
162 
163 //C     #else
164 
165 //C     typedef long             GIntBig;
166 alias int GIntBig;
167 //C     typedef unsigned long    GUIntBig;
168 alias uint GUIntBig;
169 
170 //C     #define GINTBIG_MIN     INT_MIN
171 //C     #define GINTBIG_MAX     INT_MAX
172 alias INT_MIN GINTBIG_MIN;
173 //C     #define GUINTBIG_MAX     UINT_MAX
174 alias INT_MAX GINTBIG_MAX;
175 //C     #endif
176 alias UINT_MAX GUINTBIG_MAX;
177 
178 //C     #if SIZEOF_VOIDP == 8
179 //C     typedef GIntBig          GPtrDiff_t;
180 //C     #else
181 //C     typedef int              GPtrDiff_t;
182 alias int GPtrDiff_t;
183 //C     #endif
184 
185 //C     #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
186 //C       #define CPL_FRMT_GB_WITHOUT_PREFIX     "I64"
187 //C     #elif HAVE_LONG_LONG
188 //C       #define CPL_FRMT_GB_WITHOUT_PREFIX     "ll"
189 //C     #else
190 //C       #define CPL_FRMT_GB_WITHOUT_PREFIX     "l"
191 //C     #endif
192 
193 //C     #define CPL_FRMT_GIB     "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
194 //C     #define CPL_FRMT_GUIB    "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
195 
196 /* Workaround VC6 bug */
197 //C     #if defined(_MSC_VER) && (_MSC_VER <= 1200)
198 //C     #define GUINTBIG_TO_DOUBLE(x) (double)(GIntBig)(x)
199 //C     #else
200 //C     #define GUINTBIG_TO_DOUBLE(x) (double)(x)
201 //C     #endif
202 
203 //C     #ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
204 //C     #define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
205 //C     #else
206 //C     #define CPL_INT64_FITS_ON_INT32(x) (((GIntBig)(int)(x)) == (x))
207 //C     #endif
208 
209 /* ==================================================================== */
210 /*      Other standard services.                                        */
211 /* ==================================================================== */
212 //C     #ifdef __cplusplus
213 //C     #  define CPL_C_START           extern "C" {
214 //C     #  define CPL_C_END             }
215 //C     #else
216 //C     #  define CPL_C_START
217 //C     #  define CPL_C_END
218 //C     #endif
219 
220 //C     #ifndef CPL_DLL
221 //C     #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
222 //C     #  define CPL_DLL     __declspec(dllexport)
223 //C     #else
224 //C     #  if defined(USE_GCC_VISIBILITY_FLAG)
225 //C     #    define CPL_DLL     __attribute__ ((visibility("default")))
226 //C     #  else
227 //C     #    define CPL_DLL
228 //C     #  endif
229 //C     #endif
230 //C     #endif
231 
232 /* Should optional (normally private) interfaces be exported? */
233 //C     #ifdef CPL_OPTIONAL_APIS
234 //C     #  define CPL_ODLL CPL_DLL
235 //C     #else
236 //C     #  define CPL_ODLL
237 //C     #endif
238 
239 //C     #ifndef CPL_STDCALL
240 //C     #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
241 //C     #  define CPL_STDCALL     __stdcall
242 //C     #else
243 //C     #  define CPL_STDCALL
244 //C     #endif
245 //C     #endif
246 
247 //C     #ifdef _MSC_VER
248 //C     #  define FORCE_CDECL  __cdecl
249 //C     #else
250 //C     #  define FORCE_CDECL
251 //C     #endif
252 
253 /* TODO : support for other compilers needed */
254 //C     #if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
255 //C     #define HAS_CPL_INLINE  1
256 //C     #define CPL_INLINE __inline
257 //C     #elif defined(__SUNPRO_CC)
258 //C     #define HAS_CPL_INLINE  1
259 //C     #define CPL_INLINE inline
260 //C     #else
261 //C     #define CPL_INLINE
262 //C     #endif
263 
264 // Define NULL_AS_NULLPTR together with -std=c++11 -Wzero-as-null-pointer-constant with GCC
265 // to detect misuses of NULL
266 //C     #if defined(NULL_AS_NULLPTR) && HAVE_CXX11
267 
268 //C     #ifdef __GNUC__
269 // We need to include all that bunch of system headers, otherwise
270 // as they include <stddef.h> with __need_NULL, this overrides our #define NULL nullptr
271 // with #define NULL __null
272 //C     #include <locale.h>
273 //C     #include <unistd.h>
274 //C     #include <sys/types.h>
275 //C     #ifdef HAVE_ICONV
276 //C     #include <iconv.h>
277 //C     #endif
278 //C     #ifdef HAVE_MMAP
279 //C     #include <sys/mman.h>
280 //C     #endif
281 //C     #include <signal.h>
282 //C     #ifndef _WIN32
283 //C     #include <dlfcn.h>
284 //C     #include <netdb.h>
285 //C     #include <fcntl.h>
286 //C     #endif
287 
288 //C     extern "C++" {
289 //C     #include <string>
290 //C     #include <cstdio>
291 //C     #include <cstdlib>
292 //C     #include <cstring>
293 //C     #include <cstddef>
294 //C     #include <ostream>
295 //C     #include <iostream>
296 //C     #include <sstream>
297 //C     }
298 //C     #endif /* __GNUC__ */
299 
300 //C     #undef NULL
301 //C     #define NULL nullptr
302 //C     #else /* defined(NULL_AS_NULLPTR) && HAVE_CXX11 */
303 //C     #ifndef NULL
304 //C     #  define NULL  0
305 //C     #endif
306 //C     #endif /* defined(NULL_AS_NULLPTR) && HAVE_CXX11 */
307 
308 
309 //C     #ifndef MAX
310 //C     #  define MIN(a,b)      ((a<b) ? a : b)
311 //C     #  define MAX(a,b)      ((a>b) ? a : b)
312 //C     #endif
313 
314 //C     #ifndef ABS
315 //C     #  define ABS(x)        ((x<0) ? (-1*(x)) : x)
316 //C     #endif
317 
318 //C     #ifndef M_PI
319 //C     # define M_PI		3.14159265358979323846
320 /* 3.1415926535897932384626433832795 */
321 //C     #endif
322 
323 /* -------------------------------------------------------------------- */
324 /*      Macro to test equality of two floating point values.            */
325 /*      We use fabs() function instead of ABS() macro to avoid side     */
326 /*      effects.                                                        */
327 /* -------------------------------------------------------------------- */
328 //C     #ifndef CPLIsEqual
329 //C     #  define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
330 //C     #endif
331 
332 /* -------------------------------------------------------------------- */
333 /*      Provide macros for case insensitive string comparisons.         */
334 /* -------------------------------------------------------------------- */
335 //C     #ifndef EQUAL
336 
337 //C     #if defined(AFL_FRIENDLY) && defined(__GNUC__)
338 
339 //C     static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
340 //C             __attribute__((always_inline));
341 
342 //C     static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
343 //C     {
344 //C         const unsigned char* bptr1 = (const unsigned char*)ptr1;
345 //C         const unsigned char* bptr2 = (const unsigned char*)ptr2;
346 //C         while( len-- )
347 //C         {
348 //C             unsigned char b1 = *(bptr1++);
349 //C             unsigned char b2 = *(bptr2++);
350 //C             if( b1 != b2 ) return b1 - b2;
351 //C         }
352 //C         return 0;
353 //C     }
354 
355 //C     static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
356 //C             __attribute__((always_inline));
357 
358 //C     static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
359 //C     {
360 //C         const unsigned char* usptr1 = (const unsigned char*)ptr1;
361 //C         const unsigned char* usptr2 = (const unsigned char*)ptr2;
362 //C         while( 1 )
363 //C         {
364 //C             unsigned char ch1 = *(usptr1++);
365 //C             unsigned char ch2 = *(usptr2++);
366 //C             if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
367 //C         }
368 //C     }
369 
370 //C     static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
371 //C             __attribute__((always_inline));
372 
373 //C     static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
374 //C     {
375 //C         const unsigned char* usptr1 = (const unsigned char*)ptr1;
376 //C         const unsigned char* usptr2 = (const unsigned char*)ptr2;
377 //C         while( len -- )
378 //C         {
379 //C             unsigned char ch1 = *(usptr1++);
380 //C             unsigned char ch2 = *(usptr2++);
381 //C             if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
382 //C         }
383 //C         return 0;
384 //C     }
385 
386 //C     static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
387 //C             __attribute__((always_inline));
388 
389 //C     static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
390 //C     {
391 //C         const unsigned char* usptr1 = (const unsigned char*)ptr1;
392 //C         const unsigned char* usptr2 = (const unsigned char*)ptr2;
393 //C         while( 1 )
394 //C         {
395 //C             unsigned char ch1 = *(usptr1++);
396 //C             unsigned char ch2 = *(usptr2++);
397 //C             ch1 = (unsigned char)toupper(ch1);
398 //C             ch2 = (unsigned char)toupper(ch2);
399 //C             if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
400 //C         }
401 //C     }
402 
403 //C     static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
404 //C             __attribute__((always_inline));
405 
406 //C     static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
407 //C     {
408 //C         const unsigned char* usptr1 = (const unsigned char*)ptr1;
409 //C         const unsigned char* usptr2 = (const unsigned char*)ptr2;
410 //C         while( len-- )
411 //C         {
412 //C             unsigned char ch1 = *(usptr1++);
413 //C             unsigned char ch2 = *(usptr2++);
414 //C             ch1 = (unsigned char)toupper(ch1);
415 //C             ch2 = (unsigned char)toupper(ch2);
416 //C             if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
417 //C         }
418 //C         return 0;
419 //C     }
420 
421 //C     static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
422 //C             __attribute__((always_inline));
423 
424 //C     static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
425 //C     {
426 //C         const char* ptr_haystack = haystack;
427 //C         while( 1 )
428 //C         {
429 //C             const char* ptr_haystack2 = ptr_haystack;
430 //C             const char* ptr_needle = needle;
431 //C             while( 1 )
432 //C             {
433 //C                 char ch1 = *(ptr_haystack2++);
434 //C                 char ch2 = *(ptr_needle++);
435 //C                 if( ch2 == 0 )
436 //C                     return (char*)ptr_haystack;
437 //C                 if( ch1 != ch2 )
438 //C                     break;
439 //C             }
440 //C             if( *ptr_haystack == 0 )
441 //C                 return NULL;
442 //C             ptr_haystack ++;
443 //C         }
444 //C     }
445 
446 //C     #undef strcmp
447 //C     #undef strncmp
448 //C     #define memcmp CPL_afl_friendly_memcmp
449 //C     #define strcmp CPL_afl_friendly_strcmp
450 //C     #define strncmp CPL_afl_friendly_strncmp
451 //C     #define strcasecmp CPL_afl_friendly_strcasecmp
452 //C     #define strncasecmp CPL_afl_friendly_strncasecmp
453 //C     #define strstr CPL_afl_friendly_strstr
454 
455 //C     #endif /* defined(AFL_FRIENDLY) && defined(__GNUC__) */
456 
457 //C     #  if defined(WIN32)
458 //C     #    define STRCASECMP(a,b)         (stricmp(a,b))
459 //C     #    define STRNCASECMP(a,b,n)      (strnicmp(a,b,n))
460 //C     #  else
461 //C     #    define STRCASECMP(a,b)         (strcasecmp(a,b))
462 //C     #    define STRNCASECMP(a,b,n)      (strncasecmp(a,b,n))
463 //C     #  endif
464 //C     #  define EQUALN(a,b,n)           (STRNCASECMP(a,b,n)==0)
465 //C     #  define EQUAL(a,b)              (STRCASECMP(a,b)==0)
466 //C     #endif
467 
468 /*---------------------------------------------------------------------
469  * Does a string "a" start with string "b".  Search is case-sensitive or,
470  * with CI, it is a case-insensitive comparison.
471  *--------------------------------------------------------------------- */
472 //C     #ifndef STARTS_WITH_CI
473 //C     #define STARTS_WITH(a,b)               (strncmp(a,b,strlen(b)) == 0)
474 //C     #define STARTS_WITH_CI(a,b)            EQUALN(a,b,strlen(b))
475 //C     #endif
476 
477 //C     #ifndef CPL_THREADLOCAL
478 //C     #  define CPL_THREADLOCAL
479 //C     #endif
480 
481 /* -------------------------------------------------------------------- */
482 /*      Handle isnan() and isinf().  Note that isinf() and isnan()      */
483 /*      are supposed to be macros according to C99, defined in math.h   */
484 /*      Some systems (i.e. Tru64) don't have isinf() at all, so if      */
485 /*      the macro is not defined we just assume nothing is infinite.    */
486 /*      This may mean we have no real CPLIsInf() on systems with isinf()*/
487 /*      function but no corresponding macro, but I can live with        */
488 /*      that since it isn't that important a test.                      */
489 /* -------------------------------------------------------------------- */
490 //C     #ifdef _MSC_VER
491 //C     #  include <float.h>
492 //C     #  define CPLIsNan(x) _isnan(x)
493 //C     #  define CPLIsInf(x) (!_isnan(x) && !_finite(x))
494 //C     #  define CPLIsFinite(x) _finite(x)
495 //C     #else
496 //C     #  define CPLIsNan(x) isnan(x)
497 //C     #  ifdef isinf
498 //C     #    define CPLIsInf(x) isinf(x)
499 //C     #    define CPLIsFinite(x) (!isnan(x) && !isinf(x))
500 //C     #  elif defined(__sun__)
501 //C     #    include <ieeefp.h>
502 //C     #    define CPLIsInf(x)    (!finite(x) && !isnan(x))
503 //C     #    define CPLIsFinite(x) finite(x)
504 //C     #  else
505 //C     #    define CPLIsInf(x)    (0)
506 //C     #    define CPLIsFinite(x) (!isnan(x))
507 //C     #  endif
508 //C     #endif
509 
510 /*---------------------------------------------------------------------
511  *                         CPL_LSB and CPL_MSB
512  * Only one of these 2 macros should be defined and specifies the byte
513  * ordering for the current platform.
514  * This should be defined in the Makefile, but if it is not then
515  * the default is CPL_LSB (Intel ordering, LSB first).
516  *--------------------------------------------------------------------*/
517 //C     #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
518 //C     #  define CPL_MSB
519 //C     #endif
520 
521 //C     #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
522 //C     #define CPL_LSB
523 //C     #endif
524 
525 //C     #if defined(CPL_LSB)
526 //C     #  define CPL_IS_LSB 1
527 //C     #else
528 enum CPL_IS_LSB = 1;
529 //C     #  define CPL_IS_LSB 0
530 //C     #endif
531 
532 //C     #ifdef __cplusplus
533 
534 //C     extern "C++" {
535 
536 //C     template <bool b> struct CPLStaticAssert {};
537 //C     template<> struct CPLStaticAssert<true>
538 //C     {
539 //C         static void my_function() {}
540 //C     };
541 
542 //C     } /* extern "C++" */
543 
544 //C     #define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
545 //C     #define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
546 
547 //C     #else  /* __cplusplus */
548 
549 //C     #define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
550 
551 //C     #endif  /* __cplusplus */
552 
553 /*---------------------------------------------------------------------
554  *        Little endian <==> big endian byte swap macros.
555  *--------------------------------------------------------------------*/
556 
557 //C     #define CPL_SWAP16(x)         ((GUInt16)(             (((GUInt16)(x) & 0x00ffU) << 8) |             (((GUInt16)(x) & 0xff00U) >> 8) ))
558 
559 //C     #define CPL_SWAP16PTR(x) {                                                                     GByte       byTemp, *_pabyDataT = (GByte *) (x);                  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2);                                                                       byTemp = _pabyDataT[0];                                           _pabyDataT[0] = _pabyDataT[1];                                    _pabyDataT[1] = byTemp;                                       }
560 
561 //C     #define CPL_SWAP32(x)         ((GUInt32)(             (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) |             (((GUInt32)(x) & (GUInt32)0x0000ff00UL) <<  8) |             (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >>  8) |             (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
562 
563 //C     #define CPL_SWAP32PTR(x) {                                                                     GByte       byTemp, *_pabyDataT = (GByte *) (x);                  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4);                                                                        byTemp = _pabyDataT[0];                                           _pabyDataT[0] = _pabyDataT[3];                                    _pabyDataT[3] = byTemp;                                           byTemp = _pabyDataT[1];                                           _pabyDataT[1] = _pabyDataT[2];                                    _pabyDataT[2] = byTemp;                                       }
564 
565 //C     #define CPL_SWAP64PTR(x) {                                                                     GByte       byTemp, *_pabyDataT = (GByte *) (x);                  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8);                                                                       byTemp = _pabyDataT[0];                                           _pabyDataT[0] = _pabyDataT[7];                                    _pabyDataT[7] = byTemp;                                           byTemp = _pabyDataT[1];                                           _pabyDataT[1] = _pabyDataT[6];                                    _pabyDataT[6] = byTemp;                                           byTemp = _pabyDataT[2];                                           _pabyDataT[2] = _pabyDataT[5];                                    _pabyDataT[5] = byTemp;                                           byTemp = _pabyDataT[3];                                           _pabyDataT[3] = _pabyDataT[4];                                    _pabyDataT[4] = byTemp;                                       }
566 
567 
568 /* Until we have a safe 64 bits integer data type defined, we'll replace
569  * this version of the CPL_SWAP64() macro with a less efficient one.
570  */
571 /*
572 #define CPL_SWAP64(x)         ((uint64)(             (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) |             (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) |             (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) |             (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) |             (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) |             (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) |             (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) |             (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
573 */
574 
575 //C     #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
576 
577 //C     #ifdef CPL_MSB
578 //C     #  define CPL_MSBWORD16(x)      (x)
579 //C     #  define CPL_LSBWORD16(x)      CPL_SWAP16(x)
580 //C     #  define CPL_MSBWORD32(x)      (x)
581 //C     #  define CPL_LSBWORD32(x)      CPL_SWAP32(x)
582 //C     #  define CPL_MSBPTR16(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
583 //C     #  define CPL_LSBPTR16(x)       CPL_SWAP16PTR(x)
584 //C     #  define CPL_MSBPTR32(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
585 //C     #  define CPL_LSBPTR32(x)       CPL_SWAP32PTR(x)
586 //C     #  define CPL_MSBPTR64(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
587 //C     #  define CPL_LSBPTR64(x)       CPL_SWAP64PTR(x)
588 //C     #else
589 //C     #  define CPL_LSBWORD16(x)      (x)
590 //C     #  define CPL_MSBWORD16(x)      CPL_SWAP16(x)
591 //C     #  define CPL_LSBWORD32(x)      (x)
592 //C     #  define CPL_MSBWORD32(x)      CPL_SWAP32(x)
593 //C     #  define CPL_LSBPTR16(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
594 //C     #  define CPL_MSBPTR16(x)       CPL_SWAP16PTR(x)
595 //C     #  define CPL_LSBPTR32(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
596 //C     #  define CPL_MSBPTR32(x)       CPL_SWAP32PTR(x)
597 //C     #  define CPL_LSBPTR64(x)       CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
598 //C     #  define CPL_MSBPTR64(x)       CPL_SWAP64PTR(x)
599 //C     #endif
600 
601 /** Return a Int16 from the 2 bytes ordered in LSB order at address x */
602 //C     #define CPL_LSBINT16PTR(x)    ((*(GByte*)(x)) | (*(((GByte*)(x))+1) << 8))
603 
604 /** Return a Int32 from the 4 bytes ordered in LSB order at address x */
605 //C     #define CPL_LSBINT32PTR(x)    ((*(GByte*)(x)) | (*(((GByte*)(x))+1) << 8) |                               (*(((GByte*)(x))+2) << 16) | (*(((GByte*)(x))+3) << 24))
606 
607 /** Return a signed Int16 from the 2 bytes ordered in LSB order at address x */
608 //C     #define CPL_LSBSINT16PTR(x) ((GInt16) CPL_LSBINT16PTR(x))
609 
610 /** Return a unsigned Int16 from the 2 bytes ordered in LSB order at address x */
611 //C     #define CPL_LSBUINT16PTR(x) ((GUInt16)CPL_LSBINT16PTR(x))
612 
613 /** Return a signed Int32 from the 4 bytes ordered in LSB order at address x */
614 //C     #define CPL_LSBSINT32PTR(x) ((GInt32) CPL_LSBINT32PTR(x))
615 
616 /** Return a unsigned Int32 from the 4 bytes ordered in LSB order at address x */
617 //C     #define CPL_LSBUINT32PTR(x) ((GUInt32)CPL_LSBINT32PTR(x))
618 
619 
620 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
621 //C     #ifndef UNREFERENCED_PARAM
622 //C     #  ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
623 //C     #    define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
624 //C     #  else
625 //C     #    define UNREFERENCED_PARAM(param) ((void)param)
626 //C     #  endif /* UNREFERENCED_PARAMETER */
627 //C     #endif /* UNREFERENCED_PARAM */
628 
629 /***********************************************************************
630  * Define CPL_CVSID() macro.  It can be disabled during a build by
631  * defining DISABLE_CVSID in the compiler options.
632  *
633  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
634  * being unused.
635  */
636 
637 //C     #ifndef DISABLE_CVSID
638 //C     #if defined(__GNUC__) && __GNUC__ >= 4
639 //C     #  define CPL_CVSID(string)     static const char cpl_cvsid[] __attribute__((used)) = string;
640 //C     #else
641 //C     #  define CPL_CVSID(string)     static const char cpl_cvsid[] = string; static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
642 //C     #endif
643 //C     #else
644 //C     #  define CPL_CVSID(string)
645 //C     #endif
646 
647 /* Null terminated variadic */
648 /* We exclude mingw64 4.6 which seems to be broken regarding this */
649 //C     #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
650 //C     #   define CPL_NULL_TERMINATED     __attribute__((__sentinel__))
651 //C     #else
652 //C     #   define CPL_NULL_TERMINATED
653 //C     #endif
654 
655 //C     #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
656 //C     #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )  __attribute__((__format__ (__printf__, format_idx, arg_idx)))
657 //C     #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )  __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
658 //C     #else
659 //C     #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
660 //C     #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )
661 //C     #endif
662 
663 //C     #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
664 //C     #define CPL_WARN_UNUSED_RESULT                        __attribute__((warn_unused_result))
665 //C     #else
666 //C     #define CPL_WARN_UNUSED_RESULT
667 //C     #endif
668 
669 //C     #if defined(__GNUC__) && __GNUC__ >= 4
670 //C     #  define CPL_UNUSED __attribute((__unused__))
671 //C     #else
672 /* TODO: add cases for other compilers */
673 //C     #  define CPL_UNUSED
674 //C     #endif
675 
676 //C     #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
677 //C     #define CPL_NO_RETURN                                __attribute__((noreturn))
678 //C     #else
679 //C     #define CPL_NO_RETURN
680 //C     #endif
681 
682 /* Clang __has_attribute */
683 //C     #ifndef __has_attribute
684 //C       #define __has_attribute(x) 0  // Compatibility with non-clang compilers.
685 //C     #endif
686 
687 //C     #if ((defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || __has_attribute(returns_nonnull)) && !defined(DOXYGEN_SKIP)
688 //C     #  define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
689 //C     #else
690 //C     #  define CPL_RETURNS_NONNULL
691 //C     #endif
692 
693 
694 //C     #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
695 //C     #define CPL_RESTRICT __restrict__
696 //C     #else
697 //C     #define CPL_RESTRICT
698 //C     #endif
699 
700 /* Helper to remove the copy and assignment constructors so that the compiler
701    will not generate the default versions.
702 
703    Must be placed in the private section of a class and should be at the end.
704 */
705 //C     #ifdef __cplusplus
706 
707 //C     #if HAVE_CXX11
708 //C     #  define CPL_FINAL final
709 //C     #  define CPL_DISALLOW_COPY_ASSIGN(ClassName)     ClassName( const ClassName & ) = delete;     ClassName &operator=( const ClassName & ) = delete;
710 //C     #else
711 //C     #  define CPL_FINAL
712 //C     #  define CPL_DISALLOW_COPY_ASSIGN(ClassName)     ClassName( const ClassName & );     ClassName &operator=( const ClassName & );
713 //C     #endif  /* HAVE_CXX11 */
714 
715 //C     #endif /* __cplusplus */
716 
717 //C     #if !defined(DOXYGEN_SKIP)
718 //C     #if defined(__has_extension)
719 //C       #if __has_extension(attribute_deprecated_with_message)
720     /* Clang extension */
721 //C         #define CPL_WARN_DEPRECATED(x)                       __attribute__ ((deprecated(x)))
722 //C       #else
723 //C         #define CPL_WARN_DEPRECATED(x)
724 //C       #endif
725 //C     #elif defined(__GNUC__)
726 //C         #define CPL_WARN_DEPRECATED(x)                       __attribute__ ((deprecated))
727 //C     #else
728 //C       #define CPL_WARN_DEPRECATED(x)
729 //C     #endif
730 //C     #endif
731 
732 //C     #if !defined(_MSC_VER) && !defined(__APPLE__)
733 //C     CPL_C_START
734 //C     #ifdef WARN_STANDARD_PRINTF
735 //C     int vsnprintf(char *str, size_t size, const char* fmt, va_list args) CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");
736 //C     int snprintf(char *str, size_t size, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(3,4) CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
737 //C     int sprintf(char *str, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
738 //C     #elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
739 //C     int sprintf(char *str, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_DEPRECATED("Use snprintf() or CPLsnprintf() instead");
740 //C     #endif
741 //C     CPL_C_END
742 //C     #endif /* !defined(_MSC_VER) && !defined(__APPLE__) */
743 
744 //C     #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
745 //C     #define CPL_CPU_REQUIRES_ALIGNED_ACCESS
746 //C     #define CPL_IS_DOUBLE_A_INT(d)  ( (d) >= INT_MIN && (d) <= INT_MAX && (double)(int)(d) == (d) )
747 //C     #else
748 /* This is technically unspecified behaviour if the double is out of range, but works OK on x86 */
749 //C     #define CPL_IS_DOUBLE_A_INT(d)  ( (double)(int)(d) == (d) )
750 //C     #endif
751 
752 //C     #ifdef __cplusplus
753 /* The size of C style arrays. */
754 //C     #define CPL_ARRAYSIZE(array)   ((sizeof(array) / sizeof(*(array))) /   static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
755 
756 //C     extern "C++" {
757 //C     template<class T> static void CPL_IGNORE_RET_VAL(T) {}
758 //C     inline static bool CPL_TO_BOOL(int x) { return x != 0; }
759 //C     } /* extern "C++" */
760 
761 //C     #endif  /* __cplusplus */
762 
763 //C     #if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (defined(__clang__) && __clang_major__ >= 3)) && !defined(_MSC_VER))
764 //C     #define HAVE_GCC_DIAGNOSTIC_PUSH
765 //C     #endif
766 
767 //C     #if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && !defined(_MSC_VER))
768 //C     #define HAVE_GCC_SYSTEM_HEADER
769 //C     #endif
770 
771 //C     #if defined(__clang__)
772 //C     #  define CPL_FALLTHROUGH [[clang::fallthrough]];
773 //C     #else
774 //C     #  define CPL_FALLTHROUGH
775 //C     #endif
776 
777 // Define DEBUG_BOOL to compile in "MSVC mode", ie error out when
778 // a integer is assigned to a bool
779 // WARNING: use only at compilation time, since it is know to not work
780 //  at runtime for unknown reasons (crash in MongoDB driver for example)
781 //C     #if defined(__cplusplus) && defined(DEBUG_BOOL) && !defined(DO_NOT_USE_DEBUG_BOOL)
782 //C     extern "C++" {
783 //C     class MSVCPedanticBool
784 //C     {
785 
786 //C             friend bool operator== (const bool& one, const MSVCPedanticBool& other);
787 //C             friend bool operator!= (const bool& one, const MSVCPedanticBool& other);
788 
789 //C             bool b;
790 //C             MSVCPedanticBool(int bIn);
791 
792 //C         public:
793         /* b not initialized on purpose in default ctor to flag use. */
794         /* cppcheck-suppress uninitMemberVar */
795 //C             MSVCPedanticBool() {}
796 //C             MSVCPedanticBool(bool bIn) : b(bIn) {}
797 //C             MSVCPedanticBool(const MSVCPedanticBool& other) : b(other.b) {}
798 
799 //C             MSVCPedanticBool& operator= (const MSVCPedanticBool& other) { b = other.b; return *this; }
800 //C             MSVCPedanticBool& operator&= (const MSVCPedanticBool& other) { b &= other.b; return *this; }
801 //C             MSVCPedanticBool& operator|= (const MSVCPedanticBool& other) { b |= other.b; return *this; }
802 
803 //C             bool operator== (const bool& other) const { return b == other; }
804 //C             bool operator!= (const bool& other) const { return b != other; }
805 //C             bool operator== (const MSVCPedanticBool& other) const { return b == other.b; }
806 //C             bool operator!= (const MSVCPedanticBool& other) const { return b != other.b; }
807 
808 //C             bool operator! () const { return !b; }
809 //C             operator bool() const { return b; }
810 //C             operator int() const { return b; }
811 //C     };
812 
813 //C     inline bool operator== (const bool& one, const MSVCPedanticBool& other) { return one == other.b; }
814 //C     inline bool operator!= (const bool& one, const MSVCPedanticBool& other) { return one != other.b; }
815 
816 /* We must include all C++ stuff before to avoid issues with templates that use bool */
817 //C     #include <vector>
818 //C     #include <map>
819 //C     #include <set>
820 //C     #include <string>
821 //C     #include <cstddef>
822 //C     #include <limits>
823 //C     #include <sstream>
824 //C     #include <fstream>
825 //C     #include <algorithm>
826 
827 //C     } /* extern C++ */
828 
829 //C     #undef FALSE
830 //C     #define FALSE false
831 //C     #undef TRUE
832 //C     #define TRUE true
833 
834 /* In the very few cases we really need a "simple" type, fallback to bool */
835 //C     #define EMULATED_BOOL int
836 
837 /* Use our class instead of bool */
838 //C     #define bool MSVCPedanticBool
839 
840 /* "volatile bool" with the below substitution doesn't really work. */
841 /* Just for the sake of the debug, we don't really need volatile */
842 //C     #define VOLATILE_BOOL bool
843 
844 //C     #else /* defined(__cplusplus) && defined(DEBUG_BOOL) */
845 
846 //C     #ifndef FALSE
847 //C     #  define FALSE 0
848 //C     #endif
849 enum FALSE = 0;
850 
851 //C     #ifndef TRUE
852 //C     #  define TRUE 1
853 //C     #endif
854 enum TRUE = 1;
855 
856 //C     #define EMULATED_BOOL bool
857 //C     #define VOLATILE_BOOL volatile bool
858 alias bool EMULATED_BOOL;
859 
860 //C     #endif /* defined(__cplusplus) && defined(DEBUG_BOOL) */
861 
862 //C     #endif /* ndef CPL_BASE_H_INCLUDED */