Changeset 10913

Show
Ignore:
Timestamp:
03/10/07 14:11:19 (2 years ago)
Author:
warmerdam
Message:

added trim method

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gdal/port/cpl_string.h

    r10645 r10913  
    178178    CPLString &Printf( const char *pszFormat, ... ); 
    179179    CPLString &vPrintf( const char *pszFormat, va_list args ); 
     180    CPLString &FormatC( double dfValue, const char *pszFormat = NULL ); 
     181    CPLString &Trim(); 
    180182}; 
    181183 
  • trunk/gdal/port/cplstring.cpp

    r10645 r10913  
    130130    return *this; 
    131131} 
     132 
     133/************************************************************************/ 
     134/*                              FormatC()                               */ 
     135/************************************************************************/ 
     136 
     137/** 
     138 * Format double in C locale. 
     139 *  
     140 * The passed value is formatted using the C locale (period as decimal  
     141 * seperator) and appended to the target CPLString.  
     142 * 
     143 * @param dfValue the value to format.  
     144 * @param pszFormat the sprintf() style format to use or omit for default. 
     145 * Note that this format string should only include one substitution argument  
     146 * and it must be for a double (%f or %g).  
     147 * 
     148 * @return a reference to the CPLString.  
     149 */ 
     150 
     151CPLString &CPLString::FormatC( double dfValue, const char *pszFormat ) 
     152 
     153{ 
     154    if( pszFormat == NULL ) 
     155        pszFormat = "%g"; 
     156 
     157    char szWork[512]; // presumably long enough for any number? 
     158 
     159    sprintf( szWork, pszFormat, dfValue ); 
     160    CPLAssert( strlen(szWork) < sizeof(szWork) ); 
     161     
     162    if( strchr( szWork, ',' ) != NULL ) 
     163    { 
     164        char *pszDelim = strchr( szWork, ',' ); 
     165        *pszDelim = '.'; 
     166    } 
     167 
     168    *this += szWork; 
     169     
     170    return *this; 
     171} 
     172 
     173/************************************************************************/ 
     174/*                                Trim()                                */ 
     175/************************************************************************/ 
     176 
     177/** 
     178 * Trim white space. 
     179 * 
     180 * Trims white space off the let and right of the string.  White space 
     181 * is any of a space, a tab, a newline ('\n') or a carriage control ('\r'). 
     182 * 
     183 * @return a reference to the CPLString.  
     184 */ 
     185 
     186CPLString &CPLString::Trim() 
     187 
     188{ 
     189    size_t iLeft, iRight; 
     190    static const char szWhitespace[] = " \t\r\n"; 
     191 
     192    iLeft = find_first_not_of( szWhitespace ); 
     193    iRight = find_last_not_of( szWhitespace ); 
     194 
     195    if( iLeft == string::npos ) 
     196    { 
     197        clear(); 
     198        return *this; 
     199    } 
     200     
     201    assign( substr( iLeft, iRight - iLeft + 1 ) ); 
     202 
     203    return *this; 
     204}