Files
Include/EGnStringUtils.h
T
Dario Sassi 10da4eae93 Include :
- aggiornamenti e aggiunta ImportCNC.
2014-09-18 15:00:32 +00:00

184 lines
7.2 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2013-2014
//----------------------------------------------------------------------------
// File : EgnStringUtils.h Data : 17.03.14 Versione : 1.5c2
// Contenuto : Dichiarazione delle funzioni di utilità per le stringhe.
//
//
//
// Modifiche : 20.11.13 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
#pragma once
#include <algorithm>
#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
//----------------------------------------------------------------------------
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
IsEmptyOrSpaces( const std::string& sName)
{ return ( sName.empty() ||
sName.find_first_not_of( " \t\r\n") == std::string::npos) ; }
//----------------------------------------------------------------------------
inline bool
IsNullOrSpace( char cChar)
{ return ( cChar == '\0' || cChar == ' ' || cChar == '\t' || cChar == '\r' || cChar == '\n') ; }
//----------------------------------------------------------------------------
inline bool
IsValidName( const std::string& sName)
{ if ( sName.empty())
return false ;
return ( sName.find_first_of( "\\/:*?\"<>|", 0) == std::string::npos) ; }
//----------------------------------------------------------------------------
inline bool
ValidateName( std::string& sName)
{ std::string::size_type i ;
while ( ( i = sName.find_first_of( "\\/:*?\"<>|")) != std::string::npos)
sName[i] = '_' ;
return true ; }
//----------------------------------------------------------------------------
inline bool
FromString( const std::string& sVal, int& nVal)
{ const char* pStart = sVal.c_str() ;
char* pStop ;
errno = 0 ;
nVal = strtol( pStart, &pStop, 10) ;
return ( pStop != pStart && 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 = sVal.c_str() ;
char* pStop ;
errno = 0 ;
dVal = strtod( pStart, &pStop) ;
return ( pStop != pStart && errno == 0) ; }
template <size_t size>
bool FromString( const std::string& sVal, int (&nVal)[size])
{ const char* pStart = sVal.c_str() ;
char* pStop ;
errno = 0 ;
for ( int i = 0 ; i < size ; ++ i) {
nVal[i] = strtol( pStart, &pStop, 10) ;
if ( ( i < size - 1 && *pStop != ',') || errno != 0)
return false ;
pStart = pStop + 1 ;
}
return ( errno == 0) ;
}
template <size_t size>
bool FromString( const std::string& sVal, double (&dVal)[size])
{ const char* pStart = sVal.c_str() ;
char* pStop ;
errno = 0 ;
for ( int i = 0 ; i < size ; ++ i) {
dVal[i] = strtod( pStart, &pStop) ;
if ( ( i < size - 1 && *pStop != ',') || errno != 0)
return false ;
pStart = pStop + 1 ;
}
return ( errno == 0) ;
}
//----------------------------------------------------------------------------
inline const std::string
ToString( int nVal, int nPrec = 1)
{
// eseguo conversione
char szBuff[24] ;
int nErr = _itoa_s( nVal, szBuff, 10) ;
// se errore, ritorno stringa opportuna
if ( nErr != 0) {
_ASSERT( 0) ;
return "#Error" ;
}
// verifico lunghezza minima
int nLen = (int) strlen( szBuff) ;
if ( nLen >= nPrec)
return szBuff ;
// porto la stringa alla minima lunghezza
std::string sBuff( szBuff) ;
sBuff.insert( 0, ( nPrec - nLen), '0') ;
return sBuff ;
}
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) ;
//----------------------------------------------------------------------------
EGN_EXPORT const std::string CurrDateTime( void) ;