/**
* Relational pipes
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <vector>
#include <sstream>
#include <memory>
#include <relpipe/writer/RelationalWriter.h>
#include <relpipe/writer/RelpipeWriterException.h>
#include <relpipe/writer/AttributeMetadata.h>
#include <relpipe/writer/TypeId.h>
#include <relpipe/cli/CLI.h>
#include "INICommand.h"
#include "lib/INIReader.h"
using namespace std;
using namespace relpipe::writer;
namespace relpipe {
namespace in {
namespace ini {
class FlatINIContentHandler : public INIContentHandler {
private:
wstring_convert < codecvt_utf8<wchar_t>> convertor; // INI parser works with UTF-8
std::shared_ptr<writer::RelationalWriter> writer;
Configuration& configuration;
std::vector<std::string> currentSection;
std::string getCurrentSectionFullName() {
std::stringstream result;
// TODO: configurable hierarchy delimiter
// TODO: escape delimiter characters that are part of the section names
for (int i = 0; i < currentSection.size(); i++) {
if (i > 0)result << "/";
result << currentSection[i];
}
return result.str();
}
public:
FlatINIContentHandler(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) : writer(writer), configuration(configuration) {
}
virtual ~FlatINIContentHandler() {
}
void startDocument() override {
vector<AttributeMetadata> metadata;
if (configuration.enableLineNumbers) metadata.push_back({L"line", TypeId::INTEGER});
if (configuration.enableEventNumbers) metadata.push_back({L"event", TypeId::INTEGER});
if (configuration.enableSections) metadata.push_back({L"section", TypeId::STRING});
metadata.push_back({L"key", TypeId::STRING});
if (configuration.enableSubKeys) metadata.push_back({L"sub_key", TypeId::STRING});
metadata.push_back({L"value", TypeId::STRING});
if (configuration.enableComments) metadata.push_back({L"comment", TypeId::STRING});
writer->startRelation(configuration.relation, metadata, true);
};
void endDocument() override {
currentSection.clear();
};
void startSection(const SectionStartEvent& event) override {
currentSection.push_back(event.name);
};
void endSection() override {
currentSection.pop_back();
};
void entry(const EntryEvent& event) override {
if (configuration.enableLineNumbers) writer->writeAttribute(&event.lineNumber, typeid (event.lineNumber));
if (configuration.enableEventNumbers) writer->writeAttribute(&event.eventNumber, typeid (event.eventNumber));
std::string section = getCurrentSectionFullName();
std::string key = configuration.enableSubKeys ? event.key : event.fullKey;
if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section));
else if (section.size()) key = section + "/" + key;
writer->writeAttribute(convertor.from_bytes(key));
if (configuration.enableSubKeys) writer->writeAttribute(convertor.from_bytes(event.subKey));
writer->writeAttribute(convertor.from_bytes(event.value));
if (configuration.enableComments) writer->writeAttribute(convertor.from_bytes(event.comment));
};
// TODO: handle also comments and whitespace (to allow lossless transformation from INI and back to INI)
};
void INICommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
FlatINIContentHandler handler(writer, configuration);
std::shared_ptr<INIReader> reader(INIReader::create(input));
reader->addHandler(&handler);
reader->process();
}
}
}
}