Added YAML::Newline manipulator for the emitter

This commit is contained in:
Jesse Beder
2010-10-21 22:02:29 +00:00
parent a04e2da1ff
commit 1e4210401f
5 changed files with 63 additions and 4 deletions

View File

@@ -78,6 +78,7 @@ namespace YAML
void EmitEndMap();
void EmitKey();
void EmitValue();
void EmitNewline();
void EmitKindTag();
void EmitTag(bool verbatim, const _Tag& tag);

View File

@@ -12,6 +12,7 @@ namespace YAML
// general manipulators
Auto,
TagByKind,
Newline,
// output character set
EmitNonAscii,

View File

@@ -123,6 +123,9 @@ namespace YAML
case TagByKind:
EmitKindTag();
break;
case Newline:
EmitNewline();
break;
default:
m_pState->SetLocalValue(value);
break;
@@ -505,6 +508,15 @@ namespace YAML
assert(false);
}
// EmitNewline
void Emitter::EmitNewline()
{
if(!good())
return;
m_stream << '\n';
}
// *******************************************************************************************
// overloads of Write
@@ -551,7 +563,7 @@ namespace YAML
PostAtomicWrite();
return *this;
}
void Emitter::PreWriteIntegralType(std::stringstream& str)
{
PreAtomicWrite();
@@ -572,7 +584,7 @@ namespace YAML
assert(false);
}
}
void Emitter::PostWriteIntegralType(const std::stringstream& str)
{
m_stream << str.str();

View File

@@ -651,7 +651,29 @@ namespace Test
desiredOutput = "---\n-\n x: 5\n bar: hello\n- ~";
}
void NewlineAtEnd(YAML::Emitter& out, std::string& desiredOutput)
{
out << "Hello" << YAML::Newline << YAML::Newline;
desiredOutput = "--- Hello\n\n";
}
void NewlineInBlockSequence(YAML::Emitter& out, std::string& desiredOutput)
{
out << YAML::BeginSeq;
out << "a" << YAML::Newline << "b" << "c" << YAML::Newline << "d";
out << YAML::EndSeq;
desiredOutput = "---\n- a\n\n- b\n- c\n\n- d";
}
void NewlineInFlowSequence(YAML::Emitter& out, std::string& desiredOutput)
{
out << YAML::Flow << YAML::BeginSeq;
out << "a" << YAML::Newline << "b" << "c" << YAML::Newline << "d";
out << YAML::EndSeq;
desiredOutput = "--- [a\n, b, c\n, d]";
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// incorrect emitting
@@ -832,6 +854,9 @@ namespace Test
RunEmitterTest(&Emitter::UserTypeInContainer, "user type in container", passed, total);
RunEmitterTest(&Emitter::PointerToInt, "pointer to int", passed, total);
RunEmitterTest(&Emitter::PointerToUserType, "pointer to user type", passed, total);
RunEmitterTest(&Emitter::NewlineAtEnd, "newline at end", passed, total);
RunEmitterTest(&Emitter::NewlineInBlockSequence, "newline in block sequence", passed, total);
RunEmitterTest(&Emitter::NewlineInFlowSequence, "newline in flow sequence", passed, total);
RunEmitterErrorTest(&Emitter::ExtraEndSeq, "extra EndSeq", passed, total);
RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed, total);

View File

@@ -1,4 +1,5 @@
#include "yaml-cpp/yaml.h"
#include "yaml-cpp/eventhandler.h"
#include <fstream>
#include <iostream>
#include <vector>
@@ -16,6 +17,23 @@ Params ParseArgs(int argc, char **argv) {
return p;
}
class NullEventHandler: public YAML::EventHandler
{
public:
virtual void OnDocumentStart(const YAML::Mark&) {}
virtual void OnDocumentEnd() {}
virtual void OnNull(const std::string&, YAML::anchor_t) {}
virtual void OnAlias(const YAML::Mark&, YAML::anchor_t) {}
virtual void OnScalar(const YAML::Mark&, const std::string&, YAML::anchor_t, const std::string&) {}
virtual void OnSequenceStart(const YAML::Mark&, const std::string&, YAML::anchor_t) {}
virtual void OnSequenceEnd() {}
virtual void OnMapStart(const YAML::Mark&, const std::string&, YAML::anchor_t) {}
virtual void OnMapEnd() {}
};
int main(int argc, char **argv)
{
Params p = ParseArgs(argc, argv);
@@ -28,7 +46,9 @@ int main(int argc, char **argv)
try {
YAML::Parser parser(input);
YAML::Node doc;
while(parser.GetNextDocument(doc)) {
NullEventHandler handler;
// while(parser.GetNextDocument(doc)) {
while(parser.HandleNextDocument(handler)) {
// YAML::Emitter emitter;
// emitter << doc;
// std::cout << emitter.c_str() << "\n";