📄 cpl_conv.cpp
字号:
/****************************************************************************** * $Id: cpl_conv.cpp,v 1.59 2006/12/18 17:49:04 dreamil Exp $ * * Project: CPL - Common Portability Library * Purpose: Convenience functions. * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 1998, Frank Warmerdam * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. ****************************************************************************** * * $Log: cpl_conv.cpp,v $ * Revision 1.59 2006/12/18 17:49:04 dreamil * Modified CPLReadLineL() to fix * http://bugzilla.remotesensing.org/show_bug.cgi?id=1374 * * Revision 1.58 2006/11/18 20:58:23 mloskot * Removed WIN32CE ifdef around CPLLocaleC. * * Revision 1.57 2006/11/16 14:58:33 mloskot * Added cast from __int64 to GUIntBig on Windows. * * Revision 1.56 2006/10/11 08:30:01 dron * Added CPLStrlwr() function. * * Revision 1.55 2006/08/23 15:09:46 fwarmerdam * Added --mempreload support, and VSICopyFile * * Revision 1.54 2006/06/30 18:18:17 dron * Avoid warnings. * * Revision 1.53 2006/06/30 14:55:10 dron * Avoid some warnings on win/64. * * Revision 1.52 2006/03/04 00:46:43 mloskot * [WCE] CPLLocaleC class excluded from compilation for Windows CE * * Revision 1.51 2006/03/03 19:40:53 fwarmerdam * added CPLLocaleC class * * Revision 1.50 2006/02/19 21:54:34 mloskot * [WINCE] Changes related to Windows CE port of CPL. Most changes are #ifdef wrappers. * * Revision 1.49 2006/02/09 05:54:15 fwarmerdam * use ll in format for long long on mac, not L * * Revision 1.48 2005/11/24 19:22:54 fwarmerdam * Added silly-value checks in CPLMalloc/Realloc. * * Revision 1.47 2005/10/17 23:33:51 fwarmerdam * CPLFGets() - be very wary about reads ending in the middle of CRLF. * Was causing problems with world.mif/world.mid reading. * * Revision 1.46 2005/09/11 21:08:59 fwarmerdam * properly manage TLS ReadLine buffer, added CPLReadLineL() * * Revision 1.45 2005/07/11 16:41:31 fwarmerdam * Protect config list and shared file list with mutex. * * Revision 1.44 2005/05/23 03:57:57 fwarmerdam * make make static buffers threadlocal * * Revision 1.43 2005/04/04 15:23:30 fwarmerdam * some functions now CPL_STDCALL * * Revision 1.42 2004/11/17 22:57:21 fwarmerdam * added CPLScanPointer() and CPLPrintPointer() * * Revision 1.41 2004/08/16 20:24:07 warmerda * added CPLUnlinkTree * * Revision 1.40 2004/07/31 04:51:36 warmerda * added shared file open support * * Revision 1.39 2004/06/01 20:40:02 warmerda * expanded tabs * * Revision 1.38 2004/03/28 16:22:02 warmerda * const correctness changes in scan functions * * Revision 1.37 2004/03/24 09:01:17 dron * Added CPLPrintUIntBig(). * * Revision 1.36 2004/02/25 09:02:25 dron * Fixed bug in CPLPackedDMSToDec(). * * Revision 1.35 2004/02/21 10:08:54 dron * Fixes in various string handling functions. * * Revision 1.34 2004/02/07 14:03:30 dron * CPLDecToPackedDMS() added. * * Revision 1.33 2004/02/01 08:37:55 dron * Added CPLPackedDMSToDec(). * * Revision 1.32 2003/12/28 17:24:43 warmerda * added CPLFreeConfig * * Revision 1.31 2003/10/17 07:06:06 dron * Added locale selection option to CPLScanDouble() and CPLPrintDOuble(). * * Revision 1.30 2003/09/28 14:14:16 dron * Added CPLScanString(). * * Revision 1.29 2003/09/12 20:49:24 warmerda * reimplement CPLFGets() to avoid textmode problems * * Revision 1.28 2003/09/08 12:54:42 dron * Fixed warnings. * * Revision 1.27 2003/09/08 11:09:53 dron * Added CPLPrintDouble() and CPLPrintTime(). * * Revision 1.26 2003/09/07 14:38:43 dron * Added CPLPrintString(), CPLPrintStringFill(), CPLPrintInt32(), CPLPrintUIntBig(). * * Revision 1.25 2003/09/03 13:07:26 warmerda * Cleaned up CPLScanLong() a bit to avoid warnings, and * unnecessary conversion to/from double. * * Revision 1.24 2003/08/31 14:48:05 dron * Added CPLScanLong() and CPLScanDouble(). * * Revision 1.23 2003/08/25 20:01:58 dron * Added CPLFGets() helper function. * * Revision 1.22 2003/05/08 21:51:14 warmerda * added CPL{G,S}etConfigOption() usage * * Revision 1.21 2003/03/05 16:46:54 warmerda * Cast strchr() result for Sun (patch from Graeme). * * Revision 1.20 2003/03/02 04:44:38 warmerda * added CPLStringToComplex * * Revision 1.19 2003/02/14 22:12:07 warmerda * expand tabs * * Revision 1.18 2002/12/18 20:22:53 warmerda * fiddle with roundoff issues in DecToDMS * * Revision 1.17 2002/12/10 19:46:04 warmerda * modified CPLReadLine() to seek back if it overreads past a CR or LF * * Revision 1.16 2002/12/09 18:52:51 warmerda * added DMS conversion * * Revision 1.15 2002/03/05 14:26:57 warmerda * expanded tabs * * Revision 1.14 2001/12/12 17:06:57 warmerda * added CPLStat * * Revision 1.13 2001/07/18 04:00:49 warmerda * added CPL_CVSID * * Revision 1.12 2001/03/09 03:19:24 danmo * Set pszRLBuffer=NULL after freeing it to avoid reallocating an invalid ptr * * Revision 1.11 2001/03/05 03:37:19 warmerda * Improve support for recovering CPLReadLine() working buffer. * * Revision 1.10 2001/01/19 21:16:41 warmerda * expanded tabs * * Revision 1.9 2000/04/17 15:56:11 warmerda * make configuration tests always happen * * Revision 1.8 2000/04/05 21:02:47 warmerda * Added CPLVerifyConfiguration() * * Revision 1.7 1999/08/27 12:55:39 danmo * Support 0 bytes allocations in CPLRealloc() * * Revision 1.6 1999/06/25 04:38:03 warmerda * Fixed CPLReadLine() to work for long lines. * * Revision 1.5 1999/05/20 02:54:37 warmerda * Added API documentation * * Revision 1.4 1999/01/02 20:29:53 warmerda * Allow zero length allocations * * Revision 1.3 1998/12/15 19:01:07 warmerda * Added CPLReadLine(). * * Revision 1.2 1998/12/03 18:30:04 warmerda * Use CPLError() instead of GPSError(). * * Revision 1.1 1998/12/02 19:33:23 warmerda * New * */#include "cpl_conv.h"#include "cpl_string.h"#include "cpl_vsi.h"#include "cpl_multiproc.h"CPL_CVSID("$Id: cpl_conv.cpp,v 1.59 2006/12/18 17:49:04 dreamil Exp $");#if defined(WIN32CE)# include "cpl_wince.h"# include <wce_errno.h>#endifstatic void *hConfigMutex = NULL;static volatile char **papszConfigOptions = NULL;static void *hSharedFileMutex = NULL;static volatile int nSharedFileCount = 0;static volatile CPLSharedFileInfo *pasSharedFileList = NULL;/************************************************************************//* CPLCalloc() *//************************************************************************//** * Safe version of calloc(). * * This function is like the C library calloc(), but raises a CE_Fatal * error with CPLError() if it fails to allocate the desired memory. It * should be used for small memory allocations that are unlikely to fail * and for which the application is unwilling to test for out of memory * conditions. It uses VSICalloc() to get the memory, so any hooking of * VSICalloc() will apply to CPLCalloc() as well. CPLFree() or VSIFree() * can be used free memory allocated by CPLCalloc(). * * @param nCount number of objects to allocate. * @param nSize size (in bytes) of object to allocate. * @return pointer to newly allocated memory, only NULL if nSize * nCount is * NULL. */void *CPLCalloc( size_t nCount, size_t nSize ){ void *pReturn; if( nSize * nCount == 0 ) return NULL; pReturn = VSICalloc( nCount, nSize ); if( pReturn == NULL ) { CPLError( CE_Fatal, CPLE_OutOfMemory, "CPLCalloc(): Out of memory allocating %d bytes.\n", nSize * nCount ); } return pReturn;}/************************************************************************//* CPLMalloc() *//************************************************************************//** * Safe version of malloc(). * * This function is like the C library malloc(), but raises a CE_Fatal * error with CPLError() if it fails to allocate the desired memory. It * should be used for small memory allocations that are unlikely to fail * and for which the application is unwilling to test for out of memory * conditions. It uses VSIMalloc() to get the memory, so any hooking of * VSIMalloc() will apply to CPLMalloc() as well. CPLFree() or VSIFree() * can be used free memory allocated by CPLMalloc(). * * @param nSize size (in bytes) of memory block to allocate. * @return pointer to newly allocated memory, only NULL if nSize is zero. */void *CPLMalloc( size_t nSize ){ void *pReturn; CPLVerifyConfiguration(); if( nSize == 0 ) return NULL; if( nSize < 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "CPLMalloc(%d): Silly size requested.\n", nSize ); return NULL; } pReturn = VSIMalloc( nSize ); if( pReturn == NULL ) { CPLError( CE_Fatal, CPLE_OutOfMemory, "CPLMalloc(): Out of memory allocating %d bytes.\n", nSize ); } return pReturn;}/************************************************************************//* CPLRealloc() *//************************************************************************//** * Safe version of realloc(). * * This function is like the C library realloc(), but raises a CE_Fatal * error with CPLError() if it fails to allocate the desired memory. It * should be used for small memory allocations that are unlikely to fail * and for which the application is unwilling to test for out of memory * conditions. It uses VSIRealloc() to get the memory, so any hooking of * VSIRealloc() will apply to CPLRealloc() as well. CPLFree() or VSIFree() * can be used free memory allocated by CPLRealloc(). * * It is also safe to pass NULL in as the existing memory block for * CPLRealloc(), in which case it uses VSIMalloc() to allocate a new block. * * @param pData existing memory block which should be copied to the new block. * @param nNewSize new size (in bytes) of memory block to allocate. * @return pointer to allocated memory, only NULL if nNewSize is zero. */void * CPLRealloc( void * pData, size_t nNewSize ){ void *pReturn; if ( nNewSize == 0 ) { VSIFree(pData); return NULL; } if( nNewSize < 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "CPLRealloc(%d): Silly size requested.\n", nNewSize ); return NULL; } if( pData == NULL ) pReturn = VSIMalloc( nNewSize ); else pReturn = VSIRealloc( pData, nNewSize ); if( pReturn == NULL ) { CPLError( CE_Fatal, CPLE_OutOfMemory, "CPLRealloc(): Out of memory allocating %d bytes.\n", nNewSize ); } return pReturn;}/************************************************************************//* CPLStrdup() *//************************************************************************//** * Safe version of strdup() function. * * This function is similar to the C library strdup() function, but if * the memory allocation fails it will issue a CE_Fatal error with * CPLError() instead of returning NULL. It uses VSIStrdup(), so any * hooking of that function will apply to CPLStrdup() as well. Memory * allocated with CPLStrdup() can be freed with CPLFree() or VSIFree(). * * It is also safe to pass a NULL string into CPLStrdup(). CPLStrdup() * will allocate and return a zero length string (as opposed to a NULL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -