YamlDocWriter simplification

This commit is contained in:
Jack Andersen 2016-02-29 16:57:53 -10:00
parent ca0ff04340
commit b317ac9d9f
1 changed files with 21 additions and 38 deletions

View File

@ -815,25 +815,32 @@ public:
yaml_emitter_t* getEmitter() {return &m_emitter;} yaml_emitter_t* getEmitter() {return &m_emitter;}
bool open() bool finish()
{ {
if (!yaml_emitter_open(&m_emitter)) yaml_event_t event;
{
HandleYAMLEmitterError(&m_emitter); if (!yaml_emitter_open(&m_emitter))
return false; goto err;
}
return true; event.type = YAML_DOCUMENT_START_EVENT;
} event.data.document_start.implicit = true;
if (!yaml_emitter_emit(&m_emitter, &event))
goto err;
if (!RecursiveFinish(&m_emitter, m_rootNode))
return false;
event.type = YAML_DOCUMENT_END_EVENT;
event.data.document_end.implicit = true;
if (!yaml_emitter_emit(&m_emitter, &event))
goto err;
bool close()
{
if (!yaml_emitter_close(&m_emitter) || if (!yaml_emitter_close(&m_emitter) ||
!yaml_emitter_flush(&m_emitter)) !yaml_emitter_flush(&m_emitter))
{ goto err;
HandleYAMLEmitterError(&m_emitter);
return false;
}
return true; return true;
err:
HandleYAMLEmitterError(&m_emitter);
return false;
} }
inline YAMLNode* getCurNode() const {return m_subStack.empty() ? nullptr : m_subStack.back();} inline YAMLNode* getCurNode() const {return m_subStack.empty() ? nullptr : m_subStack.back();}
@ -960,24 +967,6 @@ public:
leaveSubVector(); leaveSubVector();
} }
bool finish()
{
yaml_event_t event = {YAML_DOCUMENT_START_EVENT};
event.data.document_start.implicit = true;
if (!yaml_emitter_emit(&m_emitter, &event))
goto err;
if (!RecursiveFinish(&m_emitter, m_rootNode))
return false;
event.type = YAML_DOCUMENT_END_EVENT;
event.data.document_end.implicit = true;
if (!yaml_emitter_emit(&m_emitter, &event))
goto err;
return true;
err:
HandleYAMLEmitterError(&m_emitter);
return false;
}
template <typename INTYPE> template <typename INTYPE>
void writeVal(const char* name, const INTYPE& val) void writeVal(const char* name, const INTYPE& val)
{ {
@ -1156,12 +1145,9 @@ struct DNAYaml : DNA<DNAE>
yaml_emitter_set_unicode(docWriter.getEmitter(), true); yaml_emitter_set_unicode(docWriter.getEmitter(), true);
yaml_emitter_set_width(docWriter.getEmitter(), -1); yaml_emitter_set_width(docWriter.getEmitter(), -1);
if (!docWriter.open())
return std::string();
write(docWriter); write(docWriter);
if (!docWriter.finish()) if (!docWriter.finish())
return std::string(); return std::string();
docWriter.close();
return res; return res;
} }
@ -1201,12 +1187,9 @@ struct DNAYaml : DNA<DNAE>
yaml_emitter_set_unicode(docWriter.getEmitter(), true); yaml_emitter_set_unicode(docWriter.getEmitter(), true);
yaml_emitter_set_width(docWriter.getEmitter(), -1); yaml_emitter_set_width(docWriter.getEmitter(), -1);
if (!docWriter.open())
return false;
write(docWriter); write(docWriter);
if (!docWriter.finish()) if (!docWriter.finish())
return false; return false;
docWriter.close();
return true; return true;
} }