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.36 2004/01/06 21:42:32 warmerda Exp $
00003  *
00004  * Project:  CPL - Common Portability Library
00005  * Author:   Frank Warmerdam, warmerdam@pobox.com
00006  * Purpose:  
00007  * Include file providing low level portability services for CPL.  This
00008  * should be the first include file for any CPL based code.  It provides the
00009  * following:
00010  *
00011  * o Includes some standard system include files, such as stdio, and stdlib.
00012  *
00013  * o Defines CPL_C_START, CPL_C_END macros.
00014  *
00015  * o Ensures that some other standard macros like NULL are defined.
00016  *
00017  * o Defines some portability stuff like CPL_MSB, or CPL_LSB.
00018  *
00019  * o Ensures that core types such as GBool, GInt32, GInt16, GUInt32, 
00020  *   GUInt16, and GByte are defined.
00021  *
00022  ******************************************************************************
00023  * Copyright (c) 1998, Frank Warmerdam
00024  *
00025  * Permission is hereby granted, free of charge, to any person obtaining a
00026  * copy of this software and associated documentation files (the "Software"),
00027  * to deal in the Software without restriction, including without limitation
00028  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00029  * and/or sell copies of the Software, and to permit persons to whom the
00030  * Software is furnished to do so, subject to the following conditions:
00031  *
00032  * The above copyright notice and this permission notice shall be included
00033  * in all copies or substantial portions of the Software.
00034  *
00035  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00036  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00037  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00038  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00039  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00040  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00041  * DEALINGS IN THE SOFTWARE.
00042  ******************************************************************************
00043  *
00044  * $Log: cpl_port.h,v $
00045  * Revision 1.36  2004/01/06 21:42:32  warmerda
00046  * "Fix" for bug 455 related to CPL_IS_LSB macro.
00047  *
00048  * Revision 1.35  2003/12/11 03:16:02  warmerda
00049  * Added CPL_IS_LSB macro with value 0 (MSB) or 1 (LSB).
00050  *
00051  * Revision 1.34  2003/09/08 11:11:05  dron
00052  * Include time.h and locale.h.
00053  *
00054  * Revision 1.33  2003/05/12 14:52:56  warmerda
00055  * Use _MSC_VER to test for Microsoft Visual C++ compiler.
00056  *
00057  * Revision 1.32  2002/10/24 20:24:40  warmerda
00058  * avoid using variable names likely to conflict in macros
00059  *
00060  * Revision 1.31  2002/07/15 13:31:46  warmerda
00061  * CPL_SWAPDOUBLE had alignment problem, use CPL_SWAP64PTR
00062  *
00063  * Revision 1.30  2002/04/18 18:55:06  dron
00064  * added <ctype.h> at the list of standard include files
00065  *
00066  * Revision 1.29  2002/01/17 01:40:27  warmerda
00067  * added _LARGEFILE64_SOURCE support
00068  *
00069  * Revision 1.28  2001/08/30 21:20:49  warmerda
00070  * expand tabs
00071  *
00072  * Revision 1.27  2001/07/18 04:00:49  warmerda
00073  * added CPL_CVSID
00074  *
00075  * Revision 1.26  2001/06/21 21:17:26  warmerda
00076  * added irix 64bit file api support
00077  *
00078  * Revision 1.25  2001/04/30 18:18:38  warmerda
00079  * added macos support, standard header
00080  *
00081  * Revision 1.24  2001/01/19 21:16:41  warmerda
00082  * expanded tabs
00083  *
00084  * Revision 1.23  2001/01/13 04:06:39  warmerda
00085  * added strings.h on AIX as per patch from Dale.
00086  *
00087  * Revision 1.22  2001/01/03 16:18:07  warmerda
00088  * added GUIntBig
00089  *
00090  * Revision 1.21  2000/10/20 04:20:33  warmerda
00091  * added SWAP16PTR macros
00092  *
00093  * Revision 1.20  2000/10/13 17:32:42  warmerda
00094  * check for unix instead of IGNORE_WIN32
00095  *
00096  * Revision 1.19  2000/09/25 19:58:43  warmerda
00097  * ensure win32 doesn't get defined in Cygnus builds
00098  *
00099  * Revision 1.18  2000/07/20 13:15:03  warmerda
00100  * don't redeclare CPL_DLL
00101  */
00102 
00103 #ifndef CPL_BASE_H_INCLUDED
00104 #define CPL_BASE_H_INCLUDED
00105 
00113 /* ==================================================================== */
00114 /*      We will use macos_pre10 to indicate compilation with MacOS      */
00115 /*      versions before MacOS X.                                        */
00116 /* ==================================================================== */
00117 #ifdef macintosh
00118 #  define macos_pre10
00119 #endif
00120 
00121 /* ==================================================================== */
00122 /*      We will use WIN32 as a standard windows define.                 */
00123 /* ==================================================================== */
00124 #if defined(_WIN32) && !defined(WIN32)
00125 #  define WIN32
00126 #endif
00127 
00128 #if defined(_WINDOWS) && !defined(WIN32)
00129 #  define WIN32
00130 #endif
00131 
00132 #include "cpl_config.h"
00133 
00134 /* ==================================================================== */
00135 /*      This will disable most WIN32 stuff in a Cygnus build which      */
00136 /*      defines unix to 1.                                              */
00137 /* ==================================================================== */
00138 
00139 #ifdef unix
00140 #  undef WIN32
00141 #endif
00142 
00143 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
00144 #  define _LARGEFILE64_SOURCE 1
00145 #endif
00146 
00147 /* ==================================================================== */
00148 /*      Standard include files.                                         */
00149 /* ==================================================================== */
00150 
00151 #include <stdio.h>
00152 #include <stdlib.h>
00153 #include <math.h>
00154 #include <stdarg.h>
00155 #include <string.h>
00156 #include <ctype.h>
00157 #include <errno.h>
00158 #include <time.h>
00159 
00160 #ifdef HAVE_LOCALE_H
00161 #  include <locale.h>
00162 #endif
00163 
00164 #ifdef _AIX
00165 #  include <strings.h>
00166 #endif
00167 
00168 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
00169 #  define DBMALLOC
00170 #  include <dbmalloc.h>
00171 #endif
00172 
00173 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
00174 #  define USE_DMALLOC
00175 #  include <dmalloc.h>
00176 #endif
00177 
00178 /* ==================================================================== */
00179 /*      Base portability stuff ... this stuff may need to be            */
00180 /*      modified for new platforms.                                     */
00181 /* ==================================================================== */
00182 
00183 /*---------------------------------------------------------------------
00184  *        types for 16 and 32 bits integers, etc...
00185  *--------------------------------------------------------------------*/
00186 #if UINT_MAX == 65535
00187 typedef long            GInt32;
00188 typedef unsigned long   GUInt32;
00189 #else
00190 typedef int             GInt32;
00191 typedef unsigned int    GUInt32;
00192 #endif
00193 
00194 typedef short           GInt16;
00195 typedef unsigned short  GUInt16;
00196 typedef unsigned char   GByte;
00197 typedef int             GBool;
00198 
00199 /* -------------------------------------------------------------------- */
00200 /*      64bit support                                                   */
00201 /* -------------------------------------------------------------------- */
00202 
00203 #if defined(WIN32) && defined(_MSC_VER)
00204 
00205 #define VSI_LARGE_API_SUPPORTED
00206 typedef __int64          GIntBig;
00207 typedef unsigned __int64 GUIntBig;
00208 
00209 #elif HAVE_LONG_LONG
00210 
00211 typedef long long        GIntBig;
00212 typedef unsigned long long GUIntBig;
00213 
00214 #else
00215 
00216 typedef long             GIntBig;
00217 typedef unsigned long    GUIntBig;
00218 
00219 #endif
00220 
00221 /* ==================================================================== */
00222 /*      Other standard services.                                        */
00223 /* ==================================================================== */
00224 #ifdef __cplusplus
00225 #  define CPL_C_START           extern "C" {
00226 #  define CPL_C_END             }
00227 #else
00228 #  define CPL_C_START
00229 #  define CPL_C_END
00230 #endif
00231 
00232 #ifndef CPL_DLL
00233 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
00234 #  define CPL_DLL     __declspec(dllexport)
00235 #else
00236 #  define CPL_DLL
00237 #endif
00238 #endif
00239 
00240 
00241 #ifndef NULL
00242 #  define NULL  0
00243 #endif
00244 
00245 #ifndef FALSE
00246 #  define FALSE 0
00247 #endif
00248 
00249 #ifndef TRUE
00250 #  define TRUE  1
00251 #endif
00252 
00253 #ifndef MAX
00254 #  define MIN(a,b)      ((a<b) ? a : b)
00255 #  define MAX(a,b)      ((a>b) ? a : b)
00256 #endif
00257 
00258 #ifndef ABS
00259 #  define ABS(x)        ((x<0) ? (-1*(x)) : x)
00260 #endif
00261 
00262 #ifndef EQUAL
00263 #ifdef WIN32
00264 #  define EQUALN(a,b,n)           (strnicmp(a,b,n)==0)
00265 #  define EQUAL(a,b)              (stricmp(a,b)==0)
00266 #else
00267 #  define EQUALN(a,b,n)           (strncasecmp(a,b,n)==0)
00268 #  define EQUAL(a,b)              (strcasecmp(a,b)==0)
00269 #endif
00270 #endif
00271 
00272 #ifdef macos_pre10
00273 int strcasecmp(char * str1, char * str2);
00274 int strncasecmp(char * str1, char * str2, int len);
00275 char * strdup (char *instr);
00276 #endif
00277 
00278 /*---------------------------------------------------------------------
00279  *                         CPL_LSB and CPL_MSB
00280  * Only one of these 2 macros should be defined and specifies the byte 
00281  * ordering for the current platform.  
00282  * This should be defined in the Makefile, but if it is not then
00283  * the default is CPL_LSB (Intel ordering, LSB first).
00284  *--------------------------------------------------------------------*/
00285 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
00286 #  define CPL_MSB
00287 #endif
00288 
00289 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
00290 #define CPL_LSB
00291 #endif
00292 
00293 #if defined(CPL_LSB)
00294 #  define CPL_IS_LSB 1
00295 #else
00296 #  define CPL_IS_LSB 0
00297 #endif
00298 
00299 /*---------------------------------------------------------------------
00300  *        Little endian <==> big endian byte swap macros.
00301  *--------------------------------------------------------------------*/
00302 
00303 #define CPL_SWAP16(x) \
00304         ((GUInt16)( \
00305             (((GUInt16)(x) & 0x00ffU) << 8) | \
00306             (((GUInt16)(x) & 0xff00U) >> 8) ))
00307 
00308 #define CPL_SWAP16PTR(x) \
00309 {                                                                 \
00310     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00311                                                                   \
00312     byTemp = _pabyDataT[0];                                       \
00313     _pabyDataT[0] = _pabyDataT[1];                                \
00314     _pabyDataT[1] = byTemp;                                       \
00315 }                                                                    
00316                                                             
00317 #define CPL_SWAP32(x) \
00318         ((GUInt32)( \
00319             (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
00320             (((GUInt32)(x) & (GUInt32)0x0000ff00UL) <<  8) | \
00321             (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >>  8) | \
00322             (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
00323 
00324 #define CPL_SWAP32PTR(x) \
00325 {                                                                 \
00326     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00327                                                                   \
00328     byTemp = _pabyDataT[0];                                       \
00329     _pabyDataT[0] = _pabyDataT[3];                                \
00330     _pabyDataT[3] = byTemp;                                       \
00331     byTemp = _pabyDataT[1];                                       \
00332     _pabyDataT[1] = _pabyDataT[2];                                \
00333     _pabyDataT[2] = byTemp;                                       \
00334 }                                                                    
00335                                                             
00336 #define CPL_SWAP64PTR(x) \
00337 {                                                                 \
00338     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
00339                                                                   \
00340     byTemp = _pabyDataT[0];                                       \
00341     _pabyDataT[0] = _pabyDataT[7];                                \
00342     _pabyDataT[7] = byTemp;                                       \
00343     byTemp = _pabyDataT[1];                                       \
00344     _pabyDataT[1] = _pabyDataT[6];                                \
00345     _pabyDataT[6] = byTemp;                                       \
00346     byTemp = _pabyDataT[2];                                       \
00347     _pabyDataT[2] = _pabyDataT[5];                                \
00348     _pabyDataT[5] = byTemp;                                       \
00349     byTemp = _pabyDataT[3];                                       \
00350     _pabyDataT[3] = _pabyDataT[4];                                \
00351     _pabyDataT[4] = byTemp;                                       \
00352 }                                                                    
00353                                                             
00354 
00355 /* Until we have a safe 64 bits integer data type defined, we'll replace
00356 m * this version of the CPL_SWAP64() macro with a less efficient one.
00357  */
00358 /*
00359 #define CPL_SWAP64(x) \
00360         ((uint64)( \
00361             (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
00362             (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
00363             (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
00364             (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \
00365             (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \
00366             (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
00367             (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
00368             (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
00369 */
00370 
00371 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
00372 
00373 #ifdef CPL_MSB
00374 #  define CPL_MSBWORD16(x)      (x)
00375 #  define CPL_LSBWORD16(x)      CPL_SWAP16(x)
00376 #  define CPL_MSBWORD32(x)      (x)
00377 #  define CPL_LSBWORD32(x)      CPL_SWAP32(x)
00378 #  define CPL_MSBPTR16(x)       
00379 #  define CPL_LSBPTR16(x)       CPL_SWAP16PTR(x)
00380 #  define CPL_MSBPTR32(x)       
00381 #  define CPL_LSBPTR32(x)       CPL_SWAP32PTR(x)
00382 #  define CPL_MSBPTR64(x)       
00383 #  define CPL_LSBPTR64(x)       CPL_SWAP64PTR(x)
00384 #else
00385 #  define CPL_LSBWORD16(x)      (x)
00386 #  define CPL_MSBWORD16(x)      CPL_SWAP16(x)
00387 #  define CPL_LSBWORD32(x)      (x)
00388 #  define CPL_MSBWORD32(x)      CPL_SWAP32(x)
00389 #  define CPL_LSBPTR16(x)       
00390 #  define CPL_MSBPTR16(x)       CPL_SWAP16PTR(x)
00391 #  define CPL_LSBPTR32(x)       
00392 #  define CPL_MSBPTR32(x)       CPL_SWAP32PTR(x)
00393 #  define CPL_LSBPTR64(x)       
00394 #  define CPL_MSBPTR64(x)       CPL_SWAP64PTR(x)
00395 #endif
00396 
00397 /***********************************************************************
00398  * Define CPL_CVSID() macro.  It can be disabled during a build by
00399  * defining DISABLE_CPLID in the compiler options.
00400  *
00401  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
00402  * being unused.
00403  */
00404 
00405 #ifndef DISABLE_CVSID
00406 #  define CPL_CVSID(string)     static char cpl_cvsid[] = string; \
00407 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
00408 #else
00409 #  define CPL_CVSID(string)
00410 #endif
00411 
00412 #endif /* ndef CPL_BASE_H_INCLUDED */

Generated on Tue Mar 15 07:12:56 2005 for OGR by  doxygen 1.4.0