/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


#ifndef	__NIOBUFFER__
#define	__NIOBUFFER__


		#include "filesystem/io_handle.h"
		#include "nstring/nstring.h"
		#include "memory/nauto_ptr.h"
		#include "container/narray.h"
		#include "unit/nunit.h"


namespace GS {
	namespace IO {

//
struct	IOBuffer
{
		Array <char> buffer;

		size_t		start_pos;
		size_t		usage;

		IOBuffer() : start_pos(0), usage(0) {}
};

//
class	BufferHandle : public Handle
{
		friend class Buffer;

		AutoPtr <Handle> handle;
		size_t		size;	// file size on wrapped fs
		size_t		pos;	// position in source handle

		IOBuffer	read_buffer;

public:

virtual	size_t		GetSize() { return size; }

		BufferHandle(Base *io, Handle *h) : Handle(io), handle(h), size(0), pos(0) {}
};

/*!
	@short	Buffered I/O filesystem wrapper.
	Implements a transparent read/write buffer on top of another filesystem.
	@author	Emmanuel Julien (ejulien@nworks.fr)
*/
class	Buffer : public Base
{
		SharedPtr <Base> io;

		size_t		read_buffer_size,
					write_buffer_size;

public:

virtual	uint		GetCaps() const;

virtual	Handle		*Open(const char *, Mode = ModeRead);
virtual	void		Close(Handle *);

virtual	String		Hash(const char *);
virtual	bool		Delete(const char *);

virtual	size_t		Tell(Handle *);
virtual	size_t		Seek(Handle *, ptrdiff_t offset, SeekRef = SeekCurrent);

virtual	size_t		Read(Handle *, void *, size_t);
virtual	size_t		Write(Handle *, const void *, size_t);

virtual	bool		MkDir(const char *);

		Buffer(Base *base, size_t read_buffer_size = Units::KB(4), size_t write_buffer_size = Units::KB(4));
};

	}	// IO
}	// GS


#endif	// __NIOBUFFER__
