Remove some code duplications and move functions into classes

This removes ReadFile(), WriteFile(), ReadLine(), Lower() and Upper() from
Utils.h and adds CFile::ReadFile().

The biggest part of this patch fixes modules to use CFile and CString instead
of these global functions.


git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1311 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
psychon
2008-12-30 13:05:04 +00:00
parent fdb451a908
commit b7f38c4d4a
7 changed files with 86 additions and 112 deletions
+24
View File
@@ -326,6 +326,30 @@ bool CFile::ReadLine(CString& sData, const CString & sDelimiter) {
return false;
}
bool CFile::ReadFile(CString& sData, size_t iMaxSize) {
char buff[4096];
size_t iBytesRead = 0;
sData.clear();
while (iBytesRead < iMaxSize) {
int iBytes = Read(buff, sizeof(buff));
if (iBytes < 0)
// Error
return false;
if (iBytes == 0)
// EOF
return true;
sData.append(buff, iBytes);
}
// Buffer limit reached
return false;
}
int CFile::Write(const char *pszBuffer, u_int iBytes) {
if (m_iFD == -1) {
return -1;
+1
View File
@@ -112,6 +112,7 @@ public:
bool Open(int iFlags, mode_t iMode = 0644);
int Read(char *pszBuffer, int iBytes);
bool ReadLine(CString & sData, const CString & sDelimiter = "\n");
bool ReadFile(CString& sData, size_t iMaxSize = 512 * 1024);
int Write(const char *pszBuffer, u_int iBytes);
int Write(const CString & sData);
void Close();
-81
View File
@@ -258,87 +258,6 @@ private:
#endif /* HAVE_LIBSSL */
#define RF_BUFF 4096
inline bool ReadFile(const CString & sFilename, CString & sLine) {
char inbuff[RF_BUFF];
int bytes;
// clear ourselves out
sLine.clear();
FILE *f = fopen(sFilename.c_str(), "r");
if (!f) {
return false;
}
while ((bytes = fread(inbuff, sizeof(char), RF_BUFF, f)) > 0) {
sLine.append(inbuff, bytes);
}
fclose(f);
if (bytes < 0) {
return false;
}
return true;
}
inline bool WriteFile(const CString & sFilename, const CString & sData) {
FILE *f = fopen(sFilename.c_str(), "w");
if (!f) {
return false;
}
int iRet = fwrite(sData.data(), sizeof(char), sData.length(), f);
fclose(f);
if (iRet <= 0) {
return false;
}
return true;
}
inline bool ReadLine(const CString & sData, CString & sLine, CString::size_type & iPos) {
sLine.clear();
if (iPos >= sData.length()) {
return false;
}
CString::size_type iFind = sData.find("\n", iPos);
if (iFind == CString::npos) {
sLine = sData.substr(iPos, (sData.length() - iPos));
iPos = CString::npos;
return true;
}
sLine = sData.substr(iPos, (iFind - iPos)) + "\n";
iPos = iFind + 1;
return true;
}
inline CString Lower(const CString & sLine) {
CString sRet;
for (u_int a = 0; a < sLine.length(); a++) {
sRet += tolower(sLine[a]);
}
return sRet;
}
inline CString Upper(const CString & sLine) {
CString sRet;
for (u_int a = 0; a < sLine.length(); a++) {
sRet += toupper(sLine[a]);
}
return sRet;
}
/**
* @class TCacheMap
* @author prozac <prozac@rottenboy.com>
+19 -9
View File
@@ -101,15 +101,17 @@ public:
CString sFile;
if (DecryptMessages(sFile))
{
CString sLine;
CString::size_type iPos = 0;
while (ReadLine(sFile, sLine, iPos))
{
VCString vsLines;
VCString::iterator it;
sFile.Split("\n", vsLines);
for (it = vsLines.begin(); it != vsLines.end(); it++) {
CString sLine(*it);
sLine.Trim();
AddMessage(sLine);
}
} else
{
} else {
m_sPassword = "";
CUtils::PrintError("[" + GetModName() + ".so] Failed to Decrypt Messages");
return(false);
@@ -132,8 +134,12 @@ public:
CString sPath = GetPath();
if (!sPath.empty())
{
WriteFile(sPath, sFile);
chmod(sPath.c_str(), 0600);
CFile File(sPath);
if (File.Open(O_WRONLY | O_CREAT | O_TRUNC, 0600)) {
File.Chmod(0600);
File.Write(sFile);
}
File.Close();
}
}
}
@@ -379,12 +385,16 @@ private:
CString sFile;
sBuffer = "";
if ((sMessages.empty()) || (!ReadFile(sMessages, sFile)))
CFile File(sMessages);
if (sMessages.empty() || !File.Open(O_RDONLY) || !File.ReadFile(sFile))
{
PutModule("Unable to find buffer");
return(true); // gonna be successful here
}
File.Close();
if (!sFile.empty())
{
CBlowfish c(m_sPassword, BF_DECRYPT);
+7 -4
View File
@@ -169,10 +169,13 @@ public:
{
EmailST tmp;
tmp.sUidl = (char *)CMD5(m_sMailBuffer.substr(0, 255));
CString sLine;
CString::size_type iPos = 0;
while (::ReadLine(m_sMailBuffer, sLine, iPos))
{
VCString vsLines;
VCString::iterator it;
m_sMailBuffer.Split("\n", vsLines);
for (it = vsLines.begin(); it != vsLines.end(); it++) {
CString sLine(*it);
sLine.Trim();
if (sLine.empty())
break; // out of the headers
+33 -16
View File
@@ -103,10 +103,13 @@ public:
if (!pChan->GetBuffer().empty())
return(true); // reloaded a module probably in this case, so just verify we can decrypt the file
CString sLine;
CString::size_type iPos = 0;
while (ReadLine(sFile, sLine, iPos))
{
VCString vsLines;
VCString::iterator it;
sFile.Split("\n", vsLines);
for (it = vsLines.begin(); it != vsLines.end(); it++) {
CString sLine(*it);
sLine.Trim();
pChan->AddBuffer(sLine);
}
@@ -150,8 +153,12 @@ public:
CString sPath = GetPath(vChans[a]->GetName());
if (!sPath.empty())
{
WriteFile(sPath, sFile);
chmod(sPath.c_str(), 0600);
CFile File(sPath);
if (File.Open(O_WRONLY | O_CREAT | O_TRUNC, 0600)) {
File.Chmod(0600);
File.Write(sFile);
}
File.Close();
}
}
}
@@ -172,10 +179,13 @@ public:
CString sFile;
if (DecryptChannel(sArgs, sFile))
{
CString sLine;
CString::size_type iPos = 0;
while (ReadLine(sFile, sLine, iPos))
{
VCString vsLines;
VCString::iterator it;
sFile.Split("\n", vsLines);
for (it = vsLines.begin(); it != vsLines.end(); it++) {
CString sLine(*it);
sLine.Trim();
PutModule("[" + sLine + "]");
}
@@ -200,10 +210,13 @@ public:
PutUser(":***!znc@znc.in PRIVMSG " + sChan + " :Buffer Playback...");
if (DecryptChannel(sChan, sFile))
{
CString sLine;
CString::size_type iPos = 0;
while (ReadLine(sFile, sLine, iPos))
{
VCString vsLines;
VCString::iterator it;
sFile.Split("\n", vsLines);
for (it = vsLines.begin(); it != vsLines.end(); it++) {
CString sLine(*it);
sLine.Trim();
PutUser(sLine);
}
@@ -213,7 +226,7 @@ public:
CString GetPath(const CString & sChannel)
{
CString sBuffer = m_pUser->GetUserName() + Lower(sChannel);
CString sBuffer = m_pUser->GetUserName() + sChannel.AsLower();
CString sRet = GetSavePath();
sRet += "/" + CBlowfish::MD5(sBuffer, true);
return(sRet);
@@ -289,9 +302,13 @@ private:
CString sFile;
sBuffer = "";
if ((sChannel.empty()) || (!ReadFile(sChannel, sFile)))
CFile File(sChannel);
if (sChannel.empty() || !File.Open(O_RDONLY) || !File.ReadFile(sFile))
return(true); // gonna be successful here
File.Close();
if (!sFile.empty())
{
CBlowfish c(m_sPassword, BF_DECRYPT);
+2 -2
View File
@@ -70,7 +70,7 @@ public:
for (unsigned int a = 0; a < m_vsSources.size(); a++) {
const CWatchSource& WatchSource = m_vsSources[a];
if (sSource.AsLower().WildCmp(Lower(WatchSource.GetSource()))) {
if (sSource.AsLower().WildCmp(WatchSource.GetSource().AsLower())) {
if (WatchSource.IsNegated()) {
return false;
} else {
@@ -80,7 +80,7 @@ public:
}
}
return (bGoodSource && Nick.GetHostMask().AsLower().WildCmp(Lower(m_sHostMask))) && sText.AsLower().WildCmp(Lower(m_sPattern));
return (bGoodSource && Nick.GetHostMask().AsLower().WildCmp(m_sHostMask.AsLower())) && sText.AsLower().WildCmp(m_sPattern.AsLower());
}
bool operator ==(const CWatchEntry& WatchEntry) {