1 /**************************************************************************
3 * Copyright 2011 Zack Rusin
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
27 #ifndef TRACE_FILE_HPP
28 #define TRACE_FILE_HPP
35 #define SNAPPY_BYTE1 'a'
36 #define SNAPPY_BYTE2 't'
48 Offset(uint64_t _chunk = 0, uint32_t _offsetInChunk = 0)
50 offsetInChunk(_offsetInChunk)
53 uint32_t offsetInChunk;
57 static File *createZLib(void);
58 static File *createSnappy(void);
59 static File *createForRead(const char *filename);
60 static File *createForWrite(const char *filename);
62 File(const std::string &filename = std::string(),
63 File::Mode mode = File::Read);
66 bool isOpened() const;
67 File::Mode mode() const;
69 bool open(const std::string &filename, File::Mode mode);
70 bool write(const void *buffer, size_t length);
71 size_t read(void *buffer, size_t length);
75 bool skip(size_t length);
78 virtual bool supportsOffsets() const = 0;
79 virtual File::Offset currentOffset() = 0;
80 virtual void setCurrentOffset(const File::Offset &offset);
82 virtual bool rawOpen(const std::string &filename, File::Mode mode) = 0;
83 virtual bool rawWrite(const void *buffer, size_t length) = 0;
84 virtual size_t rawRead(void *buffer, size_t length) = 0;
85 virtual int rawGetc() = 0;
86 virtual void rawClose() = 0;
87 virtual void rawFlush() = 0;
88 virtual bool rawSkip(size_t length) = 0;
89 virtual int rawPercentRead() = 0;
96 inline bool File::isOpened() const
101 inline File::Mode File::mode() const
106 inline bool File::open(const std::string &filename, File::Mode mode)
111 m_isOpened = rawOpen(filename, mode);
117 inline bool File::write(const void *buffer, size_t length)
119 if (!m_isOpened || m_mode != File::Write) {
122 return rawWrite(buffer, length);
125 inline size_t File::read(void *buffer, size_t length)
127 if (!m_isOpened || m_mode != File::Read) {
130 return rawRead(buffer, length);
133 inline int File::percentRead()
135 if (!m_isOpened || m_mode != File::Read) {
138 return rawPercentRead();
141 inline void File::close()
149 inline void File::flush(void)
151 if (m_mode == File::Write) {
156 inline int File::getc()
158 if (!m_isOpened || m_mode != File::Read) {
164 inline bool File::skip(size_t length)
166 if (!m_isOpened || m_mode != File::Read) {
169 return rawSkip(length);
174 operator<(const File::Offset &one, const File::Offset &two)
176 return one.chunk < two.chunk ||
177 (one.chunk == two.chunk && one.offsetInChunk < two.offsetInChunk);
181 operator==(const File::Offset &one, const File::Offset &two)
183 return one.chunk == two.chunk &&
184 one.offsetInChunk == two.offsetInChunk;
188 operator>=(const File::Offset &one, const File::Offset &two)
190 return one.chunk > two.chunk ||
191 (one.chunk == two.chunk && one.offsetInChunk >= two.offsetInChunk);
195 operator>(const File::Offset &one, const File::Offset &two)
201 operator<=(const File::Offset &one, const File::Offset &two)
207 } /* namespace trace */