/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2008.
     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: src/drivers/wintel32/filesystem/win32state.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/filesystem/filesystem.hpp"
#include "drivers/wintel32/core/core.hpp"
#include "drivers/wintel32/filesystem/win32disc.hpp"

//
// State
//
CSGXDeviceBlockStateWin32Disc::CSGXDeviceBlockStateWin32Disc(HANDLE hFile) : CSGXDeviceBlockState()
{
    m_hFile = hFile;
}

tMEM_SIZE CSGXDeviceBlockStateWin32Disc::read(tBYTE *pDest, tMEM_SIZE size)
{
DWORD size2read = size;
DWORD bytesread;

    ::ReadFile(m_hFile, pDest, size2read, &bytesread, NULL);
    return bytesread;
}

tMEM_SIZE CSGXDeviceBlockStateWin32Disc::write(tBYTE *pDest, tMEM_SIZE size)
{
DWORD size2write = size;
DWORD byteswritten;

    ::WriteFile(m_hFile, pDest, size2write, &byteswritten, NULL);
    return byteswritten;
}

tBOOL CSGXDeviceBlockStateWin32Disc::seek(tINT32 offset, CSGXDeviceBlockState::tSeekFrom from)
{
DWORD win32from;

    switch(from)
    {
    case    eSeekFromBegin:   win32from = FILE_BEGIN; break;
    case    eSeekFromCurrent: win32from = FILE_CURRENT; break;
    case    eSeekFromEnd:     win32from = FILE_END; break;
    }

    ::SetFilePointer(m_hFile, offset, NULL, win32from);
    return true;
}


tDISC_SIZE 		
CSGXDeviceBlockStateWin32Disc::getFileSize() const {
	return ::GetFileSize(m_hFile, NULL);
}


tDISC_SIZE 
CSGXDeviceBlockStateWin32Disc::getSeekPos() const {
    return ::SetFilePointer(m_hFile, 0, NULL, FILE_CURRENT);
}
