src/INIWriter.h
branchv_0
changeset 2 e753a7f967c8
parent 0 1bb084f84eb9
child 3 ae8775e0bc7a
--- a/src/INIWriter.h	Wed Dec 09 23:53:30 2020 +0100
+++ b/src/INIWriter.h	Fri Dec 11 12:34:42 2020 +0100
@@ -35,6 +35,8 @@
 	std::string commentSeparatorForSections = " ; ";
 	std::string commentSeparatorForEntries = " ; ";
 	std::string commentSeparatorStandalone = "; ";
+	
+	bool hasContent = false;
 
 	enum class TokenType {
 		SectionName,
@@ -117,14 +119,15 @@
 	}
 
 	void startSection(const SectionStartEvent& event) {
+		if (hasContent) output << std::endl;
 		output << "[" << escape(TokenType::SectionName, event.name) << "]";
 		if (event.tag.size()) output << "[" << escape(TokenType::SectionTag, event.tag) << "]";
 		if (event.comment.size()) output << commentSeparatorForSections << escape(TokenType::SectionComment, event.comment);
 		output << std::endl;
+		hasContent = true;
 	}
 
 	void endSection() {
-		output << std::endl;
 		output.flush();
 	}
 
@@ -134,10 +137,13 @@
 		output << keyValueSeparator << escape(TokenType::EntryValue, event.value);
 		if (event.comment.size()) output << commentSeparatorForEntries << escape(TokenType::EntryComment, event.comment);
 		output << std::endl;
+		hasContent = true;
 	}
 
 	void comment(const CommentEvent& event) {
 		output << commentSeparatorStandalone << escape(TokenType::StandaloneComment, event.comment);
+		output << std::endl;
+		hasContent = true;
 	}
 
 	void whitespace(const WhitespaceEvent& event) {
@@ -148,6 +154,7 @@
 			else if (ch == L'\r'); // TODO: keep CR?
 			else; // TODO: throw exception if whitespace contains unexpected data? (should not happen)
 		}
+		hasContent = true;
 	}