]> git.saurik.com Git - wxWidgets.git/blob - include/wx/textfile.h
ignore these
[wxWidgets.git] / include / wx / textfile.h
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 };
46
47 WX_DEFINE_ARRAY(wxTextFileType, ArrayFileType);
48
49 class WXDLLEXPORT wxTextFile
50 {
51 public:
52 // constants and static functions
53 // default type for current platform (determined at compile time)
54 static const wxTextFileType typeDefault;
55
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
66 // ctors
67 // def ctor, use Open(string)
68 wxTextFile() { }
69 //
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?
82 bool IsOpened() const { return m_isOpened; }
83
84 // accessors
85 // get the number of lines in the file
86 size_t GetLineCount() const { return m_aLines.Count(); }
87 // the returned line may be modified (but don't add CR/LF at the end!)
88 wxString& GetLine(size_t n) const { return m_aLines[n]; }
89 wxString& operator[](size_t n) const { return m_aLines[n]; }
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; }
96 bool Eof() const { return m_nCurLine == m_aLines.Count() - 1; }
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
102 // NB: const is commented out because not all compilers understand
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
111 // get the type of the line (see also GetEOL)
112 wxTextFileType GetLineType(size_t n) const { return m_aTypes[n]; }
113 // guess the type of file (m_file is supposed to be opened)
114 wxTextFileType GuessType() const;
115 // get the name of the file
116 const wxChar *GetName() const { return m_strFile.c_str(); }
117
118 // add/remove lines
119 // add a line to the end
120 void AddLine(const wxString& str, wxTextFileType type = typeDefault)
121 { m_aLines.Add(str); m_aTypes.Add(type); }
122 // insert a line before the line number n
123 void InsertLine(const wxString& str,
124 size_t n,
125 wxTextFileType type = typeDefault)
126 { m_aLines.Insert(str, n); m_aTypes.Insert(type, n); }
127 // delete one line
128 void RemoveLine(size_t n) { m_aLines.Remove(n); m_aTypes.Remove(n); }
129
130 // change the file on disk (default argument means "don't change type")
131 // possibly in another format
132 bool Write(wxTextFileType typeNew = wxTextFileType_None);
133
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
145 wxFile m_file; // current file
146
147 ArrayFileType m_aTypes; // type of each line
148 wxArrayString m_aLines; // lines of file
149
150 size_t m_nCurLine; // number of current line in the file
151
152 bool m_isOpened; // was the file successfully opened the last time?
153
154 wxString m_strFile; // name of the file
155 };
156
157 #else // !wxUSE_TEXTFILE
158
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
187