wx/xml: Implement line-ending modes for xml saves

* Add 'wxTextFileType m_fileType' to hold the type
 * Add 'wxString m_eol' to hold the end of lines string

 * Add SetFileType() and GetFileType() to set and get the type
 * Add GetEOL() to get the end of lines wxString

 * Backwards compatibility preserved by using wxTextFileType_Unix

Change-Id: I3e8547b377e2c4060a3a2d97c299a08ea2c0a376
Signed-off-by: Adrian DC <radian.dc@gmail.com>
This commit is contained in:
Adrian DC
2017-05-29 16:08:48 +02:00
parent 2afd8bfcaf
commit 11e5413558
3 changed files with 49 additions and 10 deletions

View File

@@ -18,6 +18,7 @@
#include "wx/string.h" #include "wx/string.h"
#include "wx/object.h" #include "wx/object.h"
#include "wx/list.h" #include "wx/list.h"
#include "wx/textbuf.h"
#include "wx/versioninfo.h" #include "wx/versioninfo.h"
#ifdef WXMAKINGDLL_XML #ifdef WXMAKINGDLL_XML
@@ -317,6 +318,9 @@ public:
// encoding of in-memory representation! // encoding of in-memory representation!
const wxString& GetFileEncoding() const { return m_fileEncoding; } const wxString& GetFileEncoding() const { return m_fileEncoding; }
const wxXmlDoctype& GetDoctype() const { return m_doctype; } const wxXmlDoctype& GetDoctype() const { return m_doctype; }
// Returns file type of document
wxTextFileType GetFileType() const { return m_fileType; }
wxString GetEOL() const { return m_eol; }
// Write-access methods: // Write-access methods:
wxXmlNode *DetachDocumentNode() { wxXmlNode *old=m_docNode; m_docNode=NULL; return old; } wxXmlNode *DetachDocumentNode() { wxXmlNode *old=m_docNode; m_docNode=NULL; return old; }
@@ -326,6 +330,7 @@ public:
void SetVersion(const wxString& version) { m_version = version; } void SetVersion(const wxString& version) { m_version = version; }
void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; } void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; }
void SetDoctype(const wxXmlDoctype& doctype) { m_doctype = doctype; } void SetDoctype(const wxXmlDoctype& doctype) { m_doctype = doctype; }
void SetFileType(wxTextFileType fileType);
void AppendToProlog(wxXmlNode *node); void AppendToProlog(wxXmlNode *node);
#if !wxUSE_UNICODE #if !wxUSE_UNICODE
@@ -346,6 +351,8 @@ private:
#endif #endif
wxXmlDoctype m_doctype; wxXmlDoctype m_doctype;
wxXmlNode *m_docNode; wxXmlNode *m_docNode;
wxTextFileType m_fileType;
wxString m_eol;
void DoCopy(const wxXmlDocument& doc); void DoCopy(const wxXmlDocument& doc);

View File

@@ -786,6 +786,16 @@ public:
*/ */
const wxXmlDoctype& GetDoctype() const; const wxXmlDoctype& GetDoctype() const;
/**
Returns the output line ending format used for documents.
*/
wxTextFileType GetFileType() const;
/**
Returns the output line ending string used for documents.
*/
wxString GetEOL() const;
/** /**
Returns the document node of the document. Returns the document node of the document.
@@ -882,6 +892,11 @@ public:
*/ */
void SetDoctype(const wxXmlDoctype& doctype); void SetDoctype(const wxXmlDoctype& doctype);
/**
Sets the output line ending formats when the document is saved.
*/
void SetFileType(wxTextFileType fileType);
/** /**
Sets the root element node of this document. Sets the root element node of this document.

View File

@@ -462,11 +462,15 @@ wxXmlDocument::wxXmlDocument()
#if !wxUSE_UNICODE #if !wxUSE_UNICODE
m_encoding = wxS("UTF-8"); m_encoding = wxS("UTF-8");
#endif #endif
SetFileType(wxTextFileType_Unix);
} }
wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding) wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding)
:wxObject(), m_docNode(NULL) :wxObject(), m_docNode(NULL)
{ {
SetFileType(wxTextFileType_Unix);
if ( !Load(filename, encoding) ) if ( !Load(filename, encoding) )
{ {
wxDELETE(m_docNode); wxDELETE(m_docNode);
@@ -476,6 +480,8 @@ wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding)
wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding) wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding)
:wxObject(), m_docNode(NULL) :wxObject(), m_docNode(NULL)
{ {
SetFileType(wxTextFileType_Unix);
if ( !Load(stream, encoding) ) if ( !Load(stream, encoding) )
{ {
wxDELETE(m_docNode); wxDELETE(m_docNode);
@@ -503,6 +509,8 @@ void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
#endif #endif
m_fileEncoding = doc.m_fileEncoding; m_fileEncoding = doc.m_fileEncoding;
m_doctype = doc.m_doctype; m_doctype = doc.m_doctype;
m_fileType = doc.m_fileType;
m_eol = doc.m_eol;
if (doc.m_docNode) if (doc.m_docNode)
m_docNode = new wxXmlNode(*doc.m_docNode); m_docNode = new wxXmlNode(*doc.m_docNode);
@@ -602,6 +610,12 @@ void wxXmlDocument::SetRoot(wxXmlNode *root)
root->SetParent(m_docNode); root->SetParent(m_docNode);
} }
void wxXmlDocument::SetFileType(wxTextFileType fileType)
{
m_fileType = fileType;
m_eol = wxTextBuffer::GetEOL(m_fileType);
}
void wxXmlDocument::AppendToProlog(wxXmlNode *node) void wxXmlDocument::AppendToProlog(wxXmlNode *node)
{ {
if (!m_docNode) if (!m_docNode)
@@ -1058,9 +1072,10 @@ bool OutputEscapedString(wxOutputStream& stream,
bool OutputIndentation(wxOutputStream& stream, bool OutputIndentation(wxOutputStream& stream,
int indent, int indent,
wxMBConv *convMem, wxMBConv *convMem,
wxMBConv *convFile) wxMBConv *convFile,
const wxString& eol)
{ {
wxString str(wxS("\n")); wxString str(eol);
str += wxString(indent, wxS(' ')); str += wxString(indent, wxS(' '));
return OutputString(stream, str, convMem, convFile); return OutputString(stream, str, convMem, convFile);
} }
@@ -1070,7 +1085,8 @@ bool OutputNode(wxOutputStream& stream,
int indent, int indent,
wxMBConv *convMem, wxMBConv *convMem,
wxMBConv *convFile, wxMBConv *convFile,
int indentstep) int indentstep,
const wxString& eol)
{ {
bool rc; bool rc;
switch (node->GetType()) switch (node->GetType())
@@ -1125,12 +1141,12 @@ bool OutputNode(wxOutputStream& stream,
if ( indentstep >= 0 && n->GetType() != wxXML_TEXT_NODE ) if ( indentstep >= 0 && n->GetType() != wxXML_TEXT_NODE )
{ {
rc = OutputIndentation(stream, indent + indentstep, rc = OutputIndentation(stream, indent + indentstep,
convMem, convFile); convMem, convFile, eol);
} }
if ( rc ) if ( rc )
rc = OutputNode(stream, n, indent + indentstep, rc = OutputNode(stream, n, indent + indentstep,
convMem, convFile, indentstep); convMem, convFile, indentstep, eol);
prev = n; prev = n;
} }
@@ -1138,7 +1154,8 @@ bool OutputNode(wxOutputStream& stream,
if ( rc && indentstep >= 0 && if ( rc && indentstep >= 0 &&
prev && prev->GetType() != wxXML_TEXT_NODE ) prev && prev->GetType() != wxXML_TEXT_NODE )
{ {
rc = OutputIndentation(stream, indent, convMem, convFile); rc = OutputIndentation(stream, indent, convMem, convFile,
eol);
} }
if ( rc ) if ( rc )
@@ -1198,7 +1215,7 @@ bool wxXmlDocument::Save(wxOutputStream& stream, int indentstep) const
#endif #endif
wxString dec = wxString::Format( wxString dec = wxString::Format(
wxS("<?xml version=\"%s\" encoding=\"%s\"?>\n"), wxS("<?xml version=\"%s\" encoding=\"%s\"?>") + m_eol,
GetVersion(), GetFileEncoding() GetVersion(), GetFileEncoding()
); );
bool rc = OutputString(stream, dec, convMem.get(), convFile.get()); bool rc = OutputString(stream, dec, convMem.get(), convFile.get());
@@ -1209,7 +1226,7 @@ bool wxXmlDocument::Save(wxOutputStream& stream, int indentstep) const
if ( !doctype.empty() ) if ( !doctype.empty() )
{ {
rc = OutputString(stream, rc = OutputString(stream,
wxS("<!DOCTYPE ") + doctype + wxS(">\n"), wxS("<!DOCTYPE ") + doctype + wxS(">") + m_eol,
convMem.get(), convFile.get()); convMem.get(), convFile.get());
} }
} }
@@ -1221,8 +1238,8 @@ bool wxXmlDocument::Save(wxOutputStream& stream, int indentstep) const
while( rc && node ) while( rc && node )
{ {
rc = OutputNode(stream, node, 0, convMem.get(), rc = OutputNode(stream, node, 0, convMem.get(),
convFile.get(), indentstep) && convFile.get(), indentstep, m_eol) &&
OutputString(stream, wxS("\n"), convMem.get(), convFile.get()); OutputString(stream, m_eol, convMem.get(), convFile.get());
node = node->GetNext(); node = node->GetNext();
} }
return rc; return rc;