From 5ce45e7f614c7540f4d3d4f86db7a499f138d01e Mon Sep 17 00:00:00 2001 From: Zack Rusin Date: Fri, 5 Aug 2011 13:43:46 -0400 Subject: [PATCH] Abstract file writing operation into a class of its own. Allows us to implement different compression, decompression algos. --- CMakeLists.txt | 1 + trace_file.cpp | 128 +++++++++++++++++++++++++++++++++++++++++++++++ trace_file.hpp | 63 +++++++++++++++++++++++ trace_parser.cpp | 26 ++++------ trace_parser.hpp | 3 +- trace_writer.cpp | 33 ++++++------ trace_writer.hpp | 3 +- 7 files changed, 221 insertions(+), 36 deletions(-) create mode 100644 trace_file.cpp create mode 100644 trace_file.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d7e4a28..a543107 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -173,6 +173,7 @@ else (WIN32) endif (WIN32) add_library (common + trace_file.cpp trace_model.cpp trace_parser.cpp trace_writer.cpp diff --git a/trace_file.cpp b/trace_file.cpp new file mode 100644 index 0000000..1149978 --- /dev/null +++ b/trace_file.cpp @@ -0,0 +1,128 @@ +#include "trace_file.hpp" + +#include + +using namespace Trace; + +File::File(const std::string &filename, + File::Mode mode) + : m_filename(filename), + m_mode(mode), + m_isOpened(false) +{ + if (!m_filename.empty()) { + open(m_filename, m_mode); + } +} + +File::~File() +{ + close(); +} + +bool File::isOpened() const +{ + return m_isOpened; +} + +File::Mode File::mode() const +{ + return m_mode; +} + +std::string File::filename() const +{ + return m_filename; +} + +bool File::open(const std::string &filename, File::Mode mode) +{ + if (m_isOpened) { + close(); + } + m_isOpened = rawOpen(filename, mode); + return m_isOpened; +} + +bool File::write(const void *buffer, int length) +{ + if (!m_isOpened || m_mode != File::Write) { + return false; + } + return rawWrite(buffer, length); +} + +bool File::read(void *buffer, int length) +{ + if (!m_isOpened || m_mode != File::Read) { + return false; + } + return rawRead(buffer, length); +} + +void File::close() +{ + if (m_isOpened) { + rawClose(); + m_isOpened = false; + } +} + +void File::flush() +{ + rawFlush(); +} + +char File::getc() +{ + if (!m_isOpened || m_mode != File::Read) { + return 0; + } + return rawGetc(); +} + +ZLibFile::ZLibFile(const std::string &filename, + File::Mode mode) + : File(filename, mode), + m_gzFile(NULL) +{ +} + +ZLibFile::~ZLibFile() +{ +} + +bool ZLibFile::rawOpen(const std::string &filename, File::Mode mode) +{ + m_gzFile = gzopen(filename.c_str(), + (mode == File::Write) ? "wb" : "rb"); + return m_gzFile != NULL; +} + +bool ZLibFile::rawWrite(const void *buffer, int length) +{ + return gzwrite(m_gzFile, buffer, length); +} + +bool ZLibFile::rawRead(void *buffer, int length) +{ + return gzread(m_gzFile, buffer, length); +} + +char ZLibFile::rawGetc() +{ + return gzgetc(m_gzFile); +} + +void ZLibFile::rawClose() +{ + if (m_gzFile) { + gzclose(m_gzFile); + m_gzFile = NULL; + } +} + +void ZLibFile::rawFlush() +{ + gzflush(m_gzFile, Z_SYNC_FLUSH); +} diff --git a/trace_file.hpp b/trace_file.hpp new file mode 100644 index 0000000..9ea6bb8 --- /dev/null +++ b/trace_file.hpp @@ -0,0 +1,63 @@ +#ifndef TRACE_FILE_HPP +#define TRACE_FILE_HPP + +#include + +namespace Trace { + +class File { +public: + enum Mode { + Read, + Write + }; +public: + File(const std::string &filename = std::string(), + File::Mode mode = File::Read); + virtual ~File(); + + 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); + void close(); + void flush(); + char getc(); + +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 char rawGetc() = 0; + virtual void rawClose() = 0; + virtual void rawFlush() = 0; + +protected: + std::string m_filename; + File::Mode m_mode; + bool m_isOpened; +}; + +class ZLibFile : public File { +public: + ZLibFile(const std::string &filename = std::string(), + File::Mode mode = File::Read); + virtual ~ZLibFile(); + +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 char rawGetc(); + virtual void rawClose(); + virtual void rawFlush(); +private: + void *m_gzFile; +}; + +} + +#endif diff --git a/trace_parser.cpp b/trace_parser.cpp index 1d3c08a..f3f4c4f 100644 --- a/trace_parser.cpp +++ b/trace_parser.cpp @@ -28,8 +28,7 @@ #include #include -#include - +#include "trace_file.hpp" #include "trace_parser.hpp" @@ -40,7 +39,7 @@ namespace Trace { Parser::Parser() { - file = NULL; + file = new Trace::ZLibFile; next_call_no = 0; version = 0; } @@ -48,12 +47,12 @@ Parser::Parser() { Parser::~Parser() { close(); + delete file; } bool Parser::open(const char *filename) { - file = gzopen(filename, "rb"); - if (!file) { + if (!file->open(filename, File::Read)) { return false; } @@ -84,10 +83,7 @@ deleteAll(const Container &c) } void Parser::close(void) { - if (file) { - gzclose(file); - file = NULL; - } + file->close(); deleteAll(calls); deleteAll(functions); @@ -293,14 +289,14 @@ Value *Parser::parse_uint() { Value *Parser::parse_float() { float value; - gzread(file, &value, sizeof value); + file->read(&value, sizeof value); return new Float(value); } Value *Parser::parse_double() { double value; - gzread(file, &value, sizeof value); + file->read(&value, sizeof value); return new Float(value); } @@ -367,7 +363,7 @@ Value *Parser::parse_blob(void) { size_t size = read_uint(); Blob *blob = new Blob(size); if (size) { - gzread(file, blob->buf, (unsigned)size); + file->read(blob->buf, (unsigned)size); } return blob; } @@ -412,7 +408,7 @@ const char * Parser::read_string(void) { size_t len = read_uint(); char * value = new char[len + 1]; if (len) { - gzread(file, value, (unsigned)len); + file->read(value, (unsigned)len); } value[len] = 0; #if TRACE_VERBOSE @@ -427,7 +423,7 @@ unsigned long long Parser::read_uint(void) { int c; unsigned shift = 0; do { - c = gzgetc(file); + c = file->getc(); if (c == -1) { break; } @@ -442,7 +438,7 @@ unsigned long long Parser::read_uint(void) { inline int Parser::read_byte(void) { - int c = gzgetc(file); + int c = file->getc(); #if TRACE_VERBOSE if (c < 0) std::cerr << "\tEOF" << "\n"; diff --git a/trace_parser.hpp b/trace_parser.hpp index 0a01051..4fff9ad 100644 --- a/trace_parser.hpp +++ b/trace_parser.hpp @@ -36,11 +36,12 @@ namespace Trace { +class File; class Parser { protected: - void *file; + File *file; typedef std::list CallList; CallList calls; diff --git a/trace_writer.cpp b/trace_writer.cpp index af8d62f..a615960 100644 --- a/trace_writer.cpp +++ b/trace_writer.cpp @@ -30,10 +30,9 @@ #include #include -#include - #include "os.hpp" #include "trace_writer.hpp" +#include "trace_file.hpp" #include "trace_format.hpp" @@ -41,30 +40,29 @@ namespace Trace { Writer::Writer() : - g_gzFile(NULL), call_no(0) { + m_file = new Trace::ZLibFile; close(); -}; +} -Writer::~Writer() { +Writer::~Writer() +{ close(); -}; + delete m_file; + m_file = NULL; +} void Writer::close(void) { - if (g_gzFile != NULL) { - gzclose(g_gzFile); - g_gzFile = NULL; - } + m_file->close(); } bool Writer::open(const char *filename) { close(); - g_gzFile = gzopen(filename, "wb"); - if (!g_gzFile) { + if (!m_file->open(filename, File::Write)) { return false; } @@ -123,10 +121,7 @@ Writer::open(void) { void inline Writer::_write(const void *sBuffer, size_t dwBytesToWrite) { - if (g_gzFile == NULL) - return; - - gzwrite(g_gzFile, sBuffer, dwBytesToWrite); + m_file->write(sBuffer, dwBytesToWrite); } void inline @@ -184,7 +179,7 @@ inline bool lookup(std::vector &map, size_t index) { unsigned Writer::beginEnter(const FunctionSig *sig) { OS::AcquireMutex(); - if (!g_gzFile) { + if (!m_file->isOpened()) { open(); } @@ -204,7 +199,7 @@ unsigned Writer::beginEnter(const FunctionSig *sig) { void Writer::endEnter(void) { _writeByte(Trace::CALL_END); - gzflush(g_gzFile, Z_SYNC_FLUSH); + m_file->flush(); OS::ReleaseMutex(); } @@ -216,7 +211,7 @@ void Writer::beginLeave(unsigned call) { void Writer::endLeave(void) { _writeByte(Trace::CALL_END); - gzflush(g_gzFile, Z_SYNC_FLUSH); + m_file->flush(); OS::ReleaseMutex(); } diff --git a/trace_writer.hpp b/trace_writer.hpp index 4f9cd2b..eb81f23 100644 --- a/trace_writer.hpp +++ b/trace_writer.hpp @@ -39,10 +39,11 @@ namespace Trace { + class File; class Writer { protected: - void *g_gzFile; + File *m_file; unsigned call_no; std::vector functions; -- 2.43.0