Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

cpl_conv.cpp

00001 /******************************************************************************
00002  * $Id: cpl_conv_cpp-source.html,v 1.8 2001/07/05 13:24:08 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.8  2001/07/05 13:24:08  warmerda
00030  * *** empty log message ***
00030  *
00031  * Revision 1.12  2001/03/09 03:19:24  danmo
00032  * Set pszRLBuffer=NULL after freeing it to avoid reallocating an invalid ptr
00033  *
00034  * Revision 1.11  2001/03/05 03:37:19  warmerda
00035  * Improve support for recovering CPLReadLine() working buffer.
00036  *
00037  * Revision 1.10  2001/01/19 21:16:41  warmerda
00038  * expanded tabs
00039  *
00040  * Revision 1.9  2000/04/17 15:56:11  warmerda
00041  * make configuration tests always happen
00042  *
00043  * Revision 1.8  2000/04/05 21:02:47  warmerda
00044  * Added CPLVerifyConfiguration()
00045  *
00046  * Revision 1.7  1999/08/27 12:55:39  danmo
00047  * Support 0 bytes allocations in CPLRealloc()
00048  *
00049  * Revision 1.6  1999/06/25 04:38:03  warmerda
00050  * Fixed CPLReadLine() to work for long lines.
00051  *
00052  * Revision 1.5  1999/05/20 02:54:37  warmerda
00053  * Added API documentation
00054  *
00055  * Revision 1.4  1999/01/02 20:29:53  warmerda
00056  * Allow zero length allocations
00057  *
00058  * Revision 1.3  1998/12/15 19:01:07  warmerda
00059  * Added CPLReadLine().
00060  *
00061  * Revision 1.2  1998/12/03 18:30:04  warmerda
00062  * Use CPLError() instead of GPSError().
00063  *
00064  * Revision 1.1  1998/12/02 19:33:23  warmerda
00065  * New
00066  *
00067  */
00068 
00069 #include "cpl_conv.h"
00070 
00071 /************************************************************************/
00072 /*                             CPLCalloc()                              */
00073 /************************************************************************/
00074 
00092 void *CPLCalloc( size_t nCount, size_t nSize )
00093 
00094 {
00095     void        *pReturn;
00096 
00097     if( nSize * nCount == 0 )
00098         return NULL;
00099     
00100     pReturn = VSICalloc( nCount, nSize );
00101     if( pReturn == NULL )
00102     {
00103         CPLError( CE_Fatal, CPLE_OutOfMemory,
00104                   "CPLCalloc(): Out of memory allocating %d bytes.\n",
00105                   nSize * nCount );
00106     }
00107 
00108     return pReturn;
00109 }
00110 
00111 /************************************************************************/
00112 /*                             CPLMalloc()                              */
00113 /************************************************************************/
00114 
00130 void *CPLMalloc( size_t nSize )
00131 
00132 {
00133     void        *pReturn;
00134 
00135     CPLVerifyConfiguration();
00136 
00137     if( nSize == 0 )
00138         return NULL;
00139     
00140     pReturn = VSIMalloc( nSize );
00141     if( pReturn == NULL )
00142     {
00143         CPLError( CE_Fatal, CPLE_OutOfMemory,
00144                   "CPLMalloc(): Out of memory allocating %d bytes.\n",
00145                   nSize );
00146     }
00147 
00148     return pReturn;
00149 }
00150 
00151 /************************************************************************/
00152 /*                             CPLRealloc()                             */
00153 /************************************************************************/
00154 
00175 void * CPLRealloc( void * pData, size_t nNewSize )
00176 
00177 {
00178     void        *pReturn;
00179 
00180     if ( nNewSize == 0 )
00181     {
00182         VSIFree(pData);
00183         return NULL;
00184     }
00185 
00186     if( pData == NULL )
00187         pReturn = VSIMalloc( nNewSize );
00188     else
00189         pReturn = VSIRealloc( pData, nNewSize );
00190     
00191     if( pReturn == NULL )
00192     {
00193         CPLError( CE_Fatal, CPLE_OutOfMemory,
00194                   "CPLRealloc(): Out of memory allocating %d bytes.\n",
00195                   nNewSize );
00196     }
00197 
00198     return pReturn;
00199 }
00200 
00201 /************************************************************************/
00202 /*                             CPLStrdup()                              */
00203 /************************************************************************/
00204 
00223 char *CPLStrdup( const char * pszString )
00224 
00225 {
00226     char        *pszReturn;
00227 
00228     if( pszString == NULL )
00229         pszString = "";
00230 
00231     pszReturn = VSIStrdup( pszString );
00232         
00233     if( pszReturn == NULL )
00234     {
00235         CPLError( CE_Fatal, CPLE_OutOfMemory,
00236                   "CPLStrdup(): Out of memory allocating %d bytes.\n",
00237                   strlen(pszString) );
00238         
00239     }
00240     
00241     return( pszReturn );
00242 }
00243 
00244 /************************************************************************/
00245 /*                            CPLReadLine()                             */
00246 /************************************************************************/
00247 
00269 const char *CPLReadLine( FILE * fp )
00270 
00271 {
00272     static char *pszRLBuffer = NULL;
00273     static int  nRLBufferSize = 0;
00274     int         nLength, nReadSoFar = 0;
00275 
00276 /* -------------------------------------------------------------------- */
00277 /*      Cleanup case.                                                   */
00278 /* -------------------------------------------------------------------- */
00279     if( fp == NULL )
00280     {
00281         CPLFree( pszRLBuffer );
00282         pszRLBuffer = NULL;
00283         nRLBufferSize = 0;
00284         return NULL;
00285     }
00286 
00287 /* -------------------------------------------------------------------- */
00288 /*      Loop reading chunks of the line till we get to the end of       */
00289 /*      the line.                                                       */
00290 /* -------------------------------------------------------------------- */
00291     do {
00292 /* -------------------------------------------------------------------- */
00293 /*      Grow the working buffer if we have it nearly full.  Fail out    */
00294 /*      of read line if we can't reallocate it big enough (for          */
00295 /*      instance for a _very large_ file with no newlines).             */
00296 /* -------------------------------------------------------------------- */
00297         if( nRLBufferSize-nReadSoFar < 128 )
00298         {
00299             nRLBufferSize = nRLBufferSize*2 + 128;
00300             pszRLBuffer = (char *) VSIRealloc(pszRLBuffer, nRLBufferSize);
00301             if( pszRLBuffer == NULL )
00302             {
00303                 nRLBufferSize = 0;
00304                 return NULL;
00305             }
00306         }
00307 
00308 /* -------------------------------------------------------------------- */
00309 /*      Do the actual read.                                             */
00310 /* -------------------------------------------------------------------- */
00311         if( VSIFGets( pszRLBuffer+nReadSoFar, nRLBufferSize-nReadSoFar, fp )
00312             == NULL )
00313         {
00314             CPLFree( pszRLBuffer );
00315             pszRLBuffer = NULL;
00316             nRLBufferSize = 0;
00317 
00318             return NULL;
00319         }
00320 
00321         nReadSoFar = strlen(pszRLBuffer);
00322 
00323     } while( nReadSoFar == nRLBufferSize - 1
00324              && pszRLBuffer[nRLBufferSize-2] != 13
00325              && pszRLBuffer[nRLBufferSize-2] != 10 );
00326 
00327 /* -------------------------------------------------------------------- */
00328 /*      Clear CR and LF off the end.                                    */
00329 /* -------------------------------------------------------------------- */
00330     nLength = strlen(pszRLBuffer);
00331     if( nLength > 0
00332         && (pszRLBuffer[nLength-1] == 10 || pszRLBuffer[nLength-1] == 13) )
00333     {
00334         pszRLBuffer[--nLength] = '\0';
00335     }
00336     
00337     if( nLength > 0
00338         && (pszRLBuffer[nLength-1] == 10 || pszRLBuffer[nLength-1] == 13) )
00339     {
00340         pszRLBuffer[--nLength] = '\0';
00341     }
00342 
00343     return( pszRLBuffer );
00344 }
00345 
00346 /************************************************************************/
00347 /*                       CPLVerifyConfiguration()                       */
00348 /************************************************************************/
00349 
00350 void CPLVerifyConfiguration()
00351 
00352 {
00353 /* -------------------------------------------------------------------- */
00354 /*      Verify data types.                                              */
00355 /* -------------------------------------------------------------------- */
00356     CPLAssert( sizeof(GInt32) == 4 );
00357     CPLAssert( sizeof(GInt16) == 2 );
00358     CPLAssert( sizeof(GByte) == 1 );
00359 
00360     if( sizeof(GInt32) != 4 )
00361         CPLError( CE_Fatal, CPLE_AppDefined, 
00362                   "sizeof(GInt32) == %d ... yow!\n", 
00363                   sizeof(GInt32) );
00364 
00365 /* -------------------------------------------------------------------- */
00366 /*      Verify byte order                                               */
00367 /* -------------------------------------------------------------------- */
00368     GInt32   nTest;
00369 
00370     nTest = 1;
00371 
00372 #ifdef CPL_LSB
00373     if( ((GByte *) &nTest)[0] != 1 )
00374 #endif
00375 #ifdef CPL_MSB
00376     if( ((GByte *) &nTest)[3] != 1 )
00377 #endif    
00378         CPLError( CE_Fatal, CPLE_AppDefined, 
00379                   "CPLVerifyConfiguration(): byte order set wrong.\n" );
00380 }
00381 
00382 
00383 

Generated at Thu Jul 5 09:16:11 2001 for GDAL by doxygen1.2.3-20001105 written by Dimitri van Heesch, © 1997-2000