multi-line support: plain (unquoted) line continuations (\) v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 23 Nov 2020 19:15:01 +0100
branchv_0
changeset 22 817c83a3efab
parent 21 ccd0677746ce
child 23 dd72e4ea4399
multi-line support: plain (unquoted) line continuations (\)
src/lib/INIReader.cpp
--- a/src/lib/INIReader.cpp	Mon Nov 23 18:13:27 2020 +0100
+++ b/src/lib/INIReader.cpp	Mon Nov 23 19:15:01 2020 +0100
@@ -79,7 +79,7 @@
 				// see <https://userbase.kde.org/KDE_System_Administration/Configuration_Files>, „[$i]“ means that the section is „locked“
 				// We may emit this information somehow later, but for now, it is just ignored.
 				for (INIContentHandler* handler : handlers) handler->startSection(event);
-			} else if (std::regex_match(line, match, entryQuotesPattrern) || std::regex_match(line, match, entryApostrophesPattrern) || std::regex_match(line, match, entryPlainPattrern)) {
+			} else if (std::regex_match(line, match, entryQuotesPattrern) || std::regex_match(line, match, entryApostrophesPattrern)) {
 				INIContentHandler::EntryEvent event;
 				event.lineNumber = lineNumber;
 				event.eventNumber = ++eventNumber;
@@ -89,6 +89,22 @@
 				event.value = match[5];
 				if (match.size() == 9) event.comment = match[8];
 				for (INIContentHandler* handler : handlers) handler->entry(event);
+			} else if (std::regex_match(line, match, entryPlainPattrern)) {
+				INIContentHandler::EntryEvent event;
+				event.lineNumber = lineNumber;
+				event.eventNumber = ++eventNumber;
+				event.key = match[2];
+				event.subKey = match[4];
+				event.fullKey = match[1];
+				event.value = match[5];
+
+				while (line.back() == '\\' && std::getline(input, line)) {
+					line = std::regex_replace(line, std::regex("^\\s+|\\s+$"), ""); // trim the spaces: continuing lines might be aligned to the first line (desired spaces – if any – should be at the line end before the \ character)
+					event.value = event.value.substr(0, event.value.size() - 1); // cut the trailing \ backslash
+					event.value = event.value + line;
+				}
+
+				for (INIContentHandler* handler : handlers) handler->entry(event);
 			} else {
 				// TODO: warning, error, or support unknown content
 			}
@@ -96,7 +112,6 @@
 			// General feautres:
 			// TODO: probably switch to state-machine approach instead of regular expressions or use an existing library
 			// TODO: warning/error handler
-			// TODO: support also multiline content (\ + \n)
 			// TODO: support also quoted or multiline keys?
 			// TODO: support also escaped characters
 			// TODO: support also Java .properties and manifest.mf formats?
@@ -105,7 +120,7 @@
 			// TODO: support also nested keys e.g. key.sub.subsub.subsubsub=value – translate them to nested sections
 			// TODO: support also option for alternative key-value separator (: instead of =)
 			// TODO: support also other encodings (currently only UTF-8 is supported)
-			
+
 			// Lossless conversions:
 			// TODO: emit also the quote style ('/"/)
 			// TODO: emit also the comment style (;/#) ?