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