]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/textfile.h
Trying to hide evidence of my negative programming skills...
[wxWidgets.git] / include / wx / textfile.h
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: textfile.h
3// Purpose: class wxTextFile to work with text files of _small_ size
4// (file is fully loaded in memory) and which understands CR/LF
5// differences between platforms.
6// Author: Vadim Zeitlin
7// Modified by:
8// Created: 03.04.98
9// RCS-ID: $Id$
10// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
11// Licence: wxWindows license
12///////////////////////////////////////////////////////////////////////////////
13
14#ifndef _WX_TEXTFILE_H
15#define _WX_TEXTFILE_H
16
17#ifdef __GNUG__
18 #pragma interface "textfile.h"
19#endif
20
21#include "wx/defs.h"
22
23// ----------------------------------------------------------------------------
24// constants
25// ----------------------------------------------------------------------------
26
27// NB: this is always defined, even if !wxUSE_TEXTFILE
28
29// the line termination type
30enum wxTextFileType
31{
32 wxTextFileType_None, // incomplete (the last line of the file only)
33 wxTextFileType_Unix, // line is terminated with 'LF' = 0xA = 10 = '\n'
34 wxTextFileType_Dos, // 'CR' 'LF'
35 wxTextFileType_Mac, // 'CR' = 0xD = 13 = '\r'
36 wxTextFileType_Os2 // 'CR' 'LF'
37};
38
39#if wxUSE_TEXTFILE
40
41#include "wx/string.h"
42#include "wx/file.h"
43#include "wx/dynarray.h"
44
45// ----------------------------------------------------------------------------
46// wxTextFile
47// ----------------------------------------------------------------------------
48
49WX_DEFINE_EXPORTED_ARRAY(wxTextFileType, ArrayFileType);
50
51class WXDLLEXPORT wxTextFile
52{
53public:
54 // constants and static functions
55 // default type for current platform (determined at compile time)
56 static const wxTextFileType typeDefault;
57
58 // this function returns a string which is identical to "text" passed in
59 // except that the line terminator characters are changed to correspond the
60 // given type. Called with the default argument, the function translates
61 // the string to the native format (Unix for Unix, DOS for Windows, ...).
62 static wxString Translate(const wxString& text,
63 wxTextFileType type = typeDefault);
64
65 // get the file termination string
66 static const wxChar *GetEOL(wxTextFileType type = typeDefault);
67
68 // ctors
69 // def ctor, use Open(string)
70 wxTextFile() { }
71 //
72 wxTextFile(const wxString& strFile);
73
74 // file operations
75 // file exists?
76 bool Exists() const;
77 // create the file if it doesn't already exist
78 bool Create();
79 // same as Create() but with (another) file name
80 bool Create(const wxString& strFile);
81 // Open() also loads file in memory on success
82 bool Open();
83 // same as Open() but with (another) file name
84 bool Open(const wxString& strFile);
85 // closes the file and frees memory, losing all changes
86 bool Close();
87 // is file currently opened?
88 bool IsOpened() const { return m_isOpened; }
89
90 // accessors
91 // get the number of lines in the file
92 size_t GetLineCount() const { return m_aLines.Count(); }
93 // the returned line may be modified (but don't add CR/LF at the end!)
94 wxString& GetLine(size_t n) const { return m_aLines[n]; }
95 wxString& operator[](size_t n) const { return m_aLines[n]; }
96
97 // the current line has meaning only when you're using
98 // GetFirstLine()/GetNextLine() functions, it doesn't get updated when
99 // you're using "direct access" i.e. GetLine()
100 size_t GetCurrentLine() const { return m_nCurLine; }
101 void GoToLine(size_t n) { m_nCurLine = n; }
102 bool Eof() const { return (m_aLines.Count() == 0 || m_nCurLine == m_aLines.Count() - 1); }
103
104 // these methods allow more "iterator-like" traversal of the list of
105 // lines, i.e. you may write something like:
106 // for ( str = GetFirstLine(); !Eof(); str = GetNextLine() ) { ... }
107
108 // NB: const is commented out because not all compilers understand
109 // 'mutable' keyword yet (m_nCurLine should be mutable)
110 wxString& GetFirstLine() /* const */ { return m_aLines[m_nCurLine = 0]; }
111 wxString& GetNextLine() /* const */ { return m_aLines[++m_nCurLine]; }
112 wxString& GetPrevLine() /* const */
113 { wxASSERT(m_nCurLine > 0); return m_aLines[--m_nCurLine]; }
114 wxString& GetLastLine() /* const */
115 { return m_aLines[m_nCurLine = m_aLines.Count() - 1]; }
116
117 // get the type of the line (see also GetEOL)
118 wxTextFileType GetLineType(size_t n) const { return m_aTypes[n]; }
119 // guess the type of file (m_file is supposed to be opened)
120 wxTextFileType GuessType() const;
121 // get the name of the file
122 const wxChar *GetName() const { return m_strFile.c_str(); }
123
124 // add/remove lines
125 // add a line to the end
126 void AddLine(const wxString& str, wxTextFileType type = typeDefault)
127 { m_aLines.Add(str); m_aTypes.Add(type); }
128 // insert a line before the line number n
129 void InsertLine(const wxString& str,
130 size_t n,
131 wxTextFileType type = typeDefault)
132 { m_aLines.Insert(str, n); m_aTypes.Insert(type, n); }
133 // delete one line
134 void RemoveLine(size_t n) { m_aLines.RemoveAt(n); m_aTypes.RemoveAt(n); }
135
136 // change the file on disk (default argument means "don't change type")
137 // possibly in another format
138 bool Write(wxTextFileType typeNew = wxTextFileType_None);
139
140 // dtor
141 ~wxTextFile();
142
143private:
144 // copy ctor/assignment operator not implemented
145 wxTextFile(const wxTextFile&);
146 wxTextFile& operator=(const wxTextFile&);
147
148 // read the file in memory (m_file is supposed to be just opened)
149 bool Read();
150
151 wxFile m_file; // current file
152
153 ArrayFileType m_aTypes; // type of each line
154 wxArrayString m_aLines; // lines of file
155
156 size_t m_nCurLine; // number of current line in the file
157
158 bool m_isOpened; // was the file successfully opened the last time?
159
160 wxString m_strFile; // name of the file
161};
162
163#else // !wxUSE_TEXTFILE
164
165// these static wxTextFile methods are used internally by wxWindows, so should
166// be defined even if we're compiling without wxTextFile at all.
167
168class WXDLLEXPORT wxTextFile
169{
170public:
171 // default type for current platform (determined at compile time)
172 static const wxTextFileType typeDefault;
173
174 // this function returns a string which is identical to "text" passed in
175 // except that the line terminator characters are changed to correspond the
176 // given type. Called with the default argument, the function translates
177 // the string to the native format (Unix for Unix, DOS for Windows, ...).
178 static wxString Translate(const wxString& text,
179 wxTextFileType type = typeDefault);
180
181 // get the file termination string
182 static const wxChar *GetEOL(wxTextFileType type = typeDefault);
183
184private:
185 // copy ctor/assignment operator not implemented
186 wxTextFile(const wxTextFile&);
187 wxTextFile& operator=(const wxTextFile&);
188
189 // suppress the gcc warning: 'class defines only private constructors and
190 // has no friends'
191#ifdef __GNUG__
192 friend class wxTextFileDummyFriend;
193#endif // gcc
194};
195
196#endif // wxUSE_TEXTFILE
197
198#endif // _WX_TEXTFILE_H
199