//------------------------------------------------------------------------------
// 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/MemoryOutputStream.h"

namespace Lamp{

//------------------------------------------------------------------------------
// コンストラクタ
MemoryOutputStream::MemoryOutputStream(int bufferInitialSize){
	bufferSize_ = bufferInitialSize;
	buffer_ = new char[bufferSize_];
	size_ = position_ = 0;
}
//------------------------------------------------------------------------------
// デストラクタ
MemoryOutputStream::~MemoryOutputStream(){
	delete[] buffer_;
}
//------------------------------------------------------------------------------
// バッファチェック
void MemoryOutputStream::bufferCheck(int size){
	int newPosition = position_ + size;
	if(newPosition > bufferSize_){
		while(true){
			bufferSize_ *= 2;
			if(newPosition <= bufferSize_){ break; }
		}
		char* newBuffer = new char[bufferSize_];
		std::memcpy(newBuffer, buffer_, size_);
		delete[] buffer_;
		buffer_ = newBuffer;
	}
}
//------------------------------------------------------------------------------
// バイトデータの書き出し
void MemoryOutputStream::writeBytes(const void* data, int size){
	bufferCheck(size);
	std::memcpy(buffer_ + position_, data, size);
	position_ += size;
	if(position_ > size_){ size_ = position_; }
}
//------------------------------------------------------------------------------
// サイズの取得
int MemoryOutputStream::getSize(){
	return size_;
}
//------------------------------------------------------------------------------
// スキップ
void MemoryOutputStream::skip(int size){
	bufferCheck(size);
	std::memset(buffer_ + position_, 0, size);
	position_ += size;
	if(position_ > size_){ size_ = position_; }
}
//------------------------------------------------------------------------------
// アライメントを取る
int MemoryOutputStream::align(int alignSize){
	int size = position_ % alignSize;
	if(size== 0){ return size; }
	size = alignSize - size;
	bufferCheck(size);
	std::memset(buffer_ + position_, 0, size);
	position_ += size;
	if(position_ > size_){ size_ = position_; }
	return size;
}
//------------------------------------------------------------------------------
// 書き込み位置の取得
int MemoryOutputStream::getPosition(){
	return position_;
}
//------------------------------------------------------------------------------
// 書き込み位置の設定
void MemoryOutputStream::setPosition(int position){
	Assert(position >= 0);
	Assert(position <= size_);
	position_ = position;
}
//------------------------------------------------------------------------------
// フラッシュ
void MemoryOutputStream::flush(){
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
