diff options
author | Tiger Wang <ziwei.tiger@outlook.com> | 2021-01-11 17:39:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-11 17:39:43 +0100 |
commit | eeb63b8901a9c049f1bb594abb9ce9b4a9c47620 (patch) | |
tree | b07daae788f918b83eeb0bdbd51e49292f1c8d88 /src/OSSupport/GZipFile.cpp | |
parent | Fixed switch-ups regarding some slab and stair recipes (#5099) (diff) | |
download | cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.gz cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.bz2 cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.lz cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.xz cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.tar.zst cuberite-eeb63b8901a9c049f1bb594abb9ce9b4a9c47620.zip |
Diffstat (limited to 'src/OSSupport/GZipFile.cpp')
-rw-r--r-- | src/OSSupport/GZipFile.cpp | 96 |
1 files changed, 8 insertions, 88 deletions
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<char>(File), std::istreambuf_iterator<char>() }; + const ContiguousByteBufferView Data{ reinterpret_cast<const std::byte *>(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<size_t>(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<unsigned int>(a_Size)) != 0); -} - - - - |