]> git.saurik.com Git - wxWidgets.git/blame - include/wx/textfile.h
Listbox control code
[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 81 // Open() also loads file in memory on success
adfed1ca 82 bool Open(wxMBConv& conv = wxConvLibc);
c801d85f 83 // same as Open() but with (another) file name
adfed1ca 84 bool Open(const wxString& strFile, wxMBConv& conv = wxConvLibc);
c801d85f
KB
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
adfed1ca
VS
138 bool Write(wxTextFileType typeNew = wxTextFileType_None,
139 wxMBConv& conv = wxConvLibc);
c801d85f 140
c801d85f
KB
141 // dtor
142 ~wxTextFile();
143
144private:
145 // copy ctor/assignment operator not implemented
146 wxTextFile(const wxTextFile&);
147 wxTextFile& operator=(const wxTextFile&);
148
149 // read the file in memory (m_file is supposed to be just opened)
adfed1ca 150 bool Read(wxMBConv& conv);
c801d85f 151
c801d85f 152 wxFile m_file; // current file
be6bf94b 153
c801d85f
KB
154 ArrayFileType m_aTypes; // type of each line
155 wxArrayString m_aLines; // lines of file
be6bf94b
VZ
156
157 size_t m_nCurLine; // number of current line in the file
158
7cc98b3e
VZ
159 bool m_isOpened; // was the file successfully opened the last time?
160
c801d85f
KB
161 wxString m_strFile; // name of the file
162};
163
a1b82138 164#else // !wxUSE_TEXTFILE
ce4169a4 165
a1b82138
VZ
166// these static wxTextFile methods are used internally by wxWindows, so should
167// be defined even if we're compiling without wxTextFile at all.
168
169class WXDLLEXPORT wxTextFile
170{
171public:
172 // default type for current platform (determined at compile time)
173 static const wxTextFileType typeDefault;
174
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);
181
182 // get the file termination string
183 static const wxChar *GetEOL(wxTextFileType type = typeDefault);
184
185private:
186 // copy ctor/assignment operator not implemented
187 wxTextFile(const wxTextFile&);
188 wxTextFile& operator=(const wxTextFile&);
1e6feb95
VZ
189
190 // suppress the gcc warning: 'class defines only private constructors and
191 // has no friends'
192#ifdef __GNUG__
193 friend class wxTextFileDummyFriend;
194#endif // gcc
a1b82138
VZ
195};
196
197#endif // wxUSE_TEXTFILE
198
199#endif // _WX_TEXTFILE_H
9f04ccb1 200