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.10 2002/04/16 13:11:47 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.10  2002/04/16 13:11:47  warmerda
00030  * updated
00030  *
00031  * Revision 1.15  2002/03/05 14:26:57  warmerda
00032  * expanded tabs
00033  *
00034  * Revision 1.14  2001/12/12 17:06:57  warmerda
00035  * added CPLStat
00036  *
00037  * Revision 1.13  2001/07/18 04:00:49  warmerda
00038  * added CPL_CVSID
00039  *
00040  * Revision 1.12  2001/03/09 03:19:24  danmo
00041  * Set pszRLBuffer=NULL after freeing it to avoid reallocating an invalid ptr
00042  *
00043  * Revision 1.11  2001/03/05 03:37:19  warmerda
00044  * Improve support for recovering CPLReadLine() working buffer.
00045  *
00046  * Revision 1.10  2001/01/19 21:16:41  warmerda
00047  * expanded tabs
00048  *
00049  * Revision 1.9  2000/04/17 15:56:11  warmerda
00050  * make configuration tests always happen
00051  *
00052  * Revision 1.8  2000/04/05 21:02:47  warmerda
00053  * Added CPLVerifyConfiguration()
00054  *
00055  * Revision 1.7  1999/08/27 12:55:39  danmo
00056  * Support 0 bytes allocations in CPLRealloc()
00057  *
00058  * Revision 1.6  1999/06/25 04:38:03  warmerda
00059  * Fixed CPLReadLine() to work for long lines.
00060  *
00061  * Revision 1.5  1999/05/20 02:54:37  warmerda
00062  * Added API documentation
00063  *
00064  * Revision 1.4  1999/01/02 20:29:53  warmerda
00065  * Allow zero length allocations
00066  *
00067  * Revision 1.3  1998/12/15 19:01:07  warmerda
00068  * Added CPLReadLine().
00069  *
00070  * Revision 1.2  1998/12/03 18:30:04  warmerda
00071  * Use CPLError() instead of GPSError().
00072  *
00073  * Revision 1.1  1998/12/02 19:33:23  warmerda
00074  * New
00075  *
00076  */
00077 
00078 #include "cpl_conv.h"
00079 
00080 CPL_CVSID("$Id: cpl_conv_cpp-source.html,v 1.10 2002/04/16 13:11:47 warmerda Exp $");
00081 
00082 /************************************************************************/
00083 /*                             CPLCalloc()                              */
00084 /************************************************************************/
00085 
00103 void *CPLCalloc( size_t nCount, size_t nSize )
00104 
00105 {
00106     void        *pReturn;
00107 
00108     if( nSize * nCount == 0 )
00109         return NULL;
00110     
00111     pReturn = VSICalloc( nCount, nSize );
00112     if( pReturn == NULL )
00113     {
00114         CPLError( CE_Fatal, CPLE_OutOfMemory,
00115                   "CPLCalloc(): Out of memory allocating %d bytes.\n",
00116                   nSize * nCount );
00117     }
00118 
00119     return pReturn;
00120 }
00121 
00122 /************************************************************************/
00123 /*                             CPLMalloc()                              */
00124 /************************************************************************/
00125 
00141 void *CPLMalloc( size_t nSize )
00142 
00143 {
00144     void        *pReturn;
00145 
00146     CPLVerifyConfiguration();
00147 
00148     if( nSize == 0 )
00149         return NULL;
00150     
00151     pReturn = VSIMalloc( nSize );
00152     if( pReturn == NULL )
00153     {
00154         CPLError( CE_Fatal, CPLE_OutOfMemory,
00155                   "CPLMalloc(): Out of memory allocating %d bytes.\n",
00156                   nSize );
00157     }
00158 
00159     return pReturn;
00160 }
00161 
00162 /************************************************************************/
00163 /*                             CPLRealloc()                             */
00164 /************************************************************************/
00165 
00186 void * CPLRealloc( void * pData, size_t nNewSize )
00187 
00188 {
00189     void        *pReturn;
00190 
00191     if ( nNewSize == 0 )
00192     {
00193         VSIFree(pData);
00194         return NULL;
00195     }
00196 
00197     if( pData == NULL )
00198         pReturn = VSIMalloc( nNewSize );
00199     else
00200         pReturn = VSIRealloc( pData, nNewSize );
00201     
00202     if( pReturn == NULL )
00203     {
00204         CPLError( CE_Fatal, CPLE_OutOfMemory,
00205                   "CPLRealloc(): Out of memory allocating %d bytes.\n",
00206                   nNewSize );
00207     }
00208 
00209     return pReturn;
00210 }
00211 
00212 /************************************************************************/
00213 /*                             CPLStrdup()                              */
00214 /************************************************************************/
00215 
00234 char *CPLStrdup( const char * pszString )
00235 
00236 {
00237     char        *pszReturn;
00238 
00239     if( pszString == NULL )
00240         pszString = "";
00241 
00242     pszReturn = VSIStrdup( pszString );
00243         
00244     if( pszReturn == NULL )
00245     {
00246         CPLError( CE_Fatal, CPLE_OutOfMemory,
00247                   "CPLStrdup(): Out of memory allocating %d bytes.\n",
00248                   strlen(pszString) );
00249         
00250     }
00251     
00252     return( pszReturn );
00253 }
00254 
00255 /************************************************************************/
00256 /*                            CPLReadLine()                             */
00257 /************************************************************************/
00258 
00280 const char *CPLReadLine( FILE * fp )
00281 
00282 {
00283     static char *pszRLBuffer = NULL;
00284     static int  nRLBufferSize = 0;
00285     int         nLength, nReadSoFar = 0;
00286 
00287 /* -------------------------------------------------------------------- */
00288 /*      Cleanup case.                                                   */
00289 /* -------------------------------------------------------------------- */
00290     if( fp == NULL )
00291     {
00292         CPLFree( pszRLBuffer );
00293         pszRLBuffer = NULL;
00294         nRLBufferSize = 0;
00295         return NULL;
00296     }
00297 
00298 /* -------------------------------------------------------------------- */
00299 /*      Loop reading chunks of the line till we get to the end of       */
00300 /*      the line.                                                       */
00301 /* -------------------------------------------------------------------- */
00302     do {
00303 /* -------------------------------------------------------------------- */
00304 /*      Grow the working buffer if we have it nearly full.  Fail out    */
00305 /*      of read line if we can't reallocate it big enough (for          */
00306 /*      instance for a _very large_ file with no newlines).             */
00307 /* -------------------------------------------------------------------- */
00308         if( nRLBufferSize-nReadSoFar < 128 )
00309         {
00310             nRLBufferSize = nRLBufferSize*2 + 128;
00311             pszRLBuffer = (char *) VSIRealloc(pszRLBuffer, nRLBufferSize);
00312             if( pszRLBuffer == NULL )
00313             {
00314                 nRLBufferSize = 0;
00315                 return NULL;
00316             }
00317         }
00318 
00319 /* -------------------------------------------------------------------- */
00320 /*      Do the actual read.                                             */
00321 /* -------------------------------------------------------------------- */
00322         if( VSIFGets( pszRLBuffer+nReadSoFar, nRLBufferSize-nReadSoFar, fp )
00323             == NULL )
00324         {
00325             CPLFree( pszRLBuffer );
00326             pszRLBuffer = NULL;
00327             nRLBufferSize = 0;
00328 
00329             return NULL;
00330         }
00331 
00332         nReadSoFar = strlen(pszRLBuffer);
00333 
00334     } while( nReadSoFar == nRLBufferSize - 1
00335              && pszRLBuffer[nRLBufferSize-2] != 13
00336              && pszRLBuffer[nRLBufferSize-2] != 10 );
00337 
00338 /* -------------------------------------------------------------------- */
00339 /*      Clear CR and LF off the end.                                    */
00340 /* -------------------------------------------------------------------- */
00341     nLength = strlen(pszRLBuffer);
00342     if( nLength > 0
00343         && (pszRLBuffer[nLength-1] == 10 || pszRLBuffer[nLength-1] == 13) )
00344     {
00345         pszRLBuffer[--nLength] = '\0';
00346     }
00347     
00348     if( nLength > 0
00349         && (pszRLBuffer[nLength-1] == 10 || pszRLBuffer[nLength-1] == 13) )
00350     {
00351         pszRLBuffer[--nLength] = '\0';
00352     }
00353 
00354     return( pszRLBuffer );
00355 }
00356 
00357 /************************************************************************/
00358 /*                       CPLVerifyConfiguration()                       */
00359 /************************************************************************/
00360 
00361 void CPLVerifyConfiguration()
00362 
00363 {
00364 /* -------------------------------------------------------------------- */
00365 /*      Verify data types.                                              */
00366 /* -------------------------------------------------------------------- */
00367     CPLAssert( sizeof(GInt32) == 4 );
00368     CPLAssert( sizeof(GInt16) == 2 );
00369     CPLAssert( sizeof(GByte) == 1 );
00370 
00371     if( sizeof(GInt32) != 4 )
00372         CPLError( CE_Fatal, CPLE_AppDefined, 
00373                   "sizeof(GInt32) == %d ... yow!\n", 
00374                   sizeof(GInt32) );
00375 
00376 /* -------------------------------------------------------------------- */
00377 /*      Verify byte order                                               */
00378 /* -------------------------------------------------------------------- */
00379     GInt32   nTest;
00380 
00381     nTest = 1;
00382 
00383 #ifdef CPL_LSB
00384     if( ((GByte *) &nTest)[0] != 1 )
00385 #endif
00386 #ifdef CPL_MSB
00387     if( ((GByte *) &nTest)[3] != 1 )
00388 #endif    
00389         CPLError( CE_Fatal, CPLE_AppDefined, 
00390                   "CPLVerifyConfiguration(): byte order set wrong.\n" );
00391 }
00392 
00393 /************************************************************************/
00394 /*                              CPLStat()                               */
00395 /*                                                                      */
00396 /*      Same as VSIStat() except it works on "C:" as if it were         */
00397 /*      "C:\".                                                          */
00398 /************************************************************************/
00399 
00400 int CPLStat( const char *pszPath, VSIStatBuf *psStatBuf )
00401 
00402 {
00403     if( strlen(pszPath) == 2 && pszPath[1] == ':' )
00404     {
00405         char    szAltPath[10];
00406         
00407         strcpy( szAltPath, pszPath );
00408         strcat( szAltPath, "\\" );
00409         return VSIStat( szAltPath, psStatBuf );
00410     }
00411     else
00412         return VSIStat( pszPath, psStatBuf );
00413 }

Generated at Thu Mar 28 09:47:27 2002 for GDAL by doxygen1.2.3-20001105 written by Dimitri van Heesch, © 1997-2000