/* "CodeWorker":	a scripting language for parsing and generating text.

Copyright (C) 1996-1997, 1999-2002 Cédric Lemaire

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

To contact the author: codeworker@free.fr
*/

#ifdef WIN32
#pragma warning(disable : 4786)
#endif

#include "ScpStream.h"

#include "CGRuntime.h"
#include "CGExternalHandling.h"

namespace CodeWorker {
	CGExternalHandling::CGExternalHandling() {
		_pOldExternalHandling = CGRuntime::getExternalHandling();
		CGRuntime::setExternalHandling(this);
	}

	CGExternalHandling::~CGExternalHandling() {
		CGRuntime::setExternalHandling(_pOldExternalHandling);
	}


	CGQuietOutput::~CGQuietOutput() {}

	std::string CGQuietOutput::inputLine(bool bEcho) {
		char tcText[16384];
		if (CGRuntime::getExternalHandling() != this) return CGRuntime::getExternalHandling()->inputLine(bEcho);
		if (getOldExternalHandling() != NULL) return getOldExternalHandling()->inputLine(bEcho);
#ifndef WIN32
		closeKeyboard();
#endif
		std::cin.getline(tcText, 16383);
#ifndef WIN32
		initKeyboard();
#endif
		return tcText;
	}

	std::string CGQuietOutput::inputKey(bool bEcho) {
		if (CGRuntime::getExternalHandling() != this) return CGRuntime::getExternalHandling()->inputKey(bEcho);
		if (getOldExternalHandling() != NULL) return getOldExternalHandling()->inputKey(bEcho);
		int iKey = std::cin.peek();
		return "";
	}

	void CGQuietOutput::traceLine(const std::string& sLine) {
		_sOutput += sLine + CGRuntime::endl();
	}

	void CGQuietOutput::traceText(const std::string& sText) {
		_sOutput += sText;
	}


	CGStandardInput::CGStandardInput(const std::string& sText) : _bOwner(true) {
		_pStandardInput = new ScpStream;
		(*_pStandardInput) << sText;
	}

	CGStandardInput::~CGStandardInput() {
		if (_bOwner) delete _pStandardInput;
	}

	std::string CGStandardInput::inputLine(bool bEcho) {
		std::string sLine;
		_pStandardInput->readLine(sLine);
		if (bEcho) traceLine(sLine);
		return sLine;
	}

	std::string CGStandardInput::inputKey(bool bEcho) {
		std::string sLine;
		int iChar = _pStandardInput->readChar();
		char tcText[] = {0, 0};
		if (iChar > 0) tcText[0] = (char) iChar;
		if (bEcho) traceText(tcText);
		return tcText;
	}

	void CGStandardInput::traceLine(const std::string& sLine) {
		if (CGRuntime::getExternalHandling() != this) CGRuntime::getExternalHandling()->traceLine(sLine);
		else if (getOldExternalHandling() != NULL) getOldExternalHandling()->traceLine(sLine);
		else std::cout << sLine << std::endl;
	}

	void CGStandardInput::traceText(const std::string& sText) {
		if (CGRuntime::getExternalHandling() != this) CGRuntime::getExternalHandling()->traceText(sText);
		else if (getOldExternalHandling() != NULL) getOldExternalHandling()->traceText(sText);
		else std::cout << sText << std::flush;
	}


	CGFileStandardInput::CGFileStandardInput(const std::string& sFilename) : CGStandardInput(*(new ScpStream(sFilename, ScpStream::IN | ScpStream::PATH)), true) {
	}

	CGFileStandardInput::~CGFileStandardInput() {
	}
}
