From 14b78f815fa02ffda48f8a6599d6af45868266ff Mon Sep 17 00:00:00 2001 From: Zack Rusin Date: Sat, 6 Aug 2011 19:26:46 -0400 Subject: [PATCH] Automatically detect whether trace is zlib or snappy compressed --- trace_file.cpp | 15 +++++++++++++++ trace_file.hpp | 2 ++ trace_parser.cpp | 12 ++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/trace_file.cpp b/trace_file.cpp index 0272a5c..10e6565 100644 --- a/trace_file.cpp +++ b/trace_file.cpp @@ -89,6 +89,21 @@ int 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); +} + ZLibFile::ZLibFile(const std::string &filename, File::Mode mode) : File(filename, mode), diff --git a/trace_file.hpp b/trace_file.hpp index 7e7f9bd..3afa709 100644 --- a/trace_file.hpp +++ b/trace_file.hpp @@ -12,6 +12,8 @@ public: Read, Write }; +public: + static bool isZLibCompressed(const std::string &filename); public: File(const std::string &filename = std::string(), File::Mode mode = File::Read); diff --git a/trace_parser.cpp b/trace_parser.cpp index d53d62c..b0b501a 100644 --- a/trace_parser.cpp +++ b/trace_parser.cpp @@ -39,7 +39,7 @@ namespace Trace { Parser::Parser() { - file = new Trace::SnappyFile; + file = NULL; next_call_no = 0; version = 0; } @@ -47,11 +47,17 @@ Parser::Parser() { Parser::~Parser() { close(); - delete file; } bool Parser::open(const char *filename) { + assert(!file); + if (File::isZLibCompressed(filename)) { + file = new ZLibFile; + } else { + file = new SnappyFile; + } + if (!file->open(filename, File::Read)) { return false; } @@ -84,6 +90,8 @@ deleteAll(const Container &c) void Parser::close(void) { file->close(); + delete file; + file = NULL; deleteAll(calls); deleteAll(functions); -- 2.45.2