]> git.cworth.org Git - apitrace/blobdiff - trace_file.hpp
Fix and cleanup state lookups on frames.
[apitrace] / trace_file.hpp
index e3d797d3b20d3228f06711cfc9b3d31eef86c7a5..4b1b70ddad6f6bd886ac7aa51c28f5604b281faf 100644 (file)
@@ -40,9 +40,9 @@ public:
         Write
     };
     struct Offset {
-        Offset()
-            : chunk(0),
-              offsetInChunk(0)
+        Offset(uint64_t _chunk = 0, uint32_t _offsetInChunk = 0)
+            : chunk(_chunk),
+              offsetInChunk(_offsetInChunk)
         {}
         uint64_t chunk;
         uint32_t offsetInChunk;
@@ -59,28 +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();
+    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;
 };
@@ -95,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) {
@@ -111,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;
@@ -119,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;
@@ -127,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) {
@@ -137,7 +141,9 @@ inline void File::close()
 
 inline void File::flush(void)
 {
-    rawFlush();
+    if (m_mode == File::Write) {
+        rawFlush();
+    }
 }
 
 inline int File::getc()
@@ -148,6 +154,14 @@ 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(),
@@ -156,17 +170,55 @@ public:
 
 
     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