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