blob: 2129d1e388f197b9e2cd7e844ddcf836b3e4cbdc [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
34namespace Trace {
35
36class File {
37public:
38 enum Mode {
39 Read,
40 Write
41 };
Zack Rusin712429a2011-08-25 23:22:30 -040042 struct Offset {
43 Offset()
44 : chunk(0),
45 offsetInChunk(0)
46 {}
47 uint64_t chunk;
48 uint32_t offsetInChunk;
49 };
50
Zack Rusin5ce45e72011-08-05 13:43:46 -040051public:
Zack Rusin14b78f82011-08-06 19:26:46 -040052 static bool isZLibCompressed(const std::string &filename);
Zack Rusin6dce37c2011-08-08 09:59:58 -040053 static bool isSnappyCompressed(const std::string &filename);
Zack Rusin14b78f82011-08-06 19:26:46 -040054public:
Zack Rusin5ce45e72011-08-05 13:43:46 -040055 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;
Zack Rusin124cd342011-08-24 21:54:56 -040061
Zack Rusin5ce45e72011-08-05 13:43:46 -040062 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();
José Fonsecaaf17c802011-08-25 15:36:23 +010068 void flush(void);
Zack Rusinbb130e52011-08-06 18:58:39 -040069 int getc();
Zack Rusin5ce45e72011-08-05 13:43:46 -040070
Zack Rusin712429a2011-08-25 23:22:30 -040071 virtual bool supportsOffsets() const = 0;
72 virtual File::Offset currentOffset();
73 virtual void setCurrentOffset(const File::Offset &offset);
Zack Rusin5ce45e72011-08-05 13:43:46 -040074protected:
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;
Zack Rusinbb130e52011-08-06 18:58:39 -040078 virtual int rawGetc() = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040079 virtual void rawClose() = 0;
José Fonsecaaf17c802011-08-25 15:36:23 +010080 virtual void rawFlush() = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040081
82protected:
83 std::string m_filename;
84 File::Mode m_mode;
85 bool m_isOpened;
86};
87
Zack Rusin124cd342011-08-24 21:54:56 -040088inline bool File::isOpened() const
89{
90 return m_isOpened;
91}
92
93inline File::Mode File::mode() const
94{
95 return m_mode;
96}
97
98inline std::string File::filename() const
99{
100 return m_filename;
101}
102
103inline 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
114inline 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
122inline 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
130inline void File::close()
131{
132 if (m_isOpened) {
133 rawClose();
134 m_isOpened = false;
135 }
136}
137
José Fonsecaaf17c802011-08-25 15:36:23 +0100138inline void File::flush(void)
Zack Rusin124cd342011-08-24 21:54:56 -0400139{
José Fonsecaaf17c802011-08-25 15:36:23 +0100140 rawFlush();
Zack Rusin124cd342011-08-24 21:54:56 -0400141}
142
143inline int File::getc()
144{
145 if (!m_isOpened || m_mode != File::Read) {
José Fonseca2d0d8382011-08-26 11:38:36 +0100146 return -1;
Zack Rusin124cd342011-08-24 21:54:56 -0400147 }
148 return rawGetc();
149}
150
Zack Rusin5ce45e72011-08-05 13:43:46 -0400151class ZLibFile : public File {
152public:
153 ZLibFile(const std::string &filename = std::string(),
154 File::Mode mode = File::Read);
155 virtual ~ZLibFile();
156
Zack Rusin712429a2011-08-25 23:22:30 -0400157
158 virtual bool supportsOffsets() const;
Zack Rusin5ce45e72011-08-05 13:43:46 -0400159protected:
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);
Zack Rusinbb130e52011-08-06 18:58:39 -0400163 virtual int rawGetc();
Zack Rusin5ce45e72011-08-05 13:43:46 -0400164 virtual void rawClose();
José Fonsecaaf17c802011-08-25 15:36:23 +0100165 virtual void rawFlush();
Zack Rusin5ce45e72011-08-05 13:43:46 -0400166private:
167 void *m_gzFile;
168};
169
Zack Rusine0df9522011-09-01 01:50:56 -0400170inline bool
171operator<(const File::Offset &one, const File::Offset &two)
172{
173 return one.chunk < two.chunk ||
174 (one.chunk == two.chunk && one.offsetInChunk < two.offsetInChunk);
175}
176
177inline bool
178operator==(const File::Offset &one, const File::Offset &two)
179{
180 return one.chunk == two.chunk &&
181 one.offsetInChunk == two.offsetInChunk;
182}
183
184inline bool
185operator>=(const File::Offset &one, const File::Offset &two)
186{
187 return one.chunk > two.chunk ||
188 (one.chunk == two.chunk && one.offsetInChunk >= two.offsetInChunk);
189}
190
191inline bool
192operator>(const File::Offset &one, const File::Offset &two)
193{
194 return two < one;
195}
196
197inline bool
198operator<=(const File::Offset &one, const File::Offset &two)
199{
200 return two >= one;
201}
202
203
Zack Rusin5ce45e72011-08-05 13:43:46 -0400204}
205
206#endif