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