From 751f267f30b963d7592a0e70ebcf51ab87e740fa Mon Sep 17 00:00:00 2001 From: psychon Date: Sun, 28 Dec 2008 13:03:43 +0000 Subject: [PATCH] Improve CFile::ReadLine() a little IMHO it is now a little clearer how this function works and it might be a little faster. Biggest change is that we now read 4k of the file at once instead of reading it in 64byte chunks. I doubt that this causes a lot more memory usage, because CFile instances usually dont live for long, but it should really lower the number of syscalls we need for reading a file. git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1306 726aef4b-f618-498e-8847-2d620e286838 --- FileUtils.cpp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/FileUtils.cpp b/FileUtils.cpp index 4a171101..054a15f2 100644 --- a/FileUtils.cpp +++ b/FileUtils.cpp @@ -278,8 +278,7 @@ int CFile::Read(char *pszBuffer, int iBytes) { } bool CFile::ReadLine(CString& sData, const CString & sDelimiter) { - char buff[64]; - sData.clear(); + char buff[4096]; if (m_iFD == -1) { return false; @@ -287,16 +286,16 @@ bool CFile::ReadLine(CString& sData, const CString & sDelimiter) { bool bEOF = false; - while (true) { + while (!bEOF) { CString::size_type iFind = m_sBuffer.find(sDelimiter); if (iFind != CString::npos) { + // We found a line, return it sData = m_sBuffer.substr(0, (iFind + 1)); m_sBuffer.erase(0, (iFind + 1)); - break; + return true; } - memset((char *)buff, '\0', 64); - int iBytes = read(m_iFD, buff, 64); + int iBytes = read(m_iFD, buff, sizeof(buff)); switch(iBytes) { case -1: { @@ -312,24 +311,19 @@ bool CFile::ReadLine(CString& sData, const CString & sDelimiter) { break; } } - - if (bEOF) { - break; - } } - CString::size_type iFind = m_sBuffer.find(sDelimiter); - if (iFind != CString::npos) { - return true; - } + // We are at the end of the file or an error happened - if (bEOF && m_sBuffer.size()) { + if (m_sBuffer.size()) { + // ..but there is still some partial line in the buffer sData = m_sBuffer; m_sBuffer.clear(); return true; } - return !bEOF; + // Nothing left for reading :( + return false; } int CFile::Write(const char *pszBuffer, u_int iBytes) {