blob: 0c176aabe652c4699a707da6f475ba9973c34372 [file] [log] [blame]
José Fonseca9898b332011-08-25 13:31:31 +01001/**************************************************************************
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
Zack Rusin5ce45e72011-08-05 13:43:46 -040027#ifndef TRACE_FILE_HPP
28#define TRACE_FILE_HPP
29
30#include <string>
Zack Rusina26cf3e2011-08-06 16:12:09 -040031#include <fstream>
Zack Rusin712429a2011-08-25 23:22:30 -040032#include <stdint.h>
Zack Rusin5ce45e72011-08-05 13:43:46 -040033
José Fonsecaef701392012-11-18 15:45:27 +000034
35#define SNAPPY_BYTE1 'a'
36#define SNAPPY_BYTE2 't'
37
38
José Fonsecab4a3d142011-10-27 07:43:19 +010039namespace trace {
Zack Rusin5ce45e72011-08-05 13:43:46 -040040
41class File {
42public:
43 enum Mode {
44 Read,
45 Write
46 };
Zack Rusin712429a2011-08-25 23:22:30 -040047 struct Offset {
José Fonseca7b1d0132011-09-11 14:12:12 +010048 Offset(uint64_t _chunk = 0, uint32_t _offsetInChunk = 0)
49 : chunk(_chunk),
50 offsetInChunk(_offsetInChunk)
Zack Rusin712429a2011-08-25 23:22:30 -040051 {}
52 uint64_t chunk;
53 uint32_t offsetInChunk;
54 };
55
Zack Rusin5ce45e72011-08-05 13:43:46 -040056public:
José Fonseca4159a612011-10-26 23:37:01 +010057 static File *createZLib(void);
58 static File *createSnappy(void);
José Fonsecaa3285532011-11-27 12:32:00 +000059 static File *createForRead(const char *filename);
60 static File *createForWrite(const char *filename);
Zack Rusin14b78f82011-08-06 19:26:46 -040061public:
Zack Rusin5ce45e72011-08-05 13:43:46 -040062 File(const std::string &filename = std::string(),
63 File::Mode mode = File::Read);
64 virtual ~File();
65
66 bool isOpened() const;
67 File::Mode mode() const;
Zack Rusin124cd342011-08-24 21:54:56 -040068
Zack Rusin5ce45e72011-08-05 13:43:46 -040069 bool open(const std::string &filename, File::Mode mode);
José Fonseca94194a22011-09-01 11:54:48 +010070 bool write(const void *buffer, size_t length);
José Fonseca76d6c052011-11-27 12:15:32 +000071 size_t read(void *buffer, size_t length);
Zack Rusin5ce45e72011-08-05 13:43:46 -040072 void close();
José Fonsecaaf17c802011-08-25 15:36:23 +010073 void flush(void);
Zack Rusinbb130e52011-08-06 18:58:39 -040074 int getc();
José Fonseca46c0d852011-09-03 13:45:52 +010075 bool skip(size_t length);
Zack Rusin2b1bd4f2011-09-04 16:14:22 -040076 int percentRead();
Zack Rusin5ce45e72011-08-05 13:43:46 -040077
Zack Rusin712429a2011-08-25 23:22:30 -040078 virtual bool supportsOffsets() const = 0;
José Fonseca7b1d0132011-09-11 14:12:12 +010079 virtual File::Offset currentOffset() = 0;
Zack Rusin712429a2011-08-25 23:22:30 -040080 virtual void setCurrentOffset(const File::Offset &offset);
Zack Rusin5ce45e72011-08-05 13:43:46 -040081protected:
82 virtual bool rawOpen(const std::string &filename, File::Mode mode) = 0;
José Fonseca94194a22011-09-01 11:54:48 +010083 virtual bool rawWrite(const void *buffer, size_t length) = 0;
José Fonseca76d6c052011-11-27 12:15:32 +000084 virtual size_t rawRead(void *buffer, size_t length) = 0;
Zack Rusinbb130e52011-08-06 18:58:39 -040085 virtual int rawGetc() = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040086 virtual void rawClose() = 0;
José Fonsecaaf17c802011-08-25 15:36:23 +010087 virtual void rawFlush() = 0;
José Fonseca46c0d852011-09-03 13:45:52 +010088 virtual bool rawSkip(size_t length) = 0;
Zack Rusin2b1bd4f2011-09-04 16:14:22 -040089 virtual int rawPercentRead() = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040090
91protected:
Zack Rusin5ce45e72011-08-05 13:43:46 -040092 File::Mode m_mode;
93 bool m_isOpened;
94};
95
Zack Rusin124cd342011-08-24 21:54:56 -040096inline bool File::isOpened() const
97{
98 return m_isOpened;
99}
100
101inline File::Mode File::mode() const
102{
103 return m_mode;
104}
105
Zack Rusin124cd342011-08-24 21:54:56 -0400106inline bool File::open(const std::string &filename, File::Mode mode)
107{
108 if (m_isOpened) {
109 close();
110 }
111 m_isOpened = rawOpen(filename, mode);
112 m_mode = mode;
113
114 return m_isOpened;
115}
116
José Fonseca94194a22011-09-01 11:54:48 +0100117inline bool File::write(const void *buffer, size_t length)
Zack Rusin124cd342011-08-24 21:54:56 -0400118{
119 if (!m_isOpened || m_mode != File::Write) {
120 return false;
121 }
122 return rawWrite(buffer, length);
123}
124
José Fonseca76d6c052011-11-27 12:15:32 +0000125inline size_t File::read(void *buffer, size_t length)
Zack Rusin124cd342011-08-24 21:54:56 -0400126{
127 if (!m_isOpened || m_mode != File::Read) {
José Fonseca76d6c052011-11-27 12:15:32 +0000128 return 0;
Zack Rusin124cd342011-08-24 21:54:56 -0400129 }
130 return rawRead(buffer, length);
131}
132
Zack Rusin2b1bd4f2011-09-04 16:14:22 -0400133inline int File::percentRead()
134{
135 if (!m_isOpened || m_mode != File::Read) {
136 return 0;
137 }
138 return rawPercentRead();
139}
140
Zack Rusin124cd342011-08-24 21:54:56 -0400141inline void File::close()
142{
143 if (m_isOpened) {
144 rawClose();
145 m_isOpened = false;
146 }
147}
148
José Fonsecaaf17c802011-08-25 15:36:23 +0100149inline void File::flush(void)
Zack Rusin124cd342011-08-24 21:54:56 -0400150{
José Fonsecaf3acd092011-09-03 13:30:16 +0100151 if (m_mode == File::Write) {
152 rawFlush();
153 }
Zack Rusin124cd342011-08-24 21:54:56 -0400154}
155
156inline int File::getc()
157{
158 if (!m_isOpened || m_mode != File::Read) {
José Fonseca2d0d8382011-08-26 11:38:36 +0100159 return -1;
Zack Rusin124cd342011-08-24 21:54:56 -0400160 }
161 return rawGetc();
162}
163
José Fonseca46c0d852011-09-03 13:45:52 +0100164inline bool File::skip(size_t length)
Zack Rusin46c4a322011-09-02 01:08:49 -0400165{
166 if (!m_isOpened || m_mode != File::Read) {
167 return false;
168 }
169 return rawSkip(length);
170}
171
Zack Rusin5ce45e72011-08-05 13:43:46 -0400172
Zack Rusine0df9522011-09-01 01:50:56 -0400173inline bool
174operator<(const File::Offset &one, const File::Offset &two)
175{
176 return one.chunk < two.chunk ||
177 (one.chunk == two.chunk && one.offsetInChunk < two.offsetInChunk);
178}
179
180inline bool
181operator==(const File::Offset &one, const File::Offset &two)
182{
183 return one.chunk == two.chunk &&
184 one.offsetInChunk == two.offsetInChunk;
185}
186
187inline bool
188operator>=(const File::Offset &one, const File::Offset &two)
189{
190 return one.chunk > two.chunk ||
191 (one.chunk == two.chunk && one.offsetInChunk >= two.offsetInChunk);
192}
193
194inline bool
195operator>(const File::Offset &one, const File::Offset &two)
196{
197 return two < one;
198}
199
200inline bool
201operator<=(const File::Offset &one, const File::Offset &two)
202{
203 return two >= one;
204}
205
206
José Fonsecaef701392012-11-18 15:45:27 +0000207} /* namespace trace */
Zack Rusin5ce45e72011-08-05 13:43:46 -0400208
209#endif