summaryrefslogtreecommitdiff
path: root/source/core/slang-io.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/core/slang-io.cpp')
-rw-r--r--source/core/slang-io.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/source/core/slang-io.cpp b/source/core/slang-io.cpp
index 8e7266dc5..2e2ec3d8b 100644
--- a/source/core/slang-io.cpp
+++ b/source/core/slang-io.cpp
@@ -832,6 +832,68 @@ namespace Slang
return _Move(buffer);
}
+ SlangResult File::readAllBytes(const String& path, ScopedAllocation& out)
+ {
+ try
+ {
+ FileStream stream(path, FileMode::Open, FileAccess::Read, FileShare::ReadWrite);
+
+ const Int64 start = stream.getPosition();
+ stream.seek(SeekOrigin::End, 0);
+ const Int64 end = stream.getPosition();
+ stream.seek(SeekOrigin::Start, start);
+
+ const Int64 positionSizeInBytes = end - start;
+
+ if (UInt64(positionSizeInBytes) > UInt64(~size_t(0)))
+ {
+ // It's too large to fit in memory.
+ return SLANG_FAIL;
+ }
+
+ const size_t sizeInBytes = size_t(positionSizeInBytes);
+ void* data = out.allocate(sizeInBytes);
+ if (!data)
+ {
+ return SLANG_E_OUT_OF_MEMORY;
+ }
+
+ const size_t readSizeInBytes = stream.read(data, sizeInBytes);
+
+ // If not all read just return an error
+ if (sizeInBytes != readSizeInBytes)
+ {
+ return SLANG_FAIL;
+ }
+ }
+ catch (const IOException&)
+ {
+ return SLANG_FAIL;
+ }
+ return SLANG_OK;
+ }
+
+ SlangResult File::writeAllBytes(const String& path, const void* data, size_t size)
+ {
+ try
+ {
+ FileStream stream(path, FileMode::Create, FileAccess::Write, FileShare::ReadWrite);
+
+ const size_t writeSizeInBytes = stream.write(data, size);
+
+ // If not all written just return an error
+ if (size != writeSizeInBytes)
+ {
+ return SLANG_FAIL;
+ }
+ }
+ catch (const IOException&)
+ {
+ return SLANG_FAIL;
+ }
+ return SLANG_OK;
+ }
+
void File::writeAllText(const Slang::String& fileName, const Slang::String& text)
{
StreamWriter writer(new FileStream(fileName, FileMode::Create));