src/GuileHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 20 Jan 2019 01:02:40 +0100
branchv_0
changeset 1 9179406ab3b3
parent 0 f36bf14d45cb
child 2 7fb4d2c70e8c
permissions -rw-r--r--
link to the Guile library and add some Guile demo code – evaluate code from CLI argument try: relpipe-in-fstab | relpipe-tr-guile 'fstab' '(or (= a b) (= a a) )' | relpipe-out-tabular relpipe-in-fstab | relpipe-tr-guile 'fstab' '(or (= a b) (= a 9) )' | 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) {
			// TODO: evaluate condition and updates in Guile
			currentRecord[currentAttributeIndex] = value;
			includeCurrentRecord = false;

			// TODO: remove, just a demo code
			defineGuileVariable(L"hello", TypeId::STRING, L"world");
			defineGuileVariable(L"a", TypeId::INTEGER, L"123");
			defineGuileVariable(L"b", TypeId::INTEGER, L"456");
			defineGuileVariable(L"tr", TypeId::BOOLEAN, L"true");
			defineGuileVariable(L"fa", TypeId::BOOLEAN, L"false");

			//integer_t guileResult = scm_to_uint64(scm_eval_string(toGuileString(guileCode)));
			//std::wcerr << "result from Guile: " << guileResult << std::endl;
			
			includeCurrentRecord = scm_to_bool(scm_eval_string(toGuileString(guileCode)));
			//scm_shell(0, nullptr);
			// --------
			

			currentAttributeIndex++;

			if (currentAttributeIndex > 0 && currentAttributeIndex % currentMetadata.size() == 0) {
				if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v);
				includeCurrentRecord = false;
			}

			currentAttributeIndex = currentAttributeIndex % currentMetadata.size();
		} else {

			relationalWriter->writeAttribute(value);
		}
	}

	void endOfPipe() {

	}

};

}
}
}