1 ///////////////////////////////////////////////////////////////////////////////
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
10 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
11 // Licence: wxWindows license
12 ///////////////////////////////////////////////////////////////////////////////
14 #ifndef _WX_TEXTFILE_H
15 #define _WX_TEXTFILE_H
18 #pragma interface "textfile.h"
25 #define wxUSE_TEXTFILE 0
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 // NB: this is always defined, even if !wxUSE_TEXTFILE
34 // the line termination type
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'
46 #include "wx/string.h"
48 #include "wx/dynarray.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 WX_DEFINE_EXPORTED_ARRAY(wxTextFileType
, ArrayFileType
);
56 class WXDLLEXPORT wxTextFile
59 // constants and static functions
60 // default type for current platform (determined at compile time)
61 static const wxTextFileType typeDefault
;
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
);
70 // get the file termination string
71 static const wxChar
*GetEOL(wxTextFileType type
= typeDefault
);
74 // def ctor, use Open(string)
77 wxTextFile(const wxString
& strFile
);
82 // create the file if it doesn't already exist
84 // same as Create() but with (another) file name
85 bool Create(const wxString
& strFile
);
86 // Open() also loads file in memory on success
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
92 // is file currently opened?
93 bool IsOpened() const { return m_isOpened
; }
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
]; }
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); }
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() ) { ... }
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]; }
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(); }
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
,
136 wxTextFileType type
= typeDefault
)
137 { m_aLines
.Insert(str
, n
); m_aTypes
.Insert(type
, n
); }
139 void RemoveLine(size_t n
) { m_aLines
.Remove(n
); m_aTypes
.Remove(n
); }
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
);
149 // copy ctor/assignment operator not implemented
150 wxTextFile(const wxTextFile
&);
151 wxTextFile
& operator=(const wxTextFile
&);
153 // read the file in memory (m_file is supposed to be just opened)
156 wxFile m_file
; // current file
158 ArrayFileType m_aTypes
; // type of each line
159 wxArrayString m_aLines
; // lines of file
161 size_t m_nCurLine
; // number of current line in the file
163 bool m_isOpened
; // was the file successfully opened the last time?
165 wxString m_strFile
; // name of the file
168 #else // !wxUSE_TEXTFILE
170 // these static wxTextFile methods are used internally by wxWindows, so should
171 // be defined even if we're compiling without wxTextFile at all.
173 class WXDLLEXPORT wxTextFile
176 // default type for current platform (determined at compile time)
177 static const wxTextFileType typeDefault
;
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
);
186 // get the file termination string
187 static const wxChar
*GetEOL(wxTextFileType type
= typeDefault
);
190 // copy ctor/assignment operator not implemented
191 wxTextFile(const wxTextFile
&);
192 wxTextFile
& operator=(const wxTextFile
&);
195 #endif // wxUSE_TEXTFILE
197 #endif // _WX_TEXTFILE_H