ByteStream: Move routines from FileSystem to ByteStream
This commit is contained in:
parent
d81e156a29
commit
b5bf3593c4
|
@ -118,7 +118,8 @@ add_library(common
|
|||
|
||||
target_include_directories(common PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/..")
|
||||
target_include_directories(common PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/..")
|
||||
target_link_libraries(common PRIVATE glad stb Threads::Threads libchdr glslang vulkan-loader zlib minizip samplerate)
|
||||
target_link_libraries(common PUBLIC fmt Threads::Threads)
|
||||
target_link_libraries(common PRIVATE glad stb libchdr glslang vulkan-loader zlib minizip samplerate)
|
||||
|
||||
if(WIN32)
|
||||
target_sources(common PRIVATE
|
||||
|
|
|
@ -896,9 +896,7 @@ void GrowableMemoryByteStream::Grow(u32 MinimumGrowth)
|
|||
ResizeMemory(NewSize);
|
||||
}
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
|
||||
std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, u32 openMode)
|
||||
std::unique_ptr<ByteStream> ByteStream::OpenFile(const char* fileName, u32 openMode)
|
||||
{
|
||||
if ((openMode & (BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE)) == BYTESTREAM_OPEN_WRITE)
|
||||
{
|
||||
|
@ -945,80 +943,10 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, u32
|
|||
|
||||
modeString[modeStringLength] = 0;
|
||||
|
||||
if (openMode & BYTESTREAM_OPEN_CREATE_PATH)
|
||||
{
|
||||
u32 i;
|
||||
u32 fileNameLength = static_cast<u32>(std::strlen(fileName));
|
||||
char* tempStr = (char*)alloca(fileNameLength + 1);
|
||||
|
||||
// check if it starts with a drive letter. if so, skip ahead
|
||||
if (fileNameLength >= 2 && fileName[1] == ':')
|
||||
{
|
||||
if (fileNameLength <= 3)
|
||||
{
|
||||
// create a file called driveletter: or driveletter:\ ? you must be crazy
|
||||
i = fileNameLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::memcpy(tempStr, fileName, 3);
|
||||
i = 3;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// start at beginning
|
||||
i = 0;
|
||||
}
|
||||
|
||||
// step through each path component, create folders as necessary
|
||||
for (; i < fileNameLength; i++)
|
||||
{
|
||||
if (i > 0 && (fileName[i] == '\\' || fileName[i] == '/'))
|
||||
{
|
||||
// terminate the string
|
||||
tempStr[i] = '\0';
|
||||
|
||||
// check if it exists
|
||||
struct stat s;
|
||||
if (stat(tempStr, &s) < 0)
|
||||
{
|
||||
if (errno == ENOENT)
|
||||
{
|
||||
// try creating it
|
||||
if (_mkdir(tempStr) < 0)
|
||||
{
|
||||
// no point trying any further down the chain
|
||||
break;
|
||||
}
|
||||
}
|
||||
else // if (errno == ENOTDIR)
|
||||
{
|
||||
// well.. someone's trying to open a fucking weird path that is comprised of both directories and files...
|
||||
// I aint sticking around here to find out what disaster awaits... let fopen deal with it
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// append platform path seperator
|
||||
#if defined(_WIN32)
|
||||
tempStr[i] = '\\';
|
||||
#else
|
||||
tempStr[i] = '/';
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
// append character to temp string
|
||||
tempStr[i] = fileName[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (openMode & BYTESTREAM_OPEN_ATOMIC_UPDATE)
|
||||
{
|
||||
DebugAssert(openMode & (BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE));
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef _UWP
|
||||
// generate the temporary file name
|
||||
u32 fileNameLength = static_cast<u32>(std::strlen(fileName));
|
||||
|
@ -1116,131 +1044,7 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, u32
|
|||
|
||||
// return pointer
|
||||
return pStream;
|
||||
}
|
||||
else
|
||||
{
|
||||
// forward through
|
||||
FILE* pFile = FileSystem::OpenCFile(fileName, modeString);
|
||||
if (!pFile)
|
||||
return nullptr;
|
||||
|
||||
return std::make_unique<FileByteStream>(pFile);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, u32 openMode)
|
||||
{
|
||||
if ((openMode & (BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE)) == BYTESTREAM_OPEN_WRITE)
|
||||
{
|
||||
// if opening with write but not create, the path must exist.
|
||||
struct stat s;
|
||||
if (stat(fileName, &s) < 0)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
char modeString[16];
|
||||
u32 modeStringLength = 0;
|
||||
|
||||
if (openMode & BYTESTREAM_OPEN_WRITE)
|
||||
{
|
||||
if (openMode & BYTESTREAM_OPEN_TRUNCATE)
|
||||
modeString[modeStringLength++] = 'w';
|
||||
else
|
||||
modeString[modeStringLength++] = 'a';
|
||||
|
||||
modeString[modeStringLength++] = 'b';
|
||||
|
||||
if (openMode & BYTESTREAM_OPEN_READ)
|
||||
modeString[modeStringLength++] = '+';
|
||||
}
|
||||
else if (openMode & BYTESTREAM_OPEN_READ)
|
||||
{
|
||||
modeString[modeStringLength++] = 'r';
|
||||
modeString[modeStringLength++] = 'b';
|
||||
}
|
||||
|
||||
modeString[modeStringLength] = 0;
|
||||
|
||||
if (openMode & BYTESTREAM_OPEN_CREATE_PATH)
|
||||
{
|
||||
u32 i;
|
||||
const u32 fileNameLength = static_cast<u32>(std::strlen(fileName));
|
||||
char* tempStr = (char*)alloca(fileNameLength + 1);
|
||||
|
||||
#if defined(_WIN32)
|
||||
// check if it starts with a drive letter. if so, skip ahead
|
||||
if (fileNameLength >= 2 && fileName[1] == ':')
|
||||
{
|
||||
if (fileNameLength <= 3)
|
||||
{
|
||||
// create a file called driveletter: or driveletter:\ ? you must be crazy
|
||||
i = fileNameLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::memcpy(tempStr, fileName, 3);
|
||||
i = 3;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// start at beginning
|
||||
i = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
// step through each path component, create folders as necessary
|
||||
for (i = 0; i < fileNameLength; i++)
|
||||
{
|
||||
if (i > 0 && (fileName[i] == '\\' || fileName[i] == '/') && fileName[i] != ':')
|
||||
{
|
||||
// terminate the string
|
||||
tempStr[i] = '\0';
|
||||
|
||||
// check if it exists
|
||||
struct stat s;
|
||||
if (stat(tempStr, &s) < 0)
|
||||
{
|
||||
if (errno == ENOENT)
|
||||
{
|
||||
// try creating it
|
||||
#if defined(_WIN32)
|
||||
if (mkdir(tempStr) < 0)
|
||||
#else
|
||||
if (mkdir(tempStr, 0777) < 0)
|
||||
#endif
|
||||
{
|
||||
// no point trying any further down the chain
|
||||
break;
|
||||
}
|
||||
}
|
||||
else // if (errno == ENOTDIR)
|
||||
{
|
||||
// well.. someone's trying to open a fucking weird path that is comprised of both directories and
|
||||
// files... I aint sticking around here to find out what disaster awaits... let fopen deal with it
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// append platform path seperator
|
||||
#if defined(_WIN32)
|
||||
tempStr[i] = '\\';
|
||||
#else
|
||||
tempStr[i] = '/';
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
// append character to temp string
|
||||
tempStr[i] = fileName[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (openMode & BYTESTREAM_OPEN_ATOMIC_UPDATE)
|
||||
{
|
||||
DebugAssert(openMode & (BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE));
|
||||
|
||||
// generate the temporary file name
|
||||
|
@ -1256,7 +1060,7 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, u32
|
|||
#endif
|
||||
|
||||
// open the file
|
||||
std::FILE* pTemporaryFile = std::fopen(temporaryFileName, modeString);
|
||||
std::FILE* pTemporaryFile = FileSystem::OpenCFile(temporaryFileName, modeString);
|
||||
if (pTemporaryFile == nullptr)
|
||||
return nullptr;
|
||||
|
||||
|
@ -1267,7 +1071,7 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, u32
|
|||
// do we need to copy the existing file into this one?
|
||||
if (!(openMode & BYTESTREAM_OPEN_TRUNCATE))
|
||||
{
|
||||
std::FILE* pOriginalFile = std::fopen(fileName, "rb");
|
||||
std::FILE* pOriginalFile = FileSystem::OpenCFile(fileName, "rb");
|
||||
if (!pOriginalFile)
|
||||
{
|
||||
// this will delete the temporary file
|
||||
|
@ -1297,10 +1101,12 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, u32
|
|||
|
||||
// return pointer
|
||||
return pStream;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
std::FILE* pFile = std::fopen(fileName, modeString);
|
||||
// forward through
|
||||
std::FILE* pFile = FileSystem::OpenCFile(fileName, modeString);
|
||||
if (!pFile)
|
||||
return nullptr;
|
||||
|
||||
|
@ -1308,36 +1114,34 @@ std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* fileName, u32
|
|||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
std::unique_ptr<MemoryByteStream> ByteStream_CreateMemoryStream(void* pMemory, u32 Size)
|
||||
std::unique_ptr<MemoryByteStream> ByteStream::CreateMemoryStream(void* pMemory, u32 Size)
|
||||
{
|
||||
DebugAssert(pMemory != nullptr && Size > 0);
|
||||
return std::make_unique<MemoryByteStream>(pMemory, Size);
|
||||
}
|
||||
|
||||
std::unique_ptr<ReadOnlyMemoryByteStream> ByteStream_CreateReadOnlyMemoryStream(const void* pMemory, u32 Size)
|
||||
std::unique_ptr<ReadOnlyMemoryByteStream> ByteStream::CreateReadOnlyMemoryStream(const void* pMemory, u32 Size)
|
||||
{
|
||||
DebugAssert(pMemory != nullptr && Size > 0);
|
||||
return std::make_unique<ReadOnlyMemoryByteStream>(pMemory, Size);
|
||||
}
|
||||
|
||||
std::unique_ptr<NullByteStream> ByteStream_CreateNullStream()
|
||||
std::unique_ptr<NullByteStream> ByteStream::CreateNullStream()
|
||||
{
|
||||
return std::make_unique<NullByteStream>();
|
||||
}
|
||||
|
||||
std::unique_ptr<GrowableMemoryByteStream> ByteStream_CreateGrowableMemoryStream(void* pInitialMemory, u32 InitialSize)
|
||||
std::unique_ptr<GrowableMemoryByteStream> ByteStream::CreateGrowableMemoryStream(void* pInitialMemory, u32 InitialSize)
|
||||
{
|
||||
return std::make_unique<GrowableMemoryByteStream>(pInitialMemory, InitialSize);
|
||||
}
|
||||
|
||||
std::unique_ptr<GrowableMemoryByteStream> ByteStream_CreateGrowableMemoryStream()
|
||||
std::unique_ptr<GrowableMemoryByteStream> ByteStream::CreateGrowableMemoryStream()
|
||||
{
|
||||
return std::make_unique<GrowableMemoryByteStream>(nullptr, 0);
|
||||
}
|
||||
|
||||
bool ByteStream_CopyStream(ByteStream* pDestinationStream, ByteStream* pSourceStream)
|
||||
bool ByteStream::CopyStream(ByteStream* pDestinationStream, ByteStream* pSourceStream)
|
||||
{
|
||||
const u32 chunkSize = 4096;
|
||||
u8 chunkData[chunkSize];
|
||||
|
@ -1363,7 +1167,7 @@ bool ByteStream_CopyStream(ByteStream* pDestinationStream, ByteStream* pSourceSt
|
|||
return (pSourceStream->SeekAbsolute(oldSourcePosition) && success);
|
||||
}
|
||||
|
||||
bool ByteStream_AppendStream(ByteStream* pSourceStream, ByteStream* pDestinationStream)
|
||||
bool ByteStream::AppendStream(ByteStream* pSourceStream, ByteStream* pDestinationStream)
|
||||
{
|
||||
const u32 chunkSize = 4096;
|
||||
u8 chunkData[chunkSize];
|
||||
|
@ -1389,7 +1193,7 @@ bool ByteStream_AppendStream(ByteStream* pSourceStream, ByteStream* pDestination
|
|||
return (pSourceStream->SeekAbsolute(oldSourcePosition) && success);
|
||||
}
|
||||
|
||||
u32 ByteStream_CopyBytes(ByteStream* pSourceStream, u32 byteCount, ByteStream* pDestinationStream)
|
||||
u32 ByteStream::CopyBytes(ByteStream* pSourceStream, u32 byteCount, ByteStream* pDestinationStream)
|
||||
{
|
||||
const u32 chunkSize = 4096;
|
||||
u8 chunkData[chunkSize];
|
||||
|
@ -1411,3 +1215,69 @@ u32 ByteStream_CopyBytes(ByteStream* pSourceStream, u32 byteCount, ByteStream* p
|
|||
|
||||
return byteCount - remaining;
|
||||
}
|
||||
|
||||
std::string ByteStream::ReadStreamToString(ByteStream* stream, bool seek_to_start /* = true */)
|
||||
{
|
||||
u64 pos = stream->GetPosition();
|
||||
u64 size = stream->GetSize();
|
||||
if (pos > 0 && seek_to_start)
|
||||
{
|
||||
if (!stream->SeekAbsolute(0))
|
||||
return {};
|
||||
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
Assert(size >= pos);
|
||||
size -= pos;
|
||||
if (size == 0 || size > std::numeric_limits<u32>::max())
|
||||
return {};
|
||||
|
||||
std::string ret;
|
||||
ret.resize(static_cast<size_t>(size));
|
||||
if (!stream->Read2(ret.data(), static_cast<u32>(size)))
|
||||
return {};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ByteStream::WriteStreamToString(const std::string_view& sv, ByteStream* stream)
|
||||
{
|
||||
if (sv.size() > std::numeric_limits<u32>::max())
|
||||
return false;
|
||||
|
||||
return stream->Write2(sv.data(), static_cast<u32>(sv.size()));
|
||||
}
|
||||
|
||||
std::vector<u8> ByteStream::ReadBinaryStream(ByteStream* stream, bool seek_to_start /*= true*/)
|
||||
{
|
||||
u64 pos = stream->GetPosition();
|
||||
u64 size = stream->GetSize();
|
||||
if (pos > 0 && seek_to_start)
|
||||
{
|
||||
if (!stream->SeekAbsolute(0))
|
||||
return {};
|
||||
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
Assert(size >= pos);
|
||||
size -= pos;
|
||||
if (size == 0 || size > std::numeric_limits<u32>::max())
|
||||
return {};
|
||||
|
||||
std::vector<u8> ret;
|
||||
ret.resize(static_cast<size_t>(size));
|
||||
if (!stream->Read2(ret.data(), static_cast<u32>(size)))
|
||||
return {};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ByteStream::WriteBinaryToStream(ByteStream* stream, const void* data, size_t data_length)
|
||||
{
|
||||
if (data_length > std::numeric_limits<u32>::max())
|
||||
return false;
|
||||
|
||||
return stream->Write2(data, static_cast<u32>(data_length));
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#pragma once
|
||||
#include "types.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// base byte stream creation functions
|
||||
enum BYTESTREAM_OPEN_MODE
|
||||
|
@ -10,12 +12,18 @@ enum BYTESTREAM_OPEN_MODE
|
|||
BYTESTREAM_OPEN_APPEND = 4, // seek to the end
|
||||
BYTESTREAM_OPEN_TRUNCATE = 8, // truncate the file, seek to start
|
||||
BYTESTREAM_OPEN_CREATE = 16, // if the file does not exist, create it
|
||||
BYTESTREAM_OPEN_CREATE_PATH = 32, // if the file parent directories don't exist, create them
|
||||
BYTESTREAM_OPEN_ATOMIC_UPDATE = 64, //
|
||||
BYTESTREAM_OPEN_SEEKABLE = 128,
|
||||
BYTESTREAM_OPEN_STREAMED = 256,
|
||||
};
|
||||
|
||||
// forward declarations for implemented classes
|
||||
class ByteStream;
|
||||
class MemoryByteStream;
|
||||
class GrowableMemoryByteStream;
|
||||
class ReadOnlyMemoryByteStream;
|
||||
class NullByteStream;
|
||||
|
||||
// interface class used by readers, writers, etc.
|
||||
class ByteStream
|
||||
{
|
||||
|
@ -68,6 +76,42 @@ public:
|
|||
inline void SetErrorState() { m_errorState = true; }
|
||||
inline void ClearErrorState() { m_errorState = false; }
|
||||
|
||||
// base byte stream creation functions
|
||||
// opens a local file-based stream. fills in error if passed, and returns false if the file cannot be opened.
|
||||
static std::unique_ptr<ByteStream> OpenFile(const char* FileName, u32 OpenMode);
|
||||
|
||||
// memory byte stream, caller is responsible for management, therefore it can be located on either the stack or on the
|
||||
// heap.
|
||||
static std::unique_ptr<MemoryByteStream> CreateMemoryStream(void* pMemory, u32 Size);
|
||||
|
||||
// a growable memory byte stream will automatically allocate its own memory if the provided memory is overflowed.
|
||||
// a "pure heap" buffer, i.e. a buffer completely managed by this implementation, can be created by supplying a NULL
|
||||
// pointer and initialSize of zero.
|
||||
static std::unique_ptr<GrowableMemoryByteStream> CreateGrowableMemoryStream(void* pInitialMemory, u32 InitialSize);
|
||||
static std::unique_ptr<GrowableMemoryByteStream> CreateGrowableMemoryStream();
|
||||
|
||||
// readable memory stream
|
||||
static std::unique_ptr<ReadOnlyMemoryByteStream> CreateReadOnlyMemoryStream(const void* pMemory, u32 Size);
|
||||
|
||||
// null memory stream
|
||||
static std::unique_ptr<NullByteStream> CreateNullStream();
|
||||
|
||||
// copies one stream's contents to another. rewinds source streams automatically, and returns it back to its old
|
||||
// position.
|
||||
static bool CopyStream(ByteStream* pDestinationStream, ByteStream* pSourceStream);
|
||||
|
||||
// appends one stream's contents to another.
|
||||
static bool AppendStream(ByteStream* pSourceStream, ByteStream* pDestinationStream);
|
||||
|
||||
// copies a number of bytes from one to another
|
||||
static u32 CopyBytes(ByteStream* pSourceStream, u32 byteCount, ByteStream* pDestinationStream);
|
||||
|
||||
static std::string ReadStreamToString(ByteStream* stream, bool seek_to_start = true);
|
||||
static bool WriteStreamToString(const std::string_view& sv, ByteStream* stream);
|
||||
|
||||
static std::vector<u8> ReadBinaryStream(ByteStream* stream, bool seek_to_start = true);
|
||||
static bool WriteBinaryToStream(ByteStream* stream, const void* data, size_t data_length);
|
||||
|
||||
protected:
|
||||
ByteStream() : m_errorState(false) {}
|
||||
|
||||
|
@ -199,33 +243,3 @@ private:
|
|||
u32 m_iSize;
|
||||
u32 m_iMemorySize;
|
||||
};
|
||||
|
||||
// base byte stream creation functions
|
||||
// opens a local file-based stream. fills in error if passed, and returns false if the file cannot be opened.
|
||||
std::unique_ptr<ByteStream> ByteStream_OpenFileStream(const char* FileName, u32 OpenMode);
|
||||
|
||||
// memory byte stream, caller is responsible for management, therefore it can be located on either the stack or on the
|
||||
// heap.
|
||||
std::unique_ptr<MemoryByteStream> ByteStream_CreateMemoryStream(void* pMemory, u32 Size);
|
||||
|
||||
// a growable memory byte stream will automatically allocate its own memory if the provided memory is overflowed.
|
||||
// a "pure heap" buffer, i.e. a buffer completely managed by this implementation, can be created by supplying a NULL
|
||||
// pointer and initialSize of zero.
|
||||
std::unique_ptr<GrowableMemoryByteStream> ByteStream_CreateGrowableMemoryStream(void* pInitialMemory, u32 InitialSize);
|
||||
std::unique_ptr<GrowableMemoryByteStream> ByteStream_CreateGrowableMemoryStream();
|
||||
|
||||
// readable memory stream
|
||||
std::unique_ptr<ReadOnlyMemoryByteStream> ByteStream_CreateReadOnlyMemoryStream(const void* pMemory, u32 Size);
|
||||
|
||||
// null memory stream
|
||||
std::unique_ptr<NullByteStream> ByteStream_CreateNullStream();
|
||||
|
||||
// copies one stream's contents to another. rewinds source streams automatically, and returns it back to its old
|
||||
// position.
|
||||
bool ByteStream_CopyStream(ByteStream* pDestinationStream, ByteStream* pSourceStream);
|
||||
|
||||
// appends one stream's contents to another.
|
||||
bool ByteStream_AppendStream(ByteStream* pSourceStream, ByteStream* pDestinationStream);
|
||||
|
||||
// copies a number of bytes from one to another
|
||||
u32 ByteStream_CopyBytes(ByteStream* pSourceStream, u32 byteCount, ByteStream* pDestinationStream);
|
||||
|
|
|
@ -844,18 +844,6 @@ std::string BuildRelativePath(const std::string_view& filename, const std::strin
|
|||
return new_string;
|
||||
}
|
||||
|
||||
std::unique_ptr<ByteStream> OpenFile(const char* FileName, u32 Flags)
|
||||
{
|
||||
// has a path
|
||||
if (FileName[0] == '\0')
|
||||
return nullptr;
|
||||
|
||||
// TODO: Handle Android content URIs here.
|
||||
|
||||
// forward to local file wrapper
|
||||
return ByteStream_OpenFileStream(FileName, Flags);
|
||||
}
|
||||
|
||||
FileSystem::ManagedCFilePtr OpenManagedCFile(const char* filename, const char* mode)
|
||||
{
|
||||
return ManagedCFilePtr(OpenCFile(filename, mode), [](std::FILE* fp) { std::fclose(fp); });
|
||||
|
@ -1116,72 +1104,6 @@ bool WriteFileToString(const char* filename, const std::string_view& sv)
|
|||
return true;
|
||||
}
|
||||
|
||||
std::string ReadStreamToString(ByteStream* stream, bool seek_to_start /* = true */)
|
||||
{
|
||||
u64 pos = stream->GetPosition();
|
||||
u64 size = stream->GetSize();
|
||||
if (pos > 0 && seek_to_start)
|
||||
{
|
||||
if (!stream->SeekAbsolute(0))
|
||||
return {};
|
||||
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
Assert(size >= pos);
|
||||
size -= pos;
|
||||
if (size == 0 || size > std::numeric_limits<u32>::max())
|
||||
return {};
|
||||
|
||||
std::string ret;
|
||||
ret.resize(static_cast<size_t>(size));
|
||||
if (!stream->Read2(ret.data(), static_cast<u32>(size)))
|
||||
return {};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool WriteStreamToString(const std::string_view& sv, ByteStream* stream)
|
||||
{
|
||||
if (sv.size() > std::numeric_limits<u32>::max())
|
||||
return false;
|
||||
|
||||
return stream->Write2(sv.data(), static_cast<u32>(sv.size()));
|
||||
}
|
||||
|
||||
std::vector<u8> ReadBinaryStream(ByteStream* stream, bool seek_to_start /*= true*/)
|
||||
{
|
||||
u64 pos = stream->GetPosition();
|
||||
u64 size = stream->GetSize();
|
||||
if (pos > 0 && seek_to_start)
|
||||
{
|
||||
if (!stream->SeekAbsolute(0))
|
||||
return {};
|
||||
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
Assert(size >= pos);
|
||||
size -= pos;
|
||||
if (size == 0 || size > std::numeric_limits<u32>::max())
|
||||
return {};
|
||||
|
||||
std::vector<u8> ret;
|
||||
ret.resize(static_cast<size_t>(size));
|
||||
if (!stream->Read2(ret.data(), static_cast<u32>(size)))
|
||||
return {};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool WriteBinaryToSTream(ByteStream* stream, const void* data, size_t data_length)
|
||||
{
|
||||
if (data_length > std::numeric_limits<u32>::max())
|
||||
return false;
|
||||
|
||||
return stream->Write2(data, static_cast<u32>(data_length));
|
||||
}
|
||||
|
||||
void BuildOSPath(char* Destination, u32 cbDestination, const char* Path)
|
||||
{
|
||||
u32 i;
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
#include "timestamp.h"
|
||||
#include "types.h"
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class ByteStream;
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define FS_OSPATH_SEPARATOR_CHARACTER '\\'
|
||||
|
@ -185,9 +185,6 @@ bool DeleteFile(const char* Path);
|
|||
// rename file
|
||||
bool RenamePath(const char* OldPath, const char* NewPath);
|
||||
|
||||
// open files
|
||||
std::unique_ptr<ByteStream> OpenFile(const char* FileName, u32 Flags);
|
||||
|
||||
using ManagedCFilePtr = std::unique_ptr<std::FILE, void (*)(std::FILE*)>;
|
||||
ManagedCFilePtr OpenManagedCFile(const char* filename, const char* mode);
|
||||
std::FILE* OpenCFile(const char* filename, const char* mode);
|
||||
|
@ -201,12 +198,6 @@ std::optional<std::string> ReadFileToString(std::FILE* fp);
|
|||
bool WriteBinaryFile(const char* filename, const void* data, size_t data_length);
|
||||
bool WriteFileToString(const char* filename, const std::string_view& sv);
|
||||
|
||||
std::string ReadStreamToString(ByteStream* stream, bool seek_to_start = true);
|
||||
bool WriteStreamToString(const std::string_view& sv, ByteStream* stream);
|
||||
|
||||
std::vector<u8> ReadBinaryStream(ByteStream* stream, bool seek_to_start = true);
|
||||
bool WriteBinaryToSTream(ByteStream* stream, const void* data, size_t data_length);
|
||||
|
||||
// creates a directory in the local filesystem
|
||||
// if the directory already exists, the return value will be true.
|
||||
// if Recursive is specified, all parent directories will be created
|
||||
|
|
|
@ -691,7 +691,7 @@ bool CheatList::LoadFromPackage(const std::string& game_code)
|
|||
if (!stream)
|
||||
return false;
|
||||
|
||||
std::string db_string = FileSystem::ReadStreamToString(stream.get());
|
||||
std::string db_string = ByteStream::ReadStreamToString(stream.get());
|
||||
stream.reset();
|
||||
if (db_string.empty())
|
||||
return false;
|
||||
|
|
|
@ -409,7 +409,7 @@ bool HostInterface::HasAnyBIOSImages()
|
|||
|
||||
bool HostInterface::LoadState(const char* filename)
|
||||
{
|
||||
std::unique_ptr<ByteStream> stream = FileSystem::OpenFile(filename, BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
std::unique_ptr<ByteStream> stream = ByteStream::OpenFile(filename, BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
if (!stream)
|
||||
return false;
|
||||
|
||||
|
@ -441,7 +441,7 @@ bool HostInterface::LoadState(const char* filename)
|
|||
bool HostInterface::SaveState(const char* filename)
|
||||
{
|
||||
std::unique_ptr<ByteStream> stream =
|
||||
FileSystem::OpenFile(filename, BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE | BYTESTREAM_OPEN_TRUNCATE |
|
||||
ByteStream::OpenFile(filename, BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE | BYTESTREAM_OPEN_TRUNCATE |
|
||||
BYTESTREAM_OPEN_ATOMIC_UPDATE | BYTESTREAM_OPEN_STREAMED);
|
||||
if (!stream)
|
||||
return false;
|
||||
|
@ -1140,7 +1140,7 @@ void HostInterface::RecreateSystem()
|
|||
{
|
||||
Assert(!System::IsShutdown());
|
||||
|
||||
std::unique_ptr<ByteStream> stream = ByteStream_CreateGrowableMemoryStream(nullptr, 8 * 1024);
|
||||
std::unique_ptr<ByteStream> stream = ByteStream::CreateGrowableMemoryStream(nullptr, 8 * 1024);
|
||||
if (!System::SaveState(stream.get(), 0) || !stream->SeekAbsolute(0))
|
||||
{
|
||||
ReportError("Failed to save state before system recreation. Shutting down.");
|
||||
|
|
|
@ -97,7 +97,7 @@ bool LoadFromFile(DataArray* data, const char* filename)
|
|||
if (!FileSystem::StatFile(filename, &sd) || sd.Size != DATA_SIZE)
|
||||
return false;
|
||||
|
||||
std::unique_ptr<ByteStream> stream = FileSystem::OpenFile(filename, BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
std::unique_ptr<ByteStream> stream = ByteStream::OpenFile(filename, BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
if (!stream || stream->GetSize() != DATA_SIZE)
|
||||
return false;
|
||||
|
||||
|
@ -115,7 +115,7 @@ bool LoadFromFile(DataArray* data, const char* filename)
|
|||
bool SaveToFile(const DataArray& data, const char* filename)
|
||||
{
|
||||
std::unique_ptr<ByteStream> stream =
|
||||
FileSystem::OpenFile(filename, BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_TRUNCATE | BYTESTREAM_OPEN_WRITE |
|
||||
ByteStream::OpenFile(filename, BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_TRUNCATE | BYTESTREAM_OPEN_WRITE |
|
||||
BYTESTREAM_OPEN_ATOMIC_UPDATE | BYTESTREAM_OPEN_STREAMED);
|
||||
if (!stream)
|
||||
{
|
||||
|
@ -560,7 +560,7 @@ bool ImportCard(DataArray* data, const char* filename)
|
|||
bool ExportSave(DataArray* data, const FileInfo& fi, const char* filename)
|
||||
{
|
||||
std::unique_ptr<ByteStream> stream =
|
||||
FileSystem::OpenFile(filename, BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_TRUNCATE | BYTESTREAM_OPEN_WRITE |
|
||||
ByteStream::OpenFile(filename, BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_TRUNCATE | BYTESTREAM_OPEN_WRITE |
|
||||
BYTESTREAM_OPEN_ATOMIC_UPDATE | BYTESTREAM_OPEN_STREAMED);
|
||||
if (!stream)
|
||||
{
|
||||
|
@ -599,7 +599,7 @@ static bool ImportSaveWithDirectoryFrame(DataArray* data, const char* filename,
|
|||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<ByteStream> stream = FileSystem::OpenFile(filename, BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
std::unique_ptr<ByteStream> stream = ByteStream::OpenFile(filename, BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
if (!stream)
|
||||
{
|
||||
Log_ErrorPrintf("Failed to open '%s' for reading", filename);
|
||||
|
|
|
@ -634,7 +634,7 @@ bool RecreateGPU(GPURenderer renderer, bool update_display /* = true*/)
|
|||
g_gpu->RestoreGraphicsAPIState();
|
||||
|
||||
// save current state
|
||||
std::unique_ptr<ByteStream> state_stream = ByteStream_CreateGrowableMemoryStream();
|
||||
std::unique_ptr<ByteStream> state_stream = ByteStream::CreateGrowableMemoryStream();
|
||||
StateWrapper sw(state_stream.get(), StateWrapper::Mode::Write, SAVE_STATE_VERSION);
|
||||
const bool state_valid = g_gpu->DoState(sw, nullptr, false) && TimingEvents::DoState(sw);
|
||||
if (!state_valid)
|
||||
|
|
|
@ -483,7 +483,7 @@ bool CommonHostInterface::ParseCommandLineParameters(int argc, char* argv[],
|
|||
if (!state_filename.empty())
|
||||
{
|
||||
std::unique_ptr<ByteStream> state_stream =
|
||||
FileSystem::OpenFile(state_filename.c_str(), BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
ByteStream::OpenFile(state_filename.c_str(), BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
if (!state_stream)
|
||||
{
|
||||
Log_ErrorPrintf("Failed to open save state file '%s'", state_filename.c_str());
|
||||
|
@ -565,7 +565,7 @@ bool CommonHostInterface::CreateHostDisplayResources()
|
|||
std::unique_ptr<ByteStream> stream = OpenPackageFile("resources" FS_OSPATH_SEPARATOR_STR "roboto-regular.ttf",
|
||||
BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
std::vector<u8> font_data;
|
||||
if (!stream || (font_data = FileSystem::ReadBinaryStream(stream.get()), font_data.empty()))
|
||||
if (!stream || (font_data = ByteStream::ReadBinaryStream(stream.get()), font_data.empty()))
|
||||
{
|
||||
ReportError("Failed to load text font");
|
||||
m_display->DestroyImGuiContext();
|
||||
|
@ -580,7 +580,7 @@ bool CommonHostInterface::CreateHostDisplayResources()
|
|||
std::unique_ptr<ByteStream> stream = OpenPackageFile("resources" FS_OSPATH_SEPARATOR_STR "fa-solid-900.ttf",
|
||||
BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
std::vector<u8> font_data;
|
||||
if (!stream || (font_data = FileSystem::ReadBinaryStream(stream.get()), font_data.empty()))
|
||||
if (!stream || (font_data = ByteStream::ReadBinaryStream(stream.get()), font_data.empty()))
|
||||
{
|
||||
ReportError("Failed to load icon font");
|
||||
m_display->DestroyImGuiContext();
|
||||
|
@ -779,7 +779,7 @@ bool CommonHostInterface::SaveUndoLoadState()
|
|||
if (m_undo_load_state)
|
||||
m_undo_load_state.reset();
|
||||
|
||||
m_undo_load_state = ByteStream_CreateGrowableMemoryStream(nullptr, System::MAX_SAVE_STATE_SIZE);
|
||||
m_undo_load_state = ByteStream::CreateGrowableMemoryStream(nullptr, System::MAX_SAVE_STATE_SIZE);
|
||||
if (!System::SaveState(m_undo_load_state.get()))
|
||||
{
|
||||
AddOSDMessage(TranslateStdString("OSDMessage", "Failed to save undo load state."), 15.0f);
|
||||
|
@ -3183,7 +3183,7 @@ CommonHostInterface::GetExtendedSaveStateInfo(const char* game_code, s32 slot)
|
|||
return std::nullopt;
|
||||
|
||||
std::unique_ptr<ByteStream> stream =
|
||||
FileSystem::OpenFile(path.c_str(), BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_SEEKABLE);
|
||||
ByteStream::OpenFile(path.c_str(), BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_SEEKABLE);
|
||||
if (!stream)
|
||||
return std::nullopt;
|
||||
|
||||
|
@ -4249,7 +4249,7 @@ std::unique_ptr<ByteStream> CommonHostInterface::OpenPackageFile(const char* pat
|
|||
StringUtil::StdStringFromFormat("%s" FS_OSPATH_SEPARATOR_STR "%s", m_program_directory.c_str(), path));
|
||||
const u32 real_flags = (flags & allowed_flags) | BYTESTREAM_OPEN_READ;
|
||||
Log_DevPrintf("Requesting package file '%s'", path);
|
||||
return FileSystem::OpenFile(full_path.c_str(), real_flags);
|
||||
return ByteStream::OpenFile(full_path.c_str(), real_flags);
|
||||
}
|
||||
|
||||
bool CommonHostInterface::IsControllerNavigationActive() const
|
||||
|
|
|
@ -482,7 +482,7 @@ static std::unique_ptr<HostDisplayTexture> LoadTexture(const char* path, bool fr
|
|||
if (from_package)
|
||||
stream = g_host_interface->OpenPackageFile(path, BYTESTREAM_OPEN_READ);
|
||||
else
|
||||
stream = FileSystem::OpenFile(path, BYTESTREAM_OPEN_READ);
|
||||
stream = ByteStream::OpenFile(path, BYTESTREAM_OPEN_READ);
|
||||
if (!stream)
|
||||
{
|
||||
Log_ErrorPrintf("Failed to open texture resource '%s'", path);
|
||||
|
|
|
@ -31,7 +31,7 @@ bool GameDatabase::Load()
|
|||
return false;
|
||||
}
|
||||
|
||||
std::string gamedb_data(FileSystem::ReadStreamToString(stream.get(), false));
|
||||
std::string gamedb_data(ByteStream::ReadStreamToString(stream.get(), false));
|
||||
if (gamedb_data.empty())
|
||||
{
|
||||
Log_ErrorPrintf("Failed to read game database");
|
||||
|
|
|
@ -249,7 +249,7 @@ void GameList::LoadCache()
|
|||
return;
|
||||
|
||||
std::unique_ptr<ByteStream> stream =
|
||||
FileSystem::OpenFile(m_cache_filename.c_str(), BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
ByteStream::OpenFile(m_cache_filename.c_str(), BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
if (!stream)
|
||||
return;
|
||||
|
||||
|
@ -373,7 +373,7 @@ bool GameList::OpenCacheForWriting()
|
|||
|
||||
Assert(!m_cache_write_stream);
|
||||
m_cache_write_stream =
|
||||
FileSystem::OpenFile(m_cache_filename.c_str(), BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE |
|
||||
ByteStream::OpenFile(m_cache_filename.c_str(), BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE |
|
||||
BYTESTREAM_OPEN_APPEND | BYTESTREAM_OPEN_STREAMED);
|
||||
if (!m_cache_write_stream || !m_cache_write_stream->SeekToEnd())
|
||||
{
|
||||
|
@ -793,7 +793,7 @@ void GameList::LoadCompatibilityList()
|
|||
g_host_interface->OpenPackageFile("database/compatibility.xml", BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
if (file)
|
||||
{
|
||||
LoadCompatibilityListFromXML(FileSystem::ReadStreamToString(file.get()));
|
||||
LoadCompatibilityListFromXML(ByteStream::ReadStreamToString(file.get()));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -805,9 +805,9 @@ void GameList::LoadCompatibilityList()
|
|||
if (!m_user_compatibility_list_filename.empty() && FileSystem::FileExists(m_user_compatibility_list_filename.c_str()))
|
||||
{
|
||||
std::unique_ptr<ByteStream> file =
|
||||
FileSystem::OpenFile(m_user_compatibility_list_filename.c_str(), BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
ByteStream::OpenFile(m_user_compatibility_list_filename.c_str(), BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
if (file)
|
||||
LoadCompatibilityListFromXML(FileSystem::ReadStreamToString(file.get()));
|
||||
LoadCompatibilityListFromXML(ByteStream::ReadStreamToString(file.get()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1011,7 +1011,7 @@ void GameList::LoadGameSettings()
|
|||
g_host_interface->OpenPackageFile("database/gamesettings.ini", BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
if (file)
|
||||
{
|
||||
m_game_settings.Load(FileSystem::ReadStreamToString(file.get()));
|
||||
m_game_settings.Load(ByteStream::ReadStreamToString(file.get()));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1023,9 +1023,9 @@ void GameList::LoadGameSettings()
|
|||
if (!m_user_game_settings_filename.empty() && FileSystem::FileExists(m_user_game_settings_filename.c_str()))
|
||||
{
|
||||
std::unique_ptr<ByteStream> file =
|
||||
FileSystem::OpenFile(m_user_game_settings_filename.c_str(), BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
ByteStream::OpenFile(m_user_game_settings_filename.c_str(), BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
||||
if (file)
|
||||
m_game_settings.Load(FileSystem::ReadStreamToString(file.get()));
|
||||
m_game_settings.Load(ByteStream::ReadStreamToString(file.get()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue