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