author | František Kučera <franta-hg@frantovo.cz> |
Fri, 26 Aug 2022 22:42:04 +0200 | |
branch | v_0 |
changeset 11 | 4becf8474975 |
parent 8 | 7c85dc9a310b |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <string> |
|
20 |
#include <sstream> |
|
4
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
21 |
#include <regex> |
0 | 22 |
|
23 |
#include <relpipe/common/type/typedefs.h> |
|
24 |
#include <relpipe/reader/RelpipeReaderException.h> |
|
25 |
||
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
26 |
#include "uri.h" |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
27 |
#include "Dialect.h" |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
28 |
#include "EscapingProcessor.h" |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
29 |
|
0 | 30 |
namespace relpipe { |
31 |
namespace out { |
|
32 |
namespace ini { |
|
33 |
||
34 |
class INIWriter { |
|
35 |
private: |
|
36 |
std::ostream& output; |
|
37 |
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding or generate INI always in UTF-8 like XML? |
|
38 |
||
39 |
std::string keyValueSeparator = " = "; |
|
40 |
std::string commentSeparatorForSections = " ; "; |
|
41 |
std::string commentSeparatorForEntries = " ; "; |
|
42 |
std::string commentSeparatorStandalone = "; "; |
|
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
43 |
|
5
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
44 |
relpipe::common::type::StringX hierarchySeparator = L"/"; |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
45 |
bool allowSections = true; |
8
7c85dc9a310b
implement --writer-option skip-empty and skip-null (omit entries with missing values)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
46 |
|
7c85dc9a310b
implement --writer-option skip-empty and skip-null (omit entries with missing values)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
47 |
bool skipEmpty = false; |
7c85dc9a310b
implement --writer-option skip-empty and skip-null (omit entries with missing values)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
48 |
bool skipNull = false; |
5
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
49 |
|
2
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
bool hasContent = false; |
0 | 51 |
|
5
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
52 |
std::vector<relpipe::common::type::StringX> currentSection; |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
53 |
|
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
54 |
relpipe::common::type::StringX getCurrentSectionFullName() { |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
55 |
std::wstringstream result; |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
56 |
|
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
57 |
// TODO: configurable hierarchy delimiter |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
58 |
// TODO: escape delimiter characters that are part of the section names |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
59 |
for (int i = 0; i < currentSection.size(); i++) { |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
60 |
if (i > 0)result << hierarchySeparator; |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
61 |
result << currentSection[i]; |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
62 |
} |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
63 |
|
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
64 |
return result.str(); |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
65 |
} |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
66 |
|
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
67 |
/** |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
68 |
* TODO: use a common method |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
69 |
*/ |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
70 |
bool parseBoolean(const relpipe::common::type::StringX& value) { |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
71 |
if (value == L"true") return true; |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
72 |
else if (value == L"false") return false; |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
73 |
else throw relpipe::reader::RelpipeReaderException(L"Unable to parse boolean value: " + value + L" (expecting true or false)"); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
74 |
} |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
75 |
|
4
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
76 |
EscapingProcessor::QuotingType parseQuotingType(const relpipe::common::type::StringX& value) { |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
77 |
if (value == quoting::None) return EscapingProcessor::QuotingType::None; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
78 |
else if (value == quoting::Apostrophes) return EscapingProcessor::QuotingType::Apostrophes; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
79 |
else if (value == quoting::Quotes) return EscapingProcessor::QuotingType::Quotes; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
80 |
else throw relpipe::reader::RelpipeReaderException(L"Unsupported quoting type:" + value); |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
81 |
} |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
82 |
|
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
83 |
class QuotingRule { |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
84 |
public: |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
85 |
EscapingProcessor::QuotingType type = EscapingProcessor::QuotingType::None; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
86 |
relpipe::common::type::StringX pattern; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
87 |
|
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
88 |
EscapingProcessor::QuotingType findEffectiveQuotingType(const relpipe::common::type::StringX& value) { |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
89 |
if (type == EscapingProcessor::QuotingType::None) return type; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
90 |
else if (pattern.empty()) return type; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
91 |
else return std::regex_match(value, std::wregex(pattern)) ? type : EscapingProcessor::QuotingType::None; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
92 |
} |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
93 |
}; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
94 |
|
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
95 |
QuotingRule quotingForSections; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
96 |
QuotingRule quotingForKeys; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
97 |
QuotingRule quotingForValues; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
98 |
|
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
99 |
std::string escape(const relpipe::common::type::StringX& value, EscapingProcessor::TextType type) { |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
100 |
relpipe::common::type::StringX result = value; |
4
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
101 |
|
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
102 |
EscapingProcessor::QuotingType quotingType; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
103 |
if (type == EscapingProcessor::TextType::SectionName || type == EscapingProcessor::TextType::SectionTag) quotingType = quotingForSections.findEffectiveQuotingType(value); |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
104 |
else if (type == EscapingProcessor::TextType::EntryKey || type == EscapingProcessor::TextType::EntrySubKey) quotingType = quotingForKeys.findEffectiveQuotingType(value); |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
105 |
else if (type == EscapingProcessor::TextType::EntryValue) quotingType = quotingForValues.findEffectiveQuotingType(value); |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
106 |
else quotingType = EscapingProcessor::QuotingType::None; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
107 |
|
11
4becf8474975
fix typo: enbaled → enabled
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
108 |
for (ConfiguredEscapingProcessor p : escapingProcessors) if (p.enabled) result = p.processor->escape(result, type, quotingType); |
4
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
109 |
|
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
110 |
if (quotingType == EscapingProcessor::QuotingType::Quotes) result = L"\"" + result + L"\""; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
111 |
else if (quotingType == EscapingProcessor::QuotingType::Apostrophes) result = L"'" + result + L"'"; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
112 |
|
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
113 |
return convertor.to_bytes(result); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
114 |
} |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
115 |
|
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
116 |
class ConfiguredEscapingProcessor { |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
117 |
public: |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
118 |
std::shared_ptr<EscapingProcessor> processor; |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
119 |
const relpipe::common::type::StringX uri; |
11
4becf8474975
fix typo: enbaled → enabled
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
120 |
bool enabled; |
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
121 |
|
11
4becf8474975
fix typo: enbaled → enabled
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
122 |
ConfiguredEscapingProcessor(std::shared_ptr<EscapingProcessor> processor, const relpipe::common::type::StringX uri, bool enabled) : processor(processor), uri(uri), enabled(enabled) { |
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
123 |
} |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
124 |
|
0 | 125 |
}; |
126 |
||
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
127 |
std::vector<ConfiguredEscapingProcessor> escapingProcessors; |
0 | 128 |
|
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
129 |
bool setEscaping(const relpipe::common::type::StringX& uri, const relpipe::common::type::StringX& value) { |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
130 |
for (ConfiguredEscapingProcessor& p : escapingProcessors) { |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
131 |
if (p.uri == uri) { |
11
4becf8474975
fix typo: enbaled → enabled
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
132 |
p.enabled = parseBoolean(value); |
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
133 |
return true; |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
134 |
} |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
135 |
} |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
136 |
return false; |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
137 |
} |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
138 |
|
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
139 |
class ConfiguredDialect { |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
140 |
public: |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
141 |
std::shared_ptr<Dialect> dialect; |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
142 |
const relpipe::common::type::StringX uri; |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
143 |
|
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
144 |
ConfiguredDialect(std::shared_ptr<Dialect> dialect, const relpipe::common::type::StringX uri) : dialect(dialect), uri(uri) { |
0 | 145 |
} |
146 |
||
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
147 |
}; |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
148 |
|
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
149 |
std::vector<ConfiguredDialect> dialects; |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
150 |
|
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
151 |
void setDialect(const relpipe::common::type::StringX& uri) { |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
152 |
for (ConfiguredDialect& d : dialects) { |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
153 |
if (d.uri == uri) { |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
154 |
d.dialect->apply(*this); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
155 |
return; |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
156 |
} |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
157 |
} |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
158 |
throw relpipe::reader::RelpipeReaderException(L"Unsupported INI dialect: " + uri); |
0 | 159 |
} |
160 |
||
161 |
public: |
|
162 |
||
163 |
class SectionStartEvent { |
|
164 |
public: |
|
165 |
relpipe::common::type::StringX comment; |
|
166 |
relpipe::common::type::StringX name; |
|
167 |
relpipe::common::type::StringX tag; |
|
168 |
}; |
|
169 |
||
170 |
class EntryEvent { |
|
171 |
public: |
|
172 |
relpipe::common::type::StringX comment; |
|
173 |
relpipe::common::type::StringX key; |
|
174 |
relpipe::common::type::StringX subKey; |
|
175 |
relpipe::common::type::StringX value; |
|
176 |
}; |
|
177 |
||
178 |
class CommentEvent { |
|
179 |
public: |
|
180 |
relpipe::common::type::StringX comment; |
|
181 |
}; |
|
182 |
||
183 |
class WhitespaceEvent { |
|
184 |
public: |
|
185 |
relpipe::common::type::StringX whitespace; |
|
186 |
}; |
|
187 |
||
188 |
INIWriter(std::ostream& output) : output(output) { |
|
189 |
} |
|
190 |
||
191 |
virtual ~INIWriter() { |
|
192 |
}; |
|
193 |
||
194 |
void setOption(relpipe::common::type::StringX uri, relpipe::common::type::StringX value) { |
|
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
195 |
if (uri == option::Dialect) setDialect(value); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
196 |
else if (uri == option::CommentSeparatorForSections) commentSeparatorForSections = convertor.to_bytes(value); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
197 |
else if (uri == option::CommentSeparatorForEntries) commentSeparatorForEntries = convertor.to_bytes(value); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
198 |
else if (uri == option::CommentSeparatorStandalone) commentSeparatorStandalone = convertor.to_bytes(value); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
199 |
else if (uri == option::KeyValueSeparator) keyValueSeparator = convertor.to_bytes(value); |
5
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
200 |
else if (uri == option::HierarchySeparator) hierarchySeparator = value; |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
201 |
else if (uri == option::AllowSections) allowSections = parseBoolean(value); |
8
7c85dc9a310b
implement --writer-option skip-empty and skip-null (omit entries with missing values)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
202 |
else if (uri == option::SkipEmpty) skipEmpty = parseBoolean(value); |
7c85dc9a310b
implement --writer-option skip-empty and skip-null (omit entries with missing values)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
203 |
else if (uri == option::SkipNull) skipNull = parseBoolean(value); |
4
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
204 |
else if (uri == option::QuotesTypeForSections) quotingForSections.type = parseQuotingType(value); |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
205 |
else if (uri == option::QuotesTypeForKeys) quotingForKeys.type = parseQuotingType(value); |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
206 |
else if (uri == option::QuotesTypeForValues) quotingForValues.type = parseQuotingType(value); |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
207 |
else if (uri == option::QuotesPatternForSections) quotingForSections.pattern = value; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
208 |
else if (uri == option::QuotesPatternForKeys) quotingForKeys.pattern = value; |
372b161669e4
configurable quoting
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
209 |
else if (uri == option::QuotesPatternForValues) quotingForValues.pattern = value; |
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
210 |
else if (setEscaping(uri, value)); |
0 | 211 |
else throw relpipe::reader::RelpipeReaderException(L"Unsupported writer option: " + uri); |
212 |
} |
|
213 |
||
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
214 |
void addDialect(std::shared_ptr<Dialect> dialect, const relpipe::common::type::StringX uri, bool enabledByDefault) { |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
215 |
dialects.push_back({dialect, uri}); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
216 |
if (enabledByDefault) dialect->apply(*this); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
217 |
} |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
218 |
|
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
219 |
void addEscapingProcessor(std::shared_ptr<EscapingProcessor> processor, const relpipe::common::type::StringX uri, bool enabledByDefault) { |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
220 |
escapingProcessors.push_back({processor, uri, enabledByDefault}); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
221 |
} |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
222 |
|
0 | 223 |
void startDocument() { |
224 |
} |
|
225 |
||
226 |
void endDocument() { |
|
5
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
227 |
if (currentSection.size()) throw relpipe::reader::RelpipeReaderException(L"There are still " + std::to_wstring(currentSection.size()) + L" open sections during the endDocument() call. Need to call endSection() before."); |
0 | 228 |
output.flush(); |
229 |
} |
|
230 |
||
231 |
void startSection(const SectionStartEvent& event) { |
|
5
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
232 |
currentSection.push_back(event.name); |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
233 |
|
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
234 |
if (allowSections) { |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
235 |
if (hasContent) output << std::endl; |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
236 |
output << "[" << escape(getCurrentSectionFullName(), EscapingProcessor::TextType::SectionName) << "]"; |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
237 |
if (event.tag.size()) output << "[" << escape(event.tag, EscapingProcessor::TextType::SectionTag) << "]"; |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
238 |
if (event.comment.size()) output << commentSeparatorForSections << escape(event.comment, EscapingProcessor::TextType::SectionComment); |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
239 |
output << std::endl; |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
240 |
} |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
241 |
|
2
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
242 |
hasContent = true; |
0 | 243 |
} |
244 |
||
245 |
void endSection() { |
|
5
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
246 |
currentSection.pop_back(); |
0 | 247 |
output.flush(); |
248 |
} |
|
249 |
||
250 |
void entry(const EntryEvent& event) { |
|
8
7c85dc9a310b
implement --writer-option skip-empty and skip-null (omit entries with missing values)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
251 |
if (skipEmpty && event.value.empty()) return; |
7c85dc9a310b
implement --writer-option skip-empty and skip-null (omit entries with missing values)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
252 |
if (skipNull && event.value.empty()) return; // TODO: distinguish null and empty (when supported in the relpipe format) |
5
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
253 |
// TODO: escape/quote parts separately + configurable quoting of parts or whole + the same for sub keys |
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
254 |
if (!allowSections && currentSection.size()) output << escape(getCurrentSectionFullName(), EscapingProcessor::TextType::EntryKey) << convertor.to_bytes(hierarchySeparator); |
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
255 |
output << escape(event.key, EscapingProcessor::TextType::EntryKey); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
256 |
if (event.subKey.size()) output << "[" << escape(event.subKey, EscapingProcessor::TextType::EntrySubKey) << "]"; |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
257 |
output << keyValueSeparator << escape(event.value, EscapingProcessor::TextType::EntryValue); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
258 |
if (event.comment.size()) output << commentSeparatorForEntries << escape(event.comment, EscapingProcessor::TextType::EntryComment); |
0 | 259 |
output << std::endl; |
2
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
260 |
hasContent = true; |
0 | 261 |
} |
262 |
||
263 |
void comment(const CommentEvent& event) { |
|
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
264 |
output << commentSeparatorStandalone << escape(event.comment, EscapingProcessor::TextType::StandaloneComment); |
2
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
265 |
output << std::endl; |
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
266 |
hasContent = true; |
0 | 267 |
} |
268 |
||
269 |
void whitespace(const WhitespaceEvent& event) { |
|
270 |
for (wchar_t ch : event.whitespace) { |
|
271 |
if (ch == L' ') output << " "; |
|
272 |
else if (ch == L'\t') output << "\t"; |
|
273 |
else if (ch == L'\n') output << "\n"; |
|
274 |
else if (ch == L'\r'); // TODO: keep CR? |
|
275 |
else; // TODO: throw exception if whitespace contains unexpected data? (should not happen) |
|
276 |
} |
|
2
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
277 |
hasContent = true; |
0 | 278 |
} |
279 |
||
280 |
||
281 |
||
282 |
}; |
|
283 |
||
284 |
} |
|
285 |
} |
|
286 |
} |