textbuf.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/textbuf.h
  3. // Purpose: class wxTextBuffer to work with text buffers of _small_ size
  4. // (buffer is fully loaded in memory) and which understands CR/LF
  5. // differences between platforms.
  6. // Created: 14.11.01
  7. // Author: Morten Hanssen, Vadim Zeitlin
  8. // Copyright: (c) 1998-2001 Morten Hanssen, Vadim Zeitlin
  9. // Licence: wxWindows licence
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_TEXTBUFFER_H
  12. #define _WX_TEXTBUFFER_H
  13. #include "wx/defs.h"
  14. #include "wx/arrstr.h"
  15. #include "wx/convauto.h"
  16. // ----------------------------------------------------------------------------
  17. // constants
  18. // ----------------------------------------------------------------------------
  19. // the line termination type (kept wxTextFileType name for compatibility)
  20. enum wxTextFileType
  21. {
  22. wxTextFileType_None, // incomplete (the last line of the file only)
  23. wxTextFileType_Unix, // line is terminated with 'LF' = 0xA = 10 = '\n'
  24. wxTextFileType_Dos, // 'CR' 'LF'
  25. wxTextFileType_Mac, // 'CR' = 0xD = 13 = '\r'
  26. wxTextFileType_Os2 // 'CR' 'LF'
  27. };
  28. #include "wx/string.h"
  29. #if wxUSE_TEXTBUFFER
  30. #include "wx/dynarray.h"
  31. // ----------------------------------------------------------------------------
  32. // wxTextBuffer
  33. // ----------------------------------------------------------------------------
  34. WX_DEFINE_USER_EXPORTED_ARRAY_INT(wxTextFileType,
  35. wxArrayLinesType,
  36. class WXDLLIMPEXP_BASE);
  37. #endif // wxUSE_TEXTBUFFER
  38. class WXDLLIMPEXP_BASE wxTextBuffer
  39. {
  40. public:
  41. // constants and static functions
  42. // default type for current platform (determined at compile time)
  43. static const wxTextFileType typeDefault;
  44. // this function returns a string which is identical to "text" passed in
  45. // except that the line terminator characters are changed to correspond the
  46. // given type. Called with the default argument, the function translates
  47. // the string to the native format (Unix for Unix, DOS for Windows, ...).
  48. static wxString Translate(const wxString& text,
  49. wxTextFileType type = typeDefault);
  50. // get the buffer termination string
  51. static const wxChar *GetEOL(wxTextFileType type = typeDefault);
  52. // the static methods of this class are compiled in even when
  53. // !wxUSE_TEXTBUFFER because they are used by the library itself, but the
  54. // rest can be left out
  55. #if wxUSE_TEXTBUFFER
  56. // buffer operations
  57. // -----------------
  58. // buffer exists?
  59. bool Exists() const;
  60. // create the buffer if it doesn't already exist
  61. bool Create();
  62. // same as Create() but with (another) buffer name
  63. bool Create(const wxString& strBufferName);
  64. // Open() also loads buffer in memory on success
  65. bool Open(const wxMBConv& conv = wxConvAuto());
  66. // same as Open() but with (another) buffer name
  67. bool Open(const wxString& strBufferName, const wxMBConv& conv = wxConvAuto());
  68. // closes the buffer and frees memory, losing all changes
  69. bool Close();
  70. // is buffer currently opened?
  71. bool IsOpened() const { return m_isOpened; }
  72. // accessors
  73. // ---------
  74. // get the number of lines in the buffer
  75. size_t GetLineCount() const { return m_aLines.size(); }
  76. // the returned line may be modified (but don't add CR/LF at the end!)
  77. wxString& GetLine(size_t n) { return m_aLines[n]; }
  78. const wxString& GetLine(size_t n) const { return m_aLines[n]; }
  79. wxString& operator[](size_t n) { return m_aLines[n]; }
  80. const wxString& operator[](size_t n) const { return m_aLines[n]; }
  81. // the current line has meaning only when you're using
  82. // GetFirstLine()/GetNextLine() functions, it doesn't get updated when
  83. // you're using "direct access" i.e. GetLine()
  84. size_t GetCurrentLine() const { return m_nCurLine; }
  85. void GoToLine(size_t n) { m_nCurLine = n; }
  86. bool Eof() const { return m_nCurLine == m_aLines.size(); }
  87. // these methods allow more "iterator-like" traversal of the list of
  88. // lines, i.e. you may write something like:
  89. // for ( str = GetFirstLine(); !Eof(); str = GetNextLine() ) { ... }
  90. // NB: const is commented out because not all compilers understand
  91. // 'mutable' keyword yet (m_nCurLine should be mutable)
  92. wxString& GetFirstLine() /* const */
  93. { return m_aLines.empty() ? ms_eof : m_aLines[m_nCurLine = 0]; }
  94. wxString& GetNextLine() /* const */
  95. { return ++m_nCurLine == m_aLines.size() ? ms_eof
  96. : m_aLines[m_nCurLine]; }
  97. wxString& GetPrevLine() /* const */
  98. { wxASSERT(m_nCurLine > 0); return m_aLines[--m_nCurLine]; }
  99. wxString& GetLastLine() /* const */
  100. { m_nCurLine = m_aLines.size() - 1; return m_aLines.Last(); }
  101. // get the type of the line (see also GetEOL)
  102. wxTextFileType GetLineType(size_t n) const { return m_aTypes[n]; }
  103. // guess the type of buffer
  104. wxTextFileType GuessType() const;
  105. // get the name of the buffer
  106. const wxString& GetName() const { return m_strBufferName; }
  107. // add/remove lines
  108. // ----------------
  109. // add a line to the end
  110. void AddLine(const wxString& str, wxTextFileType type = typeDefault)
  111. { m_aLines.push_back(str); m_aTypes.push_back(type); }
  112. // insert a line before the line number n
  113. void InsertLine(const wxString& str,
  114. size_t n,
  115. wxTextFileType type = typeDefault)
  116. {
  117. m_aLines.insert(m_aLines.begin() + n, str);
  118. m_aTypes.insert(m_aTypes.begin()+n, type);
  119. }
  120. // delete one line
  121. void RemoveLine(size_t n)
  122. {
  123. m_aLines.erase(m_aLines.begin() + n);
  124. m_aTypes.erase(m_aTypes.begin() + n);
  125. }
  126. // remove all lines
  127. void Clear() { m_aLines.clear(); m_aTypes.clear(); m_nCurLine = 0; }
  128. // change the buffer (default argument means "don't change type")
  129. // possibly in another format
  130. bool Write(wxTextFileType typeNew = wxTextFileType_None,
  131. const wxMBConv& conv = wxConvAuto());
  132. // dtor
  133. virtual ~wxTextBuffer();
  134. protected:
  135. // ctors
  136. // -----
  137. // default ctor, use Open(string)
  138. wxTextBuffer() { m_nCurLine = 0; m_isOpened = false; }
  139. // ctor from filename
  140. wxTextBuffer(const wxString& strBufferName);
  141. enum wxTextBufferOpenMode { ReadAccess, WriteAccess };
  142. // Must implement these in derived classes.
  143. virtual bool OnExists() const = 0;
  144. virtual bool OnOpen(const wxString &strBufferName,
  145. wxTextBufferOpenMode openmode) = 0;
  146. virtual bool OnClose() = 0;
  147. virtual bool OnRead(const wxMBConv& conv) = 0;
  148. virtual bool OnWrite(wxTextFileType typeNew, const wxMBConv& conv) = 0;
  149. static wxString ms_eof; // dummy string returned at EOF
  150. wxString m_strBufferName; // name of the buffer
  151. private:
  152. wxArrayLinesType m_aTypes; // type of each line
  153. wxArrayString m_aLines; // lines of file
  154. size_t m_nCurLine; // number of current line in the buffer
  155. bool m_isOpened; // was the buffer successfully opened the last time?
  156. #endif // wxUSE_TEXTBUFFER
  157. // copy ctor/assignment operator not implemented
  158. wxTextBuffer(const wxTextBuffer&);
  159. wxTextBuffer& operator=(const wxTextBuffer&);
  160. };
  161. #endif // _WX_TEXTBUFFER_H