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
This commit is contained in:
imaginos
2005-05-15 03:05:08 +00:00
parent 47bc3c99f7
commit 8a1b797187
2 changed files with 142 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
#include <string.h>
#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 );
}