]>
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 | ||
ce4169a4 RR |
14 | #ifndef _TEXTFILE_H |
15 | #define _TEXTFILE_H | |
c801d85f KB |
16 | |
17 | #ifdef __GNUG__ | |
18 | #pragma interface "textfile.h" | |
19 | #endif | |
20 | ||
21 | #include "wx/defs.h" | |
ce4169a4 RR |
22 | |
23 | #if wxUSE_TEXTFILE && wxUSE_FILE | |
24 | ||
c801d85f KB |
25 | #include "wx/string.h" |
26 | #include "wx/file.h" | |
27 | #include "wx/dynarray.h" | |
28 | ||
29 | // ---------------------------------------------------------------------------- | |
30 | // wxTextFile | |
31 | // ---------------------------------------------------------------------------- | |
c2389495 VZ |
32 | |
33 | // the line termination type | |
34 | enum wxTextFileType | |
35 | { | |
36 | wxTextFileType_None, // incomplete (the last line of the file only) | |
37 | wxTextFileType_Unix, // line is terminated with 'LF' = 0xA = 10 = '\n' | |
38 | wxTextFileType_Dos, // 'CR' 'LF' | |
39 | wxTextFileType_Mac // 'CR' = 0xD = 13 = '\r' | |
40 | }; | |
41 | ||
42 | WX_DEFINE_ARRAY(wxTextFileType, ArrayFileType); | |
43 | ||
c801d85f KB |
44 | class wxTextFile |
45 | { | |
46 | public: | |
c801d85f | 47 | // default type for current platform (determined at compile time) |
c2389495 | 48 | static const wxTextFileType typeDefault; |
c801d85f KB |
49 | |
50 | // ctors | |
51 | // def ctor, use Open(string) | |
52 | wxTextFile() { } | |
f42d2aba | 53 | // |
c801d85f KB |
54 | wxTextFile(const wxString& strFile); |
55 | ||
56 | // file operations | |
57 | // file exists? | |
58 | bool Exists() const; | |
59 | // Open() also loads file in memory on success | |
60 | bool Open(); | |
61 | // same as Open() but with (another) file name | |
62 | bool Open(const wxString& strFile); | |
63 | // closes the file and frees memory, losing all changes | |
64 | bool Close(); | |
65 | // is file currently opened? | |
66 | bool IsOpened() const { return m_file.IsOpened(); } | |
67 | ||
68 | // accessors | |
69 | // get the number of lines in the file | |
be6bf94b | 70 | size_t GetLineCount() const { return m_aLines.Count(); } |
c801d85f | 71 | // the returned line may be modified (but don't add CR/LF at the end!) |
c86f1403 VZ |
72 | wxString& GetLine(size_t n) const { return m_aLines[n]; } |
73 | wxString& operator[](size_t n) const { return m_aLines[n]; } | |
be6bf94b VZ |
74 | |
75 | // the current line has meaning only when you're using | |
76 | // GetFirstLine()/GetNextLine() functions, it doesn't get updated when | |
77 | // you're using "direct access" i.e. GetLine() | |
78 | size_t GetCurrentLine() const { return m_nCurLine; } | |
79 | void GoToLine(size_t n) { m_nCurLine = n; } | |
d79b79b5 | 80 | bool Eof() const { return m_nCurLine == m_aLines.Count() - 1; } |
be6bf94b VZ |
81 | |
82 | // these methods allow more "iterator-like" traversal of the list of | |
83 | // lines, i.e. you may write something like: | |
84 | // for ( str = GetFirstLine(); !Eof(); str = GetNextLine() ) { ... } | |
85 | ||
f42d2aba | 86 | // NB: const is commented out because not all compilers understand |
be6bf94b VZ |
87 | // 'mutable' keyword yet (m_nCurLine should be mutable) |
88 | wxString& GetFirstLine() /* const */ { return m_aLines[m_nCurLine = 0]; } | |
89 | wxString& GetNextLine() /* const */ { return m_aLines[++m_nCurLine]; } | |
90 | wxString& GetPrevLine() /* const */ | |
91 | { wxASSERT(m_nCurLine > 0); return m_aLines[--m_nCurLine]; } | |
92 | wxString& GetLastLine() /* const */ | |
93 | { return m_aLines[m_nCurLine = m_aLines.Count() - 1]; } | |
94 | ||
c801d85f | 95 | // get the type of the line (see also GetEOL) |
c2389495 | 96 | wxTextFileType GetLineType(size_t n) const { return m_aTypes[n]; } |
c801d85f | 97 | // guess the type of file (m_file is supposed to be opened) |
c2389495 | 98 | wxTextFileType GuessType() const; |
c801d85f | 99 | // get the name of the file |
9d2f3c71 | 100 | const wxChar *GetName() const { return m_strFile.c_str(); } |
c801d85f KB |
101 | |
102 | // add/remove lines | |
103 | // add a line to the end | |
f42d2aba | 104 | void AddLine(const wxString& str, wxTextFileType type = typeDefault) |
c801d85f KB |
105 | { m_aLines.Add(str); m_aTypes.Add(type); } |
106 | // insert a line before the line number n | |
c2389495 VZ |
107 | void InsertLine(const wxString& str, |
108 | size_t n, | |
f42d2aba | 109 | wxTextFileType type = typeDefault) |
c801d85f KB |
110 | { m_aLines.Insert(str, n); m_aTypes.Insert(type, n); } |
111 | // delete one line | |
c86f1403 | 112 | void RemoveLine(size_t n) { m_aLines.Remove(n); m_aTypes.Remove(n); } |
c801d85f KB |
113 | |
114 | // change the file on disk (default argument means "don't change type") | |
115 | // possibly in another format | |
c2389495 | 116 | bool Write(wxTextFileType typeNew = wxTextFileType_None); |
c801d85f KB |
117 | |
118 | // get the file termination string | |
6164d85e | 119 | // Note: implementation moved to textfile to prevent warning due to switch. |
9d2f3c71 | 120 | static const wxChar *GetEOL(wxTextFileType type = typeDefault); |
c801d85f KB |
121 | |
122 | // dtor | |
123 | ~wxTextFile(); | |
124 | ||
125 | private: | |
126 | // copy ctor/assignment operator not implemented | |
127 | wxTextFile(const wxTextFile&); | |
128 | wxTextFile& operator=(const wxTextFile&); | |
129 | ||
130 | // read the file in memory (m_file is supposed to be just opened) | |
131 | bool Read(); | |
132 | ||
c801d85f | 133 | wxFile m_file; // current file |
be6bf94b | 134 | |
c801d85f KB |
135 | ArrayFileType m_aTypes; // type of each line |
136 | wxArrayString m_aLines; // lines of file | |
be6bf94b VZ |
137 | |
138 | size_t m_nCurLine; // number of current line in the file | |
139 | ||
c801d85f KB |
140 | wxString m_strFile; // name of the file |
141 | }; | |
142 | ||
ce4169a4 RR |
143 | #endif |
144 | // wxUSE_TEXTFILE && wxUSE_FILE | |
145 | ||
146 | #endif | |
147 | // _TEXTFILE_H | |
148 |