2001-11-14 15:40:20 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/textbuf.h
|
|
|
|
// Purpose: class wxTextBuffer to work with text buffers of _small_ size
|
|
|
|
// (buffer is fully loaded in memory) and which understands CR/LF
|
|
|
|
// differences between platforms.
|
|
|
|
// Created: 14.11.01
|
|
|
|
// Author: Morten Hanssen, Vadim Zeitlin
|
2005-05-04 14:57:50 -04:00
|
|
|
// Copyright: (c) 1998-2001 Morten Hanssen, Vadim Zeitlin
|
2004-05-23 16:53:33 -04:00
|
|
|
// Licence: wxWindows licence
|
2001-11-14 15:40:20 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_TEXTBUFFER_H
|
|
|
|
#define _WX_TEXTBUFFER_H
|
|
|
|
|
|
|
|
#include "wx/defs.h"
|
Added --use-stl to cnfigure, wxUSE_STL to setup0.h
Moved wx/datetime.inl contents to wx/datetime.h and removed
inline redefinition hack.
Implemented STL-like interface on top of wxList/wxArray, when wxUSE_STL=0.
Implemented wxList-like and wxArray interfaces on top of std::list and
std::vector, when wxUSE_STL=1.
Added arrstr.h, moved wxArrayString declaration there; string.h
#includes arrstr.h only if WXWIN_COMPATIBILITY_2_4 is enabled.
Added WX_CLEAR_HASH_MAP, WX_CLEAR_HASH_TABLE, WX_CLEAR_LIST macros,
to clear a wxHashMap, wxHashTable, wxList containing pointers: deletes
pointers and makes container zero-sized.
When wxUSE_STL=1, wxStringList works like a std::list<wxString>.
Made wxBase compile when wxUSE_STL=1.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21768 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2003-07-08 15:52:35 -04:00
|
|
|
#include "wx/arrstr.h"
|
2006-04-05 10:37:47 -04:00
|
|
|
#include "wx/convauto.h"
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// the line termination type (kept wxTextFileType name for compability)
|
|
|
|
enum wxTextFileType
|
|
|
|
{
|
|
|
|
wxTextFileType_None, // incomplete (the last line of the file only)
|
|
|
|
wxTextFileType_Unix, // line is terminated with 'LF' = 0xA = 10 = '\n'
|
|
|
|
wxTextFileType_Dos, // 'CR' 'LF'
|
|
|
|
wxTextFileType_Mac, // 'CR' = 0xD = 13 = '\r'
|
|
|
|
wxTextFileType_Os2 // 'CR' 'LF'
|
|
|
|
};
|
|
|
|
|
|
|
|
#include "wx/string.h"
|
|
|
|
|
|
|
|
#if wxUSE_TEXTBUFFER
|
|
|
|
|
|
|
|
#include "wx/dynarray.h"
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxTextBuffer
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2003-08-03 16:03:59 -04:00
|
|
|
WX_DEFINE_USER_EXPORTED_ARRAY_INT(wxTextFileType,
|
|
|
|
wxArrayLinesType,
|
|
|
|
class WXDLLIMPEXP_BASE);
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
#endif // wxUSE_TEXTBUFFER
|
|
|
|
|
2003-07-01 21:59:24 -04:00
|
|
|
class WXDLLIMPEXP_BASE wxTextBuffer
|
2001-11-14 15:40:20 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// constants and static functions
|
|
|
|
// default type for current platform (determined at compile time)
|
|
|
|
static const wxTextFileType typeDefault;
|
|
|
|
|
|
|
|
// this function returns a string which is identical to "text" passed in
|
|
|
|
// except that the line terminator characters are changed to correspond the
|
|
|
|
// given type. Called with the default argument, the function translates
|
|
|
|
// the string to the native format (Unix for Unix, DOS for Windows, ...).
|
|
|
|
static wxString Translate(const wxString& text,
|
|
|
|
wxTextFileType type = typeDefault);
|
|
|
|
|
|
|
|
// get the buffer termination string
|
|
|
|
static const wxChar *GetEOL(wxTextFileType type = typeDefault);
|
|
|
|
|
|
|
|
// the static methods of this class are compiled in even when
|
|
|
|
// !wxUSE_TEXTBUFFER because they are used by the library itself, but the
|
|
|
|
// rest can be left out
|
|
|
|
#if wxUSE_TEXTBUFFER
|
|
|
|
|
|
|
|
// buffer operations
|
|
|
|
// -----------------
|
|
|
|
|
|
|
|
// buffer exists?
|
|
|
|
bool Exists() const;
|
|
|
|
|
|
|
|
// create the buffer if it doesn't already exist
|
|
|
|
bool Create();
|
|
|
|
|
|
|
|
// same as Create() but with (another) buffer name
|
|
|
|
bool Create(const wxString& strBufferName);
|
|
|
|
|
|
|
|
// Open() also loads buffer in memory on success
|
2006-04-05 10:37:47 -04:00
|
|
|
bool Open(const wxMBConv& conv = wxConvAuto());
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
// same as Open() but with (another) buffer name
|
2006-04-05 10:37:47 -04:00
|
|
|
bool Open(const wxString& strBufferName, const wxMBConv& conv = wxConvAuto());
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
// closes the buffer and frees memory, losing all changes
|
|
|
|
bool Close();
|
|
|
|
|
|
|
|
// is buffer currently opened?
|
|
|
|
bool IsOpened() const { return m_isOpened; }
|
|
|
|
|
|
|
|
// accessors
|
|
|
|
// ---------
|
|
|
|
|
|
|
|
// get the number of lines in the buffer
|
Added --use-stl to cnfigure, wxUSE_STL to setup0.h
Moved wx/datetime.inl contents to wx/datetime.h and removed
inline redefinition hack.
Implemented STL-like interface on top of wxList/wxArray, when wxUSE_STL=0.
Implemented wxList-like and wxArray interfaces on top of std::list and
std::vector, when wxUSE_STL=1.
Added arrstr.h, moved wxArrayString declaration there; string.h
#includes arrstr.h only if WXWIN_COMPATIBILITY_2_4 is enabled.
Added WX_CLEAR_HASH_MAP, WX_CLEAR_HASH_TABLE, WX_CLEAR_LIST macros,
to clear a wxHashMap, wxHashTable, wxList containing pointers: deletes
pointers and makes container zero-sized.
When wxUSE_STL=1, wxStringList works like a std::list<wxString>.
Made wxBase compile when wxUSE_STL=1.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21768 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2003-07-08 15:52:35 -04:00
|
|
|
size_t GetLineCount() const { return m_aLines.size(); }
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
// the returned line may be modified (but don't add CR/LF at the end!)
|
2011-01-08 12:17:10 -05:00
|
|
|
wxString& GetLine(size_t n) { return m_aLines[n]; }
|
|
|
|
const wxString& GetLine(size_t n) const { return m_aLines[n]; }
|
|
|
|
wxString& operator[](size_t n) { return m_aLines[n]; }
|
|
|
|
const wxString& operator[](size_t n) const { return m_aLines[n]; }
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
// the current line has meaning only when you're using
|
|
|
|
// GetFirstLine()/GetNextLine() functions, it doesn't get updated when
|
|
|
|
// you're using "direct access" i.e. GetLine()
|
|
|
|
size_t GetCurrentLine() const { return m_nCurLine; }
|
|
|
|
void GoToLine(size_t n) { m_nCurLine = n; }
|
2006-03-27 15:39:48 -05:00
|
|
|
bool Eof() const { return m_nCurLine == m_aLines.size(); }
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
// these methods allow more "iterator-like" traversal of the list of
|
|
|
|
// lines, i.e. you may write something like:
|
|
|
|
// for ( str = GetFirstLine(); !Eof(); str = GetNextLine() ) { ... }
|
|
|
|
|
|
|
|
// NB: const is commented out because not all compilers understand
|
|
|
|
// 'mutable' keyword yet (m_nCurLine should be mutable)
|
2006-03-27 18:01:00 -05:00
|
|
|
wxString& GetFirstLine() /* const */
|
|
|
|
{ return m_aLines.empty() ? ms_eof : m_aLines[m_nCurLine = 0]; }
|
|
|
|
wxString& GetNextLine() /* const */
|
|
|
|
{ return ++m_nCurLine == m_aLines.size() ? ms_eof
|
|
|
|
: m_aLines[m_nCurLine]; }
|
2001-11-14 15:40:20 -05:00
|
|
|
wxString& GetPrevLine() /* const */
|
2006-03-27 18:01:00 -05:00
|
|
|
{ wxASSERT(m_nCurLine > 0); return m_aLines[--m_nCurLine]; }
|
2001-11-14 15:40:20 -05:00
|
|
|
wxString& GetLastLine() /* const */
|
2006-03-27 18:01:00 -05:00
|
|
|
{ m_nCurLine = m_aLines.size() - 1; return m_aLines.Last(); }
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
// get the type of the line (see also GetEOL)
|
|
|
|
wxTextFileType GetLineType(size_t n) const { return m_aTypes[n]; }
|
|
|
|
|
|
|
|
// guess the type of buffer
|
|
|
|
wxTextFileType GuessType() const;
|
|
|
|
|
|
|
|
// get the name of the buffer
|
2007-06-19 09:54:23 -04:00
|
|
|
const wxString& GetName() const { return m_strBufferName; }
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
// add/remove lines
|
|
|
|
// ----------------
|
|
|
|
|
|
|
|
// add a line to the end
|
|
|
|
void AddLine(const wxString& str, wxTextFileType type = typeDefault)
|
Added --use-stl to cnfigure, wxUSE_STL to setup0.h
Moved wx/datetime.inl contents to wx/datetime.h and removed
inline redefinition hack.
Implemented STL-like interface on top of wxList/wxArray, when wxUSE_STL=0.
Implemented wxList-like and wxArray interfaces on top of std::list and
std::vector, when wxUSE_STL=1.
Added arrstr.h, moved wxArrayString declaration there; string.h
#includes arrstr.h only if WXWIN_COMPATIBILITY_2_4 is enabled.
Added WX_CLEAR_HASH_MAP, WX_CLEAR_HASH_TABLE, WX_CLEAR_LIST macros,
to clear a wxHashMap, wxHashTable, wxList containing pointers: deletes
pointers and makes container zero-sized.
When wxUSE_STL=1, wxStringList works like a std::list<wxString>.
Made wxBase compile when wxUSE_STL=1.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21768 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2003-07-08 15:52:35 -04:00
|
|
|
{ m_aLines.push_back(str); m_aTypes.push_back(type); }
|
2001-11-14 15:40:20 -05:00
|
|
|
// insert a line before the line number n
|
|
|
|
void InsertLine(const wxString& str,
|
|
|
|
size_t n,
|
|
|
|
wxTextFileType type = typeDefault)
|
Added --use-stl to cnfigure, wxUSE_STL to setup0.h
Moved wx/datetime.inl contents to wx/datetime.h and removed
inline redefinition hack.
Implemented STL-like interface on top of wxList/wxArray, when wxUSE_STL=0.
Implemented wxList-like and wxArray interfaces on top of std::list and
std::vector, when wxUSE_STL=1.
Added arrstr.h, moved wxArrayString declaration there; string.h
#includes arrstr.h only if WXWIN_COMPATIBILITY_2_4 is enabled.
Added WX_CLEAR_HASH_MAP, WX_CLEAR_HASH_TABLE, WX_CLEAR_LIST macros,
to clear a wxHashMap, wxHashTable, wxList containing pointers: deletes
pointers and makes container zero-sized.
When wxUSE_STL=1, wxStringList works like a std::list<wxString>.
Made wxBase compile when wxUSE_STL=1.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21768 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2003-07-08 15:52:35 -04:00
|
|
|
{
|
2004-09-23 14:20:56 -04:00
|
|
|
m_aLines.insert(m_aLines.begin() + n, str);
|
|
|
|
m_aTypes.insert(m_aTypes.begin()+n, type);
|
Added --use-stl to cnfigure, wxUSE_STL to setup0.h
Moved wx/datetime.inl contents to wx/datetime.h and removed
inline redefinition hack.
Implemented STL-like interface on top of wxList/wxArray, when wxUSE_STL=0.
Implemented wxList-like and wxArray interfaces on top of std::list and
std::vector, when wxUSE_STL=1.
Added arrstr.h, moved wxArrayString declaration there; string.h
#includes arrstr.h only if WXWIN_COMPATIBILITY_2_4 is enabled.
Added WX_CLEAR_HASH_MAP, WX_CLEAR_HASH_TABLE, WX_CLEAR_LIST macros,
to clear a wxHashMap, wxHashTable, wxList containing pointers: deletes
pointers and makes container zero-sized.
When wxUSE_STL=1, wxStringList works like a std::list<wxString>.
Made wxBase compile when wxUSE_STL=1.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21768 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2003-07-08 15:52:35 -04:00
|
|
|
}
|
|
|
|
|
2001-11-14 15:40:20 -05:00
|
|
|
// delete one line
|
Added --use-stl to cnfigure, wxUSE_STL to setup0.h
Moved wx/datetime.inl contents to wx/datetime.h and removed
inline redefinition hack.
Implemented STL-like interface on top of wxList/wxArray, when wxUSE_STL=0.
Implemented wxList-like and wxArray interfaces on top of std::list and
std::vector, when wxUSE_STL=1.
Added arrstr.h, moved wxArrayString declaration there; string.h
#includes arrstr.h only if WXWIN_COMPATIBILITY_2_4 is enabled.
Added WX_CLEAR_HASH_MAP, WX_CLEAR_HASH_TABLE, WX_CLEAR_LIST macros,
to clear a wxHashMap, wxHashTable, wxList containing pointers: deletes
pointers and makes container zero-sized.
When wxUSE_STL=1, wxStringList works like a std::list<wxString>.
Made wxBase compile when wxUSE_STL=1.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21768 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2003-07-08 15:52:35 -04:00
|
|
|
void RemoveLine(size_t n)
|
|
|
|
{
|
|
|
|
m_aLines.erase(m_aLines.begin() + n);
|
2004-09-23 14:20:56 -04:00
|
|
|
m_aTypes.erase(m_aTypes.begin() + n);
|
Added --use-stl to cnfigure, wxUSE_STL to setup0.h
Moved wx/datetime.inl contents to wx/datetime.h and removed
inline redefinition hack.
Implemented STL-like interface on top of wxList/wxArray, when wxUSE_STL=0.
Implemented wxList-like and wxArray interfaces on top of std::list and
std::vector, when wxUSE_STL=1.
Added arrstr.h, moved wxArrayString declaration there; string.h
#includes arrstr.h only if WXWIN_COMPATIBILITY_2_4 is enabled.
Added WX_CLEAR_HASH_MAP, WX_CLEAR_HASH_TABLE, WX_CLEAR_LIST macros,
to clear a wxHashMap, wxHashTable, wxList containing pointers: deletes
pointers and makes container zero-sized.
When wxUSE_STL=1, wxStringList works like a std::list<wxString>.
Made wxBase compile when wxUSE_STL=1.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21768 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2003-07-08 15:52:35 -04:00
|
|
|
}
|
2001-11-14 15:40:20 -05:00
|
|
|
|
2003-03-24 15:37:27 -05:00
|
|
|
// remove all lines
|
2005-03-22 19:17:49 -05:00
|
|
|
void Clear() { m_aLines.clear(); m_aTypes.clear(); m_nCurLine = 0; }
|
2003-05-11 18:28:16 -04:00
|
|
|
|
2001-11-14 15:40:20 -05:00
|
|
|
// change the buffer (default argument means "don't change type")
|
|
|
|
// possibly in another format
|
|
|
|
bool Write(wxTextFileType typeNew = wxTextFileType_None,
|
2006-04-05 10:37:47 -04:00
|
|
|
const wxMBConv& conv = wxConvAuto());
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
// dtor
|
2004-10-13 10:09:48 -04:00
|
|
|
virtual ~wxTextBuffer();
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// ctors
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// default ctor, use Open(string)
|
2008-03-14 22:38:43 -04:00
|
|
|
wxTextBuffer() { m_nCurLine = 0; m_isOpened = false; }
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
// ctor from filename
|
|
|
|
wxTextBuffer(const wxString& strBufferName);
|
|
|
|
|
|
|
|
enum wxTextBufferOpenMode { ReadAccess, WriteAccess };
|
|
|
|
|
|
|
|
// Must implement these in derived classes.
|
|
|
|
virtual bool OnExists() const = 0;
|
|
|
|
virtual bool OnOpen(const wxString &strBufferName,
|
|
|
|
wxTextBufferOpenMode openmode) = 0;
|
|
|
|
virtual bool OnClose() = 0;
|
2006-04-05 10:37:47 -04:00
|
|
|
virtual bool OnRead(const wxMBConv& conv) = 0;
|
|
|
|
virtual bool OnWrite(wxTextFileType typeNew, const wxMBConv& conv) = 0;
|
2001-11-14 15:40:20 -05:00
|
|
|
|
2006-03-27 18:01:00 -05:00
|
|
|
static wxString ms_eof; // dummy string returned at EOF
|
|
|
|
wxString m_strBufferName; // name of the buffer
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
private:
|
2003-08-03 16:03:59 -04:00
|
|
|
wxArrayLinesType m_aTypes; // type of each line
|
|
|
|
wxArrayString m_aLines; // lines of file
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
size_t m_nCurLine; // number of current line in the buffer
|
|
|
|
|
|
|
|
bool m_isOpened; // was the buffer successfully opened the last time?
|
|
|
|
#endif // wxUSE_TEXTBUFFER
|
|
|
|
|
|
|
|
// copy ctor/assignment operator not implemented
|
|
|
|
wxTextBuffer(const wxTextBuffer&);
|
|
|
|
wxTextBuffer& operator=(const wxTextBuffer&);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _WX_TEXTBUFFER_H
|
|
|
|
|