]>
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 'CR' = 0xA = 10 = '\n'
37 Type_Dos
, // 'LF' 'CR'
38 Type_Mac
// 'LF' = 0xD = 12 = '\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
]; }
68 // get the type of the line (see also GetEOL)
69 Type
GetLineType(size_t n
) const { return m_aTypes
[n
]; }
70 // guess the type of file (m_file is supposed to be opened)
71 Type
GuessType() const;
72 // get the name of the file
73 const char *GetName() const { return m_strFile
.c_str(); }
76 // add a line to the end
77 void AddLine(const wxString
& str
, Type type
= typeDefault
)
78 { m_aLines
.Add(str
); m_aTypes
.Add(type
); }
79 // insert a line before the line number n
80 void InsertLine(const wxString
& str
, size_t n
, Type type
= typeDefault
)
81 { m_aLines
.Insert(str
, n
); m_aTypes
.Insert(type
, n
); }
83 void RemoveLine(size_t n
) { m_aLines
.Remove(n
); m_aTypes
.Remove(n
); }
85 // change the file on disk (default argument means "don't change type")
86 // possibly in another format
87 bool Write(Type typeNew
= Type_None
);
89 // get the file termination string
90 inline static const char *GetEOL(Type type
= typeDefault
)
93 case Type_None
: return "";
94 case Type_Unix
: return "\n";
95 case Type_Dos
: return "\r\n";
96 case Type_Mac
: return "\r";
99 wxFAIL_MSG("bad file type in wxTextFile::GetEOL.");
100 return (const char *) NULL
;
108 // copy ctor/assignment operator not implemented
109 wxTextFile(const wxTextFile
&);
110 wxTextFile
& operator=(const wxTextFile
&);
112 // read the file in memory (m_file is supposed to be just opened)
115 WX_DEFINE_ARRAY(Type
, ArrayFileType
);
117 wxFile m_file
; // current file
118 ArrayFileType m_aTypes
; // type of each line
119 wxArrayString m_aLines
; // lines of file
120 wxString m_strFile
; // name of the file