From 296fc08a0bcd0a6289120b44a103b8561721dcc2 Mon Sep 17 00:00:00 2001 From: upstream svn Date: Sun, 16 May 2010 11:54:23 +0000 Subject: [PATCH] Upstream tarball 10173 --- .svn-revision | 2 +- src/IPFilter.cpp | 23 ++++------ src/libs/common/TextFile.cpp | 103 +++++++++++++++++++++++++++---------------- src/libs/common/TextFile.h | 6 ++- 4 files changed, 77 insertions(+), 57 deletions(-) diff --git a/.svn-revision b/.svn-revision index 3d79c658..76cea4a4 100644 --- a/.svn-revision +++ b/.svn-revision @@ -1 +1 @@ -10171 +10173 diff --git a/src/IPFilter.cpp b/src/IPFilter.cpp index bad25315..b433a9ff 100644 --- a/src/IPFilter.cpp +++ b/src/IPFilter.cpp @@ -345,17 +345,14 @@ private: */ bool ProcessAntiP2PLine(const wxString& sLine) { - // remove spaces from the left and right. - const wxString line = sLine.Strip(wxString::leading); - // Extract description (first) and IP-range (second) form the line - int pos = line.Find(wxT(':'), true); + int pos = sLine.Find(wxT(':'), true); if (pos == -1) { return false; } - wxString Description = line.Left(pos).Strip(wxString::trailing); - wxString IPRange = line.Right(line.Len() - pos - 1); + wxString Description = sLine.Left(pos).Strip(wxString::trailing); + wxString IPRange = sLine.Right(sLine.Len() - pos - 1); // Convert string IP's to host order IP numbers uint32 IPStart = 0; @@ -417,10 +414,10 @@ private: return 0; } - wxString line = readFile.GetNextLine(); + bool result = true; + wxString line = readFile.GetNextLine(txtReadDefault /*Ignores comments, empty and whitespaces*/, wxConvLibc, &result); - // Comments and empty lines are ignored as soon as possible - if (!line.IsEmpty() && !line.StartsWith(wxT("#"))) { + if (result) { bool processed_ok = false; if (func) { @@ -436,12 +433,8 @@ private: if (processed_ok) { filtercount++; } else { - // It may be a padded comment or line with just spaces. - line = line.Strip(wxString::both); - if (!line.IsEmpty() && !line.StartsWith(wxT("#"))) { - discardedCount++; - AddDebugLogLineM(false, logIPFilter, wxT("Invalid line found while reading ipfilter file: ") + line); - } + discardedCount++; + AddDebugLogLineM(false, logIPFilter, wxT("Invalid line found while reading ipfilter file: ") + line); } } } diff --git a/src/libs/common/TextFile.cpp b/src/libs/common/TextFile.cpp index 015f38c9..a390b80d 100644 --- a/src/libs/common/TextFile.cpp +++ b/src/libs/common/TextFile.cpp @@ -90,42 +90,83 @@ bool CTextFile::Close() } -wxString CTextFile::GetNextLine(const wxMBConv& conv) +wxString CTextFile::GetNextLine(EReadTextFile flags, const wxMBConv& conv, bool* result) { wxCHECK_MSG(m_file.IsOpened(), wxEmptyString, wxT("Trying to read from closed file.")); wxCHECK_MSG(!m_file.Eof(), wxEmptyString, wxT("Trying to read past EOF")); wxCHECK_MSG((m_mode == read), wxEmptyString, wxT("Trying to read from non-readable file.")); + bool is_filtered = false; wxString line; char buffer[TXTBUF_SIZE]; // Loop until EOF (fgets will then return NULL) or a newline is read. while (fgets(buffer, TXTBUF_SIZE, m_file.fp())) { - // NB: The majority of the time spent by this function is - // spent converting the multibyte string to wide-char. - line += conv.cMB2WC(buffer); - - // Remove any newlines, carriage returns, etc. - if (line.Length() && (line.Last() == wxT('\n'))) { - if ((line.Length() > 1)) { - if (line[line.Length() - 2] == wxT('\r')) { - // Carriage return + newline - line.RemoveLast(2); - } else { - // Only a newline. - line.RemoveLast(1); + // Filters must be first applied here to avoid unnecessary CPU usage. + + if (line.IsEmpty()) { + if (buffer[0] == '\0') { + // Empty line. + break; + } else if (flags & txtIgnoreComments) { + int i = 0; + char t = buffer[i]; + while (t) { + if ((t == ' ') || (t == '\t')) { + ++i; + t = buffer[i]; + } else { + is_filtered = (buffer[i] == '#'); + break; + } } - } else { - // Empty line - line.Clear(); } + } - // We've read an entire line. + if (!is_filtered) { + // NB: The majority of the time spent by this function is + // spent converting the multibyte string to wide-char. + line += conv.cMB2WC(buffer); + + // Remove any newlines, carriage returns, etc. + if (line.Length() && (line.Last() == wxT('\n'))) { + if ((line.Length() > 1)) { + if (line[line.Length() - 2] == wxT('\r')) { + // Carriage return + newline + line.RemoveLast(2); + } else { + // Only a newline. + line.RemoveLast(1); + } + } else { + // Empty line + line.Clear(); + } + + // We've read an entire line. + break; + } + } else { + // Filtered line. break; } } + if (!is_filtered) { + if (flags & txtStripWhitespace) { + line = line.Strip(wxString::both); + } + + if ((flags & txtIgnoreEmptyLines) && line.IsEmpty()) { + is_filtered = true; + } + } + + if (result) { + *result = !is_filtered; + } + return line; } @@ -161,29 +202,13 @@ wxArrayString CTextFile::ReadLines(EReadTextFile flags, const wxMBConv& conv) wxArrayString lines; while (!Eof()) { - wxString line = GetNextLine(conv); - - if (flags & txtStripWhitespace) { - line = line.Strip(wxString::both); - } - - if (flags & txtIgnoreEmptyLines) { - if (line.IsEmpty()) { - continue; - } - } + bool result = true; - if (flags & txtIgnoreComments) { - if (flags & txtStripWhitespace) { - if (line.StartsWith(wxT("#"))) { - continue; - } - } else if (line.Strip(wxString::leading).StartsWith(wxT("#"))) { - continue; - } - } + wxString line = GetNextLine(flags, conv, &result); - lines.Add(line); + if (result) { + lines.Add(line); + } } return lines; diff --git a/src/libs/common/TextFile.h b/src/libs/common/TextFile.h index 932f44de..915c0704 100644 --- a/src/libs/common/TextFile.h +++ b/src/libs/common/TextFile.h @@ -35,6 +35,8 @@ class CPath; /** Criteria used when reading an entire file to an array of strings. */ enum EReadTextFile { + /** Do not filter anything */ + txtReadAll = 0, /** Do not return empty lines. Can be combined with txtStripWhiteSpace */ txtIgnoreEmptyLines = 1, /** Do not return lines starting with a '#' */ @@ -96,10 +98,10 @@ public: * * Note that GetNextLine will return an empty string if the file has reached * EOF, or if the file is closed, or not readable. However, empty lines in - * the file will also be returned, so this cannot be used to test for EOF. + * the file will also be returned unless otherwise specified, so this cannot be used to test for EOF. * Instead, use the function Eof(). **/ - wxString GetNextLine(const wxMBConv& conv = wxConvLibc); + wxString GetNextLine(EReadTextFile flags = txtReadAll, const wxMBConv& conv = wxConvLibc, bool* result = NULL); /** * Writes the line to a writable file, returning true on success. -- 2.11.4.GIT