X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=trace_file.hpp;h=4b1b70ddad6f6bd886ac7aa51c28f5604b281faf;hb=353f0535f43b54d04cc9c8080790ffac606e5b0b;hp=a8b33459a024dc44d348f1c6cdd075d2e52a5439;hpb=af17c80a3ad1e77afd6bc19a0828a66b824ff8bb;p=apitrace diff --git a/trace_file.hpp b/trace_file.hpp index a8b3345..4b1b70d 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(uint64_t _chunk = 0, uint32_t _offsetInChunk = 0) + : chunk(_chunk), + offsetInChunk(_offsetInChunk) + {} + uint64_t chunk; + uint32_t offsetInChunk; + }; + public: static bool isZLibCompressed(const std::string &filename); static bool isSnappyCompressed(const std::string &filename); @@ -49,25 +59,29 @@ public: bool isOpened() const; File::Mode mode() const; - std::string filename() const; - bool open(const std::string &filename, File::Mode mode); - bool write(const void *buffer, int length); - bool read(void *buffer, int length); + bool write(const void *buffer, size_t length); + bool read(void *buffer, size_t length); void close(); void flush(void); int getc(); + bool skip(size_t length); + int percentRead(); + virtual bool supportsOffsets() const = 0; + virtual File::Offset currentOffset() = 0; + 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, int length) = 0; - virtual bool rawRead(void *buffer, int length) = 0; + virtual bool rawWrite(const void *buffer, size_t length) = 0; + virtual bool rawRead(void *buffer, size_t length) = 0; 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; File::Mode m_mode; bool m_isOpened; }; @@ -82,11 +96,6 @@ inline File::Mode File::mode() const return m_mode; } -inline std::string File::filename() const -{ - return m_filename; -} - inline bool File::open(const std::string &filename, File::Mode mode) { if (m_isOpened) { @@ -98,7 +107,7 @@ inline bool File::open(const std::string &filename, File::Mode mode) return m_isOpened; } -inline bool File::write(const void *buffer, int length) +inline bool File::write(const void *buffer, size_t length) { if (!m_isOpened || m_mode != File::Write) { return false; @@ -106,7 +115,7 @@ inline bool File::write(const void *buffer, int length) return rawWrite(buffer, length); } -inline bool File::read(void *buffer, int length) +inline bool File::read(void *buffer, size_t length) { if (!m_isOpened || m_mode != File::Read) { return false; @@ -114,6 +123,14 @@ inline bool File::read(void *buffer, int 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) { @@ -124,34 +141,84 @@ inline void File::close() inline void File::flush(void) { - rawFlush(); + if (m_mode == File::Write) { + rawFlush(); + } } inline int File::getc() { if (!m_isOpened || m_mode != File::Read) { - return 0; + return -1; } 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; + virtual File::Offset currentOffset(); protected: virtual bool rawOpen(const std::string &filename, File::Mode mode); - virtual bool rawWrite(const void *buffer, int length); - virtual bool rawRead(void *buffer, int length); + virtual bool rawWrite(const void *buffer, size_t length); + virtual bool 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; + 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