From b7f38c4d4a58720b1554f018934bfbb4d28d0b2c Mon Sep 17 00:00:00 2001 From: psychon Date: Tue, 30 Dec 2008 13:05:04 +0000 Subject: [PATCH] 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 --- FileUtils.cpp | 24 +++++++++++++ FileUtils.h | 1 + Utils.h | 81 -------------------------------------------- modules/away.cpp | 28 ++++++++++----- modules/email.cpp | 11 +++--- modules/savebuff.cpp | 49 ++++++++++++++++++--------- modules/watch.cpp | 4 +-- 7 files changed, 86 insertions(+), 112 deletions(-) diff --git a/FileUtils.cpp b/FileUtils.cpp index 054a15f2..5c484d94 100644 --- a/FileUtils.cpp +++ b/FileUtils.cpp @@ -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; diff --git a/FileUtils.h b/FileUtils.h index c8c57386..5d2efcf9 100644 --- a/FileUtils.h +++ b/FileUtils.h @@ -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(); diff --git a/Utils.h b/Utils.h index d834b1e0..550088c9 100644 --- a/Utils.h +++ b/Utils.h @@ -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 diff --git a/modules/away.cpp b/modules/away.cpp index f132546b..5910d97f 100644 --- a/modules/away.cpp +++ b/modules/away.cpp @@ -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); diff --git a/modules/email.cpp b/modules/email.cpp index 0b8c21f0..8c946061 100644 --- a/modules/email.cpp +++ b/modules/email.cpp @@ -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 diff --git a/modules/savebuff.cpp b/modules/savebuff.cpp index 1b09df51..a61f4b6b 100644 --- a/modules/savebuff.cpp +++ b/modules/savebuff.cpp @@ -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); diff --git a/modules/watch.cpp b/modules/watch.cpp index 02cc4e81..2b67fb7d 100644 --- a/modules/watch.cpp +++ b/modules/watch.cpp @@ -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) {