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