src/lib/INIReader.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 23 Nov 2020 19:41:06 +0100
branchv_0
changeset 23 dd72e4ea4399
parent 22 817c83a3efab
child 24 dd3c03162e89
permissions -rw-r--r--
support unquoted empty 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*(.*))?");
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
    43
		std::regex entryQuotesPattrern(/***/"\\s*(([^=\\]]+?[^=\\s\\]]*)(\\[([^\\]]+)\\])?)\\s*=\\s*\"([^']+)\"\\s*((;|#)\\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
    44
		std::regex entryApostrophesPattrern("\\s*(([^=\\]]+?[^=\\s\\]]*)(\\[([^\\]]+)\\])?)\\s*=\\s*'([^']+)'\\s*((;|#)\\s*(.*))?");
23
dd72e4ea4399 support unquoted empty values
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    45
		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
    46
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
		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
    48
		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
    49
		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
    50
		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
    51
		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
    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
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
		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
    55
			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
    56
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
    57
			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
    58
				INIContentHandler::WhitespaceEvent event;
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    59
				event.lineNumber = lineNumber;
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    60
				event.eventNumber = ++eventNumber;
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    61
				event.whitespace = match[0];
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    62
				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
    63
			} 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
    64
				INIContentHandler::CommentEvent event;
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    65
				event.lineNumber = lineNumber;
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    66
				event.eventNumber = ++eventNumber;
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    67
				event.comment = match[2];
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    68
				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
    69
			} 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
    70
				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
    71
				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
    72
				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
    73
				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
    74
				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
    75
				event.name = match[1];
20
fc8f9aab211d partial support of KDE syntax: [section][]
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    76
				event.comment = match[6];
fc8f9aab211d partial support of KDE syntax: [section][]
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    77
				// event.tag = match[3];
fc8f9aab211d partial support of KDE syntax: [section][]
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    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.
fc8f9aab211d partial support of KDE syntax: [section][]
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    79
				// 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
    80
				// 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
    81
				for (INIContentHandler* handler : handlers) handler->startSection(event);
22
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    82
			} else if (std::regex_match(line, match, entryQuotesPattrern) || std::regex_match(line, match, entryApostrophesPattrern)) {
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
    83
				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
    84
				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
    85
				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
    86
				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
    87
				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
    88
				event.fullKey = match[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
    89
				event.value = match[5];
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
				if (match.size() == 9) event.comment = match[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
    91
				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
    92
			} 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
    93
				INIContentHandler::EntryEvent event;
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    94
				event.lineNumber = lineNumber;
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    95
				event.eventNumber = ++eventNumber;
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    96
				event.key = match[2];
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    97
				event.subKey = match[4];
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    98
				event.fullKey = match[1];
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
    99
				event.value = match[5];
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   100
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   101
				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
   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)
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.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
   104
					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
   105
				}
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   106
817c83a3efab multi-line support: plain (unquoted) line continuations (\)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   107
				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
   108
			} 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
   109
				// 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
   110
			}
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
   111
19
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   112
			// General feautres:
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   113
			// 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
   114
			// 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
   115
			// 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
   116
			// 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
   117
			// 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
   118
			// 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
   119
			// 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
   120
			// 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
   121
			// 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
   122
			// 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
   123
19
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   124
			// Lossless conversions:
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   125
			// 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
   126
			// 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
   127
			// 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
   128
			// 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
   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
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
		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
   132
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
		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
   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
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
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
   138
	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
   139
}