From 050ddfb3f3236fd03c72f7dc936332eed6b3997f Mon Sep 17 00:00:00 2001 From: Vitaly Stakhovsky Date: Tue, 3 Apr 2018 17:28:08 -0400 Subject: cmXMLWriter: code improvement New Indent member will be used for XML indentation --- Source/cmXMLWriter.cxx | 29 ++++++++++++++++------------- Source/cmXMLWriter.h | 3 ++- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Source/cmXMLWriter.cxx b/Source/cmXMLWriter.cxx index 3cbc70d..9d2a3c4 100644 --- a/Source/cmXMLWriter.cxx +++ b/Source/cmXMLWriter.cxx @@ -9,6 +9,7 @@ cmXMLWriter::cmXMLWriter(std::ostream& output, std::size_t level) : Output(output) , IndentationElement(1, '\t') , Level(level) + , Indent(0) , ElementOpen(false) , BreakAttrib(false) , IsContent(false) @@ -17,7 +18,7 @@ cmXMLWriter::cmXMLWriter(std::ostream& output, std::size_t level) cmXMLWriter::~cmXMLWriter() { - assert(this->Elements.empty()); + assert(this->Indent == 0); } void cmXMLWriter::StartDocument(const char* encoding) @@ -27,27 +28,29 @@ void cmXMLWriter::StartDocument(const char* encoding) void cmXMLWriter::EndDocument() { - assert(this->Elements.empty()); + assert(this->Indent == 0); this->Output << '\n'; } void cmXMLWriter::StartElement(std::string const& name) { this->CloseStartElement(); - this->ConditionalLineBreak(!this->IsContent, this->Elements.size()); + this->ConditionalLineBreak(!this->IsContent); this->Output << '<' << name; this->Elements.push(name); + ++this->Indent; this->ElementOpen = true; this->BreakAttrib = false; } void cmXMLWriter::EndElement() { - assert(!this->Elements.empty()); + assert(this->Indent > 0); + --this->Indent; if (this->ElementOpen) { this->Output << "/>"; } else { - this->ConditionalLineBreak(!this->IsContent, this->Elements.size() - 1); + this->ConditionalLineBreak(!this->IsContent); this->IsContent = false; this->Output << "Elements.top() << '>'; } @@ -58,7 +61,7 @@ void cmXMLWriter::EndElement() void cmXMLWriter::Element(const char* name) { this->CloseStartElement(); - this->ConditionalLineBreak(!this->IsContent, this->Elements.size()); + this->ConditionalLineBreak(!this->IsContent); this->Output << '<' << name << "/>"; } @@ -70,7 +73,7 @@ void cmXMLWriter::BreakAttributes() void cmXMLWriter::Comment(const char* comment) { this->CloseStartElement(); - this->ConditionalLineBreak(!this->IsContent, this->Elements.size()); + this->ConditionalLineBreak(!this->IsContent); this->Output << ""; } @@ -83,14 +86,14 @@ void cmXMLWriter::CData(std::string const& data) void cmXMLWriter::Doctype(const char* doctype) { this->CloseStartElement(); - this->ConditionalLineBreak(!this->IsContent, this->Elements.size()); + this->ConditionalLineBreak(!this->IsContent); this->Output << ""; } void cmXMLWriter::ProcessingInstruction(const char* target, const char* data) { this->CloseStartElement(); - this->ConditionalLineBreak(!this->IsContent, this->Elements.size()); + this->ConditionalLineBreak(!this->IsContent); this->Output << ""; } @@ -106,11 +109,11 @@ void cmXMLWriter::SetIndentationElement(std::string const& element) this->IndentationElement = element; } -void cmXMLWriter::ConditionalLineBreak(bool condition, std::size_t indent) +void cmXMLWriter::ConditionalLineBreak(bool condition) { if (condition) { this->Output << '\n'; - for (std::size_t i = 0; i < indent + this->Level; ++i) { + for (std::size_t i = 0; i < this->Indent + this->Level; ++i) { this->Output << this->IndentationElement; } } @@ -119,7 +122,7 @@ void cmXMLWriter::ConditionalLineBreak(bool condition, std::size_t indent) void cmXMLWriter::PreAttribute() { assert(this->ElementOpen); - this->ConditionalLineBreak(this->BreakAttrib, this->Elements.size()); + this->ConditionalLineBreak(this->BreakAttrib); if (!this->BreakAttrib) { this->Output << ' '; } @@ -134,7 +137,7 @@ void cmXMLWriter::PreContent() void cmXMLWriter::CloseStartElement() { if (this->ElementOpen) { - this->ConditionalLineBreak(this->BreakAttrib, this->Elements.size()); + this->ConditionalLineBreak(this->BreakAttrib); this->Output << '>'; this->ElementOpen = false; } diff --git a/Source/cmXMLWriter.h b/Source/cmXMLWriter.h index 80940ee..ff0df18 100644 --- a/Source/cmXMLWriter.h +++ b/Source/cmXMLWriter.h @@ -67,7 +67,7 @@ public: void SetIndentationElement(std::string const& element); private: - void ConditionalLineBreak(bool condition, std::size_t indent); + void ConditionalLineBreak(bool condition); void PreAttribute(); void PreContent(); @@ -128,6 +128,7 @@ private: std::stack> Elements; std::string IndentationElement; std::size_t Level; + std::size_t Indent; bool ElementOpen; bool BreakAttrib; bool IsContent; -- cgit v0.12