Upstream tarball 10148
[amule.git] / src / libs / common / TextFile.h
blob932f44de9272eab8a1bd64b6ee9fa2f532f722ac
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 #ifndef TEXTFILE_H
26 #define TEXTFILE_H
28 #include <wx/ffile.h>
29 #include <wx/string.h>
30 #include <wx/strconv.h>
33 class CPath;
35 /** Criteria used when reading an entire file to an array of strings. */
36 enum EReadTextFile
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
50 /**
51 * Text file class.
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
55 * EOL-style handling.
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.
62 class CTextFile
64 public:
65 // Open modes. Note that these are mutually exclusive!
66 enum EOpenMode {
67 //! Opens the file for reading, if it exists.
68 read,
69 //! Opens the file for writing, overwriting old contents.
70 write
73 /* Constructor. */
74 CTextFile();
75 /** Destructor. Closes the file if still open. */
76 ~CTextFile();
78 /** Opens the specified file, returning true on success. */
79 //\{
80 bool Open(const wxString& path, EOpenMode mode);
81 bool Open(const CPath& path, EOpenMode mode);
82 //\}
84 /** Returns true if the file is opened. */
85 bool IsOpened() const;
86 /** Returns true if GetNextLine has reached the end of the file. */
87 bool Eof() const;
88 /** Closes the file, returning true on success. */
89 bool Close();
92 /**
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);
104 /**
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);
118 private:
119 //! The actual file object.
120 wxFFile m_file;
121 //! The mode in with which the file was opened.
122 EOpenMode m_mode;
125 #endif /* TEXTFILE_H */