author | František Kučera <franta-hg@frantovo.cz> |
Fri, 11 Dec 2020 12:34:42 +0100 | |
branch | v_0 |
changeset 2 | e753a7f967c8 |
parent 0 | 1bb084f84eb9 |
child 3 | ae8775e0bc7a |
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> |
|
21 |
||
22 |
#include <relpipe/common/type/typedefs.h> |
|
23 |
#include <relpipe/reader/RelpipeReaderException.h> |
|
24 |
||
25 |
namespace relpipe { |
|
26 |
namespace out { |
|
27 |
namespace ini { |
|
28 |
||
29 |
class INIWriter { |
|
30 |
private: |
|
31 |
std::ostream& output; |
|
32 |
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding or generate INI always in UTF-8 like XML? |
|
33 |
||
34 |
std::string keyValueSeparator = " = "; |
|
35 |
std::string commentSeparatorForSections = " ; "; |
|
36 |
std::string commentSeparatorForEntries = " ; "; |
|
37 |
std::string commentSeparatorStandalone = "; "; |
|
2
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
|
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
39 |
bool hasContent = false; |
0 | 40 |
|
41 |
enum class TokenType { |
|
42 |
SectionName, |
|
43 |
SectionTag, |
|
44 |
SectionComment, |
|
45 |
EntryKey, |
|
46 |
EntrySubKey, |
|
47 |
EntryValue, |
|
48 |
EntryComment, |
|
49 |
StandaloneComment, |
|
50 |
}; |
|
51 |
||
52 |
std::string escape(TokenType type, relpipe::common::type::StringX value) { |
|
53 |
std::wstringstream result; |
|
54 |
||
55 |
for (wchar_t ch : value) { |
|
56 |
if (ch == L'\\') result << "\\\\"; |
|
57 |
else if (ch == L'\n') result << L"\\n"; |
|
58 |
else if (ch == L'\r') result << L"\\r"; |
|
59 |
else if (ch == L'\t') result << L"\\t"; |
|
60 |
else if (ch == L'"') result << "\\\""; |
|
61 |
else result.put(ch); |
|
62 |
} |
|
63 |
||
64 |
// TODO: modular escaping (like unescaping in relpipe-in-ini) |
|
65 |
return convertor.to_bytes(result.str()); |
|
66 |
} |
|
67 |
||
68 |
public: |
|
69 |
||
70 |
class SectionStartEvent { |
|
71 |
public: |
|
72 |
relpipe::common::type::StringX comment; |
|
73 |
relpipe::common::type::StringX name; |
|
74 |
relpipe::common::type::StringX tag; |
|
75 |
}; |
|
76 |
||
77 |
class EntryEvent { |
|
78 |
public: |
|
79 |
relpipe::common::type::StringX comment; |
|
80 |
relpipe::common::type::StringX key; |
|
81 |
relpipe::common::type::StringX subKey; |
|
82 |
relpipe::common::type::StringX value; |
|
83 |
}; |
|
84 |
||
85 |
class CommentEvent { |
|
86 |
public: |
|
87 |
relpipe::common::type::StringX comment; |
|
88 |
}; |
|
89 |
||
90 |
class WhitespaceEvent { |
|
91 |
public: |
|
92 |
relpipe::common::type::StringX whitespace; |
|
93 |
}; |
|
94 |
||
95 |
INIWriter(std::ostream& output) : output(output) { |
|
96 |
} |
|
97 |
||
98 |
virtual ~INIWriter() { |
|
99 |
}; |
|
100 |
||
101 |
void setOption(relpipe::common::type::StringX uri, relpipe::common::type::StringX value) { |
|
102 |
// TODO: setOption() |
|
103 |
if (uri == L"dialect"); |
|
104 |
else if (uri == L"comment-separator-for-sections") commentSeparatorForSections = convertor.to_bytes(value); |
|
105 |
else if (uri == L"comment-separator-for-entries") commentSeparatorForEntries = convertor.to_bytes(value); |
|
106 |
else if (uri == L"comment-separator-standalone") commentSeparatorStandalone = convertor.to_bytes(value); |
|
107 |
else if (uri == L"key-value-separator") keyValueSeparator = convertor.to_bytes(value); |
|
108 |
else if (uri == L"escape-backspace"); |
|
109 |
else if (uri == L"escape-basic"); |
|
110 |
else if (uri == L"escape-java-properties"); |
|
111 |
else throw relpipe::reader::RelpipeReaderException(L"Unsupported writer option: " + uri); |
|
112 |
} |
|
113 |
||
114 |
void startDocument() { |
|
115 |
} |
|
116 |
||
117 |
void endDocument() { |
|
118 |
output.flush(); |
|
119 |
} |
|
120 |
||
121 |
void startSection(const SectionStartEvent& event) { |
|
2
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
122 |
if (hasContent) output << std::endl; |
0 | 123 |
output << "[" << escape(TokenType::SectionName, event.name) << "]"; |
124 |
if (event.tag.size()) output << "[" << escape(TokenType::SectionTag, event.tag) << "]"; |
|
125 |
if (event.comment.size()) output << commentSeparatorForSections << escape(TokenType::SectionComment, event.comment); |
|
126 |
output << std::endl; |
|
2
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
127 |
hasContent = true; |
0 | 128 |
} |
129 |
||
130 |
void endSection() { |
|
131 |
output.flush(); |
|
132 |
} |
|
133 |
||
134 |
void entry(const EntryEvent& event) { |
|
135 |
output << escape(TokenType::EntryKey, event.key); |
|
136 |
if (event.subKey.size()) output << "[" << escape(TokenType::EntrySubKey, event.subKey) << "]"; |
|
137 |
output << keyValueSeparator << escape(TokenType::EntryValue, event.value); |
|
138 |
if (event.comment.size()) output << commentSeparatorForEntries << escape(TokenType::EntryComment, event.comment); |
|
139 |
output << std::endl; |
|
2
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
140 |
hasContent = true; |
0 | 141 |
} |
142 |
||
143 |
void comment(const CommentEvent& event) { |
|
144 |
output << commentSeparatorStandalone << escape(TokenType::StandaloneComment, event.comment); |
|
2
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
145 |
output << std::endl; |
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
146 |
hasContent = true; |
0 | 147 |
} |
148 |
||
149 |
void whitespace(const WhitespaceEvent& event) { |
|
150 |
for (wchar_t ch : event.whitespace) { |
|
151 |
if (ch == L' ') output << " "; |
|
152 |
else if (ch == L'\t') output << "\t"; |
|
153 |
else if (ch == L'\n') output << "\n"; |
|
154 |
else if (ch == L'\r'); // TODO: keep CR? |
|
155 |
else; // TODO: throw exception if whitespace contains unexpected data? (should not happen) |
|
156 |
} |
|
2
e753a7f967c8
implement --style standard
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
157 |
hasContent = true; |
0 | 158 |
} |
159 |
||
160 |
||
161 |
||
162 |
}; |
|
163 |
||
164 |
} |
|
165 |
} |
|
166 |
} |