Common/FileSystem: Add helpers for ByteStream

This commit is contained in:
Connor McLaughlin 2020-11-27 17:57:43 +10:00
parent 5f69216f70
commit 4a482875ca
2 changed files with 40 additions and 4 deletions

View file

@ -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<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()));
}
void BuildOSPath(char* Destination, u32 cbDestination, const char* Path)
{
u32 i;

View file

@ -175,6 +175,9 @@ std::optional<std::string> 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