]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/textfile.h
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 ///////////////////////////////////////////////////////////////////////////////
18 #pragma interface "textfile.h"
22 #include "wx/string.h"
24 #include "wx/dynarray.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
35 Type_None
, // incomplete (the last line of the file only)
36 Type_Unix
, // line is terminated with 'LF' = 0xA = 10 = '\n'
37 Type_Dos
, // 'CR' 'LF'
38 Type_Mac
// 'CR' = 0xD = 13 = '\r'
41 // default type for current platform (determined at compile time)
42 static const Type typeDefault
;
45 // def ctor, use Open(string)
48 wxTextFile(const wxString
& strFile
);
53 // Open() also loads file in memory on success
55 // same as Open() but with (another) file name
56 bool Open(const wxString
& strFile
);
57 // closes the file and frees memory, losing all changes
59 // is file currently opened?
60 bool IsOpened() const { return m_file
.IsOpened(); }
63 // get the number of lines in the file
64 size_t GetLineCount() const { return m_aLines
.Count(); }
65 // the returned line may be modified (but don't add CR/LF at the end!)
66 wxString
& GetLine(size_t n
) const { return m_aLines
[n
]; }
67 wxString
& operator[](size_t n
) const { return m_aLines
[n
]; }
69 // the current line has meaning only when you're using
70 // GetFirstLine()/GetNextLine() functions, it doesn't get updated when
71 // you're using "direct access" i.e. GetLine()
72 size_t GetCurrentLine() const { return m_nCurLine
; }
73 void GoToLine(size_t n
) { m_nCurLine
= n
; }
74 bool Eof() const { return m_nCurLine
== m_aLines
.Count(); }
76 // these methods allow more "iterator-like" traversal of the list of
77 // lines, i.e. you may write something like:
78 // for ( str = GetFirstLine(); !Eof(); str = GetNextLine() ) { ... }
80 // @@@ const is commented out because not all compilers understand
81 // 'mutable' keyword yet (m_nCurLine should be mutable)
82 wxString
& GetFirstLine() /* const */ { return m_aLines
[m_nCurLine
= 0]; }
83 wxString
& GetNextLine() /* const */ { return m_aLines
[++m_nCurLine
]; }
84 wxString
& GetPrevLine() /* const */
85 { wxASSERT(m_nCurLine
> 0); return m_aLines
[--m_nCurLine
]; }
86 wxString
& GetLastLine() /* const */
87 { return m_aLines
[m_nCurLine
= m_aLines
.Count() - 1]; }
89 // get the type of the line (see also GetEOL)
90 Type
GetLineType(size_t n
) const { return m_aTypes
[n
]; }
91 // guess the type of file (m_file is supposed to be opened)
92 Type
GuessType() const;
93 // get the name of the file
94 const char *GetName() const { return m_strFile
.c_str(); }
97 // add a line to the end
98 void AddLine(const wxString
& str
, Type type
= typeDefault
)
99 { m_aLines
.Add(str
); m_aTypes
.Add(type
); }
100 // insert a line before the line number n
101 void InsertLine(const wxString
& str
, size_t n
, Type type
= typeDefault
)
102 { m_aLines
.Insert(str
, n
); m_aTypes
.Insert(type
, n
); }
104 void RemoveLine(size_t n
) { m_aLines
.Remove(n
); m_aTypes
.Remove(n
); }
106 // change the file on disk (default argument means "don't change type")
107 // possibly in another format
108 bool Write(Type typeNew
= Type_None
);
110 // get the file termination string
111 inline static const char *GetEOL(Type type
= typeDefault
)
114 case Type_None
: return "";
115 case Type_Unix
: return "\n";
116 case Type_Dos
: return "\r\n";
117 case Type_Mac
: return "\r";
120 wxFAIL_MSG("bad file type in wxTextFile::GetEOL.");
121 return (const char *) NULL
;
129 // copy ctor/assignment operator not implemented
130 wxTextFile(const wxTextFile
&);
131 wxTextFile
& operator=(const wxTextFile
&);
133 // read the file in memory (m_file is supposed to be just opened)
136 WX_DEFINE_ARRAY(Type
, ArrayFileType
);
138 wxFile m_file
; // current file
140 ArrayFileType m_aTypes
; // type of each line
141 wxArrayString m_aLines
; // lines of file
143 size_t m_nCurLine
; // number of current line in the file
145 wxString m_strFile
; // name of the file