From c95bcae49fea5251b542d8f4b93443f720f2a270 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Tue, 22 May 2012 13:57:44 -0500 Subject: [PATCH] Added writing integral types --- include/yaml-cpp/emitter.h | 12 ++++++++++++ src/emitter.cpp | 37 +++++++++++++++++++++++++++++++------ util/sandbox.cpp | 2 +- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/include/yaml-cpp/emitter.h b/include/yaml-cpp/emitter.h index 3d6f0b3..76876cd 100644 --- a/include/yaml-cpp/emitter.h +++ b/include/yaml-cpp/emitter.h @@ -74,6 +74,9 @@ namespace YAML template void SetStreamablePrecision(std::stringstream&) {} unsigned GetFloatPrecision() const; unsigned GetDoublePrecision() const; + + void PrepareIntegralStream(std::stringstream& stream) const; + void StartedScalar(); private: void EmitBeginDoc(); @@ -111,6 +114,15 @@ namespace YAML if(!good()) return *this; + PrepareNode(EmitterNodeType::Scalar); + + std::stringstream stream; + PrepareIntegralStream(stream); + stream << value; + m_stream << stream.str(); + + StartedScalar(); + return *this; } diff --git a/src/emitter.cpp b/src/emitter.cpp index 6e49e54..02a2ad9 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -436,6 +436,31 @@ namespace YAML m_stream << IndentTo(indent); } + void Emitter::PrepareIntegralStream(std::stringstream& stream) const + { + + switch(m_pState->GetIntFormat()) { + case Dec: + stream << std::dec; + break; + case Hex: + stream << "0x"; + stream << std::hex; + break; + case Oct: + stream << "0"; + stream << std::oct; + break; + default: + assert(false); + } + } + + void Emitter::StartedScalar() + { + m_pState->StartedScalar(); + } + // ******************************************************************************************* // overloads of Write @@ -464,7 +489,7 @@ namespace YAML break; } - m_pState->StartedScalar(); + StartedScalar(); return *this; } @@ -521,7 +546,7 @@ namespace YAML PrepareNode(EmitterNodeType::Scalar); m_stream << ComputeFullBoolName(b); - m_pState->StartedScalar(); + StartedScalar(); return *this; } @@ -533,7 +558,7 @@ namespace YAML PrepareNode(EmitterNodeType::Scalar); m_stream << ch; - m_pState->StartedScalar(); + StartedScalar(); return *this; } @@ -555,7 +580,7 @@ namespace YAML return *this; } - m_pState->StartedScalar(); + StartedScalar(); return *this; } @@ -639,7 +664,7 @@ namespace YAML return *this; // TODO - m_pState->StartedScalar(); + StartedScalar(); return *this; } @@ -653,7 +678,7 @@ namespace YAML PrepareNode(EmitterNodeType::Scalar); Utils::WriteBinary(m_stream, binary); - m_pState->StartedScalar(); + StartedScalar(); return *this; } diff --git a/util/sandbox.cpp b/util/sandbox.cpp index b67fc2b..aa05394 100644 --- a/util/sandbox.cpp +++ b/util/sandbox.cpp @@ -10,7 +10,7 @@ int main() out << YAML::Anchor("a") << YAML::Comment("anchor") << "item 1" << YAML::Comment("a"); out << YAML::BeginMap << YAML::Comment("b"); out << "pens" << YAML::Comment("foo") << "a" << YAML::Comment("bar"); - out << "pencils" << "b"; + out << "pencils" << 15; out << YAML::EndMap << YAML::Comment("monkey"); out << "item 2"; out << YAML::EndSeq;