From 081481330277969c9e25e2888714817ef00cd681 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Tue, 22 May 2012 12:29:36 -0500 Subject: [PATCH] Added comments, not entirely correct --- include/yaml-cpp/ostream.h | 3 +++ src/emitter.cpp | 8 +++++++- src/emitterutils.cpp | 7 +++++-- src/ostream.cpp | 3 ++- util/sandbox.cpp | 1 + 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/yaml-cpp/ostream.h b/include/yaml-cpp/ostream.h index 65839b1..5f11ce6 100644 --- a/include/yaml-cpp/ostream.h +++ b/include/yaml-cpp/ostream.h @@ -18,11 +18,13 @@ namespace YAML void reserve(unsigned size); void put(char ch); + void set_comment() { m_comment = true; } const char *str() const { return m_buffer; } unsigned row() const { return m_row; } unsigned col() const { return m_col; } unsigned pos() const { return m_pos; } + bool comment() const { return m_comment; } private: char *m_buffer; @@ -30,6 +32,7 @@ namespace YAML unsigned m_size; unsigned m_row, m_col; + bool m_comment; }; ostream& operator << (ostream& out, const char *str); diff --git a/src/emitter.cpp b/src/emitter.cpp index 176a21c..5aa8c88 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -589,7 +589,13 @@ namespace YAML if(!good()) return *this; - m_pState->StartedScalar(); + PrepareNode(EmitterNodeType::None); + + if(m_stream.col() > 0) + m_stream << Indentation(m_pState->GetPreCommentIndent()); + Utils::WriteComment(m_stream, comment.content, m_pState->GetPostCommentIndent()); + + m_pState->SetNonContent(); return *this; } diff --git a/src/emitterutils.cpp b/src/emitterutils.cpp index c54d9be..f17e350 100644 --- a/src/emitterutils.cpp +++ b/src/emitterutils.cpp @@ -333,15 +333,18 @@ namespace YAML { const unsigned curIndent = out.col(); out << "#" << Indentation(postCommentIndent); + out.set_comment(); int codePoint; for(std::string::const_iterator i = str.begin(); GetNextCodePointAndAdvance(codePoint, i, str.end()); ) { - if(codePoint == '\n') + if(codePoint == '\n') { out << "\n" << IndentTo(curIndent) << "#" << Indentation(postCommentIndent); - else + out.set_comment(); + } else { WriteCodePoint(out, codePoint); + } } return true; } diff --git a/src/ostream.cpp b/src/ostream.cpp index a7f1e14..9bcb5e5 100644 --- a/src/ostream.cpp +++ b/src/ostream.cpp @@ -3,7 +3,7 @@ namespace YAML { - ostream::ostream(): m_buffer(0), m_pos(0), m_size(0), m_row(0), m_col(0) + ostream::ostream(): m_buffer(0), m_pos(0), m_size(0), m_row(0), m_col(0), m_comment(false) { reserve(1024); } @@ -37,6 +37,7 @@ namespace YAML if(ch == '\n') { m_row++; m_col = 0; + m_comment = false; } else m_col++; } diff --git a/util/sandbox.cpp b/util/sandbox.cpp index 8c3ccd2..8960638 100644 --- a/util/sandbox.cpp +++ b/util/sandbox.cpp @@ -4,6 +4,7 @@ int main() { YAML::Emitter out; + out << YAML::Comment("Hello"); out << YAML::BeginSeq; out << "item 1"; out << YAML::BeginMap;