Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

cpl_port.h

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

Generated for GDAL by doxygen 1.4.4.