00001 /****************************************************************************** 00002 * Copyright (c) 1998, Frank Warmerdam 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00020 * DEALINGS IN THE SOFTWARE. 00021 ****************************************************************************** 00022 * 00023 * cpl_port.h 00024 * 00025 * Include file providing low level portability services for CPL. This 00026 * should be the first include file for any CPL based code. It provides the 00027 * following: 00028 * 00029 * o Includes some standard system include files, such as stdio, and stdlib. 00030 * 00031 * o Defines CPL_C_START, CPL_C_END macros. 00032 * 00033 * o Ensures that some other standard macros like NULL are defined. 00034 * 00035 * o Defines some portability stuff like CPL_MSB, or CPL_LSB. 00036 * 00037 * o Ensures that core types such as GBool, GInt32, GInt16, GUInt32, 00038 * GUInt16, and GByte are defined. 00039 * 00040 * $Log: cpl_port_h-source.html,v $ 00040 * Revision 1.6 2001/01/22 22:22:23 warmerda 00040 * *** empty log message *** 00040 * 00041 * Revision 1.23 2001/01/13 04:06:39 warmerda 00042 * added strings.h on AIX as per patch from Dale. 00043 * 00044 * Revision 1.22 2001/01/03 16:18:07 warmerda 00045 * added GUIntBig 00046 * 00047 * Revision 1.21 2000/10/20 04:20:33 warmerda 00048 * added SWAP16PTR macros 00049 * 00050 * Revision 1.20 2000/10/13 17:32:42 warmerda 00051 * check for unix instead of IGNORE_WIN32 00052 * 00053 * Revision 1.19 2000/09/25 19:58:43 warmerda 00054 * ensure win32 doesn't get defined in Cygnus builds 00055 * 00056 * Revision 1.18 2000/07/20 13:15:03 warmerda 00057 * don't redeclare CPL_DLL 00058 * 00059 * Revision 1.17 2000/07/06 20:30:03 warmerda 00060 * Removed extra NULL definition. 00061 * 00062 * Revision 1.16 2000/04/26 18:25:10 warmerda 00063 * implement CPL_DLL 00064 * 00065 * Revision 1.15 2000/01/29 22:31:02 warmerda 00066 * Define WIN32 if _WINDOWS is defined. 00067 * 00068 * Revision 1.14 1999/09/21 13:39:54 warmerda 00069 * Added some more swapping macros. 00070 * 00071 * Revision 1.13 1999/05/20 02:54:38 warmerda 00072 * Added API documentation 00073 * 00074 * Revision 1.12 1999/05/14 20:35:03 warmerda 00075 * added some more swapping macros 00076 * 00077 * Revision 1.11 1999/05/13 19:19:06 warmerda 00078 * Only use dbmalloc if DEBUG is set. 00079 * 00080 * Revision 1.10 1999/03/02 21:08:11 warmerda 00081 * autoconf switch 00082 * 00083 * Revision 1.9 1999/02/17 01:41:17 warmerda 00084 * Added NULL. 00085 * 00086 * Revision 1.8 1999/02/02 21:32:38 warmerda 00087 * Added CPL_{MSB,LSB}WORD{16,32} macros. 00088 * 00089 * Revision 1.7 1999/02/02 19:02:36 warmerda 00090 * Removed duplicates of base types, and CPL_LSB 00091 * 00092 * Revision 1.6 1999/01/28 18:36:06 warmerda 00093 * Ensure WIN32 is defined on Windows. 00094 * 00095 * Revision 1.5 1999/01/28 05:26:12 danmo 00096 * Added byte swapping macros. 00097 * 00098 * Revision 1.4 1998/12/15 19:05:30 warmerda 00099 * added errno.h 00100 * 00101 * Revision 1.3 1998/12/14 04:50:07 warmerda 00102 * Added DBMALLOC support 00103 * 00104 * Revision 1.2 1998/12/04 21:38:40 danmo 00105 * Changed str*casecmp() to str*icmp() for WIN32 00106 * 00107 * Revision 1.1 1998/12/03 18:26:02 warmerda 00108 * New 00109 * 00110 */ 00111 00112 #ifndef CPL_BASE_H_INCLUDED 00113 #define CPL_BASE_H_INCLUDED 00114 00122 /* ==================================================================== */ 00123 /* We will use WIN32 as a standard windows define. */ 00124 /* ==================================================================== */ 00125 #if defined(_WIN32) && !defined(WIN32) 00126 # define WIN32 00127 #endif 00128 00129 #if defined(_WINDOWS) && !defined(WIN32) 00130 # define WIN32 00131 #endif 00132 00133 #include "cpl_config.h" 00134 00135 /* ==================================================================== */ 00136 /* This will disable most WIN32 stuff in a Cygnus build which */ 00137 /* defines unix to 1. */ 00138 /* ==================================================================== */ 00139 00140 #ifdef unix 00141 # undef WIN32 00142 #endif 00143 00144 /* ==================================================================== */ 00145 /* Standard include files. */ 00146 /* ==================================================================== */ 00147 00148 #include <stdio.h> 00149 #include <stdlib.h> 00150 #include <math.h> 00151 #include <stdarg.h> 00152 #include <string.h> 00153 #include <errno.h> 00154 00155 #ifdef _AIX 00156 # include <strings.h> 00157 #endif 00158 00159 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG) 00160 # define DBMALLOC 00161 # include <dbmalloc.h> 00162 #endif 00163 00164 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H) 00165 # define USE_DMALLOC 00166 # include <dmalloc.h> 00167 #endif 00168 00169 /* ==================================================================== */ 00170 /* Base portability stuff ... this stuff may need to be */ 00171 /* modified for new platforms. */ 00172 /* ==================================================================== */ 00173 00174 /*--------------------------------------------------------------------- 00175 * types for 16 and 32 bits integers, etc... 00176 *--------------------------------------------------------------------*/ 00177 #if UINT_MAX == 65535 00178 typedef long GInt32; 00179 typedef unsigned long GUInt32; 00180 #else 00181 typedef int GInt32; 00182 typedef unsigned int GUInt32; 00183 #endif 00184 00185 typedef short GInt16; 00186 typedef unsigned short GUInt16; 00187 typedef unsigned char GByte; 00188 typedef int GBool; 00189 00190 /* -------------------------------------------------------------------- */ 00191 /* 64bit support */ 00192 /* -------------------------------------------------------------------- */ 00193 00194 #ifdef WIN32 00195 00196 #define VSI_LARGE_API_SUPPORTED 00197 typedef __int64 GIntBig; 00198 typedef unsigned __int64 GUIntBig; 00199 00200 #else 00201 00202 typedef long GIntBig; 00203 typedef unsigned long GUIntBig; 00204 00205 #endif 00206 00207 /* ==================================================================== */ 00208 /* Other standard services. */ 00209 /* ==================================================================== */ 00210 #ifdef __cplusplus 00211 # define CPL_C_START extern "C" { 00212 # define CPL_C_END } 00213 #else 00214 # define CPL_C_START 00215 # define CPL_C_END 00216 #endif 00217 00218 #ifndef CPL_DLL 00219 #if defined(WIN32) && !defined(CPL_DISABLE_DLL) 00220 # define CPL_DLL __declspec(dllexport) 00221 #else 00222 # define CPL_DLL 00223 #endif 00224 #endif 00225 00226 00227 #ifndef NULL 00228 # define NULL 0 00229 #endif 00230 00231 #ifndef FALSE 00232 # define FALSE 0 00233 #endif 00234 00235 #ifndef TRUE 00236 # define TRUE 1 00237 #endif 00238 00239 #ifndef MAX 00240 # define MIN(a,b) ((a<b) ? a : b) 00241 # define MAX(a,b) ((a>b) ? a : b) 00242 #endif 00243 00244 #ifndef ABS 00245 # define ABS(x) ((x<0) ? (-1*(x)) : x) 00246 #endif 00247 00248 #ifndef EQUAL 00249 #ifdef WIN32 00250 # define EQUALN(a,b,n) (strnicmp(a,b,n)==0) 00251 # define EQUAL(a,b) (stricmp(a,b)==0) 00252 #else 00253 # define EQUALN(a,b,n) (strncasecmp(a,b,n)==0) 00254 # define EQUAL(a,b) (strcasecmp(a,b)==0) 00255 #endif 00256 #endif 00257 00258 /*--------------------------------------------------------------------- 00259 * CPL_LSB and CPL_MSB 00260 * Only one of these 2 macros should be defined and specifies the byte 00261 * ordering for the current platform. 00262 * This should be defined in the Makefile, but if it is not then 00263 * the default is CPL_LSB (Intel ordering, LSB first). 00264 *--------------------------------------------------------------------*/ 00265 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB) 00266 # define CPL_MSB 00267 #endif 00268 00269 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) ) 00270 #define CPL_LSB 00271 #endif 00272 00273 /*--------------------------------------------------------------------- 00274 * Little endian <==> big endian byte swap macros. 00275 *--------------------------------------------------------------------*/ 00276 00277 #define CPL_SWAP16(x) \ 00278 ((GUInt16)( \ 00279 (((GUInt16)(x) & 0x00ffU) << 8) | \ 00280 (((GUInt16)(x) & 0xff00U) >> 8) )) 00281 00282 #define CPL_SWAP16PTR(x) \ 00283 { \ 00284 GByte byTemp, *pabyData = (GByte *) (x); \ 00285 \ 00286 byTemp = pabyData[0]; \ 00287 pabyData[0] = pabyData[1]; \ 00288 pabyData[1] = byTemp; \ 00289 } 00290 00291 #define CPL_SWAP32(x) \ 00292 ((GUInt32)( \ 00293 (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \ 00294 (((GUInt32)(x) & (GUInt32)0x0000ff00UL) << 8) | \ 00295 (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >> 8) | \ 00296 (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) )) 00297 00298 #define CPL_SWAP32PTR(x) \ 00299 { \ 00300 GByte byTemp, *pabyData = (GByte *) (x); \ 00301 \ 00302 byTemp = pabyData[0]; \ 00303 pabyData[0] = pabyData[3]; \ 00304 pabyData[3] = byTemp; \ 00305 byTemp = pabyData[1]; \ 00306 pabyData[1] = pabyData[2]; \ 00307 pabyData[2] = byTemp; \ 00308 } 00309 00310 #define CPL_SWAP64PTR(x) \ 00311 { \ 00312 GByte byTemp, *pabyData = (GByte *) (x); \ 00313 \ 00314 byTemp = pabyData[0]; \ 00315 pabyData[0] = pabyData[7]; \ 00316 pabyData[7] = byTemp; \ 00317 byTemp = pabyData[1]; \ 00318 pabyData[1] = pabyData[6]; \ 00319 pabyData[6] = byTemp; \ 00320 byTemp = pabyData[2]; \ 00321 pabyData[2] = pabyData[5]; \ 00322 pabyData[5] = byTemp; \ 00323 byTemp = pabyData[3]; \ 00324 pabyData[3] = pabyData[4]; \ 00325 pabyData[4] = byTemp; \ 00326 } 00327 00328 00329 /* Until we have a safe 64 bits integer data type defined, we'll replace 00330 m * this version of the CPL_SWAP64() macro with a less efficient one. 00331 */ 00332 /* 00333 #define CPL_SWAP64(x) \ 00334 ((uint64)( \ 00335 (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \ 00336 (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \ 00337 (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \ 00338 (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \ 00339 (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \ 00340 (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \ 00341 (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \ 00342 (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) )) 00343 */ 00344 00345 #define CPL_SWAPDOUBLE(p) { \ 00346 double _tmp = *(double *)(p); \ 00347 ((GByte *)(p))[0] = ((GByte *)&_tmp)[7]; \ 00348 ((GByte *)(p))[1] = ((GByte *)&_tmp)[6]; \ 00349 ((GByte *)(p))[2] = ((GByte *)&_tmp)[5]; \ 00350 ((GByte *)(p))[3] = ((GByte *)&_tmp)[4]; \ 00351 ((GByte *)(p))[4] = ((GByte *)&_tmp)[3]; \ 00352 ((GByte *)(p))[5] = ((GByte *)&_tmp)[2]; \ 00353 ((GByte *)(p))[6] = ((GByte *)&_tmp)[1]; \ 00354 ((GByte *)(p))[7] = ((GByte *)&_tmp)[0]; \ 00355 } 00356 00357 #ifdef CPL_MSB 00358 # define CPL_MSBWORD16(x) (x) 00359 # define CPL_LSBWORD16(x) CPL_SWAP16(x) 00360 # define CPL_MSBWORD32(x) (x) 00361 # define CPL_LSBWORD32(x) CPL_SWAP32(x) 00362 # define CPL_MSBPTR16(x) 00363 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x) 00364 # define CPL_MSBPTR32(x) 00365 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x) 00366 # define CPL_MSBPTR64(x) 00367 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x) 00368 #else 00369 # define CPL_LSBWORD16(x) (x) 00370 # define CPL_MSBWORD16(x) CPL_SWAP16(x) 00371 # define CPL_LSBWORD32(x) (x) 00372 # define CPL_MSBWORD32(x) CPL_SWAP32(x) 00373 # define CPL_LSBPTR16(x) 00374 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x) 00375 # define CPL_LSBPTR32(x) 00376 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x) 00377 # define CPL_LSBPTR64(x) 00378 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x) 00379 #endif 00380 00381 #endif /* ndef CPL_BASE_H_INCLUDED */