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


#ifndef	__NIOMEMORY__
#define	__NIOMEMORY__


		#include "filesystem/io_handle.h"
		#include "container/nlist.h"
		#include "nstring/nstring.h"


namespace GS {
	namespace IO {

//
class	MemoryFile
{
		friend class Memory;

		String		uri;
		Array <char> data;
		size_t		size;		//< Might be != data.GetSize()

		MemoryFile(const char *_uri = 0) : uri(_uri), size(0) {}

public:

		const String &GetUri() const { return uri; }
		const Array <char> &GetData() const { return data; }
		size_t		GetSize() const { return size; }
};

//
class	MemoryHandle : public Handle
{
		friend class Memory;

		MemoryFile	*file;
		size_t		cursor;
		Mode		mode;

		MemoryHandle(Base *, MemoryFile *, Mode = ModeRead);

public:

		~MemoryHandle();
};

/*!
	@short	Memory I/O.
	@author	Emmanuel Julien (ejulien@nworks.fr)
*/
class	Memory : public Base
{
		AutoList <MemoryFile *> fat;

public:

		/// Return the system file allocation table.
		const List <MemoryFile *> &GetFat() const { return fat; }

virtual	uint		GetCaps() const;

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

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 *) { return false; }
};

	}	// IO
}	// GS


#endif	// __NIOMEMORY__
