GDAL
cpl_port.h
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id: cpl_port.h 27701 2014-09-20 15:07:02Z 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 macos_pre10 to indicate compilation with MacOS */
44 /* versions before MacOS X. */
45 /* ==================================================================== */
46 #ifdef macintosh
47 # define macos_pre10
48 #endif
49 
50 /* ==================================================================== */
51 /* We will use WIN32 as a standard windows define. */
52 /* ==================================================================== */
53 #if defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE)
54 # define WIN32
55 #endif
56 
57 #if defined(_WINDOWS) && !defined(WIN32) && !defined(_WIN32_WCE)
58 # define WIN32
59 #endif
60 
61 /* ==================================================================== */
62 /* We will use WIN32CE as a standard Windows CE (Mobile) define. */
63 /* ==================================================================== */
64 #if defined(_WIN32_WCE)
65 # define WIN32CE
66 #endif
67 
68 /* -------------------------------------------------------------------- */
69 /* The following apparently allow you to use strcpy() and other */
70 /* functions judged "unsafe" by microsoft in VS 8 (2005). */
71 /* -------------------------------------------------------------------- */
72 #ifdef _MSC_VER
73 # ifndef _CRT_SECURE_NO_DEPRECATE
74 # define _CRT_SECURE_NO_DEPRECATE
75 # endif
76 # ifndef _CRT_NONSTDC_NO_DEPRECATE
77 # define _CRT_NONSTDC_NO_DEPRECATE
78 # endif
79 #endif
80 
81 #include "cpl_config.h"
82 
83 /* ==================================================================== */
84 /* A few sanity checks, mainly to detect problems that sometimes */
85 /* arise with bad configured cross-compilation. */
86 /* ==================================================================== */
87 
88 #if !defined(SIZEOF_INT) || SIZEOF_INT != 4
89 #error "Unexpected value for SIZEOF_INT"
90 #endif
91 
92 #if !defined(SIZEOF_UNSIGNED_LONG) || (SIZEOF_UNSIGNED_LONG != 4 && SIZEOF_UNSIGNED_LONG != 8)
93 #error "Unexpected value for SIZEOF_UNSIGNED_LONG"
94 #endif
95 
96 #if !defined(SIZEOF_VOIDP) || (SIZEOF_VOIDP != 4 && SIZEOF_VOIDP != 8)
97 #error "Unexpected value for SIZEOF_VOIDP"
98 #endif
99 
100 
101 /* ==================================================================== */
102 /* This will disable most WIN32 stuff in a Cygnus build which */
103 /* defines unix to 1. */
104 /* ==================================================================== */
105 
106 #ifdef unix
107 # undef WIN32
108 # undef WIN32CE
109 #endif
110 
111 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
112 # define _LARGEFILE64_SOURCE 1
113 #endif
114 
115 /* ==================================================================== */
116 /* If iconv() is available use extended recoding module. */
117 /* Stub implementation is always compiled in, because it works */
118 /* faster than iconv() for encodings it supports. */
119 /* ==================================================================== */
120 
121 #if defined(HAVE_ICONV)
122 # define CPL_RECODE_ICONV
123 #endif
124 
125 #define CPL_RECODE_STUB
126 
127 /* ==================================================================== */
128 /* MinGW stuff */
129 /* ==================================================================== */
130 
131 /* We need __MSVCRT_VERSION__ >= 0x0601 to have "struct __stat64" */
132 /* Latest versions of mingw32 define it, but with older ones, */
133 /* we need to define it manually */
134 #if defined(__MINGW32__)
135 #ifndef __MSVCRT_VERSION__
136 #define __MSVCRT_VERSION__ 0x0601
137 #endif
138 #endif
139 
140 /* ==================================================================== */
141 /* Standard include files. */
142 /* ==================================================================== */
143 
144 #include <stdio.h>
145 #include <stdlib.h>
146 #include <math.h>
147 #include <stdarg.h>
148 #include <string.h>
149 #include <ctype.h>
150 #include <limits.h>
151 
152 #if !defined(WIN32CE)
153 # include <time.h>
154 #else
155 # include <wce_time.h>
156 # include <wce_errno.h>
157 #endif
158 
159 
160 #if defined(HAVE_ERRNO_H)
161 # include <errno.h>
162 #endif
163 
164 #ifdef HAVE_LOCALE_H
165 # include <locale.h>
166 #endif
167 
168 #ifdef HAVE_DIRECT_H
169 # include <direct.h>
170 #endif
171 
172 #if !(defined(WIN32) || defined(WIN32CE))
173 # include <strings.h>
174 #endif
175 
176 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
177 # define DBMALLOC
178 # include <dbmalloc.h>
179 #endif
180 
181 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
182 # define USE_DMALLOC
183 # include <dmalloc.h>
184 #endif
185 
186 /* ==================================================================== */
187 /* Base portability stuff ... this stuff may need to be */
188 /* modified for new platforms. */
189 /* ==================================================================== */
190 
191 /*---------------------------------------------------------------------
192  * types for 16 and 32 bits integers, etc...
193  *--------------------------------------------------------------------*/
194 #if UINT_MAX == 65535
195 typedef long GInt32;
196 typedef unsigned long GUInt32;
197 #else
198 typedef int GInt32;
199 typedef unsigned int GUInt32;
200 #endif
201 
202 typedef short GInt16;
203 typedef unsigned short GUInt16;
204 typedef unsigned char GByte;
205 /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
206 /* in include/poppler/goo/gtypes.h */
207 #ifndef CPL_GBOOL_DEFINED
208 #define CPL_GBOOL_DEFINED
209 typedef int GBool;
210 #endif
211 
212 /* -------------------------------------------------------------------- */
213 /* 64bit support */
214 /* -------------------------------------------------------------------- */
215 
216 #if defined(WIN32) && defined(_MSC_VER)
217 
218 #define VSI_LARGE_API_SUPPORTED
219 typedef __int64 GIntBig;
220 typedef unsigned __int64 GUIntBig;
221 
222 #elif HAVE_LONG_LONG
223 
224 typedef long long GIntBig;
225 typedef unsigned long long GUIntBig;
226 
227 #else
228 
229 typedef long GIntBig;
230 typedef unsigned long GUIntBig;
231 
232 #endif
233 
234 #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
235  #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
236 #elif HAVE_LONG_LONG
237  #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
238 #else
239  #define CPL_FRMT_GB_WITHOUT_PREFIX "l"
240 #endif
241 
242 #define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
243 #define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
244 
245 /* Workaround VC6 bug */
246 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
247 #define GUINTBIG_TO_DOUBLE(x) (double)(GIntBig)(x)
248 #else
249 #define GUINTBIG_TO_DOUBLE(x) (double)(x)
250 #endif
251 
252 /* ==================================================================== */
253 /* Other standard services. */
254 /* ==================================================================== */
255 #ifdef __cplusplus
256 # define CPL_C_START extern "C" {
257 # define CPL_C_END }
258 #else
259 # define CPL_C_START
260 # define CPL_C_END
261 #endif
262 
263 #ifndef CPL_DLL
264 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
265 # define CPL_DLL __declspec(dllexport)
266 #else
267 # if defined(USE_GCC_VISIBILITY_FLAG)
268 # define CPL_DLL __attribute__ ((visibility("default")))
269 # else
270 # define CPL_DLL
271 # endif
272 #endif
273 #endif
274 
275 /* Should optional (normally private) interfaces be exported? */
276 #ifdef CPL_OPTIONAL_APIS
277 # define CPL_ODLL CPL_DLL
278 #else
279 # define CPL_ODLL
280 #endif
281 
282 #ifndef CPL_STDCALL
283 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
284 # define CPL_STDCALL __stdcall
285 #else
286 # define CPL_STDCALL
287 #endif
288 #endif
289 
290 #ifdef _MSC_VER
291 # define FORCE_CDECL __cdecl
292 #else
293 # define FORCE_CDECL
294 #endif
295 
296 /* TODO : support for other compilers needed */
297 #if defined(__GNUC__) || defined(_MSC_VER)
298 #define HAS_CPL_INLINE 1
299 #define CPL_INLINE __inline
300 #elif defined(__SUNPRO_CC)
301 #define HAS_CPL_INLINE 1
302 #define CPL_INLINE inline
303 #else
304 #define CPL_INLINE
305 #endif
306 
307 #ifndef NULL
308 # define NULL 0
309 #endif
310 
311 #ifndef FALSE
312 # define FALSE 0
313 #endif
314 
315 #ifndef TRUE
316 # define TRUE 1
317 #endif
318 
319 #ifndef MAX
320 # define MIN(a,b) ((a<b) ? a : b)
321 # define MAX(a,b) ((a>b) ? a : b)
322 #endif
323 
324 #ifndef ABS
325 # define ABS(x) ((x<0) ? (-1*(x)) : x)
326 #endif
327 
328 #ifndef M_PI
329 # define M_PI 3.14159265358979323846 /* pi */
330 #endif
331 
332 /* -------------------------------------------------------------------- */
333 /* Macro to test equality of two floating point values. */
334 /* We use fabs() function instead of ABS() macro to avoid side */
335 /* effects. */
336 /* -------------------------------------------------------------------- */
337 #ifndef CPLIsEqual
338 # define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
339 #endif
340 
341 /* -------------------------------------------------------------------- */
342 /* Provide macros for case insensitive string comparisons. */
343 /* -------------------------------------------------------------------- */
344 #ifndef EQUAL
345 # if defined(WIN32) || defined(WIN32CE)
346 # define STRCASECMP(a,b) (stricmp(a,b))
347 # define STRNCASECMP(a,b,n) (strnicmp(a,b,n))
348 # else
349 # define STRCASECMP(a,b) (strcasecmp(a,b))
350 # define STRNCASECMP(a,b,n) (strncasecmp(a,b,n))
351 # endif
352 # define EQUALN(a,b,n) (STRNCASECMP(a,b,n)==0)
353 # define EQUAL(a,b) (STRCASECMP(a,b)==0)
354 #endif
355 
356 #ifdef macos_pre10
357 int strcasecmp(char * str1, char * str2);
358 int strncasecmp(char * str1, char * str2, int len);
359 char * strdup (char *instr);
360 #endif
361 
362 #ifndef CPL_THREADLOCAL
363 # define CPL_THREADLOCAL
364 #endif
365 
366 /* -------------------------------------------------------------------- */
367 /* Handle isnan() and isinf(). Note that isinf() and isnan() */
368 /* are supposed to be macros according to C99, defined in math.h */
369 /* Some systems (ie. Tru64) don't have isinf() at all, so if */
370 /* the macro is not defined we just assume nothing is infinite. */
371 /* This may mean we have no real CPLIsInf() on systems with isinf()*/
372 /* function but no corresponding macro, but I can live with */
373 /* that since it isn't that important a test. */
374 /* -------------------------------------------------------------------- */
375 #ifdef _MSC_VER
376 # include <float.h>
377 # define CPLIsNan(x) _isnan(x)
378 # define CPLIsInf(x) (!_isnan(x) && !_finite(x))
379 # define CPLIsFinite(x) _finite(x)
380 #else
381 # define CPLIsNan(x) isnan(x)
382 # ifdef isinf
383 # define CPLIsInf(x) isinf(x)
384 # define CPLIsFinite(x) (!isnan(x) && !isinf(x))
385 # else
386 # define CPLIsInf(x) FALSE
387 # define CPLIsFinite(x) (!isnan(x))
388 # endif
389 #endif
390 
391 /*---------------------------------------------------------------------
392  * CPL_LSB and CPL_MSB
393  * Only one of these 2 macros should be defined and specifies the byte
394  * ordering for the current platform.
395  * This should be defined in the Makefile, but if it is not then
396  * the default is CPL_LSB (Intel ordering, LSB first).
397  *--------------------------------------------------------------------*/
398 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
399 # define CPL_MSB
400 #endif
401 
402 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
403 #define CPL_LSB
404 #endif
405 
406 #if defined(CPL_LSB)
407 # define CPL_IS_LSB 1
408 #else
409 # define CPL_IS_LSB 0
410 #endif
411 
412 /*---------------------------------------------------------------------
413  * Little endian <==> big endian byte swap macros.
414  *--------------------------------------------------------------------*/
415 
416 #define CPL_SWAP16(x) \
417  ((GUInt16)( \
418  (((GUInt16)(x) & 0x00ffU) << 8) | \
419  (((GUInt16)(x) & 0xff00U) >> 8) ))
420 
421 #define CPL_SWAP16PTR(x) \
422 { \
423  GByte byTemp, *_pabyDataT = (GByte *) (x); \
424  \
425  byTemp = _pabyDataT[0]; \
426  _pabyDataT[0] = _pabyDataT[1]; \
427  _pabyDataT[1] = byTemp; \
428 }
429 
430 #define CPL_SWAP32(x) \
431  ((GUInt32)( \
432  (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
433  (((GUInt32)(x) & (GUInt32)0x0000ff00UL) << 8) | \
434  (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >> 8) | \
435  (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
436 
437 #define CPL_SWAP32PTR(x) \
438 { \
439  GByte byTemp, *_pabyDataT = (GByte *) (x); \
440  \
441  byTemp = _pabyDataT[0]; \
442  _pabyDataT[0] = _pabyDataT[3]; \
443  _pabyDataT[3] = byTemp; \
444  byTemp = _pabyDataT[1]; \
445  _pabyDataT[1] = _pabyDataT[2]; \
446  _pabyDataT[2] = byTemp; \
447 }
448 
449 #define CPL_SWAP64PTR(x) \
450 { \
451  GByte byTemp, *_pabyDataT = (GByte *) (x); \
452  \
453  byTemp = _pabyDataT[0]; \
454  _pabyDataT[0] = _pabyDataT[7]; \
455  _pabyDataT[7] = byTemp; \
456  byTemp = _pabyDataT[1]; \
457  _pabyDataT[1] = _pabyDataT[6]; \
458  _pabyDataT[6] = byTemp; \
459  byTemp = _pabyDataT[2]; \
460  _pabyDataT[2] = _pabyDataT[5]; \
461  _pabyDataT[5] = byTemp; \
462  byTemp = _pabyDataT[3]; \
463  _pabyDataT[3] = _pabyDataT[4]; \
464  _pabyDataT[4] = byTemp; \
465 }
466 
467 
468 /* Until we have a safe 64 bits integer data type defined, we'll replace
469  * this version of the CPL_SWAP64() macro with a less efficient one.
470  */
471 /*
472 #define CPL_SWAP64(x) \
473  ((uint64)( \
474  (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
475  (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
476  (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
477  (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \
478  (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \
479  (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
480  (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
481  (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
482 */
483 
484 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
485 
486 #ifdef CPL_MSB
487 # define CPL_MSBWORD16(x) (x)
488 # define CPL_LSBWORD16(x) CPL_SWAP16(x)
489 # define CPL_MSBWORD32(x) (x)
490 # define CPL_LSBWORD32(x) CPL_SWAP32(x)
491 # define CPL_MSBPTR16(x)
492 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
493 # define CPL_MSBPTR32(x)
494 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
495 # define CPL_MSBPTR64(x)
496 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
497 #else
498 # define CPL_LSBWORD16(x) (x)
499 # define CPL_MSBWORD16(x) CPL_SWAP16(x)
500 # define CPL_LSBWORD32(x) (x)
501 # define CPL_MSBWORD32(x) CPL_SWAP32(x)
502 # define CPL_LSBPTR16(x)
503 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
504 # define CPL_LSBPTR32(x)
505 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
506 # define CPL_LSBPTR64(x)
507 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
508 #endif
509 
511 #define CPL_LSBINT16PTR(x) ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8))
512 
514 #define CPL_LSBINT32PTR(x) ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8) | \
515  ((*(GByte*)((x)+2)) << 16) | ((*(GByte*)((x)+3)) << 24))
516 
518 #define CPL_LSBSINT16PTR(x) ((GInt16) CPL_LSBINT16PTR(x))
519 
521 #define CPL_LSBUINT16PTR(x) ((GUInt16)CPL_LSBINT16PTR(x))
522 
524 #define CPL_LSBSINT32PTR(x) ((GInt32) CPL_LSBINT32PTR(x))
525 
527 #define CPL_LSBUINT32PTR(x) ((GUInt32)CPL_LSBINT32PTR(x))
528 
529 
530 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
531 #ifndef UNREFERENCED_PARAM
532 # ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
533 # define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
534 # else
535 # define UNREFERENCED_PARAM(param) ((void)param)
536 # endif /* UNREFERENCED_PARAMETER */
537 #endif /* UNREFERENCED_PARAM */
538 
539 /***********************************************************************
540  * Define CPL_CVSID() macro. It can be disabled during a build by
541  * defining DISABLE_CPLID in the compiler options.
542  *
543  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
544  * being unused.
545  */
546 
547 #ifndef DISABLE_CVSID
548 #if defined(__GNUC__) && __GNUC__ >= 4
549 # define CPL_CVSID(string) static char cpl_cvsid[] __attribute__((used)) = string;
550 #else
551 # define CPL_CVSID(string) static char cpl_cvsid[] = string; \
552 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
553 #endif
554 #else
555 # define CPL_CVSID(string)
556 #endif
557 
558 /* Null terminated variadic */
559 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
560 # define CPL_NULL_TERMINATED __attribute__((__sentinel__))
561 #else
562 # define CPL_NULL_TERMINATED
563 #endif
564 
565 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
566 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
567 #else
568 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
569 #endif
570 
571 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
572 #define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
573 #else
574 #define CPL_WARN_UNUSED_RESULT
575 #endif
576 
577 #if defined(__GNUC__) && __GNUC__ >= 4
578 # define CPL_UNUSED __attribute((__unused__))
579 #else
580 /* TODO: add cases for other compilers */
581 # define CPL_UNUSED
582 #endif
583 
584 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
585 #define CPL_NO_RETURN __attribute__((noreturn))
586 #else
587 #define CPL_NO_RETURN
588 #endif
589 
590 #if !defined(DOXYGEN_SKIP)
591 #if defined(__has_extension)
592  #if __has_extension(attribute_deprecated_with_message)
593  /* Clang extension */
594  #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated(x)))
595  #else
596  #define CPL_WARN_DEPRECATED(x)
597  #endif
598 #elif defined(__GNUC__)
599  #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated))
600 #else
601  #define CPL_WARN_DEPRECATED(x)
602 #endif
603 #endif
604 
605 #endif /* ndef CPL_BASE_H_INCLUDED */

Generated for GDAL by doxygen 1.8.11.