Streamlined validation object usage

This commit is contained in:
Jack Andersen 2016-03-01 10:28:42 -10:00
parent b317ac9d9f
commit 92d4334a74
2 changed files with 28 additions and 34 deletions

View File

@ -535,7 +535,9 @@ public:
return true; return true;
} }
static bool ValidateClassType(yaml_parser_t* doc, const char* expectedType); bool ClassTypeOperation(std::function<bool(const char* dnaType)> func);
bool ValidateClassType(const char* expectedType);
inline const YAMLNode* getRootNode() const {return m_rootNode.get();} inline const YAMLNode* getRootNode() const {return m_rootNode.get();}
inline const YAMLNode* getCurNode() const {return m_subStack.empty() ? nullptr : m_subStack.back();} inline const YAMLNode* getCurNode() const {return m_subStack.empty() ? nullptr : m_subStack.back();}
std::unique_ptr<YAMLNode> releaseRootNode() {return std::move(m_rootNode);} std::unique_ptr<YAMLNode> releaseRootNode() {return std::move(m_rootNode);}
@ -1166,16 +1168,10 @@ struct DNAYaml : DNA<DNAE>
template<class DNASubtype> template<class DNASubtype>
static bool ValidateFromYAMLString(const std::string& str) static bool ValidateFromYAMLString(const std::string& str)
{ {
yaml_parser_t parser;
if (!yaml_parser_initialize(&parser))
{
HandleYAMLParserError(&parser);
return false;
}
YAMLStdStringReaderState reader(str); YAMLStdStringReaderState reader(str);
yaml_parser_set_input(&parser, (yaml_read_handler_t*)YAMLStdStringReader, &reader); YAMLDocReader docReader;
bool retval = YAMLDocReader::ValidateClassType(&parser, DNASubtype::DNAType()); yaml_parser_set_input(docReader.getParser(), (yaml_read_handler_t*)YAMLStdStringReader, &reader);
yaml_parser_delete(&parser); bool retval = docReader.ValidateClassType(DNASubtype::DNAType());
return retval; return retval;
} }
@ -1207,17 +1203,11 @@ struct DNAYaml : DNA<DNAE>
template<class DNASubtype> template<class DNASubtype>
static bool ValidateFromYAMLFile(FILE* fin) static bool ValidateFromYAMLFile(FILE* fin)
{ {
yaml_parser_t parser; YAMLDocReader reader;
if (!yaml_parser_initialize(&parser))
{
HandleYAMLParserError(&parser);
return false;
}
long pos = ftell(fin); long pos = ftell(fin);
yaml_parser_set_input_file(&parser, fin); yaml_parser_set_input_file(reader.getParser(), fin);
bool retval = YAMLDocReader::ValidateClassType(&parser, DNASubtype::DNAType()); bool retval = reader.ValidateClassType(DNASubtype::DNAType());
fseek(fin, pos, SEEK_SET); fseek(fin, pos, SEEK_SET);
yaml_parser_delete(&parser);
return retval; return retval;
} }

View File

@ -169,28 +169,25 @@ std::unique_ptr<YAMLNode> YAMLDocReader::ParseEvents()
return std::unique_ptr<YAMLNode>(); return std::unique_ptr<YAMLNode>();
} }
bool YAMLDocReader::ValidateClassType(yaml_parser_t* doc, const char* expectedType) bool YAMLDocReader::ClassTypeOperation(std::function<bool(const char* dnaType)> func)
{ {
if (!expectedType)
return false;
yaml_event_t event; yaml_event_t event;
if (!yaml_parser_parse(doc, &event)) if (!yaml_parser_parse(&m_parser, &event))
{ {
HandleYAMLParserError(doc); HandleYAMLParserError(&m_parser);
return false; return false;
} }
int result; int result;
int mappingLevel = 0; int mappingLevel = 0;
bool inDNA = false; bool inDNA = false;
for (result = yaml_parser_parse(doc, &event); for (result = yaml_parser_parse(&m_parser, &event);
event.type != YAML_STREAM_END_EVENT; event.type != YAML_STREAM_END_EVENT;
result = yaml_parser_parse(doc, &event)) result = yaml_parser_parse(&m_parser, &event))
{ {
if (!result) if (!result)
{ {
HandleYAMLParserError(doc); HandleYAMLParserError(&m_parser);
return false; return false;
} }
switch (event.type) switch (event.type)
@ -201,13 +198,9 @@ bool YAMLDocReader::ValidateClassType(yaml_parser_t* doc, const char* expectedTy
{ {
if (inDNA) if (inDNA)
{ {
if (!strcmp(expectedType, reinterpret_cast<const char*>(event.data.scalar.value))) bool result = func(reinterpret_cast<const char*>(event.data.scalar.value));
{
yaml_event_delete(&event);
return true;
}
yaml_event_delete(&event); yaml_event_delete(&event);
return false; return result;
} }
if (!strcmp("DNAType", reinterpret_cast<const char*>(event.data.scalar.value))) if (!strcmp("DNAType", reinterpret_cast<const char*>(event.data.scalar.value)))
inDNA = true; inDNA = true;
@ -239,6 +232,17 @@ bool YAMLDocReader::ValidateClassType(yaml_parser_t* doc, const char* expectedTy
return false; return false;
} }
bool YAMLDocReader::ValidateClassType(const char* expectedType)
{
if (!expectedType)
return false;
return ClassTypeOperation([&](const char* dnaType) -> bool
{
return (strcmp(expectedType, dnaType) == 0);
});
}
static inline bool EmitKeyScalar(yaml_emitter_t* doc, const char* val) static inline bool EmitKeyScalar(yaml_emitter_t* doc, const char* val)
{ {
yaml_event_t event; yaml_event_t event;