X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=trace_file.hpp;h=f6e77ae8b69802c88f96b8b31b48d6f3cd2c013c;hb=3a8ffad2f31f4438e19dd5a0f1c068e1fdffdeea;hp=60a8459d93c9436283deceae508b7708071ace0b;hpb=f3acd09b5367d91ae4e8168b55a5be53ae4e2386;p=apitrace diff --git a/trace_file.hpp b/trace_file.hpp index 60a8459..f6e77ae 100644 --- a/trace_file.hpp +++ b/trace_file.hpp @@ -29,6 +29,7 @@ #include #include +#include namespace Trace { @@ -38,6 +39,15 @@ public: Read, Write }; + struct Offset { + Offset() + : chunk(0), + offsetInChunk(0) + {} + uint64_t chunk; + uint32_t offsetInChunk; + }; + public: static bool isZLibCompressed(const std::string &filename); static bool isSnappyCompressed(const std::string &filename); @@ -57,7 +67,12 @@ public: void close(); void flush(void); int getc(); + bool skip(size_t length); + int percentRead(); + virtual bool supportsOffsets() const = 0; + virtual File::Offset currentOffset(); + virtual void setCurrentOffset(const File::Offset &offset); protected: virtual bool rawOpen(const std::string &filename, File::Mode mode) = 0; virtual bool rawWrite(const void *buffer, size_t length) = 0; @@ -65,6 +80,8 @@ protected: virtual int rawGetc() = 0; virtual void rawClose() = 0; virtual void rawFlush() = 0; + virtual bool rawSkip(size_t length) = 0; + virtual int rawPercentRead() = 0; protected: std::string m_filename; @@ -114,6 +131,14 @@ inline bool File::read(void *buffer, size_t length) return rawRead(buffer, length); } +inline int File::percentRead() +{ + if (!m_isOpened || m_mode != File::Read) { + return 0; + } + return rawPercentRead(); +} + inline void File::close() { if (m_isOpened) { @@ -137,12 +162,22 @@ inline int File::getc() return rawGetc(); } +inline bool File::skip(size_t length) +{ + if (!m_isOpened || m_mode != File::Read) { + return false; + } + return rawSkip(length); +} + class ZLibFile : public File { public: ZLibFile(const std::string &filename = std::string(), File::Mode mode = File::Read); virtual ~ZLibFile(); + + virtual bool supportsOffsets() const; protected: virtual bool rawOpen(const std::string &filename, File::Mode mode); virtual bool rawWrite(const void *buffer, size_t length); @@ -150,10 +185,47 @@ protected: virtual int rawGetc(); virtual void rawClose(); virtual void rawFlush(); + virtual bool rawSkip(size_t length); + virtual int rawPercentRead(); private: void *m_gzFile; + double m_endOffset; }; +inline bool +operator<(const File::Offset &one, const File::Offset &two) +{ + return one.chunk < two.chunk || + (one.chunk == two.chunk && one.offsetInChunk < two.offsetInChunk); +} + +inline bool +operator==(const File::Offset &one, const File::Offset &two) +{ + return one.chunk == two.chunk && + one.offsetInChunk == two.offsetInChunk; +} + +inline bool +operator>=(const File::Offset &one, const File::Offset &two) +{ + return one.chunk > two.chunk || + (one.chunk == two.chunk && one.offsetInChunk >= two.offsetInChunk); +} + +inline bool +operator>(const File::Offset &one, const File::Offset &two) +{ + return two < one; +} + +inline bool +operator<=(const File::Offset &one, const File::Offset &two) +{ + return two >= one; +} + + } #endif