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
30 #include "wx/string.h"
32 #include "wx/dynarray.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // the line termination type
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'
47 WX_DEFINE_ARRAY(wxTextFileType
, ArrayFileType
);
49 class WXDLLEXPORT wxTextFile
52 // constants and static functions
53 // default type for current platform (determined at compile time)
54 static const wxTextFileType typeDefault
;
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
);
63 // get the file termination string
64 static const wxChar
*GetEOL(wxTextFileType type
= typeDefault
);
67 // def ctor, use Open(string)
70 wxTextFile(const wxString
& strFile
);
75 // Open() also loads file in memory on success
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
81 // is file currently opened?
82 bool IsOpened() const { return m_isOpened
; }
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
]; }
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; }
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() ) { ... }
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]; }
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(); }
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
,
125 wxTextFileType type
= typeDefault
)
126 { m_aLines
.Insert(str
, n
); m_aTypes
.Insert(type
, n
); }
128 void RemoveLine(size_t n
) { m_aLines
.Remove(n
); m_aTypes
.Remove(n
); }
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
);
138 // copy ctor/assignment operator not implemented
139 wxTextFile(const wxTextFile
&);
140 wxTextFile
& operator=(const wxTextFile
&);
142 // read the file in memory (m_file is supposed to be just opened)
145 wxFile m_file
; // current file
147 ArrayFileType m_aTypes
; // type of each line
148 wxArrayString m_aLines
; // lines of file
150 size_t m_nCurLine
; // number of current line in the file
152 bool m_isOpened
; // was the file successfully opened the last time?
154 wxString m_strFile
; // name of the file
157 #else // !wxUSE_TEXTFILE
159 // these static wxTextFile methods are used internally by wxWindows, so should
160 // be defined even if we're compiling without wxTextFile at all.
162 class WXDLLEXPORT wxTextFile
165 // default type for current platform (determined at compile time)
166 static const wxTextFileType typeDefault
;
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
);
175 // get the file termination string
176 static const wxChar
*GetEOL(wxTextFileType type
= typeDefault
);
179 // copy ctor/assignment operator not implemented
180 wxTextFile(const wxTextFile
&);
181 wxTextFile
& operator=(const wxTextFile
&);
184 #endif // wxUSE_TEXTFILE
186 #endif // _WX_TEXTFILE_H