//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// 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
//------------------------------------------------------------------------------

/** @file
 * テキストリーダ実装
 * @author Junpee
 */

#include "LampBasic.h"
#include "Core/InputOutput/TextReader.h"
#include "Core/System/StringMethod.h"

namespace Lamp{

//------------------------------------------------------------------------------
// コンストラクタ
TextReader::TextReader(int bufferSize) : Reader(){
	bufferSize_ = bufferSize;
	buffer_ = new char[bufferSize_];
	output_ = new char[bufferSize_];
	position_ = bufferSize_;
}
//------------------------------------------------------------------------------
// デストラクタ
TextReader::~TextReader(){
	SafeArrayDelete(output_);
	SafeArrayDelete(buffer_);
}
//------------------------------------------------------------------------------
// 文字列の一行読み込み
// ここでストリームでなくなっている
String TextReader::readLine(){
	// 初期バッファの読み込み
	if(position_ == bufferSize_){
		readSize_ = Math::minimum(bufferSize_, getSize());
		readBytes(buffer_, readSize_);
		// EOF対策
		filePosition_ = getPosition();
		setPosition(0);
		position_ = 0;
	}
	String returnString;
	int outputPosition = 0;
	while(true){
		char character = buffer_[position_];
		position_++;
		output_[outputPosition] = character;
		outputPosition++;
		// 改行処理
		bool breakFlag = false;
		if(character == '\n'){
			outputPosition--;
			output_[outputPosition] = '\0';
			breakFlag = true;
		}
		if(position_ == readSize_){
			// ファイル終端
			if(readSize_ != bufferSize_){
				output_[outputPosition] = '\0';
				setPosition(filePosition_);
				break;
			}
			// バッファの読み込み
			setPosition(filePosition_);
			readSize_ = Math::minimum(bufferSize_, getSize() - getPosition());
			// 丁度読み終わる
			if(readSize_ == 0){
				output_[outputPosition] = '\0';
				setPosition(filePosition_);
				break;
			}
			readBytes(buffer_, readSize_);
			// EOF対策
			filePosition_ = getPosition();
			setPosition(0);
			position_ = 0;
			// 出力文字列に継ぎ足して出力位置をクリア
			output_[outputPosition] = '\0';
			returnString += output_;
			outputPosition = 0;
			output_[outputPosition] = '\0';
		}
		if(breakFlag){ break; }
	}
	// 出力
	if(output_[outputPosition - 1] == '\r'){
		output_[outputPosition - 1] = '\0';
	}
	returnString += output_;
	return returnString;
}
//------------------------------------------------------------------------------
// テキストリーダデータのコピー
void TextReader::copyTextReaderData(TextReader* destination){
	::memcpy(destination->buffer_, buffer_, bufferSize_);
	::memcpy(destination->output_, output_, bufferSize_);
	destination->readSize_ = readSize_;
	destination->position_ = position_;
	destination->filePosition_ = filePosition_;
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
