]> git.saurik.com Git - wxWidgets.git/blob - include/wx/textfile.h
check for empty data pointer in Len() and Empty()
[wxWidgets.git] / include / wx / textfile.h
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
7 // Modified by:
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
14 #ifndef _TEXTFILE_H
15 #define _TEXTFILE_H
16
17 #ifdef __GNUG__
18 #pragma interface "textfile.h"
19 #endif
20
21 #include "wx/defs.h"
22 #include "wx/string.h"
23 #include "wx/file.h"
24 #include "wx/dynarray.h"
25
26 // ----------------------------------------------------------------------------
27 // wxTextFile
28 // ----------------------------------------------------------------------------
29 class wxTextFile
30 {
31 public:
32 // constants
33 enum Type
34 {
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'
39 };
40
41 // default type for current platform (determined at compile time)
42 static const Type typeDefault;
43
44 // ctors
45 // def ctor, use Open(string)
46 wxTextFile() { }
47 //
48 wxTextFile(const wxString& strFile);
49
50 // file operations
51 // file exists?
52 bool Exists() const;
53 // Open() also loads file in memory on success
54 bool Open();
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
58 bool Close();
59 // is file currently opened?
60 bool IsOpened() const { return m_file.IsOpened(); }
61
62 // accessors
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(); }
74
75 // add/remove lines
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); }
82 // delete one line
83 void RemoveLine(size_t n) { m_aLines.Remove(n); m_aTypes.Remove(n); }
84
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);
88
89 // get the file termination string
90 inline static const char *GetEOL(Type type = typeDefault)
91 {
92 switch ( type ) {
93 case Type_None: return "";
94 case Type_Unix: return "\n";
95 case Type_Dos: return "\r\n";
96 case Type_Mac: return "\r";
97
98 default:
99 wxFAIL_MSG("bad file type in wxTextFile::GetEOL.");
100 return (const char *) NULL;
101 }
102 }
103
104 // dtor
105 ~wxTextFile();
106
107 private:
108 // copy ctor/assignment operator not implemented
109 wxTextFile(const wxTextFile&);
110 wxTextFile& operator=(const wxTextFile&);
111
112 // read the file in memory (m_file is supposed to be just opened)
113 bool Read();
114
115 WX_DEFINE_ARRAY(Type, ArrayFileType);
116
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
121 };
122
123 #endif //_TEXTFILE_H