From 41e4cd3308b04af875f1f04111713f5f20b10fb5 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Tue, 22 May 2012 14:20:50 -0500 Subject: [PATCH] Split block map simple/long key for both key/value --- include/yaml-cpp/emitter.h | 6 ++- src/emitter.cpp | 85 +++++++++++++++++++++++++++++++++----- src/emitterstate.cpp | 23 ++++++++++- src/emitterstate.h | 5 ++- util/sandbox.cpp | 17 +++----- 5 files changed, 109 insertions(+), 27 deletions(-) diff --git a/include/yaml-cpp/emitter.h b/include/yaml-cpp/emitter.h index e6caddb..ba963a0 100644 --- a/include/yaml-cpp/emitter.h +++ b/include/yaml-cpp/emitter.h @@ -95,8 +95,10 @@ namespace YAML void BlockSeqPrepareNode(EmitterNodeType::value child); void FlowMapPrepareNode(EmitterNodeType::value child); void BlockMapPrepareNode(EmitterNodeType::value child); - void BlockMapPrepareKey(EmitterNodeType::value child); - void BlockMapPrepareValue(EmitterNodeType::value child); + void BlockMapPrepareLongKey(EmitterNodeType::value child); + void BlockMapPrepareSimpleKey(EmitterNodeType::value child); + void BlockMapPrepareLongKeyValue(EmitterNodeType::value child); + void BlockMapPrepareSimpleKeyValue(EmitterNodeType::value child); void SpaceOrIndentTo(bool requireSpace, unsigned indent); diff --git a/src/emitter.cpp b/src/emitter.cpp index 02a2ad9..841ba83 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -360,13 +360,23 @@ namespace YAML { const std::size_t childCount = m_pState->CurGroupChildCount(); - if(childCount % 2 == 0) - BlockMapPrepareKey(child); - else - BlockMapPrepareValue(child); + if(childCount % 2 == 0) { + if(m_pState->GetMapKeyFormat() == LongKey || child == EmitterNodeType::BlockSeq || child == EmitterNodeType::BlockMap) + m_pState->SetLongKey(); + + if(m_pState->CurGroupLongKey()) + BlockMapPrepareLongKey(child); + else + BlockMapPrepareSimpleKey(child); + } else { + if(m_pState->CurGroupLongKey()) + BlockMapPrepareLongKeyValue(child); + else + BlockMapPrepareSimpleKeyValue(child); + } } - void Emitter::BlockMapPrepareKey(EmitterNodeType::value child) + void Emitter::BlockMapPrepareLongKey(EmitterNodeType::value child) { const unsigned curIndent = m_pState->CurIndent(); const std::size_t childCount = m_pState->CurGroupChildCount(); @@ -378,7 +388,39 @@ namespace YAML if(childCount > 0) { m_stream << "\n"; } - if(false /* long key */) { + if(m_stream.comment()) { + m_stream << "\n"; + } + m_stream << IndentTo(curIndent); + m_stream << "?"; + } + + switch(child) { + case EmitterNodeType::None: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + SpaceOrIndentTo(m_pState->HasBegunContent(), curIndent); + break; + case EmitterNodeType::BlockSeq: + case EmitterNodeType::BlockMap: + break; + } + } + + void Emitter::BlockMapPrepareSimpleKey(EmitterNodeType::value child) + { + const unsigned curIndent = m_pState->CurIndent(); + const std::size_t childCount = m_pState->CurGroupChildCount(); + + if(child == EmitterNodeType::None) + return; + + if(!m_pState->HasBegunNode()) { + if(childCount > 0) { + m_stream << "\n"; } } @@ -397,16 +439,12 @@ namespace YAML } } - void Emitter::BlockMapPrepareValue(EmitterNodeType::value child) + void Emitter::BlockMapPrepareLongKeyValue(EmitterNodeType::value child) { const unsigned curIndent = m_pState->CurIndent(); const unsigned nextIndent = curIndent + m_pState->CurGroupIndent(); if(!m_pState->HasBegunNode()) { - if(false /* was long key */) { - } else { - m_stream << ":"; - } } switch(child) { @@ -425,6 +463,31 @@ namespace YAML } } + void Emitter::BlockMapPrepareSimpleKeyValue(EmitterNodeType::value child) + { + const unsigned curIndent = m_pState->CurIndent(); + const unsigned nextIndent = curIndent + m_pState->CurGroupIndent(); + + if(!m_pState->HasBegunNode()) { + m_stream << ":"; + } + + switch(child) { + case EmitterNodeType::None: + break; + case EmitterNodeType::Property: + case EmitterNodeType::Scalar: + case EmitterNodeType::FlowSeq: + case EmitterNodeType::FlowMap: + SpaceOrIndentTo(true, nextIndent); + break; + case EmitterNodeType::BlockSeq: + case EmitterNodeType::BlockMap: + m_stream << "\n"; + break; + } + } + // SpaceOrIndentTo // . Prepares for some more content by proper spacing void Emitter::SpaceOrIndentTo(bool requireSpace, unsigned indent) diff --git a/src/emitterstate.cpp b/src/emitterstate.cpp index 09ad567..6862d40 100644 --- a/src/emitterstate.cpp +++ b/src/emitterstate.cpp @@ -58,12 +58,26 @@ namespace YAML m_hasNonContent = true; } + void EmitterState::SetLongKey() + { + assert(!m_groups.empty()); + if(m_groups.empty()) + return; + + assert(m_groups.top().type == GroupType::Map); + assert(m_groups.top().flowType == FlowType::Block); + m_groups.top().longKey = true; + } + void EmitterState::StartedNode() { - if(m_groups.empty()) + if(m_groups.empty()) { m_docCount++; - else + } else { m_groups.top().childCount++; + if(m_groups.top().childCount % 2 == 0) + m_groups.top().longKey = false; + } m_hasAnchor = false; m_hasTag = false; @@ -166,6 +180,11 @@ namespace YAML return m_groups.empty() ? m_docCount : m_groups.top().childCount; } + bool EmitterState::CurGroupLongKey() const + { + return m_groups.empty() ? false : m_groups.top().longKey; + } + void EmitterState::ClearModifiedSettings() { m_modifiedSettings.clear(); diff --git a/src/emitterstate.h b/src/emitterstate.h index 1dfa699..6125bb5 100644 --- a/src/emitterstate.h +++ b/src/emitterstate.h @@ -37,6 +37,7 @@ namespace YAML void SetAnchor(); void SetTag(); void SetNonContent(); + void SetLongKey(); void StartedScalar(); void StartedGroup(GroupType::value type); void EndedGroup(GroupType::value type); @@ -48,6 +49,7 @@ namespace YAML FlowType::value CurGroupFlowType() const; int CurGroupIndent() const; std::size_t CurGroupChildCount() const; + bool CurGroupLongKey() const; int CurIndent() const { return m_curIndent; } bool HasAnchor() const { return m_hasAnchor; } @@ -127,12 +129,13 @@ namespace YAML SettingChanges m_globalModifiedSettings; struct Group { - explicit Group(GroupType::value type_): type(type_), indent(0), childCount(0) {} + explicit Group(GroupType::value type_): type(type_), indent(0), childCount(0), longKey(false) {} GroupType::value type; FlowType::value flowType; int indent; std::size_t childCount; + bool longKey; SettingChanges modifiedSettings; diff --git a/util/sandbox.cpp b/util/sandbox.cpp index 31e7d4e..2b617eb 100644 --- a/util/sandbox.cpp +++ b/util/sandbox.cpp @@ -4,17 +4,12 @@ int main() { YAML::Emitter out; - out << YAML::Comment("Hello"); - out << YAML::BeginSeq; - out << YAML::Comment("Hello"); - out << YAML::Anchor("a") << YAML::Comment("anchor") << "item 1" << YAML::Comment("a"); - out << YAML::BeginMap << YAML::Comment("b"); - out << "pens" << YAML::Comment("foo") << 2.3 << YAML::Comment("bar"); - out << "pencils" << 15; - out << YAML::EndMap << YAML::Comment("monkey"); - out << "item 2"; - out << YAML::EndSeq; - out << YAML::Comment("end"); + out << YAML::BeginMap; + out << YAML::BeginMap; + out << "a" << "b"; + out << YAML::EndMap; + out << "c"; + out << YAML::EndMap; std::cout << out.c_str() << "\n"; return 0;