GDAL
cpl_port.h
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id: cpl_port.h 33907 2016-04-07 00:37:06Z goatbar $
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 
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 /* ==================================================================== */
87 /* This will disable most WIN32 stuff in a Cygnus build which */
88 /* defines unix to 1. */
89 /* ==================================================================== */
90 
91 #ifdef unix
92 # undef WIN32
93 #endif
94 
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 
111 /* ==================================================================== */
112 /* MinGW stuff */
113 /* ==================================================================== */
114 
115 /* We need __MSVCRT_VERSION__ >= 0x0601 to have "struct __stat64" */
116 /* Latest versions of mingw32 define it, but with older ones, */
117 /* we need to define it manually */
118 #if defined(__MINGW32__)
119 #ifndef __MSVCRT_VERSION__
120 #define __MSVCRT_VERSION__ 0x0601
121 #endif
122 #endif
123 
124 /* ==================================================================== */
125 /* Standard include files. */
126 /* ==================================================================== */
127 
128 #include <stdio.h>
129 #include <stdlib.h>
130 #include <math.h>
131 #include <stdarg.h>
132 #include <string.h>
133 #include <ctype.h>
134 #include <limits.h>
135 
136 #include <time.h>
137 
138 #if defined(HAVE_ERRNO_H)
139 # include <errno.h>
140 #endif
141 
142 #ifdef HAVE_LOCALE_H
143 # include <locale.h>
144 #endif
145 
146 #ifdef HAVE_DIRECT_H
147 # include <direct.h>
148 #endif
149 
150 #if !defined(WIN32)
151 # include <strings.h>
152 #endif
153 
154 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
155 # define DBMALLOC
156 # include <dbmalloc.h>
157 #endif
158 
159 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
160 # define USE_DMALLOC
161 # include <dmalloc.h>
162 #endif
163 
164 /* ==================================================================== */
165 /* Base portability stuff ... this stuff may need to be */
166 /* modified for new platforms. */
167 /* ==================================================================== */
168 
169 /* -------------------------------------------------------------------- */
170 /* Which versions of C++ are available. */
171 /* -------------------------------------------------------------------- */
172 
173 #ifdef __cplusplus
174 # if __cplusplus >= 201103L
175 # define HAVE_CXX11 1
176 # endif
177 /* TODO(schwehr): What are the correct tests for C++ 14 and 17? */
178 #endif /* __cpluscplus */
179 
180 /*---------------------------------------------------------------------
181  * types for 16 and 32 bits integers, etc...
182  *--------------------------------------------------------------------*/
183 #if UINT_MAX == 65535
184 typedef long GInt32;
185 typedef unsigned long GUInt32;
186 #else
187 typedef int GInt32;
188 typedef unsigned int GUInt32;
189 #endif
190 
191 typedef short GInt16;
192 typedef unsigned short GUInt16;
193 typedef unsigned char GByte;
194 /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
195 /* in include/poppler/goo/gtypes.h */
196 #ifndef CPL_GBOOL_DEFINED
197 #define CPL_GBOOL_DEFINED
198 typedef int GBool;
199 #endif
200 
201 /* -------------------------------------------------------------------- */
202 /* 64bit support */
203 /* -------------------------------------------------------------------- */
204 
205 #if defined(WIN32) && defined(_MSC_VER)
206 
207 #define VSI_LARGE_API_SUPPORTED
208 typedef __int64 GIntBig;
209 typedef unsigned __int64 GUIntBig;
210 
211 #define GINTBIG_MIN ((GIntBig)(0x80000000) << 32)
212 #define GINTBIG_MAX (((GIntBig)(0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
213 #define GUINTBIG_MAX (((GUIntBig)(0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
214 
215 #elif HAVE_LONG_LONG
216 
217 typedef long long GIntBig;
218 typedef unsigned long long GUIntBig;
219 
220 #define GINTBIG_MIN ((GIntBig)(0x80000000) << 32)
221 #define GINTBIG_MAX (((GIntBig)(0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
222 #define GUINTBIG_MAX (((GUIntBig)(0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
223 
224 #else
225 
226 typedef long GIntBig;
227 typedef unsigned long GUIntBig;
228 
229 #define GINTBIG_MIN INT_MIN
230 #define GINTBIG_MAX INT_MAX
231 #define GUINTBIG_MAX UINT_MAX
232 #endif
233 
234 #if SIZEOF_VOIDP == 8
235 typedef GIntBig GPtrDiff_t;
236 #else
237 typedef int GPtrDiff_t;
238 #endif
239 
240 #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
241  #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
242 #elif HAVE_LONG_LONG
243  #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
244 #else
245  #define CPL_FRMT_GB_WITHOUT_PREFIX "l"
246 #endif
247 
248 #define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
249 #define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
250 
251 /* Workaround VC6 bug */
252 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
253 #define GUINTBIG_TO_DOUBLE(x) (double)(GIntBig)(x)
254 #else
255 #define GUINTBIG_TO_DOUBLE(x) (double)(x)
256 #endif
257 
258 #ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
259 #define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
260 #else
261 #define CPL_INT64_FITS_ON_INT32(x) (((GIntBig)(int)(x)) == (x))
262 #endif
263 
264 /* ==================================================================== */
265 /* Other standard services. */
266 /* ==================================================================== */
267 #ifdef __cplusplus
268 # define CPL_C_START extern "C" {
269 # define CPL_C_END }
270 #else
271 # define CPL_C_START
272 # define CPL_C_END
273 #endif
274 
275 #ifndef CPL_DLL
276 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
277 # define CPL_DLL __declspec(dllexport)
278 #else
279 # if defined(USE_GCC_VISIBILITY_FLAG)
280 # define CPL_DLL __attribute__ ((visibility("default")))
281 # else
282 # define CPL_DLL
283 # endif
284 #endif
285 #endif
286 
287 /* Should optional (normally private) interfaces be exported? */
288 #ifdef CPL_OPTIONAL_APIS
289 # define CPL_ODLL CPL_DLL
290 #else
291 # define CPL_ODLL
292 #endif
293 
294 #ifndef CPL_STDCALL
295 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
296 # define CPL_STDCALL __stdcall
297 #else
298 # define CPL_STDCALL
299 #endif
300 #endif
301 
302 #ifdef _MSC_VER
303 # define FORCE_CDECL __cdecl
304 #else
305 # define FORCE_CDECL
306 #endif
307 
308 /* TODO : support for other compilers needed */
309 #if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
310 #define HAS_CPL_INLINE 1
311 #define CPL_INLINE __inline
312 #elif defined(__SUNPRO_CC)
313 #define HAS_CPL_INLINE 1
314 #define CPL_INLINE inline
315 #else
316 #define CPL_INLINE
317 #endif
318 
319 // Define NULL_AS_NULLPTR together with -std=c++11 -Wzero-as-null-pointer-constant with GCC
320 // to detect misuses of NULL
321 #if defined(NULL_AS_NULLPTR) && HAVE_CXX11
322 
323 #ifdef __GNUC__
324 // We need to include all that bunch of system headers, otherwise
325 // as they include <stddef.h> with __need_NULL, this overrides our #define NULL nullptr
326 // with #define NULL __null
327 #include <locale.h>
328 #include <unistd.h>
329 #include <sys/types.h>
330 #ifdef HAVE_ICONV
331 #include <iconv.h>
332 #endif
333 #ifdef HAVE_MMAP
334 #include <sys/mman.h>
335 #endif
336 #include <signal.h>
337 #ifndef _WIN32
338 #include <dlfcn.h>
339 #include <netdb.h>
340 #include <fcntl.h>
341 #endif
342 
343 extern "C++" {
344 #include <string>
345 #include <cstdio>
346 #include <cstdlib>
347 #include <cstring>
348 #include <cstddef>
349 #include <ostream>
350 #include <iostream>
351 #include <sstream>
352 }
353 #endif /* __GNUC__ */
354 
355 #undef NULL
356 #define NULL nullptr
357 #else /* defined(NULL_AS_NULLPTR) && HAVE_CXX11 */
358 #ifndef NULL
359 # define NULL 0
360 #endif
361 #endif /* defined(NULL_AS_NULLPTR) && HAVE_CXX11 */
362 
363 
364 #ifndef MAX
365 # define MIN(a,b) ((a<b) ? a : b)
366 # define MAX(a,b) ((a>b) ? a : b)
367 #endif
368 
369 #ifndef ABS
370 # define ABS(x) ((x<0) ? (-1*(x)) : x)
371 #endif
372 
373 #ifndef M_PI
374 # define M_PI 3.14159265358979323846
375 /* 3.1415926535897932384626433832795 */
376 #endif
377 
378 /* -------------------------------------------------------------------- */
379 /* Macro to test equality of two floating point values. */
380 /* We use fabs() function instead of ABS() macro to avoid side */
381 /* effects. */
382 /* -------------------------------------------------------------------- */
383 #ifndef CPLIsEqual
384 # define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
385 #endif
386 
387 /* -------------------------------------------------------------------- */
388 /* Provide macros for case insensitive string comparisons. */
389 /* -------------------------------------------------------------------- */
390 #ifndef EQUAL
391 
392 #if defined(AFL_FRIENDLY) && defined(__GNUC__)
393 
394 static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
395  __attribute__((always_inline));
396 
397 static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
398 {
399  const unsigned char* bptr1 = (const unsigned char*)ptr1;
400  const unsigned char* bptr2 = (const unsigned char*)ptr2;
401  while( len-- )
402  {
403  unsigned char b1 = *(bptr1++);
404  unsigned char b2 = *(bptr2++);
405  if( b1 != b2 ) return b1 - b2;
406  }
407  return 0;
408 }
409 
410 static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
411  __attribute__((always_inline));
412 
413 static inline int CPL_afl_friendly_strcmp(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  if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
422  }
423 }
424 
425 static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
426  __attribute__((always_inline));
427 
428 static inline int CPL_afl_friendly_strncmp(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  if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
437  }
438  return 0;
439 }
440 
441 static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
442  __attribute__((always_inline));
443 
444 static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
445 {
446  const unsigned char* usptr1 = (const unsigned char*)ptr1;
447  const unsigned char* usptr2 = (const unsigned char*)ptr2;
448  while( 1 )
449  {
450  unsigned char ch1 = *(usptr1++);
451  unsigned char ch2 = *(usptr2++);
452  ch1 = (unsigned char)toupper(ch1);
453  ch2 = (unsigned char)toupper(ch2);
454  if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
455  }
456 }
457 
458 static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
459  __attribute__((always_inline));
460 
461 static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
462 {
463  const unsigned char* usptr1 = (const unsigned char*)ptr1;
464  const unsigned char* usptr2 = (const unsigned char*)ptr2;
465  while( len-- )
466  {
467  unsigned char ch1 = *(usptr1++);
468  unsigned char ch2 = *(usptr2++);
469  ch1 = (unsigned char)toupper(ch1);
470  ch2 = (unsigned char)toupper(ch2);
471  if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
472  }
473  return 0;
474 }
475 
476 static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
477  __attribute__((always_inline));
478 
479 static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
480 {
481  const char* ptr_haystack = haystack;
482  while( 1 )
483  {
484  const char* ptr_haystack2 = ptr_haystack;
485  const char* ptr_needle = needle;
486  while( 1 )
487  {
488  char ch1 = *(ptr_haystack2++);
489  char ch2 = *(ptr_needle++);
490  if( ch2 == 0 )
491  return (char*)ptr_haystack;
492  if( ch1 != ch2 )
493  break;
494  }
495  if( *ptr_haystack == 0 )
496  return NULL;
497  ptr_haystack ++;
498  }
499 }
500 
501 #undef strcmp
502 #undef strncmp
503 #define memcmp CPL_afl_friendly_memcmp
504 #define strcmp CPL_afl_friendly_strcmp
505 #define strncmp CPL_afl_friendly_strncmp
506 #define strcasecmp CPL_afl_friendly_strcasecmp
507 #define strncasecmp CPL_afl_friendly_strncasecmp
508 #define strstr CPL_afl_friendly_strstr
509 
510 #endif /* defined(AFL_FRIENDLY) && defined(__GNUC__) */
511 
512 # if defined(WIN32)
513 # define STRCASECMP(a,b) (stricmp(a,b))
514 # define STRNCASECMP(a,b,n) (strnicmp(a,b,n))
515 # else
516 # define STRCASECMP(a,b) (strcasecmp(a,b))
517 # define STRNCASECMP(a,b,n) (strncasecmp(a,b,n))
518 # endif
519 # define EQUALN(a,b,n) (STRNCASECMP(a,b,n)==0)
520 # define EQUAL(a,b) (STRCASECMP(a,b)==0)
521 #endif
522 
523 /*---------------------------------------------------------------------
524  * Does a string "a" start with string "b". Search is case-sensitive or,
525  * with CI, it is a case-insensitive comparison.
526  *--------------------------------------------------------------------- */
527 #ifndef STARTS_WITH_CI
528 #define STARTS_WITH(a,b) (strncmp(a,b,strlen(b)) == 0)
529 #define STARTS_WITH_CI(a,b) EQUALN(a,b,strlen(b))
530 #endif
531 
532 #ifndef CPL_THREADLOCAL
533 # define CPL_THREADLOCAL
534 #endif
535 
536 /* -------------------------------------------------------------------- */
537 /* Handle isnan() and isinf(). Note that isinf() and isnan() */
538 /* are supposed to be macros according to C99, defined in math.h */
539 /* Some systems (i.e. Tru64) don't have isinf() at all, so if */
540 /* the macro is not defined we just assume nothing is infinite. */
541 /* This may mean we have no real CPLIsInf() on systems with isinf()*/
542 /* function but no corresponding macro, but I can live with */
543 /* that since it isn't that important a test. */
544 /* -------------------------------------------------------------------- */
545 #ifdef _MSC_VER
546 # include <float.h>
547 # define CPLIsNan(x) _isnan(x)
548 # define CPLIsInf(x) (!_isnan(x) && !_finite(x))
549 # define CPLIsFinite(x) _finite(x)
550 #else
551 # define CPLIsNan(x) isnan(x)
552 # ifdef isinf
553 # define CPLIsInf(x) isinf(x)
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 
565 /*---------------------------------------------------------------------
566  * CPL_LSB and CPL_MSB
567  * Only one of these 2 macros should be defined and specifies the byte
568  * ordering for the current platform.
569  * This should be defined in the Makefile, but if it is not then
570  * the default is CPL_LSB (Intel ordering, LSB first).
571  *--------------------------------------------------------------------*/
572 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
573 # define CPL_MSB
574 #endif
575 
576 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
577 #define CPL_LSB
578 #endif
579 
580 #if defined(CPL_LSB)
581 # define CPL_IS_LSB 1
582 #else
583 # define CPL_IS_LSB 0
584 #endif
585 
586 #ifdef __cplusplus
587 
588 extern "C++" {
589 
590 template <bool b> struct CPLStaticAssert {};
591 template<> struct CPLStaticAssert<true>
592 {
593  static void my_function() {}
594 };
595 
596 } /* extern "C++" */
597 
598 #define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
599 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
600 
601 #else /* __cplusplus */
602 
603 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
604 
605 #endif /* __cplusplus */
606 
607 /*---------------------------------------------------------------------
608  * Little endian <==> big endian byte swap macros.
609  *--------------------------------------------------------------------*/
610 
611 #define CPL_SWAP16(x) \
612  ((GUInt16)( \
613  (((GUInt16)(x) & 0x00ffU) << 8) | \
614  (((GUInt16)(x) & 0xff00U) >> 8) ))
615 
616 #define CPL_SWAP16PTR(x) \
617 { \
618  GByte byTemp, *_pabyDataT = (GByte *) (x); \
619  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2); \
620  \
621  byTemp = _pabyDataT[0]; \
622  _pabyDataT[0] = _pabyDataT[1]; \
623  _pabyDataT[1] = byTemp; \
624 }
625 
626 #define CPL_SWAP32(x) \
627  ((GUInt32)( \
628  (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
629  (((GUInt32)(x) & (GUInt32)0x0000ff00UL) << 8) | \
630  (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >> 8) | \
631  (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
632 
633 #define CPL_SWAP32PTR(x) \
634 { \
635  GByte byTemp, *_pabyDataT = (GByte *) (x); \
636  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
637  \
638  byTemp = _pabyDataT[0]; \
639  _pabyDataT[0] = _pabyDataT[3]; \
640  _pabyDataT[3] = byTemp; \
641  byTemp = _pabyDataT[1]; \
642  _pabyDataT[1] = _pabyDataT[2]; \
643  _pabyDataT[2] = byTemp; \
644 }
645 
646 #define CPL_SWAP64PTR(x) \
647 { \
648  GByte byTemp, *_pabyDataT = (GByte *) (x); \
649  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
650  \
651  byTemp = _pabyDataT[0]; \
652  _pabyDataT[0] = _pabyDataT[7]; \
653  _pabyDataT[7] = byTemp; \
654  byTemp = _pabyDataT[1]; \
655  _pabyDataT[1] = _pabyDataT[6]; \
656  _pabyDataT[6] = byTemp; \
657  byTemp = _pabyDataT[2]; \
658  _pabyDataT[2] = _pabyDataT[5]; \
659  _pabyDataT[5] = byTemp; \
660  byTemp = _pabyDataT[3]; \
661  _pabyDataT[3] = _pabyDataT[4]; \
662  _pabyDataT[4] = byTemp; \
663 }
664 
665 
666 /* Until we have a safe 64 bits integer data type defined, we'll replace
667  * this version of the CPL_SWAP64() macro with a less efficient one.
668  */
669 /*
670 #define CPL_SWAP64(x) \
671  ((uint64)( \
672  (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
673  (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
674  (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
675  (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \
676  (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \
677  (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
678  (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
679  (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
680 */
681 
682 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
683 
684 #ifdef CPL_MSB
685 # define CPL_MSBWORD16(x) (x)
686 # define CPL_LSBWORD16(x) CPL_SWAP16(x)
687 # define CPL_MSBWORD32(x) (x)
688 # define CPL_LSBWORD32(x) CPL_SWAP32(x)
689 # define CPL_MSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
690 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
691 # define CPL_MSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
692 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
693 # define CPL_MSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
694 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
695 #else
696 # define CPL_LSBWORD16(x) (x)
697 # define CPL_MSBWORD16(x) CPL_SWAP16(x)
698 # define CPL_LSBWORD32(x) (x)
699 # define CPL_MSBWORD32(x) CPL_SWAP32(x)
700 # define CPL_LSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
701 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
702 # define CPL_LSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
703 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
704 # define CPL_LSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
705 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
706 #endif
707 
709 #define CPL_LSBINT16PTR(x) ((*(GByte*)(x)) | (*(((GByte*)(x))+1) << 8))
710 
712 #define CPL_LSBINT32PTR(x) ((*(GByte*)(x)) | (*(((GByte*)(x))+1) << 8) | \
713  (*(((GByte*)(x))+2) << 16) | (*(((GByte*)(x))+3) << 24))
714 
716 #define CPL_LSBSINT16PTR(x) ((GInt16) CPL_LSBINT16PTR(x))
717 
719 #define CPL_LSBUINT16PTR(x) ((GUInt16)CPL_LSBINT16PTR(x))
720 
722 #define CPL_LSBSINT32PTR(x) ((GInt32) CPL_LSBINT32PTR(x))
723 
725 #define CPL_LSBUINT32PTR(x) ((GUInt32)CPL_LSBINT32PTR(x))
726 
727 
728 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
729 #ifndef UNREFERENCED_PARAM
730 # ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
731 # define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
732 # else
733 # define UNREFERENCED_PARAM(param) ((void)param)
734 # endif /* UNREFERENCED_PARAMETER */
735 #endif /* UNREFERENCED_PARAM */
736 
737 /***********************************************************************
738  * Define CPL_CVSID() macro. It can be disabled during a build by
739  * defining DISABLE_CVSID in the compiler options.
740  *
741  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
742  * being unused.
743  */
744 
745 #ifndef DISABLE_CVSID
746 #if defined(__GNUC__) && __GNUC__ >= 4
747 # define CPL_CVSID(string) static const char cpl_cvsid[] __attribute__((used)) = string;
748 #else
749 # define CPL_CVSID(string) static const char cpl_cvsid[] = string; \
750 static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
751 #endif
752 #else
753 # define CPL_CVSID(string)
754 #endif
755 
756 /* Null terminated variadic */
757 /* We exclude mingw64 4.6 which seems to be broken regarding this */
758 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
759 # define CPL_NULL_TERMINATED __attribute__((__sentinel__))
760 #else
761 # define CPL_NULL_TERMINATED
762 #endif
763 
764 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
765 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
766 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
767 #else
768 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
769 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )
770 #endif
771 
772 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
773 #define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
774 #else
775 #define CPL_WARN_UNUSED_RESULT
776 #endif
777 
778 #if defined(__GNUC__) && __GNUC__ >= 4
779 # define CPL_UNUSED __attribute((__unused__))
780 #else
781 /* TODO: add cases for other compilers */
782 # define CPL_UNUSED
783 #endif
784 
785 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
786 #define CPL_NO_RETURN __attribute__((noreturn))
787 #else
788 #define CPL_NO_RETURN
789 #endif
790 
791 /* Clang __has_attribute */
792 #ifndef __has_attribute
793  #define __has_attribute(x) 0 // Compatibility with non-clang compilers.
794 #endif
795 
796 #if ((defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || __has_attribute(returns_nonnull)) && !defined(DOXYGEN_SKIP)
797 # define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
798 #else
799 # define CPL_RETURNS_NONNULL
800 #endif
801 
802 
803 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
804 #define CPL_RESTRICT __restrict__
805 #else
806 #define CPL_RESTRICT
807 #endif
808 
809 /* Helper to remove the copy and assignment constructors so that the compiler
810  will not generate the default versions.
811 
812  Must be placed in the private section of a class and should be at the end.
813 */
814 #ifdef __cplusplus
815 
816 #if HAVE_CXX11
817 # define CPL_FINAL final
818 # define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
819  ClassName( const ClassName & ) = delete; \
820  ClassName &operator=( const ClassName & ) = delete;
821 #else
822 # define CPL_FINAL
823 # define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
824  ClassName( const ClassName & ); \
825  ClassName &operator=( const ClassName & );
826 #endif /* HAVE_CXX11 */
827 
828 #endif /* __cplusplus */
829 
830 #if !defined(DOXYGEN_SKIP)
831 #if defined(__has_extension)
832  #if __has_extension(attribute_deprecated_with_message)
833  /* Clang extension */
834  #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated(x)))
835  #else
836  #define CPL_WARN_DEPRECATED(x)
837  #endif
838 #elif defined(__GNUC__)
839  #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated))
840 #else
841  #define CPL_WARN_DEPRECATED(x)
842 #endif
843 #endif
844 
845 #if !defined(_MSC_VER) && !defined(__APPLE__)
846 CPL_C_START
847 #ifdef WARN_STANDARD_PRINTF
848 int vsnprintf(char *str, size_t size, const char* fmt, va_list args) CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");
849 int snprintf(char *str, size_t size, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(3,4) CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
850 int sprintf(char *str, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
851 #elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
852 int sprintf(char *str, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_DEPRECATED("Use snprintf() or CPLsnprintf() instead");
853 #endif
854 CPL_C_END
855 #endif /* !defined(_MSC_VER) && !defined(__APPLE__) */
856 
857 #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
858 #define CPL_CPU_REQUIRES_ALIGNED_ACCESS
859 #define CPL_IS_DOUBLE_A_INT(d) ( (d) >= INT_MIN && (d) <= INT_MAX && (double)(int)(d) == (d) )
860 #else
861 /* This is technically unspecified behaviour if the double is out of range, but works OK on x86 */
862 #define CPL_IS_DOUBLE_A_INT(d) ( (double)(int)(d) == (d) )
863 #endif
864 
865 #ifdef __cplusplus
866 /* The size of C style arrays. */
867 #define CPL_ARRAYSIZE(array) \
868  ((sizeof(array) / sizeof(*(array))) / \
869  static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
870 
871 extern "C++" {
872 template<class T> static void CPL_IGNORE_RET_VAL(T) {}
873 inline static bool CPL_TO_BOOL(int x) { return x != 0; }
874 } /* extern "C++" */
875 
876 #endif /* __cplusplus */
877 
878 #if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (defined(__clang__) && __clang_major__ >= 3)) && !defined(_MSC_VER))
879 #define HAVE_GCC_DIAGNOSTIC_PUSH
880 #endif
881 
882 #if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && !defined(_MSC_VER))
883 #define HAVE_GCC_SYSTEM_HEADER
884 #endif
885 
886 #if defined(__clang__)
887 # define CPL_FALLTHROUGH [[clang::fallthrough]];
888 #else
889 # define CPL_FALLTHROUGH
890 #endif
891 
892 // Define DEBUG_BOOL to compile in "MSVC mode", ie error out when
893 // a integer is assigned to a bool
894 // WARNING: use only at compilation time, since it is know to not work
895 // at runtime for unknown reasons (crash in MongoDB driver for example)
896 #if defined(__cplusplus) && defined(DEBUG_BOOL) && !defined(DO_NOT_USE_DEBUG_BOOL)
897 extern "C++" {
898 class MSVCPedanticBool
899 {
900 
901  friend bool operator== (const bool& one, const MSVCPedanticBool& other);
902  friend bool operator!= (const bool& one, const MSVCPedanticBool& other);
903 
904  bool b;
905  MSVCPedanticBool(int bIn);
906 
907  public:
908  /* b not initialized on purpose in default ctor to flag use. */
909  /* cppcheck-suppress uninitMemberVar */
910  MSVCPedanticBool() {}
911  MSVCPedanticBool(bool bIn) : b(bIn) {}
912  MSVCPedanticBool(const MSVCPedanticBool& other) : b(other.b) {}
913 
914  MSVCPedanticBool& operator= (const MSVCPedanticBool& other) { b = other.b; return *this; }
915  MSVCPedanticBool& operator&= (const MSVCPedanticBool& other) { b &= other.b; return *this; }
916  MSVCPedanticBool& operator|= (const MSVCPedanticBool& other) { b |= other.b; return *this; }
917 
918  bool operator== (const bool& other) const { return b == other; }
919  bool operator!= (const bool& other) const { return b != other; }
920  bool operator== (const MSVCPedanticBool& other) const { return b == other.b; }
921  bool operator!= (const MSVCPedanticBool& other) const { return b != other.b; }
922 
923  bool operator! () const { return !b; }
924  operator bool() const { return b; }
925  operator int() const { return b; }
926 };
927 
928 inline bool operator== (const bool& one, const MSVCPedanticBool& other) { return one == other.b; }
929 inline bool operator!= (const bool& one, const MSVCPedanticBool& other) { return one != other.b; }
930 
931 /* We must include all C++ stuff before to avoid issues with templates that use bool */
932 #include <vector>
933 #include <map>
934 #include <set>
935 #include <string>
936 #include <cstddef>
937 #include <limits>
938 #include <sstream>
939 #include <fstream>
940 #include <algorithm>
941 
942 } /* extern C++ */
943 
944 #undef FALSE
945 #define FALSE false
946 #undef TRUE
947 #define TRUE true
948 
949 /* In the very few cases we really need a "simple" type, fallback to bool */
950 #define EMULATED_BOOL int
951 
952 /* Use our class instead of bool */
953 #define bool MSVCPedanticBool
954 
955 /* "volatile bool" with the below substitution doesn't really work. */
956 /* Just for the sake of the debug, we don't really need volatile */
957 #define VOLATILE_BOOL bool
958 
959 #else /* defined(__cplusplus) && defined(DEBUG_BOOL) */
960 
961 #ifndef FALSE
962 # define FALSE 0
963 #endif
964 
965 #ifndef TRUE
966 # define TRUE 1
967 #endif
968 
969 #define EMULATED_BOOL bool
970 #define VOLATILE_BOOL volatile bool
971 
972 #endif /* defined(__cplusplus) && defined(DEBUG_BOOL) */
973 
974 #endif /* ndef CPL_BASE_H_INCLUDED */
Definition: cpl_port.h:590
int CPLsnprintf(char *str, size_t size, const char *fmt,...)
snprintf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition: cpl_string.cpp:1305

Generated for GDAL by doxygen 1.8.11.