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.5 2000/11/06 04:49:01 warmerda 00040 * *** empty log message *** 00040 * 00041 * Revision 1.21 2000/10/20 04:20:33 warmerda 00042 * added SWAP16PTR macros 00043 * 00044 * Revision 1.20 2000/10/13 17:32:42 warmerda 00045 * check for unix instead of IGNORE_WIN32 00046 * 00047 * Revision 1.19 2000/09/25 19:58:43 warmerda 00048 * ensure win32 doesn't get defined in Cygnus builds 00049 * 00050 * Revision 1.18 2000/07/20 13:15:03 warmerda 00051 * don't redeclare CPL_DLL 00052 * 00053 * Revision 1.17 2000/07/06 20:30:03 warmerda 00054 * Removed extra NULL definition. 00055 * 00056 * Revision 1.16 2000/04/26 18:25:10 warmerda 00057 * implement CPL_DLL 00058 * 00059 * Revision 1.15 2000/01/29 22:31:02 warmerda 00060 * Define WIN32 if _WINDOWS is defined. 00061 * 00062 * Revision 1.14 1999/09/21 13:39:54 warmerda 00063 * Added some more swapping macros. 00064 * 00065 * Revision 1.13 1999/05/20 02:54:38 warmerda 00066 * Added API documentation 00067 * 00068 * Revision 1.12 1999/05/14 20:35:03 warmerda 00069 * added some more swapping macros 00070 * 00071 * Revision 1.11 1999/05/13 19:19:06 warmerda 00072 * Only use dbmalloc if DEBUG is set. 00073 * 00074 * Revision 1.10 1999/03/02 21:08:11 warmerda 00075 * autoconf switch 00076 * 00077 * Revision 1.9 1999/02/17 01:41:17 warmerda 00078 * Added NULL. 00079 * 00080 * Revision 1.8 1999/02/02 21:32:38 warmerda 00081 * Added CPL_{MSB,LSB}WORD{16,32} macros. 00082 * 00083 * Revision 1.7 1999/02/02 19:02:36 warmerda 00084 * Removed duplicates of base types, and CPL_LSB 00085 * 00086 * Revision 1.6 1999/01/28 18:36:06 warmerda 00087 * Ensure WIN32 is defined on Windows. 00088 * 00089 * Revision 1.5 1999/01/28 05:26:12 danmo 00090 * Added byte swapping macros. 00091 * 00092 * Revision 1.4 1998/12/15 19:05:30 warmerda 00093 * added errno.h 00094 * 00095 * Revision 1.3 1998/12/14 04:50:07 warmerda 00096 * Added DBMALLOC support 00097 * 00098 * Revision 1.2 1998/12/04 21:38:40 danmo 00099 * Changed str*casecmp() to str*icmp() for WIN32 00100 * 00101 * Revision 1.1 1998/12/03 18:26:02 warmerda 00102 * New 00103 * 00104 */ 00105 00106 #ifndef CPL_BASE_H_INCLUDED 00107 #define CPL_BASE_H_INCLUDED 00108 00116 /* ==================================================================== */ 00117 /* We will use WIN32 as a standard windows define. */ 00118 /* ==================================================================== */ 00119 #if defined(_WIN32) && !defined(WIN32) 00120 # define WIN32 00121 #endif 00122 00123 #if defined(_WINDOWS) && !defined(WIN32) 00124 # define WIN32 00125 #endif 00126 00127 #include "cpl_config.h" 00128 00129 /* ==================================================================== */ 00130 /* This will disable most WIN32 stuff in a Cygnus build which */ 00131 /* defines unix to 1. */ 00132 /* ==================================================================== */ 00133 00134 #ifdef unix 00135 # undef WIN32 00136 #endif 00137 00138 /* ==================================================================== */ 00139 /* Standard include files. */ 00140 /* ==================================================================== */ 00141 00142 #include <stdio.h> 00143 #include <stdlib.h> 00144 #include <math.h> 00145 #include <stdarg.h> 00146 #include <string.h> 00147 #include <errno.h> 00148 00149 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG) 00150 # define DBMALLOC 00151 # include <dbmalloc.h> 00152 #endif 00153 00154 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H) 00155 # define USE_DMALLOC 00156 # include <dmalloc.h> 00157 #endif 00158 00159 /* ==================================================================== */ 00160 /* Base portability stuff ... this stuff may need to be */ 00161 /* modified for new platforms. */ 00162 /* ==================================================================== */ 00163 00164 /*--------------------------------------------------------------------- 00165 * types for 16 and 32 bits integers, etc... 00166 *--------------------------------------------------------------------*/ 00167 #if UINT_MAX == 65535 00168 typedef long GInt32; 00169 typedef unsigned long GUInt32; 00170 #else 00171 typedef int GInt32; 00172 typedef unsigned int GUInt32; 00173 #endif 00174 00175 typedef short GInt16; 00176 typedef unsigned short GUInt16; 00177 typedef unsigned char GByte; 00178 typedef int GBool; 00179 00180 00181 /* ==================================================================== */ 00182 /* Other standard services. */ 00183 /* ==================================================================== */ 00184 #ifdef __cplusplus 00185 # define CPL_C_START extern "C" { 00186 # define CPL_C_END } 00187 #else 00188 # define CPL_C_START 00189 # define CPL_C_END 00190 #endif 00191 00192 #ifndef CPL_DLL 00193 #if defined(WIN32) && !defined(CPL_DISABLE_DLL) 00194 # define CPL_DLL __declspec(dllexport) 00195 #else 00196 # define CPL_DLL 00197 #endif 00198 #endif 00199 00200 00201 #ifndef NULL 00202 # define NULL 0 00203 #endif 00204 00205 #ifndef FALSE 00206 # define FALSE 0 00207 #endif 00208 00209 #ifndef TRUE 00210 # define TRUE 1 00211 #endif 00212 00213 #ifndef MAX 00214 # define MIN(a,b) ((a<b) ? a : b) 00215 # define MAX(a,b) ((a>b) ? a : b) 00216 #endif 00217 00218 #ifndef ABS 00219 # define ABS(x) ((x<0) ? (-1*(x)) : x) 00220 #endif 00221 00222 #ifndef EQUAL 00223 #ifdef WIN32 00224 # define EQUALN(a,b,n) (strnicmp(a,b,n)==0) 00225 # define EQUAL(a,b) (stricmp(a,b)==0) 00226 #else 00227 # define EQUALN(a,b,n) (strncasecmp(a,b,n)==0) 00228 # define EQUAL(a,b) (strcasecmp(a,b)==0) 00229 #endif 00230 #endif 00231 00232 /*--------------------------------------------------------------------- 00233 * CPL_LSB and CPL_MSB 00234 * Only one of these 2 macros should be defined and specifies the byte 00235 * ordering for the current platform. 00236 * This should be defined in the Makefile, but if it is not then 00237 * the default is CPL_LSB (Intel ordering, LSB first). 00238 *--------------------------------------------------------------------*/ 00239 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB) 00240 # define CPL_MSB 00241 #endif 00242 00243 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) ) 00244 #define CPL_LSB 00245 #endif 00246 00247 /*--------------------------------------------------------------------- 00248 * Little endian <==> big endian byte swap macros. 00249 *--------------------------------------------------------------------*/ 00250 00251 #define CPL_SWAP16(x) \ 00252 ((GUInt16)( \ 00253 (((GUInt16)(x) & 0x00ffU) << 8) | \ 00254 (((GUInt16)(x) & 0xff00U) >> 8) )) 00255 00256 #define CPL_SWAP16PTR(x) \ 00257 { \ 00258 GByte byTemp, *pabyData = (GByte *) (x); \ 00259 \ 00260 byTemp = pabyData[0]; \ 00261 pabyData[0] = pabyData[1]; \ 00262 pabyData[1] = byTemp; \ 00263 } 00264 00265 #define CPL_SWAP32(x) \ 00266 ((GUInt32)( \ 00267 (((GUInt32)(x) & (GUInt32)0x000000ffUL) << 24) | \ 00268 (((GUInt32)(x) & (GUInt32)0x0000ff00UL) << 8) | \ 00269 (((GUInt32)(x) & (GUInt32)0x00ff0000UL) >> 8) | \ 00270 (((GUInt32)(x) & (GUInt32)0xff000000UL) >> 24) )) 00271 00272 #define CPL_SWAP32PTR(x) \ 00273 { \ 00274 GByte byTemp, *pabyData = (GByte *) (x); \ 00275 \ 00276 byTemp = pabyData[0]; \ 00277 pabyData[0] = pabyData[3]; \ 00278 pabyData[3] = byTemp; \ 00279 byTemp = pabyData[1]; \ 00280 pabyData[1] = pabyData[2]; \ 00281 pabyData[2] = byTemp; \ 00282 } 00283 00284 #define CPL_SWAP64PTR(x) \ 00285 { \ 00286 GByte byTemp, *pabyData = (GByte *) (x); \ 00287 \ 00288 byTemp = pabyData[0]; \ 00289 pabyData[0] = pabyData[7]; \ 00290 pabyData[7] = byTemp; \ 00291 byTemp = pabyData[1]; \ 00292 pabyData[1] = pabyData[6]; \ 00293 pabyData[6] = byTemp; \ 00294 byTemp = pabyData[2]; \ 00295 pabyData[2] = pabyData[5]; \ 00296 pabyData[5] = byTemp; \ 00297 byTemp = pabyData[3]; \ 00298 pabyData[3] = pabyData[4]; \ 00299 pabyData[4] = byTemp; \ 00300 } 00301 00302 00303 /* Until we have a safe 64 bits integer data type defined, we'll replace 00304 m * this version of the CPL_SWAP64() macro with a less efficient one. 00305 */ 00306 /* 00307 #define CPL_SWAP64(x) \ 00308 ((uint64)( \ 00309 (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \ 00310 (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \ 00311 (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \ 00312 (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) << 8) | \ 00313 (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >> 8) | \ 00314 (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \ 00315 (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \ 00316 (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) )) 00317 */ 00318 00319 #define CPL_SWAPDOUBLE(p) { \ 00320 double _tmp = *(double *)(p); \ 00321 ((GByte *)(p))[0] = ((GByte *)&_tmp)[7]; \ 00322 ((GByte *)(p))[1] = ((GByte *)&_tmp)[6]; \ 00323 ((GByte *)(p))[2] = ((GByte *)&_tmp)[5]; \ 00324 ((GByte *)(p))[3] = ((GByte *)&_tmp)[4]; \ 00325 ((GByte *)(p))[4] = ((GByte *)&_tmp)[3]; \ 00326 ((GByte *)(p))[5] = ((GByte *)&_tmp)[2]; \ 00327 ((GByte *)(p))[6] = ((GByte *)&_tmp)[1]; \ 00328 ((GByte *)(p))[7] = ((GByte *)&_tmp)[0]; \ 00329 } 00330 00331 #ifdef CPL_MSB 00332 # define CPL_MSBWORD16(x) (x) 00333 # define CPL_LSBWORD16(x) CPL_SWAP16(x) 00334 # define CPL_MSBWORD32(x) (x) 00335 # define CPL_LSBWORD32(x) CPL_SWAP32(x) 00336 # define CPL_MSBPTR16(x) 00337 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x) 00338 # define CPL_MSBPTR32(x) 00339 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x) 00340 # define CPL_MSBPTR64(x) 00341 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x) 00342 #else 00343 # define CPL_LSBWORD16(x) (x) 00344 # define CPL_MSBWORD16(x) CPL_SWAP16(x) 00345 # define CPL_LSBWORD32(x) (x) 00346 # define CPL_MSBWORD32(x) CPL_SWAP32(x) 00347 # define CPL_LSBPTR16(x) 00348 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x) 00349 # define CPL_LSBPTR32(x) 00350 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x) 00351 # define CPL_LSBPTR64(x) 00352 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x) 00353 #endif 00354 00355 #endif /* ndef CPL_BASE_H_INCLUDED */