]> git.cworth.org Git - apitrace/blob - trace_file.hpp
Add a file identifier to snappy compressed traces
[apitrace] / trace_file.hpp
1 #ifndef TRACE_FILE_HPP
2 #define TRACE_FILE_HPP
3
4 #include <string>
5 #include <fstream>
6
7 namespace Trace {
8
9 class File {
10 public:
11     enum Mode {
12         Read,
13         Write
14     };
15 public:
16     static bool isZLibCompressed(const std::string &filename);
17     static bool isSnappyCompressed(const std::string &filename);
18 public:
19     File(const std::string &filename = std::string(),
20          File::Mode mode = File::Read);
21     virtual ~File();
22
23     bool isOpened() const;
24     File::Mode mode() const;
25     std::string filename() const;
26
27     bool open(const std::string &filename, File::Mode mode);
28     bool write(const void *buffer, int length);
29     bool read(void *buffer, int length);
30     void close();
31     void flush();
32     int getc();
33
34 protected:
35     virtual bool rawOpen(const std::string &filename, File::Mode mode) = 0;
36     virtual bool rawWrite(const void *buffer, int length) = 0;
37     virtual bool rawRead(void *buffer, int length) = 0;
38     virtual int rawGetc() = 0;
39     virtual void rawClose() = 0;
40     virtual void rawFlush() = 0;
41
42 protected:
43     std::string m_filename;
44     File::Mode m_mode;
45     bool m_isOpened;
46 };
47
48 class ZLibFile : public File {
49 public:
50     ZLibFile(const std::string &filename = std::string(),
51              File::Mode mode = File::Read);
52     virtual ~ZLibFile();
53
54 protected:
55     virtual bool rawOpen(const std::string &filename, File::Mode mode);
56     virtual bool rawWrite(const void *buffer, int length);
57     virtual bool rawRead(void *buffer, int length);
58     virtual int rawGetc();
59     virtual void rawClose();
60     virtual void rawFlush();
61 private:
62     void *m_gzFile;
63 };
64
65 namespace snappy {
66     class File;
67 }
68
69 #define SNAPPY_CHUNK_SIZE (1 * 1024 * 1024)
70 class SnappyFile : public File {
71 public:
72     SnappyFile(const std::string &filename = std::string(),
73                File::Mode mode = File::Read);
74     virtual ~SnappyFile();
75
76 protected:
77     virtual bool rawOpen(const std::string &filename, File::Mode mode);
78     virtual bool rawWrite(const void *buffer, int length);
79     virtual bool rawRead(void *buffer, int length);
80     virtual int rawGetc();
81     virtual void rawClose();
82     virtual void rawFlush();
83
84 private:
85     inline int freeCacheSize() const
86     {
87         if (m_cacheSize > 0)
88             return m_cacheSize - (m_cachePtr - m_cache);
89         else
90             return 0;
91     }
92     void flushCache();
93     void createCache(size_t size);
94 private:
95     std::fstream m_stream;
96     char *m_cache;
97     char *m_cachePtr;
98     size_t m_cacheSize;
99
100     char *m_compressedCache;
101 };
102
103
104 }
105
106 #endif