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