From 8a1b79718791c957542f66a93b4ceb9fe9acdedb Mon Sep 17 00:00:00 2001 From: imaginos Date: Sun, 15 May 2005 03:05:08 +0000 Subject: [PATCH] added MCString, including some filtering capabilities and disk writing git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@298 726aef4b-f618-498e-8847-2d620e286838 --- String.cpp | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ String.h | 25 ++++++++++++ 2 files changed, 142 insertions(+) diff --git a/String.cpp b/String.cpp index 759e441a..c2689298 100644 --- a/String.cpp +++ b/String.cpp @@ -1,5 +1,6 @@ #include #include "String.h" +#include "FileUtils.h" int CString::CaseCmp(const CString& s) const { return strcasecmp(c_str(), s.c_str()); @@ -227,3 +228,119 @@ bool CString::RightChomp(unsigned int uLen) { return bRet; } + +//////////////// MCString //////////////// +int MCString::WriteToDisk( const CString & sPath, mode_t iMode ) +{ + CFile cFile( sPath ); + if ( !cFile.Open( O_WRONLY|O_CREAT|O_TRUNC, iMode ) ) + return( MCS_EOPEN ); + + for( MCString::iterator it = this->begin(); it != this->end(); it++ ) + { + CString sKey = it->first; + CString sValue = it->second; + if ( !WriteFilter( sKey, sValue ) ) + return( MCS_EWRITEFIL ); + + if ( sKey.empty() ) + continue; + + if ( cFile.Write( Encode( sKey ) + " " + Encode( sValue ) + "\n" ) <= 0 ) + return( MCS_EWRITE ); + } + + cFile.Close(); + + return( MCS_SUCCESS ); +} + +int MCString::ReadFromDisk( const CString & sPath, mode_t iMode ) +{ + CFile cFile( sPath ); + if ( !cFile.Open( O_RDONLY|O_CREAT, iMode ) ) + return( MCS_EOPEN ); + + CString sBuffer; + + while( cFile.ReadLine( sBuffer ) ) + { + sBuffer.Trim(); + CString sKey = sBuffer.Token( 0 ); + CString sValue = sBuffer.Token( 1 ); + Decode( sKey ); + Decode( sValue ); + + if ( !ReadFilter( sKey, sValue ) ) + return( MCS_EREADFIL ); + + (*this)[sKey] = sValue; + } + cFile.Close(); + + return( MCS_SUCCESS ); +} + + +static const char hexdigits[] = "0123456789abcdef"; + +CString & MCString::Encode( CString & sValue ) +{ + CString sTmp; + for( CString::iterator it = sValue.begin(); it != sValue.end(); it++ ) + { + if ( isalnum( *it ) ) + sTmp += *it; + else + { + sTmp += "%"; + sTmp += hexdigits[*it >> 4]; + sTmp += hexdigits[*it & 0xf]; + sTmp += ";"; + } + } + sValue = sTmp; + return( sValue ); +} + +CString & MCString::Decode( CString & sValue ) +{ + const char *pTmp = sValue.c_str(); + char *endptr; + CString sTmp; + while( *pTmp ) + { + if ( *pTmp != '%' ) + sTmp += *pTmp++; + else + { + char ch = (char )strtol( ((const char *)pTmp + 1), &endptr, 16 ); + if ( *endptr == ';' ) + { + sTmp += ch; + pTmp = ++endptr; + } + else + { + sTmp += *pTmp++; + } + } + } + + sValue = sTmp; + return( sValue ); +} + + + + + + + + + + + + + + diff --git a/String.h b/String.h index 467a7b29..dfca389e 100644 --- a/String.h +++ b/String.h @@ -3,6 +3,7 @@ #include #include +#include using std::string; using std::stringstream; @@ -61,5 +62,29 @@ private: protected: }; +class MCString : public std::map< CString, CString > { +public: + MCString() : std::map< CString, CString >() {} + virtual ~MCString() {} + + enum + { + MCS_SUCCESS = 0, + MCS_EOPEN = 1, + MCS_EWRITE = 2, + MCS_EWRITEFIL = 3, + MCS_EREADFIL = 4 + }; + + int WriteToDisk( const CString & sPath, mode_t iMode = 0644 ); + int ReadFromDisk( const CString & sPath, mode_t iMode = 0644 ); + + virtual bool WriteFilter( CString & sKey, CString & sValue ) { return( true ); } + virtual bool ReadFilter( CString & sKey, CString & sValue ) { return( true ); } + + //! make them parse safe, right now using hex encoding on anything !isalnum + virtual CString & Encode( CString & sValue ); + virtual CString & Decode( CString & sValue ); +}; #endif // !X_STRING_H