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