//---------------------------------------------------------------------------- // EgalTech 2013-2013 //---------------------------------------------------------------------------- // File : EgnStringUtils.h Data : 20.11.13 Versione : 1.3a1 // Contenuto : Dichiarazione delle funzioni di utilità per le stringhe. // // // // Modifiche : 20.11.13 DS Creazione modulo. // // //---------------------------------------------------------------------------- #pragma once #include #include "/EgtDev/Include/EgnStringBase.h" //----------------------- Macro per import/export ----------------------------- #undef EGN_EXPORT #if defined( I_AM_EGN) // da definirsi solo nella DLL #define EGN_EXPORT __declspec( dllexport) #else #define EGN_EXPORT __declspec( dllimport) #endif //---------------------------------------------------------------------------- typedef std::vector STRVECTOR ; //---------------------------------------------------------------------------- inline std::string& TrimLeft( std::string& sString, const char* szTarget = " \t\r\n") { size_t iStartPos = sString.find_first_not_of( szTarget) ; if ( iStartPos != std::string::npos) sString.erase( 0, iStartPos) ; else sString.clear() ; return sString ; } inline std::string& TrimRight( std::string& sString, const char* szTarget = " \t\r\n") { size_t iEndPos = sString.find_last_not_of( szTarget) ; if ( iEndPos != std::string::npos) sString.erase( iEndPos + 1) ; else sString.clear() ; return sString ; } inline std::string& Trim( std::string& sString, const char* szTarget = " \t\r\n") { return TrimLeft( TrimRight( sString, szTarget), szTarget) ; } inline std::string& TrimUtf8Bom( std::string& sString) { if ( sString[0] == '\xef' && sString[1] == '\xbb' && sString[2] == '\xbf') sString.erase( 0, 3) ; return sString ; } //---------------------------------------------------------------------------- inline std::string& ToUpper( std::string& sString) { std::transform( sString.begin(), sString.end(), sString.begin(), ::toupper) ; return sString ; } inline std::string& ToLower( std::string& sString) { std::transform( sString.begin(), sString.end(), sString.begin(), ::tolower) ; return sString ; } //---------------------------------------------------------------------------- inline bool FromString( const std::string& sVal, int& nVal) { const char* pStart ; char* pStop ; errno = 0 ; pStart = sVal.c_str() ; nVal = strtol( pStart, &pStop, 10) ; return ( pStop != pStart && *pStop == '\0' && errno == 0) ; } inline bool FromString( const std::string& sVal, bool& bVal) { int nTmp ; if ( ! FromString( sVal, nTmp)) return false ; bVal = ( nTmp != 0) ; return true ; } inline bool FromString( const std::string& sVal, double& dVal) { const char* pStart ; char* pStop ; errno = 0 ; pStart = sVal.c_str() ; dVal = strtod( pStart, &pStop) ; return ( pStop != pStart && *pStop == '\0' && errno == 0) ; } EGN_EXPORT const std::string ToString( int nVal, int nPrec = 1) ; inline const std::string ToString( bool bVal) { return std::string( ( bVal ? "1" : "0")) ; } EGN_EXPORT const std::string ToString( double dVal, int nPrec = 6) ; //---------------------------------------------------------------------------- EGN_EXPORT void Split( const std::string& sString, const std::string& sSeparator, bool bFirstVsLast, std::string& sFirst, std::string& sLast) ; inline void SplitFirst( const std::string& sString, const std::string& sSeparator, std::string& sFirst, std::string& sLast) { Split( sString, sSeparator, true, sFirst, sLast) ; } inline void SplitLast( const std::string& sString, const std::string& sSeparator, std::string& sFirst, std::string& sLast) { Split( sString, sSeparator, false, sFirst, sLast) ; } //---------------------------------------------------------------------------- EGN_EXPORT bool Tokenize( const std::string& sString, const std::string& sSeparators, STRVECTOR& vsTokens) ; EGN_EXPORT bool Tokenize( const std::string& sString, const std::string& sSeparators, const std::string& sAtomStarts, const std::string& sAtomEnds, STRVECTOR& vsTokens) ; //---------------------------------------------------------------------------- EGN_EXPORT int ReplaceString( std::string& sString, const std::string& sOld, const std::string& sNew) ;