<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta name="robots" content="noindex"> <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>cpl_conv.cpp Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body bgcolor="#ffffff"> <!-- Generated by Doxygen 1.2.3-20001105 on Thu Mar 28 09:47:27 2002 --> <center> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> <a class="qindex" href="pages.html">Related Pages</a> </center> <hr><h1>cpl_conv.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/******************************************************************************</font> 00002 <font class="comment"> * $Id: cpl_conv_cpp-source.html,v 1.10 2002/04/16 13:11:47 warmerda Exp $</font> 00003 <font class="comment"> *</font> 00004 <font class="comment"> * Project: CPL - Common Portability Library</font> 00005 <font class="comment"> * Purpose: Convenience functions.</font> 00006 <font class="comment"> * Author: Frank Warmerdam, warmerda@home.com</font> 00007 <font class="comment"> *</font> 00008 <font class="comment"> ******************************************************************************</font> 00009 <font class="comment"> * Copyright (c) 1998, Frank Warmerdam</font> 00010 <font class="comment"> *</font> 00011 <font class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a</font> 00012 <font class="comment"> * copy of this software and associated documentation files (the "Software"),</font> 00013 <font class="comment"> * to deal in the Software without restriction, including without limitation</font> 00014 <font class="comment"> * the rights to use, copy, modify, merge, publish, distribute, sublicense,</font> 00015 <font class="comment"> * and/or sell copies of the Software, and to permit persons to whom the</font> 00016 <font class="comment"> * Software is furnished to do so, subject to the following conditions:</font> 00017 <font class="comment"> *</font> 00018 <font class="comment"> * The above copyright notice and this permission notice shall be included</font> 00019 <font class="comment"> * in all copies or substantial portions of the Software.</font> 00020 <font class="comment"> *</font> 00021 <font class="comment"> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS</font> 00022 <font class="comment"> * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</font> 00023 <font class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL</font> 00024 <font class="comment"> * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</font> 00025 <font class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING</font> 00026 <font class="comment"> * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER</font> 00027 <font class="comment"> * DEALINGS IN THE SOFTWARE.</font> 00028 <font class="comment"> ******************************************************************************</font> 00029 <font class="comment"> *</font> 00030 <font class="comment"> * $Log: cpl_conv_cpp-source.html,v $ 00030 <font class="comment"> * Revision 1.10 2002/04/16 13:11:47 warmerda 00030 <font class="comment"> * updated 00030 <font class="comment"> *</font> 00031 <font class="comment"> * Revision 1.15 2002/03/05 14:26:57 warmerda</font> 00032 <font class="comment"> * expanded tabs</font> 00033 <font class="comment"> *</font> 00034 <font class="comment"> * Revision 1.14 2001/12/12 17:06:57 warmerda</font> 00035 <font class="comment"> * added CPLStat</font> 00036 <font class="comment"> *</font> 00037 <font class="comment"> * Revision 1.13 2001/07/18 04:00:49 warmerda</font> 00038 <font class="comment"> * added CPL_CVSID</font> 00039 <font class="comment"> *</font> 00040 <font class="comment"> * Revision 1.12 2001/03/09 03:19:24 danmo</font> 00041 <font class="comment"> * Set pszRLBuffer=NULL after freeing it to avoid reallocating an invalid ptr</font> 00042 <font class="comment"> *</font> 00043 <font class="comment"> * Revision 1.11 2001/03/05 03:37:19 warmerda</font> 00044 <font class="comment"> * Improve support for recovering CPLReadLine() working buffer.</font> 00045 <font class="comment"> *</font> 00046 <font class="comment"> * Revision 1.10 2001/01/19 21:16:41 warmerda</font> 00047 <font class="comment"> * expanded tabs</font> 00048 <font class="comment"> *</font> 00049 <font class="comment"> * Revision 1.9 2000/04/17 15:56:11 warmerda</font> 00050 <font class="comment"> * make configuration tests always happen</font> 00051 <font class="comment"> *</font> 00052 <font class="comment"> * Revision 1.8 2000/04/05 21:02:47 warmerda</font> 00053 <font class="comment"> * Added CPLVerifyConfiguration()</font> 00054 <font class="comment"> *</font> 00055 <font class="comment"> * Revision 1.7 1999/08/27 12:55:39 danmo</font> 00056 <font class="comment"> * Support 0 bytes allocations in CPLRealloc()</font> 00057 <font class="comment"> *</font> 00058 <font class="comment"> * Revision 1.6 1999/06/25 04:38:03 warmerda</font> 00059 <font class="comment"> * Fixed CPLReadLine() to work for long lines.</font> 00060 <font class="comment"> *</font> 00061 <font class="comment"> * Revision 1.5 1999/05/20 02:54:37 warmerda</font> 00062 <font class="comment"> * Added API documentation</font> 00063 <font class="comment"> *</font> 00064 <font class="comment"> * Revision 1.4 1999/01/02 20:29:53 warmerda</font> 00065 <font class="comment"> * Allow zero length allocations</font> 00066 <font class="comment"> *</font> 00067 <font class="comment"> * Revision 1.3 1998/12/15 19:01:07 warmerda</font> 00068 <font class="comment"> * Added CPLReadLine().</font> 00069 <font class="comment"> *</font> 00070 <font class="comment"> * Revision 1.2 1998/12/03 18:30:04 warmerda</font> 00071 <font class="comment"> * Use CPLError() instead of GPSError().</font> 00072 <font class="comment"> *</font> 00073 <font class="comment"> * Revision 1.1 1998/12/02 19:33:23 warmerda</font> 00074 <font class="comment"> * New</font> 00075 <font class="comment"> *</font> 00076 <font class="comment"> */</font> 00077 00078 <font class="preprocessor">#include "<a class="code" href="cpl_conv_h.html">cpl_conv.h</a>"</font> 00079 00080 CPL_CVSID(<font class="stringliteral">"$Id: cpl_conv_cpp-source.html,v 1.10 2002/04/16 13:11:47 warmerda Exp $"</font>); 00081 00082 <font class="comment">/************************************************************************/</font> 00083 <font class="comment">/* CPLCalloc() */</font> 00084 <font class="comment">/************************************************************************/</font> 00085 00103 <font class="keywordtype">void</font> *<a class="code" href="cpl_conv_h.html#a4">CPLCalloc</a>( size_t nCount, size_t nSize )<font class="keyword"></font> 00104 <font class="keyword"></font> 00105 <font class="keyword"></font>{ 00106 <font class="keywordtype">void</font> *pReturn; 00107 00108 <font class="keywordflow">if</font>( nSize * nCount == 0 ) 00109 <font class="keywordflow">return</font> NULL; 00110 00111 pReturn = VSICalloc( nCount, nSize ); 00112 <font class="keywordflow">if</font>( pReturn == NULL ) 00113 { 00114 <a class="code" href="cpl_error_h.html#a17">CPLError</a>( CE_Fatal, CPLE_OutOfMemory, 00115 <font class="stringliteral">"CPLCalloc(): Out of memory allocating %d bytes.\n"</font>, 00116 nSize * nCount ); 00117 } 00118 00119 <font class="keywordflow">return</font> pReturn; 00120 } 00121 00122 <font class="comment">/************************************************************************/</font> 00123 <font class="comment">/* CPLMalloc() */</font> 00124 <font class="comment">/************************************************************************/</font> 00125 00141 <font class="keywordtype">void</font> *<a class="code" href="cpl_conv_h.html#a3">CPLMalloc</a>( size_t nSize )<font class="keyword"></font> 00142 <font class="keyword"></font> 00143 <font class="keyword"></font>{ 00144 <font class="keywordtype">void</font> *pReturn; 00145 00146 CPLVerifyConfiguration(); 00147 00148 <font class="keywordflow">if</font>( nSize == 0 ) 00149 <font class="keywordflow">return</font> NULL; 00150 00151 pReturn = VSIMalloc( nSize ); 00152 <font class="keywordflow">if</font>( pReturn == NULL ) 00153 { 00154 <a class="code" href="cpl_error_h.html#a17">CPLError</a>( CE_Fatal, CPLE_OutOfMemory, 00155 <font class="stringliteral">"CPLMalloc(): Out of memory allocating %d bytes.\n"</font>, 00156 nSize ); 00157 } 00158 00159 <font class="keywordflow">return</font> pReturn; 00160 } 00161 00162 <font class="comment">/************************************************************************/</font> 00163 <font class="comment">/* CPLRealloc() */</font> 00164 <font class="comment">/************************************************************************/</font> 00165 00186 <font class="keywordtype">void</font> * <a class="code" href="cpl_conv_h.html#a5">CPLRealloc</a>( <font class="keywordtype">void</font> * pData, size_t nNewSize )<font class="keyword"></font> 00187 <font class="keyword"></font> 00188 <font class="keyword"></font>{ 00189 <font class="keywordtype">void</font> *pReturn; 00190 00191 <font class="keywordflow">if</font> ( nNewSize == 0 ) 00192 { 00193 VSIFree(pData); 00194 <font class="keywordflow">return</font> NULL; 00195 } 00196 00197 <font class="keywordflow">if</font>( pData == NULL ) 00198 pReturn = VSIMalloc( nNewSize ); 00199 <font class="keywordflow">else</font> 00200 pReturn = VSIRealloc( pData, nNewSize ); 00201 00202 <font class="keywordflow">if</font>( pReturn == NULL ) 00203 { 00204 <a class="code" href="cpl_error_h.html#a17">CPLError</a>( CE_Fatal, CPLE_OutOfMemory, 00205 <font class="stringliteral">"CPLRealloc(): Out of memory allocating %d bytes.\n"</font>, 00206 nNewSize ); 00207 } 00208 00209 <font class="keywordflow">return</font> pReturn; 00210 } 00211 00212 <font class="comment">/************************************************************************/</font> 00213 <font class="comment">/* CPLStrdup() */</font> 00214 <font class="comment">/************************************************************************/</font> 00215 00234 <font class="keywordtype">char</font> *<a class="code" href="cpl_conv_h.html#a6">CPLStrdup</a>( <font class="keyword">const</font> <font class="keywordtype">char</font> * pszString )<font class="keyword"></font> 00235 <font class="keyword"></font> 00236 <font class="keyword"></font>{ 00237 <font class="keywordtype">char</font> *pszReturn; 00238 00239 <font class="keywordflow">if</font>( pszString == NULL ) 00240 pszString = <font class="stringliteral">""</font>; 00241 00242 pszReturn = VSIStrdup( pszString ); 00243 00244 <font class="keywordflow">if</font>( pszReturn == NULL ) 00245 { 00246 <a class="code" href="cpl_error_h.html#a17">CPLError</a>( CE_Fatal, CPLE_OutOfMemory, 00247 <font class="stringliteral">"CPLStrdup(): Out of memory allocating %d bytes.\n"</font>, 00248 strlen(pszString) ); 00249 00250 } 00251 00252 <font class="keywordflow">return</font>( pszReturn ); 00253 } 00254 00255 <font class="comment">/************************************************************************/</font> 00256 <font class="comment">/* CPLReadLine() */</font> 00257 <font class="comment">/************************************************************************/</font> 00258 00280 <font class="keyword">const</font> <font class="keywordtype">char</font> *<a class="code" href="cpl_conv_h.html#a7">CPLReadLine</a>( FILE * fp )<font class="keyword"></font> 00281 <font class="keyword"></font> 00282 <font class="keyword"></font>{ 00283 <font class="keyword">static</font> <font class="keywordtype">char</font> *pszRLBuffer = NULL; 00284 <font class="keyword">static</font> <font class="keywordtype">int</font> nRLBufferSize = 0; 00285 <font class="keywordtype">int</font> nLength, nReadSoFar = 0; 00286 00287 <font class="comment">/* -------------------------------------------------------------------- */</font> 00288 <font class="comment">/* Cleanup case. */</font> 00289 <font class="comment">/* -------------------------------------------------------------------- */</font> 00290 <font class="keywordflow">if</font>( fp == NULL ) 00291 { 00292 CPLFree( pszRLBuffer ); 00293 pszRLBuffer = NULL; 00294 nRLBufferSize = 0; 00295 <font class="keywordflow">return</font> NULL; 00296 } 00297 00298 <font class="comment">/* -------------------------------------------------------------------- */</font> 00299 <font class="comment">/* Loop reading chunks of the line till we get to the end of */</font> 00300 <font class="comment">/* the line. */</font> 00301 <font class="comment">/* -------------------------------------------------------------------- */</font> 00302 <font class="keywordflow">do</font> { 00303 <font class="comment">/* -------------------------------------------------------------------- */</font> 00304 <font class="comment">/* Grow the working buffer if we have it nearly full. Fail out */</font> 00305 <font class="comment">/* of read line if we can't reallocate it big enough (for */</font> 00306 <font class="comment">/* instance for a _very large_ file with no newlines). */</font> 00307 <font class="comment">/* -------------------------------------------------------------------- */</font> 00308 <font class="keywordflow">if</font>( nRLBufferSize-nReadSoFar < 128 ) 00309 { 00310 nRLBufferSize = nRLBufferSize*2 + 128; 00311 pszRLBuffer = (<font class="keywordtype">char</font> *) VSIRealloc(pszRLBuffer, nRLBufferSize); 00312 <font class="keywordflow">if</font>( pszRLBuffer == NULL ) 00313 { 00314 nRLBufferSize = 0; 00315 <font class="keywordflow">return</font> NULL; 00316 } 00317 } 00318 00319 <font class="comment">/* -------------------------------------------------------------------- */</font> 00320 <font class="comment">/* Do the actual read. */</font> 00321 <font class="comment">/* -------------------------------------------------------------------- */</font> 00322 <font class="keywordflow">if</font>( VSIFGets( pszRLBuffer+nReadSoFar, nRLBufferSize-nReadSoFar, fp ) 00323 == NULL ) 00324 { 00325 CPLFree( pszRLBuffer ); 00326 pszRLBuffer = NULL; 00327 nRLBufferSize = 0; 00328 00329 <font class="keywordflow">return</font> NULL; 00330 } 00331 00332 nReadSoFar = strlen(pszRLBuffer); 00333 00334 } <font class="keywordflow">while</font>( nReadSoFar == nRLBufferSize - 1 00335 && pszRLBuffer[nRLBufferSize-2] != 13 00336 && pszRLBuffer[nRLBufferSize-2] != 10 ); 00337 00338 <font class="comment">/* -------------------------------------------------------------------- */</font> 00339 <font class="comment">/* Clear CR and LF off the end. */</font> 00340 <font class="comment">/* -------------------------------------------------------------------- */</font> 00341 nLength = strlen(pszRLBuffer); 00342 <font class="keywordflow">if</font>( nLength > 0 00343 && (pszRLBuffer[nLength-1] == 10 || pszRLBuffer[nLength-1] == 13) ) 00344 { 00345 pszRLBuffer[--nLength] = <font class="charliteral">'\0'</font>; 00346 } 00347 00348 <font class="keywordflow">if</font>( nLength > 0 00349 && (pszRLBuffer[nLength-1] == 10 || pszRLBuffer[nLength-1] == 13) ) 00350 { 00351 pszRLBuffer[--nLength] = <font class="charliteral">'\0'</font>; 00352 } 00353 00354 <font class="keywordflow">return</font>( pszRLBuffer ); 00355 } 00356 00357 <font class="comment">/************************************************************************/</font> 00358 <font class="comment">/* CPLVerifyConfiguration() */</font> 00359 <font class="comment">/************************************************************************/</font> 00360 00361 <font class="keywordtype">void</font> CPLVerifyConfiguration()<font class="keyword"></font> 00362 <font class="keyword"></font> 00363 <font class="keyword"></font>{ 00364 <font class="comment">/* -------------------------------------------------------------------- */</font> 00365 <font class="comment">/* Verify data types. */</font> 00366 <font class="comment">/* -------------------------------------------------------------------- */</font> 00367 CPLAssert( <font class="keyword">sizeof</font>(GInt32) == 4 ); 00368 CPLAssert( <font class="keyword">sizeof</font>(GInt16) == 2 ); 00369 CPLAssert( <font class="keyword">sizeof</font>(GByte) == 1 ); 00370 00371 <font class="keywordflow">if</font>( <font class="keyword">sizeof</font>(GInt32) != 4 ) 00372 <a class="code" href="cpl_error_h.html#a17">CPLError</a>( CE_Fatal, CPLE_AppDefined, 00373 <font class="stringliteral">"sizeof(GInt32) == %d ... yow!\n"</font>, 00374 <font class="keyword">sizeof</font>(GInt32) ); 00375 00376 <font class="comment">/* -------------------------------------------------------------------- */</font> 00377 <font class="comment">/* Verify byte order */</font> 00378 <font class="comment">/* -------------------------------------------------------------------- */</font> 00379 GInt32 nTest; 00380 00381 nTest = 1; 00382 00383 <font class="preprocessor">#ifdef CPL_LSB</font> 00384 <font class="preprocessor"></font> <font class="keywordflow">if</font>( ((GByte *) &nTest)[0] != 1 ) 00385 <font class="preprocessor">#endif</font> 00386 <font class="preprocessor"></font><font class="preprocessor">#ifdef CPL_MSB</font> 00387 <font class="preprocessor"></font> <font class="keywordflow">if</font>( ((GByte *) &nTest)[3] != 1 ) 00388 <font class="preprocessor">#endif </font> 00389 <font class="preprocessor"></font> <a class="code" href="cpl_error_h.html#a17">CPLError</a>( CE_Fatal, CPLE_AppDefined, 00390 <font class="stringliteral">"CPLVerifyConfiguration(): byte order set wrong.\n"</font> ); 00391 } 00392 00393 <font class="comment">/************************************************************************/</font> 00394 <font class="comment">/* CPLStat() */</font> 00395 <font class="comment">/* */</font> 00396 <font class="comment">/* Same as VSIStat() except it works on "C:" as if it were */</font> 00397 <font class="comment">/* "C:\". */</font> 00398 <font class="comment">/************************************************************************/</font> 00399 00400 <font class="keywordtype">int</font> CPLStat( <font class="keyword">const</font> <font class="keywordtype">char</font> *pszPath, VSIStatBuf *psStatBuf )<font class="keyword"></font> 00401 <font class="keyword"></font> 00402 <font class="keyword"></font>{ 00403 <font class="keywordflow">if</font>( strlen(pszPath) == 2 && pszPath[1] == <font class="charliteral">':'</font> ) 00404 { 00405 <font class="keywordtype">char</font> szAltPath[10]; 00406 00407 strcpy( szAltPath, pszPath ); 00408 strcat( szAltPath, <font class="stringliteral">"\\"</font> ); 00409 <font class="keywordflow">return</font> VSIStat( szAltPath, psStatBuf ); 00410 } 00411 <font class="keywordflow">else</font> 00412 <font class="keywordflow">return</font> VSIStat( pszPath, psStatBuf ); 00413 } </div></pre><hr><address><small>Generated at Thu Mar 28 09:47:27 2002 for GDAL by <a href="http://www.stack.nl/~dimitri/doxygen/index.html"> <img src="doxygen.gif" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.3-20001105 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>, © 1997-2000</small></address> </body> </html>