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/GZipFile.cpp | 96 ++++------------------------------------------ 1 file changed, 8 insertions(+), 88 deletions(-) (limited to 'src/OSSupport/GZipFile.cpp') diff --git a/src/OSSupport/GZipFile.cpp b/src/OSSupport/GZipFile.cpp index e83d42583..0bf26dfed 100644 --- a/src/OSSupport/GZipFile.cpp +++ b/src/OSSupport/GZipFile.cpp @@ -4,107 +4,27 @@ // Implements the cGZipFile class representing a RAII wrapper over zlib's GZip file routines #include "Globals.h" +#include "File.h" #include "GZipFile.h" -cGZipFile::cGZipFile(void) : - m_File(nullptr), m_Mode(fmRead) +Compression::Result GZipFile::ReadRestOfFile(const std::string & a_FileName) { -} - - - - - -cGZipFile::~cGZipFile() -{ - Close(); -} - + InputFileStream File(a_FileName, InputFileStream::binary); + const std::string Input{ std::istreambuf_iterator(File), std::istreambuf_iterator() }; + const ContiguousByteBufferView Data{ reinterpret_cast(Input.data()), Input.size() }; - - - -bool cGZipFile::Open(const AString & a_FileName, eMode a_Mode) -{ - if (m_File != nullptr) - { - ASSERT(!"A file is already open in this object"); - return false; - } - m_File = gzopen(a_FileName.c_str(), (a_Mode == fmRead) ? "r" : "w"); - m_Mode = a_Mode; - return (m_File != nullptr); + return Compression::Extractor().ExtractGZip(Data); } -void cGZipFile::Close(void) +void GZipFile::Write(const std::string & a_FileName, ContiguousByteBufferView a_Contents) { - if (m_File != nullptr) - { - gzclose(m_File); - m_File = nullptr; - } + OutputFileStream(a_FileName, OutputFileStream::binary) << Compression::Compressor().CompressGZip(a_Contents).GetStringView(); } - - - - - -int cGZipFile::ReadRestOfFile(AString & a_Contents) -{ - if (m_File == nullptr) - { - ASSERT(!"No file has been opened"); - return -1; - } - - if (m_Mode != fmRead) - { - ASSERT(!"Bad file mode, cannot read"); - return -1; - } - - // Since the gzip format doesn't really support getting the uncompressed length, we need to read incrementally. Yuck! - int NumBytesRead = 0; - int TotalBytes = 0; - char Buffer[64 KiB]; - while ((NumBytesRead = gzread(m_File, Buffer, sizeof(Buffer))) > 0) - { - TotalBytes += NumBytesRead; - a_Contents.append(Buffer, static_cast(NumBytesRead)); - } - // NumBytesRead is < 0 on error - return (NumBytesRead >= 0) ? TotalBytes : NumBytesRead; -} - - - - - -bool cGZipFile::Write(const char * a_Contents, int a_Size) -{ - if (m_File == nullptr) - { - ASSERT(!"No file has been opened"); - return false; - } - - if (m_Mode != fmWrite) - { - ASSERT(!"Bad file mode, cannot write"); - return false; - } - - return (gzwrite(m_File, a_Contents, static_cast(a_Size)) != 0); -} - - - - -- cgit v1.2.3