simplify FlatINIContentHandler v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 23 Nov 2020 17:21:04 +0100
branchv_0
changeset 7 95b21edc9519
parent 6 fb717cfbfea1
child 8 83b23480d41f
simplify FlatINIContentHandler
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 <relpipe/writer/RelationalWriter.h>
 #include <relpipe/writer/RelpipeWriterException.h>
 #include <relpipe/writer/AttributeMetadata.h>
-#include <relpipe/writer/TypeId.h>
+#include <relpipe/common/type/typedefs.h>
 
 #include <relpipe/cli/CLI.h>
 
@@ -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::RelationalWriter> 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
 
 };