src/relpipe-in-csv.cpp
author František Kučera <franta-hg@frantovo.cz>
Wed, 09 Jan 2019 23:04:51 +0100
branchv_0
changeset 1 5eb4d149c6e2
parent 0 eca0b23802e8
child 2 e83895da3e8f
permissions -rw-r--r--
first working version

/**
 * Relational pipes
 * Copyright © 2018 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/>.
 */
#include <cstdlib>
#include <vector>
#include <memory>
#include <regex>
#include <algorithm>
#include <unistd.h>

#include <relpipe/writer/RelationalWriter.h>
#include <relpipe/writer/RelpipeWriterException.h>
#include <relpipe/writer/AttributeMetadata.h>
#include <relpipe/writer/Factory.h>
#include <relpipe/writer/TypeId.h>

#include <relpipe/cli/CLI.h>

using namespace std;
using namespace relpipe::cli;
using namespace relpipe::writer;

bool readValue(istream& input, stringstream& currentValue, bool& lastInRecord) {
	lastInRecord = false;
	char ch;
	input.get(ch);
	if (ch == '"') {
		while (input.get(ch)) {
			if (ch == '"') {
				input.get(ch);
				if (ch == '"') {
					currentValue << ch;
				} else {
					if (ch == '\r') input.get(ch);
					if (ch == '\n') lastInRecord = true;
					else if (ch != ',') throw RelpipeWriterException(L"Unexpected character (should be „\\n“ or „,“)");
					return true;
				}
			} else {
				currentValue << ch;
			}
		}
	} else if (ch == ',') {
		return true;
	} else if (ch == '\n') {
		lastInRecord = true;
		return true;
	} else if (ch == '\r') {
		currentValue << ch;
		if (ch == '\n') {
			lastInRecord = true;
			return true;
		} else {
			throw RelpipeWriterException(L"Crazy carriage stuck during journey");
		}
	} else {
		for (currentValue << ch; input.get(ch);) {
			switch (ch) {
				case ',': return true;
				case '\r': break;
				case '\n':
					lastInRecord = true;
					return true;
				default: currentValue << ch;
			}
		}
	}
	return false;
}

void processDataStream(ostream &output, istream& input) {
	wstring_convert < codecvt_utf8<wchar_t>> convertor; // UTF-8 is required for CSV
	std::shared_ptr<RelationalWriter> writer(Factory::create(output));
	vector<AttributeMetadata> metadata;
	bool headerDone = false;
	bool lastInRecord = false;
	stringstream currentValue;


	while (readValue(input, currentValue, lastInRecord) && input.good()) {
		if (headerDone) {
			writer->writeAttribute(convertor.from_bytes(currentValue.str()));
		} else {
			AttributeMetadata am;
			am.attributeName = convertor.from_bytes(currentValue.str());
			am.typeId = TypeId::STRING; // TODO: support also other types passed as CLI parameters
			metadata.push_back(am);
			if (lastInRecord) {
				headerDone = true;
				writer->startRelation(L"csv", metadata, true); // TODO: support also custom relation name passed as CLI parameter
			}
		}

		currentValue.str("");
		currentValue.clear();
	}
}

int main(int argc, char** argv) {
	setlocale(LC_ALL, "");
	CLI::untieStdIO();
	CLI cli(argc, argv);

	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;

	try {
		processDataStream(cout, cin);
		resultCode = CLI::EXIT_CODE_SUCCESS;
	} catch (RelpipeWriterException e) {
		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
	}

	return resultCode;
}