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