2 // This file is part of the aMule Project.
4 // Copyright (c) 2006-2008 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 return empty lines. Can be combined with txtStripWhiteSpace */
39 txtIgnoreEmptyLines
= 1,
40 /** Do not return lines starting with a '#' */
41 txtIgnoreComments
= 2,
42 /** Strip whitespace from the beginning/end of lines. */
43 txtStripWhitespace
= 4,
45 /** Default parameters for file reading. */
46 txtReadDefault
= txtIgnoreEmptyLines
| txtIgnoreComments
| txtStripWhitespace
53 * This class is a wrapper around wxFFile, letting an text file be read
54 * or written line-by-line. The class provides transparent and automatic
57 * Note that it is not possible to seek in a CTextFile, only sequential
58 * reading or writing is possible. Also note that the maximum length of a
59 * line is fixed (see CTextFile::GetNextLine), however this shouldn't be
60 * a problem, given the uses of this class.
65 // Open modes. Note that these are mutually exclusive!
67 //! Opens the file for reading, if it exists.
69 //! Opens the file for writing, overwriting old contents.
75 /** Destructor. Closes the file if still open. */
78 /** Opens the specified file, returning true on success. */
80 bool Open(const wxString
& path
, EOpenMode mode
);
81 bool Open(const CPath
& path
, EOpenMode mode
);
84 /** Returns true if the file is opened. */
85 bool IsOpened() const;
86 /** Returns true if GetNextLine has reached the end of the file. */
88 /** Closes the file, returning true on success. */
93 * Returns the next line of a readable file.
95 * @param conv The converter used to convert from multibyte to widechar.
97 * Note that GetNextLine will return an empty string if the file has reached
98 * EOF, or if the file is closed, or not readable. However, empty lines in
99 * the file will also be returned, so this cannot be used to test for EOF.
100 * Instead, use the function Eof().
102 wxString
GetNextLine(const wxMBConv
& conv
= wxConvLibc
);
105 * Writes the line to a writable file, returning true on success.
107 * @param conv The converter used to convert from widechar to multibyte.
109 bool WriteLine(const wxString
& line
, const wxMBConv
& conv
= wxConvLibc
);
112 /** Reads and returns the contents of a text-file, using the specifed criteria and converter. */
113 wxArrayString
ReadLines(EReadTextFile flags
= txtReadDefault
, const wxMBConv
& conv
= wxConvLibc
);
115 /** Writes the lines to the file, using the given converter, returning true if no errors occured. */
116 bool WriteLines(const wxArrayString
& lines
, const wxMBConv
& conv
= wxConvLibc
);
119 //! The actual file object.
121 //! The mode in with which the file was opened.
125 #endif /* TEXTFILE_H */