]> git.cworth.org Git - apitrace/blobdiff - trace_file.hpp
Merge remote-tracking branch 'origin/master' into on-demand-loading
[apitrace] / trace_file.hpp
index f0e183d70693912b9feb176e5ecc9c8fb35136ef..487121187fdd9dad368fe631445b3ffea218a80e 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <string>
 #include <fstream>
+#include <stdint.h>
 
 namespace Trace {
 
@@ -38,10 +39,15 @@ public:
         Read,
         Write
     };
-    enum FlushType {
-        FlushShallow,
-        FlushDeep
+    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);
@@ -56,19 +62,24 @@ public:
     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(FlushType type = FlushShallow);
+    void flush(void);
     int getc();
+    bool skip(unsigned length);
 
+    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, 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(FlushType type) = 0;
+    virtual void rawFlush() = 0;
+    virtual bool rawSkip(unsigned length) = 0;
 
 protected:
     std::string m_filename;
@@ -102,7 +113,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;
@@ -110,7 +121,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;
@@ -126,36 +137,81 @@ inline void File::close()
     }
 }
 
-inline void File::flush(File::FlushType type)
+inline void File::flush(void)
 {
-    rawFlush(type);
+    rawFlush();
 }
 
 inline int File::getc()
 {
     if (!m_isOpened || m_mode != File::Read) {
-        return 0;
+        return -1;
     }
     return rawGetc();
 }
 
+inline bool File::skip(unsigned 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, 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(FlushType type);
+    virtual void rawFlush();
+    virtual bool rawSkip(unsigned length);
 private:
     void *m_gzFile;
 };
 
+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