1//
2// This file is part of the aMule Project.
3//
4// Copyright (c) 2006-2011 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.
14//
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
23//
24
25#ifndef TEXTFILE_H
26#define TEXTFILE_H
27
28#include <wx/ffile.h>
29#include <wx/string.h>
30#include <wx/strconv.h>
31
32
33class CPath;
34
35/** Criteria used when reading an entire file to an array of strings. */
36enum EReadTextFile
37{
38	/** Do not filter anything */
39	txtReadAll = 0,
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,
46
47	/** Default parameters for file reading. */
48	txtReadDefault = txtIgnoreEmptyLines | txtIgnoreComments | txtStripWhitespace
49};
50
51
52/**
53 * Text file class.
54 *
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
57 * EOL-style handling.
58 *
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.
63 */
64class CTextFile
65{
66public:
67	// Open modes. Note that these are mutually exclusive!
68	enum EOpenMode {
69		//! Opens the file for reading, if it exists.
70		read,
71		//! Opens the file for writing, overwriting old contents.
72		write
73	};
74
75	/* Constructor. */
76	CTextFile();
77	/** Destructor. Closes the file if still open. */
78	~CTextFile();
79
80	/** Opens the specified file, returning true on success. */
81	//\{
82	bool Open(const wxString& path, EOpenMode mode);
83	bool Open(const CPath& path, EOpenMode mode);
84	//\}
85
86	/** Returns true if the file is opened. */
87	bool		IsOpened() const;
88	/** Returns true if GetNextLine has reached the end of the file. */
89	bool		Eof() const;
90	/** Closes the file, returning true on success. */
91	bool		Close();
92
93
94	/**
95	 * Returns the next line of a readable file.
96	 *
97	 * @param conv The converter used to convert from multibyte to widechar.
98	 *
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().
103	 **/
104	wxString	GetNextLine(EReadTextFile flags = txtReadAll, const wxMBConv& conv = wxConvLibc, bool* result = NULL);
105
106	/**
107	 * Writes the line to a writable file, returning true on success.
108	 *
109	 * @param conv The converter used to convert from widechar to multibyte.
110	 */
111	bool		WriteLine(const wxString& line, const wxMBConv& conv = wxConvLibc);
112
113
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);
116
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);
119
120private:
121	//! The actual file object.
122	wxFFile		m_file;
123	//! The mode in with which the file was opened.
124	EOpenMode	m_mode;
125};
126
127#endif /* TEXTFILE_H */
128