mirror of
https://github.com/elasota/Aerofoil.git
synced 2025-12-15 04:29:37 +00:00
Move IOStream to GpCommon
This commit is contained in:
@@ -1,149 +1,149 @@
|
||||
#include "GpFileStream_Win32.h"
|
||||
|
||||
GpFileStream_Win32::GpFileStream_Win32(HANDLE handle, bool readable, bool writeable, bool seekable)
|
||||
: m_handle(handle)
|
||||
, m_readable(readable)
|
||||
, m_writeable(writeable)
|
||||
, m_seekable(seekable)
|
||||
{
|
||||
}
|
||||
|
||||
size_t GpFileStream_Win32::Read(void *bytesOut, size_t size)
|
||||
{
|
||||
if (!m_readable)
|
||||
return 0;
|
||||
|
||||
size_t totalRead = 0;
|
||||
while (size)
|
||||
{
|
||||
const DWORD chunkSizeToRead = (size > MAXDWORD) ? MAXDWORD : size;
|
||||
DWORD numRead = 0;
|
||||
|
||||
BOOL readSucceeded = ReadFile(m_handle, bytesOut, chunkSizeToRead, &numRead, nullptr);
|
||||
if (!readSucceeded)
|
||||
return totalRead;
|
||||
|
||||
totalRead += static_cast<size_t>(numRead);
|
||||
size -= static_cast<size_t>(numRead);
|
||||
bytesOut = static_cast<void*>(static_cast<uint8_t*>(bytesOut) + numRead);
|
||||
|
||||
if (numRead != chunkSizeToRead)
|
||||
return totalRead;
|
||||
}
|
||||
|
||||
return totalRead;
|
||||
}
|
||||
|
||||
size_t GpFileStream_Win32::Write(const void *bytes, size_t size)
|
||||
{
|
||||
if (!m_writeable)
|
||||
return 0;
|
||||
|
||||
size_t totalWritten = 0;
|
||||
while (size)
|
||||
{
|
||||
const DWORD chunkSizeToWrite = (size > MAXDWORD) ? MAXDWORD : size;
|
||||
DWORD numWritten = 0;
|
||||
|
||||
BOOL writeSucceeded = WriteFile(m_handle, bytes, chunkSizeToWrite, &numWritten, nullptr);
|
||||
#include "GpFileStream_Win32.h"
|
||||
|
||||
GpFileStream_Win32::GpFileStream_Win32(HANDLE handle, bool readable, bool writeable, bool seekable)
|
||||
: m_handle(handle)
|
||||
, m_readable(readable)
|
||||
, m_writeable(writeable)
|
||||
, m_seekable(seekable)
|
||||
{
|
||||
}
|
||||
|
||||
size_t GpFileStream_Win32::Read(void *bytesOut, size_t size)
|
||||
{
|
||||
if (!m_readable)
|
||||
return 0;
|
||||
|
||||
size_t totalRead = 0;
|
||||
while (size)
|
||||
{
|
||||
const DWORD chunkSizeToRead = (size > MAXDWORD) ? MAXDWORD : size;
|
||||
DWORD numRead = 0;
|
||||
|
||||
BOOL readSucceeded = ReadFile(m_handle, bytesOut, chunkSizeToRead, &numRead, nullptr);
|
||||
if (!readSucceeded)
|
||||
return totalRead;
|
||||
|
||||
totalRead += static_cast<size_t>(numRead);
|
||||
size -= static_cast<size_t>(numRead);
|
||||
bytesOut = static_cast<void*>(static_cast<uint8_t*>(bytesOut) + numRead);
|
||||
|
||||
if (numRead != chunkSizeToRead)
|
||||
return totalRead;
|
||||
}
|
||||
|
||||
return totalRead;
|
||||
}
|
||||
|
||||
size_t GpFileStream_Win32::Write(const void *bytes, size_t size)
|
||||
{
|
||||
if (!m_writeable)
|
||||
return 0;
|
||||
|
||||
size_t totalWritten = 0;
|
||||
while (size)
|
||||
{
|
||||
const DWORD chunkSizeToWrite = (size > MAXDWORD) ? MAXDWORD : size;
|
||||
DWORD numWritten = 0;
|
||||
|
||||
BOOL writeSucceeded = WriteFile(m_handle, bytes, chunkSizeToWrite, &numWritten, nullptr);
|
||||
if (!writeSucceeded)
|
||||
{
|
||||
DWORD lastError = GetLastError();
|
||||
DWORD lastError = GetLastError();
|
||||
return totalWritten;
|
||||
}
|
||||
|
||||
totalWritten += static_cast<size_t>(numWritten);
|
||||
size -= static_cast<size_t>(numWritten);
|
||||
bytes = static_cast<const void*>(static_cast<const uint8_t*>(bytes) + numWritten);
|
||||
|
||||
if (numWritten != chunkSizeToWrite)
|
||||
return totalWritten;
|
||||
}
|
||||
|
||||
return totalWritten;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::IsSeekable() const
|
||||
{
|
||||
return m_seekable;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::IsReadOnly() const
|
||||
{
|
||||
return !m_writeable;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::IsWriteOnly() const
|
||||
{
|
||||
return !m_readable;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::SeekStart(PortabilityLayer::UFilePos_t loc)
|
||||
{
|
||||
LARGE_INTEGER li;
|
||||
li.QuadPart = static_cast<LONGLONG>(loc);
|
||||
return SetFilePointerEx(m_handle, li, nullptr, FILE_BEGIN) != 0;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::SeekCurrent(PortabilityLayer::FilePos_t loc)
|
||||
{
|
||||
LARGE_INTEGER li;
|
||||
li.QuadPart = static_cast<LONGLONG>(loc);
|
||||
return SetFilePointerEx(m_handle, li, nullptr, FILE_CURRENT) != 0;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::SeekEnd(PortabilityLayer::UFilePos_t loc)
|
||||
{
|
||||
LARGE_INTEGER li;
|
||||
li.QuadPart = -static_cast<LONGLONG>(loc);
|
||||
return SetFilePointerEx(m_handle, li, nullptr, FILE_END) != 0;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::Truncate(PortabilityLayer::UFilePos_t loc)
|
||||
{
|
||||
if (!m_writeable)
|
||||
return false;
|
||||
|
||||
PortabilityLayer::UFilePos_t oldPos = Tell();
|
||||
if (!SeekStart(loc))
|
||||
return false;
|
||||
|
||||
if (!SetEndOfFile(m_handle))
|
||||
return false;
|
||||
|
||||
if (!SeekStart(oldPos))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
PortabilityLayer::UFilePos_t GpFileStream_Win32::Size() const
|
||||
{
|
||||
LARGE_INTEGER fsize;
|
||||
if (!GetFileSizeEx(m_handle, &fsize))
|
||||
return 0;
|
||||
|
||||
return static_cast<PortabilityLayer::UFilePos_t>(fsize.QuadPart);
|
||||
}
|
||||
|
||||
PortabilityLayer::UFilePos_t GpFileStream_Win32::Tell() const
|
||||
{
|
||||
LARGE_INTEGER zero;
|
||||
zero.QuadPart = 0;
|
||||
|
||||
LARGE_INTEGER fpos;
|
||||
if (!SetFilePointerEx(m_handle, zero, &fpos, FILE_CURRENT))
|
||||
return 0;
|
||||
|
||||
return static_cast<PortabilityLayer::UFilePos_t>(fpos.QuadPart);
|
||||
}
|
||||
|
||||
void GpFileStream_Win32::Close()
|
||||
{
|
||||
CloseHandle(m_handle);
|
||||
}
|
||||
}
|
||||
|
||||
totalWritten += static_cast<size_t>(numWritten);
|
||||
size -= static_cast<size_t>(numWritten);
|
||||
bytes = static_cast<const void*>(static_cast<const uint8_t*>(bytes) + numWritten);
|
||||
|
||||
if (numWritten != chunkSizeToWrite)
|
||||
return totalWritten;
|
||||
}
|
||||
|
||||
return totalWritten;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::IsSeekable() const
|
||||
{
|
||||
return m_seekable;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::IsReadOnly() const
|
||||
{
|
||||
return !m_writeable;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::IsWriteOnly() const
|
||||
{
|
||||
return !m_readable;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::SeekStart(GpUFilePos_t loc)
|
||||
{
|
||||
LARGE_INTEGER li;
|
||||
li.QuadPart = static_cast<LONGLONG>(loc);
|
||||
return SetFilePointerEx(m_handle, li, nullptr, FILE_BEGIN) != 0;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::SeekCurrent(GpFilePos_t loc)
|
||||
{
|
||||
LARGE_INTEGER li;
|
||||
li.QuadPart = static_cast<LONGLONG>(loc);
|
||||
return SetFilePointerEx(m_handle, li, nullptr, FILE_CURRENT) != 0;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::SeekEnd(GpUFilePos_t loc)
|
||||
{
|
||||
LARGE_INTEGER li;
|
||||
li.QuadPart = -static_cast<LONGLONG>(loc);
|
||||
return SetFilePointerEx(m_handle, li, nullptr, FILE_END) != 0;
|
||||
}
|
||||
|
||||
bool GpFileStream_Win32::Truncate(GpUFilePos_t loc)
|
||||
{
|
||||
if (!m_writeable)
|
||||
return false;
|
||||
|
||||
GpUFilePos_t oldPos = Tell();
|
||||
if (!SeekStart(loc))
|
||||
return false;
|
||||
|
||||
if (!SetEndOfFile(m_handle))
|
||||
return false;
|
||||
|
||||
if (!SeekStart(oldPos))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
GpUFilePos_t GpFileStream_Win32::Size() const
|
||||
{
|
||||
LARGE_INTEGER fsize;
|
||||
if (!GetFileSizeEx(m_handle, &fsize))
|
||||
return 0;
|
||||
|
||||
return static_cast<GpUFilePos_t>(fsize.QuadPart);
|
||||
}
|
||||
|
||||
GpUFilePos_t GpFileStream_Win32::Tell() const
|
||||
{
|
||||
LARGE_INTEGER zero;
|
||||
zero.QuadPart = 0;
|
||||
|
||||
LARGE_INTEGER fpos;
|
||||
if (!SetFilePointerEx(m_handle, zero, &fpos, FILE_CURRENT))
|
||||
return 0;
|
||||
|
||||
return static_cast<GpUFilePos_t>(fpos.QuadPart);
|
||||
}
|
||||
|
||||
void GpFileStream_Win32::Close()
|
||||
{
|
||||
CloseHandle(m_handle);
|
||||
}
|
||||
|
||||
void GpFileStream_Win32::Flush()
|
||||
{
|
||||
FlushFileBuffers(m_handle);
|
||||
FlushFileBuffers(m_handle);
|
||||
}
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "GpCoreDefs.h"
|
||||
#include "GpWindows.h"
|
||||
#include "IOStream.h"
|
||||
|
||||
class GpFileStream_Win32 final : public PortabilityLayer::IOStream
|
||||
{
|
||||
public:
|
||||
explicit GpFileStream_Win32(HANDLE handle, bool readable, bool writeable, bool seekable);
|
||||
|
||||
size_t Read(void *bytesOut, size_t size) override;
|
||||
size_t Write(const void *bytes, size_t size) override;
|
||||
bool IsSeekable() const override;
|
||||
bool IsReadOnly() const override;
|
||||
bool IsWriteOnly() const override;
|
||||
bool SeekStart(PortabilityLayer::UFilePos_t loc) override;
|
||||
bool SeekCurrent(PortabilityLayer::FilePos_t loc) override;
|
||||
bool SeekEnd(PortabilityLayer::UFilePos_t loc) override;
|
||||
bool Truncate(PortabilityLayer::UFilePos_t loc) override;
|
||||
PortabilityLayer::UFilePos_t Size() const override;
|
||||
PortabilityLayer::UFilePos_t Tell() const override;
|
||||
#pragma once
|
||||
|
||||
#include "GpCoreDefs.h"
|
||||
#include "GpWindows.h"
|
||||
#include "GpIOStream.h"
|
||||
|
||||
class GpFileStream_Win32 final : public GpIOStream
|
||||
{
|
||||
public:
|
||||
explicit GpFileStream_Win32(HANDLE handle, bool readable, bool writeable, bool seekable);
|
||||
|
||||
size_t Read(void *bytesOut, size_t size) override;
|
||||
size_t Write(const void *bytes, size_t size) override;
|
||||
bool IsSeekable() const override;
|
||||
bool IsReadOnly() const override;
|
||||
bool IsWriteOnly() const override;
|
||||
bool SeekStart(GpUFilePos_t loc) override;
|
||||
bool SeekCurrent(GpFilePos_t loc) override;
|
||||
bool SeekEnd(GpUFilePos_t loc) override;
|
||||
bool Truncate(GpUFilePos_t loc) override;
|
||||
GpUFilePos_t Size() const override;
|
||||
GpUFilePos_t Tell() const override;
|
||||
void Close() override;
|
||||
void Flush() override;
|
||||
|
||||
private:
|
||||
HANDLE m_handle;
|
||||
bool m_readable;
|
||||
bool m_writeable;
|
||||
bool m_seekable;
|
||||
};
|
||||
void Flush() override;
|
||||
|
||||
private:
|
||||
HANDLE m_handle;
|
||||
bool m_readable;
|
||||
bool m_writeable;
|
||||
bool m_seekable;
|
||||
};
|
||||
|
||||
@@ -210,7 +210,7 @@ bool GpFileSystem_Win32::FileLocked(PortabilityLayer::VirtualDirectory_t virtual
|
||||
return (attribs & FILE_ATTRIBUTE_READONLY) != 0;
|
||||
}
|
||||
|
||||
PortabilityLayer::IOStream *GpFileSystem_Win32::OpenFile(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path, bool writeAccess, GpFileCreationDisposition_t createDisposition)
|
||||
GpIOStream *GpFileSystem_Win32::OpenFile(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path, bool writeAccess, GpFileCreationDisposition_t createDisposition)
|
||||
{
|
||||
wchar_t winPath[MAX_PATH + 1];
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ public:
|
||||
|
||||
bool FileExists(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path) override;
|
||||
bool FileLocked(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path, bool *exists) override;
|
||||
PortabilityLayer::IOStream *OpenFile(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path, bool writeAccess, GpFileCreationDisposition_t createDisposition) override;
|
||||
GpIOStream *OpenFile(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path, bool writeAccess, GpFileCreationDisposition_t createDisposition) override;
|
||||
bool DeleteFile(PortabilityLayer::VirtualDirectory_t virtualDirectory, const char *path, bool &existed) override;
|
||||
PortabilityLayer::HostDirectoryCursor *ScanDirectory(PortabilityLayer::VirtualDirectory_t virtualDirectory) override;
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "GpFileSystem_Win32.h"
|
||||
|
||||
#include "GpApplicationName.h"
|
||||
#include "IOStream.h"
|
||||
#include "GpIOStream.h"
|
||||
|
||||
GpLogDriver_Win32::GpLogDriver_Win32()
|
||||
: m_stream(nullptr)
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
#pragma once
|
||||
#pragma once
|
||||
|
||||
#include "IGpLogDriver.h"
|
||||
|
||||
namespace PortabilityLayer
|
||||
{
|
||||
class IOStream;
|
||||
}
|
||||
class GpIOStream;
|
||||
|
||||
class GpLogDriver_Win32 : public IGpLogDriver
|
||||
{
|
||||
@@ -22,7 +19,7 @@ public:
|
||||
private:
|
||||
void InitInternal();
|
||||
|
||||
PortabilityLayer::IOStream *m_stream;
|
||||
GpIOStream *m_stream;
|
||||
bool m_isInitialized;
|
||||
|
||||
static GpLogDriver_Win32 ms_instance;
|
||||
|
||||
Reference in New Issue
Block a user