GDAL
cpl_port.h
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id: cpl_port.h 34135 2016-04-27 21:44:07Z rouault $
3  *
4  * Project: CPL - Common Portability Library
5  * Author: Frank Warmerdam, warmerdam@pobox.com
6  * Purpose: Include file providing low level portability services for CPL.
7  * This should be the first include file for any CPL based code.
8  *
9  ******************************************************************************
10  * Copyright (c) 1998, 2005, Frank Warmerdam <warmerdam@pobox.com>
11  * Copyright (c) 2008-2013, Even Rouault <even dot rouault at mines-paris dot org>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included
21  * in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  ****************************************************************************/
31 
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 #elif defined(HAVE_CXX11) && HAVE_CXX11 && defined(__GNUC__)
551 /* When including <cmath> in C++11 the isnan() macro is undefined, so that */
552 /* std::isnan() can work (#6489). This is a GCC specific workaround for now. */
553 # define CPLIsNan(x) __builtin_isnan(x)
554 # define CPLIsInf(x) __builtin_isinf(x)
555 # define CPLIsFinite(x) __builtin_isfinite(x)
556 #else
557 # define CPLIsNan(x) isnan(x)
558 # ifdef isinf
559 # define CPLIsInf(x) isinf(x)
560 # define CPLIsFinite(x) (!isnan(x) && !isinf(x))
561 # elif defined(__sun__)
562 # include <ieeefp.h>
563 # define CPLIsInf(x) (!finite(x) && !isnan(x))
564 # define CPLIsFinite(x) finite(x)
565 # else
566 # define CPLIsInf(x) (0)
567 # define CPLIsFinite(x) (!isnan(x))
568 # endif
569 #endif
570 
571 /*---------------------------------------------------------------------
572  * CPL_LSB and CPL_MSB
573  * Only one of these 2 macros should be defined and specifies the byte
574  * ordering for the current platform.
575  * This should be defined in the Makefile, but if it is not then
576  * the default is CPL_LSB (Intel ordering, LSB first).
577  *--------------------------------------------------------------------*/
578 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
579 # define CPL_MSB
580 #endif
581 
582 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
583 #define CPL_LSB
584 #endif
585 
586 #if defined(CPL_LSB)
587 # define CPL_IS_LSB 1
588 #else
589 # define CPL_IS_LSB 0
590 #endif
591 
592 #ifdef __cplusplus
593 
594 extern "C++" {
595 
596 template <bool b> struct CPLStaticAssert {};
597 template<> struct CPLStaticAssert<true>
598 {
599  static void my_function() {}
600 };
601 
602 } /* extern "C++" */
603 
604 #define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
605 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
606 
607 #else /* __cplusplus */
608 
609 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
610 
611 #endif /* __cplusplus */
612 
613 /*---------------------------------------------------------------------
614  * Little endian <==> big endian byte swap macros.
615  *--------------------------------------------------------------------*/
616 
617 #define CPL_SWAP16(x) \
618  ((GUInt16)( \
619  (((GUInt16)(x) & 0x00ffU) << 8) | \
620  (((GUInt16)(x) & 0xff00U) >> 8) ))
621 
622 #define CPL_SWAP16PTR(x) \
623 { \
624  GByte byTemp, *_pabyDataT = (GByte *) (x); \
625  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2); \
626  \
627  byTemp = _pabyDataT[0]; \
628  _pabyDataT[0] = _pabyDataT[1]; \
629  _pabyDataT[1] = byTemp; \
630 }
631 
632 #define CPL_SWAP32(x) \
633  ((GUInt32)( \
634  (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
635  (((GUInt32)(x) & (GUInt32)0x0000ff00UL) << 8) | \
636  (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >> 8) | \
637  (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
638 
639 #define CPL_SWAP32PTR(x) \
640 { \
641  GByte byTemp, *_pabyDataT = (GByte *) (x); \
642  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
643  \
644  byTemp = _pabyDataT[0]; \
645  _pabyDataT[0] = _pabyDataT[3]; \
646  _pabyDataT[3] = byTemp; \
647  byTemp = _pabyDataT[1]; \
648  _pabyDataT[1] = _pabyDataT[2]; \
649  _pabyDataT[2] = byTemp; \
650 }
651 
652 #define CPL_SWAP64PTR(x) \
653 { \
654  GByte byTemp, *_pabyDataT = (GByte *) (x); \
655  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
656  \
657  byTemp = _pabyDataT[0]; \
658  _pabyDataT[0] = _pabyDataT[7]; \
659  _pabyDataT[7] = byTemp; \
660  byTemp = _pabyDataT[1]; \
661  _pabyDataT[1] = _pabyDataT[6]; \
662  _pabyDataT[6] = byTemp; \
663  byTemp = _pabyDataT[2]; \
664  _pabyDataT[2] = _pabyDataT[5]; \
665  _pabyDataT[5] = byTemp; \
666  byTemp = _pabyDataT[3]; \
667  _pabyDataT[3] = _pabyDataT[4]; \
668  _pabyDataT[4] = byTemp; \
669 }
670 
671 
672 /* Until we have a safe 64 bits integer data type defined, we'll replace
673  * this version of the CPL_SWAP64() macro with a less efficient one.
674  */
675 /*
676 #define CPL_SWAP64(x) \
677  ((uint64)( \
678  (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
679  (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
680  (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
681  (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \
682  (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \
683  (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
684  (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
685  (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
686 */
687 
688 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
689 
690 #ifdef CPL_MSB
691 # define CPL_MSBWORD16(x) (x)
692 # define CPL_LSBWORD16(x) CPL_SWAP16(x)
693 # define CPL_MSBWORD32(x) (x)
694 # define CPL_LSBWORD32(x) CPL_SWAP32(x)
695 # define CPL_MSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
696 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
697 # define CPL_MSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
698 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
699 # define CPL_MSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
700 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
701 #else
702 # define CPL_LSBWORD16(x) (x)
703 # define CPL_MSBWORD16(x) CPL_SWAP16(x)
704 # define CPL_LSBWORD32(x) (x)
705 # define CPL_MSBWORD32(x) CPL_SWAP32(x)
706 # define CPL_LSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
707 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
708 # define CPL_LSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
709 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
710 # define CPL_LSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
711 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
712 #endif
713 
715 #define CPL_LSBINT16PTR(x) ((*(GByte*)(x)) | (*(((GByte*)(x))+1) << 8))
716 
718 #define CPL_LSBINT32PTR(x) ((*(GByte*)(x)) | (*(((GByte*)(x))+1) << 8) | \
719  (*(((GByte*)(x))+2) << 16) | (*(((GByte*)(x))+3) << 24))
720 
722 #define CPL_LSBSINT16PTR(x) ((GInt16) CPL_LSBINT16PTR(x))
723 
725 #define CPL_LSBUINT16PTR(x) ((GUInt16)CPL_LSBINT16PTR(x))
726 
728 #define CPL_LSBSINT32PTR(x) ((GInt32) CPL_LSBINT32PTR(x))
729 
731 #define CPL_LSBUINT32PTR(x) ((GUInt32)CPL_LSBINT32PTR(x))
732 
733 
734 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
735 #ifndef UNREFERENCED_PARAM
736 # ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
737 # define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
738 # else
739 # define UNREFERENCED_PARAM(param) ((void)param)
740 # endif /* UNREFERENCED_PARAMETER */
741 #endif /* UNREFERENCED_PARAM */
742 
743 /***********************************************************************
744  * Define CPL_CVSID() macro. It can be disabled during a build by
745  * defining DISABLE_CVSID in the compiler options.
746  *
747  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
748  * being unused.
749  */
750 
751 #ifndef DISABLE_CVSID
752 #if defined(__GNUC__) && __GNUC__ >= 4
753 # define CPL_CVSID(string) static const char cpl_cvsid[] __attribute__((used)) = string;
754 #else
755 # define CPL_CVSID(string) static const char cpl_cvsid[] = string; \
756 static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
757 #endif
758 #else
759 # define CPL_CVSID(string)
760 #endif
761 
762 /* Null terminated variadic */
763 /* We exclude mingw64 4.6 which seems to be broken regarding this */
764 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
765 # define CPL_NULL_TERMINATED __attribute__((__sentinel__))
766 #else
767 # define CPL_NULL_TERMINATED
768 #endif
769 
770 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
771 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
772 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
773 #else
774 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
775 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )
776 #endif
777 
778 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
779 #define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
780 #else
781 #define CPL_WARN_UNUSED_RESULT
782 #endif
783 
784 #if defined(__GNUC__) && __GNUC__ >= 4
785 # define CPL_UNUSED __attribute((__unused__))
786 #else
787 /* TODO: add cases for other compilers */
788 # define CPL_UNUSED
789 #endif
790 
791 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
792 #define CPL_NO_RETURN __attribute__((noreturn))
793 #else
794 #define CPL_NO_RETURN
795 #endif
796 
797 /* Clang __has_attribute */
798 #ifndef __has_attribute
799  #define __has_attribute(x) 0 // Compatibility with non-clang compilers.
800 #endif
801 
802 #if ((defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || __has_attribute(returns_nonnull)) && !defined(DOXYGEN_SKIP)
803 # define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
804 #else
805 # define CPL_RETURNS_NONNULL
806 #endif
807 
808 
809 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
810 #define CPL_RESTRICT __restrict__
811 #else
812 #define CPL_RESTRICT
813 #endif
814 
815 /* Helper to remove the copy and assignment constructors so that the compiler
816  will not generate the default versions.
817 
818  Must be placed in the private section of a class and should be at the end.
819 */
820 #ifdef __cplusplus
821 
822 #if HAVE_CXX11
823 # define CPL_FINAL final
824 # define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
825  ClassName( const ClassName & ) = delete; \
826  ClassName &operator=( const ClassName & ) = delete;
827 #else
828 # define CPL_FINAL
829 # define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
830  ClassName( const ClassName & ); \
831  ClassName &operator=( const ClassName & );
832 #endif /* HAVE_CXX11 */
833 
834 #endif /* __cplusplus */
835 
836 #if !defined(DOXYGEN_SKIP)
837 #if defined(__has_extension)
838  #if __has_extension(attribute_deprecated_with_message)
839  /* Clang extension */
840  #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated(x)))
841  #else
842  #define CPL_WARN_DEPRECATED(x)
843  #endif
844 #elif defined(__GNUC__)
845  #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated))
846 #else
847  #define CPL_WARN_DEPRECATED(x)
848 #endif
849 #endif
850 
851 #if !defined(_MSC_VER) && !defined(__APPLE__)
852 CPL_C_START
853 #ifdef WARN_STANDARD_PRINTF
854 int vsnprintf(char *str, size_t size, const char* fmt, va_list args) CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");
855 int snprintf(char *str, size_t size, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(3,4) CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
856 int sprintf(char *str, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
857 #elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
858 int sprintf(char *str, const char* fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3) CPL_WARN_DEPRECATED("Use snprintf() or CPLsnprintf() instead");
859 #endif
860 CPL_C_END
861 #endif /* !defined(_MSC_VER) && !defined(__APPLE__) */
862 
863 #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
864 #define CPL_CPU_REQUIRES_ALIGNED_ACCESS
865 #define CPL_IS_DOUBLE_A_INT(d) ( (d) >= INT_MIN && (d) <= INT_MAX && (double)(int)(d) == (d) )
866 #else
867 /* This is technically unspecified behaviour if the double is out of range, but works OK on x86 */
868 #define CPL_IS_DOUBLE_A_INT(d) ( (double)(int)(d) == (d) )
869 #endif
870 
871 #ifdef __cplusplus
872 /* The size of C style arrays. */
873 #define CPL_ARRAYSIZE(array) \
874  ((sizeof(array) / sizeof(*(array))) / \
875  static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
876 
877 extern "C++" {
878 template<class T> static void CPL_IGNORE_RET_VAL(T) {}
879 inline static bool CPL_TO_BOOL(int x) { return x != 0; }
880 } /* extern "C++" */
881 
882 #endif /* __cplusplus */
883 
884 #if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (defined(__clang__) && __clang_major__ >= 3)) && !defined(_MSC_VER))
885 #define HAVE_GCC_DIAGNOSTIC_PUSH
886 #endif
887 
888 #if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && !defined(_MSC_VER))
889 #define HAVE_GCC_SYSTEM_HEADER
890 #endif
891 
892 #if defined(__clang__)
893 # define CPL_FALLTHROUGH [[clang::fallthrough]];
894 #else
895 # define CPL_FALLTHROUGH
896 #endif
897 
898 // Define DEBUG_BOOL to compile in "MSVC mode", ie error out when
899 // a integer is assigned to a bool
900 // WARNING: use only at compilation time, since it is know to not work
901 // at runtime for unknown reasons (crash in MongoDB driver for example)
902 #if defined(__cplusplus) && defined(DEBUG_BOOL) && !defined(DO_NOT_USE_DEBUG_BOOL)
903 extern "C++" {
904 class MSVCPedanticBool
905 {
906 
907  friend bool operator== (const bool& one, const MSVCPedanticBool& other);
908  friend bool operator!= (const bool& one, const MSVCPedanticBool& other);
909 
910  bool b;
911  MSVCPedanticBool(int bIn);
912 
913  public:
914  /* b not initialized on purpose in default ctor to flag use. */
915  /* cppcheck-suppress uninitMemberVar */
916  MSVCPedanticBool() {}
917  MSVCPedanticBool(bool bIn) : b(bIn) {}
918  MSVCPedanticBool(const MSVCPedanticBool& other) : b(other.b) {}
919 
920  MSVCPedanticBool& operator= (const MSVCPedanticBool& other) { b = other.b; return *this; }
921  MSVCPedanticBool& operator&= (const MSVCPedanticBool& other) { b &= other.b; return *this; }
922  MSVCPedanticBool& operator|= (const MSVCPedanticBool& other) { b |= other.b; return *this; }
923 
924  bool operator== (const bool& other) const { return b == other; }
925  bool operator!= (const bool& other) const { return b != other; }
926  bool operator== (const MSVCPedanticBool& other) const { return b == other.b; }
927  bool operator!= (const MSVCPedanticBool& other) const { return b != other.b; }
928 
929  bool operator! () const { return !b; }
930  operator bool() const { return b; }
931  operator int() const { return b; }
932 };
933 
934 inline bool operator== (const bool& one, const MSVCPedanticBool& other) { return one == other.b; }
935 inline bool operator!= (const bool& one, const MSVCPedanticBool& other) { return one != other.b; }
936 
937 /* We must include all C++ stuff before to avoid issues with templates that use bool */
938 #include <vector>
939 #include <map>
940 #include <set>
941 #include <string>
942 #include <cstddef>
943 #include <limits>
944 #include <sstream>
945 #include <fstream>
946 #include <algorithm>
947 
948 } /* extern C++ */
949 
950 #undef FALSE
951 #define FALSE false
952 #undef TRUE
953 #define TRUE true
954 
955 /* In the very few cases we really need a "simple" type, fallback to bool */
956 #define EMULATED_BOOL int
957 
958 /* Use our class instead of bool */
959 #define bool MSVCPedanticBool
960 
961 /* "volatile bool" with the below substitution doesn't really work. */
962 /* Just for the sake of the debug, we don't really need volatile */
963 #define VOLATILE_BOOL bool
964 
965 #else /* defined(__cplusplus) && defined(DEBUG_BOOL) */
966 
967 #ifndef FALSE
968 # define FALSE 0
969 #endif
970 
971 #ifndef TRUE
972 # define TRUE 1
973 #endif
974 
975 #define EMULATED_BOOL bool
976 #define VOLATILE_BOOL volatile bool
977 
978 #endif /* defined(__cplusplus) && defined(DEBUG_BOOL) */
979 
980 #endif /* ndef CPL_BASE_H_INCLUDED */
Definition: cpl_port.h:596
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.12.