Whitespace fixes
[amule.git] / src / libs / common / TextFile.cpp
blob5c6e94937773a2cb3c434edaa45e9feaeda0da53
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2006-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 //
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
8 // respective authors.
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "TextFile.h"
26 #include "Path.h"
28 #include <wx/textbuf.h>
31 //! The maximum number of chars read at once in GetNextLine
32 const size_t TXTBUF_SIZE = 1024;
35 CTextFile::CTextFile()
40 bool CTextFile::Open(const wxString& path, EOpenMode mode)
42 return Open(CPath(path), mode);
46 bool CTextFile::Open(const CPath& path, EOpenMode mode)
48 // wxFFile doesn't call close itself, but asserts instead.
49 Close();
51 m_mode = mode;
53 if (mode == read) {
54 if (path.FileExists()) {
55 m_file.Open(path.GetRaw(), wxT("r"));
57 } else if (mode == write) {
58 m_file.Open(path.GetRaw(), wxT("w"));
59 } else {
60 wxFAIL;
63 return IsOpened();
67 CTextFile::~CTextFile()
72 bool CTextFile::IsOpened() const
74 return m_file.IsOpened();
78 bool CTextFile::Eof() const
80 // This is needed because feof will crash if the
81 // underlying FILE pointer is NULL, as is the
82 // case when the file is closed.
83 return m_file.IsOpened() ? m_file.Eof() : true;
87 bool CTextFile::Close()
89 return m_file.Close();
93 wxString CTextFile::GetNextLine(EReadTextFile flags, const wxMBConv& conv, bool* result)
95 wxCHECK_MSG(m_file.IsOpened(), wxEmptyString, wxT("Trying to read from closed file."));
96 wxCHECK_MSG(!m_file.Eof(), wxEmptyString, wxT("Trying to read past EOF"));
97 wxCHECK_MSG((m_mode == read), wxEmptyString, wxT("Trying to read from non-readable file."));
99 bool is_filtered = false;
101 wxString line;
102 char buffer[TXTBUF_SIZE];
104 // Loop until EOF (fgets will then return NULL) or a newline is read.
105 while (fgets(buffer, TXTBUF_SIZE, m_file.fp())) {
106 // Filters must be first applied here to avoid unnecessary CPU usage.
108 if (line.IsEmpty()) {
109 if (buffer[0] == '\0') {
110 // Empty line.
111 break;
112 } else if (flags & txtIgnoreComments) {
113 int i = 0;
114 char t = buffer[i];
115 while (t) {
116 if ((t == ' ') || (t == '\t')) {
117 ++i;
118 t = buffer[i];
119 } else {
120 is_filtered = (buffer[i] == '#');
121 break;
127 if (!is_filtered) {
128 // NB: The majority of the time spent by this function is
129 // spent converting the multibyte string to wide-char.
130 line += conv.cMB2WC(buffer);
132 // Remove any newlines, carriage returns, etc.
133 if (line.Length() && (line.Last() == wxT('\n'))) {
134 if ((line.Length() > 1)) {
135 if (line[line.Length() - 2] == wxT('\r')) {
136 // Carriage return + newline
137 line.RemoveLast(2);
138 } else {
139 // Only a newline.
140 line.RemoveLast(1);
142 } else {
143 // Empty line
144 line.Clear();
147 // We've read an entire line.
148 break;
150 } else {
151 // Filtered line.
152 break;
156 if (!is_filtered) {
157 if (flags & txtStripWhitespace) {
158 line = line.Strip(wxString::both);
161 if ((flags & txtIgnoreEmptyLines) && line.IsEmpty()) {
162 is_filtered = true;
166 if (result) {
167 *result = !is_filtered;
170 return line;
174 bool CTextFile::WriteLine(const wxString& line, const wxMBConv& conv)
176 wxCHECK_MSG(m_file.IsOpened(), false, wxT("Trying to read from closed file."));
177 wxCHECK_MSG((m_mode == write), false, wxT("Trying to read from non-readable file."));
179 // Ensures that use of newlines/carriage-returns matches the OS
180 wxString result = wxTextBuffer::Translate(line);
182 // Only add line-breaks between lines, as otherwise the number of
183 // lines would grow as the result of the addition of an empty line,
184 // at the end of the file.
185 if (m_file.Tell() > 0) {
186 result = wxTextBuffer::GetEOL() + result;
189 wxCharBuffer strBuffer = conv.cWC2MB(result);
190 if (strBuffer) {
191 const size_t length = strlen(strBuffer);
193 return (m_file.Write(strBuffer, length) == length);
196 return false;
200 wxArrayString CTextFile::ReadLines(EReadTextFile flags, const wxMBConv& conv)
202 wxArrayString lines;
204 while (!Eof()) {
205 bool result = true;
207 wxString line = GetNextLine(flags, conv, &result);
209 if (result) {
210 lines.Add(line);
214 return lines;
218 bool CTextFile::WriteLines(const wxArrayString& lines, const wxMBConv& conv)
220 bool result = true;
222 for (size_t i = 0; i < lines.GetCount(); ++i) {
223 result &= WriteLine(lines[i], conv);
226 return result;