1
1
/******************************************************************************
2
* $Id: cpl_conv.cpp 18060 2009-11-21 20:26:25Z rouault $
2
* $Id: cpl_conv.cpp 23504 2011-12-09 20:43:50Z rouault $
4
4
* Project: CPL - Common Portability Library
5
5
* Purpose: Convenience functions.
77
77
if( nSize * nCount == 0 )
80
pReturn = VSICalloc( nCount, nSize );
83
CPLError( CE_Fatal, CPLE_OutOfMemory,
84
"CPLCalloc(): Out of memory allocating %ld bytes.\n",
85
(long) (nSize * nCount) );
80
pReturn = CPLMalloc( nCount * nSize );
81
memset( pReturn, 0, nCount * nSize );
128
122
pReturn = VSIMalloc( nSize );
129
123
if( pReturn == NULL )
131
CPLError( CE_Fatal, CPLE_OutOfMemory,
132
"CPLMalloc(): Out of memory allocating %ld bytes.\n",
125
if( nSize > 0 && nSize < 2000 )
130
"CPLMalloc(): Out of memory allocating %ld bytes.",
132
CPLEmergencyError( szSmallMsg );
135
CPLError( CE_Fatal, CPLE_OutOfMemory,
136
"CPLMalloc(): Out of memory allocating %ld bytes.\n",
187
191
if( pReturn == NULL )
189
CPLError( CE_Fatal, CPLE_OutOfMemory,
190
"CPLRealloc(): Out of memory allocating %ld bytes.\n",
193
if( nNewSize > 0 && nNewSize < 2000 )
198
"CPLRealloc(): Out of memory allocating %ld bytes.",
200
CPLEmergencyError( szSmallMsg );
203
CPLError( CE_Fatal, CPLE_OutOfMemory,
204
"CPLRealloc(): Out of memory allocating %ld bytes.\n",
394
408
/* Fetch readline buffer, and ensure it is the desired size, */
395
409
/* reallocating if needed. Manages TLS (thread local storage) */
396
410
/* issues for the buffer. */
411
/* We use a special trick to track the actual size of the buffer */
412
/* The first 4 bytes are reserved to store it as a int, hence the */
413
/* -4 / +4 hacks with the size and pointer. */
397
414
/************************************************************************/
398
415
static char *CPLReadLineBuffer( int nRequiredSize )
427
444
/* -------------------------------------------------------------------- */
428
445
/* If it is too small, grow it bigger. */
429
446
/* -------------------------------------------------------------------- */
430
if( (int) *pnAlloc < nRequiredSize+1 )
447
if( ((int) *pnAlloc) -1 < nRequiredSize )
432
449
int nNewSize = nRequiredSize + 4 + 500;
453
CPLSetTLS( CTLS_RLBUFFERINFO, NULL, FALSE );
454
CPLError( CE_Failure, CPLE_OutOfMemory,
455
"CPLReadLineBuffer(): Trying to allocate more than 2 GB." );
434
459
GUInt32* pnAllocNew = (GUInt32 *) VSIRealloc(pnAlloc,nNewSize);
435
460
if( pnAllocNew == NULL )
437
462
VSIFree( pnAlloc );
438
463
CPLSetTLS( CTLS_RLBUFFERINFO, NULL, FALSE );
464
CPLError( CE_Failure, CPLE_OutOfMemory,
465
"CPLReadLineBuffer(): Out of memory allocating %ld bytes.",
441
469
pnAlloc = pnAllocNew;
536
564
* from the file or NULL if the end of file was encountered.
539
const char *CPLReadLineL( FILE * fp )
567
const char *CPLReadLineL( VSILFILE * fp )
541
569
return CPLReadLine2L( fp, -1, NULL );
561
589
* @since GDAL 1.7.0
564
const char *CPLReadLine2L( FILE * fp, int nMaxCars, char** papszOptions )
592
const char *CPLReadLine2L( VSILFILE * fp, int nMaxCars, char** papszOptions )
567
597
/* -------------------------------------------------------------------- */
568
598
/* Cleanup case. */
569
599
/* -------------------------------------------------------------------- */
589
619
/* -------------------------------------------------------------------- */
590
620
/* Read a chunk from the input file. */
591
621
/* -------------------------------------------------------------------- */
622
if ( nBufLength > INT_MAX - nChunkSize - 1 )
624
CPLError( CE_Failure, CPLE_AppDefined,
625
"Too big line : more than 2 billion characters!." );
626
CPLReadLineBuffer( -1 );
592
630
pszRLBuffer = CPLReadLineBuffer( nBufLength + nChunkSize + 1 );
631
if( pszRLBuffer == NULL )
594
634
if( nChunkBytesRead == nChunkBytesConsumed + 1 )
917
957
sscanf( szTemp+2, "%p", &pResult );
919
959
sscanf( szTemp, "%p", &pResult );
961
/* Solaris actually behaves like MSVCRT... */
964
sscanf( szTemp+2, "%p", &pResult );
1383
1429
"CPLVerifyConfiguration(): byte order set wrong.\n" );
1432
/* Uncomment to get list of options that have been fetched and set */
1433
//#define DEBUG_CONFIG_OPTIONS
1435
#ifdef DEBUG_CONFIG_OPTIONS
1438
#include "cpl_multiproc.h"
1440
static void* hRegisterConfigurationOptionMutex = 0;
1441
static std::set<CPLString>* paoGetKeys = NULL;
1442
static std::set<CPLString>* paoSetKeys = NULL;
1444
/************************************************************************/
1445
/* CPLShowAccessedOptions() */
1446
/************************************************************************/
1448
static void CPLShowAccessedOptions()
1450
std::set<CPLString>::iterator aoIter;
1452
printf("Configuration options accessed in reading : "),
1453
aoIter = paoGetKeys->begin();
1454
while(aoIter != paoGetKeys->end())
1456
printf("%s, ", (*aoIter).c_str());
1461
printf("Configuration options accessed in writing : "),
1462
aoIter = paoSetKeys->begin();
1463
while(aoIter != paoSetKeys->end())
1465
printf("%s, ", (*aoIter).c_str());
1472
paoGetKeys = paoSetKeys = NULL;
1475
/************************************************************************/
1476
/* CPLAccessConfigOption() */
1477
/************************************************************************/
1479
static void CPLAccessConfigOption(const char* pszKey, int bGet)
1481
CPLMutexHolderD(&hRegisterConfigurationOptionMutex);
1482
if (paoGetKeys == NULL)
1484
paoGetKeys = new std::set<CPLString>;
1485
paoSetKeys = new std::set<CPLString>;
1486
atexit(CPLShowAccessedOptions);
1489
paoGetKeys->insert(pszKey);
1491
paoSetKeys->insert(pszKey);
1386
1495
/************************************************************************/
1387
1496
/* CPLGetConfigOption() */
1388
1497
/************************************************************************/
1398
1507
* @param pszDefault a default value if the key does not match existing defined options (may be NULL)
1399
1508
* @return the value associated to the key, or the default value if not found
1401
* @see CPLSetConfigOption()
1510
* @see CPLSetConfigOption(), http://trac.osgeo.org/gdal/wiki/ConfigOptions
1403
1512
const char * CPL_STDCALL
1404
1513
CPLGetConfigOption( const char *pszKey, const char *pszDefault )
1516
#ifdef DEBUG_CONFIG_OPTIONS
1517
CPLAccessConfigOption(pszKey, TRUE);
1407
1520
const char *pszResult = NULL;
1409
1522
char **papszTLConfigOptions = (char **) CPLGetTLS( CTLS_CONFIGOPTIONS );
1450
1563
* ogrinfo --config CPL_DEBUG ON ~/data/test/point.shp
1452
1565
* @param pszKey the key of the option
1453
* @param pszValue the value of the option
1566
* @param pszValue the value of the option, or NULL to clear a setting.
1568
* @see http://trac.osgeo.org/gdal/wiki/ConfigOptions
1455
1570
void CPL_STDCALL
1456
1571
CPLSetConfigOption( const char *pszKey, const char *pszValue )
1574
#ifdef DEBUG_CONFIG_OPTIONS
1575
CPLAccessConfigOption(pszKey, FALSE);
1459
1577
CPLMutexHolderD( &hConfigMutex );
1461
1579
papszConfigOptions = (volatile char **)
1477
1595
* that applies on all threads.
1479
1597
* @param pszKey the key of the option
1480
* @param pszValue the value of the option
1598
* @param pszValue the value of the option, or NULL to clear a setting.
1483
1601
void CPL_STDCALL
1484
1602
CPLSetThreadLocalConfigOption( const char *pszKey, const char *pszValue )
1605
#ifdef DEBUG_CONFIG_OPTIONS
1606
CPLAccessConfigOption(pszKey, FALSE);
1487
1609
char **papszTLConfigOptions = (char **) CPLGetTLS( CTLS_CONFIGOPTIONS );
1489
1611
papszTLConfigOptions =
1490
1612
CSLSetNameValue( papszTLConfigOptions, pszKey, pszValue );
1492
CPLSetTLS( CTLS_CONFIGOPTIONS, papszTLConfigOptions, FALSE );
1614
CPLSetTLSWithFreeFunc( CTLS_CONFIGOPTIONS, papszTLConfigOptions, (CPLTLSFreeFunc)CSLDestroy );
1495
1617
/************************************************************************/
1499
1621
void CPL_STDCALL CPLFreeConfig()
1502
CPLMutexHolderD( &hConfigMutex );
1504
CSLDestroy( (char **) papszConfigOptions);
1505
papszConfigOptions = NULL;
1507
char **papszTLConfigOptions = (char **) CPLGetTLS( CTLS_CONFIGOPTIONS );
1508
if( papszTLConfigOptions != NULL )
1510
CSLDestroy( papszTLConfigOptions );
1511
CPLSetTLS( CTLS_CONFIGOPTIONS, NULL, FALSE );
1625
CPLMutexHolderD( &hConfigMutex );
1627
CSLDestroy( (char **) papszConfigOptions);
1628
papszConfigOptions = NULL;
1630
char **papszTLConfigOptions = (char **) CPLGetTLS( CTLS_CONFIGOPTIONS );
1631
if( papszTLConfigOptions != NULL )
1633
CSLDestroy( papszTLConfigOptions );
1634
CPLSetTLS( CTLS_CONFIGOPTIONS, NULL, FALSE );
1637
CPLDestroyMutex( hConfigMutex );
1638
hConfigMutex = NULL;
1515
1641
/************************************************************************/
1679
1805
pszHemisphere = "N";
1681
sprintf( szFormat, "%%3dd%%2d\'%%.%df\"%s", nPrecision, pszHemisphere );
1807
sprintf( szFormat, "%%3dd%%2d\'%%%d.%df\"%s", nPrecision+3, nPrecision, pszHemisphere );
1682
1808
sprintf( szBuffer, szFormat, nDegrees, nMinutes, dfSeconds );
1684
1810
return( szBuffer );
1943
2069
/* Close the file, and remove the information. */
1944
2070
/* -------------------------------------------------------------------- */
1945
2071
if( pasSharedFileList[i].bLarge )
1946
VSIFCloseL( pasSharedFileList[i].fp );
2072
VSIFCloseL( (VSILFILE*) pasSharedFileList[i].fp );
1948
2074
VSIFClose( pasSharedFileList[i].fp );
1951
2077
CPLFree( pasSharedFileList[i].pszAccess );
1953
2079
// pasSharedFileList[i] = pasSharedFileList[--nSharedFileCount];
1954
memcpy( (void *) (pasSharedFileList + i),
1955
(void *) (pasSharedFileList + --nSharedFileCount),
1956
sizeof(CPLSharedFileInfo) );
2080
memmove( (void *) (pasSharedFileList + i),
2081
(void *) (pasSharedFileList + --nSharedFileCount),
2082
sizeof(CPLSharedFileInfo) );
1958
2084
if( nSharedFileCount == 0 )
2161
2287
/* -------------------------------------------------------------------- */
2163
2289
nBytesRead = VSIFReadL( pabyBuffer, 1, nBufferSize, fpOld );
2164
if( nBytesRead < 0 )
2290
if( long(nBytesRead) < 0 )
2187
2313
int CPLMoveFile( const char *pszNewPath, const char *pszOldPath )
2190
if( VSIRename( pszNewPath, pszOldPath ) == 0 )
2316
if( VSIRename( pszOldPath, pszNewPath ) == 0 )
2193
2319
int nRet = CPLCopyFile( pszNewPath, pszOldPath );
2213
2339
CPLLocaleC::CPLLocaleC() : pszOldLocale(CPLStrdup(setlocale(LC_NUMERIC,NULL)))
2216
if( setlocale(LC_NUMERIC,"C") == NULL )
2342
if( CSLTestBoolean(CPLGetConfigOption("GDAL_DISABLE_CPLLOCALEC","NO"))
2343
|| EQUAL(pszOldLocale,"C")
2344
|| EQUAL(pszOldLocale,"POSIX")
2345
|| setlocale(LC_NUMERIC,"C") == NULL )
2218
2347
CPLFree( pszOldLocale );
2219
2348
pszOldLocale = NULL;
2426
/************************************************************************/
2427
/* Stub implementation of zip services if we don't have libz. */
2428
/************************************************************************/
2430
#if !defined(HAVE_LIBZ)
2432
void *CPLCreateZip( const char *pszZipFilename, char **papszOptions )
2435
CPLError( CE_Failure, CPLE_NotSupported,
2436
"This GDAL/OGR build does not include zlib and zip services." );
2440
CPLErr CPLCreateFileInZip( void *hZip, const char *pszFilename,
2441
char **papszOptions )
2447
CPLErr CPLWriteFileInZip( void *hZip, const void *pBuffer, int nBufferSize )
2453
CPLErr CPLCloseFileInZip( void *hZip )
2459
CPLErr CPLCloseZip( void *hZip )
2465
#endif /* !defined(HAVE_LIBZ) */