/** 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/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;
}

CSGXDeviceBlockStateWin32Disc::~CSGXDeviceBlockStateWin32Disc() {
    ::CloseHandle(m_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(const 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;
	default:
		sgxError("Unknown seek from type");
		win32from = FILE_BEGIN;
    }

    ::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);
}
