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