#include <assert.h>
#include "filesystem/stream.h"
#include "filesystem/filemanager.h"

class FileStream : public StreamBase
{
public:
	FileStream(File* file);
	~FileStream();

	size_t readBuffer(void* buffer, size_t size);
	size_t writeBuffer(void* buffer, size_t size);
	void seek(SeekDir way, long offset);
	size_t tell();
	bool eof();

private:
	File* m_file;
};

FileStream::FileStream(File* file)
{
	assert(file && "Cannot open file stream with nullptr file handle.");
	m_file = file;
}

FileStream::~FileStream()
{
	if (m_file)
		g_fileManager->CloseFile(m_file);
}

size_t FileStream::readBuffer(void* buffer, size_t size)
{
	return m_file->read(buffer, size);
}

size_t FileStream::writeBuffer(void* buffer, size_t size)
{
	return m_file->write(buffer, size);
}

void FileStream::seek(SeekDir way, long offset)
{
	m_file->seek(way, offset);
}

size_t FileStream::tell()
{
	return m_file->tell();
}

bool FileStream::eof()
{
	return m_file->eof();;
}

StreamBase* createFileStream(File* file)
{
	return new FileStream(file);
}