2 // This file is part of the aMule Project.
4 // Copyright (c) 2006-2011 aMule Team ( admin@amule.org / http://www.amule.org )
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
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
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.
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"));
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;
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') {
112 } else if (flags
& txtIgnoreComments
) {
116 if ((t
== ' ') || (t
== '\t')) {
120 is_filtered
= (buffer
[i
] == '#');
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
147 // We've read an entire line.
157 if (flags
& txtStripWhitespace
) {
158 line
= line
.Strip(wxString::both
);
161 if ((flags
& txtIgnoreEmptyLines
) && line
.IsEmpty()) {
167 *result
= !is_filtered
;
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
);
191 const size_t length
= strlen(strBuffer
);
193 return (m_file
.Write(strBuffer
, length
) == length
);
200 wxArrayString
CTextFile::ReadLines(EReadTextFile flags
, const wxMBConv
& conv
)
207 wxString line
= GetNextLine(flags
, conv
, &result
);
218 bool CTextFile::WriteLines(const wxArrayString
& lines
, const wxMBConv
& conv
)
222 for (size_t i
= 0; i
< lines
.GetCount(); ++i
) {
223 result
&= WriteLine(lines
[i
], conv
);