From 97bb82ebcdf8f1391b9d93b5a8d7b1dfc4e88e52 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Mon, 4 Oct 2021 14:15:51 -0400 Subject: Removing exceptions from core/compiler-core (#1953) * #include an absolute path didn't work - because paths were taken to always be relative. * Refactor Stream. Working on all tests. * Split out CharEncode. * Make method names lower camel. m_prefix in Writer/Reader * Tidy up around CharEncode interface. * Small improvements around encode/decode. * Better use of types. * Remove readLine from TextReader. * Remove exceptions from Stream/Text handling. * Fix some typos. * Fix tabbing. * Fix missing override. * Remove remaining exception throw/catch via using signal mechanism. * Remove exceptions that are not used anymore. * Document the Stream interface. * Remove index for decoding 'get byte' function. * Fix CharReader -> ByteReader. --- source/core/slang-io.cpp | 130 ++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 63 deletions(-) (limited to 'source/core/slang-io.cpp') diff --git a/source/core/slang-io.cpp b/source/core/slang-io.cpp index 0755a6a2f..162e49980 100644 --- a/source/core/slang-io.cpp +++ b/source/core/slang-io.cpp @@ -825,94 +825,98 @@ namespace Slang return _getExecutablePath(); } - Slang::String File::readAllText(const Slang::String& fileName) + SlangResult File::readAllText(const Slang::String& fileName, String& outText) { - StreamReader reader(new FileStream(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite)); - return reader.ReadToEnd(); + RefPtr stream(new FileStream); + SLANG_RETURN_ON_FAIL(stream->init(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite)); + + StreamReader reader; + SLANG_RETURN_ON_FAIL(reader.init(stream)); + SLANG_RETURN_ON_FAIL(reader.readToEnd(outText)); + + return SLANG_OK; } - Slang::List File::readAllBytes(const Slang::String& fileName) + SlangResult File::readAllBytes(const Slang::String& path, Slang::List& out) { - RefPtr fs = new FileStream(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite); - List buffer; - while (!fs->isEnd()) + FileStream stream; + SLANG_RETURN_ON_FAIL(stream.init(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(kMaxIndex)) { - unsigned char ch; - int read = (int)fs->read(&ch, 1); - if (read) - buffer.add(ch); - else - break; + // It's too large to fit in memory. + return SLANG_FAIL; } - return _Move(buffer); + + const Index sizeInBytes = Index(positionSizeInBytes); + + out.setCount(sizeInBytes); + + size_t readSizeInBytes; + SLANG_RETURN_ON_FAIL(stream.read(out.getBuffer(), sizeInBytes, readSizeInBytes)); + + // If not all read just return an error + return (size_t(sizeInBytes) == readSizeInBytes) ? SLANG_OK : SLANG_FAIL; } 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); + FileStream stream; + SLANG_RETURN_ON_FAIL(stream.init(path, FileMode::Open, FileAccess::Read, FileShare::ReadWrite)); - const Int64 positionSizeInBytes = end - start; + const Int64 start = stream.getPosition(); + stream.seek(SeekOrigin::End, 0); + const Int64 end = stream.getPosition(); + stream.seek(SeekOrigin::Start, start); - if (UInt64(positionSizeInBytes) > UInt64(~size_t(0))) - { - // It's too large to fit in memory. - return SLANG_FAIL; - } + const Int64 positionSizeInBytes = end - start; - const size_t sizeInBytes = size_t(positionSizeInBytes); - void* data = out.allocate(sizeInBytes); - if (!data) - { - return SLANG_E_OUT_OF_MEMORY; - } + if (UInt64(positionSizeInBytes) > UInt64(~size_t(0))) + { + // It's too large to fit in memory. + return SLANG_FAIL; + } - const size_t readSizeInBytes = stream.read(data, sizeInBytes); + const size_t sizeInBytes = size_t(positionSizeInBytes); - // If not all read just return an error - if (sizeInBytes != readSizeInBytes) - { - return SLANG_FAIL; - } - } - catch (const IOException&) + void* data = out.allocate(sizeInBytes); + if (!data) { - return SLANG_FAIL; + return SLANG_E_OUT_OF_MEMORY; } - return SLANG_OK; + + size_t readSizeInBytes; + SLANG_RETURN_ON_FAIL(stream.read(data, sizeInBytes, readSizeInBytes)); + + // If not all read just return an error + return (sizeInBytes == readSizeInBytes) ? SLANG_OK : SLANG_FAIL; } 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; - } + FileStream stream; + SLANG_RETURN_ON_FAIL(stream.init(path, FileMode::Create, FileAccess::Write, FileShare::ReadWrite)); + SLANG_RETURN_ON_FAIL(stream.write(data, size)); return SLANG_OK; } - void File::writeAllText(const Slang::String& fileName, const Slang::String& text) + SlangResult File::writeAllText(const Slang::String& fileName, const Slang::String& text) { - StreamWriter writer(new FileStream(fileName, FileMode::Create)); - writer.Write(text); + RefPtr stream = new FileStream; + SLANG_RETURN_ON_FAIL(stream->init(fileName, FileMode::Create)); + + StreamWriter writer; + SLANG_RETURN_ON_FAIL(writer.init(stream)); + SLANG_RETURN_ON_FAIL(writer.write(text)); + + return SLANG_OK; } -- cgit v1.2.3