implement --writer-option skip-empty and skip-null (omit entries with missing values) v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 09 Sep 2021 18:57:37 +0200
branchv_0
changeset 8 7c85dc9a310b
parent 7 bda9d3c83cb7
child 9 42dbdee1d9f0
implement --writer-option skip-empty and skip-null (omit entries with missing values)
bash-completion.sh
src/INIWriter.h
src/uri.h
--- a/bash-completion.sh	Sun Dec 13 16:47:25 2020 +0100
+++ b/bash-completion.sh	Thu Sep 09 18:57:37 2021 +0200
@@ -61,6 +61,8 @@
 		"quotes-pattern-for-sections"
 		"quotes-pattern-for-keys"
 		"quotes-pattern-for-values"
+		"skip-empty"
+		"skip-null"
 		"allow-sections"
 		"hierarchy-separator"
 	);
@@ -80,6 +82,8 @@
 	elif [[ "$w2" == "--writer-option" && "$w1" == "quotes-type-for-sections"                 ]];    then COMPREPLY=($(compgen -W "${QUOTING_TYPES[*]}" -- "$w0"))
 	elif [[ "$w2" == "--writer-option" && "$w1" == "quotes-type-for-keys"                     ]];    then COMPREPLY=($(compgen -W "${QUOTING_TYPES[*]}" -- "$w0"))
 	elif [[ "$w2" == "--writer-option" && "$w1" == "quotes-type-for-values"                   ]];    then COMPREPLY=($(compgen -W "${QUOTING_TYPES[*]}" -- "$w0"))
+	elif [[ "$w2" == "--writer-option" && "$w1" == "skip-empty"                               ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
+	elif [[ "$w2" == "--writer-option" && "$w1" == "skip-null"                                ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
 	elif [[ "$w2" == "--writer-option" && "$w1" == "allow-sections"                           ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
 	elif [[ "$w2" == "--writer-option" && "$w1" == "hierarchy-separator"                      ]];    then COMPREPLY=("'/'")
 	elif [[ "$w2" == "--writer-option"                                       && "x$w0" == "x" ]];    then COMPREPLY=("''")
--- a/src/INIWriter.h	Sun Dec 13 16:47:25 2020 +0100
+++ b/src/INIWriter.h	Thu Sep 09 18:57:37 2021 +0200
@@ -43,6 +43,9 @@
 
 	relpipe::common::type::StringX hierarchySeparator = L"/";
 	bool allowSections = true;
+	
+	bool skipEmpty = false;
+	bool skipNull = false;
 
 	bool hasContent = false;
 
@@ -196,6 +199,8 @@
 		else if (uri == option::KeyValueSeparator) keyValueSeparator = convertor.to_bytes(value);
 		else if (uri == option::HierarchySeparator) hierarchySeparator = value;
 		else if (uri == option::AllowSections) allowSections = parseBoolean(value);
+		else if (uri == option::SkipEmpty) skipEmpty = parseBoolean(value);
+		else if (uri == option::SkipNull) skipNull = parseBoolean(value);
 		else if (uri == option::QuotesTypeForSections) quotingForSections.type = parseQuotingType(value);
 		else if (uri == option::QuotesTypeForKeys) quotingForKeys.type = parseQuotingType(value);
 		else if (uri == option::QuotesTypeForValues) quotingForValues.type = parseQuotingType(value);
@@ -243,6 +248,8 @@
 	}
 
 	void entry(const EntryEvent& event) {
+		if (skipEmpty && event.value.empty()) return;
+		if (skipNull && event.value.empty()) return; // TODO: distinguish null and empty (when supported in the relpipe format)
 		// TODO: escape/quote parts separately + configurable quoting of parts or whole + the same for sub keys
 		if (!allowSections && currentSection.size()) output << escape(getCurrentSectionFullName(), EscapingProcessor::TextType::EntryKey) << convertor.to_bytes(hierarchySeparator);
 		output << escape(event.key, EscapingProcessor::TextType::EntryKey);
--- a/src/uri.h	Sun Dec 13 16:47:25 2020 +0100
+++ b/src/uri.h	Thu Sep 09 18:57:37 2021 +0200
@@ -40,6 +40,8 @@
 static const wchar_t* QuotesPatternForKeys = L"quotes-pattern-for-keys";
 static const wchar_t* QuotesPatternForValues = L"quotes-pattern-for-values";
 static const wchar_t* Dialect = L"dialect";
+static const wchar_t* SkipEmpty = L"skip-empty";
+static const wchar_t* SkipNull = L"skip-null";
 }
 
 /** names of dynamically registered escaping processors; they are also options */