diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2020-12-02 11:29:38 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-02 08:29:38 -0800 |
| commit | ae222bf4fa131b8b86dd0662b32214eb161ace1a (patch) | |
| tree | de18f3d8b6600b5046d34d9743cb8dbf934927a0 /source/core/slang-io.cpp | |
| parent | e631a2599babac42d3032adc0c6d17fa7d342e80 (diff) | |
Zip FileSystem support (#1617)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Add miniz
* Fix for separator in CacheFileSystem.
Add compression unit test for zip.
* Put zip compression into core.
* Remove delimiter stripping if simplifying a path - as stripping will fix delimiters.
* ZipFileSystem WIP.
* More ZipFileSystem working.
* Added isEmpty.
Fixed small bug is contains.
* First pass support for mutability on zip.
* Improvements to File::read/writeAllBytes
* Can access and save archive - but has memory leaks.
* Fix memory leak.
* Some ZIP compression tests.
* Fix memory leak on ScopedAllocation.
Fix off by one bug on UIntSet
* Bug fix in UIntSet
* Fix remaining ZipFileSystem issues.
Adde stand alone unit-test.
* Turn tabs to spaces in slang-io.h
* Renamed mode ReadWrite (instead of just Write)
* Make miniz it's own project.
* Fix windows warning on win32.
* Remove warnings needed when miniz was included as a header library.
* Set the C++ standard via 'flags' in premake.
* Add support for 'implicit' paths.
* Add testing for implicit directories.
Better handling of implicit directories.
* Improve comments in ZipFileSystem.
* Update comment around reader/writer transformation.
Diffstat (limited to 'source/core/slang-io.cpp')
| -rw-r--r-- | source/core/slang-io.cpp | 62 |
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)); |
