]> git.cworth.org Git - apitrace/blob - trace_file.hpp
Make sure that the size of the compressed length is constant.
[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
30     std::string filename() const;
31
32     bool open(const std::string &filename, File::Mode mode);
33     bool write(const void *buffer, int length);
34     bool read(void *buffer, int length);
35     void close();
36     void flush(FlushType type = FlushShallow);
37     int getc();
38
39 protected:
40     virtual bool rawOpen(const std::string &filename, File::Mode mode) = 0;
41     virtual bool rawWrite(const void *buffer, int length) = 0;
42     virtual bool rawRead(void *buffer, int length) = 0;
43     virtual int rawGetc() = 0;
44     virtual void rawClose() = 0;
45     virtual void rawFlush(FlushType type) = 0;
46
47 protected:
48     std::string m_filename;
49     File::Mode m_mode;
50     bool m_isOpened;
51 };
52
53 inline bool File::isOpened() const
54 {
55     return m_isOpened;
56 }
57
58 inline File::Mode File::mode() const
59 {
60     return m_mode;
61 }
62
63 inline std::string File::filename() const
64 {
65     return m_filename;
66 }
67
68 inline bool File::open(const std::string &filename, File::Mode mode)
69 {
70     if (m_isOpened) {
71         close();
72     }
73     m_isOpened = rawOpen(filename, mode);
74     m_mode = mode;
75
76     return m_isOpened;
77 }
78
79 inline bool File::write(const void *buffer, int length)
80 {
81     if (!m_isOpened || m_mode != File::Write) {
82         return false;
83     }
84     return rawWrite(buffer, length);
85 }
86
87 inline bool File::read(void *buffer, int length)
88 {
89     if (!m_isOpened || m_mode != File::Read) {
90         return false;
91     }
92     return rawRead(buffer, length);
93 }
94
95 inline void File::close()
96 {
97     if (m_isOpened) {
98         rawClose();
99         m_isOpened = false;
100     }
101 }
102
103 inline void File::flush(File::FlushType type)
104 {
105     rawFlush(type);
106 }
107
108 inline int File::getc()
109 {
110     if (!m_isOpened || m_mode != File::Read) {
111         return 0;
112     }
113     return rawGetc();
114 }
115
116 class ZLibFile : public File {
117 public:
118     ZLibFile(const std::string &filename = std::string(),
119              File::Mode mode = File::Read);
120     virtual ~ZLibFile();
121
122 protected:
123     virtual bool rawOpen(const std::string &filename, File::Mode mode);
124     virtual bool rawWrite(const void *buffer, int length);
125     virtual bool rawRead(void *buffer, int length);
126     virtual int rawGetc();
127     virtual void rawClose();
128     virtual void rawFlush(FlushType type);
129 private:
130     void *m_gzFile;
131 };
132
133 }
134
135 #endif