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