Upstream tarball 10013
[amule.git] / src / libs / common / TextFile.cpp
blob015f38c9ad0096b29250193eb7511da26fddd1f9
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2006-2008 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.
19 //
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(const wxMBConv& conv)
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."));
100 wxString line;
101 char buffer[TXTBUF_SIZE];
103 // Loop until EOF (fgets will then return NULL) or a newline is read.
104 while (fgets(buffer, TXTBUF_SIZE, m_file.fp())) {
105 // NB: The majority of the time spent by this function is
106 // spent converting the multibyte string to wide-char.
107 line += conv.cMB2WC(buffer);
109 // Remove any newlines, carriage returns, etc.
110 if (line.Length() && (line.Last() == wxT('\n'))) {
111 if ((line.Length() > 1)) {
112 if (line[line.Length() - 2] == wxT('\r')) {
113 // Carriage return + newline
114 line.RemoveLast(2);
115 } else {
116 // Only a newline.
117 line.RemoveLast(1);
119 } else {
120 // Empty line
121 line.Clear();
124 // We've read an entire line.
125 break;
129 return line;
133 bool CTextFile::WriteLine(const wxString& line, const wxMBConv& conv)
135 wxCHECK_MSG(m_file.IsOpened(), false, wxT("Trying to read from closed file."));
136 wxCHECK_MSG((m_mode == write), false, wxT("Trying to read from non-readable file."));
138 // Ensures that use of newlines/carriage-returns matches the OS
139 wxString result = wxTextBuffer::Translate(line);
141 // Only add line-breaks between lines, as otherwise the number of
142 // lines would grow as the result of the addition of an empty line,
143 // at the end of the file.
144 if (m_file.Tell() > 0) {
145 result = wxTextBuffer::GetEOL() + result;
148 wxCharBuffer strBuffer = conv.cWC2MB(result);
149 if (strBuffer) {
150 const size_t length = strlen(strBuffer);
152 return (m_file.Write(strBuffer, length) == length);
155 return false;
159 wxArrayString CTextFile::ReadLines(EReadTextFile flags, const wxMBConv& conv)
161 wxArrayString lines;
163 while (!Eof()) {
164 wxString line = GetNextLine(conv);
166 if (flags & txtStripWhitespace) {
167 line = line.Strip(wxString::both);
170 if (flags & txtIgnoreEmptyLines) {
171 if (line.IsEmpty()) {
172 continue;
176 if (flags & txtIgnoreComments) {
177 if (flags & txtStripWhitespace) {
178 if (line.StartsWith(wxT("#"))) {
179 continue;
181 } else if (line.Strip(wxString::leading).StartsWith(wxT("#"))) {
182 continue;
186 lines.Add(line);
189 return lines;
193 bool CTextFile::WriteLines(const wxArrayString& lines, const wxMBConv& conv)
195 bool result = true;
197 for (size_t i = 0; i < lines.GetCount(); ++i) {
198 result &= WriteLine(lines[i], conv);
201 return result;