src/INICommand.cpp
author František Kučera <franta-hg@frantovo.cz>
Wed, 25 Nov 2020 21:35:07 +0100
branchv_0
changeset 16 db994a2ddffa
parent 15 0cf8bcb9c8fc
child 17 4b1612d20cb2
permissions -rw-r--r--
new INI parser
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
16c7fa9b7c49 project and parser skeleton + output demo data
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
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <cstdlib>
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <vector>
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <sstream>
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/writer/RelationalWriter.h>
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/writer/RelpipeWriterException.h>
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/writer/AttributeMetadata.h>
7
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    25
#include <relpipe/common/type/typedefs.h>
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/cli/CLI.h>
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include "INICommand.h"
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include "lib/INIReader.h"
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    31
#include "lib/BasicUnescapingINIHandler.h"
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
using namespace std;
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
using namespace relpipe::writer;
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    35
using namespace relpipe::in::ini::lib;
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace relpipe {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace in {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
namespace ini {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
class FlatINIContentHandler : public INIContentHandler {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
private:
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	wstring_convert < codecvt_utf8<wchar_t>> convertor; // INI parser works with UTF-8
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	std::shared_ptr<writer::RelationalWriter> writer;
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	Configuration& configuration;
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	std::vector<std::string> currentSection;
15
0cf8bcb9c8fc enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    47
	bool inDocument = false;
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	std::string getCurrentSectionFullName() {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		std::stringstream result;
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		// TODO: configurable hierarchy delimiter
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		// TODO: escape delimiter characters that are part of the section names
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    54
		// TODO: hierarchical sections should be returned as an array/sequence when such data type is supported in the Relational pipes format and API
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		for (int i = 0; i < currentSection.size(); i++) {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
			if (i > 0)result << "/";
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
			result << currentSection[i];
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		}
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		return result.str();
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	}
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
7
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    63
	class Record {
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    64
	public:
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    65
		relpipe::common::type::Integer lineNumber = -1;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    66
		relpipe::common::type::Integer eventNumber = -1;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    67
		std::string key;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    68
		std::string subKey;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    69
		std::string value;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    70
		std::string comment;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    71
		std::string whitespace;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    72
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    73
		Record(const Event * const event) : lineNumber(event->lineNumber), eventNumber(event->eventNumber) {
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    74
		}
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    75
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    76
	};
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    77
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    78
	void write(const Record& record) {
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    79
		if (configuration.enableLineNumbers) writer->writeAttribute(&record.lineNumber, typeid (record.lineNumber));
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    80
		if (configuration.enableEventNumbers) writer->writeAttribute(&record.eventNumber, typeid (record.eventNumber));
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    81
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    82
		std::string section = getCurrentSectionFullName();
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    83
		std::string key = record.key;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    84
		if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section));
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    85
		else if (section.size()) key = section + "/" + record.key;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    86
		writer->writeAttribute(convertor.from_bytes(key));
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    87
		if (configuration.enableSubKeys) writer->writeAttribute(convertor.from_bytes(record.subKey));
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    88
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    89
		writer->writeAttribute(convertor.from_bytes(record.value));
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    90
		if (configuration.enableComments) writer->writeAttribute(convertor.from_bytes(record.comment));
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    91
		if (configuration.enableWhitespace) writer->writeAttribute(convertor.from_bytes(record.whitespace));
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    92
	}
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    93
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
public:
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
	FlatINIContentHandler(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) : writer(writer), configuration(configuration) {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
	}
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
	virtual ~FlatINIContentHandler() {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
	}
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
	void startDocument() override {
15
0cf8bcb9c8fc enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   103
		if (inDocument) throw std::out_of_range("Lunatic INI parser tried to start a document without ending the previous one.");
0cf8bcb9c8fc enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   104
		inDocument = true;
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
		vector<AttributeMetadata> metadata;
2
f031a4dc7c52 add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   106
		if (configuration.enableLineNumbers) metadata.push_back({L"line", TypeId::INTEGER});
f031a4dc7c52 add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   107
		if (configuration.enableEventNumbers) metadata.push_back({L"event", TypeId::INTEGER});
f031a4dc7c52 add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   108
		if (configuration.enableSections) metadata.push_back({L"section", TypeId::STRING});
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
		metadata.push_back({L"key", TypeId::STRING});
5
d70ea23682aa rename subkey to sub_key/sub-key
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   110
		if (configuration.enableSubKeys) metadata.push_back({L"sub_key", TypeId::STRING});
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
		metadata.push_back({L"value", TypeId::STRING});
2
f031a4dc7c52 add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   112
		if (configuration.enableComments) metadata.push_back({L"comment", TypeId::STRING});
6
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   113
		if (configuration.enableWhitespace) metadata.push_back({L"whitespace", TypeId::STRING});
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
		writer->startRelation(configuration.relation, metadata, true);
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
	};
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
	void endDocument() override {
15
0cf8bcb9c8fc enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   118
		if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to end a document without starting it before.");
0cf8bcb9c8fc enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   119
		inDocument = false;
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
		currentSection.clear();
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
	};
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
2
f031a4dc7c52 add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   123
	void startSection(const SectionStartEvent& event) override {
15
0cf8bcb9c8fc enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   124
		if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to start a section without starting a document.");
2
f031a4dc7c52 add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   125
		currentSection.push_back(event.name);
8
83b23480d41f emit also the section comments: e.g. „[section] ; comment“
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   126
83b23480d41f emit also the section comments: e.g. „[section] ; comment“
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   127
		if (configuration.enableComments && event.comment.size()) {
83b23480d41f emit also the section comments: e.g. „[section] ; comment“
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   128
			Record record(&event);
83b23480d41f emit also the section comments: e.g. „[section] ; comment“
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   129
			record.comment = event.comment;
83b23480d41f emit also the section comments: e.g. „[section] ; comment“
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   130
			write(record);
83b23480d41f emit also the section comments: e.g. „[section] ; comment“
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   131
		}
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
	};
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   133
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   134
	void endSection() override {
15
0cf8bcb9c8fc enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   135
		if (currentSection.empty()) throw std::out_of_range("Lunatic INI parser tried to end a section without starting it before.");
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   136
		currentSection.pop_back();
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   137
	};
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
2
f031a4dc7c52 add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   139
	void entry(const EntryEvent& event) override {
15
0cf8bcb9c8fc enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   140
		if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to emit an entry without starting a document.");
7
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   141
		Record record(&event);
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   142
		record.comment = event.comment;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   143
		record.key = configuration.enableSubKeys ? event.key : event.fullKey;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   144
		record.subKey = event.subKey;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   145
		record.value = event.value;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   146
		write(record);
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   147
	};
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   148
6
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   149
	void comment(const CommentEvent& event) override {
15
0cf8bcb9c8fc enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   150
		if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to emit a comment without starting a document.");
6
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   151
		if (configuration.enableComments) {
7
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   152
			Record record(&event);
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   153
			record.comment = event.comment;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   154
			write(record);
6
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   155
		}
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   156
	}
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   157
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   158
	void whitespace(const WhitespaceEvent& event) override {
15
0cf8bcb9c8fc enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   159
		if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to emit a whitespace without starting a document.");
6
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   160
		if (configuration.enableWhitespace) {
7
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   161
			Record record(&event);
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   162
			record.whitespace = event.whitespace;
95b21edc9519 simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   163
			write(record);
6
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   164
		}
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   165
	}
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
};
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
void INICommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   170
	FlatINIContentHandler handler(writer, configuration);
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   171
	std::shared_ptr<INIReader> reader(INIReader::create(input));
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   172
	// TODO: configure the INIReader (features/properties) according to our Configuration (sub-keys etc.)
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   173
	BasicUnescapingINIContentHandler unescapingHandler(handler, true);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   174
	reader->addHandler(&unescapingHandler);
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   175
	reader->process();
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   177
}
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   178
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   179
}
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   180
}
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   181
}