OGR
cpl_port.h
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id: cpl_port.h 23431 2011-11-27 15:02:24Z 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 # ifdef MSVC_USE_VLD
79 # include <vld.h>
80 # endif
81 #endif
82 
83 #include "cpl_config.h"
84 
85 /* ==================================================================== */
86 /* This will disable most WIN32 stuff in a Cygnus build which */
87 /* defines unix to 1. */
88 /* ==================================================================== */
89 
90 #ifdef unix
91 # undef WIN32
92 # undef WIN32CE
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 #if !defined(WIN32CE)
137 # include <time.h>
138 #else
139 # include <wce_time.h>
140 # include <wce_errno.h>
141 #endif
142 
143 
144 #if defined(HAVE_ERRNO_H)
145 # include <errno.h>
146 #endif
147 
148 #ifdef HAVE_LOCALE_H
149 # include <locale.h>
150 #endif
151 
152 #ifdef HAVE_DIRECT_H
153 # include <direct.h>
154 #endif
155 
156 #ifdef _AIX
157 # include <strings.h>
158 #endif
159 
160 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
161 # define DBMALLOC
162 # include <dbmalloc.h>
163 #endif
164 
165 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
166 # define USE_DMALLOC
167 # include <dmalloc.h>
168 #endif
169 
170 /* ==================================================================== */
171 /* Base portability stuff ... this stuff may need to be */
172 /* modified for new platforms. */
173 /* ==================================================================== */
174 
175 /*---------------------------------------------------------------------
176  * types for 16 and 32 bits integers, etc...
177  *--------------------------------------------------------------------*/
178 #if UINT_MAX == 65535
179 typedef long GInt32;
180 typedef unsigned long GUInt32;
181 #else
182 typedef int GInt32;
183 typedef unsigned int GUInt32;
184 #endif
185 
186 typedef short GInt16;
187 typedef unsigned short GUInt16;
188 typedef unsigned char GByte;
189 /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
190 /* in include/poppler/goo/gtypes.h */
191 #ifndef CPL_GBOOL_DEFINED
192 #define CPL_GBOOL_DEFINED
193 typedef int GBool;
194 #endif
195 
196 /* -------------------------------------------------------------------- */
197 /* 64bit support */
198 /* -------------------------------------------------------------------- */
199 
200 #if defined(WIN32) && defined(_MSC_VER)
201 
202 #define VSI_LARGE_API_SUPPORTED
203 typedef __int64 GIntBig;
204 typedef unsigned __int64 GUIntBig;
205 
206 #elif HAVE_LONG_LONG
207 
208 typedef long long GIntBig;
209 typedef unsigned long long GUIntBig;
210 
211 #else
212 
213 typedef long GIntBig;
214 typedef unsigned long GUIntBig;
215 
216 #endif
217 
218 #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
219  #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
220 #elif HAVE_LONG_LONG
221  #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
222 #else
223  #define CPL_FRMT_GB_WITHOUT_PREFIX "l"
224 #endif
225 
226 #define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
227 #define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
228 
229 /* Workaround VC6 bug */
230 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
231 #define GUINTBIG_TO_DOUBLE(x) (double)(GIntBig)(x)
232 #else
233 #define GUINTBIG_TO_DOUBLE(x) (double)(x)
234 #endif
235 
236 /* ==================================================================== */
237 /* Other standard services. */
238 /* ==================================================================== */
239 #ifdef __cplusplus
240 # define CPL_C_START extern "C" {
241 # define CPL_C_END }
242 #else
243 # define CPL_C_START
244 # define CPL_C_END
245 #endif
246 
247 #ifndef CPL_DLL
248 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
249 # define CPL_DLL __declspec(dllexport)
250 #else
251 # if defined(USE_GCC_VISIBILITY_FLAG)
252 # define CPL_DLL __attribute__ ((visibility("default")))
253 # else
254 # define CPL_DLL
255 # endif
256 #endif
257 #endif
258 
259 /* Should optional (normally private) interfaces be exported? */
260 #ifdef CPL_OPTIONAL_APIS
261 # define CPL_ODLL CPL_DLL
262 #else
263 # define CPL_ODLL
264 #endif
265 
266 #ifndef CPL_STDCALL
267 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
268 # define CPL_STDCALL __stdcall
269 #else
270 # define CPL_STDCALL
271 #endif
272 #endif
273 
274 #ifdef _MSC_VER
275 # define FORCE_CDECL __cdecl
276 #else
277 # define FORCE_CDECL
278 #endif
279 
280 /* TODO : support for other compilers needed */
281 #if defined(__GNUC__) || defined(_MSC_VER)
282 #define HAS_CPL_INLINE 1
283 #define CPL_INLINE __inline
284 #elif defined(__SUNPRO_CC)
285 #define HAS_CPL_INLINE 1
286 #define CPL_INLINE inline
287 #else
288 #define CPL_INLINE
289 #endif
290 
291 #ifndef NULL
292 # define NULL 0
293 #endif
294 
295 #ifndef FALSE
296 # define FALSE 0
297 #endif
298 
299 #ifndef TRUE
300 # define TRUE 1
301 #endif
302 
303 #ifndef MAX
304 # define MIN(a,b) ((a<b) ? a : b)
305 # define MAX(a,b) ((a>b) ? a : b)
306 #endif
307 
308 #ifndef ABS
309 # define ABS(x) ((x<0) ? (-1*(x)) : x)
310 #endif
311 
312 /* -------------------------------------------------------------------- */
313 /* Macro to test equality of two floating point values. */
314 /* We use fabs() function instead of ABS() macro to avoid side */
315 /* effects. */
316 /* -------------------------------------------------------------------- */
317 #ifndef CPLIsEqual
318 # define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
319 #endif
320 
321 /* -------------------------------------------------------------------- */
322 /* Provide macros for case insensitive string comparisons. */
323 /* -------------------------------------------------------------------- */
324 #ifndef EQUAL
325 # if defined(WIN32) || defined(WIN32CE)
326 # define STRCASECMP(a,b) (stricmp(a,b))
327 # define STRNCASECMP(a,b,n) (strnicmp(a,b,n))
328 # else
329 # define STRCASECMP(a,b) (strcasecmp(a,b))
330 # define STRNCASECMP(a,b,n) (strncasecmp(a,b,n))
331 # endif
332 # define EQUALN(a,b,n) (STRNCASECMP(a,b,n)==0)
333 # define EQUAL(a,b) (STRCASECMP(a,b)==0)
334 #endif
335 
336 #ifdef macos_pre10
337 int strcasecmp(char * str1, char * str2);
338 int strncasecmp(char * str1, char * str2, int len);
339 char * strdup (char *instr);
340 #endif
341 
342 #ifndef CPL_THREADLOCAL
343 # define CPL_THREADLOCAL
344 #endif
345 
346 /* -------------------------------------------------------------------- */
347 /* Handle isnan() and isinf(). Note that isinf() and isnan() */
348 /* are supposed to be macros according to C99, defined in math.h */
349 /* Some systems (ie. Tru64) don't have isinf() at all, so if */
350 /* the macro is not defined we just assume nothing is infinite. */
351 /* This may mean we have no real CPLIsInf() on systems with isinf()*/
352 /* function but no corresponding macro, but I can live with */
353 /* that since it isn't that important a test. */
354 /* -------------------------------------------------------------------- */
355 #ifdef _MSC_VER
356 # include <float.h>
357 # define CPLIsNan(x) _isnan(x)
358 # define CPLIsInf(x) (!_isnan(x) && !_finite(x))
359 # define CPLIsFinite(x) _finite(x)
360 #else
361 # define CPLIsNan(x) isnan(x)
362 # ifdef isinf
363 # define CPLIsInf(x) isinf(x)
364 # define CPLIsFinite(x) (!isnan(x) && !isinf(x))
365 # else
366 # define CPLIsInf(x) FALSE
367 # define CPLIsFinite(x) (!isnan(x))
368 # endif
369 #endif
370 
371 /*---------------------------------------------------------------------
372  * CPL_LSB and CPL_MSB
373  * Only one of these 2 macros should be defined and specifies the byte
374  * ordering for the current platform.
375  * This should be defined in the Makefile, but if it is not then
376  * the default is CPL_LSB (Intel ordering, LSB first).
377  *--------------------------------------------------------------------*/
378 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
379 # define CPL_MSB
380 #endif
381 
382 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
383 #define CPL_LSB
384 #endif
385 
386 #if defined(CPL_LSB)
387 # define CPL_IS_LSB 1
388 #else
389 # define CPL_IS_LSB 0
390 #endif
391 
392 /*---------------------------------------------------------------------
393  * Little endian <==> big endian byte swap macros.
394  *--------------------------------------------------------------------*/
395 
396 #define CPL_SWAP16(x) \
397  ((GUInt16)( \
398  (((GUInt16)(x) & 0x00ffU) << 8) | \
399  (((GUInt16)(x) & 0xff00U) >> 8) ))
400 
401 #define CPL_SWAP16PTR(x) \
402 { \
403  GByte byTemp, *_pabyDataT = (GByte *) (x); \
404  \
405  byTemp = _pabyDataT[0]; \
406  _pabyDataT[0] = _pabyDataT[1]; \
407  _pabyDataT[1] = byTemp; \
408 }
409 
410 #define CPL_SWAP32(x) \
411  ((GUInt32)( \
412  (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
413  (((GUInt32)(x) & (GUInt32)0x0000ff00UL) << 8) | \
414  (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >> 8) | \
415  (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
416 
417 #define CPL_SWAP32PTR(x) \
418 { \
419  GByte byTemp, *_pabyDataT = (GByte *) (x); \
420  \
421  byTemp = _pabyDataT[0]; \
422  _pabyDataT[0] = _pabyDataT[3]; \
423  _pabyDataT[3] = byTemp; \
424  byTemp = _pabyDataT[1]; \
425  _pabyDataT[1] = _pabyDataT[2]; \
426  _pabyDataT[2] = byTemp; \
427 }
428 
429 #define CPL_SWAP64PTR(x) \
430 { \
431  GByte byTemp, *_pabyDataT = (GByte *) (x); \
432  \
433  byTemp = _pabyDataT[0]; \
434  _pabyDataT[0] = _pabyDataT[7]; \
435  _pabyDataT[7] = byTemp; \
436  byTemp = _pabyDataT[1]; \
437  _pabyDataT[1] = _pabyDataT[6]; \
438  _pabyDataT[6] = byTemp; \
439  byTemp = _pabyDataT[2]; \
440  _pabyDataT[2] = _pabyDataT[5]; \
441  _pabyDataT[5] = byTemp; \
442  byTemp = _pabyDataT[3]; \
443  _pabyDataT[3] = _pabyDataT[4]; \
444  _pabyDataT[4] = byTemp; \
445 }
446 
447 
448 /* Until we have a safe 64 bits integer data type defined, we'll replace
449  * this version of the CPL_SWAP64() macro with a less efficient one.
450  */
451 /*
452 #define CPL_SWAP64(x) \
453  ((uint64)( \
454  (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
455  (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
456  (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
457  (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \
458  (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \
459  (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
460  (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
461  (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
462 */
463 
464 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
465 
466 #ifdef CPL_MSB
467 # define CPL_MSBWORD16(x) (x)
468 # define CPL_LSBWORD16(x) CPL_SWAP16(x)
469 # define CPL_MSBWORD32(x) (x)
470 # define CPL_LSBWORD32(x) CPL_SWAP32(x)
471 # define CPL_MSBPTR16(x)
472 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
473 # define CPL_MSBPTR32(x)
474 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
475 # define CPL_MSBPTR64(x)
476 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
477 #else
478 # define CPL_LSBWORD16(x) (x)
479 # define CPL_MSBWORD16(x) CPL_SWAP16(x)
480 # define CPL_LSBWORD32(x) (x)
481 # define CPL_MSBWORD32(x) CPL_SWAP32(x)
482 # define CPL_LSBPTR16(x)
483 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
484 # define CPL_LSBPTR32(x)
485 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
486 # define CPL_LSBPTR64(x)
487 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
488 #endif
489 
491 #define CPL_LSBINT16PTR(x) ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8))
492 
494 #define CPL_LSBINT32PTR(x) ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8) | \
495  ((*(GByte*)((x)+2)) << 16) | ((*(GByte*)((x)+3)) << 24))
496 
497 
498 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
499 #ifndef UNREFERENCED_PARAM
500 # ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
501 # define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
502 # else
503 # define UNREFERENCED_PARAM(param) ((void)param)
504 # endif /* UNREFERENCED_PARAMETER */
505 #endif /* UNREFERENCED_PARAM */
506 
507 /***********************************************************************
508  * Define CPL_CVSID() macro. It can be disabled during a build by
509  * defining DISABLE_CPLID in the compiler options.
510  *
511  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
512  * being unused.
513  */
514 
515 #ifndef DISABLE_CVSID
516 #if defined(__GNUC__) && __GNUC__ >= 4
517 # define CPL_CVSID(string) static char cpl_cvsid[] __attribute__((used)) = string;
518 #else
519 # define CPL_CVSID(string) static char cpl_cvsid[] = string; \
520 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
521 #endif
522 #else
523 # define CPL_CVSID(string)
524 #endif
525 
526 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
527 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
528 #else
529 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
530 #endif
531 
532 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
533 #define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
534 #else
535 #define CPL_WARN_UNUSED_RESULT
536 #endif
537 
538 
539 #endif /* ndef CPL_BASE_H_INCLUDED */

Generated for GDAL by doxygen 1.8.1.