src/lib/INIReader.cpp
branchv_0
changeset 22 817c83a3efab
parent 21 ccd0677746ce
child 23 dd72e4ea4399
equal deleted inserted replaced
21:ccd0677746ce 22:817c83a3efab
    77 				// event.tag = match[3];
    77 				// event.tag = match[3];
    78 				// KDE uses some weird INI dialect that allows [section][x] syntax where „x“ is kind of „tag“ that signalizes some properties of given section.
    78 				// KDE uses some weird INI dialect that allows [section][x] syntax where „x“ is kind of „tag“ that signalizes some properties of given section.
    79 				// see <https://userbase.kde.org/KDE_System_Administration/Configuration_Files>, „[$i]“ means that the section is „locked“
    79 				// see <https://userbase.kde.org/KDE_System_Administration/Configuration_Files>, „[$i]“ means that the section is „locked“
    80 				// We may emit this information somehow later, but for now, it is just ignored.
    80 				// We may emit this information somehow later, but for now, it is just ignored.
    81 				for (INIContentHandler* handler : handlers) handler->startSection(event);
    81 				for (INIContentHandler* handler : handlers) handler->startSection(event);
    82 			} else if (std::regex_match(line, match, entryQuotesPattrern) || std::regex_match(line, match, entryApostrophesPattrern) || std::regex_match(line, match, entryPlainPattrern)) {
    82 			} else if (std::regex_match(line, match, entryQuotesPattrern) || std::regex_match(line, match, entryApostrophesPattrern)) {
    83 				INIContentHandler::EntryEvent event;
    83 				INIContentHandler::EntryEvent event;
    84 				event.lineNumber = lineNumber;
    84 				event.lineNumber = lineNumber;
    85 				event.eventNumber = ++eventNumber;
    85 				event.eventNumber = ++eventNumber;
    86 				event.key = match[2];
    86 				event.key = match[2];
    87 				event.subKey = match[4];
    87 				event.subKey = match[4];
    88 				event.fullKey = match[1];
    88 				event.fullKey = match[1];
    89 				event.value = match[5];
    89 				event.value = match[5];
    90 				if (match.size() == 9) event.comment = match[8];
    90 				if (match.size() == 9) event.comment = match[8];
    91 				for (INIContentHandler* handler : handlers) handler->entry(event);
    91 				for (INIContentHandler* handler : handlers) handler->entry(event);
       
    92 			} else if (std::regex_match(line, match, entryPlainPattrern)) {
       
    93 				INIContentHandler::EntryEvent event;
       
    94 				event.lineNumber = lineNumber;
       
    95 				event.eventNumber = ++eventNumber;
       
    96 				event.key = match[2];
       
    97 				event.subKey = match[4];
       
    98 				event.fullKey = match[1];
       
    99 				event.value = match[5];
       
   100 
       
   101 				while (line.back() == '\\' && std::getline(input, line)) {
       
   102 					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)
       
   103 					event.value = event.value.substr(0, event.value.size() - 1); // cut the trailing \ backslash
       
   104 					event.value = event.value + line;
       
   105 				}
       
   106 
       
   107 				for (INIContentHandler* handler : handlers) handler->entry(event);
    92 			} else {
   108 			} else {
    93 				// TODO: warning, error, or support unknown content
   109 				// TODO: warning, error, or support unknown content
    94 			}
   110 			}
    95 
   111 
    96 			// General feautres:
   112 			// General feautres:
    97 			// TODO: probably switch to state-machine approach instead of regular expressions or use an existing library
   113 			// TODO: probably switch to state-machine approach instead of regular expressions or use an existing library
    98 			// TODO: warning/error handler
   114 			// TODO: warning/error handler
    99 			// TODO: support also multiline content (\ + \n)
       
   100 			// TODO: support also quoted or multiline keys?
   115 			// TODO: support also quoted or multiline keys?
   101 			// TODO: support also escaped characters
   116 			// TODO: support also escaped characters
   102 			// TODO: support also Java .properties and manifest.mf formats?
   117 			// TODO: support also Java .properties and manifest.mf formats?
   103 			// TODO: support also quoted sections ["qoted section"] – useful for hierarchy (the path element may contain the separator character)
   118 			// TODO: support also quoted sections ["qoted section"] – useful for hierarchy (the path element may contain the separator character)
   104 			// TODO: support also nested sections – hierarchy
   119 			// TODO: support also nested sections – hierarchy
   105 			// TODO: support also nested keys e.g. key.sub.subsub.subsubsub=value – translate them to nested sections
   120 			// TODO: support also nested keys e.g. key.sub.subsub.subsubsub=value – translate them to nested sections
   106 			// TODO: support also option for alternative key-value separator (: instead of =)
   121 			// TODO: support also option for alternative key-value separator (: instead of =)
   107 			// TODO: support also other encodings (currently only UTF-8 is supported)
   122 			// TODO: support also other encodings (currently only UTF-8 is supported)
   108 			
   123 
   109 			// Lossless conversions:
   124 			// Lossless conversions:
   110 			// TODO: emit also the quote style ('/"/)
   125 			// TODO: emit also the quote style ('/"/)
   111 			// TODO: emit also the comment style (;/#) ?
   126 			// TODO: emit also the comment style (;/#) ?
   112 			// TODO: emit also the whitespace before key name, around =, after "values"/'values', around [sections] ?
   127 			// TODO: emit also the whitespace before key name, around =, after "values"/'values', around [sections] ?
   113 			// TODO: emit also the line-end type (LF/CRLF) ?
   128 			// TODO: emit also the line-end type (LF/CRLF) ?