src/lib/INIReader.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 23 Nov 2020 21:09:46 +0100
branchv_0
changeset 25 ee70b17950bd
parent 24 dd3c03162e89
child 26 80e129ec3408
permissions -rw-r--r--
multi-line support: quoted and apostrophed
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*(.*))?");
25
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
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];
25
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    90
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    91
				// the "/' at the end is missing → line continues
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    92
				if (match.length(7) == 0) {
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    93
					std::regex endPattern(std::string("(.*?)") + (match[5] == "'" ? "'" : "\"") + "\\s*((;|#)\\s*(.*))?");
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    94
					while (std::getline(input, line)) {
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    95
						lineNumber++;
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    96
						event.value += "\n";
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    97
						if (std::regex_match(line, match, endPattern)) {
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    98
							event.value += std::string(match[1]);
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    99
							event.comment = match[4];
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   100
							break;
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   101
						} else {
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   102
							event.value += line;
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   103
						}
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   104
					}
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   105
				}
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   106
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
				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
   108
			} 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
   109
				INIContentHandler::EntryEvent event;
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   110
				event.lineNumber = lineNumber;
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   111
				event.eventNumber = ++eventNumber;
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   112
				event.key = match[2];
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   113
				event.subKey = match[4];
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   114
				event.fullKey = match[1];
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   115
				event.value = match[5];
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   116
25
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   117
				// the \ at the end → line continues
22
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   118
				while (line.back() == '\\' && std::getline(input, line)) {
25
ee70b17950bd multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   119
					lineNumber++;
22
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   120
					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
   121
					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
   122
					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
   123
				}
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   124
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   125
				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
   126
			} 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
   127
				// 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
   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
19
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   130
			// General feautres:
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   131
			// 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
   132
			// 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
   133
			// 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
   134
			// 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
   135
			// 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
   136
			// 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
   137
			// 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
   138
			// 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
   139
			// 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
   140
			// 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
   141
19
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   142
			// Lossless conversions:
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   143
			// 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
   144
			// 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
   145
			// 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
   146
			// 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
   147
		}
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
   148
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
   149
		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
   150
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
   151
		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
   152
	}
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
   153
};
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
   154
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
   155
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
   156
	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
   157
}