mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Merged emitter branch into trunk, changes r105:r151
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
set(FILES alias.cpp content.cpp iterator.cpp node.cpp parserstate.cpp
|
||||
scalar.cpp scanscalar.cpp sequence.cpp stream.cpp
|
||||
exp.cpp map.cpp parser.cpp regex.cpp scanner.cpp
|
||||
scantoken.cpp simplekey.cpp)
|
||||
scantoken.cpp simplekey.cpp
|
||||
emitter.cpp emitterstate.h emitterstate.cpp emitterutils.h emitterutils.cpp
|
||||
ostream.cpp)
|
||||
|
||||
include_directories(${YAML_CPP_SOURCE_DIR}/include)
|
||||
add_library(yaml-cpp ${FILES})
|
||||
|
697
src/emitter.cpp
Normal file
697
src/emitter.cpp
Normal file
@@ -0,0 +1,697 @@
|
||||
#include "emitter.h"
|
||||
#include "emitterstate.h"
|
||||
#include "emitterutils.h"
|
||||
#include "indentation.h"
|
||||
#include "exceptions.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Emitter::Emitter(): m_pState(new EmitterState)
|
||||
{
|
||||
}
|
||||
|
||||
Emitter::~Emitter()
|
||||
{
|
||||
}
|
||||
|
||||
bool Emitter::WriteToStream(std::ostream& out) const
|
||||
{
|
||||
out << m_stream.str();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Emitter::WriteToFile(const std::string& fileName) const
|
||||
{
|
||||
std::ofstream fout(fileName.c_str());
|
||||
if(!fout)
|
||||
return false;
|
||||
|
||||
return WriteToStream(fout);
|
||||
}
|
||||
|
||||
// state checking
|
||||
bool Emitter::good() const
|
||||
{
|
||||
return m_pState->good();
|
||||
}
|
||||
|
||||
const std::string Emitter::GetLastError() const
|
||||
{
|
||||
return m_pState->GetLastError();
|
||||
}
|
||||
|
||||
// global setters
|
||||
bool Emitter::SetStringFormat(EMITTER_MANIP value)
|
||||
{
|
||||
return m_pState->SetStringFormat(value, GLOBAL);
|
||||
}
|
||||
|
||||
bool Emitter::SetBoolFormat(EMITTER_MANIP value)
|
||||
{
|
||||
bool ok = false;
|
||||
if(m_pState->SetBoolFormat(value, GLOBAL))
|
||||
ok = true;
|
||||
if(m_pState->SetBoolCaseFormat(value, GLOBAL))
|
||||
ok = true;
|
||||
if(m_pState->SetBoolLengthFormat(value, GLOBAL))
|
||||
ok = true;
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool Emitter::SetIntBase(EMITTER_MANIP value)
|
||||
{
|
||||
return m_pState->SetIntFormat(value, GLOBAL);
|
||||
}
|
||||
|
||||
bool Emitter::SetSeqFormat(EMITTER_MANIP value)
|
||||
{
|
||||
return m_pState->SetFlowType(GT_SEQ, value, GLOBAL);
|
||||
}
|
||||
|
||||
bool Emitter::SetMapFormat(EMITTER_MANIP value)
|
||||
{
|
||||
bool ok = false;
|
||||
if(m_pState->SetFlowType(GT_MAP, value, GLOBAL))
|
||||
ok = true;
|
||||
if(m_pState->SetMapKeyFormat(value, GLOBAL))
|
||||
ok = true;
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool Emitter::SetIndent(unsigned n)
|
||||
{
|
||||
return m_pState->SetIndent(n, GLOBAL);
|
||||
}
|
||||
|
||||
bool Emitter::SetPreCommentIndent(unsigned n)
|
||||
{
|
||||
return m_pState->SetPreCommentIndent(n, GLOBAL);
|
||||
}
|
||||
|
||||
bool Emitter::SetPostCommentIndent(unsigned n)
|
||||
{
|
||||
return m_pState->SetPostCommentIndent(n, GLOBAL);
|
||||
}
|
||||
|
||||
// SetLocalValue
|
||||
// . Either start/end a group, or set a modifier locally
|
||||
Emitter& Emitter::SetLocalValue(EMITTER_MANIP value)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
switch(value) {
|
||||
case BeginSeq:
|
||||
EmitBeginSeq();
|
||||
break;
|
||||
case EndSeq:
|
||||
EmitEndSeq();
|
||||
break;
|
||||
case BeginMap:
|
||||
EmitBeginMap();
|
||||
break;
|
||||
case EndMap:
|
||||
EmitEndMap();
|
||||
break;
|
||||
case Key:
|
||||
EmitKey();
|
||||
break;
|
||||
case Value:
|
||||
EmitValue();
|
||||
break;
|
||||
default:
|
||||
m_pState->SetLocalValue(value);
|
||||
break;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::SetLocalIndent(const _Indent& indent)
|
||||
{
|
||||
m_pState->SetIndent(indent.value, LOCAL);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// GotoNextPreAtomicState
|
||||
// . Runs the state machine, emitting if necessary, and returns 'true' if done (i.e., ready to emit an atom)
|
||||
bool Emitter::GotoNextPreAtomicState()
|
||||
{
|
||||
if(!good())
|
||||
return true;
|
||||
|
||||
unsigned curIndent = m_pState->GetCurIndent();
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
switch(curState) {
|
||||
// document-level
|
||||
case ES_WAITING_FOR_DOC:
|
||||
std::cerr << "waiting for doc (pre)\n";
|
||||
m_pState->SwitchState(ES_WRITING_DOC);
|
||||
return true;
|
||||
case ES_WRITING_DOC:
|
||||
std::cerr << "writing doc (pre)\n";
|
||||
return true;
|
||||
|
||||
// block sequence
|
||||
case ES_WAITING_FOR_BLOCK_SEQ_ENTRY:
|
||||
std::cerr << "waiting for block seq entry (pre)\n";
|
||||
m_stream << IndentTo(curIndent) << "-";
|
||||
m_pState->RequireSeparation();
|
||||
m_pState->SwitchState(ES_WRITING_BLOCK_SEQ_ENTRY);
|
||||
return true;
|
||||
case ES_WRITING_BLOCK_SEQ_ENTRY:
|
||||
std::cerr << "writing block seq entry (pre)\n";
|
||||
return true;
|
||||
case ES_DONE_WITH_BLOCK_SEQ_ENTRY:
|
||||
std::cerr << "done with block seq entry (pre)\n";
|
||||
m_stream << '\n';
|
||||
m_pState->SwitchState(ES_WAITING_FOR_BLOCK_SEQ_ENTRY);
|
||||
return false;
|
||||
|
||||
// flow sequence
|
||||
case ES_WAITING_FOR_FLOW_SEQ_ENTRY:
|
||||
std::cerr << "waiting for flow seq entry (pre)\n";
|
||||
m_pState->SwitchState(ES_WRITING_FLOW_SEQ_ENTRY);
|
||||
return true;
|
||||
case ES_WRITING_FLOW_SEQ_ENTRY:
|
||||
std::cerr << "writing flow seq entry (pre)\n";
|
||||
return true;
|
||||
case ES_DONE_WITH_FLOW_SEQ_ENTRY:
|
||||
std::cerr << "done with flow seq entry (pre)\n";
|
||||
m_stream << ',';
|
||||
m_pState->RequireSeparation();
|
||||
m_pState->SwitchState(ES_WAITING_FOR_FLOW_SEQ_ENTRY);
|
||||
return false;
|
||||
|
||||
// block map
|
||||
case ES_WAITING_FOR_BLOCK_MAP_ENTRY:
|
||||
std::cerr << "waiting for block map entry (pre)\n";
|
||||
m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN);
|
||||
return true;
|
||||
case ES_WAITING_FOR_BLOCK_MAP_KEY:
|
||||
std::cerr << "waiting for block map key (pre)\n";
|
||||
if(m_pState->CurrentlyInLongKey()) {
|
||||
m_stream << IndentTo(curIndent) << '?';
|
||||
m_pState->RequireSeparation();
|
||||
}
|
||||
m_pState->SwitchState(ES_WRITING_BLOCK_MAP_KEY);
|
||||
return true;
|
||||
case ES_WRITING_BLOCK_MAP_KEY:
|
||||
std::cerr << "writing block map key (pre)\n";
|
||||
return true;
|
||||
case ES_DONE_WITH_BLOCK_MAP_KEY:
|
||||
std::cerr << "done with block map key (pre)\n";
|
||||
m_pState->SetError(ErrorMsg::EXPECTED_VALUE_TOKEN);
|
||||
return true;
|
||||
case ES_WAITING_FOR_BLOCK_MAP_VALUE:
|
||||
std::cerr << "waiting for block map value (pre)\n";
|
||||
if(m_pState->CurrentlyInLongKey())
|
||||
m_stream << IndentTo(curIndent);
|
||||
m_stream << ':';
|
||||
m_pState->RequireSeparation();
|
||||
m_pState->SwitchState(ES_WRITING_BLOCK_MAP_VALUE);
|
||||
return true;
|
||||
case ES_WRITING_BLOCK_MAP_VALUE:
|
||||
std::cerr << "writing block map value (pre)\n";
|
||||
return true;
|
||||
case ES_DONE_WITH_BLOCK_MAP_VALUE:
|
||||
std::cerr << "done with block map value (pre)\n";
|
||||
m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN);
|
||||
return true;
|
||||
|
||||
// flow map
|
||||
case ES_WAITING_FOR_FLOW_MAP_ENTRY:
|
||||
std::cerr << "waiting for flow map entry (pre)\n";
|
||||
m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN);
|
||||
return true;
|
||||
case ES_WAITING_FOR_FLOW_MAP_KEY:
|
||||
std::cerr << "waiting for flow map key (pre)\n";
|
||||
m_pState->SwitchState(ES_WRITING_FLOW_MAP_KEY);
|
||||
if(m_pState->CurrentlyInLongKey()) {
|
||||
EmitSeparationIfNecessary();
|
||||
m_stream << '?';
|
||||
m_pState->RequireSeparation();
|
||||
}
|
||||
return true;
|
||||
case ES_WRITING_FLOW_MAP_KEY:
|
||||
std::cerr << "writing flow map key (pre)\n";
|
||||
return true;
|
||||
case ES_DONE_WITH_FLOW_MAP_KEY:
|
||||
std::cerr << "done with flow map key (pre)\n";
|
||||
m_pState->SetError(ErrorMsg::EXPECTED_VALUE_TOKEN);
|
||||
return true;
|
||||
case ES_WAITING_FOR_FLOW_MAP_VALUE:
|
||||
std::cerr << "waiting for flow map value (pre)\n";
|
||||
m_stream << ':';
|
||||
m_pState->RequireSeparation();
|
||||
m_pState->SwitchState(ES_WRITING_FLOW_MAP_VALUE);
|
||||
return true;
|
||||
case ES_WRITING_FLOW_MAP_VALUE:
|
||||
std::cerr << "writing flow map value (pre)\n";
|
||||
return true;
|
||||
case ES_DONE_WITH_FLOW_MAP_VALUE:
|
||||
std::cerr << "done with flow map value (pre)\n";
|
||||
m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN);
|
||||
return true;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
|
||||
assert(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
// PreAtomicWrite
|
||||
// . Depending on the emitter state, write to the stream to get it
|
||||
// in position to do an atomic write (e.g., scalar, sequence, or map)
|
||||
void Emitter::PreAtomicWrite()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
while(!GotoNextPreAtomicState())
|
||||
;
|
||||
}
|
||||
|
||||
// PostAtomicWrite
|
||||
// . Clean up
|
||||
void Emitter::PostAtomicWrite()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
switch(curState) {
|
||||
// document-level
|
||||
case ES_WRITING_DOC:
|
||||
m_pState->SwitchState(ES_DONE_WITH_DOC);
|
||||
break;
|
||||
|
||||
// block seq
|
||||
case ES_WRITING_BLOCK_SEQ_ENTRY:
|
||||
m_pState->SwitchState(ES_DONE_WITH_BLOCK_SEQ_ENTRY);
|
||||
break;
|
||||
|
||||
// flow seq
|
||||
case ES_WRITING_FLOW_SEQ_ENTRY:
|
||||
m_pState->SwitchState(ES_DONE_WITH_FLOW_SEQ_ENTRY);
|
||||
break;
|
||||
|
||||
// block map
|
||||
case ES_WRITING_BLOCK_MAP_KEY:
|
||||
m_pState->SwitchState(ES_DONE_WITH_BLOCK_MAP_KEY);
|
||||
break;
|
||||
case ES_WRITING_BLOCK_MAP_VALUE:
|
||||
m_pState->SwitchState(ES_DONE_WITH_BLOCK_MAP_VALUE);
|
||||
break;
|
||||
|
||||
// flow map
|
||||
case ES_WRITING_FLOW_MAP_KEY:
|
||||
m_pState->SwitchState(ES_DONE_WITH_FLOW_MAP_KEY);
|
||||
break;
|
||||
case ES_WRITING_FLOW_MAP_VALUE:
|
||||
m_pState->SwitchState(ES_DONE_WITH_FLOW_MAP_VALUE);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
};
|
||||
|
||||
m_pState->ClearModifiedSettings();
|
||||
}
|
||||
|
||||
// EmitSeparationIfNecessary
|
||||
void Emitter::EmitSeparationIfNecessary()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
if(m_pState->RequiresSeparation())
|
||||
m_stream << ' ';
|
||||
m_pState->UnsetSeparation();
|
||||
}
|
||||
|
||||
// EmitBeginSeq
|
||||
void Emitter::EmitBeginSeq()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
// must have a long key if we're emitting a sequence
|
||||
m_pState->StartLongKey();
|
||||
|
||||
PreAtomicWrite();
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
EMITTER_MANIP flowType = m_pState->GetFlowType(GT_SEQ);
|
||||
if(flowType == Block) {
|
||||
if(curState == ES_WRITING_BLOCK_SEQ_ENTRY || curState == ES_WRITING_BLOCK_MAP_KEY || curState == ES_WRITING_BLOCK_MAP_VALUE) {
|
||||
m_stream << "\n";
|
||||
m_pState->UnsetSeparation();
|
||||
}
|
||||
m_pState->PushState(ES_WAITING_FOR_BLOCK_SEQ_ENTRY);
|
||||
} else if(flowType == Flow) {
|
||||
EmitSeparationIfNecessary();
|
||||
m_stream << "[";
|
||||
m_pState->PushState(ES_WAITING_FOR_FLOW_SEQ_ENTRY);
|
||||
} else
|
||||
assert(false);
|
||||
|
||||
m_pState->BeginGroup(GT_SEQ);
|
||||
}
|
||||
|
||||
// EmitEndSeq
|
||||
void Emitter::EmitEndSeq()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
if(m_pState->GetCurGroupType() != GT_SEQ)
|
||||
return m_pState->SetError(ErrorMsg::UNEXPECTED_END_SEQ);
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
|
||||
if(flowType == FT_BLOCK)
|
||||
assert(curState == ES_DONE_WITH_BLOCK_SEQ_ENTRY);
|
||||
else if(flowType == FT_FLOW) {
|
||||
m_stream << "]";
|
||||
// Note: flow sequences are allowed to be empty
|
||||
assert(curState == ES_DONE_WITH_FLOW_SEQ_ENTRY || curState == ES_WAITING_FOR_FLOW_SEQ_ENTRY);
|
||||
} else
|
||||
assert(false);
|
||||
|
||||
m_pState->PopState();
|
||||
m_pState->EndGroup(GT_SEQ);
|
||||
|
||||
PostAtomicWrite();
|
||||
}
|
||||
|
||||
// EmitBeginMap
|
||||
void Emitter::EmitBeginMap()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
// must have a long key if we're emitting a map
|
||||
m_pState->StartLongKey();
|
||||
|
||||
PreAtomicWrite();
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
EMITTER_MANIP flowType = m_pState->GetFlowType(GT_MAP);
|
||||
if(flowType == Block) {
|
||||
if(curState == ES_WRITING_BLOCK_SEQ_ENTRY || curState == ES_WRITING_BLOCK_MAP_KEY || curState == ES_WRITING_BLOCK_MAP_VALUE) {
|
||||
m_stream << "\n";
|
||||
m_pState->UnsetSeparation();
|
||||
}
|
||||
m_pState->PushState(ES_WAITING_FOR_BLOCK_MAP_ENTRY);
|
||||
} else if(flowType == Flow) {
|
||||
EmitSeparationIfNecessary();
|
||||
m_stream << "{";
|
||||
m_pState->PushState(ES_WAITING_FOR_FLOW_MAP_ENTRY);
|
||||
} else
|
||||
assert(false);
|
||||
|
||||
m_pState->BeginGroup(GT_MAP);
|
||||
}
|
||||
|
||||
// EmitEndMap
|
||||
void Emitter::EmitEndMap()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
if(m_pState->GetCurGroupType() != GT_MAP)
|
||||
return m_pState->SetError(ErrorMsg::UNEXPECTED_END_MAP);
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
|
||||
if(flowType == FT_BLOCK)
|
||||
assert(curState == ES_DONE_WITH_BLOCK_MAP_VALUE);
|
||||
else if(flowType == FT_FLOW) {
|
||||
m_stream << "}";
|
||||
// Note: flow maps are allowed to be empty
|
||||
assert(curState == ES_DONE_WITH_FLOW_MAP_VALUE || curState == ES_WAITING_FOR_FLOW_MAP_ENTRY);
|
||||
} else
|
||||
assert(false);
|
||||
|
||||
m_pState->PopState();
|
||||
m_pState->EndGroup(GT_MAP);
|
||||
|
||||
PostAtomicWrite();
|
||||
}
|
||||
|
||||
// EmitKey
|
||||
void Emitter::EmitKey()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
|
||||
if(curState != ES_WAITING_FOR_BLOCK_MAP_ENTRY && curState != ES_DONE_WITH_BLOCK_MAP_VALUE
|
||||
&& curState != ES_WAITING_FOR_FLOW_MAP_ENTRY && curState != ES_DONE_WITH_FLOW_MAP_VALUE)
|
||||
return m_pState->SetError(ErrorMsg::UNEXPECTED_KEY_TOKEN);
|
||||
|
||||
if(flowType == FT_BLOCK) {
|
||||
if(curState == ES_DONE_WITH_BLOCK_MAP_VALUE)
|
||||
m_stream << '\n';
|
||||
unsigned curIndent = m_pState->GetCurIndent();
|
||||
m_stream << IndentTo(curIndent);
|
||||
m_pState->SwitchState(ES_WAITING_FOR_BLOCK_MAP_KEY);
|
||||
} else if(flowType == FT_FLOW) {
|
||||
if(curState == ES_DONE_WITH_FLOW_MAP_VALUE) {
|
||||
m_stream << ',';
|
||||
m_pState->RequireSeparation();
|
||||
}
|
||||
m_pState->SwitchState(ES_WAITING_FOR_FLOW_MAP_KEY);
|
||||
} else
|
||||
assert(false);
|
||||
|
||||
if(m_pState->GetMapKeyFormat() == LongKey)
|
||||
m_pState->StartLongKey();
|
||||
else if(m_pState->GetMapKeyFormat() == Auto)
|
||||
m_pState->StartSimpleKey();
|
||||
else
|
||||
assert(false);
|
||||
}
|
||||
|
||||
// EmitValue
|
||||
void Emitter::EmitValue()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
|
||||
if(curState != ES_DONE_WITH_BLOCK_MAP_KEY && curState != ES_DONE_WITH_FLOW_MAP_KEY)
|
||||
return m_pState->SetError(ErrorMsg::UNEXPECTED_VALUE_TOKEN);
|
||||
|
||||
if(flowType == FT_BLOCK) {
|
||||
if(m_pState->CurrentlyInLongKey())
|
||||
m_stream << '\n';
|
||||
m_pState->SwitchState(ES_WAITING_FOR_BLOCK_MAP_VALUE);
|
||||
} else if(flowType == FT_FLOW) {
|
||||
m_pState->SwitchState(ES_WAITING_FOR_FLOW_MAP_VALUE);
|
||||
} else
|
||||
assert(false);
|
||||
}
|
||||
|
||||
// *******************************************************************************************
|
||||
// overloads of Write
|
||||
|
||||
Emitter& Emitter::Write(const std::string& str)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
// literal scalars must use long keys
|
||||
if(m_pState->GetStringFormat() == Literal && m_pState->GetCurGroupFlowType() != FT_FLOW)
|
||||
m_pState->StartLongKey();
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
|
||||
EMITTER_MANIP strFmt = m_pState->GetStringFormat();
|
||||
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
|
||||
unsigned curIndent = m_pState->GetCurIndent();
|
||||
|
||||
switch(strFmt) {
|
||||
case Auto:
|
||||
Utils::WriteString(m_stream, str, flowType == FT_FLOW);
|
||||
break;
|
||||
case SingleQuoted:
|
||||
if(!Utils::WriteSingleQuotedString(m_stream, str)) {
|
||||
m_pState->SetError(ErrorMsg::SINGLE_QUOTED_CHAR);
|
||||
return *this;
|
||||
}
|
||||
break;
|
||||
case DoubleQuoted:
|
||||
Utils::WriteDoubleQuotedString(m_stream, str);
|
||||
break;
|
||||
case Literal:
|
||||
if(flowType == FT_FLOW)
|
||||
Utils::WriteString(m_stream, str, flowType == FT_FLOW);
|
||||
else
|
||||
Utils::WriteLiteralString(m_stream, str, curIndent + m_pState->GetIndent());
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(const char *str)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
return Write(std::string(str));
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(int i)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
|
||||
EMITTER_MANIP intFmt = m_pState->GetIntFormat();
|
||||
std::stringstream str;
|
||||
switch(intFmt) {
|
||||
case Dec:
|
||||
str << std::dec;
|
||||
break;
|
||||
case Hex:
|
||||
str << std::hex;
|
||||
break;
|
||||
case Oct:
|
||||
str << std::oct;
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
|
||||
str << i;
|
||||
m_stream << str.str();
|
||||
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(bool b)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
|
||||
// set up all possible bools to write
|
||||
struct BoolName { std::string trueName, falseName; };
|
||||
struct BoolFormatNames { BoolName upper, lower, camel; };
|
||||
struct BoolTypes { BoolFormatNames yesNo, trueFalse, onOff; };
|
||||
|
||||
static const BoolTypes boolTypes = {
|
||||
{ { "YES", "NO" }, { "yes", "no" }, { "Yes", "No" } },
|
||||
{ { "TRUE", "FALSE" }, { "true", "false" }, { "True", "False" } },
|
||||
{ { "ON", "OFF" }, { "on", "off" }, { "On", "Off" } }
|
||||
};
|
||||
|
||||
// select the right one
|
||||
EMITTER_MANIP boolFmt = m_pState->GetBoolFormat();
|
||||
EMITTER_MANIP boolLengthFmt = m_pState->GetBoolLengthFormat();
|
||||
EMITTER_MANIP boolCaseFmt = m_pState->GetBoolCaseFormat();
|
||||
|
||||
const BoolFormatNames& fmtNames = (boolFmt == YesNoBool ? boolTypes.yesNo : boolFmt == TrueFalseBool ? boolTypes.trueFalse : boolTypes.onOff);
|
||||
const BoolName& boolName = (boolCaseFmt == UpperCase ? fmtNames.upper : boolCaseFmt == LowerCase ? fmtNames.lower : fmtNames.camel);
|
||||
const std::string& name = (b ? boolName.trueName : boolName.falseName);
|
||||
|
||||
// and say it!
|
||||
// TODO: should we disallow writing OnOffBool with ShortBool? (it'll just print "o" for both, which is silly)
|
||||
if(boolLengthFmt == ShortBool)
|
||||
m_stream << name[0];
|
||||
else
|
||||
m_stream << name;
|
||||
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(float f)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
|
||||
std::stringstream str;
|
||||
str << f;
|
||||
m_stream << str.str();
|
||||
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(double d)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
|
||||
std::stringstream str;
|
||||
str << d;
|
||||
m_stream << str.str();
|
||||
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(const _Alias& alias)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
if(!Utils::WriteAlias(m_stream, alias.content)) {
|
||||
m_pState->SetError(ErrorMsg::INVALID_ALIAS);
|
||||
return *this;
|
||||
}
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(const _Anchor& anchor)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
if(!Utils::WriteAnchor(m_stream, anchor.content)) {
|
||||
m_pState->SetError(ErrorMsg::INVALID_ANCHOR);
|
||||
return *this;
|
||||
}
|
||||
m_pState->RequireSeparation();
|
||||
// Note: no PostAtomicWrite() because we need another value for this node
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(const _Comment& comment)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
m_stream << Indentation(m_pState->GetPreCommentIndent());
|
||||
Utils::WriteComment(m_stream, comment.content, m_pState->GetPostCommentIndent());
|
||||
return *this;
|
||||
}
|
||||
}
|
263
src/emitterstate.cpp
Normal file
263
src/emitterstate.cpp
Normal file
@@ -0,0 +1,263 @@
|
||||
#include "emitterstate.h"
|
||||
#include "exceptions.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
EmitterState::EmitterState(): m_isGood(true), m_curIndent(0), m_requiresSeparation(false)
|
||||
{
|
||||
// start up
|
||||
m_stateStack.push(ES_WAITING_FOR_DOC);
|
||||
|
||||
// set default global manipulators
|
||||
m_strFmt.set(Auto);
|
||||
m_boolFmt.set(TrueFalseBool);
|
||||
m_boolLengthFmt.set(LongBool);
|
||||
m_boolCaseFmt.set(LowerCase);
|
||||
m_intFmt.set(Dec);
|
||||
m_indent.set(2);
|
||||
m_preCommentIndent.set(2);
|
||||
m_postCommentIndent.set(1);
|
||||
m_seqFmt.set(Block);
|
||||
m_mapFmt.set(Block);
|
||||
m_mapKeyFmt.set(Auto);
|
||||
}
|
||||
|
||||
EmitterState::~EmitterState()
|
||||
{
|
||||
while(!m_groups.empty())
|
||||
_PopGroup();
|
||||
}
|
||||
|
||||
std::auto_ptr <EmitterState::Group> EmitterState::_PopGroup()
|
||||
{
|
||||
if(m_groups.empty())
|
||||
return std::auto_ptr <Group> (0);
|
||||
|
||||
std::auto_ptr <Group> pGroup(m_groups.top());
|
||||
m_groups.pop();
|
||||
return pGroup;
|
||||
}
|
||||
|
||||
// SetLocalValue
|
||||
// . We blindly tries to set all possible formatters to this value
|
||||
// . Only the ones that make sense will be accepted
|
||||
void EmitterState::SetLocalValue(EMITTER_MANIP value)
|
||||
{
|
||||
SetStringFormat(value, LOCAL);
|
||||
SetBoolFormat(value, LOCAL);
|
||||
SetBoolCaseFormat(value, LOCAL);
|
||||
SetBoolLengthFormat(value, LOCAL);
|
||||
SetIntFormat(value, LOCAL);
|
||||
SetFlowType(GT_SEQ, value, LOCAL);
|
||||
SetFlowType(GT_MAP, value, LOCAL);
|
||||
SetMapKeyFormat(value, LOCAL);
|
||||
}
|
||||
|
||||
void EmitterState::BeginGroup(GROUP_TYPE type)
|
||||
{
|
||||
unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top()->indent);
|
||||
m_curIndent += lastIndent;
|
||||
|
||||
std::auto_ptr <Group> pGroup(new Group(type));
|
||||
|
||||
// transfer settings (which last until this group is done)
|
||||
pGroup->modifiedSettings = m_modifiedSettings;
|
||||
|
||||
// set up group
|
||||
pGroup->flow = GetFlowType(type);
|
||||
pGroup->indent = GetIndent();
|
||||
pGroup->usingLongKey = (GetMapKeyFormat() == LongKey ? true : false);
|
||||
|
||||
m_groups.push(pGroup.release());
|
||||
}
|
||||
|
||||
void EmitterState::EndGroup(GROUP_TYPE type)
|
||||
{
|
||||
if(m_groups.empty())
|
||||
return SetError(ErrorMsg::UNMATCHED_GROUP_TAG);
|
||||
|
||||
// get rid of the current group
|
||||
{
|
||||
std::auto_ptr <Group> pFinishedGroup = _PopGroup();
|
||||
if(pFinishedGroup->type != type)
|
||||
return SetError(ErrorMsg::UNMATCHED_GROUP_TAG);
|
||||
}
|
||||
|
||||
// reset old settings
|
||||
unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top()->indent);
|
||||
assert(m_curIndent >= lastIndent);
|
||||
m_curIndent -= lastIndent;
|
||||
|
||||
// some global settings that we changed may have been overridden
|
||||
// by a local setting we just popped, so we need to restore them
|
||||
m_globalModifiedSettings.restore();
|
||||
}
|
||||
|
||||
GROUP_TYPE EmitterState::GetCurGroupType() const
|
||||
{
|
||||
if(m_groups.empty())
|
||||
return GT_NONE;
|
||||
|
||||
return m_groups.top()->type;
|
||||
}
|
||||
|
||||
FLOW_TYPE EmitterState::GetCurGroupFlowType() const
|
||||
{
|
||||
if(m_groups.empty())
|
||||
return FT_NONE;
|
||||
|
||||
return (m_groups.top()->flow == Flow ? FT_FLOW : FT_BLOCK);
|
||||
}
|
||||
|
||||
bool EmitterState::CurrentlyInLongKey()
|
||||
{
|
||||
if(m_groups.empty())
|
||||
return false;
|
||||
return m_groups.top()->usingLongKey;
|
||||
}
|
||||
|
||||
void EmitterState::StartLongKey()
|
||||
{
|
||||
if(!m_groups.empty())
|
||||
m_groups.top()->usingLongKey = true;
|
||||
}
|
||||
|
||||
void EmitterState::StartSimpleKey()
|
||||
{
|
||||
if(!m_groups.empty())
|
||||
m_groups.top()->usingLongKey = false;
|
||||
}
|
||||
|
||||
void EmitterState::ClearModifiedSettings()
|
||||
{
|
||||
m_modifiedSettings.clear();
|
||||
}
|
||||
|
||||
bool EmitterState::SetStringFormat(EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case Auto:
|
||||
case SingleQuoted:
|
||||
case DoubleQuoted:
|
||||
case Literal:
|
||||
_Set(m_strFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EmitterState::SetBoolFormat(EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case OnOffBool:
|
||||
case TrueFalseBool:
|
||||
case YesNoBool:
|
||||
_Set(m_boolFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EmitterState::SetBoolLengthFormat(EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case LongBool:
|
||||
case ShortBool:
|
||||
_Set(m_boolLengthFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EmitterState::SetBoolCaseFormat(EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case UpperCase:
|
||||
case LowerCase:
|
||||
case CamelCase:
|
||||
_Set(m_boolCaseFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EmitterState::SetIntFormat(EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case Dec:
|
||||
case Hex:
|
||||
case Oct:
|
||||
_Set(m_intFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EmitterState::SetIndent(unsigned value, FMT_SCOPE scope)
|
||||
{
|
||||
if(value == 0)
|
||||
return false;
|
||||
|
||||
_Set(m_indent, value, scope);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EmitterState::SetPreCommentIndent(unsigned value, FMT_SCOPE scope)
|
||||
{
|
||||
if(value == 0)
|
||||
return false;
|
||||
|
||||
_Set(m_preCommentIndent, value, scope);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EmitterState::SetPostCommentIndent(unsigned value, FMT_SCOPE scope)
|
||||
{
|
||||
if(value == 0)
|
||||
return false;
|
||||
|
||||
_Set(m_postCommentIndent, value, scope);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EmitterState::SetFlowType(GROUP_TYPE groupType, EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case Block:
|
||||
case Flow:
|
||||
_Set(groupType == GT_SEQ ? m_seqFmt : m_mapFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
EMITTER_MANIP EmitterState::GetFlowType(GROUP_TYPE groupType) const
|
||||
{
|
||||
// force flow style if we're currently in a flow
|
||||
FLOW_TYPE flowType = GetCurGroupFlowType();
|
||||
if(flowType == FT_FLOW)
|
||||
return Flow;
|
||||
|
||||
// otherwise, go with what's asked of use
|
||||
return (groupType == GT_SEQ ? m_seqFmt.get() : m_mapFmt.get());
|
||||
}
|
||||
|
||||
bool EmitterState::SetMapKeyFormat(EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case Auto:
|
||||
case LongKey:
|
||||
_Set(m_mapKeyFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
195
src/emitterstate.h
Normal file
195
src/emitterstate.h
Normal file
@@ -0,0 +1,195 @@
|
||||
#pragma once
|
||||
|
||||
#include "setting.h"
|
||||
#include "emittermanip.h"
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <memory>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
enum FMT_SCOPE {
|
||||
LOCAL,
|
||||
GLOBAL
|
||||
};
|
||||
|
||||
enum GROUP_TYPE {
|
||||
GT_NONE,
|
||||
GT_SEQ,
|
||||
GT_MAP
|
||||
};
|
||||
|
||||
enum FLOW_TYPE {
|
||||
FT_NONE,
|
||||
FT_FLOW,
|
||||
FT_BLOCK
|
||||
};
|
||||
|
||||
enum NODE_STATE {
|
||||
NS_START,
|
||||
NS_READY_FOR_ATOM,
|
||||
NS_END
|
||||
};
|
||||
|
||||
enum EMITTER_STATE {
|
||||
ES_WAITING_FOR_DOC,
|
||||
ES_WRITING_DOC,
|
||||
ES_DONE_WITH_DOC,
|
||||
|
||||
// block seq
|
||||
ES_WAITING_FOR_BLOCK_SEQ_ENTRY,
|
||||
ES_WRITING_BLOCK_SEQ_ENTRY,
|
||||
ES_DONE_WITH_BLOCK_SEQ_ENTRY,
|
||||
|
||||
// flow seq
|
||||
ES_WAITING_FOR_FLOW_SEQ_ENTRY,
|
||||
ES_WRITING_FLOW_SEQ_ENTRY,
|
||||
ES_DONE_WITH_FLOW_SEQ_ENTRY,
|
||||
|
||||
// block map
|
||||
ES_WAITING_FOR_BLOCK_MAP_ENTRY,
|
||||
ES_WAITING_FOR_BLOCK_MAP_KEY,
|
||||
ES_WRITING_BLOCK_MAP_KEY,
|
||||
ES_DONE_WITH_BLOCK_MAP_KEY,
|
||||
ES_WAITING_FOR_BLOCK_MAP_VALUE,
|
||||
ES_WRITING_BLOCK_MAP_VALUE,
|
||||
ES_DONE_WITH_BLOCK_MAP_VALUE,
|
||||
|
||||
// flow map
|
||||
ES_WAITING_FOR_FLOW_MAP_ENTRY,
|
||||
ES_WAITING_FOR_FLOW_MAP_KEY,
|
||||
ES_WRITING_FLOW_MAP_KEY,
|
||||
ES_DONE_WITH_FLOW_MAP_KEY,
|
||||
ES_WAITING_FOR_FLOW_MAP_VALUE,
|
||||
ES_WRITING_FLOW_MAP_VALUE,
|
||||
ES_DONE_WITH_FLOW_MAP_VALUE,
|
||||
};
|
||||
|
||||
class EmitterState
|
||||
{
|
||||
public:
|
||||
EmitterState();
|
||||
~EmitterState();
|
||||
|
||||
// basic state checking
|
||||
bool good() const { return m_isGood; }
|
||||
const std::string GetLastError() const { return m_lastError; }
|
||||
void SetError(const std::string& error) { m_isGood = false; m_lastError = error; }
|
||||
|
||||
// main state of the machine
|
||||
EMITTER_STATE GetCurState() const { return m_stateStack.top(); }
|
||||
void SwitchState(EMITTER_STATE state) { PopState(); PushState(state); }
|
||||
void PushState(EMITTER_STATE state) { m_stateStack.push(state); }
|
||||
void PopState() { m_stateStack.pop(); }
|
||||
|
||||
void SetLocalValue(EMITTER_MANIP value);
|
||||
|
||||
// group handling
|
||||
void BeginGroup(GROUP_TYPE type);
|
||||
void EndGroup(GROUP_TYPE type);
|
||||
|
||||
GROUP_TYPE GetCurGroupType() const;
|
||||
FLOW_TYPE GetCurGroupFlowType() const;
|
||||
int GetCurIndent() const { return m_curIndent; }
|
||||
|
||||
bool CurrentlyInLongKey();
|
||||
void StartLongKey();
|
||||
void StartSimpleKey();
|
||||
|
||||
bool RequiresSeparation() const { return m_requiresSeparation; }
|
||||
void RequireSeparation() { m_requiresSeparation = true; }
|
||||
void UnsetSeparation() { m_requiresSeparation = false; }
|
||||
|
||||
void ClearModifiedSettings();
|
||||
|
||||
// formatters
|
||||
bool SetStringFormat(EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetStringFormat() const { return m_strFmt.get(); }
|
||||
|
||||
bool SetBoolFormat(EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetBoolFormat() const { return m_boolFmt.get(); }
|
||||
|
||||
bool SetBoolLengthFormat(EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetBoolLengthFormat() const { return m_boolLengthFmt.get(); }
|
||||
|
||||
bool SetBoolCaseFormat(EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetBoolCaseFormat() const { return m_boolCaseFmt.get(); }
|
||||
|
||||
bool SetIntFormat(EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetIntFormat() const { return m_intFmt.get(); }
|
||||
|
||||
bool SetIndent(unsigned value, FMT_SCOPE scope);
|
||||
int GetIndent() const { return m_indent.get(); }
|
||||
|
||||
bool SetPreCommentIndent(unsigned value, FMT_SCOPE scope);
|
||||
int GetPreCommentIndent() const { return m_preCommentIndent.get(); }
|
||||
bool SetPostCommentIndent(unsigned value, FMT_SCOPE scope);
|
||||
int GetPostCommentIndent() const { return m_postCommentIndent.get(); }
|
||||
|
||||
bool SetFlowType(GROUP_TYPE groupType, EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetFlowType(GROUP_TYPE groupType) const;
|
||||
|
||||
bool SetMapKeyFormat(EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetMapKeyFormat() const { return m_mapKeyFmt.get(); }
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
void _Set(Setting<T>& fmt, T value, FMT_SCOPE scope);
|
||||
|
||||
private:
|
||||
// basic state ok?
|
||||
bool m_isGood;
|
||||
std::string m_lastError;
|
||||
|
||||
// other state
|
||||
std::stack <EMITTER_STATE> m_stateStack;
|
||||
|
||||
Setting <EMITTER_MANIP> m_strFmt;
|
||||
Setting <EMITTER_MANIP> m_boolFmt;
|
||||
Setting <EMITTER_MANIP> m_boolLengthFmt;
|
||||
Setting <EMITTER_MANIP> m_boolCaseFmt;
|
||||
Setting <EMITTER_MANIP> m_intFmt;
|
||||
Setting <unsigned> m_indent;
|
||||
Setting <unsigned> m_preCommentIndent, m_postCommentIndent;
|
||||
Setting <EMITTER_MANIP> m_seqFmt;
|
||||
Setting <EMITTER_MANIP> m_mapFmt;
|
||||
Setting <EMITTER_MANIP> m_mapKeyFmt;
|
||||
|
||||
SettingChanges m_modifiedSettings;
|
||||
SettingChanges m_globalModifiedSettings;
|
||||
|
||||
struct Group {
|
||||
Group(GROUP_TYPE type_): type(type_), usingLongKey(false), indent(0) {}
|
||||
|
||||
GROUP_TYPE type;
|
||||
EMITTER_MANIP flow;
|
||||
bool usingLongKey;
|
||||
int indent;
|
||||
|
||||
SettingChanges modifiedSettings;
|
||||
};
|
||||
|
||||
std::auto_ptr <Group> _PopGroup();
|
||||
|
||||
std::stack <Group *> m_groups;
|
||||
unsigned m_curIndent;
|
||||
bool m_requiresSeparation;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void EmitterState::_Set(Setting<T>& fmt, T value, FMT_SCOPE scope) {
|
||||
switch(scope) {
|
||||
case LOCAL:
|
||||
m_modifiedSettings.push(fmt.set(value));
|
||||
break;
|
||||
case GLOBAL:
|
||||
fmt.set(value);
|
||||
m_globalModifiedSettings.push(fmt.set(value)); // this pushes an identity set, so when we restore,
|
||||
// it restores to the value here, and not the previous one
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
}
|
143
src/emitterutils.cpp
Normal file
143
src/emitterutils.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
#include "emitterutils.h"
|
||||
#include "exp.h"
|
||||
#include "indentation.h"
|
||||
#include "exceptions.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
namespace Utils
|
||||
{
|
||||
namespace {
|
||||
bool IsPrintable(char ch) {
|
||||
return (0x20 <= ch && ch <= 0x7E);
|
||||
}
|
||||
|
||||
bool IsValidPlainScalar(const std::string& str, bool inFlow) {
|
||||
// first check the start
|
||||
const RegEx& start = (inFlow ? Exp::PlainScalarInFlow : Exp::PlainScalar);
|
||||
if(!start.Matches(str))
|
||||
return false;
|
||||
|
||||
// and check the end for plain whitespace (which can't be faithfully kept in a plain scalar)
|
||||
if(!str.empty() && *str.rbegin() == ' ')
|
||||
return false;
|
||||
|
||||
// then check until something is disallowed
|
||||
const RegEx& disallowed = (inFlow ? Exp::EndScalarInFlow : Exp::EndScalar)
|
||||
|| (Exp::BlankOrBreak + Exp::Comment)
|
||||
|| (!Exp::Printable)
|
||||
|| Exp::Break
|
||||
|| Exp::Tab;
|
||||
Buffer buffer(&str[0], str.size());
|
||||
while(buffer.size) {
|
||||
if(disallowed.Matches(buffer))
|
||||
return false;
|
||||
++buffer;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool WriteString(ostream& out, const std::string& str, bool inFlow)
|
||||
{
|
||||
if(IsValidPlainScalar(str, inFlow)) {
|
||||
out << str;
|
||||
return true;
|
||||
} else
|
||||
return WriteDoubleQuotedString(out, str);
|
||||
}
|
||||
|
||||
bool WriteSingleQuotedString(ostream& out, const std::string& str)
|
||||
{
|
||||
out << "'";
|
||||
for(unsigned i=0;i<str.size();i++) {
|
||||
char ch = str[i];
|
||||
if(!IsPrintable(ch))
|
||||
return false;
|
||||
|
||||
if(ch == '\'')
|
||||
out << "''";
|
||||
else
|
||||
out << ch;
|
||||
}
|
||||
out << "'";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteDoubleQuotedString(ostream& out, const std::string& str)
|
||||
{
|
||||
out << "\"";
|
||||
for(unsigned i=0;i<str.size();i++) {
|
||||
char ch = str[i];
|
||||
if(IsPrintable(ch)) {
|
||||
if(ch == '\"')
|
||||
out << "\\\"";
|
||||
else if(ch == '\\')
|
||||
out << "\\\\";
|
||||
else
|
||||
out << ch;
|
||||
} else {
|
||||
// TODO: for the common escaped characters, give their usual symbol
|
||||
std::stringstream str;
|
||||
str << "\\x" << std::hex << static_cast <int>(ch);
|
||||
out << str.str();
|
||||
}
|
||||
}
|
||||
out << "\"";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteLiteralString(ostream& out, const std::string& str, int indent)
|
||||
{
|
||||
out << "|\n";
|
||||
out << IndentTo(indent);
|
||||
for(unsigned i=0;i<str.size();i++) {
|
||||
if(str[i] == '\n')
|
||||
out << "\n" << IndentTo(indent);
|
||||
else
|
||||
out << str[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteComment(ostream& out, const std::string& str, int postCommentIndent)
|
||||
{
|
||||
unsigned curIndent = out.col();
|
||||
out << "#" << Indentation(postCommentIndent);
|
||||
for(unsigned i=0;i<str.size();i++) {
|
||||
if(str[i] == '\n')
|
||||
out << "\n" << IndentTo(curIndent) << "#" << Indentation(postCommentIndent);
|
||||
else
|
||||
out << str[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteAlias(ostream& out, const std::string& str)
|
||||
{
|
||||
out << "*";
|
||||
for(unsigned i=0;i<str.size();i++) {
|
||||
if(!IsPrintable(str[i]) || str[i] == ' ' || str[i] == '\t' || str[i] == '\n' || str[i] == '\r')
|
||||
return false;
|
||||
|
||||
out << str[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteAnchor(ostream& out, const std::string& str)
|
||||
{
|
||||
out << "&";
|
||||
for(unsigned i=0;i<str.size();i++) {
|
||||
if(!IsPrintable(str[i]) || str[i] == ' ' || str[i] == '\t' || str[i] == '\n' || str[i] == '\r')
|
||||
return false;
|
||||
|
||||
out << str[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
18
src/emitterutils.h
Normal file
18
src/emitterutils.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "ostream.h"
|
||||
#include <string>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
namespace Utils
|
||||
{
|
||||
bool WriteString(ostream& out, const std::string& str, bool inFlow);
|
||||
bool WriteSingleQuotedString(ostream& out, const std::string& str);
|
||||
bool WriteDoubleQuotedString(ostream& out, const std::string& str);
|
||||
bool WriteLiteralString(ostream& out, const std::string& str, int indent);
|
||||
bool WriteComment(ostream& out, const std::string& str, int postCommentIndent);
|
||||
bool WriteAlias(ostream& out, const std::string& str);
|
||||
bool WriteAnchor(ostream& out, const std::string& str);
|
||||
}
|
||||
}
|
@@ -13,13 +13,16 @@ namespace YAML
|
||||
namespace Exp
|
||||
{
|
||||
// misc
|
||||
const RegEx Blank = RegEx(' ') || RegEx('\t');
|
||||
const RegEx Space = RegEx(' ');
|
||||
const RegEx Tab = RegEx('\t');
|
||||
const RegEx Blank = Space || Tab;
|
||||
const RegEx Break = RegEx('\n') || RegEx("\r\n");
|
||||
const RegEx BlankOrBreak = Blank || Break;
|
||||
const RegEx Digit = RegEx('0', '9');
|
||||
const RegEx Alpha = RegEx('a', 'z') || RegEx('A', 'Z');
|
||||
const RegEx AlphaNumeric = Alpha || Digit;
|
||||
const RegEx Hex = Digit || RegEx('A', 'F') || RegEx('a', 'f');
|
||||
const RegEx Printable = RegEx(0x20, 0x7E);
|
||||
|
||||
// actual tags
|
||||
|
||||
|
30
src/indentation.h
Normal file
30
src/indentation.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "ostream.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
struct Indentation {
|
||||
Indentation(unsigned n_): n(n_) {}
|
||||
unsigned n;
|
||||
};
|
||||
|
||||
inline ostream& operator << (ostream& out, const Indentation& indent) {
|
||||
for(unsigned i=0;i<indent.n;i++)
|
||||
out << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
struct IndentTo {
|
||||
IndentTo(unsigned n_): n(n_) {}
|
||||
unsigned n;
|
||||
};
|
||||
|
||||
inline ostream& operator << (ostream& out, const IndentTo& indent) {
|
||||
while(out.col() < indent.n)
|
||||
out << ' ';
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
63
src/ostream.cpp
Normal file
63
src/ostream.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "ostream.h"
|
||||
#include <cstring>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
ostream::ostream(): m_buffer(0), m_pos(0), m_size(0), m_row(0), m_col(0)
|
||||
{
|
||||
reserve(1024);
|
||||
}
|
||||
|
||||
ostream::~ostream()
|
||||
{
|
||||
delete [] m_buffer;
|
||||
}
|
||||
|
||||
void ostream::reserve(unsigned size)
|
||||
{
|
||||
if(size <= m_size)
|
||||
return;
|
||||
|
||||
char *newBuffer = new char[size];
|
||||
std::memset(newBuffer, 0, size * sizeof(char));
|
||||
std::memcpy(newBuffer, m_buffer, m_size * sizeof(char));
|
||||
delete [] m_buffer;
|
||||
m_buffer = newBuffer;
|
||||
m_size = size;
|
||||
}
|
||||
|
||||
void ostream::put(char ch)
|
||||
{
|
||||
if(m_pos >= m_size - 1) // an extra space for the NULL terminator
|
||||
reserve(m_size * 2);
|
||||
|
||||
m_buffer[m_pos] = ch;
|
||||
m_pos++;
|
||||
|
||||
if(ch == '\n') {
|
||||
m_row++;
|
||||
m_col = 0;
|
||||
} else
|
||||
m_col++;
|
||||
}
|
||||
|
||||
ostream& operator << (ostream& out, const char *str)
|
||||
{
|
||||
unsigned length = std::strlen(str);
|
||||
for(unsigned i=0;i<length;i++)
|
||||
out.put(str[i]);
|
||||
return out;
|
||||
}
|
||||
|
||||
ostream& operator << (ostream& out, const std::string& str)
|
||||
{
|
||||
out << str.c_str();
|
||||
return out;
|
||||
}
|
||||
|
||||
ostream& operator << (ostream& out, char ch)
|
||||
{
|
||||
out.put(ch);
|
||||
return out;
|
||||
}
|
||||
}
|
97
src/setting.h
Normal file
97
src/setting.h
Normal file
@@ -0,0 +1,97 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "noncopyable.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class SettingChangeBase;
|
||||
|
||||
template <typename T>
|
||||
class Setting
|
||||
{
|
||||
public:
|
||||
Setting(): m_value() {}
|
||||
|
||||
const T get() const { return m_value; }
|
||||
std::auto_ptr <SettingChangeBase> set(const T& value);
|
||||
void restore(const Setting<T>& oldSetting) {
|
||||
m_value = oldSetting.get();
|
||||
}
|
||||
|
||||
private:
|
||||
T m_value;
|
||||
};
|
||||
|
||||
class SettingChangeBase
|
||||
{
|
||||
public:
|
||||
virtual ~SettingChangeBase() {}
|
||||
virtual void pop() = 0;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class SettingChange: public SettingChangeBase
|
||||
{
|
||||
public:
|
||||
SettingChange(Setting<T> *pSetting): m_pCurSetting(pSetting) {
|
||||
// copy old setting to save its state
|
||||
m_oldSetting = *pSetting;
|
||||
}
|
||||
|
||||
virtual void pop() {
|
||||
m_pCurSetting->restore(m_oldSetting);
|
||||
}
|
||||
|
||||
private:
|
||||
Setting<T> *m_pCurSetting;
|
||||
Setting<T> m_oldSetting;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline std::auto_ptr <SettingChangeBase> Setting<T>::set(const T& value) {
|
||||
std::auto_ptr <SettingChangeBase> pChange(new SettingChange<T> (this));
|
||||
m_value = value;
|
||||
return pChange;
|
||||
}
|
||||
|
||||
class SettingChanges: private noncopyable
|
||||
{
|
||||
public:
|
||||
SettingChanges() {}
|
||||
~SettingChanges() { clear(); }
|
||||
|
||||
void clear() {
|
||||
restore();
|
||||
|
||||
for(setting_changes::const_iterator it=m_settingChanges.begin();it!=m_settingChanges.end();++it)
|
||||
delete *it;
|
||||
m_settingChanges.clear();
|
||||
}
|
||||
|
||||
void restore() {
|
||||
for(setting_changes::const_iterator it=m_settingChanges.begin();it!=m_settingChanges.end();++it)
|
||||
(*it)->pop();
|
||||
}
|
||||
|
||||
void push(std::auto_ptr <SettingChangeBase> pSettingChange) {
|
||||
m_settingChanges.push_back(pSettingChange.release());
|
||||
}
|
||||
|
||||
// like std::auto_ptr - assignment is transfer of ownership
|
||||
SettingChanges& operator = (SettingChanges& rhs) {
|
||||
if(this == &rhs)
|
||||
return *this;
|
||||
|
||||
clear();
|
||||
m_settingChanges = rhs.m_settingChanges;
|
||||
rhs.m_settingChanges.clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
typedef std::vector <SettingChangeBase *> setting_changes;
|
||||
setting_changes m_settingChanges;
|
||||
};
|
||||
}
|
@@ -7,14 +7,15 @@ namespace YAML
|
||||
{
|
||||
// a simple buffer wrapper that knows how big it is
|
||||
struct Buffer {
|
||||
Buffer(char *b, int s): buffer(b), size(s) {}
|
||||
Buffer(const char *b, int s): buffer(b), size(s) {}
|
||||
|
||||
operator bool() const { return size > 0; }
|
||||
bool operator !() const { return !static_cast <bool> (*this); }
|
||||
char operator [] (int i) const { return buffer[i]; }
|
||||
const Buffer operator + (int offset) const { return Buffer(buffer + offset, size - offset); }
|
||||
Buffer& operator ++ () { ++buffer; --size; return *this; }
|
||||
|
||||
char *buffer;
|
||||
const char *buffer;
|
||||
int size;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user