diff --git a/include/Athena/DNAYaml.hpp b/include/Athena/DNAYaml.hpp index be22337..4a6ad3a 100644 --- a/include/Athena/DNAYaml.hpp +++ b/include/Athena/DNAYaml.hpp @@ -535,7 +535,9 @@ public: return true; } - static bool ValidateClassType(yaml_parser_t* doc, const char* expectedType); + bool ClassTypeOperation(std::function func); + bool ValidateClassType(const char* expectedType); + inline const YAMLNode* getRootNode() const {return m_rootNode.get();} inline const YAMLNode* getCurNode() const {return m_subStack.empty() ? nullptr : m_subStack.back();} std::unique_ptr releaseRootNode() {return std::move(m_rootNode);} @@ -1166,16 +1168,10 @@ struct DNAYaml : DNA template static bool ValidateFromYAMLString(const std::string& str) { - yaml_parser_t parser; - if (!yaml_parser_initialize(&parser)) - { - HandleYAMLParserError(&parser); - return false; - } YAMLStdStringReaderState reader(str); - yaml_parser_set_input(&parser, (yaml_read_handler_t*)YAMLStdStringReader, &reader); - bool retval = YAMLDocReader::ValidateClassType(&parser, DNASubtype::DNAType()); - yaml_parser_delete(&parser); + YAMLDocReader docReader; + yaml_parser_set_input(docReader.getParser(), (yaml_read_handler_t*)YAMLStdStringReader, &reader); + bool retval = docReader.ValidateClassType(DNASubtype::DNAType()); return retval; } @@ -1207,17 +1203,11 @@ struct DNAYaml : DNA template static bool ValidateFromYAMLFile(FILE* fin) { - yaml_parser_t parser; - if (!yaml_parser_initialize(&parser)) - { - HandleYAMLParserError(&parser); - return false; - } + YAMLDocReader reader; long pos = ftell(fin); - yaml_parser_set_input_file(&parser, fin); - bool retval = YAMLDocReader::ValidateClassType(&parser, DNASubtype::DNAType()); + yaml_parser_set_input_file(reader.getParser(), fin); + bool retval = reader.ValidateClassType(DNASubtype::DNAType()); fseek(fin, pos, SEEK_SET); - yaml_parser_delete(&parser); return retval; } diff --git a/src/Athena/DNAYaml.cpp b/src/Athena/DNAYaml.cpp index f4016ce..d590889 100644 --- a/src/Athena/DNAYaml.cpp +++ b/src/Athena/DNAYaml.cpp @@ -169,28 +169,25 @@ std::unique_ptr YAMLDocReader::ParseEvents() return std::unique_ptr(); } -bool YAMLDocReader::ValidateClassType(yaml_parser_t* doc, const char* expectedType) +bool YAMLDocReader::ClassTypeOperation(std::function func) { - if (!expectedType) - return false; - yaml_event_t event; - if (!yaml_parser_parse(doc, &event)) + if (!yaml_parser_parse(&m_parser, &event)) { - HandleYAMLParserError(doc); + HandleYAMLParserError(&m_parser); return false; } int result; int mappingLevel = 0; bool inDNA = false; - for (result = yaml_parser_parse(doc, &event); + for (result = yaml_parser_parse(&m_parser, &event); event.type != YAML_STREAM_END_EVENT; - result = yaml_parser_parse(doc, &event)) + result = yaml_parser_parse(&m_parser, &event)) { if (!result) { - HandleYAMLParserError(doc); + HandleYAMLParserError(&m_parser); return false; } switch (event.type) @@ -201,13 +198,9 @@ bool YAMLDocReader::ValidateClassType(yaml_parser_t* doc, const char* expectedTy { if (inDNA) { - if (!strcmp(expectedType, reinterpret_cast(event.data.scalar.value))) - { - yaml_event_delete(&event); - return true; - } + bool result = func(reinterpret_cast(event.data.scalar.value)); yaml_event_delete(&event); - return false; + return result; } if (!strcmp("DNAType", reinterpret_cast(event.data.scalar.value))) inDNA = true; @@ -239,6 +232,17 @@ bool YAMLDocReader::ValidateClassType(yaml_parser_t* doc, const char* expectedTy 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) { yaml_event_t event;