diff --git a/EgtGeneral.rc b/EgtGeneral.rc index 33bd648..4702d26 100644 Binary files a/EgtGeneral.rc and b/EgtGeneral.rc differ diff --git a/StringUtils.cpp b/StringUtils.cpp index dcca1b2..a31ce6a 100644 --- a/StringUtils.cpp +++ b/StringUtils.cpp @@ -21,6 +21,74 @@ using namespace std ; +//---------------------------------------------------------------------------- +static inline void +SkipSpaces( const char*& pC) +{ + while ( *pC == ' ' || *pC == '\t') + ++ pC ; +} + +//---------------------------------------------------------------------------- +static inline void +SkipSpaces( char*& pC) +{ + while ( *pC == ' ' || *pC == '\t') + ++ pC ; +} + +//---------------------------------------------------------------------------- +bool +FromString( const string& sVal, INTVECTOR& vnVal) +{ + // pulisco il vettore dei risultati + vnVal.clear() ; + // reset errori di conversione + errno = 0 ; + // ciclo sui caratteri della stringa + const char* pStart = sVal.c_str() ; + SkipSpaces( pStart) ; + while ( *pStart >= '0' && *pStart <= '9') { + char* pStop ; + int nVal = strtol( pStart, &pStop, 10) ; + vnVal.push_back( nVal) ; + SkipSpaces( pStop) ; + if ( *pStop == '\0' && errno == 0) + return true ; + if ( *pStop != ',' || errno != 0) + return false ; + pStart = pStop + 1 ; + SkipSpaces( pStart) ; + } + return ( *pStart == '\0' && errno == 0) ; +} + +//---------------------------------------------------------------------------- +bool +FromString( const string& sVal, DBLVECTOR& vdVal) +{ + // pulisco il vettore dei risultati + vdVal.clear() ; + // reset errori di conversione + errno = 0 ; + // ciclo sui caratteri della stringa + const char* pStart = sVal.c_str() ; + SkipSpaces( pStart) ; + while ( *pStart >= '0' && *pStart <= '9') { + char* pStop ; + double dVal = strtod( pStart, &pStop) ; + vdVal.push_back( dVal) ; + SkipSpaces( pStop) ; + if ( *pStop == '\0' && errno == 0) + return true ; + if ( *pStop != ',' || errno != 0) + return false ; + pStart = pStop + 1 ; + SkipSpaces( pStart) ; + } + return ( *pStart == '\0' && errno == 0) ; +} + //---------------------------------------------------------------------------- const string ToString( double dVal, int nPrec) @@ -28,21 +96,13 @@ ToString( double dVal, int nPrec) const double BIG_NUMBER = 1e10 ; const int BIG_DIGITNBR = 10 ; const int MAX_PREC = 12 ; - char szBuff[24] ; - char szDest[32] ; - char* pBuff ; - char* pDest ; - int nErr ; - int nDecimal ; - int nSign ; - int nExp ; - int nTmp ; - // verifico la precisione nPrec = min( nPrec, MAX_PREC) ; // converto la mantissa + char szBuff[24] ; + int nErr, nDecimal, nSign ; if ( fabs( dVal) < BIG_NUMBER) nErr = _fcvt_s( szBuff, dVal, nPrec, &nDecimal, &nSign) ; else @@ -59,6 +119,7 @@ ToString( double dVal, int nPrec) return "0" ; // verifica per forma esponenziale + int nExp ; if ( nDecimal > BIG_DIGITNBR) { nExp = nDecimal - 1 ; nDecimal = 1 ; @@ -67,8 +128,9 @@ ToString( double dVal, int nPrec) nExp = 0 ; // puntatori a inizio stringhe - pBuff = szBuff ; - pDest = szDest ; + char szDest[32] ; + char* pBuff = szBuff ; + char* pDest = szDest ; // eventuale inserimento segno '-' if ( nSign != 0) { *pDest = '-' ; @@ -116,7 +178,7 @@ ToString( double dVal, int nPrec) if ( nExp > 0) { *pDest = 'e' ; pDest ++ ; - nTmp = nExp ; + int nTmp = nExp ; if ( nTmp >= 100) { *pDest = '0' + ( nTmp / 100) ; pDest ++ ; @@ -138,16 +200,45 @@ ToString( double dVal, int nPrec) return szDest ; } +//---------------------------------------------------------------------------- +const string +ToString( const INTVECTOR& vnVal, int nPrec) +{ + // se vettore vuoto, stringa vuota + if ( vnVal.empty()) + return "" ; + // costruisco la stringa + string sDest ; + sDest.reserve( 8 * vnVal.size()) ; + for ( size_t i = 0 ; i < vnVal.size() ; ++ i) + sDest += ToString( vnVal[i], nPrec) + "," ; + sDest.pop_back() ; + return sDest ; +} + +//---------------------------------------------------------------------------- +const string +ToString( const DBLVECTOR& vdVal, int nPrec) +{ + // se vettore vuoto, stringa vuota + if ( vdVal.empty()) + return "" ; + // costruisco la stringa + string sDest ; + sDest.reserve( 14 * vdVal.size()) ; + for ( size_t i = 0 ; i < vdVal.size() ; ++ i) + sDest += ToString( vdVal[i], nPrec) + "," ; + sDest.pop_back() ; + return sDest ; +} + //---------------------------------------------------------------------------- void Split( const string& sString, const string& sSeparator, bool bFirstVsLast, string& sFirst, string& sLast) { - string::size_type iPos ; - - // cerco il separatore - iPos = ( bFirstVsLast ? sString.find( sSeparator) : sString.rfind( sSeparator)) ; + string::size_type iPos = ( bFirstVsLast ? sString.find( sSeparator) : sString.rfind( sSeparator)) ; // se trovato if ( iPos != string::npos) { if ( iPos > 0) @@ -170,24 +261,19 @@ Split( const string& sString, const string& sSeparator, bool bFirstVsLast, bool Tokenize( const string& sString, const string& sSeparators, STRVECTOR& vsTokens) { - string::size_type iPosStart ; - string::size_type iPosEnd ; - - // pulisco il risultato vsTokens.clear() ; // parto dall'inizio - iPosStart = ( sString.empty() ? string::npos : 0) ; + string::size_type iPosStart = ( sString.empty() ? string::npos : 0) ; // mentre esiste un nuovo inizio di token while ( iPosStart != string::npos) { // cerco il primo separatore - iPosEnd = sString.find_first_of( sSeparators, iPosStart) ; + string::size_type iPosEnd = sString.find_first_of( sSeparators, iPosStart) ; // aggiungo il token al vettore vsTokens.push_back( sString.substr( iPosStart, iPosEnd - iPosStart)) ; // cerco l'inizio di un nuovo token iPosStart = sString.find_first_not_of( sSeparators, iPosEnd) ; } - return true ; } @@ -200,7 +286,6 @@ Tokenize( const string& sString, const string& sSeparators, if ( sAtomStarts.find_first_of( sSeparators) != string::npos || sAtomEnds.find_first_of( sSeparators) != string::npos) return false ; - // pulisco il risultato vsTokens.clear() ; // se la stringa è vuota non c'è niente da cercare @@ -240,26 +325,20 @@ Tokenize( const string& sString, const string& sSeparators, // se è rimasto un ultimo token if ( iPosStart != string::npos) vsTokens.push_back( sString.substr( iPosStart)) ; - return true ; } //---------------------------------------------------------------------------- int -ReplaceString( std::string& sString, const std::string& sOld, const std::string& sNew) +ReplaceString( string& sString, const string& sOld, const string& sNew) { - int nSubs ; - size_t pos ; - - - nSubs = 0 ; - pos = 0 ; - while ( ( pos = sString.find( sOld, pos)) != std::string::npos) { + int nSubs = 0 ; + size_t pos = 0 ; + while ( ( pos = sString.find( sOld, pos)) != string::npos) { sString.replace( pos, sOld.length(), sNew) ; pos += sNew.length() ; nSubs ++ ; } - return nSubs ; }