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