# HG changeset patch # User František Kučera # Date 1606148464 -3600 # Node ID 95b21edc9519e8af066ac1b7070b10c9c31a3551 # Parent fb717cfbfea1e60861ebec7c93b4461ef3e2741a simplify FlatINIContentHandler diff -r fb717cfbfea1 -r 95b21edc9519 src/INICommand.cpp --- a/src/INICommand.cpp Mon Nov 23 16:25:57 2020 +0100 +++ b/src/INICommand.cpp Mon Nov 23 17:21:04 2020 +0100 @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include @@ -56,6 +56,37 @@ return result.str(); } + class Record { + public: + relpipe::common::type::Integer lineNumber = -1; + relpipe::common::type::Integer eventNumber = -1; + std::string key; + std::string subKey; + std::string value; + std::string comment; + std::string whitespace; + + Record(const Event * const event) : lineNumber(event->lineNumber), eventNumber(event->eventNumber) { + } + + }; + + void write(const Record& record) { + if (configuration.enableLineNumbers) writer->writeAttribute(&record.lineNumber, typeid (record.lineNumber)); + if (configuration.enableEventNumbers) writer->writeAttribute(&record.eventNumber, typeid (record.eventNumber)); + + std::string section = getCurrentSectionFullName(); + std::string key = record.key; + if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section)); + else if (section.size()) key = section + "/" + record.key; + writer->writeAttribute(convertor.from_bytes(key)); + if (configuration.enableSubKeys) writer->writeAttribute(convertor.from_bytes(record.subKey)); + + writer->writeAttribute(convertor.from_bytes(record.value)); + if (configuration.enableComments) writer->writeAttribute(convertor.from_bytes(record.comment)); + if (configuration.enableWhitespace) writer->writeAttribute(convertor.from_bytes(record.whitespace)); + } + public: FlatINIContentHandler(std::shared_ptr writer, Configuration& configuration) : writer(writer), configuration(configuration) { @@ -90,60 +121,29 @@ }; void entry(const EntryEvent& event) override { - if (configuration.enableLineNumbers) writer->writeAttribute(&event.lineNumber, typeid (event.lineNumber)); - if (configuration.enableEventNumbers) writer->writeAttribute(&event.eventNumber, typeid (event.eventNumber)); - - std::string section = getCurrentSectionFullName(); - std::string key = configuration.enableSubKeys ? event.key : event.fullKey; - - if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section)); - else if (section.size()) key = section + "/" + key; - writer->writeAttribute(convertor.from_bytes(key)); - if (configuration.enableSubKeys) writer->writeAttribute(convertor.from_bytes(event.subKey)); - - writer->writeAttribute(convertor.from_bytes(event.value)); - if (configuration.enableComments) writer->writeAttribute(convertor.from_bytes(event.comment)); - if (configuration.enableWhitespace) writer->writeAttribute(L""); + Record record(&event); + record.comment = event.comment; + record.key = configuration.enableSubKeys ? event.key : event.fullKey; + record.subKey = event.subKey; + record.value = event.value; + write(record); }; void comment(const CommentEvent& event) override { if (configuration.enableComments) { - if (configuration.enableLineNumbers) writer->writeAttribute(&event.lineNumber, typeid (event.lineNumber)); - if (configuration.enableEventNumbers) writer->writeAttribute(&event.eventNumber, typeid (event.eventNumber)); - - std::string section = getCurrentSectionFullName(); - std::string key; - - if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section)); - else if (section.size()) key = section + "/"; - writer->writeAttribute(convertor.from_bytes(key)); - - if (configuration.enableSubKeys) writer->writeAttribute(L""); - writer->writeAttribute(L""); // value - writer->writeAttribute(convertor.from_bytes(event.comment)); - if (configuration.enableWhitespace) writer->writeAttribute(L""); + Record record(&event); + record.comment = event.comment; + write(record); } } void whitespace(const WhitespaceEvent& event) override { if (configuration.enableWhitespace) { - if (configuration.enableLineNumbers) writer->writeAttribute(&event.lineNumber, typeid (event.lineNumber)); - if (configuration.enableEventNumbers) writer->writeAttribute(&event.eventNumber, typeid (event.eventNumber)); - - std::string section = getCurrentSectionFullName(); - std::string key; - - if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section)); - else if (section.size()) key = section + "/"; - writer->writeAttribute(convertor.from_bytes(key)); - - if (configuration.enableSubKeys) writer->writeAttribute(L""); - writer->writeAttribute(L""); // value - if (configuration.enableComments) writer->writeAttribute(L""); - writer->writeAttribute(convertor.from_bytes(event.whitespace)); + Record record(&event); + record.whitespace = event.whitespace; + write(record); } } - // TODO: unify methods, DRY };