]> git.cworth.org Git - apitrace/blobdiff - trace_file.cpp
Export SnappyFile class to its own file.
[apitrace] / trace_file.cpp
index 11499783a71ab03e34c9382a6e0b21686cf19ba3..758ecd78c264d6e218b001473489394607da4b8f 100644 (file)
@@ -1,9 +1,19 @@
 #include "trace_file.hpp"
 
+#include "trace_snappyfile.hpp"
+
+#include <assert.h>
+#include <string.h>
+
 #include <zlib.h>
 
+#include "os.hpp"
+
+#include <iostream>
+
 using namespace Trace;
 
+
 File::File(const std::string &filename,
            File::Mode mode)
     : m_filename(filename),
@@ -15,6 +25,7 @@ File::File(const std::string &filename,
     }
 }
 
+
 File::~File()
 {
     close();
@@ -41,6 +52,8 @@ bool File::open(const std::string &filename, File::Mode mode)
         close();
     }
     m_isOpened = rawOpen(filename, mode);
+    m_mode = mode;
+
     return m_isOpened;
 }
 
@@ -68,12 +81,12 @@ void File::close()
     }
 }
 
-void File::flush()
+void File::flush(FlushType type)
 {
-    rawFlush();
+    rawFlush(type);
 }
 
-char File::getc()
+int File::getc()
 {
     if (!m_isOpened || m_mode != File::Read) {
         return 0;
@@ -81,6 +94,38 @@ char File::getc()
     return rawGetc();
 }
 
+bool File::isZLibCompressed(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 == 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),
@@ -101,15 +146,15 @@ bool ZLibFile::rawOpen(const std::string &filename, File::Mode mode)
 
 bool ZLibFile::rawWrite(const void *buffer, int length)
 {
-    return gzwrite(m_gzFile, buffer, length);
+    return gzwrite(m_gzFile, buffer, length) != -1;
 }
 
 bool ZLibFile::rawRead(void *buffer, int length)
 {
-    return gzread(m_gzFile, buffer, length);
+    return gzread(m_gzFile, buffer, length) != -1;
 }
 
-char ZLibFile::rawGetc()
+int ZLibFile::rawGetc()
 {
     return gzgetc(m_gzFile);
 }
@@ -122,7 +167,7 @@ void ZLibFile::rawClose()
     }
 }
 
-void ZLibFile::rawFlush()
+void ZLibFile::rawFlush(FlushType type)
 {
     gzflush(m_gzFile, Z_SYNC_FLUSH);
 }