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