src/lib/INIReader.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 21 Nov 2020 20:09:18 +0100
branchv_0
changeset 1 3876a9c56a66
parent 0 16c7fa9b7c49
child 2 f031a4dc7c52
permissions -rw-r--r--
simple INI parser based on regular expressions patterns taken from alt2xml: - https://alt2xml.globalcode.info/ - https://hg.frantovo.cz/alt2xml/file/94081a55bf41/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java#l151

/**
 * Relational pipes
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <vector>
#include <regex>

#include "INIReader.h"

class INIReaderImpl : public INIReader {
private:
	std::istream& input;
	std::vector<INIContentHandler*> handlers;
public:

	INIReaderImpl(std::istream& input) : input(input) {
	}

	void addHandler(INIContentHandler* handler) override {
		handlers.push_back(handler);
	}

	void process() override {

		for (INIContentHandler* handler : handlers) handler->startDocument();

		std::regex whitespacePattrern("\\s*");
		std::regex commentPattrern("\\s*(;|#)\\s*(.*)");
		std::regex sectionPattrern("\\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*(.*)");

		std::smatch match;
		std::string section;
		std::string line;

		while (std::getline(input, line)) {

			if (std::regex_match(line, match, whitespacePattrern)) {
				// TODO: support also whitespace
			} else if (std::regex_match(line, match, commentPattrern)) {
				// TODO: support also comments + emit also the comment style (;/#)
			} else if (std::regex_match(line, match, sectionPattrern)) {
				if (section.size()) for (INIContentHandler* handler : handlers) handler->endSection();
				section = match[1];
				for (INIContentHandler* handler : handlers) handler->startSection(section);
			} else if (std::regex_match(line, match, entryQuotesPattrern) || std::regex_match(line, match, entryApostrophesPattrern)) {
				// TODO: support also comments + emit also the comment style (;/#)
				// TODO: emit also the quote style ('/"/) and surrounding whitespace
				for (INIContentHandler* handler : handlers) handler->entry(match[1], match[3], match[4]);
			} else if (std::regex_match(line, match, entryPlainPattrern)) {
				for (INIContentHandler* handler : handlers) handler->entry(match[1], match[3], match[4]);
			} else {
				// TODO: warning, error, or support unknown content
			}

			// TODO: probably switch to state-machine approach instead of regular expressions
			// 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?
			// TODO: support also nested sections – hierarchy
			// TODO: support also option for alternative key-value separator (: instead of =)
			// TODO: support also other encodings (currently only UTF-8 is supported)
			// TODO: emit line numbers and/or event order?
		}

		if (section.size()) for (INIContentHandler* handler : handlers) handler->endSection();

		for (INIContentHandler* handler : handlers) handler->endDocument();
	}
};

INIReader* INIReader::create(std::istream& input) {
	return new INIReaderImpl(input);
}