blob: 5ad03fc732a4acdbf2b6177227a4218a4640f4ee [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
Jose Fonseca9653f952015-05-19 16:32:43 +010027#pragma once
Zack Rusin5ce45e72011-08-05 13:43:46 -040028
29#include <string>
Zack Rusina26cf3e2011-08-06 16:12:09 -040030#include <fstream>
Zack Rusin712429a2011-08-25 23:22:30 -040031#include <stdint.h>
Zack Rusin5ce45e72011-08-05 13:43:46 -040032
José Fonsecaef701392012-11-18 15:45:27 +000033
34#define SNAPPY_BYTE1 'a'
35#define SNAPPY_BYTE2 't'
36
37
José Fonsecab4a3d142011-10-27 07:43:19 +010038namespace trace {
Zack Rusin5ce45e72011-08-05 13:43:46 -040039
40class File {
41public:
42 enum Mode {
43 Read,
44 Write
45 };
Zack Rusin712429a2011-08-25 23:22:30 -040046 struct Offset {
José Fonseca7b1d0132011-09-11 14:12:12 +010047 Offset(uint64_t _chunk = 0, uint32_t _offsetInChunk = 0)
48 : chunk(_chunk),
49 offsetInChunk(_offsetInChunk)
Zack Rusin712429a2011-08-25 23:22:30 -040050 {}
51 uint64_t chunk;
52 uint32_t offsetInChunk;
53 };
54
Zack Rusin5ce45e72011-08-05 13:43:46 -040055public:
José Fonseca4159a612011-10-26 23:37:01 +010056 static File *createZLib(void);
57 static File *createSnappy(void);
José Fonsecaa3285532011-11-27 12:32:00 +000058 static File *createForRead(const char *filename);
59 static File *createForWrite(const char *filename);
Zack Rusin14b78f82011-08-06 19:26:46 -040060public:
Zack Rusin5ce45e72011-08-05 13:43:46 -040061 File(const std::string &filename = std::string(),
62 File::Mode mode = File::Read);
63 virtual ~File();
64
65 bool isOpened() const;
66 File::Mode mode() const;
Zack Rusin124cd342011-08-24 21:54:56 -040067
Zack Rusin5ce45e72011-08-05 13:43:46 -040068 bool open(const std::string &filename, File::Mode mode);
José Fonseca94194a22011-09-01 11:54:48 +010069 bool write(const void *buffer, size_t length);
José Fonseca76d6c052011-11-27 12:15:32 +000070 size_t read(void *buffer, size_t length);
Zack Rusin5ce45e72011-08-05 13:43:46 -040071 void close();
José Fonsecaaf17c802011-08-25 15:36:23 +010072 void flush(void);
Zack Rusinbb130e52011-08-06 18:58:39 -040073 int getc();
José Fonseca46c0d852011-09-03 13:45:52 +010074 bool skip(size_t length);
Zack Rusin2b1bd4f2011-09-04 16:14:22 -040075 int percentRead();
Zack Rusin5ce45e72011-08-05 13:43:46 -040076
Zack Rusin712429a2011-08-25 23:22:30 -040077 virtual bool supportsOffsets() const = 0;
José Fonseca7b1d0132011-09-11 14:12:12 +010078 virtual File::Offset currentOffset() = 0;
Zack Rusin712429a2011-08-25 23:22:30 -040079 virtual void setCurrentOffset(const File::Offset &offset);
Zack Rusin5ce45e72011-08-05 13:43:46 -040080protected:
81 virtual bool rawOpen(const std::string &filename, File::Mode mode) = 0;
José Fonseca94194a22011-09-01 11:54:48 +010082 virtual bool rawWrite(const void *buffer, size_t length) = 0;
José Fonseca76d6c052011-11-27 12:15:32 +000083 virtual size_t rawRead(void *buffer, size_t length) = 0;
Zack Rusinbb130e52011-08-06 18:58:39 -040084 virtual int rawGetc() = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040085 virtual void rawClose() = 0;
José Fonsecaaf17c802011-08-25 15:36:23 +010086 virtual void rawFlush() = 0;
José Fonseca46c0d852011-09-03 13:45:52 +010087 virtual bool rawSkip(size_t length) = 0;
Zack Rusin2b1bd4f2011-09-04 16:14:22 -040088 virtual int rawPercentRead() = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040089
90protected:
Zack Rusin5ce45e72011-08-05 13:43:46 -040091 File::Mode m_mode;
92 bool m_isOpened;
93};
94
Zack Rusin124cd342011-08-24 21:54:56 -040095inline bool File::isOpened() const
96{
97 return m_isOpened;
98}
99
100inline File::Mode File::mode() const
101{
102 return m_mode;
103}
104
Zack Rusin124cd342011-08-24 21:54:56 -0400105inline bool File::open(const std::string &filename, File::Mode mode)
106{
107 if (m_isOpened) {
108 close();
109 }
110 m_isOpened = rawOpen(filename, mode);
111 m_mode = mode;
112
113 return m_isOpened;
114}
115
José Fonseca94194a22011-09-01 11:54:48 +0100116inline bool File::write(const void *buffer, size_t length)
Zack Rusin124cd342011-08-24 21:54:56 -0400117{
118 if (!m_isOpened || m_mode != File::Write) {
119 return false;
120 }
121 return rawWrite(buffer, length);
122}
123
José Fonseca76d6c052011-11-27 12:15:32 +0000124inline size_t File::read(void *buffer, size_t length)
Zack Rusin124cd342011-08-24 21:54:56 -0400125{
126 if (!m_isOpened || m_mode != File::Read) {
José Fonseca76d6c052011-11-27 12:15:32 +0000127 return 0;
Zack Rusin124cd342011-08-24 21:54:56 -0400128 }
129 return rawRead(buffer, length);
130}
131
Zack Rusin2b1bd4f2011-09-04 16:14:22 -0400132inline int File::percentRead()
133{
134 if (!m_isOpened || m_mode != File::Read) {
135 return 0;
136 }
137 return rawPercentRead();
138}
139
Zack Rusin124cd342011-08-24 21:54:56 -0400140inline void File::close()
141{
142 if (m_isOpened) {
143 rawClose();
144 m_isOpened = false;
145 }
146}
147
José Fonsecaaf17c802011-08-25 15:36:23 +0100148inline void File::flush(void)
Zack Rusin124cd342011-08-24 21:54:56 -0400149{
José Fonsecaf3acd092011-09-03 13:30:16 +0100150 if (m_mode == File::Write) {
151 rawFlush();
152 }
Zack Rusin124cd342011-08-24 21:54:56 -0400153}
154
155inline int File::getc()
156{
157 if (!m_isOpened || m_mode != File::Read) {
José Fonseca2d0d8382011-08-26 11:38:36 +0100158 return -1;
Zack Rusin124cd342011-08-24 21:54:56 -0400159 }
160 return rawGetc();
161}
162
José Fonseca46c0d852011-09-03 13:45:52 +0100163inline bool File::skip(size_t length)
Zack Rusin46c4a322011-09-02 01:08:49 -0400164{
165 if (!m_isOpened || m_mode != File::Read) {
166 return false;
167 }
168 return rawSkip(length);
169}
170
Zack Rusin5ce45e72011-08-05 13:43:46 -0400171
Zack Rusine0df9522011-09-01 01:50:56 -0400172inline bool
173operator<(const File::Offset &one, const File::Offset &two)
174{
175 return one.chunk < two.chunk ||
176 (one.chunk == two.chunk && one.offsetInChunk < two.offsetInChunk);
177}
178
179inline bool
180operator==(const File::Offset &one, const File::Offset &two)
181{
182 return one.chunk == two.chunk &&
183 one.offsetInChunk == two.offsetInChunk;
184}
185
186inline bool
187operator>=(const File::Offset &one, const File::Offset &two)
188{
189 return one.chunk > two.chunk ||
190 (one.chunk == two.chunk && one.offsetInChunk >= two.offsetInChunk);
191}
192
193inline bool
194operator>(const File::Offset &one, const File::Offset &two)
195{
196 return two < one;
197}
198
199inline bool
200operator<=(const File::Offset &one, const File::Offset &two)
201{
202 return two >= one;
203}
204
205
José Fonsecaef701392012-11-18 15:45:27 +0000206} /* namespace trace */