simple INI parser based on regular expressions
patterns taken from alt2xml:
- https://alt2xml.globalcode.info/
- https://hg.frantovo.cz/alt2xml/file/94081a55bf41/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java#l151
/**
* 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;
metadata.push_back({L"section", TypeId::STRING});
metadata.push_back({L"key", TypeId::STRING});
metadata.push_back({L"subkey", TypeId::STRING});
metadata.push_back({L"value", TypeId::STRING});
writer->startRelation(configuration.relation, metadata, true);
};
void endDocument() override {
currentSection.clear();
};
void startSection(const std::string& name) override {
currentSection.push_back(name);
};
void endSection() override {
currentSection.pop_back();
};
void entry(const std::string& key, const std::string& subkey, const std::string& value) override {
writer->writeAttribute(convertor.from_bytes(getCurrentSectionFullName()));
writer->writeAttribute(convertor.from_bytes(key));
writer->writeAttribute(convertor.from_bytes(subkey));
writer->writeAttribute(convertor.from_bytes(value));
};
// TODO: handle also comments and whitespace (to allow lossless transformation from INI and back to INI)
// TODO: make subkeys (in [] brackets in the key) optional/configurable
};
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();
}
}
}
}