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
29 #include <wx/string.h>
30 #include <wx/strconv.h>
35 /** Criteria used when reading an entire file to an array of strings. */
38 /** Do not filter anything */
40 /** Do not return empty lines. Can be combined with txtStripWhiteSpace */
41 txtIgnoreEmptyLines
= 1,
42 /** Do not return lines starting with a '#' */
43 txtIgnoreComments
= 2,
44 /** Strip whitespace from the beginning/end of lines. */
45 txtStripWhitespace
= 4,
47 /** Default parameters for file reading. */
48 txtReadDefault
= txtIgnoreEmptyLines
| txtIgnoreComments
| txtStripWhitespace
55 * This class is a wrapper around wxFFile, letting an text file be read
56 * or written line-by-line. The class provides transparent and automatic
59 * Note that it is not possible to seek in a CTextFile, only sequential
60 * reading or writing is possible. Also note that the maximum length of a
61 * line is fixed (see CTextFile::GetNextLine), however this shouldn't be
62 * a problem, given the uses of this class.
67 // Open modes. Note that these are mutually exclusive!
69 //! Opens the file for reading, if it exists.
71 //! Opens the file for writing, overwriting old contents.
77 /** Destructor. Closes the file if still open. */
80 /** Opens the specified file, returning true on success. */
82 bool Open(const wxString
& path
, EOpenMode mode
);
83 bool Open(const CPath
& path
, EOpenMode mode
);
86 /** Returns true if the file is opened. */
87 bool IsOpened() const;
88 /** Returns true if GetNextLine has reached the end of the file. */
90 /** Closes the file, returning true on success. */
95 * Returns the next line of a readable file.
97 * @param conv The converter used to convert from multibyte to widechar.
99 * Note that GetNextLine will return an empty string if the file has reached
100 * EOF, or if the file is closed, or not readable. However, empty lines in
101 * the file will also be returned unless otherwise specified, so this cannot be used to test for EOF.
102 * Instead, use the function Eof().
104 wxString
GetNextLine(EReadTextFile flags
= txtReadAll
, const wxMBConv
& conv
= wxConvLibc
, bool* result
= NULL
);
107 * Writes the line to a writable file, returning true on success.
109 * @param conv The converter used to convert from widechar to multibyte.
111 bool WriteLine(const wxString
& line
, const wxMBConv
& conv
= wxConvLibc
);
114 /** Reads and returns the contents of a text-file, using the specifed criteria and converter. */
115 wxArrayString
ReadLines(EReadTextFile flags
= txtReadDefault
, const wxMBConv
& conv
= wxConvLibc
);
117 /** Writes the lines to the file, using the given converter, returning true if no errors occured. */
118 bool WriteLines(const wxArrayString
& lines
, const wxMBConv
& conv
= wxConvLibc
);
121 //! The actual file object.
123 //! The mode in with which the file was opened.
127 #endif /* TEXTFILE_H */