José Fonseca | 9898b33 | 2011-08-25 13:31:31 +0100 | [diff] [blame] | 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 | |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 27 | #ifndef TRACE_FILE_HPP |
| 28 | #define TRACE_FILE_HPP |
| 29 | |
| 30 | #include <string> |
Zack Rusin | a26cf3e | 2011-08-06 16:12:09 -0400 | [diff] [blame] | 31 | #include <fstream> |
Zack Rusin | 712429a | 2011-08-25 23:22:30 -0400 | [diff] [blame] | 32 | #include <stdint.h> |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 33 | |
| 34 | namespace Trace { |
| 35 | |
| 36 | class File { |
| 37 | public: |
| 38 | enum Mode { |
| 39 | Read, |
| 40 | Write |
| 41 | }; |
Zack Rusin | 712429a | 2011-08-25 23:22:30 -0400 | [diff] [blame] | 42 | struct Offset { |
| 43 | Offset() |
| 44 | : chunk(0), |
| 45 | offsetInChunk(0) |
| 46 | {} |
| 47 | uint64_t chunk; |
| 48 | uint32_t offsetInChunk; |
| 49 | }; |
| 50 | |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 51 | public: |
Zack Rusin | 14b78f8 | 2011-08-06 19:26:46 -0400 | [diff] [blame] | 52 | static bool isZLibCompressed(const std::string &filename); |
Zack Rusin | 6dce37c | 2011-08-08 09:59:58 -0400 | [diff] [blame] | 53 | static bool isSnappyCompressed(const std::string &filename); |
Zack Rusin | 14b78f8 | 2011-08-06 19:26:46 -0400 | [diff] [blame] | 54 | public: |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 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; |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 61 | |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 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(); |
José Fonseca | af17c80 | 2011-08-25 15:36:23 +0100 | [diff] [blame] | 68 | void flush(void); |
Zack Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 69 | int getc(); |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 70 | |
Zack Rusin | 712429a | 2011-08-25 23:22:30 -0400 | [diff] [blame] | 71 | virtual bool supportsOffsets() const = 0; |
| 72 | virtual File::Offset currentOffset(); |
| 73 | virtual void setCurrentOffset(const File::Offset &offset); |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 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; |
Zack Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 78 | virtual int rawGetc() = 0; |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 79 | virtual void rawClose() = 0; |
José Fonseca | af17c80 | 2011-08-25 15:36:23 +0100 | [diff] [blame] | 80 | virtual void rawFlush() = 0; |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 81 | |
| 82 | protected: |
| 83 | std::string m_filename; |
| 84 | File::Mode m_mode; |
| 85 | bool m_isOpened; |
| 86 | }; |
| 87 | |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 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 | |
José Fonseca | af17c80 | 2011-08-25 15:36:23 +0100 | [diff] [blame] | 138 | inline void File::flush(void) |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 139 | { |
José Fonseca | af17c80 | 2011-08-25 15:36:23 +0100 | [diff] [blame] | 140 | rawFlush(); |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | inline int File::getc() |
| 144 | { |
| 145 | if (!m_isOpened || m_mode != File::Read) { |
José Fonseca | 2d0d838 | 2011-08-26 11:38:36 +0100 | [diff] [blame] | 146 | return -1; |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 147 | } |
| 148 | return rawGetc(); |
| 149 | } |
| 150 | |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 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 | |
Zack Rusin | 712429a | 2011-08-25 23:22:30 -0400 | [diff] [blame] | 157 | |
| 158 | virtual bool supportsOffsets() const; |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 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); |
Zack Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 163 | virtual int rawGetc(); |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 164 | virtual void rawClose(); |
José Fonseca | af17c80 | 2011-08-25 15:36:23 +0100 | [diff] [blame] | 165 | virtual void rawFlush(); |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 166 | private: |
| 167 | void *m_gzFile; |
| 168 | }; |
| 169 | |
Zack Rusin | e0df952 | 2011-09-01 01:50:56 -0400 | [diff] [blame^] | 170 | inline bool |
| 171 | operator<(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 | |
| 177 | inline bool |
| 178 | operator==(const File::Offset &one, const File::Offset &two) |
| 179 | { |
| 180 | return one.chunk == two.chunk && |
| 181 | one.offsetInChunk == two.offsetInChunk; |
| 182 | } |
| 183 | |
| 184 | inline bool |
| 185 | operator>=(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 | |
| 191 | inline bool |
| 192 | operator>(const File::Offset &one, const File::Offset &two) |
| 193 | { |
| 194 | return two < one; |
| 195 | } |
| 196 | |
| 197 | inline bool |
| 198 | operator<=(const File::Offset &one, const File::Offset &two) |
| 199 | { |
| 200 | return two >= one; |
| 201 | } |
| 202 | |
| 203 | |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | #endif |