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
|
2004-05-23 10:56:36 -04:00
|
|
|
// Copyright: (c) 1998-2001 wxWidgets team
|
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
|
|
|
|
|
2003-08-09 08:38:21 -04:00
|
|
|
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
|
2001-11-14 15:40:20 -05:00
|
|
|
#pragma interface "textbuf.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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"
|
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
|
2003-02-26 14:31:44 -05:00
|
|
|
bool Open(wxMBConv& conv = wxConvUTF8);
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
// same as Open() but with (another) buffer name
|
2003-02-26 14:31:44 -05:00
|
|
|
bool Open(const wxString& strBufferName, wxMBConv& conv = wxConvUTF8);
|
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!)
|
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
|
|
|
wxString& GetLine(size_t n) const { return (wxString&)m_aLines[n]; }
|
|
|
|
wxString& operator[](size_t n) const { return (wxString&)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; }
|
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
|
|
|
bool Eof() const { return (m_aLines.size() == 0 || m_nCurLine == m_aLines.size() - 1); }
|
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)
|
|
|
|
wxString& GetFirstLine() /* const */ { return m_aLines[m_nCurLine = 0]; }
|
|
|
|
wxString& GetNextLine() /* const */ { return m_aLines[++m_nCurLine]; }
|
|
|
|
wxString& GetPrevLine() /* const */
|
|
|
|
{ wxASSERT(m_nCurLine > 0); return m_aLines[--m_nCurLine]; }
|
|
|
|
wxString& GetLastLine() /* const */
|
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
|
|
|
{ return m_aLines[m_nCurLine = m_aLines.size() - 1]; }
|
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
|
|
|
|
const wxChar *GetName() const { return m_strBufferName.c_str(); }
|
|
|
|
|
|
|
|
// 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
|
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 Clear() { m_aLines.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,
|
2003-02-26 14:31:44 -05:00
|
|
|
wxMBConv& conv = wxConvUTF8);
|
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)
|
2003-05-11 18:28:16 -04:00
|
|
|
wxTextBuffer() { 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;
|
|
|
|
virtual bool OnRead(wxMBConv& conv) = 0;
|
2002-12-04 09:11:26 -05:00
|
|
|
virtual bool OnWrite(wxTextFileType typeNew, wxMBConv& conv) = 0;
|
2001-11-14 15:40:20 -05:00
|
|
|
|
|
|
|
wxString m_strBufferName; // name of the buffer
|
|
|
|
|
|
|
|
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
|
|
|
|
|