src/INIStandardHandler.h
branchv_0
changeset 2 e753a7f967c8
parent 0 1bb084f84eb9
--- a/src/INIStandardHandler.h	Wed Dec 09 23:53:30 2020 +0100
+++ b/src/INIStandardHandler.h	Fri Dec 11 12:34:42 2020 +0100
@@ -39,22 +39,80 @@
 class INIStandardHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
 private:
 	INIWriter& writer;
+	size_t currentAttributeIndex = 0;
+	std::vector<relpipe::reader::handlers::AttributeMetadata> currentAttributes;
+	relpipe::common::type::StringX previousSection;
+	relpipe::common::type::StringX currentSection;
+	relpipe::common::type::StringX currentKey;
+	relpipe::common::type::StringX currentSubKey;
+	relpipe::common::type::StringX currentValue;
+	relpipe::common::type::StringX currentComment;
+	relpipe::common::type::StringX currentWhitespace;
+	bool hasSections = false;
 public:
 
 	INIStandardHandler(INIWriter& writer) : writer(writer) {
 	}
 
 	virtual ~INIStandardHandler() {
-
 	}
 
 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
+		currentAttributes = attributes;
+		currentSection.clear();
+		currentKey.clear();
+		currentSubKey.clear();
+		currentValue.clear();
+		currentComment.clear();
+		currentWhitespace.clear();
 	}
 
 	void attribute(const relpipe::common::type::StringX& value) override {
+		auto name = currentAttributes[currentAttributeIndex % currentAttributes.size()].getAttributeName();
+		if (name == L"section") currentSection = value;
+		else if (name == L"key") currentKey = value;
+		else if (name == L"sub_key") currentSubKey = value;
+		else if (name == L"value") currentValue = value;
+		else if (name == L"comment") currentComment = value;
+		else if (name == L"whitespace") currentWhitespace = value;
+
+		currentAttributeIndex++;
+
+		if (currentAttributeIndex == currentAttributes.size()) {
+			currentAttributeIndex = 0;
+
+			if (currentSection != previousSection) {
+				if (previousSection.size()) writer.endSection();
+				writer.startSection({
+					.comment = currentKey.empty() ? currentComment : L"",
+					.name = currentSection,
+					.tag = L"",
+				});
+				previousSection = currentSection;
+				hasSections = true;
+			}
+
+			if (currentKey.size()) {
+				writer.entry({
+					.comment = currentComment,
+					.key = currentKey,
+					.subKey = currentSubKey,
+					.value = currentValue,
+				});
+			} else if (currentComment.size()) {
+				writer.comment({
+					.comment = currentComment,
+				});
+			} else if (currentWhitespace.size()) {
+				writer.whitespace({
+					.whitespace = currentWhitespace,
+				});
+			}
+		}
 	}
 
 	void endOfPipe() {
+		if (hasSections) writer.endSection();
 	}
 
 };