/*
    Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301  USA.
*/

#include "inputoutputhandler.h"
#include "moc_inputoutputhandler.cpp"

#include <QTextStream>

#include <iostream>
#include <string>

InputOutputHandler::InputOutputHandler(QObject* parent)
	: QObject(parent)
{
	_out = new QTextStream(stdout);
	_in = new QTextStream(stdin);
}

InputOutputHandler::~InputOutputHandler()
{
	if(_out)
		delete _out;
	if(_in)
		delete _in;
}

QString InputOutputHandler::readLine()
{
	if(!_in)
		return QString();
	
	QString line = _in->readLine();
	return line.trimmed();
}

void InputOutputHandler::setFieldWidth(int columns)
{
	if(!_out)
		return;
	_out->setFieldWidth(columns);
	_out->setFieldAlignment(QTextStream::AlignLeft);
}

bool InputOutputHandler::print(const QString& content)
{
	if(!_out)
		return false;
	*_out << content << flush;
	return true;
}

InputOutputHandler& InputOutputHandler::operator<<(const QString& output)
{
	print(output);
	return *this;
}

InputOutputHandler& InputOutputHandler::operator>>(QString& input)
{
	input = readLine();
	return *this;
}

InputOutputHandler& endl(InputOutputHandler &handler)
{
	return handler << "\n";
}
