]> git.cworth.org Git - apitrace/blob - trace_file.hpp
Merge branch 'master' into on-demand-loading
[apitrace] / trace_file.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Zack Rusin
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #ifndef TRACE_FILE_HPP
28 #define TRACE_FILE_HPP
29
30 #include <string>
31 #include <fstream>
32 #include <stdint.h>
33
34 namespace Trace {
35
36 class File {
37 public:
38     enum Mode {
39         Read,
40         Write
41     };
42     struct Offset {
43         Offset()
44             : chunk(0),
45               offsetInChunk(0)
46         {}
47         uint64_t chunk;
48         uint32_t offsetInChunk;
49     };
50
51 public:
52     static bool isZLibCompressed(const std::string &filename);
53     static bool isSnappyCompressed(const std::string &filename);
54 public:
55     File(const std::string &filename = std::string(),
56          File::Mode mode = File::Read);
57     virtual ~File();
58
59     bool isOpened() const;
60     File::Mode mode() const;
61
62     std::string filename() const;
63
64     bool open(const std::string &filename, File::Mode mode);
65     bool write(const void *buffer, int length);
66     bool read(void *buffer, int length);
67     void close();
68     void flush(void);
69     int getc();
70
71     virtual bool supportsOffsets() const = 0;
72     virtual File::Offset currentOffset();
73     virtual void setCurrentOffset(const File::Offset &offset);
74 protected:
75     virtual bool rawOpen(const std::string &filename, File::Mode mode) = 0;
76     virtual bool rawWrite(const void *buffer, int length) = 0;
77     virtual bool rawRead(void *buffer, int length) = 0;
78     virtual int rawGetc() = 0;
79     virtual void rawClose() = 0;
80     virtual void rawFlush() = 0;
81
82 protected:
83     std::string m_filename;
84     File::Mode m_mode;
85     bool m_isOpened;
86 };
87
88 inline bool File::isOpened() const
89 {
90     return m_isOpened;
91 }
92
93 inline File::Mode File::mode() const
94 {
95     return m_mode;
96 }
97
98 inline std::string File::filename() const
99 {
100     return m_filename;
101 }
102
103 inline bool File::open(const std::string &filename, File::Mode mode)
104 {
105     if (m_isOpened) {
106         close();
107     }
108     m_isOpened = rawOpen(filename, mode);
109     m_mode = mode;
110
111     return m_isOpened;
112 }
113
114 inline bool File::write(const void *buffer, int length)
115 {
116     if (!m_isOpened || m_mode != File::Write) {
117         return false;
118     }
119     return rawWrite(buffer, length);
120 }
121
122 inline bool File::read(void *buffer, int length)
123 {
124     if (!m_isOpened || m_mode != File::Read) {
125         return false;
126     }
127     return rawRead(buffer, length);
128 }
129
130 inline void File::close()
131 {
132     if (m_isOpened) {
133         rawClose();
134         m_isOpened = false;
135     }
136 }
137
138 inline void File::flush(void)
139 {
140     rawFlush();
141 }
142
143 inline int File::getc()
144 {
145     if (!m_isOpened || m_mode != File::Read) {
146         return -1;
147     }
148     return rawGetc();
149 }
150
151 class ZLibFile : public File {
152 public:
153     ZLibFile(const std::string &filename = std::string(),
154              File::Mode mode = File::Read);
155     virtual ~ZLibFile();
156
157
158     virtual bool supportsOffsets() const;
159 protected:
160     virtual bool rawOpen(const std::string &filename, File::Mode mode);
161     virtual bool rawWrite(const void *buffer, int length);
162     virtual bool rawRead(void *buffer, int length);
163     virtual int rawGetc();
164     virtual void rawClose();
165     virtual void rawFlush();
166 private:
167     void *m_gzFile;
168 };
169
170 }
171
172 #endif