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

Generated for GDAL by doxygen 1.8.6.