diff --git a/src/common/file_system.cpp b/src/common/file_system.cpp index addb88acd..e9c90a92a 100644 --- a/src/common/file_system.cpp +++ b/src/common/file_system.cpp @@ -496,6 +496,39 @@ 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::max()) + return {}; + + std::string ret; + ret.resize(static_cast(size)); + if (!stream->Read2(ret.data(), static_cast(size))) + return {}; + + return ret; +} + +bool WriteStreamToString(const std::string_view& sv, ByteStream* stream) +{ + if (sv.size() > std::numeric_limits::max()) + return false; + + return stream->Write2(sv.data(), static_cast(sv.size())); +} + void BuildOSPath(char* Destination, u32 cbDestination, const char* Path) { u32 i; @@ -1250,7 +1283,7 @@ static u32 RecursiveFindFiles(const char* OriginPath, const char* ParentPath, co FILESYSTEM_FIND_DATA outData; outData.Attributes = 0; -#if defined (__HAIKU__) || defined (__APPLE__) +#if defined(__HAIKU__) || defined(__APPLE__) struct stat sDir; if (stat(full_path, &sDir) < 0) continue; @@ -1347,7 +1380,7 @@ bool StatFile(const char* Path, FILESYSTEM_STAT_DATA* pStatData) return false; // stat file -#if defined (__HAIKU__) || defined (__APPLE__) +#if defined(__HAIKU__) || defined(__APPLE__) struct stat sysStatData; if (stat(Path, &sysStatData) < 0) #else @@ -1381,7 +1414,7 @@ bool FileExists(const char* Path) return false; // stat file -#if defined (__HAIKU__) || defined (__APPLE__) +#if defined(__HAIKU__) || defined(__APPLE__) struct stat sysStatData; if (stat(Path, &sysStatData) < 0) #else @@ -1403,7 +1436,7 @@ bool DirectoryExists(const char* Path) return false; // stat file -#if defined (__HAIKU__) || defined (__APPLE__) +#if defined(__HAIKU__) || defined(__APPLE__) struct stat sysStatData; if (stat(Path, &sysStatData) < 0) #else diff --git a/src/common/file_system.h b/src/common/file_system.h index 80ac04931..e7a804bd5 100644 --- a/src/common/file_system.h +++ b/src/common/file_system.h @@ -175,6 +175,9 @@ std::optional ReadFileToString(const char* filename); 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); + // 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