cpl_port.h

Go to the documentation of this file.
00001 /******************************************************************************
00002  * $Id: cpl_port.h 17734 2009-10-03 09:48:01Z rouault $
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 
00034 /* Remove annoying warnings Microsoft Visual C++ */
00035 #if defined(_MSC_VER)
00036 #  pragma warning(disable:4251 4275 4786)
00037 #endif
00038 
00046 /* ==================================================================== */
00047 /*      We will use macos_pre10 to indicate compilation with MacOS      */
00048 /*      versions before MacOS X.                                        */
00049 /* ==================================================================== */
00050 #ifdef macintosh
00051 #  define macos_pre10
00052 #endif
00053 
00054 /* ==================================================================== */
00055 /*      We will use WIN32 as a standard windows define.                 */
00056 /* ==================================================================== */
00057 #if defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE)
00058 #  define WIN32
00059 #endif
00060 
00061 #if defined(_WINDOWS) && !defined(WIN32) && !defined(_WIN32_WCE)
00062 #  define WIN32
00063 #endif
00064 
00065 /* ==================================================================== */
00066 /*      We will use WIN32CE as a standard Windows CE (Mobile) define.   */
00067 /* ==================================================================== */
00068 #if defined(_WIN32_WCE)
00069 #  define WIN32CE
00070 #endif
00071 
00072 /* -------------------------------------------------------------------- */
00073 /*      The following apparently allow you to use strcpy() and other    */
00074 /*      functions judged "unsafe" by microsoft in VS 8 (2005).          */
00075 /* -------------------------------------------------------------------- */
00076 #ifdef _MSC_VER
00077 #  ifndef _CRT_SECURE_NO_DEPRECATE
00078 #    define _CRT_SECURE_NO_DEPRECATE
00079 #  endif
00080 #  ifndef _CRT_NONSTDC_NO_DEPRECATE
00081 #    define _CRT_NONSTDC_NO_DEPRECATE
00082 #  endif
00083 #  ifdef MSVC_USE_VLD
00084 #    include <vld.h>
00085 #  endif
00086 #endif
00087 
00088 #include "cpl_config.h"
00089 
00090 /* ==================================================================== */
00091 /*      This will disable most WIN32 stuff in a Cygnus build which      */
00092 /*      defines unix to 1.                                              */
00093 /* ==================================================================== */
00094 
00095 #ifdef unix
00096 #  undef WIN32
00097 #  undef WIN32CE
00098 #endif
00099 
00100 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
00101 #  define _LARGEFILE64_SOURCE 1
00102 #endif
00103 
00104 /* ==================================================================== */
00105 /*      Standard include files.                                         */
00106 /* ==================================================================== */
00107 
00108 #include <stdio.h>
00109 #include <stdlib.h>
00110 #include <math.h>
00111 #include <stdarg.h>
00112 #include <string.h>
00113 #include <ctype.h>
00114 #include <limits.h>
00115 
00116 #if !defined(WIN32CE)
00117 #  include <time.h>
00118 #else
00119 #  include <wce_time.h>
00120 #  include <wce_errno.h>
00121 #endif
00122 
00123 
00124 #if defined(HAVE_ERRNO_H)
00125 #  include <errno.h>
00126 #endif 
00127 
00128 #ifdef HAVE_LOCALE_H
00129 #  include <locale.h>
00130 #endif
00131 
00132 #ifdef HAVE_DIRECT_H
00133 #  include <direct.h>
00134 #endif
00135 
00136 #ifdef _AIX
00137 #  include <strings.h>
00138 #endif
00139 
00140 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
00141 #  define DBMALLOC
00142 #  include <dbmalloc.h>
00143 #endif
00144 
00145 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
00146 #  define USE_DMALLOC
00147 #  include <dmalloc.h>
00148 #endif
00149 
00150 /* ==================================================================== */
00151 /*      Base portability stuff ... this stuff may need to be            */
00152 /*      modified for new platforms.                                     */
00153 /* ==================================================================== */
00154 
00155 /*---------------------------------------------------------------------
00156  *        types for 16 and 32 bits integers, etc...
00157  *--------------------------------------------------------------------*/
00158 #if UINT_MAX == 65535
00159 typedef long            GInt32;
00160 typedef unsigned long   GUInt32;
00161 #else
00162 typedef int             GInt32;
00163 typedef unsigned int    GUInt32;
00164 #endif
00165 
00166 typedef short           GInt16;
00167 typedef unsigned short  GUInt16;
00168 typedef unsigned char   GByte;
00169 typedef int             GBool;
00170 
00171 /* -------------------------------------------------------------------- */
00172 /*      64bit support                                                   */
00173 /* -------------------------------------------------------------------- */
00174 
00175 #if defined(WIN32) && defined(_MSC_VER)
00176 
00177 #define VSI_LARGE_API_SUPPORTED
00178 typedef __int64          GIntBig;
00179 typedef unsigned __int64 GUIntBig;
00180 
00181 #elif HAVE_LONG_LONG
00182 
00183 typedef long long        GIntBig;
00184 typedef unsigned long long GUIntBig;
00185 
00186 #else
00187 
00188 typedef long             GIntBig;
00189 typedef unsigned long    GUIntBig;
00190 
00191 #endif
00192 
00193 #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
00194   #define CPL_FRMT_GB_WITHOUT_PREFIX     "I64"
00195 #elif HAVE_LONG_LONG
00196   #define CPL_FRMT_GB_WITHOUT_PREFIX     "ll"
00197 #else
00198   #define CPL_FRMT_GB_WITHOUT_PREFIX     "l"
00199 #endif
00200 
00201 #define CPL_FRMT_GIB     "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
00202 #define CPL_FRMT_GUIB    "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
00203 
00204 /* Workaround VC6 bug */
00205 #if defined(_MSC_VER) && (_MSC_VER <= 1200)
00206 #define GUINTBIG_TO_DOUBLE(x) (double)(GIntBig)(x)
00207 #else
00208 #define GUINTBIG_TO_DOUBLE(x) (double)(x)
00209 #endif
00210 
00211 /* ==================================================================== */
00212 /*      Other standard services.                                        */
00213 /* ==================================================================== */
00214 #ifdef __cplusplus
00215 #  define CPL_C_START           extern "C" {
00216 #  define CPL_C_END             }
00217 #else
00218 #  define CPL_C_START
00219 #  define CPL_C_END
00220 #endif
00221 
00222 #ifndef CPL_DLL
00223 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
00224 #  define CPL_DLL     __declspec(dllexport)
00225 #else
00226 #  if defined(USE_GCC_VISIBILITY_FLAG)
00227 #    define CPL_DLL     __attribute__ ((visibility("default")))
00228 #  else
00229 #    define CPL_DLL
00230 #  endif
00231 #endif
00232 #endif
00233 
00234 /* Should optional (normally private) interfaces be exported? */
00235 #ifdef CPL_OPTIONAL_APIS
00236 #  define CPL_ODLL CPL_DLL
00237 #else
00238 #  define CPL_ODLL
00239 #endif
00240 
00241 #ifndef CPL_STDCALL
00242 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
00243 #  define CPL_STDCALL     __stdcall
00244 #else
00245 #  define CPL_STDCALL
00246 #endif
00247 #endif
00248 
00249 #ifdef _MSC_VER
00250 #  define FORCE_CDECL  __cdecl
00251 #else
00252 #  define FORCE_CDECL 
00253 #endif
00254 
00255 /* TODO : support for other compilers needed */
00256 #if defined(__GNUC__) || defined(_MSC_VER)
00257 #define CPL_INLINE __inline
00258 #else
00259 #define CPL_INLINE
00260 #endif
00261 
00262 #ifndef NULL
00263 #  define NULL  0
00264 #endif
00265 
00266 #ifndef FALSE
00267 #  define FALSE 0
00268 #endif
00269 
00270 #ifndef TRUE
00271 #  define TRUE  1
00272 #endif
00273 
00274 #ifndef MAX
00275 #  define MIN(a,b)      ((a<b) ? a : b)
00276 #  define MAX(a,b)      ((a>b) ? a : b)
00277 #endif
00278 
00279 #ifndef ABS
00280 #  define ABS(x)        ((x<0) ? (-1*(x)) : x)
00281 #endif
00282 
00283 /* -------------------------------------------------------------------- */
00284 /*      Macro to test equality of two floating point values.            */
00285 /*      We use fabs() function instead of ABS() macro to avoid side     */
00286 /*      effects.                                                        */
00287 /* -------------------------------------------------------------------- */
00288 #ifndef CPLIsEqual
00289 #  define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
00290 #endif
00291 
00292 #ifndef EQUAL
00293 #if defined(WIN32) || defined(WIN32CE)
00294 #  define EQUALN(a,b,n)           (strnicmp(a,b,n)==0)
00295 #  define EQUAL(a,b)              (stricmp(a,b)==0)
00296 #else
00297 #  define EQUALN(a,b,n)           (strncasecmp(a,b,n)==0)
00298 #  define EQUAL(a,b)              (strcasecmp(a,b)==0)
00299 #endif
00300 #endif
00301 
00302 #ifdef macos_pre10
00303 int strcasecmp(char * str1, char * str2);
00304 int strncasecmp(char * str1, char * str2, int len);
00305 char * strdup (char *instr);
00306 #endif
00307 
00308 #ifndef CPL_THREADLOCAL 
00309 #  define CPL_THREADLOCAL 
00310 #endif
00311 
00312 /* -------------------------------------------------------------------- */
00313 /*      Handle isnan() and isinf().  Note that isinf() and isnan()      */
00314 /*      are supposed to be macros according to C99, defined in math.h   */
00315 /*      Some systems (ie. Tru64) don't have isinf() at all, so if       */
00316 /*      the macro is not defined we just assume nothing is infinite.    */
00317 /*      This may mean we have no real CPLIsInf() on systems with isinf()*/
00318 /*      function but no corresponding macro, but I can live with        */
00319 /*      that since it isn't that important a test.                      */
00320 /* -------------------------------------------------------------------- */
00321 #ifdef _MSC_VER
00322 #  include <float.h>
00323 #  define CPLIsNan(x) _isnan(x)
00324 #  define CPLIsInf(x) (!_isnan(x) && !_finite(x))
00325 #  define CPLIsFinite(x) _finite(x)
00326 #else
00327 #  define CPLIsNan(x) isnan(x)
00328 #  ifdef isinf 
00329 #    define CPLIsInf(x) isinf(x)
00330 #    define CPLIsFinite(x) (!isnan(x) && !isinf(x))
00331 #  else
00332 #    define CPLIsInf(x)    FALSE
00333 #    define CPLIsFinite(x) (!isnan(x))
00334 #  endif
00335 #endif
00336 
00337 /*---------------------------------------------------------------------
00338  *                         CPL_LSB and CPL_MSB
00339  * Only one of these 2 macros should be defined and specifies the byte 
00340  * ordering for the current platform.  
00341  * This should be defined in the Makefile, but if it is not then
00342  * the default is CPL_LSB (Intel ordering, LSB first).
00343  *--------------------------------------------------------------------*/
00344 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
00345 #  define CPL_MSB
00346 #endif
00347 
00348 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
00349 #define CPL_LSB
00350 #endif
00351 
00352 #if defined(CPL_LSB)
00353 #  define CPL_IS_LSB 1
00354 #else
00355 #  define CPL_IS_LSB 0
00356 #endif
00357 
00358 /*---------------------------------------------------------------------
00359  *        Little endian <==> big endian byte swap macros.
00360  *--------------------------------------------------------------------*/
00361 
00362 #define CPL_SWAP16(x) \
00363         ((GUInt16)( \
00364             (((GUInt16)(x) & 0x00ffU) << 8) | \
00365             (((GUInt16)(x) & 0xff00U) >> 8) ))
00366 
00367 #define CPL_SWAP16PTR(x) \
00368 {                                                                 \
00369     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00370                                                                   \
00371     byTemp = _pabyDataT[0];                                       \
00372     _pabyDataT[0] = _pabyDataT[1];                                \
00373     _pabyDataT[1] = byTemp;                                       \
00374 }                                                                    
00375                                                             
00376 #define CPL_SWAP32(x) \
00377         ((GUInt32)( \
00378             (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
00379             (((GUInt32)(x) & (GUInt32)0x0000ff00UL) <<  8) | \
00380             (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >>  8) | \
00381             (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
00382 
00383 #define CPL_SWAP32PTR(x) \
00384 {                                                                 \
00385     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00386                                                                   \
00387     byTemp = _pabyDataT[0];                                       \
00388     _pabyDataT[0] = _pabyDataT[3];                                \
00389     _pabyDataT[3] = byTemp;                                       \
00390     byTemp = _pabyDataT[1];                                       \
00391     _pabyDataT[1] = _pabyDataT[2];                                \
00392     _pabyDataT[2] = byTemp;                                       \
00393 }                                                                    
00394                                                             
00395 #define CPL_SWAP64PTR(x) \
00396 {                                                                 \
00397     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00398                                                                   \
00399     byTemp = _pabyDataT[0];                                       \
00400     _pabyDataT[0] = _pabyDataT[7];                                \
00401     _pabyDataT[7] = byTemp;                                       \
00402     byTemp = _pabyDataT[1];                                       \
00403     _pabyDataT[1] = _pabyDataT[6];                                \
00404     _pabyDataT[6] = byTemp;                                       \
00405     byTemp = _pabyDataT[2];                                       \
00406     _pabyDataT[2] = _pabyDataT[5];                                \
00407     _pabyDataT[5] = byTemp;                                       \
00408     byTemp = _pabyDataT[3];                                       \
00409     _pabyDataT[3] = _pabyDataT[4];                                \
00410     _pabyDataT[4] = byTemp;                                       \
00411 }                                                                    
00412                                                             
00413 
00414 /* Until we have a safe 64 bits integer data type defined, we'll replace
00415  * this version of the CPL_SWAP64() macro with a less efficient one.
00416  */
00417 /*
00418 #define CPL_SWAP64(x) \
00419         ((uint64)( \
00420             (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
00421             (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
00422             (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
00423             (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \
00424             (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \
00425             (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
00426             (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
00427             (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
00428 */
00429 
00430 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
00431 
00432 #ifdef CPL_MSB
00433 #  define CPL_MSBWORD16(x)      (x)
00434 #  define CPL_LSBWORD16(x)      CPL_SWAP16(x)
00435 #  define CPL_MSBWORD32(x)      (x)
00436 #  define CPL_LSBWORD32(x)      CPL_SWAP32(x)
00437 #  define CPL_MSBPTR16(x)       
00438 #  define CPL_LSBPTR16(x)       CPL_SWAP16PTR(x)
00439 #  define CPL_MSBPTR32(x)       
00440 #  define CPL_LSBPTR32(x)       CPL_SWAP32PTR(x)
00441 #  define CPL_MSBPTR64(x)       
00442 #  define CPL_LSBPTR64(x)       CPL_SWAP64PTR(x)
00443 #else
00444 #  define CPL_LSBWORD16(x)      (x)
00445 #  define CPL_MSBWORD16(x)      CPL_SWAP16(x)
00446 #  define CPL_LSBWORD32(x)      (x)
00447 #  define CPL_MSBWORD32(x)      CPL_SWAP32(x)
00448 #  define CPL_LSBPTR16(x)       
00449 #  define CPL_MSBPTR16(x)       CPL_SWAP16PTR(x)
00450 #  define CPL_LSBPTR32(x)       
00451 #  define CPL_MSBPTR32(x)       CPL_SWAP32PTR(x)
00452 #  define CPL_LSBPTR64(x)       
00453 #  define CPL_MSBPTR64(x)       CPL_SWAP64PTR(x)
00454 #endif
00455 
00457 #define CPL_LSBINT16PTR(x)    ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8))
00458 
00460 #define CPL_LSBINT32PTR(x)    ((*(GByte*)(x)) | ((*(GByte*)((x)+1)) << 8) | \
00461                               ((*(GByte*)((x)+2)) << 16) | ((*(GByte*)((x)+3)) << 24))
00462 
00463 
00464 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
00465 #ifndef UNREFERENCED_PARAM 
00466 #  ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
00467 #    define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
00468 #  else
00469 #    define UNREFERENCED_PARAM(param) ((void)param)
00470 #  endif /* UNREFERENCED_PARAMETER */
00471 #endif /* UNREFERENCED_PARAM */
00472 
00473 /***********************************************************************
00474  * Define CPL_CVSID() macro.  It can be disabled during a build by
00475  * defining DISABLE_CPLID in the compiler options.
00476  *
00477  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
00478  * being unused.
00479  */
00480 
00481 #ifndef DISABLE_CVSID
00482 #if defined(__GNUC__) && __GNUC__ >= 4
00483 #  define CPL_CVSID(string)     static char cpl_cvsid[] __attribute__((used)) = string;
00484 #else
00485 #  define CPL_CVSID(string)     static char cpl_cvsid[] = string; \
00486 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
00487 #endif
00488 #else
00489 #  define CPL_CVSID(string)
00490 #endif
00491 
00492 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
00493 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )  __attribute__((__format__ (__printf__, format_idx, arg_idx)))
00494 #else
00495 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
00496 #endif
00497 
00498 #endif /* ndef CPL_BASE_H_INCLUDED */

Generated for GDAL by doxygen 1.5.7.1.