/** 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/ramfile/ramstate.cpp

*/

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

//
// Block State
//
tMEM_SIZE CSGXDeviceBlockStateMemory::read(tBYTE *pDest, tMEM_SIZE size)
{
    tMEM_SIZE readbytes;

    // Check if file is open? Done by filemanager? Yes!
    if (m_CurrOffset + size > m_pMemDevice->m_Size) {
        readbytes = m_pMemDevice->m_Size - m_CurrOffset;
    } else {
        readbytes = size;
    }

    sgxMemmove(pDest, &m_pMemDevice->m_Ptr[m_CurrOffset], readbytes);
    m_CurrOffset += readbytes;
    return readbytes;
}

tMEM_SIZE CSGXDeviceBlockStateMemory::write(const tBYTE *pSource, tMEM_SIZE size)
{
    tMEM_SIZE written;

    if (m_CurrOffset + size > m_pMemDevice->m_Size) {
        written = m_pMemDevice->m_Size - m_CurrOffset;
    } else {
        written = size;
    }

    sgxMemmove(&m_pMemDevice->m_Ptr[m_CurrOffset], pSource, written);
    m_CurrOffset += written;
    return written;
}

tBOOL CSGXDeviceBlockStateMemory::seek(tINT32 offset, CSGXDeviceBlockState::tSeekFrom from)
{
    switch(from)
    {
    case    eSeekFromBegin:   m_CurrOffset = offset; break;
    case    eSeekFromCurrent: m_CurrOffset += offset; break;
    case    eSeekFromEnd:     m_CurrOffset = m_pMemDevice->m_Size+offset; break;
    }

    return true;
}

tDISC_SIZE CSGXDeviceBlockStateMemory::getSeekPos() const
{
    return m_CurrOffset;
}
