]> git.cworth.org Git - apitrace/commitdiff
Abstract file writing operation into a class of its own.
authorZack Rusin <zack@kde.org>
Fri, 5 Aug 2011 17:43:46 +0000 (13:43 -0400)
committerZack Rusin <zack@kde.org>
Sat, 6 Aug 2011 21:15:18 +0000 (17:15 -0400)
Allows us to implement different compression, decompression algos.

CMakeLists.txt
trace_file.cpp [new file with mode: 0644]
trace_file.hpp [new file with mode: 0644]
trace_parser.cpp
trace_parser.hpp
trace_writer.cpp
trace_writer.hpp

index d7e4a28a7fb01e76b5aa23ad344106acde771c66..a543107de15ab4857c3053d31900f541c3d58265 100755 (executable)
@@ -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 (file)
index 0000000..1149978
--- /dev/null
@@ -0,0 +1,128 @@
+#include "trace_file.hpp"
+
+#include <zlib.h>
+
+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 (file)
index 0000000..9ea6bb8
--- /dev/null
@@ -0,0 +1,63 @@
+#ifndef TRACE_FILE_HPP
+#define TRACE_FILE_HPP
+
+#include <string>
+
+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
index 1d3c08ad62e0561da4fe2df60a6df36c2305fa1a..f3f4c4f15f17fcfb95f7e65b87b32815f3de79c7 100644 (file)
@@ -28,8 +28,7 @@
 #include <assert.h>
 #include <stdlib.h>
 
-#include <zlib.h>
-
+#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";
index 0a0105138bb6e144787670dc04ab0e44705eb704..4fff9ad1157129e0da8458f8cca87e68687197ec 100644 (file)
 
 namespace Trace {
 
+class File;
 
 class Parser
 {
 protected:
-    void *file;
+    File *file;
 
     typedef std::list<Call *> CallList;
     CallList calls;
index af8d62f7aee45602d4d8f18e8d7abfe9dda5f34a..a615960474d48b522d8d5606c1290451bd7868d5 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
-#include <zlib.h>
-
 #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<bool> &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();
 }
 
index 4f9cd2b27a32e913bad5889fe32c750afa099a6f..eb81f233f604bef706d1149d867d13ef1748e2fc 100644 (file)
 
 
 namespace Trace {
+    class File;
 
     class Writer {
     protected:
-        void *g_gzFile;
+        File *m_file;
         unsigned call_no;
 
         std::vector<bool> functions;