record filtering in Guile
try:
relpipe-in-fstab | relpipe-tr-guile 'fstab' '(= pass 2)' | relpipe-out-tabular
relpipe-in-fstab | relpipe-tr-guile 'fstab' '(equal? mount_point "/")' | relpipe-out-tabular
/**
* Relational pipes
* Copyright © 2019 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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/>.
*/
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <regex>
#include <libguile.h>
#include <relpipe/reader/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>
#include <relpipe/writer/Factory.h>
#include <relpipe/cli/RelpipeCLIException.h>
namespace relpipe {
namespace tr {
namespace guile {
using namespace std;
using namespace relpipe;
using namespace relpipe::reader;
using namespace relpipe::reader::handlers;
class GuileHandler : public RelationalReaderStringHadler {
private:
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
shared_ptr<writer::RelationalWriter> relationalWriter;
wregex relationNameRegEx;
vector<AttributeMetadata> currentMetadata;
vector<string_t> currentRecord;
integer_t currentAttributeIndex = 0;
boolean_t includeCurrentRecord = false;
boolean_t filterCurrentRelation = false;
string_t guileCode;
SCM toGuileSymbol(const string_t& name) {
return scm_string_to_symbol(scm_from_locale_string(convertor.to_bytes(name).c_str()));
}
SCM toGuileString(const string_t& value) {
return scm_from_locale_string(convertor.to_bytes(value).c_str());
}
SCM toGuileInteger(const string_t& value) {
return scm_from_uint64(stoul(value));
}
SCM toGuileBoolean(const string_t& value) {
return value == L"true" ? SCM_BOOL_T : SCM_BOOL_F;
}
void defineGuileVariable(const string_t& name, TypeId type, const string_t& value) {
// TODO: RelationalReaderValueHadler?
switch (type) {
case TypeId::BOOLEAN:
scm_define(toGuileSymbol(name), toGuileBoolean(value));
break;
case TypeId::INTEGER:
scm_define(toGuileSymbol(name), toGuileInteger(value));
break;
case TypeId::STRING:
scm_define(toGuileSymbol(name), toGuileString(value));
break;
default:
throw cli::RelpipeCLIException(L"Unsupported type in defineGuileVariable()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR);
}
}
void undefineGuileVariable(const string_t& name, TypeId type, const string_t& value) {
scm_define(toGuileSymbol(name), scm_make_undefined_variable()); // undefined != (define n)
}
public:
GuileHandler(ostream& output, const vector<string_t>& arguments) {
relationalWriter.reset(writer::Factory::create(output));
// TODO: options and parser
if (arguments.size() == 2) {
relationNameRegEx = wregex(arguments[0]);
guileCode = arguments[1];
} else {
throw cli::RelpipeCLIException(L"Usage: relpipe-tr-guile <relationNameRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
}
}
void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
currentMetadata = attributes;
// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
vector<writer::AttributeMetadata> writerMetadata;
for (AttributeMetadata readerMetadata : attributes) {
writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
}
currentRecord.resize(attributes.size());
filterCurrentRelation = regex_match(name, relationNameRegEx);
relationalWriter->startRelation(name, writerMetadata, true);
}
void attribute(const string_t& value) override {
if (filterCurrentRelation) {
currentRecord[currentAttributeIndex] = value;
defineGuileVariable(currentMetadata[currentAttributeIndex].getAttributeName(), currentMetadata[currentAttributeIndex].getTypeId(), value);
currentAttributeIndex++;
if (currentAttributeIndex > 0 && currentAttributeIndex % currentMetadata.size() == 0) {
// TODO: support also updates in Guile
includeCurrentRecord = scm_to_bool(scm_eval_string(toGuileString(guileCode)));
if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v);
includeCurrentRecord = false;
}
currentAttributeIndex = currentAttributeIndex % currentMetadata.size();
} else {
relationalWriter->writeAttribute(value);
}
}
void endOfPipe() {
}
};
}
}
}