X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=common%2Ftrace_file_zlib.cpp;h=a432ccde44598984f2503ff4a68e304a723ec54d;hb=dfd413a5f54bd450850b5e84886949bcdf85b1e7;hp=92cead9188862ddb57d4bf74d79e14082680eac2;hpb=b4a3d1495a5e92ba23bf463bcea34a6e75b55294;p=apitrace diff --git a/common/trace_file_zlib.cpp b/common/trace_file_zlib.cpp index 92cead9..a432ccd 100644 --- a/common/trace_file_zlib.cpp +++ b/common/trace_file_zlib.cpp @@ -33,6 +33,14 @@ #include #include +// for lseek +#ifdef _WIN32 +#include +#else +#include +#include +#endif + #include "os.hpp" #include @@ -53,14 +61,14 @@ public: protected: virtual bool rawOpen(const std::string &filename, File::Mode mode); virtual bool rawWrite(const void *buffer, size_t length); - virtual bool rawRead(void *buffer, size_t length); + virtual size_t rawRead(void *buffer, size_t length); virtual int rawGetc(); virtual void rawClose(); virtual void rawFlush(); virtual bool rawSkip(size_t length); virtual int rawPercentRead(); private: - void *m_gzFile; + gzFile m_gzFile; double m_endOffset; }; @@ -73,6 +81,7 @@ ZLibFile::ZLibFile(const std::string &filename, ZLibFile::~ZLibFile() { + close(); } bool ZLibFile::rawOpen(const std::string &filename, File::Mode mode) @@ -96,12 +105,13 @@ bool ZLibFile::rawOpen(const std::string &filename, File::Mode mode) bool ZLibFile::rawWrite(const void *buffer, size_t length) { - return gzwrite(m_gzFile, buffer, length) != -1; + return gzwrite(m_gzFile, buffer, unsigned(length)) != -1; } -bool ZLibFile::rawRead(void *buffer, size_t length) +size_t ZLibFile::rawRead(void *buffer, size_t length) { - return gzread(m_gzFile, buffer, length) != -1; + int ret = gzread(m_gzFile, buffer, unsigned(length)); + return ret < 0 ? 0 : ret; } int ZLibFile::rawGetc() @@ -140,26 +150,10 @@ bool ZLibFile::rawSkip(size_t) int ZLibFile::rawPercentRead() { gz_state *state = (gz_state *)m_gzFile; - return 100 * (lseek(state->fd, 0, SEEK_CUR) / m_endOffset); + return int(100 * (lseek(state->fd, 0, SEEK_CUR) / m_endOffset)); } File * File::createZLib(void) { return new ZLibFile; } - -bool File::isZLibCompressed(const std::string &filename) -{ - std::fstream stream(filename.c_str(), - std::fstream::binary | std::fstream::in); - if (!stream.is_open()) - return false; - - unsigned char byte1, byte2; - stream >> byte1; - stream >> byte2; - stream.close(); - - return (byte1 == 0x1f && byte2 == 0x8b); -} -