"vscode:/vscode.git/clone" did not exist on "fa31da29e591ed2e64a7c6ba9153c0b2e5a0ddc2"
Commit 1e421040 authored by Jesse Beder's avatar Jesse Beder
Browse files

Added YAML::Newline manipulator for the emitter

parent a04e2da1
......@@ -78,6 +78,7 @@ namespace YAML
void EmitEndMap();
void EmitKey();
void EmitValue();
void EmitNewline();
void EmitKindTag();
void EmitTag(bool verbatim, const _Tag& tag);
......
......@@ -12,6 +12,7 @@ namespace YAML
// general manipulators
Auto,
TagByKind,
Newline,
// output character set
EmitNonAscii,
......
......@@ -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();
......
......@@ -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);
......
#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";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment