From ae222bf4fa131b8b86dd0662b32214eb161ace1a Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 2 Dec 2020 11:29:38 -0500 Subject: 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. --- source/core/slang-io.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'source/core/slang-io.cpp') 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)); -- cgit v1.2.3