mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Added YAML::Newline manipulator for the emitter
This commit is contained in:
@@ -78,6 +78,7 @@ namespace YAML
|
|||||||
void EmitEndMap();
|
void EmitEndMap();
|
||||||
void EmitKey();
|
void EmitKey();
|
||||||
void EmitValue();
|
void EmitValue();
|
||||||
|
void EmitNewline();
|
||||||
void EmitKindTag();
|
void EmitKindTag();
|
||||||
void EmitTag(bool verbatim, const _Tag& tag);
|
void EmitTag(bool verbatim, const _Tag& tag);
|
||||||
|
|
||||||
|
@@ -12,6 +12,7 @@ namespace YAML
|
|||||||
// general manipulators
|
// general manipulators
|
||||||
Auto,
|
Auto,
|
||||||
TagByKind,
|
TagByKind,
|
||||||
|
Newline,
|
||||||
|
|
||||||
// output character set
|
// output character set
|
||||||
EmitNonAscii,
|
EmitNonAscii,
|
||||||
|
@@ -123,6 +123,9 @@ namespace YAML
|
|||||||
case TagByKind:
|
case TagByKind:
|
||||||
EmitKindTag();
|
EmitKindTag();
|
||||||
break;
|
break;
|
||||||
|
case Newline:
|
||||||
|
EmitNewline();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
m_pState->SetLocalValue(value);
|
m_pState->SetLocalValue(value);
|
||||||
break;
|
break;
|
||||||
@@ -505,6 +508,15 @@ namespace YAML
|
|||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EmitNewline
|
||||||
|
void Emitter::EmitNewline()
|
||||||
|
{
|
||||||
|
if(!good())
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_stream << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
// *******************************************************************************************
|
// *******************************************************************************************
|
||||||
// overloads of Write
|
// overloads of Write
|
||||||
|
|
||||||
@@ -551,7 +563,7 @@ namespace YAML
|
|||||||
PostAtomicWrite();
|
PostAtomicWrite();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Emitter::PreWriteIntegralType(std::stringstream& str)
|
void Emitter::PreWriteIntegralType(std::stringstream& str)
|
||||||
{
|
{
|
||||||
PreAtomicWrite();
|
PreAtomicWrite();
|
||||||
@@ -572,7 +584,7 @@ namespace YAML
|
|||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Emitter::PostWriteIntegralType(const std::stringstream& str)
|
void Emitter::PostWriteIntegralType(const std::stringstream& str)
|
||||||
{
|
{
|
||||||
m_stream << str.str();
|
m_stream << str.str();
|
||||||
|
@@ -651,7 +651,29 @@ namespace Test
|
|||||||
|
|
||||||
desiredOutput = "---\n-\n x: 5\n bar: hello\n- ~";
|
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
|
// incorrect emitting
|
||||||
|
|
||||||
@@ -832,6 +854,9 @@ namespace Test
|
|||||||
RunEmitterTest(&Emitter::UserTypeInContainer, "user type in container", passed, total);
|
RunEmitterTest(&Emitter::UserTypeInContainer, "user type in container", passed, total);
|
||||||
RunEmitterTest(&Emitter::PointerToInt, "pointer to int", passed, total);
|
RunEmitterTest(&Emitter::PointerToInt, "pointer to int", passed, total);
|
||||||
RunEmitterTest(&Emitter::PointerToUserType, "pointer to user type", 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::ExtraEndSeq, "extra EndSeq", passed, total);
|
||||||
RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed, total);
|
RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed, total);
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
#include "yaml-cpp/yaml.h"
|
#include "yaml-cpp/yaml.h"
|
||||||
|
#include "yaml-cpp/eventhandler.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -16,6 +17,23 @@ Params ParseArgs(int argc, char **argv) {
|
|||||||
return p;
|
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)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
Params p = ParseArgs(argc, argv);
|
Params p = ParseArgs(argc, argv);
|
||||||
@@ -28,7 +46,9 @@ int main(int argc, char **argv)
|
|||||||
try {
|
try {
|
||||||
YAML::Parser parser(input);
|
YAML::Parser parser(input);
|
||||||
YAML::Node doc;
|
YAML::Node doc;
|
||||||
while(parser.GetNextDocument(doc)) {
|
NullEventHandler handler;
|
||||||
|
// while(parser.GetNextDocument(doc)) {
|
||||||
|
while(parser.HandleNextDocument(handler)) {
|
||||||
// YAML::Emitter emitter;
|
// YAML::Emitter emitter;
|
||||||
// emitter << doc;
|
// emitter << doc;
|
||||||
// std::cout << emitter.c_str() << "\n";
|
// std::cout << emitter.c_str() << "\n";
|
||||||
|
Reference in New Issue
Block a user