~ubuntu-branches/debian/sid/gdal/sid

« back to all changes in this revision

Viewing changes to port/cpl_conv.cpp

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-05-07 15:04:42 UTC
  • mfrom: (5.5.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120507150442-2eks97loeh6rq005
Tags: 1.9.0-1
* Ready for sid, starting transition.
* All symfiles updated to latest builds.
* Added dh_numpy call in debian/rules to depend on numpy ABI.
* Policy bumped to 3.9.3, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 $
3
3
 *
4
4
 * Project:  CPL - Common Portability Library
5
5
 * Purpose:  Convenience functions.
34
34
#include "cpl_vsi.h"
35
35
#include "cpl_multiproc.h"
36
36
 
37
 
CPL_CVSID("$Id: cpl_conv.cpp 18060 2009-11-21 20:26:25Z rouault $");
 
37
CPL_CVSID("$Id: cpl_conv.cpp 23504 2011-12-09 20:43:50Z rouault $");
38
38
 
39
39
#if defined(WIN32CE)
40
40
#  include "cpl_wince.h"
77
77
    if( nSize * nCount == 0 )
78
78
        return NULL;
79
79
    
80
 
    pReturn = VSICalloc( nCount, nSize );
81
 
    if( pReturn == NULL )
82
 
    {
83
 
        CPLError( CE_Fatal, CPLE_OutOfMemory,
84
 
                  "CPLCalloc(): Out of memory allocating %ld bytes.\n",
85
 
                  (long) (nSize * nCount) );
86
 
    }
87
 
 
 
80
    pReturn = CPLMalloc( nCount * nSize );
 
81
    memset( pReturn, 0, nCount * nSize );
88
82
    return pReturn;
89
83
}
90
84
 
117
111
    if( nSize == 0 )
118
112
        return NULL;
119
113
 
120
 
    if( nSize < 0 )
 
114
    if( long(nSize) < 0 )
121
115
    {
122
116
        CPLError( CE_Failure, CPLE_AppDefined,
123
117
                  "CPLMalloc(%ld): Silly size requested.\n",
128
122
    pReturn = VSIMalloc( nSize );
129
123
    if( pReturn == NULL )
130
124
    {
131
 
        CPLError( CE_Fatal, CPLE_OutOfMemory,
132
 
                  "CPLMalloc(): Out of memory allocating %ld bytes.\n",
133
 
                  (long) nSize );
 
125
        if( nSize > 0 && nSize < 2000 )
 
126
        {
 
127
            char szSmallMsg[60];
 
128
 
 
129
            sprintf( szSmallMsg, 
 
130
                     "CPLMalloc(): Out of memory allocating %ld bytes.", 
 
131
                     (long) nSize );
 
132
            CPLEmergencyError( szSmallMsg ); 
 
133
        }
 
134
        else
 
135
            CPLError( CE_Fatal, CPLE_OutOfMemory,
 
136
                      "CPLMalloc(): Out of memory allocating %ld bytes.\n",
 
137
                      (long) nSize );
134
138
    }
135
139
 
136
140
    return pReturn;
171
175
        return NULL;
172
176
    }
173
177
 
174
 
    if( nNewSize < 0 )
 
178
    if( long(nNewSize) < 0 )
175
179
    {
176
180
        CPLError( CE_Failure, CPLE_AppDefined,
177
181
                  "CPLRealloc(%ld): Silly size requested.\n",
186
190
    
187
191
    if( pReturn == NULL )
188
192
    {
189
 
        CPLError( CE_Fatal, CPLE_OutOfMemory,
190
 
                  "CPLRealloc(): Out of memory allocating %ld bytes.\n",
191
 
                  (long)nNewSize );
 
193
        if( nNewSize > 0 && nNewSize < 2000 )
 
194
        {
 
195
            char szSmallMsg[60];
 
196
 
 
197
            sprintf( szSmallMsg, 
 
198
                     "CPLRealloc(): Out of memory allocating %ld bytes.", 
 
199
                     (long) nNewSize );
 
200
            CPLEmergencyError( szSmallMsg ); 
 
201
        }
 
202
        else
 
203
            CPLError( CE_Fatal, CPLE_OutOfMemory,
 
204
                      "CPLRealloc(): Out of memory allocating %ld bytes.\n",
 
205
                      (long) nNewSize );
192
206
    }
193
207
 
194
208
    return pReturn;
224
238
    if( pszString == NULL )
225
239
        pszString = "";
226
240
 
227
 
    pszReturn = VSIStrdup( pszString );
228
 
        
 
241
    pszReturn = (char *) CPLMalloc(strlen(pszString)+1);
229
242
    if( pszReturn == NULL )
230
243
    {
231
244
        CPLError( CE_Fatal, CPLE_OutOfMemory,
233
246
                  (long) strlen(pszString) );
234
247
        
235
248
    }
236
 
    
 
249
 
 
250
    strcpy( pszReturn, pszString );
237
251
    return( pszReturn );
238
252
}
239
253
 
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 )
399
416
 
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 )
431
448
    {
432
449
        int nNewSize = nRequiredSize + 4 + 500;
 
450
        if (nNewSize <= 0)
 
451
        {
 
452
            VSIFree( pnAlloc );
 
453
            CPLSetTLS( CTLS_RLBUFFERINFO, NULL, FALSE );
 
454
            CPLError( CE_Failure, CPLE_OutOfMemory,
 
455
                      "CPLReadLineBuffer(): Trying to allocate more than 2 GB." );
 
456
            return NULL;
 
457
        }
433
458
 
434
459
        GUInt32* pnAllocNew = (GUInt32 *) VSIRealloc(pnAlloc,nNewSize);
435
460
        if( pnAllocNew == NULL )
436
461
        {
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.",
 
466
                      (long) nNewSize );
439
467
            return NULL;
440
468
        }
441
469
        pnAlloc = pnAllocNew;
536
564
 * from the file or NULL if the end of file was encountered.
537
565
 */
538
566
 
539
 
const char *CPLReadLineL( FILE * fp )
 
567
const char *CPLReadLineL( VSILFILE * fp )
540
568
{
541
569
    return CPLReadLine2L( fp, -1, NULL );
542
570
}
561
589
 * @since GDAL 1.7.0
562
590
 */
563
591
 
564
 
const char *CPLReadLine2L( FILE * fp, int nMaxCars, char** papszOptions )
 
592
const char *CPLReadLine2L( VSILFILE * fp, int nMaxCars, char** papszOptions )
565
593
 
566
594
{
 
595
    (void) papszOptions;
 
596
 
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 )
 
623
        {
 
624
            CPLError( CE_Failure, CPLE_AppDefined,
 
625
                      "Too big line : more than 2 billion characters!." );
 
626
            CPLReadLineBuffer( -1 );
 
627
            return NULL;
 
628
        }
 
629
 
592
630
        pszRLBuffer = CPLReadLineBuffer( nBufLength + nChunkSize + 1 );
 
631
        if( pszRLBuffer == NULL )
 
632
            return NULL;
593
633
 
594
634
        if( nChunkBytesRead == nChunkBytesConsumed + 1 )
595
635
        {
917
957
        sscanf( szTemp+2, "%p", &pResult );
918
958
#else
919
959
        sscanf( szTemp, "%p", &pResult );
 
960
 
 
961
        /* Solaris actually behaves like MSVCRT... */
 
962
        if (pResult == NULL)
 
963
        {
 
964
            sscanf( szTemp+2, "%p", &pResult );
 
965
        }
920
966
#endif
921
967
    }
922
968
    
1383
1429
                  "CPLVerifyConfiguration(): byte order set wrong.\n" );
1384
1430
}
1385
1431
 
 
1432
/* Uncomment to get list of options that have been fetched and set */
 
1433
//#define DEBUG_CONFIG_OPTIONS
 
1434
 
 
1435
#ifdef DEBUG_CONFIG_OPTIONS
 
1436
 
 
1437
#include <set>
 
1438
#include "cpl_multiproc.h"
 
1439
 
 
1440
static void* hRegisterConfigurationOptionMutex = 0;
 
1441
static std::set<CPLString>* paoGetKeys = NULL;
 
1442
static std::set<CPLString>* paoSetKeys = NULL;
 
1443
 
 
1444
/************************************************************************/
 
1445
/*                      CPLShowAccessedOptions()                        */
 
1446
/************************************************************************/
 
1447
 
 
1448
static void CPLShowAccessedOptions()
 
1449
{
 
1450
    std::set<CPLString>::iterator aoIter;
 
1451
 
 
1452
    printf("Configuration options accessed in reading : "),
 
1453
    aoIter = paoGetKeys->begin();
 
1454
    while(aoIter != paoGetKeys->end())
 
1455
    {
 
1456
        printf("%s, ", (*aoIter).c_str());
 
1457
        aoIter ++;
 
1458
    }
 
1459
    printf("\n");
 
1460
 
 
1461
    printf("Configuration options accessed in writing : "),
 
1462
    aoIter = paoSetKeys->begin();
 
1463
    while(aoIter != paoSetKeys->end())
 
1464
    {
 
1465
        printf("%s, ", (*aoIter).c_str());
 
1466
        aoIter ++;
 
1467
    }
 
1468
    printf("\n");
 
1469
 
 
1470
    delete paoGetKeys;
 
1471
    delete paoSetKeys;
 
1472
    paoGetKeys = paoSetKeys = NULL;
 
1473
}
 
1474
 
 
1475
/************************************************************************/
 
1476
/*                       CPLAccessConfigOption()                        */
 
1477
/************************************************************************/
 
1478
 
 
1479
static void CPLAccessConfigOption(const char* pszKey, int bGet)
 
1480
{
 
1481
    CPLMutexHolderD(&hRegisterConfigurationOptionMutex);
 
1482
    if (paoGetKeys == NULL)
 
1483
    {
 
1484
        paoGetKeys = new std::set<CPLString>;
 
1485
        paoSetKeys = new std::set<CPLString>;
 
1486
        atexit(CPLShowAccessedOptions);
 
1487
    }
 
1488
    if (bGet)
 
1489
        paoGetKeys->insert(pszKey);
 
1490
    else
 
1491
        paoSetKeys->insert(pszKey);
 
1492
}
 
1493
#endif
 
1494
 
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
1400
1509
  *
1401
 
  * @see CPLSetConfigOption()
 
1510
  * @see CPLSetConfigOption(), http://trac.osgeo.org/gdal/wiki/ConfigOptions
1402
1511
  */
1403
1512
const char * CPL_STDCALL
1404
1513
CPLGetConfigOption( const char *pszKey, const char *pszDefault )
1405
1514
 
1406
1515
{
 
1516
#ifdef DEBUG_CONFIG_OPTIONS
 
1517
    CPLAccessConfigOption(pszKey, TRUE);
 
1518
#endif
 
1519
 
1407
1520
    const char *pszResult = NULL;
1408
1521
 
1409
1522
    char **papszTLConfigOptions = (char **) CPLGetTLS( CTLS_CONFIGOPTIONS );
1450
1563
  * ogrinfo --config CPL_DEBUG ON ~/data/test/point.shp
1451
1564
  *
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.
 
1567
  * 
 
1568
  * @see http://trac.osgeo.org/gdal/wiki/ConfigOptions
1454
1569
  */
1455
1570
void CPL_STDCALL 
1456
1571
CPLSetConfigOption( const char *pszKey, const char *pszValue )
1457
1572
 
1458
1573
{
 
1574
#ifdef DEBUG_CONFIG_OPTIONS
 
1575
    CPLAccessConfigOption(pszKey, FALSE);
 
1576
#endif
1459
1577
    CPLMutexHolderD( &hConfigMutex );
1460
1578
 
1461
1579
    papszConfigOptions = (volatile char **) 
1477
1595
  * that applies on all threads.
1478
1596
  *
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.
1481
1599
  */
1482
1600
 
1483
1601
void CPL_STDCALL 
1484
1602
CPLSetThreadLocalConfigOption( const char *pszKey, const char *pszValue )
1485
1603
 
1486
1604
{
 
1605
#ifdef DEBUG_CONFIG_OPTIONS
 
1606
    CPLAccessConfigOption(pszKey, FALSE);
 
1607
#endif
 
1608
 
1487
1609
    char **papszTLConfigOptions = (char **) CPLGetTLS( CTLS_CONFIGOPTIONS );
1488
1610
 
1489
1611
    papszTLConfigOptions = 
1490
1612
        CSLSetNameValue( papszTLConfigOptions, pszKey, pszValue );
1491
1613
 
1492
 
    CPLSetTLS( CTLS_CONFIGOPTIONS, papszTLConfigOptions, FALSE );
 
1614
    CPLSetTLSWithFreeFunc( CTLS_CONFIGOPTIONS, papszTLConfigOptions, (CPLTLSFreeFunc)CSLDestroy );
1493
1615
}
1494
1616
 
1495
1617
/************************************************************************/
1499
1621
void CPL_STDCALL CPLFreeConfig()
1500
1622
 
1501
1623
{
1502
 
    CPLMutexHolderD( &hConfigMutex );
1503
 
 
1504
 
    CSLDestroy( (char **) papszConfigOptions);
1505
 
    papszConfigOptions = NULL;
1506
 
 
1507
 
    char **papszTLConfigOptions = (char **) CPLGetTLS( CTLS_CONFIGOPTIONS );
1508
 
    if( papszTLConfigOptions != NULL )
1509
1624
    {
1510
 
        CSLDestroy( papszTLConfigOptions );
1511
 
        CPLSetTLS( CTLS_CONFIGOPTIONS, NULL, FALSE );
 
1625
        CPLMutexHolderD( &hConfigMutex );
 
1626
 
 
1627
        CSLDestroy( (char **) papszConfigOptions);
 
1628
        papszConfigOptions = NULL;
 
1629
        
 
1630
        char **papszTLConfigOptions = (char **) CPLGetTLS( CTLS_CONFIGOPTIONS );
 
1631
        if( papszTLConfigOptions != NULL )
 
1632
        {
 
1633
            CSLDestroy( papszTLConfigOptions );
 
1634
            CPLSetTLS( CTLS_CONFIGOPTIONS, NULL, FALSE );
 
1635
        }
1512
1636
    }
 
1637
    CPLDestroyMutex( hConfigMutex );
 
1638
    hConfigMutex = NULL;
1513
1639
}
1514
1640
 
1515
1641
/************************************************************************/
1623
1749
        ++s;
1624
1750
    }
1625
1751
    /* postfix sign */
1626
 
    if (*s && (p = (char *) strchr(sym, *s))) {
 
1752
    if (*s && ((p = (char *) strchr(sym, *s))) != NULL) {
1627
1753
        sign = (p - sym) >= 4 ? '-' : '+';
1628
1754
        ++s;
1629
1755
    }
1678
1804
    else
1679
1805
        pszHemisphere = "N";
1680
1806
 
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 );
1683
1809
 
1684
1810
    return( szBuffer );
1795
1921
    while( *pszString == ' ' )
1796
1922
        pszString++;
1797
1923
 
1798
 
    *pdfReal = atof(pszString);
 
1924
    *pdfReal = CPLAtof(pszString);
1799
1925
    *pdfImag = 0.0;
1800
1926
 
1801
1927
    for( i = 0; pszString[i] != '\0' && pszString[i] != ' ' && i < 100; i++ )
1810
1936
 
1811
1937
    if( iPlus > -1 && iImagEnd > -1 && iPlus < iImagEnd )
1812
1938
    {
1813
 
        *pdfImag = atof(pszString + iPlus);
 
1939
        *pdfImag = CPLAtof(pszString + iPlus);
1814
1940
    }
1815
1941
 
1816
1942
    return;
1875
2001
    FILE *fp;
1876
2002
 
1877
2003
    if( bLarge )
1878
 
        fp = VSIFOpenL( pszFilename, pszAccess );
 
2004
        fp = (FILE*) VSIFOpenL( pszFilename, pszAccess );
1879
2005
    else
1880
2006
        fp = VSIFOpen( pszFilename, pszAccess );
1881
2007
 
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 );
1947
2073
    else
1948
2074
        VSIFClose( pasSharedFileList[i].fp );
1949
2075
 
1951
2077
    CPLFree( pasSharedFileList[i].pszAccess );
1952
2078
 
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) );
1957
2083
 
1958
2084
    if( nSharedFileCount == 0 )
1959
2085
    {
2130
2256
int CPLCopyFile( const char *pszNewPath, const char *pszOldPath )
2131
2257
 
2132
2258
{
2133
 
    FILE *fpOld, *fpNew;
 
2259
    VSILFILE *fpOld, *fpNew;
2134
2260
    GByte *pabyBuffer;
2135
2261
    size_t nBufferSize;
2136
2262
    size_t nBytesRead;
2161
2287
/* -------------------------------------------------------------------- */
2162
2288
    do { 
2163
2289
        nBytesRead = VSIFReadL( pabyBuffer, 1, nBufferSize, fpOld );
2164
 
        if( nBytesRead < 0 )
 
2290
        if( long(nBytesRead) < 0 )
2165
2291
            nRet = -1;
2166
2292
 
2167
2293
        if( nRet == 0
2187
2313
int CPLMoveFile( const char *pszNewPath, const char *pszOldPath )
2188
2314
 
2189
2315
{
2190
 
    if( VSIRename( pszNewPath, pszOldPath ) == 0 )
 
2316
    if( VSIRename( pszOldPath, pszNewPath ) == 0 )
2191
2317
        return 0;
2192
2318
 
2193
2319
    int nRet = CPLCopyFile( pszNewPath, pszOldPath );
2213
2339
CPLLocaleC::CPLLocaleC() : pszOldLocale(CPLStrdup(setlocale(LC_NUMERIC,NULL)))
2214
2340
 
2215
2341
{
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 )
2217
2346
    {
2218
2347
        CPLFree( pszOldLocale );
2219
2348
        pszOldLocale = NULL;
2293
2422
 
2294
2423
    return FALSE;
2295
2424
}
 
2425
 
 
2426
/************************************************************************/
 
2427
/*      Stub implementation of zip services if we don't have libz.      */
 
2428
/************************************************************************/
 
2429
 
 
2430
#if !defined(HAVE_LIBZ)
 
2431
 
 
2432
void *CPLCreateZip( const char *pszZipFilename, char **papszOptions )
 
2433
 
 
2434
{
 
2435
    CPLError( CE_Failure, CPLE_NotSupported,
 
2436
              "This GDAL/OGR build does not include zlib and zip services." );
 
2437
    return NULL;
 
2438
}
 
2439
 
 
2440
CPLErr CPLCreateFileInZip( void *hZip, const char *pszFilename, 
 
2441
                           char **papszOptions )
 
2442
 
 
2443
{
 
2444
    return CE_Failure;
 
2445
}
 
2446
 
 
2447
CPLErr CPLWriteFileInZip( void *hZip, const void *pBuffer, int nBufferSize )
 
2448
 
 
2449
{
 
2450
    return CE_Failure;
 
2451
}
 
2452
 
 
2453
CPLErr CPLCloseFileInZip( void *hZip )
 
2454
 
 
2455
{
 
2456
    return CE_Failure;
 
2457
}
 
2458
 
 
2459
CPLErr CPLCloseZip( void *hZip )
 
2460
 
 
2461
{
 
2462
    return CE_Failure;
 
2463
}
 
2464
 
 
2465
#endif /* !defined(HAVE_LIBZ) */