//------------------------------------------------------------------------------
// 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 <stdio.h>
#include "LampBasic.h"
#include "Core/InputOutput/MemoryInputStream.h"

namespace Lamp{

//------------------------------------------------------------------------------
// コンストラクタ
MemoryInputStream::MemoryInputStream(const char* buffer, int size) :
	buffer_(buffer), size_(size), position_(0){
}
//------------------------------------------------------------------------------
// デストラクタ
MemoryInputStream::~MemoryInputStream(){
}
//------------------------------------------------------------------------------
// メモリ入力ストリームの複製
MemoryInputStream* MemoryInputStream::cloneMemoryInputStream(){
	MemoryInputStream* memoryInputStream =
		new MemoryInputStream(buffer_, size_);
	memoryInputStream->setPosition(getPosition());
	return memoryInputStream;
}
//------------------------------------------------------------------------------
// 終端かどうか
bool MemoryInputStream::isEnd(){
	return (position_ == size_);
}
//------------------------------------------------------------------------------
// バイトデータの読み出し
void MemoryInputStream::readBytes(void* data, int size){
	Assert((position_ + size) <= size_);
	std::memcpy(data, buffer_ + position_, size);
	position_ += size;
}
//------------------------------------------------------------------------------
// サイズの取得
int MemoryInputStream::getSize(){
	return size_;
}
//------------------------------------------------------------------------------
// スキップ
void MemoryInputStream::skip(int size){
	Assert((position_ + size) <= size_);
	position_ += size;
}
//------------------------------------------------------------------------------
// アライメントを取る
int MemoryInputStream::align(int alignSize){
	int size = position_ % alignSize;
	if(size == 0){ return size; }
	size = alignSize - size;
	Assert((position_ + size) <= size_);
	position_ += size;
	return size;
}
//------------------------------------------------------------------------------
// 読み込み位置の取得
int MemoryInputStream::getPosition(){
	return position_;
}
//------------------------------------------------------------------------------
// 読み込み位置の設定
void MemoryInputStream::setPosition(int position){
	Assert(position >= 0);
	Assert(position <= size_);
	position_ = position;
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
