mirror of
https://github.com/znc/znc.git
synced 2026-03-28 17:42:41 +01:00
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
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user