/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/sgx/filesystem/memorycard/memcardfiledevice.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/filesystem/filesystem.hpp"
#include "sgx/filesystem/memorycard/memorycard.hpp"

//
// Memory Device
//
CSGXDeviceMemoryCard::CSGXDeviceMemoryCard(const tUINT32 maxSize) : CSGXDevice()
{
	m_Size = maxSize;
}

tBOOL CSGXDeviceMemoryCard::mountDirectory(CSGXFileEntry *pEntry, const sgxString &rootmount, tBOOL recursive)
{
SGX_UNUSED(rootmount);
SGX_UNUSED(recursive);

	// TODO: search the slots for existing saved games, and add
	// these to the filesystem, thus:
	// CSGXFileSystem::get()->addNewFile(const CSGXFileSysData &FileData);

    return TRUE;
}

void CSGXDeviceMemoryCard::getRoot(sgxString &rootPath) {
	rootPath = "";
}

tMEM_SIZE 
CSGXDeviceMemoryCard::getBlockSize() {
	return m_Size;
}

CSGXDeviceBlockState *CSGXDeviceMemoryCard::open(CSGXDeviceBlock *pBlk, const sgxString &access)
{
	// If we read: load everything into the state block, delete RAM on close
	// If we write: write everything into a temporary memory block, write data on close

	CSGXDeviceBlockMemoryCard *pBlock = SGX_STATIC_CAST(CSGXDeviceBlockMemoryCard *, pBlk);
	CSGXDeviceMemoryCardFile *pState = new CSGXDeviceMemoryCardFile(this, pBlock, (access == "rw")?TRUE:FALSE);
	return pState;
}

CSGXFileEntry *CSGXDeviceMemoryCard::addNewFile(CSGXFileEntry *pNewEntry)
{
    pNewEntry->m_pBlock = new CSGXDeviceBlockMemoryCard(pNewEntry->m_Data.m_Name);
    pNewEntry->m_pFSDevice = this;
    return pNewEntry;
}

CSGXFileEntry *CSGXDeviceMemoryCard::createNewFile(CSGXFileEntry *pParentEntry, const sgxString &filename) {
	CSGXFileEntry *pFileEntry = CSGXFileSystem::get()->createFileEntry(pParentEntry, filename);
	addNewFile(pFileEntry);
	return pFileEntry;
}
