00001 /****************************************************************************** 00002 * $Id: cpl_conv_cpp-source.html,v 1.5 2000/11/06 04:49:01 warmerda Exp $ 00003 * 00004 * Project: CPL - Common Portability Library 00005 * Purpose: Convenience functions. 00006 * Author: Frank Warmerdam, warmerda@home.com 00007 * 00008 ****************************************************************************** 00009 * Copyright (c) 1998, Frank Warmerdam 00010 * 00011 * Permission is hereby granted, free of charge, to any person obtaining a 00012 * copy of this software and associated documentation files (the "Software"), 00013 * to deal in the Software without restriction, including without limitation 00014 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00015 * and/or sell copies of the Software, and to permit persons to whom the 00016 * Software is furnished to do so, subject to the following conditions: 00017 * 00018 * The above copyright notice and this permission notice shall be included 00019 * in all copies or substantial portions of the Software. 00020 * 00021 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00022 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00023 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00024 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00025 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00026 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00027 * DEALINGS IN THE SOFTWARE. 00028 ****************************************************************************** 00029 * 00030 * $Log: cpl_conv_cpp-source.html,v $ 00030 * Revision 1.5 2000/11/06 04:49:01 warmerda 00030 * *** empty log message *** 00030 * 00031 * Revision 1.9 2000/04/17 15:56:11 warmerda 00032 * make configuration tests always happen 00033 * 00034 * Revision 1.8 2000/04/05 21:02:47 warmerda 00035 * Added CPLVerifyConfiguration() 00036 * 00037 * Revision 1.7 1999/08/27 12:55:39 danmo 00038 * Support 0 bytes allocations in CPLRealloc() 00039 * 00040 * Revision 1.6 1999/06/25 04:38:03 warmerda 00041 * Fixed CPLReadLine() to work for long lines. 00042 * 00043 * Revision 1.5 1999/05/20 02:54:37 warmerda 00044 * Added API documentation 00045 * 00046 * Revision 1.4 1999/01/02 20:29:53 warmerda 00047 * Allow zero length allocations 00048 * 00049 * Revision 1.3 1998/12/15 19:01:07 warmerda 00050 * Added CPLReadLine(). 00051 * 00052 * Revision 1.2 1998/12/03 18:30:04 warmerda 00053 * Use CPLError() instead of GPSError(). 00054 * 00055 * Revision 1.1 1998/12/02 19:33:23 warmerda 00056 * New 00057 * 00058 */ 00059 00060 #include "cpl_conv.h" 00061 00062 /************************************************************************/ 00063 /* CPLCalloc() */ 00064 /************************************************************************/ 00065 00083 void *CPLCalloc( size_t nCount, size_t nSize ) 00084 00085 { 00086 void *pReturn; 00087 00088 if( nSize * nCount == 0 ) 00089 return NULL; 00090 00091 pReturn = VSICalloc( nCount, nSize ); 00092 if( pReturn == NULL ) 00093 { 00094 CPLError( CE_Fatal, CPLE_OutOfMemory, 00095 "CPLCalloc(): Out of memory allocating %d bytes.\n", 00096 nSize * nCount ); 00097 } 00098 00099 return pReturn; 00100 } 00101 00102 /************************************************************************/ 00103 /* CPLMalloc() */ 00104 /************************************************************************/ 00105 00121 void *CPLMalloc( size_t nSize ) 00122 00123 { 00124 void *pReturn; 00125 00126 CPLVerifyConfiguration(); 00127 00128 if( nSize == 0 ) 00129 return NULL; 00130 00131 pReturn = VSIMalloc( nSize ); 00132 if( pReturn == NULL ) 00133 { 00134 CPLError( CE_Fatal, CPLE_OutOfMemory, 00135 "CPLMalloc(): Out of memory allocating %d bytes.\n", 00136 nSize ); 00137 } 00138 00139 return pReturn; 00140 } 00141 00142 /************************************************************************/ 00143 /* CPLRealloc() */ 00144 /************************************************************************/ 00145 00166 void * CPLRealloc( void * pData, size_t nNewSize ) 00167 00168 { 00169 void *pReturn; 00170 00171 if ( nNewSize == 0 ) 00172 { 00173 VSIFree(pData); 00174 return NULL; 00175 } 00176 00177 if( pData == NULL ) 00178 pReturn = VSIMalloc( nNewSize ); 00179 else 00180 pReturn = VSIRealloc( pData, nNewSize ); 00181 00182 if( pReturn == NULL ) 00183 { 00184 CPLError( CE_Fatal, CPLE_OutOfMemory, 00185 "CPLRealloc(): Out of memory allocating %d bytes.\n", 00186 nNewSize ); 00187 } 00188 00189 return pReturn; 00190 } 00191 00192 /************************************************************************/ 00193 /* CPLStrdup() */ 00194 /************************************************************************/ 00195 00214 char *CPLStrdup( const char * pszString ) 00215 00216 { 00217 char *pszReturn; 00218 00219 if( pszString == NULL ) 00220 pszString = ""; 00221 00222 pszReturn = VSIStrdup( pszString ); 00223 00224 if( pszReturn == NULL ) 00225 { 00226 CPLError( CE_Fatal, CPLE_OutOfMemory, 00227 "CPLStrdup(): Out of memory allocating %d bytes.\n", 00228 strlen(pszString) ); 00229 00230 } 00231 00232 return( pszReturn ); 00233 } 00234 00235 /************************************************************************/ 00236 /* CPLReadLine() */ 00237 /************************************************************************/ 00238 00256 const char *CPLReadLine( FILE * fp ) 00257 00258 { 00259 static char *pszRLBuffer = NULL; 00260 static int nRLBufferSize = 0; 00261 int nLength, nReadSoFar = 0; 00262 00263 /* -------------------------------------------------------------------- */ 00264 /* Loop reading chunks of the line till we get to the end of */ 00265 /* the line. */ 00266 /* -------------------------------------------------------------------- */ 00267 do { 00268 /* -------------------------------------------------------------------- */ 00269 /* Grow the working buffer if we have it nearly full. Fail out */ 00270 /* of read line if we can't reallocate it big enough (for */ 00271 /* instance for a _very large_ file with no newlines). */ 00272 /* -------------------------------------------------------------------- */ 00273 if( nRLBufferSize-nReadSoFar < 128 ) 00274 { 00275 nRLBufferSize = nRLBufferSize*2 + 128; 00276 pszRLBuffer = (char *) VSIRealloc(pszRLBuffer, nRLBufferSize); 00277 if( pszRLBuffer == NULL ) 00278 { 00279 nRLBufferSize = 0; 00280 return NULL; 00281 } 00282 } 00283 00284 /* -------------------------------------------------------------------- */ 00285 /* Do the actual read. */ 00286 /* -------------------------------------------------------------------- */ 00287 if( VSIFGets( pszRLBuffer+nReadSoFar, nRLBufferSize-nReadSoFar, fp ) 00288 == NULL ) 00289 return NULL; 00290 00291 nReadSoFar = strlen(pszRLBuffer); 00292 00293 } while( nReadSoFar == nRLBufferSize - 1 00294 && pszRLBuffer[nRLBufferSize-2] != 13 00295 && pszRLBuffer[nRLBufferSize-2] != 10 ); 00296 00297 /* -------------------------------------------------------------------- */ 00298 /* Clear CR and LF off the end. */ 00299 /* -------------------------------------------------------------------- */ 00300 nLength = strlen(pszRLBuffer); 00301 if( nLength > 0 00302 && (pszRLBuffer[nLength-1] == 10 || pszRLBuffer[nLength-1] == 13) ) 00303 { 00304 pszRLBuffer[--nLength] = '\0'; 00305 } 00306 00307 if( nLength > 0 00308 && (pszRLBuffer[nLength-1] == 10 || pszRLBuffer[nLength-1] == 13) ) 00309 { 00310 pszRLBuffer[--nLength] = '\0'; 00311 } 00312 00313 return( pszRLBuffer ); 00314 } 00315 00316 /************************************************************************/ 00317 /* CPLVerifyConfiguration() */ 00318 /************************************************************************/ 00319 00320 void CPLVerifyConfiguration() 00321 00322 { 00323 /* -------------------------------------------------------------------- */ 00324 /* Verify data types. */ 00325 /* -------------------------------------------------------------------- */ 00326 CPLAssert( sizeof(GInt32) == 4 ); 00327 CPLAssert( sizeof(GInt16) == 2 ); 00328 CPLAssert( sizeof(GByte) == 1 ); 00329 00330 if( sizeof(GInt32) != 4 ) 00331 CPLError( CE_Fatal, CPLE_AppDefined, 00332 "sizeof(GInt32) == %d ... yow!\n", 00333 sizeof(GInt32) ); 00334 00335 /* -------------------------------------------------------------------- */ 00336 /* Verify byte order */ 00337 /* -------------------------------------------------------------------- */ 00338 GInt32 nTest; 00339 00340 nTest = 1; 00341 00342 #ifdef CPL_LSB 00343 if( ((GByte *) &nTest)[0] != 1 ) 00344 #endif 00345 #ifdef CPL_MSB 00346 if( ((GByte *) &nTest)[3] != 1 ) 00347 #endif 00348 CPLError( CE_Fatal, CPLE_AppDefined, 00349 "CPLVerifyConfiguration(): byte order set wrong.\n" ); 00350 } 00351 00352 00353