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"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 // NB: this is always defined, even if !wxUSE_TEXTFILE
29 // the line termination type
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'
35 wxTextFileType_Mac
, // 'CR' = 0xD = 13 = '\r'
36 wxTextFileType_Os2
// 'CR' 'LF'
41 #include "wx/string.h"
43 #include "wx/dynarray.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 WX_DEFINE_EXPORTED_ARRAY(wxTextFileType
, ArrayFileType
);
51 class WXDLLEXPORT wxTextFile
54 // constants and static functions
55 // default type for current platform (determined at compile time)
56 static const wxTextFileType typeDefault
;
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
);
65 // get the file termination string
66 static const wxChar
*GetEOL(wxTextFileType type
= typeDefault
);
69 // def ctor, use Open(string)
72 wxTextFile(const wxString
& strFile
);
77 // create the file if it doesn't already exist
79 // same as Create() but with (another) file name
80 bool Create(const wxString
& strFile
);
81 // Open() also loads file in memory on success
82 bool Open(wxMBConv
& conv
= wxConvLibc
);
83 // same as Open() but with (another) file name
84 bool Open(const wxString
& strFile
, wxMBConv
& conv
= wxConvLibc
);
85 // closes the file and frees memory, losing all changes
87 // is file currently opened?
88 bool IsOpened() const { return m_isOpened
; }
91 // get the number of lines in the file
92 size_t GetLineCount() const { return m_aLines
.Count(); }
93 // the returned line may be modified (but don't add CR/LF at the end!)
94 wxString
& GetLine(size_t n
) const { return m_aLines
[n
]; }
95 wxString
& operator[](size_t n
) const { return m_aLines
[n
]; }
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
; }
102 bool Eof() const { return (m_aLines
.Count() == 0 || m_nCurLine
== m_aLines
.Count() - 1); }
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() ) { ... }
108 // NB: const is commented out because not all compilers understand
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]; }
117 // get the type of the line (see also GetEOL)
118 wxTextFileType
GetLineType(size_t n
) const { return m_aTypes
[n
]; }
119 // guess the type of file (m_file is supposed to be opened)
120 wxTextFileType
GuessType() const;
121 // get the name of the file
122 const wxChar
*GetName() const { return m_strFile
.c_str(); }
125 // add a line to the end
126 void AddLine(const wxString
& str
, wxTextFileType type
= typeDefault
)
127 { m_aLines
.Add(str
); m_aTypes
.Add(type
); }
128 // insert a line before the line number n
129 void InsertLine(const wxString
& str
,
131 wxTextFileType type
= typeDefault
)
132 { m_aLines
.Insert(str
, n
); m_aTypes
.Insert(type
, n
); }
134 void RemoveLine(size_t n
) { m_aLines
.RemoveAt(n
); m_aTypes
.RemoveAt(n
); }
136 // change the file on disk (default argument means "don't change type")
137 // possibly in another format
138 bool Write(wxTextFileType typeNew
= wxTextFileType_None
,
139 wxMBConv
& conv
= wxConvLibc
);
145 // copy ctor/assignment operator not implemented
146 wxTextFile(const wxTextFile
&);
147 wxTextFile
& operator=(const wxTextFile
&);
149 // read the file in memory (m_file is supposed to be just opened)
150 bool Read(wxMBConv
& conv
);
152 wxFile m_file
; // current file
154 ArrayFileType m_aTypes
; // type of each line
155 wxArrayString m_aLines
; // lines of file
157 size_t m_nCurLine
; // number of current line in the file
159 bool m_isOpened
; // was the file successfully opened the last time?
161 wxString m_strFile
; // name of the file
164 #else // !wxUSE_TEXTFILE
166 // these static wxTextFile methods are used internally by wxWindows, so should
167 // be defined even if we're compiling without wxTextFile at all.
169 class WXDLLEXPORT wxTextFile
172 // default type for current platform (determined at compile time)
173 static const wxTextFileType typeDefault
;
175 // this function returns a string which is identical to "text" passed in
176 // except that the line terminator characters are changed to correspond the
177 // given type. Called with the default argument, the function translates
178 // the string to the native format (Unix for Unix, DOS for Windows, ...).
179 static wxString
Translate(const wxString
& text
,
180 wxTextFileType type
= typeDefault
);
182 // get the file termination string
183 static const wxChar
*GetEOL(wxTextFileType type
= typeDefault
);
186 // copy ctor/assignment operator not implemented
187 wxTextFile(const wxTextFile
&);
188 wxTextFile
& operator=(const wxTextFile
&);
190 // suppress the gcc warning: 'class defines only private constructors and
193 friend class wxTextFileDummyFriend
;
197 #endif // wxUSE_TEXTFILE
199 #endif // _WX_TEXTFILE_H