]> git.cworth.org Git - apitrace/commitdiff
Add a file identifier to snappy compressed traces
authorZack Rusin <zack@kde.org>
Mon, 8 Aug 2011 13:59:58 +0000 (09:59 -0400)
committerZack Rusin <zack@kde.org>
Mon, 8 Aug 2011 13:59:58 +0000 (09:59 -0400)
trace_file.cpp
trace_file.hpp

index 10e6565925449f3b820088587cdad1453139dea5..27b3eb18c07d10eb7e0a2fc3e5dc1bdd9e366b26 100644 (file)
@@ -10,6 +10,9 @@
 
 using namespace Trace;
 
+#define SNAPPY_BYTE1 'a'
+#define SNAPPY_BYTE2 't'
+
 File::File(const std::string &filename,
            File::Mode mode)
     : m_filename(filename),
@@ -104,6 +107,23 @@ bool File::isZLibCompressed(const std::string &filename)
     return (byte1 == 0x1f && byte2 == 0x8b);
 }
 
+
+bool File::isSnappyCompressed(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 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
+}
+
+
 ZLibFile::ZLibFile(const std::string &filename,
                    File::Mode mode)
     : File(filename, mode),
@@ -179,7 +199,17 @@ bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
 
     //read in the initial buffer if we're reading
     if (m_stream.is_open() && mode == File::Read) {
+        // read the snappy file identifier
+        unsigned char byte1, byte2;
+        m_stream >> byte1;
+        m_stream >> byte2;
+        assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
+
         flushCache();
+    } else if (m_stream.is_open() && mode == File::Write) {
+        // write the snappy file identifier
+        m_stream << SNAPPY_BYTE1;
+        m_stream << SNAPPY_BYTE2;
     }
     return m_stream.is_open();
 }
index 3afa7096553c97a2ee7281862e5eaa85db0590f8..ddc36c0c8b49a616a0ff5307f40746395b1f7d63 100644 (file)
@@ -14,6 +14,7 @@ public:
     };
 public:
     static bool isZLibCompressed(const std::string &filename);
+    static bool isSnappyCompressed(const std::string &filename);
 public:
     File(const std::string &filename = std::string(),
          File::Mode mode = File::Read);