From 6971b49f85d9ddeb2da968a594d65affec3d2b44 Mon Sep 17 00:00:00 2001 From: Benjamin Dobell Date: Tue, 10 Mar 2015 02:49:32 +1100 Subject: Lots of Frontend refactoring with a few bug fixes There is still far too much overly convoluted code. However, the introduction of some RAII when dealing with files has made the code-base slightly less error prone. --- heimdall-frontend/source/GZipFile.cpp | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 heimdall-frontend/source/GZipFile.cpp (limited to 'heimdall-frontend/source/GZipFile.cpp') diff --git a/heimdall-frontend/source/GZipFile.cpp b/heimdall-frontend/source/GZipFile.cpp new file mode 100644 index 0000000..af75f39 --- /dev/null +++ b/heimdall-frontend/source/GZipFile.cpp @@ -0,0 +1,51 @@ +#import "GZipFile.h" + +using namespace HeimdallFrontend; + +GZipFile::GZipFile(const QString& path) : + file(path), + gzFile(nullptr) +{ +} + +GZipFile::~GZipFile() +{ + Close(); + + if (temporary) + { + file.remove(); + } +} + +bool GZipFile::Open(Mode mode) +{ + if (!file.isOpen() && !file.open(mode == GZipFile::ReadOnly ? QFile::ReadOnly : QFile::WriteOnly)) + { + return (false); + } + + gzFile = gzdopen(file.handle(), mode == GZipFile::ReadOnly ? "rb" : "wb"); + return (gzFile != nullptr); +} + +void GZipFile::Close() +{ + file.close(); + gzclose(gzFile); +} + +int GZipFile::Read(void *buffer, int length) +{ + return (length >= 0 && !file.isWritable() ? gzread(gzFile, buffer, length) : -1); +} + +bool GZipFile::Write(void *buffer, int length) +{ + return (length >= 0 && gzwrite(gzFile, buffer, length) == length); +} + +qint64 GZipFile::Offset() const +{ + return gzoffset(gzFile); +} -- cgit v1.2.3