From 11e54135587d2073c4f1c0986e072078678bf229 Mon Sep 17 00:00:00 2001 From: Adrian DC Date: Mon, 29 May 2017 16:08:48 +0200 Subject: [PATCH] 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 --- include/wx/xml/xml.h | 7 +++++++ interface/wx/xml/xml.h | 15 +++++++++++++++ src/xml/xml.cpp | 37 +++++++++++++++++++++++++++---------- 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/include/wx/xml/xml.h b/include/wx/xml/xml.h index 4701f6812b..e6c8c36e10 100644 --- a/include/wx/xml/xml.h +++ b/include/wx/xml/xml.h @@ -18,6 +18,7 @@ #include "wx/string.h" #include "wx/object.h" #include "wx/list.h" +#include "wx/textbuf.h" #include "wx/versioninfo.h" #ifdef WXMAKINGDLL_XML @@ -317,6 +318,9 @@ public: // encoding of in-memory representation! const wxString& GetFileEncoding() const { return m_fileEncoding; } 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: 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 SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; } void SetDoctype(const wxXmlDoctype& doctype) { m_doctype = doctype; } + void SetFileType(wxTextFileType fileType); void AppendToProlog(wxXmlNode *node); #if !wxUSE_UNICODE @@ -346,6 +351,8 @@ private: #endif wxXmlDoctype m_doctype; wxXmlNode *m_docNode; + wxTextFileType m_fileType; + wxString m_eol; void DoCopy(const wxXmlDocument& doc); diff --git a/interface/wx/xml/xml.h b/interface/wx/xml/xml.h index 8c9c60f1ed..aa644e3614 100644 --- a/interface/wx/xml/xml.h +++ b/interface/wx/xml/xml.h @@ -786,6 +786,16 @@ public: */ 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. @@ -882,6 +892,11 @@ public: */ 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. diff --git a/src/xml/xml.cpp b/src/xml/xml.cpp index 4ea6b80baa..1413d75a64 100644 --- a/src/xml/xml.cpp +++ b/src/xml/xml.cpp @@ -462,11 +462,15 @@ wxXmlDocument::wxXmlDocument() #if !wxUSE_UNICODE m_encoding = wxS("UTF-8"); #endif + + SetFileType(wxTextFileType_Unix); } wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding) :wxObject(), m_docNode(NULL) { + SetFileType(wxTextFileType_Unix); + if ( !Load(filename, encoding) ) { wxDELETE(m_docNode); @@ -476,6 +480,8 @@ wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding) wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding) :wxObject(), m_docNode(NULL) { + SetFileType(wxTextFileType_Unix); + if ( !Load(stream, encoding) ) { wxDELETE(m_docNode); @@ -503,6 +509,8 @@ void wxXmlDocument::DoCopy(const wxXmlDocument& doc) #endif m_fileEncoding = doc.m_fileEncoding; m_doctype = doc.m_doctype; + m_fileType = doc.m_fileType; + m_eol = doc.m_eol; if (doc.m_docNode) m_docNode = new wxXmlNode(*doc.m_docNode); @@ -602,6 +610,12 @@ void wxXmlDocument::SetRoot(wxXmlNode *root) root->SetParent(m_docNode); } +void wxXmlDocument::SetFileType(wxTextFileType fileType) +{ + m_fileType = fileType; + m_eol = wxTextBuffer::GetEOL(m_fileType); +} + void wxXmlDocument::AppendToProlog(wxXmlNode *node) { if (!m_docNode) @@ -1058,9 +1072,10 @@ bool OutputEscapedString(wxOutputStream& stream, bool OutputIndentation(wxOutputStream& stream, int indent, wxMBConv *convMem, - wxMBConv *convFile) + wxMBConv *convFile, + const wxString& eol) { - wxString str(wxS("\n")); + wxString str(eol); str += wxString(indent, wxS(' ')); return OutputString(stream, str, convMem, convFile); } @@ -1070,7 +1085,8 @@ bool OutputNode(wxOutputStream& stream, int indent, wxMBConv *convMem, wxMBConv *convFile, - int indentstep) + int indentstep, + const wxString& eol) { bool rc; switch (node->GetType()) @@ -1125,12 +1141,12 @@ bool OutputNode(wxOutputStream& stream, if ( indentstep >= 0 && n->GetType() != wxXML_TEXT_NODE ) { rc = OutputIndentation(stream, indent + indentstep, - convMem, convFile); + convMem, convFile, eol); } if ( rc ) rc = OutputNode(stream, n, indent + indentstep, - convMem, convFile, indentstep); + convMem, convFile, indentstep, eol); prev = n; } @@ -1138,7 +1154,8 @@ bool OutputNode(wxOutputStream& stream, if ( rc && indentstep >= 0 && prev && prev->GetType() != wxXML_TEXT_NODE ) { - rc = OutputIndentation(stream, indent, convMem, convFile); + rc = OutputIndentation(stream, indent, convMem, convFile, + eol); } if ( rc ) @@ -1198,7 +1215,7 @@ bool wxXmlDocument::Save(wxOutputStream& stream, int indentstep) const #endif wxString dec = wxString::Format( - wxS("\n"), + wxS("") + m_eol, GetVersion(), GetFileEncoding() ); bool rc = OutputString(stream, dec, convMem.get(), convFile.get()); @@ -1209,7 +1226,7 @@ bool wxXmlDocument::Save(wxOutputStream& stream, int indentstep) const if ( !doctype.empty() ) { rc = OutputString(stream, - wxS("\n"), + wxS("") + m_eol, convMem.get(), convFile.get()); } } @@ -1221,8 +1238,8 @@ bool wxXmlDocument::Save(wxOutputStream& stream, int indentstep) const while( rc && node ) { rc = OutputNode(stream, node, 0, convMem.get(), - convFile.get(), indentstep) && - OutputString(stream, wxS("\n"), convMem.get(), convFile.get()); + convFile.get(), indentstep, m_eol) && + OutputString(stream, m_eol, convMem.get(), convFile.get()); node = node->GetNext(); } return rc;