author | František Kučera <franta-hg@frantovo.cz> |
Wed, 09 Dec 2020 23:53:30 +0100 | |
branch | v_0 |
changeset 1 | e04e5bbc147b |
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 <memory> |
|
20 |
#include <string> |
|
21 |
#include <vector> |
|
22 |
#include <iostream> |
|
23 |
#include <sstream> |
|
24 |
#include <locale> |
|
25 |
#include <codecvt> |
|
26 |
#include <regex> |
|
27 |
#include <stdexcept> |
|
28 |
||
29 |
#include <relpipe/common/type/typedefs.h> |
|
30 |
#include <relpipe/reader/TypeId.h> |
|
31 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
32 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
33 |
||
34 |
#include "Configuration.h" |
|
35 |
#include "INIWriter.h" |
|
36 |
#include "INIStandardHandler.h" |
|
37 |
#include "INILiteralHandler.h" |
|
1
e04e5bbc147b
add --style section-first
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
#include "INISectionFirstHandler.h" |
0 | 39 |
|
40 |
namespace relpipe { |
|
41 |
namespace out { |
|
42 |
namespace ini { |
|
43 |
||
44 |
class INIDispatchHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
|
45 |
private: |
|
46 |
std::ostream& output; |
|
47 |
INIWriter writer; |
|
48 |
Configuration& configuration; |
|
49 |
std::shared_ptr<relpipe::reader::handlers::RelationalReaderStringHandler> currentHandler; |
|
50 |
||
51 |
relpipe::reader::handlers::RelationalReaderStringHandler* createHandlerAuto(const relpipe::common::type::StringX& name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) { |
|
52 |
bool hasKey = false; |
|
53 |
bool hasValue = false; |
|
54 |
||
55 |
for (auto a : attributes) { |
|
56 |
if (a.getAttributeName() == L"key") hasKey = true; |
|
57 |
else if (a.getAttributeName() == L"value") hasValue = true; |
|
58 |
} |
|
59 |
||
60 |
if (hasKey && hasValue) return new INIStandardHandler(writer); |
|
61 |
else return new INILiteralHandler(writer); |
|
62 |
} |
|
63 |
||
64 |
relpipe::reader::handlers::RelationalReaderStringHandler* createHandler(const relpipe::common::type::StringX& name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) { |
|
65 |
for (auto rc : configuration.relationConfigurations) { |
|
66 |
if (std::regex_match(name, std::wregex(rc.relation))) { |
|
67 |
if (rc.style == Style::Standard) return new INIStandardHandler(writer); |
|
68 |
else if (rc.style == Style::Literal) return new INILiteralHandler(writer); |
|
1
e04e5bbc147b
add --style section-first
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
else if (rc.style == Style::SectionFirst) return new INISectionFirstHandler(writer); |
0 | 70 |
else if (rc.style == Style::Dropped) return nullptr; |
71 |
else if (rc.style == Style::Automatic) return createHandlerAuto(name, attributes); |
|
72 |
else throw std::invalid_argument("Unsupported style: " + std::to_string((int) rc.style)); |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
if (configuration.relationConfigurations.size()) return nullptr; |
|
77 |
else return createHandlerAuto(name, attributes); |
|
78 |
} |
|
79 |
||
80 |
public: |
|
81 |
||
82 |
INIDispatchHandler(std::ostream& output, Configuration& configuration) : output(output), writer(output), configuration(configuration) { |
|
83 |
for (auto o : configuration.writerOptions) writer.setOption(o.uri, o.value); |
|
84 |
} |
|
85 |
||
86 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
87 |
if (currentHandler) currentHandler->endOfPipe(); |
|
88 |
currentHandler.reset(createHandler(name, attributes)); |
|
89 |
if (currentHandler) currentHandler->startRelation(name, attributes); |
|
90 |
} |
|
91 |
||
92 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
93 |
if (currentHandler) currentHandler->attribute(value); |
|
94 |
} |
|
95 |
||
96 |
void endOfPipe() { |
|
97 |
if (currentHandler) currentHandler->endOfPipe(); |
|
98 |
currentHandler.reset(); |
|
99 |
output.flush(); |
|
100 |
} |
|
101 |
||
102 |
}; |
|
103 |
||
104 |
} |
|
105 |
} |
|
106 |
} |