src/lib/INIReader.cpp
branchv_0
changeset 19 90f2b8ca32bf
parent 16 b9a3c806468a
child 20 fc8f9aab211d
--- a/src/lib/INIReader.cpp	Sun Nov 22 19:25:42 2020 +0100
+++ b/src/lib/INIReader.cpp	Mon Nov 23 16:25:39 2020 +0100
@@ -39,7 +39,7 @@
 
 		std::regex whitespacePattrern("\\s*");
 		std::regex commentPattrern("\\s*(;|#)\\s*(.*)");
-		std::regex sectionPattrern("\\s*\\[\\s*([^\\]]+)\\s*\\]\\s*");
+		std::regex sectionPattrern("\\s*\\[\\s*([^\\]]+)\\s*\\]\\s*((;|#)\\s*(.*))?");
 		std::regex entryQuotesPattrern(/***/"\\s*(([^=\\]]+?[^=\\s\\]]*)(\\[([^\\]]+)\\])?)\\s*=\\s*\"([^']+)\"\\s*((;|#)\\s*(.*))?");
 		std::regex entryApostrophesPattrern("\\s*(([^=\\]]+?[^=\\s\\]]*)(\\[([^\\]]+)\\])?)\\s*=\\s*'([^']+)'\\s*((;|#)\\s*(.*))?");
 		std::regex entryPlainPattrern("\\s*(([^=\\]]+?[^=\\s\\]]*)(\\[([^\\]]+)\\])?)\\s*=\\s*(.*)");
@@ -55,9 +55,17 @@
 			lineNumber++;
 
 			if (std::regex_match(line, match, whitespacePattrern)) {
-				// TODO: support also whitespace
+				INIContentHandler::WhitespaceEvent event;
+				event.lineNumber = lineNumber;
+				event.eventNumber = ++eventNumber;
+				event.whitespace = match[0];
+				for (INIContentHandler* handler : handlers) handler->whitespace(event);
 			} else if (std::regex_match(line, match, commentPattrern)) {
-				// TODO: support also comments + emit also the comment style (;/#)
+				INIContentHandler::CommentEvent event;
+				event.lineNumber = lineNumber;
+				event.eventNumber = ++eventNumber;
+				event.comment = match[2];
+				for (INIContentHandler* handler : handlers) handler->comment(event);
 			} else if (std::regex_match(line, match, sectionPattrern)) {
 				if (inSection) for (INIContentHandler* handler : handlers) handler->endSection();
 				inSection = true;
@@ -65,7 +73,7 @@
 				event.lineNumber = lineNumber;
 				event.eventNumber = ++eventNumber;
 				event.name = match[1];
-				// TODO: support also comments + emit also the comment style (;/#)
+				event.comment = match[4];
 				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)) {
 				INIContentHandler::EntryEvent event;
@@ -76,13 +84,13 @@
 				event.fullKey = match[1];
 				event.value = match[5];
 				if (match.size() == 9) event.comment = match[8];
-				// TODO: emit also the quote style ('/"/) and surrounding whitespace
 				for (INIContentHandler* handler : handlers) handler->entry(event);
 			} else {
 				// TODO: warning, error, or support unknown content
 			}
 
-			// TODO: probably switch to state-machine approach instead of regular expressions
+			// 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?
@@ -92,6 +100,12 @@
 			// 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 (;/#) ?
+			// TODO: emit also the whitespace before key name, around =, after "values"/'values', around [sections] ?
+			// TODO: emit also the line-end type (LF/CRLF) ?
 		}
 
 		if (inSection) for (INIContentHandler* handler : handlers) handler->endSection();