src/lib/INIReader.cpp
author František Kučera <franta-hg@frantovo.cz>
Fri, 26 Aug 2022 22:41:46 +0200
branchv_0
changeset 39 509e4b02f3c2
parent 35 930f17f16fd7
permissions -rw-r--r--
fix typo: enbaled → enabled (thanks Jiří Wolker for reporting)
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
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <vector>
1
3876a9c56a66 simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    19
#include <regex>
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    20
#include <sstream>
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    21
#include <stdexcept>
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include "INIReader.h"
28
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    24
#include "uri.h"
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    26
namespace relpipe {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    27
namespace in {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    28
namespace ini {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    29
namespace lib {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    30
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
class INIReaderImpl : public INIReader {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
private:
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	std::istream& input;
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	std::vector<INIContentHandler*> handlers;
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    35
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    36
	class ConfiguredUnescapingProcessor {
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    37
	public:
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    38
		std::shared_ptr<UnescapingProcessor> processor;
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    39
		const std::string uri;
39
509e4b02f3c2 fix typo: enbaled → enabled
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
    40
		bool enabled;
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    41
39
509e4b02f3c2 fix typo: enbaled → enabled
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
    42
		ConfiguredUnescapingProcessor(std::shared_ptr<UnescapingProcessor> processor, const std::string uri, bool enabled) : processor(processor), uri(uri), enabled(enabled) {
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    43
		}
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    44
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    45
	};
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    46
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    47
	std::vector<ConfiguredUnescapingProcessor> unescapingProcessors;
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    48
28
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    49
	class ConfiguredDialect {
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    50
	public:
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    51
		std::shared_ptr<Dialect> dialect;
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    52
		const std::string uri;
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    53
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    54
		ConfiguredDialect(std::shared_ptr<Dialect> dialect, const std::string uri) : dialect(dialect), uri(uri) {
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    55
		}
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    56
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    57
	};
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    58
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    59
	std::vector<ConfiguredDialect> dialects;
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    60
34
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    61
	/**
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    62
	 * If there is a „\“ backspace at the end of a physical line, the logical line continues on the next physical line.
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    63
	 *
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    64
	 * Disabling this option makes sense only if we also disable the unescaping processors (unescape-basic, unescape-backspace).
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    65
	 * Otherwise they will complain about „Missing escape sequence“ because they got „\“ at the end of the value.
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    66
	 */
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    67
	bool allowLineContinuationsWithEscaping = true;
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    68
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    69
	/**
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    70
	 * If a line starts with a space, it is continuation of the previous line.
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    71
	 * This rule conflicts with default ignorance of such insignificant whitespace and is quite specific to the Java MANIFEST.MF dialect.
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    72
	 */
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    73
	bool allowLineContinuationsWithSpace = false;
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    74
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    75
	/** 
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    76
	 * By default, we ignore all leading whitespace on continuing lines.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    77
	 * If there should be some spaces or tabs, they should be placed on the previous line before the „\“.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    78
	 * If a line break is desired, it should be written as \n (escaped) or the value should be quoted in " or '.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    79
	 * 
26
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    80
	 * TODO: several options:
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    81
	 *  - enabled, disabled
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    82
	 *  - if disabled, then: keep backslash, trim backslash, escape backslash
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    83
	 *    (keep requires support in some further unescaping phase, or it will cause an error)
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    84
	 *  - keep or trim the line end
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    85
	 *  - keep or trim the leading spaces
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    86
	 *  - allow comments interleaved with continuing lines (the freaky systemd syntax)
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    87
	 * 
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    88
	 * Related specifications:
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    89
	 *  - https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Properties.html
26
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    90
	 *  - https://www.freedesktop.org/software/systemd/man/systemd.syntax.html
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    91
	 */
23
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    92
	bool trimLeadingSpacesOnContinuingLines = true;
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    93
24
07e0a2edf3bc add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    94
07e0a2edf3bc add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    95
	/**
07e0a2edf3bc add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    96
	 * Some dialects or configuration files in general does not support sections.
07e0a2edf3bc add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    97
	 * Then a line, that looks like an INI section, should be interpreted as a key
07e0a2edf3bc add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    98
	 * (or error, if does not have a proper key-value separator).
07e0a2edf3bc add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    99
	 */
07e0a2edf3bc add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   100
	bool allowSections = true;
07e0a2edf3bc add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   101
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   102
	/**
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   103
	 * KDE uses some weird INI dialect that allows [section][x] syntax where „x“ is kind of „tag“ that signalizes some properties of given section.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   104
	 * Line „[section_1][$i]“ means that the „section_1“ is „locked“.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   105
	 * We may emit this information somehow later, but for now, it is just ignored.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   106
	 * 
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   107
	 * TODO: Is „section tag“ right name?
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   108
	 * 
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   109
	 * Related specifications:
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   110
	 *  - https://userbase.kde.org/KDE_System_Administration/Configuration_Files#Lock_Down
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   111
	 */
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   112
	bool allowSectionTags = true;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   113
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   114
	/**
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   115
	 * If whole key is „aaa[bbb]“ then „aaa“ is considered to be the key and „bbb“ the sub-key.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   116
	 * No \[ escaping is currently supported, so the key might not contain the bracket character.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   117
	 * 
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   118
	 * Related specifications:
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   119
	 *  - https://userbase.kde.org/KDE_System_Administration/Configuration_Files#Shell_Expansion
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   120
	 *  - https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s05.html
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   121
	 */
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   122
	bool allowSubKeys = true;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   123
20
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   124
	/**
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   125
	 * Classic INI uses „key=value“ syntax.
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   126
	 * But some other formats/dialects might use key:value.
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   127
	 * 
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   128
	 * Only single character separators are supported.
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   129
	 * If multiple separators should be recognized (e.g. both „=“ and „:“), this string will contain all of them,
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   130
	 * i.e. „:=“ does not mean that the „key:=value“ syntax, but „key=value“ or „key:value“.
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   131
	 */
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   132
	std::string keyValueSeparators = "=";
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   133
21
b35baebf5005 prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   134
	/**
b35baebf5005 prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   135
	 * Classic INI uses „; comment“ syntax.
b35baebf5005 prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   136
	 * But many existing files contain „# comment“ lines.
b35baebf5005 prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   137
	 * 
b35baebf5005 prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   138
	 * Only single character separators are supported (works same as keyValueSeparators).
b35baebf5005 prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   139
	 */
b35baebf5005 prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   140
	std::string commentSeparators = ";#";
b35baebf5005 prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   141
22
29d673a54ecf prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   142
	/**
29d673a54ecf prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   143
	 * INI often support both "quotes" and 'apostrophes' styles.
29d673a54ecf prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   144
	 * But some dialects may support only one of them or not support quoting at all.
29d673a54ecf prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   145
	 * 
29d673a54ecf prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   146
	 * In such case e.g. „key="some value"“ would mean that the value is „"value"“ (including the quotes).
29d673a54ecf prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   147
	 * Thus it is important to allow disabling quote recognizing (which is done by setting this parameter to empty string).
29d673a54ecf prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   148
	 * 
29d673a54ecf prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   149
	 * Only single character quotes are supported (works same as keyValueSeparators).
29d673a54ecf prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   150
	 */
29d673a54ecf prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   151
	std::string quotes = "\"'";
29d673a54ecf prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   152
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   153
	int lineNumber = 1;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   154
	int eventNumber = 0;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   155
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   156
	/**
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   157
	 * Should be always used instead of input.peek().
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   158
	 * Skips \r.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   159
	 */
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   160
	char peek() {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   161
		// In 2020 there is no need to manually return the carriage. However some legacy systems still do it.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   162
		char ch = input.peek();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   163
		if (ch == '\r') {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   164
			input.get();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   165
			ch = input.peek();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   166
		}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   167
		return ch;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   168
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   169
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   170
	/**
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   171
	 * Should be always used instead of input.get().
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   172
	 * Counts the lines and skips \r.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   173
	 */
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   174
	char get() {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   175
		char ch = input.get();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   176
		if (ch == '\n') lineNumber++;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   177
		else if (ch == '\r') ch = get();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   178
		return ch;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   179
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   180
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   181
	std::string readSpacesAndTabs() {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   182
		std::stringstream result;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   183
		for (char ch = peek(); input.good() && (ch == ' ' || ch == '\t'); ch = peek()) result.put(get());
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   184
		return result.str();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   185
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   186
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   187
	std::string readAllWhitespace() {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   188
		std::stringstream result;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   189
		for (char ch = peek(); input.good() && (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); ch = peek()) result.put(get());
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   190
		return result.str();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   191
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   192
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   193
	void processContinuingLine(std::stringstream& result) {
23
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   194
		if (trimLeadingSpacesOnContinuingLines) readSpacesAndTabs();
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   195
		else result.put('\n');
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   196
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   197
20
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   198
	std::string readUntil(const char until, bool* found = nullptr) {
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   199
		return readUntil(std::string(1, until), found);
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   200
	}
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   201
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   202
	std::string readUntil(const std::string& until, bool* found = nullptr) {
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   203
		std::stringstream result;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   204
35
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   205
		for (char ch = peek(); input.good(); ch = peek()) {
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   206
			if (allowLineContinuationsWithSpace && ch == '\n') {
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   207
				get();
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   208
				ch = peek();
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   209
				if (ch == ' ') get();
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   210
				else if (ch == std::istream::traits_type::eof()) break;
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   211
				else {
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   212
					if (found) *found = true;
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   213
					return result.str();
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   214
				}
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   215
			} else if (oneOf(ch, until)) {
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   216
				break;
930f17f16fd7 partially implement --parser-option allow-line-continuation-with-space and java-manifest-mf dialect (MANIFEST.MF)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   217
			} else if (allowLineContinuationsWithEscaping && ch == '\\') {
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   218
				get();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   219
				ch = get();
20
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   220
				if (oneOf(ch, until) && ch == '\n') processContinuingLine(result);
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   221
				else if (oneOf(ch, until)) result.put(ch);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   222
				else if (ch == std::istream::traits_type::eof()) break;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   223
				else result.put('\\').put(ch);
20
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   224
				// unescaping is done in two phases:
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   225
				// here we unescape just the \n (LF)
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   226
				// other escape sequences are leaved untouched and will be processed in later phases, see see UnescapingINIHandler
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   227
			} else {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   228
				ch = get();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   229
				result.put(ch);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   230
			}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   231
		}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   232
20
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   233
		if (oneOf(peek(), until)) {
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   234
			get();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   235
			if (found) *found = true;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   236
		} else {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   237
			if (found) *found = false;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   238
		}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   239
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   240
		return result.str();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   241
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   242
20
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   243
	std::string readToken(const char until, char* quote = nullptr, bool* found = nullptr) {
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   244
		return readToken(std::string(1, until), quote, found);
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   245
	}
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   246
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   247
	std::string readToken(const std::string& until, char* quote = nullptr, bool* found = nullptr) {
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   248
		std::string result;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   249
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   250
		char ch = peek();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   251
		if (isQuote(ch)) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   252
			if (quote) *quote = ch;
20
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   253
			result = readUntil(std::string(1, get()), found);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   254
		} else {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   255
			if (quote) *quote = 0;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   256
			result = readUntil(until, found);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   257
		}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   258
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   259
		return result;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   260
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   261
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   262
	std::string readTokenAndEatTerminator(char until, char* quote = nullptr, bool* found = nullptr) {
20
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   263
		return readTokenAndEatTerminator(std::string(1, until), quote, found);
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   264
	}
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   265
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   266
	std::string readTokenAndEatTerminator(const std::string& until, char* quote = nullptr, bool* found = nullptr) {
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   267
		std::string result = readToken(until, quote, found);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   268
		if (*quote) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   269
			readAllWhitespace();
20
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   270
			if (!oneOf(get(), until)) throw std::logic_error(std::string("missing „") + until + "“ after quoted section name");
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   271
		}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   272
		return result;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   273
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   274
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   275
	std::string unescape(const std::string& value, UnescapingProcessor::TextType type) {
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   276
		std::string result = value;
39
509e4b02f3c2 fix typo: enbaled → enabled
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   277
		for (ConfiguredUnescapingProcessor p : unescapingProcessors) if (p.enabled) result = p.processor->unescape(result, type);
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   278
		return result;
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   279
	}
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   280
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   281
	bool isComment(char ch) {
21
b35baebf5005 prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   282
		return oneOf(ch, commentSeparators);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   283
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   284
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   285
	bool isQuote(char ch) {
22
29d673a54ecf prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents: 21
diff changeset
   286
		return oneOf(ch, quotes);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   287
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   288
20
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   289
	/**
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   290
	 * @param ch character to be evaluated
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   291
	 * @param options list of options (characters)
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   292
	 * @return whether ch is one of options
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   293
	 */
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   294
	bool oneOf(char ch, const std::string& options) {
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   295
		return options.find(ch) != std::string::npos;
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   296
	}
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   297
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   298
	std::string trim(std::string s) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   299
		return std::regex_replace(s, std::regex("^\\s+|\\s+$"), "");
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   300
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   301
23
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   302
	/**
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   303
	 * TODO: use a common method
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   304
	 */
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   305
	bool parseBoolean(const std::string& value) {
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   306
		if (value == "true") return true;
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   307
		else if (value == "false") return false;
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   308
		else throw std::invalid_argument(std::string("Unable to parse boolean value: ") + value + " (expecting true or false)");
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   309
	}
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   310
28
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   311
	void setDialect(const std::string& uri) {
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   312
		for (ConfiguredDialect& d : dialects) {
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   313
			if (d.uri == uri) {
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   314
				d.dialect->apply(*this);
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   315
				return;
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   316
			}
23
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   317
		}
28
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   318
		throw std::invalid_argument(std::string("Unsupported INI dialect: ") + uri);
23
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   319
	}
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   320
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   321
	bool setUnescaping(const std::string& uri, const std::string& value) {
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   322
		for (ConfiguredUnescapingProcessor& p : unescapingProcessors) {
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   323
			if (p.uri == uri) {
39
509e4b02f3c2 fix typo: enbaled → enabled
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   324
				p.enabled = parseBoolean(value);
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   325
				return true;
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   326
			}
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   327
		}
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   328
		return false;
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   329
	}
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   330
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   331
public:
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   332
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   333
	INIReaderImpl(std::istream& input) : input(input) {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   334
	}
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   335
23
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   336
	void setOption(const std::string& uri, const std::string& value) override {
34
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   337
		if (uri == option::AllowLineContinuationWithEscaping) allowLineContinuationsWithEscaping = parseBoolean(value);
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   338
		else if (uri == option::AllowLineContinuationWithSpace) allowLineContinuationsWithSpace = parseBoolean(value);
7eb3dcacba7b implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   339
		else if (uri == option::TrimContinuingLines) trimLeadingSpacesOnContinuingLines = parseBoolean(value);
28
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   340
		else if (uri == option::AllowSections) allowSections = parseBoolean(value);
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   341
		else if (uri == option::AllowSectionTags) allowSectionTags = parseBoolean(value);
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   342
		else if (uri == option::AllowSubKeys) allowSubKeys = parseBoolean(value);
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   343
		else if (uri == option::CommentSeparators) commentSeparators = value;
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   344
		else if (uri == option::KeyValueSeparators) keyValueSeparators = value;
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   345
		else if (uri == option::Quotes) quotes = value;
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   346
		else if (uri == option::Dialect) setDialect(value);
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   347
		else if (setUnescaping(uri, value));
23
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   348
		else throw std::invalid_argument(std::string("Invalid parser option: „") + uri + "“ with value: „" + value + "“");
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   349
	}
b497140b0b63 enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   350
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   351
	void addHandler(INIContentHandler* handler) override {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   352
		handlers.push_back(handler);
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   353
	}
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   354
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   355
	void addUnescapingProcessor(std::shared_ptr<UnescapingProcessor> processor, const std::string uri, bool enabledByDefault) override {
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   356
		unescapingProcessors.push_back({processor, uri, enabledByDefault});
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   357
	}
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   358
28
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   359
	void addDialect(std::shared_ptr<Dialect> dialect, const std::string uri, bool enabledByDefault) override {
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   360
		dialects.push_back({dialect, uri});
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   361
		if (enabledByDefault) dialect->apply(*this);
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   362
	}
596a724fbb83 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   363
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   364
	void process() override {
1
3876a9c56a66 simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   365
		for (INIContentHandler* handler : handlers) handler->startDocument();
3876a9c56a66 simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   366
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   367
		bool inSection = false;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   368
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   369
		while (input.good()) { // TODO: condition
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   370
			{
19
967f73af64a4 start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   371
				INIContentHandler::WhitespaceEvent event;
967f73af64a4 start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   372
				event.lineNumber = lineNumber;
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   373
				std::string whitespace = readAllWhitespace();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   374
				if (whitespace.size()) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   375
					event.eventNumber = ++eventNumber;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   376
					event.whitespace = whitespace;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   377
					for (INIContentHandler* handler : handlers) handler->whitespace(event);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   378
				}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   379
			}
1
3876a9c56a66 simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   380
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   381
			bool found;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   382
			char quote;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   383
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   384
			char ch = peek();
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
   385
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   386
			if (ch == std::istream::traits_type::eof()) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   387
				break;
24
07e0a2edf3bc add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
   388
			} else if (ch == '[' && allowSections) {
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   389
				if (inSection) for (INIContentHandler* handler : handlers) handler->endSection();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   390
				inSection = true;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   391
				INIContentHandler::SectionStartEvent event;
6
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   392
				event.lineNumber = lineNumber;
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   393
				event.eventNumber = ++eventNumber;
19
967f73af64a4 start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   394
				get();
967f73af64a4 start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   395
				readAllWhitespace();
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   396
				event.name = readTokenAndEatTerminator(']', &quote, &found);
25
b9067d2812e5 trim section names if not in quotes
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   397
				if (!quote) event.name = trim(event.name);
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   398
				event.name = unescape(event.name, UnescapingProcessor::TextType::SectionName);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   399
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   400
				readSpacesAndTabs();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   401
				if (allowSectionTags && peek() == '[') {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   402
					get();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   403
					event.tag = readTokenAndEatTerminator(']', &quote, &found);
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   404
					event.tag = unescape(event.tag, UnescapingProcessor::TextType::SectionTag);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   405
				}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   406
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   407
				readSpacesAndTabs();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   408
				ch = peek();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   409
				if (isComment(ch)) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   410
					get();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   411
					readSpacesAndTabs();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   412
					event.comment = readUntil('\n', &found);
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   413
					event.comment = unescape(event.comment, UnescapingProcessor::TextType::SectionComment);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   414
				} else if (ch == '\n') {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   415
					get();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   416
				} else {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   417
					throw std::logic_error(std::string("unexpected content after the section: '") + event.name + "'");
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   418
				}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   419
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   420
				for (INIContentHandler* handler : handlers) handler->startSection(event);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   421
			} else if (isComment(ch)) {
6
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   422
				INIContentHandler::CommentEvent event;
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   423
				event.lineNumber = lineNumber;
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   424
				event.eventNumber = ++eventNumber;
19
967f73af64a4 start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   425
				get();
967f73af64a4 start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   426
				readSpacesAndTabs();
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   427
				event.comment = readUntil('\n', &found);
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   428
				event.comment = unescape(event.comment, UnescapingProcessor::TextType::Comment);
6
fb717cfbfea1 improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   429
				for (INIContentHandler* handler : handlers) handler->comment(event);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   430
			} else {
19
967f73af64a4 start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   431
				INIContentHandler::EntryEvent event;
967f73af64a4 start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   432
				event.lineNumber = lineNumber;
967f73af64a4 start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   433
				event.eventNumber = ++eventNumber;
967f73af64a4 start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   434
20
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   435
				std::string fullKey = readToken(keyValueSeparators, &quote, &found);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   436
				if (!found) throw std::logic_error(std::string("missing = after key: '") + fullKey + "'");
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   437
				if (!quote) fullKey = trim(fullKey);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   438
				readSpacesAndTabs();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   439
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   440
				if (quote) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   441
					ch = get();
20
9187f0439ca9 prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   442
					if (oneOf(ch, keyValueSeparators)) readSpacesAndTabs();
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   443
					else throw std::logic_error(std::string("missing = after quoted key: '") + fullKey + "'");
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   444
				}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   445
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   446
				std::string value = readToken('\n', &quote, &found);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   447
				if (!quote) value = trim(value);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   448
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   449
				event.key = fullKey;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   450
				event.fullKey = fullKey;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   451
				event.value = value;
14
ea431c469403 multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   452
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   453
				if (allowSubKeys) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   454
					std::smatch match;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   455
					if (std::regex_match(fullKey, match, std::regex("([^\\[]+)\\[([^\\[]+)\\]"))) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   456
						event.key = match[1];
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   457
						event.subKey = match[2];
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   458
						event.fullKey = fullKey;
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   459
						event.subKey = unescape(event.subKey, UnescapingProcessor::TextType::EntryKey);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   460
					}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   461
				}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   462
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   463
				event.key = unescape(event.key, UnescapingProcessor::TextType::EntryKey);
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   464
				event.fullKey = unescape(event.fullKey, UnescapingProcessor::TextType::EntryKey);
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   465
				event.value = unescape(event.value, UnescapingProcessor::TextType::EntryValue);
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   466
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   467
				if (quote) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   468
					readSpacesAndTabs();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   469
					ch = peek();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   470
					if (isComment(ch)) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   471
						get();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   472
						readSpacesAndTabs();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   473
						event.comment = readUntil('\n', &found);
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   474
						event.comment = unescape(event.comment, UnescapingProcessor::TextType::EntryComment);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   475
					} else if (ch == '\n') {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   476
						get();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   477
					} else {
26
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   478
						// TODO: optional support for multiple tokens in a single entry?
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   479
						// modes: array, concatenate
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   480
						// some-array-1 = "item 1" "item 2" 'item 3' item 4
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   481
						// some-array-2 = "item 1" "item 2" 'item 3' item_4 item_5
b1f6fa3a6555 TODO comments
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   482
						// some-bash-style-string-value = "this "will' be' concatenated → this will be concatenated
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   483
						throw std::logic_error(std::string("unexpected content after the quoted value: key='") + fullKey + "' value='" + event.value + "'");
14
ea431c469403 multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   484
					}
ea431c469403 multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   485
				}
ea431c469403 multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   486
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
   487
				for (INIContentHandler* handler : handlers) handler->entry(event);
1
3876a9c56a66 simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   488
			}
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   489
		}
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   490
		// TODO: error at the end, catch premature/unexpected EOF
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   491
		// TODO: unescape + trim values + ignore \r
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   492
		// TODO: count lines
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
   493
		if (inSection) for (INIContentHandler* handler : handlers) handler->endSection();
1
3876a9c56a66 simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   494
		for (INIContentHandler* handler : handlers) handler->endDocument();
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   495
	}
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   496
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   497
	// General feautres:
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   498
	// TODO: warning/error handler
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   499
	// TODO: support also escaped characters
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   500
	// TODO: support also Java .properties and manifest.mf formats?
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   501
	// TODO: support also nested sections – hierarchy
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   502
	// TODO: support also nested keys e.g. key.sub.subsub.subsubsub=value – translate them to nested sections
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   503
	// TODO: support also option for alternative key-value separator (: instead of =)
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   504
	// TODO: support also other encodings (currently only UTF-8 is supported)
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   505
	// TODO: better exceptions
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   506
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   507
	// Lossless conversions:
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   508
	// TODO: emit also the quote style ('/"/)
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   509
	// TODO: emit also the comment style (;/#) ?
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   510
	// TODO: emit also the whitespace before key name, around =, after "values"/'values', around [sections] ?
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   511
	// TODO: emit also the line-end type (LF/CRLF) ?
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   512
0
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   513
};
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   514
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   515
INIReader* INIReader::create(std::istream& input) {
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   516
	return new INIReaderImpl(input);
16c7fa9b7c49 project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   517
}
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   518
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   519
}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   520
}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   521
}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   522
}