From eeb63b8901a9c049f1bb594abb9ce9b4a9c47620 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 11 Jan 2021 16:39:43 +0000 Subject: zlib -> libdeflate (#5085) + Use libdeflate + Use std::byte * Fix passing temporary to string_view + Emulate make_unique_for_overwrite --- src/OSSupport/File.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 9 deletions(-) (limited to 'src/OSSupport/File.cpp') diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp index 8c5eb92a4..618463bd6 100644 --- a/src/OSSupport/File.cpp +++ b/src/OSSupport/File.cpp @@ -157,19 +157,18 @@ int cFile::Read (void * a_Buffer, size_t a_NumBytes) -AString cFile::Read(size_t a_NumBytes) +ContiguousByteBuffer cFile::Read(size_t a_NumBytes) { ASSERT(IsOpen()); if (!IsOpen()) { - return AString(); + return {}; } - // HACK: This depends on the knowledge that AString::data() returns the internal buffer, rather than a copy of it. - AString res; - res.resize(a_NumBytes); - auto newSize = fread(const_cast(res.data()), 1, a_NumBytes, m_File); + ContiguousByteBuffer res; + res.resize(a_NumBytes); // TODO: investigate if worth hacking around std::string internals to avoid initialisation + auto newSize = fread(res.data(), sizeof(std::byte), a_NumBytes, m_File); res.resize(newSize); return res; } @@ -284,9 +283,8 @@ int cFile::ReadRestOfFile(AString & a_Contents) auto DataSize = static_cast(TotalSize - Position); - // HACK: This depends on the internal knowledge that AString's data() function returns the internal buffer directly - a_Contents.assign(DataSize, '\0'); - return Read(static_cast(const_cast(a_Contents.data())), DataSize); + a_Contents.resize(DataSize); // TODO: investigate if worth hacking around std::string internals to avoid initialisation + return Read(a_Contents.data(), DataSize); } @@ -709,3 +707,54 @@ void cFile::Flush(void) { fflush(m_File); } + + + + + +template +FileStream::FileStream(const std::string & Path) +{ + // Except on failbit, which is what open sets on failure: + FileStream::exceptions(FileStream::failbit | FileStream::badbit); + + // Open the file: + FileStream::open(Path); + + // Only subsequently except on serious errors, and not on conditions like EOF or malformed input: + FileStream::exceptions(FileStream::badbit); +} + + + + + +template +FileStream::FileStream(const std::string & Path, const typename FileStream::openmode Mode) +{ + // Except on failbit, which is what open sets on failure: + FileStream::exceptions(FileStream::failbit | FileStream::badbit); + + // Open the file: + FileStream::open(Path, Mode); + + // Only subsequently except on serious errors, and not on conditions like EOF or malformed input: + FileStream::exceptions(FileStream::badbit); +} + + + + + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wweak-template-vtables" // http://bugs.llvm.org/show_bug.cgi?id=18733 +#endif + +// Instantiate the templated wrapper for input and output: +template class FileStream; +template class FileStream; + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif -- cgit v1.2.3