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