| 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 _WX_TEXTFILE_H |
| 15 | #define _WX_TEXTFILE_H |
| 16 | |
| 17 | #ifdef __GNUG__ |
| 18 | #pragma interface "textfile.h" |
| 19 | #endif |
| 20 | |
| 21 | #include "wx/defs.h" |
| 22 | |
| 23 | // ---------------------------------------------------------------------------- |
| 24 | // constants |
| 25 | // ---------------------------------------------------------------------------- |
| 26 | |
| 27 | // NB: this is always defined, even if !wxUSE_TEXTFILE |
| 28 | |
| 29 | // the line termination type |
| 30 | enum 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' |
| 35 | wxTextFileType_Mac, // 'CR' = 0xD = 13 = '\r' |
| 36 | wxTextFileType_Os2 // 'CR' 'LF' |
| 37 | }; |
| 38 | |
| 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 | |
| 49 | WX_DEFINE_EXPORTED_ARRAY(wxTextFileType, ArrayFileType); |
| 50 | |
| 51 | class WXDLLEXPORT wxTextFile |
| 52 | { |
| 53 | public: |
| 54 | // constants and static functions |
| 55 | // default type for current platform (determined at compile time) |
| 56 | static const wxTextFileType typeDefault; |
| 57 | |
| 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 | |
| 68 | // ctors |
| 69 | // def ctor, use Open(string) |
| 70 | wxTextFile() { } |
| 71 | // |
| 72 | wxTextFile(const wxString& strFile); |
| 73 | |
| 74 | // file operations |
| 75 | // file exists? |
| 76 | bool Exists() const; |
| 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); |
| 81 | // Open() also loads file in memory on success |
| 82 | bool Open(wxMBConv& conv = wxConvLibc); |
| 83 | // same as Open() but with (another) file name |
| 84 | bool Open(const wxString& strFile, wxMBConv& conv = wxConvLibc); |
| 85 | // closes the file and frees memory, losing all changes |
| 86 | bool Close(); |
| 87 | // is file currently opened? |
| 88 | bool IsOpened() const { return m_isOpened; } |
| 89 | |
| 90 | // accessors |
| 91 | // get the number of lines in the file |
| 92 | size_t GetLineCount() const { return m_aLines.Count(); } |
| 93 | // the returned line may be modified (but don't add CR/LF at the end!) |
| 94 | wxString& GetLine(size_t n) const { return m_aLines[n]; } |
| 95 | wxString& operator[](size_t n) const { return m_aLines[n]; } |
| 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; } |
| 102 | bool Eof() const { return (m_aLines.Count() == 0 || m_nCurLine == m_aLines.Count() - 1); } |
| 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 | |
| 108 | // NB: const is commented out because not all compilers understand |
| 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 | |
| 117 | // get the type of the line (see also GetEOL) |
| 118 | wxTextFileType GetLineType(size_t n) const { return m_aTypes[n]; } |
| 119 | // guess the type of file (m_file is supposed to be opened) |
| 120 | wxTextFileType GuessType() const; |
| 121 | // get the name of the file |
| 122 | const wxChar *GetName() const { return m_strFile.c_str(); } |
| 123 | |
| 124 | // add/remove lines |
| 125 | // add a line to the end |
| 126 | void AddLine(const wxString& str, wxTextFileType type = typeDefault) |
| 127 | { m_aLines.Add(str); m_aTypes.Add(type); } |
| 128 | // insert a line before the line number n |
| 129 | void InsertLine(const wxString& str, |
| 130 | size_t n, |
| 131 | wxTextFileType type = typeDefault) |
| 132 | { m_aLines.Insert(str, n); m_aTypes.Insert(type, n); } |
| 133 | // delete one line |
| 134 | void RemoveLine(size_t n) { m_aLines.RemoveAt(n); m_aTypes.RemoveAt(n); } |
| 135 | |
| 136 | // change the file on disk (default argument means "don't change type") |
| 137 | // possibly in another format |
| 138 | bool Write(wxTextFileType typeNew = wxTextFileType_None, |
| 139 | wxMBConv& conv = wxConvLibc); |
| 140 | |
| 141 | // dtor |
| 142 | ~wxTextFile(); |
| 143 | |
| 144 | private: |
| 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) |
| 150 | bool Read(wxMBConv& conv); |
| 151 | |
| 152 | wxFile m_file; // current file |
| 153 | |
| 154 | ArrayFileType m_aTypes; // type of each line |
| 155 | wxArrayString m_aLines; // lines of file |
| 156 | |
| 157 | size_t m_nCurLine; // number of current line in the file |
| 158 | |
| 159 | bool m_isOpened; // was the file successfully opened the last time? |
| 160 | |
| 161 | wxString m_strFile; // name of the file |
| 162 | }; |
| 163 | |
| 164 | #else // !wxUSE_TEXTFILE |
| 165 | |
| 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 | |
| 169 | class WXDLLEXPORT wxTextFile |
| 170 | { |
| 171 | public: |
| 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 | |
| 185 | private: |
| 186 | // copy ctor/assignment operator not implemented |
| 187 | wxTextFile(const wxTextFile&); |
| 188 | wxTextFile& operator=(const wxTextFile&); |
| 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 |
| 195 | }; |
| 196 | |
| 197 | #endif // wxUSE_TEXTFILE |
| 198 | |
| 199 | #endif // _WX_TEXTFILE_H |
| 200 | |