Main Page Class Hierarchy Compound List File List Compound Members File Members Related Pages

# cpl_port.h

Go to the documentation of this file.

    00001 /******************************************************************************
    00002  * $Id: cpl_port_h-source.html,v 1.13 2002/12/21 19:13:12 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-source.html,v $
    00044  * Revision 1.13  2002/12/21 19:13:12  warmerda
    00044  * updated
    00044  *
    00045  * Revision 1.32  2002/10/24 20:24:40  warmerda
    00046  * avoid using variable names likely to conflict in macros
    00047  *
    00048  * Revision 1.31  2002/07/15 13:31:46  warmerda
    00049  * CPL_SWAPDOUBLE had alignment problem, use CPL_SWAP64PTR
    00050  *
    00051  * Revision 1.30  2002/04/18 18:55:06  dron
    00052  * added <ctype.h> at the list of standard include files
    00053  *
    00054  * Revision 1.29  2002/01/17 01:40:27  warmerda
    00055  * added _LARGEFILE64_SOURCE support
    00056  *
    00057  * Revision 1.28  2001/08/30 21:20:49  warmerda
    00058  * expand tabs
    00059  *
    00060  * Revision 1.27  2001/07/18 04:00:49  warmerda
    00061  * added CPL_CVSID
    00062  *
    00063  * Revision 1.26  2001/06/21 21:17:26  warmerda
    00064  * added irix 64bit file api support
    00065  *
    00066  * Revision 1.25  2001/04/30 18:18:38  warmerda
    00067  * added macos support, standard header
    00068  *
    00069  * Revision 1.24  2001/01/19 21:16:41  warmerda
    00070  * expanded tabs
    00071  *
    00072  * Revision 1.23  2001/01/13 04:06:39  warmerda
    00073  * added strings.h on AIX as per patch from Dale.
    00074  *
    00075  * Revision 1.22  2001/01/03 16:18:07  warmerda
    00076  * added GUIntBig
    00077  *
    00078  * Revision 1.21  2000/10/20 04:20:33  warmerda
    00079  * added SWAP16PTR macros
    00080  *
    00081  * Revision 1.20  2000/10/13 17:32:42  warmerda
    00082  * check for unix instead of IGNORE_WIN32
    00083  *
    00084  * Revision 1.19  2000/09/25 19:58:43  warmerda
    00085  * ensure win32 doesn't get defined in Cygnus builds
    00086  *
    00087  * Revision 1.18  2000/07/20 13:15:03  warmerda
    00088  * don't redeclare CPL_DLL
    00089  */
    00090
    00091 #ifndef CPL_BASE_H_INCLUDED
    00092 #define CPL_BASE_H_INCLUDED
    00093
    00101 /* ==================================================================== */
    00102 /*      We will use macos_pre10 to indicate compilation with MacOS      */
    00103 /*      versions before MacOS X.                                        */
    00104 /* ==================================================================== */
    00105 #ifdef macintosh
    00106 #  define macos_pre10
    00107 #endif
    00108
    00109 /* ==================================================================== */
    00110 /*      We will use WIN32 as a standard windows define.                 */
    00111 /* ==================================================================== */
    00112 #if defined(_WIN32) && !defined(WIN32)
    00113 #  define WIN32
    00114 #endif
    00115
    00116 #if defined(_WINDOWS) && !defined(WIN32)
    00117 #  define WIN32
    00118 #endif
    00119
    00120 #include "cpl_config.h"
    00121
    00122 /* ==================================================================== */
    00123 /*      This will disable most WIN32 stuff in a Cygnus build which      */
    00124 /*      defines unix to 1.                                              */
    00125 /* ==================================================================== */
    00126
    00127 #ifdef unix
    00128 #  undef WIN32
    00129 #endif
    00130
    00131 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
    00132 #  define _LARGEFILE64_SOURCE 1
    00133 #endif
    00134
    00135 /* ==================================================================== */
    00136 /*      Standard include files.                                         */
    00137 /* ==================================================================== */
    00138
    00139 #include <stdio.h>
    00140 #include <stdlib.h>
    00141 #include <math.h>
    00142 #include <stdarg.h>
    00143 #include <string.h>
    00144 #include <ctype.h>
    00145 #include <errno.h>
    00146
    00147 #ifdef _AIX
    00148 #  include <strings.h>
    00149 #endif
    00150
    00151 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
    00152 #  define DBMALLOC
    00153 #  include <dbmalloc.h>
    00154 #endif
    00155
    00156 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
    00157 #  define USE_DMALLOC
    00158 #  include <dmalloc.h>
    00159 #endif
    00160
    00161 /* ==================================================================== */
    00162 /*      Base portability stuff ... this stuff may need to be            */
    00163 /*      modified for new platforms.                                     */
    00164 /* ==================================================================== */
    00165
    00166 /*---------------------------------------------------------------------
    00167  *        types for 16 and 32 bits integers, etc...
    00168  *--------------------------------------------------------------------*/
    00169 #if UINT_MAX == 65535
    00170 typedef long            GInt32;
    00171 typedef unsigned long   GUInt32;
    00172 #else
    00173 typedef int             GInt32;
    00174 typedef unsigned int    GUInt32;
    00175 #endif
    00176
    00177 typedef short           GInt16;
    00178 typedef unsigned short  GUInt16;
    00179 typedef unsigned char   GByte;
    00180 typedef int             GBool;
    00181
    00182 /* -------------------------------------------------------------------- */
    00183 /*      64bit support                                                   */
    00184 /* -------------------------------------------------------------------- */
    00185
    00186 #ifdef WIN32
    00187
    00188 #define VSI_LARGE_API_SUPPORTED
    00189 typedef __int64          GIntBig;
    00190 typedef unsigned __int64 GUIntBig;
    00191
    00192 #elif HAVE_LONG_LONG
    00193
    00194 typedef long long        GIntBig;
    00195 typedef unsigned long long GUIntBig;
    00196
    00197 #else
    00198
    00199 typedef long             GIntBig;
    00200 typedef unsigned long    GUIntBig;
    00201
    00202 #endif
    00203
    00204 /* ==================================================================== */
    00205 /*      Other standard services.                                        */
    00206 /* ==================================================================== */
    00207 #ifdef __cplusplus
    00208 #  define CPL_C_START           extern "C" {
    00209 #  define CPL_C_END             }
    00210 #else
    00211 #  define CPL_C_START
    00212 #  define CPL_C_END
    00213 #endif
    00214
    00215 #ifndef CPL_DLL
    00216 #if defined(WIN32) && !defined(CPL_DISABLE_DLL)
    00217 #  define CPL_DLL     __declspec(dllexport)
    00218 #else
    00219 #  define CPL_DLL
    00220 #endif
    00221 #endif
    00222
    00223
    00224 #ifndef NULL
    00225 #  define NULL  0
    00226 #endif
    00227
    00228 #ifndef FALSE
    00229 #  define FALSE 0
    00230 #endif
    00231
    00232 #ifndef TRUE
    00233 #  define TRUE  1
    00234 #endif
    00235
    00236 #ifndef MAX
    00237 #  define MIN(a,b)      ((a<b) ? a : b)
    00238 #  define MAX(a,b)      ((a>b) ? a : b)
    00239 #endif
    00240
    00241 #ifndef ABS
    00242 #  define ABS(x)        ((x<0) ? (-1*(x)) : x)
    00243 #endif
    00244
    00245 #ifndef EQUAL
    00246 #ifdef WIN32
    00247 #  define EQUALN(a,b,n)           (strnicmp(a,b,n)==0)
    00248 #  define EQUAL(a,b)              (stricmp(a,b)==0)
    00249 #else
    00250 #  define EQUALN(a,b,n)           (strncasecmp(a,b,n)==0)
    00251 #  define EQUAL(a,b)              (strcasecmp(a,b)==0)
    00252 #endif
    00253 #endif
    00254
    00255 #ifdef macos_pre10
    00256 int strcasecmp(char * str1, char * str2);
    00257 int strncasecmp(char * str1, char * str2, int len);
    00258 char * strdup (char *instr);
    00259 #endif
    00260
    00261 /*---------------------------------------------------------------------
    00262  *                         CPL_LSB and CPL_MSB
    00263  * Only one of these 2 macros should be defined and specifies the byte
    00264  * ordering for the current platform.
    00265  * This should be defined in the Makefile, but if it is not then
    00266  * the default is CPL_LSB (Intel ordering, LSB first).
    00267  *--------------------------------------------------------------------*/
    00268 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
    00269 #  define CPL_MSB
    00270 #endif
    00271
    00272 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
    00273 #define CPL_LSB
    00274 #endif
    00275
    00276 /*---------------------------------------------------------------------
    00277  *        Little endian <==> big endian byte swap macros.
    00278  *--------------------------------------------------------------------*/
    00279
    00280 #define CPL_SWAP16(x) \
    00281         ((GUInt16)( \
    00282             (((GUInt16)(x) & 0x00ffU) << 8) | \
    00283             (((GUInt16)(x) & 0xff00U) >> 8) ))
    00284
    00285 #define CPL_SWAP16PTR(x) \
    00286 {                                                                 \
    00287     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
    00288                                                                   \
    00289     byTemp = _pabyDataT[0];                                       \
    00290     _pabyDataT[0] = _pabyDataT[1];                                \
    00291     _pabyDataT[1] = byTemp;                                       \
    00292 }
    00293
    00294 #define CPL_SWAP32(x) \
    00295         ((GUInt32)( \
    00296             (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \
    00297             (((GUInt32)(x) & (GUInt32)0x0000ff00UL) <<  8) | \
    00298             (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >>  8) | \
    00299             (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) ))
    00300
    00301 #define CPL_SWAP32PTR(x) \
    00302 {                                                                 \
    00303     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
    00304                                                                   \
    00305     byTemp = _pabyDataT[0];                                       \
    00306     _pabyDataT[0] = _pabyDataT[3];                                \
    00307     _pabyDataT[3] = byTemp;                                       \
    00308     byTemp = _pabyDataT[1];                                       \
    00309     _pabyDataT[1] = _pabyDataT[2];                                \
    00310     _pabyDataT[2] = byTemp;                                       \
    00311 }
    00312
    00313 #define CPL_SWAP64PTR(x) \
    00314 {                                                                 \
    00315     GByte       byTemp, *_pabyDataT = (GByte *) (x);              \
    00316                                                                   \
    00317     byTemp = _pabyDataT[0];                                       \
    00318     _pabyDataT[0] = _pabyDataT[7];                                \
    00319     _pabyDataT[7] = byTemp;                                       \
    00320     byTemp = _pabyDataT[1];                                       \
    00321     _pabyDataT[1] = _pabyDataT[6];                                \
    00322     _pabyDataT[6] = byTemp;                                       \
    00323     byTemp = _pabyDataT[2];                                       \
    00324     _pabyDataT[2] = _pabyDataT[5];                                \
    00325     _pabyDataT[5] = byTemp;                                       \
    00326     byTemp = _pabyDataT[3];                                       \
    00327     _pabyDataT[3] = _pabyDataT[4];                                \
    00328     _pabyDataT[4] = byTemp;                                       \
    00329 }
    00330
    00331
    00332 /* Until we have a safe 64 bits integer data type defined, we'll replace
    00333 m * this version of the CPL_SWAP64() macro with a less efficient one.
    00334  */
    00335 /*
    00336 #define CPL_SWAP64(x) \
    00337         ((uint64)( \
    00338             (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
    00339             (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
    00340             (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
    00341             (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \
    00342             (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \
    00343             (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
    00344             (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
    00345             (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))
    00346 */
    00347
    00348 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
    00349
    00350 #ifdef CPL_MSB
    00351 #  define CPL_MSBWORD16(x)      (x)
    00352 #  define CPL_LSBWORD16(x)      CPL_SWAP16(x)
    00353 #  define CPL_MSBWORD32(x)      (x)
    00354 #  define CPL_LSBWORD32(x)      CPL_SWAP32(x)
    00355 #  define CPL_MSBPTR16(x)
    00356 #  define CPL_LSBPTR16(x)       CPL_SWAP16PTR(x)
    00357 #  define CPL_MSBPTR32(x)
    00358 #  define CPL_LSBPTR32(x)       CPL_SWAP32PTR(x)
    00359 #  define CPL_MSBPTR64(x)
    00360 #  define CPL_LSBPTR64(x)       CPL_SWAP64PTR(x)
    00361 #else
    00362 #  define CPL_LSBWORD16(x)      (x)
    00363 #  define CPL_MSBWORD16(x)      CPL_SWAP16(x)
    00364 #  define CPL_LSBWORD32(x)      (x)
    00365 #  define CPL_MSBWORD32(x)      CPL_SWAP32(x)
    00366 #  define CPL_LSBPTR16(x)
    00367 #  define CPL_MSBPTR16(x)       CPL_SWAP16PTR(x)
    00368 #  define CPL_LSBPTR32(x)
    00369 #  define CPL_MSBPTR32(x)       CPL_SWAP32PTR(x)
    00370 #  define CPL_LSBPTR64(x)
    00371 #  define CPL_MSBPTR64(x)       CPL_SWAP64PTR(x)
    00372 #endif
    00373
    00374 /***********************************************************************
    00375  * Define CPL_CVSID() macro.  It can be disabled during a build by
    00376  * defining DISABLE_CPLID in the compiler options.
    00377  *
    00378  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
    00379  * being unused.
    00380  */
    00381
    00382 #ifndef DISABLE_CVSID
    00383 #  define CPL_CVSID(string)     static char cpl_cvsid[] = string; \
    00384 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
    00385 #else
    00386 #  define CPL_CVSID(string)
    00387 #endif
    00388
    00389 #endif /* ndef CPL_BASE_H_INCLUDED */