cpl_port.h

Go to the documentation of this file.
00001 /******************************************************************************
00002  * $Id: cpl_port.h 24288 2012-04-22 00:37:53Z mloskot $
00003  *
00004  * Project:  CPL - Common Portability Library
00005  * Author:   Frank Warmerdam, warmerdam@pobox.com
00006  * Purpose:  Include file providing low level portability services for CPL.  
00007  *           This should be the first include file for any CPL based code.  
00008  *
00009  ******************************************************************************
00010  * Copyright (c) 1998, 2005, Frank Warmerdam <warmerdam@pobox.com>
00011  *
00012  * Permission is hereby granted, free of charge, to any person obtaining a
00013  * copy of this software and associated documentation files (the "Software"),
00014  * to deal in the Software without restriction, including without limitation
00015  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00016  * and/or sell copies of the Software, and to permit persons to whom the
00017  * Software is furnished to do so, subject to the following conditions:
00018  *
00019  * The above copyright notice and this permission notice shall be included
00020  * in all copies or substantial portions of the Software.
00021  *
00022  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00023  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00025  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00027  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00028  * DEALINGS IN THE SOFTWARE.
00029  ****************************************************************************/
00030 
00031 #ifndef CPL_BASE_H_INCLUDED
00032 #define CPL_BASE_H_INCLUDED
00033 
00041 /* ==================================================================== */
00042 /*      We will use macos_pre10 to indicate compilation with MacOS      */
00043 /*      versions before MacOS X.                                        */
00044 /* ==================================================================== */
00045 #ifdef macintosh
00046 #  define macos_pre10
00047 #endif
00048 
00049 /* ==================================================================== */
00050 /*      We will use WIN32 as a standard windows define.                 */
00051 /* ==================================================================== */
00052 #if defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE)
00053 #  define WIN32
00054 #endif
00055 
00056 #if defined(_WINDOWS) && !defined(WIN32) && !defined(_WIN32_WCE)
00057 #  define WIN32
00058 #endif
00059 
00060 /* ==================================================================== */
00061 /*      We will use WIN32CE as a standard Windows CE (Mobile) define.   */
00062 /* ==================================================================== */
00063 #if defined(_WIN32_WCE)
00064 #  define WIN32CE
00065 #endif
00066 
00067 /* -------------------------------------------------------------------- */
00068 /*      The following apparently allow you to use strcpy() and other    */
00069 /*      functions judged "unsafe" by microsoft in VS 8 (2005).          */
00070 /* -------------------------------------------------------------------- */
00071 #ifdef _MSC_VER
00072 #  ifndef _CRT_SECURE_NO_DEPRECATE
00073 #    define _CRT_SECURE_NO_DEPRECATE
00074 #  endif
00075 #  ifndef _CRT_NONSTDC_NO_DEPRECATE
00076 #    define _CRT_NONSTDC_NO_DEPRECATE
00077 #  endif
00078 #  ifdef MSVC_USE_VLD
00079 #    include <wchar.h>
00080 #    include <vld.h>
00081 #  endif
00082 #endif
00083 
00084 #include "cpl_config.h"
00085 
00086 /* ==================================================================== */
00087 /*      This will disable most WIN32 stuff in a Cygnus build which      */
00088 /*      defines unix to 1.                                              */
00089 /* ==================================================================== */
00090 
00091 #ifdef unix
00092 #  undef WIN32
00093 #  undef WIN32CE
00094 #endif
00095 
00096 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
00097 #  define _LARGEFILE64_SOURCE 1
00098 #endif
00099 
00100 /* ==================================================================== */
00101 /*      If iconv() is available use extended recoding module.           */
00102 /*      Stub implementation is always compiled in, because it works     */
00103 /*      faster than iconv() for encodings it supports.                  */
00104 /* ==================================================================== */
00105 
00106 #if defined(HAVE_ICONV)
00107 #  define CPL_RECODE_ICONV
00108 #endif
00109 
00110 #define CPL_RECODE_STUB
00111 
00112 /* ==================================================================== */
00113 /*      MinGW stuff                                                     */
00114 /* ==================================================================== */
00115 
00116 /* We need __MSVCRT_VERSION__ >= 0x0601 to have "struct __stat64" */
00117 /* Latest versions of mingw32 define it, but with older ones, */
00118 /* we need to define it manually */
00119 #if defined(__MINGW32__)
00120 #ifndef __MSVCRT_VERSION__
00121 #define __MSVCRT_VERSION__ 0x0601
00122 #endif
00123 #endif
00124 
00125 /* ==================================================================== */
00126 /*      Standard include files.                                         */
00127 /* ==================================================================== */
00128 
00129 #include <stdio.h>
00130 #include <stdlib.h>
00131 #include <math.h>
00132 #include <stdarg.h>
00133 #include <string.h>
00134 #include <ctype.h>
00135 #include <limits.h>
00136 
00137 #if !defined(WIN32CE)
00138 #  include <time.h>
00139 #else
00140 #  include <wce_time.h>
00141 #  include <wce_errno.h>
00142 #endif
00143 
00144 
00145 #if defined(HAVE_ERRNO_H)
00146 #  include <errno.h>
00147 #endif 
00148 
00149 #ifdef HAVE_LOCALE_H
00150 #  include <locale.h>
00151 #endif
00152 
00153 #ifdef HAVE_DIRECT_H
00154 #  include <direct.h>
00155 #endif
00156 
00157 #ifdef _AIX
00158 #  include <strings.h>
00159 #endif
00160 
00161 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
00162 #  define DBMALLOC
00163 #  include <dbmalloc.h>
00164 #endif
00165 
00166 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
00167 #  define USE_DMALLOC
00168 #  include <dmalloc.h>
00169 #endif
00170 
00171 /* ==================================================================== */
00172 /*      Base portability stuff ... this stuff may need to be            */
00173 /*      modified for new platforms.                                     */
00174 /* ==================================================================== */
00175 
00176 /*---------------------------------------------------------------------
00177  *        types for 16 and 32 bits integers, etc...
00178  *--------------------------------------------------------------------*/
00179 #if UINT_MAX == 65535
00180 typedef long            GInt32;
00181 typedef unsigned long   GUInt32;
00182 #else
00183 typedef int             GInt32;
00184 typedef unsigned int    GUInt32;
00185 #endif
00186 
00187 typedef short           GInt16;
00188 typedef unsigned short  GUInt16;
00189 typedef unsigned char   GByte;
00190 /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
00191 /* in include/poppler/goo/gtypes.h */
00192 #ifndef CPL_GBOOL_DEFINED
00193 #define CPL_GBOOL_DEFINED
00194 typedef int             GBool;
00195 #endif
00196 
00197 /* -------------------------------------------------------------------- */
00198 /*      64bit support                                                   */
00199 /* -------------------------------------------------------------------- */
00200 
00201 #if defined(WIN32) && defined(_MSC_VER)
00202 
00203 #define VSI_LARGE_API_SUPPORTED
00204 typedef __int64          GIntBig;
00205 typedef unsigned __int64 GUIntBig;
00206 
00207 #elif HAVE_LONG_LONG
00208 
00209 typedef long long        GIntBig;
00210 typedef unsigned long long GUIntBig;
00211 
00212 #else
00213 
00214 typedef long             GIntBig;
00215 typedef unsigned long    GUIntBig;
00216 
00217 #endif
00218 
00219 #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
00220   #define CPL_FRMT_GB_WITHOUT_PREFIX     "I64"
00221 #elif HAVE_LONG_LONG
00222   #define CPL_FRMT_GB_WITHOUT_PREFIX     "ll"
00223 #else
00224   #define CPL_FRMT_GB_WITHOUT_PREFIX     "l"
00225 #endif
00226 
00227 #define CPL_FRMT_GIB     "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
00228 #define CPL_FRMT_GUIB    "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
00229 
00230 /* Workaround VC6 bug */
00231 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
00232 #define GUINTBIG_TO_DOUBLE(x) (double)(GIntBig)(x)
00233 #else
00234 #define GUINTBIG_TO_DOUBLE(x) (double)(x)
00235 #endif
00236 
00237 /* ==================================================================== */
00238 /*      Other standard services.                                        */
00239 /* ==================================================================== */
00240 #ifdef __cplusplus
00241 #  define CPL_C_START           extern "C" {
00242 #  define CPL_C_END             }
00243 #else
00244 #  define CPL_C_START
00245 #  define CPL_C_END
00246 #endif
00247 
00248 #ifndef CPL_DLL
00249 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
00250 #  define CPL_DLL     __declspec(dllexport)
00251 #else
00252 #  if defined(USE_GCC_VISIBILITY_FLAG)
00253 #    define CPL_DLL     __attribute__ ((visibility("default")))
00254 #  else
00255 #    define CPL_DLL
00256 #  endif
00257 #endif
00258 #endif
00259 
00260 /* Should optional (normally private) interfaces be exported? */
00261 #ifdef CPL_OPTIONAL_APIS
00262 #  define CPL_ODLL CPL_DLL
00263 #else
00264 #  define CPL_ODLL
00265 #endif
00266 
00267 #ifndef CPL_STDCALL
00268 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
00269 #  define CPL_STDCALL     __stdcall
00270 #else
00271 #  define CPL_STDCALL
00272 #endif
00273 #endif
00274 
00275 #ifdef _MSC_VER
00276 #  define FORCE_CDECL  __cdecl
00277 #else
00278 #  define FORCE_CDECL 
00279 #endif
00280 
00281 /* TODO : support for other compilers needed */
00282 #if defined(__GNUC__) || defined(_MSC_VER)
00283 #define HAS_CPL_INLINE  1
00284 #define CPL_INLINE __inline
00285 #elif defined(__SUNPRO_CC)
00286 #define HAS_CPL_INLINE  1
00287 #define CPL_INLINE inline
00288 #else
00289 #define CPL_INLINE
00290 #endif
00291 
00292 #ifndef NULL
00293 #  define NULL  0
00294 #endif
00295 
00296 #ifndef FALSE
00297 #  define FALSE 0
00298 #endif
00299 
00300 #ifndef TRUE
00301 #  define TRUE  1
00302 #endif
00303 
00304 #ifndef MAX
00305 #  define MIN(a,b)      ((a<b) ? a : b)
00306 #  define MAX(a,b)      ((a>b) ? a : b)
00307 #endif
00308 
00309 #ifndef ABS
00310 #  define ABS(x)        ((x<0) ? (-1*(x)) : x)
00311 #endif
00312 
00313 #ifndef M_PI
00314 # define M_PI           3.14159265358979323846  /* pi */
00315 #endif
00316 
00317 /* -------------------------------------------------------------------- */
00318 /*      Macro to test equality of two floating point values.            */
00319 /*      We use fabs() function instead of ABS() macro to avoid side     */
00320 /*      effects.                                                        */
00321 /* -------------------------------------------------------------------- */
00322 #ifndef CPLIsEqual
00323 #  define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
00324 #endif
00325 
00326 /* -------------------------------------------------------------------- */
00327 /*      Provide macros for case insensitive string comparisons.         */
00328 /* -------------------------------------------------------------------- */
00329 #ifndef EQUAL
00330 #  if defined(WIN32) || defined(WIN32CE)
00331 #    define STRCASECMP(a,b)         (stricmp(a,b))
00332 #    define STRNCASECMP(a,b,n)      (strnicmp(a,b,n))
00333 #  else
00334 #    define STRCASECMP(a,b)         (strcasecmp(a,b))
00335 #    define STRNCASECMP(a,b,n)      (strncasecmp(a,b,n))
00336 #  endif
00337 #  define EQUALN(a,b,n)           (STRNCASECMP(a,b,n)==0)
00338 #  define EQUAL(a,b)              (STRCASECMP(a,b)==0)
00339 #endif
00340 
00341 #ifdef macos_pre10
00342 int strcasecmp(char * str1, char * str2);
00343 int strncasecmp(char * str1, char * str2, int len);
00344 char * strdup (char *instr);
00345 #endif
00346 
00347 #ifndef CPL_THREADLOCAL
00348 #  define CPL_THREADLOCAL
00349 #endif
00350 
00351 /* -------------------------------------------------------------------- */
00352 /*      Handle isnan() and isinf().  Note that isinf() and isnan()      */
00353 /*      are supposed to be macros according to C99, defined in math.h   */
00354 /*      Some systems (ie. Tru64) don't have isinf() at all, so if       */
00355 /*      the macro is not defined we just assume nothing is infinite.    */
00356 /*      This may mean we have no real CPLIsInf() on systems with isinf()*/
00357 /*      function but no corresponding macro, but I can live with        */
00358 /*      that since it isn't that important a test.                      */
00359 /* -------------------------------------------------------------------- */
00360 #ifdef _MSC_VER
00361 #  include <float.h>
00362 #  define CPLIsNan(x) _isnan(x)
00363 #  define CPLIsInf(x) (!_isnan(x) && !_finite(x))
00364 #  define CPLIsFinite(x) _finite(x)
00365 #else
00366 #  define CPLIsNan(x) isnan(x)
00367 #  ifdef isinf 
00368 #    define CPLIsInf(x) isinf(x)
00369 #    define CPLIsFinite(x) (!isnan(x) && !isinf(x))
00370 #  else
00371 #    define CPLIsInf(x)    FALSE
00372 #    define CPLIsFinite(x) (!isnan(x))
00373 #  endif
00374 #endif
00375 
00376 /*---------------------------------------------------------------------
00377  *                         CPL_LSB and CPL_MSB
00378  * Only one of these 2 macros should be defined and specifies the byte 
00379  * ordering for the current platform.  
00380  * This should be defined in the Makefile, but if it is not then
00381  * the default is CPL_LSB (Intel ordering, LSB first).
00382  *--------------------------------------------------------------------*/
00383 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
00384 #  define CPL_MSB
00385 #endif
00386 
00387 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
00388 #define CPL_LSB
00389 #endif
00390 
00391 #if defined(CPL_LSB)
00392 #  define CPL_IS_LSB 1
00393 #else
00394 #  define CPL_IS_LSB 0
00395 #endif
00396 
00397 /*---------------------------------------------------------------------
00398  *        Little endian <==> big endian byte swap macros.
00399  *--------------------------------------------------------------------*/
00400 
00401 #define CPL_SWAP16(x) \
00402         ((GUInt16)( \
00403             (((GUInt16)(x) & 0x00ffU) << 8) | \
00404             (((GUInt16)(x) & 0xff00U) >> 8) ))
00405 
00406 #define CPL_SWAP16PTR(x) \
00407 {                                                                 \
00408     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00409                                                                   \
00410     byTemp = _pabyDataT[0];                                       \
00411     _pabyDataT[0] = _pabyDataT[1];                                \
00412     _pabyDataT[1] = byTemp;                                       \
00413 }                                                                    
00414                                                             
00415 #define CPL_SWAP32(x) \
00416         ((GUInt32)( \
00417             (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
00418             (((GUInt32)(x) & (GUInt32)0x0000ff00UL) <<  8) | \
00419             (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >>  8) | \
00420             (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
00421 
00422 #define CPL_SWAP32PTR(x) \
00423 {                                                                 \
00424     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00425                                                                   \
00426     byTemp = _pabyDataT[0];                                       \
00427     _pabyDataT[0] = _pabyDataT[3];                                \
00428     _pabyDataT[3] = byTemp;                                       \
00429     byTemp = _pabyDataT[1];                                       \
00430     _pabyDataT[1] = _pabyDataT[2];                                \
00431     _pabyDataT[2] = byTemp;                                       \
00432 }                                                                    
00433                                                             
00434 #define CPL_SWAP64PTR(x) \
00435 {                                                                 \
00436     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00437                                                                   \
00438     byTemp = _pabyDataT[0];                                       \
00439     _pabyDataT[0] = _pabyDataT[7];                                \
00440     _pabyDataT[7] = byTemp;                                       \
00441     byTemp = _pabyDataT[1];                                       \
00442     _pabyDataT[1] = _pabyDataT[6];                                \
00443     _pabyDataT[6] = byTemp;                                       \
00444     byTemp = _pabyDataT[2];                                       \
00445     _pabyDataT[2] = _pabyDataT[5];                                \
00446     _pabyDataT[5] = byTemp;                                       \
00447     byTemp = _pabyDataT[3];                                       \
00448     _pabyDataT[3] = _pabyDataT[4];                                \
00449     _pabyDataT[4] = byTemp;                                       \
00450 }                                                                    
00451                                                             
00452 
00453 /* Until we have a safe 64 bits integer data type defined, we'll replace
00454  * this version of the CPL_SWAP64() macro with a less efficient one.
00455  */
00456 /*
00457 #define CPL_SWAP64(x) \
00458         ((uint64)( \
00459             (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
00460             (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
00461             (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
00462             (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \
00463             (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \
00464             (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
00465             (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
00466             (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
00467 */
00468 
00469 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
00470 
00471 #ifdef CPL_MSB
00472 #  define CPL_MSBWORD16(x)      (x)
00473 #  define CPL_LSBWORD16(x)      CPL_SWAP16(x)
00474 #  define CPL_MSBWORD32(x)      (x)
00475 #  define CPL_LSBWORD32(x)      CPL_SWAP32(x)
00476 #  define CPL_MSBPTR16(x)       
00477 #  define CPL_LSBPTR16(x)       CPL_SWAP16PTR(x)
00478 #  define CPL_MSBPTR32(x)       
00479 #  define CPL_LSBPTR32(x)       CPL_SWAP32PTR(x)
00480 #  define CPL_MSBPTR64(x)       
00481 #  define CPL_LSBPTR64(x)       CPL_SWAP64PTR(x)
00482 #else
00483 #  define CPL_LSBWORD16(x)      (x)
00484 #  define CPL_MSBWORD16(x)      CPL_SWAP16(x)
00485 #  define CPL_LSBWORD32(x)      (x)
00486 #  define CPL_MSBWORD32(x)      CPL_SWAP32(x)
00487 #  define CPL_LSBPTR16(x)       
00488 #  define CPL_MSBPTR16(x)       CPL_SWAP16PTR(x)
00489 #  define CPL_LSBPTR32(x)       
00490 #  define CPL_MSBPTR32(x)       CPL_SWAP32PTR(x)
00491 #  define CPL_LSBPTR64(x)       
00492 #  define CPL_MSBPTR64(x)       CPL_SWAP64PTR(x)
00493 #endif
00494 
00496 #define CPL_LSBINT16PTR(x)    ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8))
00497 
00499 #define CPL_LSBINT32PTR(x)    ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8) | \
00500                               ((*(GByte*)((x)+2)) << 16) | ((*(GByte*)((x)+3)) << 24))
00501 
00502 
00503 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
00504 #ifndef UNREFERENCED_PARAM 
00505 #  ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
00506 #    define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
00507 #  else
00508 #    define UNREFERENCED_PARAM(param) ((void)param)
00509 #  endif /* UNREFERENCED_PARAMETER */
00510 #endif /* UNREFERENCED_PARAM */
00511 
00512 /***********************************************************************
00513  * Define CPL_CVSID() macro.  It can be disabled during a build by
00514  * defining DISABLE_CPLID in the compiler options.
00515  *
00516  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
00517  * being unused.
00518  */
00519 
00520 #ifndef DISABLE_CVSID
00521 #if defined(__GNUC__) && __GNUC__ >= 4
00522 #  define CPL_CVSID(string)     static char cpl_cvsid[] __attribute__((used)) = string;
00523 #else
00524 #  define CPL_CVSID(string)     static char cpl_cvsid[] = string; \
00525 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
00526 #endif
00527 #else
00528 #  define CPL_CVSID(string)
00529 #endif
00530 
00531 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
00532 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )  __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00533 #else
00534 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
00535 #endif
00536 
00537 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
00538 #define CPL_WARN_UNUSED_RESULT                        __attribute__((warn_unused_result))
00539 #else
00540 #define CPL_WARN_UNUSED_RESULT
00541 #endif
00542 
00543 
00544 #endif /* ndef CPL_BASE_H_INCLUDED */

Generated for GDAL by doxygen 1.7.1.