src/lib/INIReader.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 23 Nov 2020 19:49:35 +0100
branchv_0
changeset 24 dd3c03162e89
parent 23 dd72e4ea4399
child 25 ee70b17950bd
permissions -rw-r--r--
simplify/unify the quoted and apostrophed values
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <vector>
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <regex>
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include "INIReader.h"
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
class INIReaderImpl : public INIReader {
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
private:
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
	std::istream& input;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
	std::vector<INIContentHandler*> handlers;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
public:
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
	INIReaderImpl(std::istream& input) : input(input) {
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
	}
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	void addHandler(INIContentHandler* handler) override {
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
		handlers.push_back(handler);
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	}
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	void process() override {
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		for (INIContentHandler* handler : handlers) handler->startDocument();
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		std::regex whitespacePattrern("\\s*");
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		std::regex commentPattrern("\\s*(;|#)\\s*(.*)");
20
fc8f9aab211d partial support of KDE syntax: [section][]
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    42
		std::regex sectionPattrern("\\s*\\[\\s*([^\\]]+)\\s*\\]\\s*(\\[\\s*([^\\]]+)\\s*\\])?\\s*((;|#)\\s*(.*))?");
24
dd3c03162e89 simplify/unify the quoted and apostrophed values
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    43
		std::regex entryQuotedPattrern("\\s*(([^=\\]]+?[^=\\s\\]]*)(\\[([^\\]]+)\\])?)\\s*=\\s*(\"|')((?:(?!\\5).)*)(\\5)\\s*((;|#)\\s*(.*))?");
23
dd72e4ea4399 support unquoted empty values
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    44
		std::regex entryPlainPattrern("\\s*(([^=\\]]+?[^=\\s\\]]*)(\\[([^\\]]+)\\])?)\\s*=\\s*(.*?)\\s*");
16
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		std::smatch match;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		bool inSection = false;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		std::string line;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		int lineNumber = 0;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		int eventNumber = 0;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		while (std::getline(input, line)) {
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
			lineNumber++;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
			if (std::regex_match(line, match, whitespacePattrern)) {
19
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    57
				INIContentHandler::WhitespaceEvent event;
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    58
				event.lineNumber = lineNumber;
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    59
				event.eventNumber = ++eventNumber;
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    60
				event.whitespace = match[0];
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    61
				for (INIContentHandler* handler : handlers) handler->whitespace(event);
16
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
			} else if (std::regex_match(line, match, commentPattrern)) {
19
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    63
				INIContentHandler::CommentEvent event;
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    64
				event.lineNumber = lineNumber;
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    65
				event.eventNumber = ++eventNumber;
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    66
				event.comment = match[2];
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    67
				for (INIContentHandler* handler : handlers) handler->comment(event);
16
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
			} else if (std::regex_match(line, match, sectionPattrern)) {
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
				if (inSection) for (INIContentHandler* handler : handlers) handler->endSection();
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
				inSection = true;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
				INIContentHandler::SectionStartEvent event;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
				event.lineNumber = lineNumber;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
				event.eventNumber = ++eventNumber;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
				event.name = match[1];
20
fc8f9aab211d partial support of KDE syntax: [section][]
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    75
				event.comment = match[6];
fc8f9aab211d partial support of KDE syntax: [section][]
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    76
				// event.tag = match[3];
fc8f9aab211d partial support of KDE syntax: [section][]
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    77
				// KDE uses some weird INI dialect that allows [section][x] syntax where „x“ is kind of „tag“ that signalizes some properties of given section.
fc8f9aab211d partial support of KDE syntax: [section][]
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    78
				// see <https://userbase.kde.org/KDE_System_Administration/Configuration_Files>, „[$i]“ means that the section is „locked“
fc8f9aab211d partial support of KDE syntax: [section][]
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    79
				// We may emit this information somehow later, but for now, it is just ignored.
16
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
				for (INIContentHandler* handler : handlers) handler->startSection(event);
24
dd3c03162e89 simplify/unify the quoted and apostrophed values
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    81
			} else if (std::regex_match(line, match, entryQuotedPattrern)) {
16
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
				INIContentHandler::EntryEvent event;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
				event.lineNumber = lineNumber;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
				event.eventNumber = ++eventNumber;
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
				event.key = match[2];
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
				event.subKey = match[4];
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
				event.fullKey = match[1];
24
dd3c03162e89 simplify/unify the quoted and apostrophed values
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    88
				event.value = match[6];
dd3c03162e89 simplify/unify the quoted and apostrophed values
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    89
				event.comment = match[10];
16
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
				for (INIContentHandler* handler : handlers) handler->entry(event);
22
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    91
			} else if (std::regex_match(line, match, entryPlainPattrern)) {
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    92
				INIContentHandler::EntryEvent event;
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    93
				event.lineNumber = lineNumber;
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    94
				event.eventNumber = ++eventNumber;
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    95
				event.key = match[2];
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    96
				event.subKey = match[4];
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    97
				event.fullKey = match[1];
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    98
				event.value = match[5];
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    99
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   100
				while (line.back() == '\\' && std::getline(input, line)) {
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   101
					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)
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   102
					event.value = event.value.substr(0, event.value.size() - 1); // cut the trailing \ backslash
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   103
					event.value = event.value + line;
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   104
				}
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   105
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   106
				for (INIContentHandler* handler : handlers) handler->entry(event);
16
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
			} else {
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
				// TODO: warning, error, or support unknown content
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
			}
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
19
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   111
			// General feautres:
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   112
			// TODO: probably switch to state-machine approach instead of regular expressions or use an existing library
16
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
			// TODO: warning/error handler
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
			// TODO: support also quoted or multiline keys?
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
			// TODO: support also escaped characters
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
			// TODO: support also Java .properties and manifest.mf formats?
20
fc8f9aab211d partial support of KDE syntax: [section][]
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   117
			// TODO: support also quoted sections ["qoted section"] – useful for hierarchy (the path element may contain the separator character)
16
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
			// TODO: support also nested sections – hierarchy
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
			// TODO: support also nested keys e.g. key.sub.subsub.subsubsub=value – translate them to nested sections
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
			// TODO: support also option for alternative key-value separator (: instead of =)
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
			// TODO: support also other encodings (currently only UTF-8 is supported)
22
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   122
19
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   123
			// Lossless conversions:
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   124
			// TODO: emit also the quote style ('/"/)
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   125
			// TODO: emit also the comment style (;/#) ?
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   126
			// TODO: emit also the whitespace before key name, around =, after "values"/'values', around [sections] ?
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   127
			// TODO: emit also the line-end type (LF/CRLF) ?
16
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
		}
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
		if (inSection) for (INIContentHandler* handler : handlers) handler->endSection();
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
		for (INIContentHandler* handler : handlers) handler->endDocument();
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   133
	}
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   134
};
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   135
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   136
INIReader* INIReader::create(std::istream& input) {
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   137
	return new INIReaderImpl(input);
b9a3c806468a temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
}