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 | |
Jose Fonseca | 9653f95 | 2015-05-19 16:32:43 +0100 | [diff] [blame] | 27 | #pragma once |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 28 | |
| 29 | #include <string> |
Zack Rusin | a26cf3e | 2011-08-06 16:12:09 -0400 | [diff] [blame] | 30 | #include <fstream> |
Zack Rusin | 712429a | 2011-08-25 23:22:30 -0400 | [diff] [blame] | 31 | #include <stdint.h> |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 32 | |
José Fonseca | ef70139 | 2012-11-18 15:45:27 +0000 | [diff] [blame] | 33 | |
José Fonseca | b4a3d14 | 2011-10-27 07:43:19 +0100 | [diff] [blame] | 34 | namespace trace { |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 35 | |
| 36 | class File { |
| 37 | public: |
Zack Rusin | 712429a | 2011-08-25 23:22:30 -0400 | [diff] [blame] | 38 | struct Offset { |
José Fonseca | 7b1d013 | 2011-09-11 14:12:12 +0100 | [diff] [blame] | 39 | Offset(uint64_t _chunk = 0, uint32_t _offsetInChunk = 0) |
| 40 | : chunk(_chunk), |
| 41 | offsetInChunk(_offsetInChunk) |
Zack Rusin | 712429a | 2011-08-25 23:22:30 -0400 | [diff] [blame] | 42 | {} |
| 43 | uint64_t chunk; |
| 44 | uint32_t offsetInChunk; |
| 45 | }; |
| 46 | |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 47 | public: |
José Fonseca | 4159a61 | 2011-10-26 23:37:01 +0100 | [diff] [blame] | 48 | static File *createZLib(void); |
| 49 | static File *createSnappy(void); |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 50 | static File *createForRead(const char *filename); |
Zack Rusin | 14b78f8 | 2011-08-06 19:26:46 -0400 | [diff] [blame] | 51 | public: |
Jose Fonseca | ce2ed37 | 2015-11-07 23:01:11 +0000 | [diff] [blame^] | 52 | File(const std::string &filename = std::string()); |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 53 | virtual ~File(); |
| 54 | |
| 55 | bool isOpened() const; |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 56 | |
Jose Fonseca | ce2ed37 | 2015-11-07 23:01:11 +0000 | [diff] [blame^] | 57 | bool open(const std::string &filename); |
José Fonseca | 76d6c05 | 2011-11-27 12:15:32 +0000 | [diff] [blame] | 58 | size_t read(void *buffer, size_t length); |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 59 | void close(); |
Zack Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 60 | int getc(); |
José Fonseca | 46c0d85 | 2011-09-03 13:45:52 +0100 | [diff] [blame] | 61 | bool skip(size_t length); |
Zack Rusin | 2b1bd4f | 2011-09-04 16:14:22 -0400 | [diff] [blame] | 62 | int percentRead(); |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 63 | |
Zack Rusin | 712429a | 2011-08-25 23:22:30 -0400 | [diff] [blame] | 64 | virtual bool supportsOffsets() const = 0; |
José Fonseca | 7b1d013 | 2011-09-11 14:12:12 +0100 | [diff] [blame] | 65 | virtual File::Offset currentOffset() = 0; |
Zack Rusin | 712429a | 2011-08-25 23:22:30 -0400 | [diff] [blame] | 66 | virtual void setCurrentOffset(const File::Offset &offset); |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 67 | protected: |
Jose Fonseca | ce2ed37 | 2015-11-07 23:01:11 +0000 | [diff] [blame^] | 68 | virtual bool rawOpen(const std::string &filename) = 0; |
José Fonseca | 76d6c05 | 2011-11-27 12:15:32 +0000 | [diff] [blame] | 69 | virtual size_t rawRead(void *buffer, size_t length) = 0; |
Zack Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 70 | virtual int rawGetc() = 0; |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 71 | virtual void rawClose() = 0; |
José Fonseca | 46c0d85 | 2011-09-03 13:45:52 +0100 | [diff] [blame] | 72 | virtual bool rawSkip(size_t length) = 0; |
Zack Rusin | 2b1bd4f | 2011-09-04 16:14:22 -0400 | [diff] [blame] | 73 | virtual int rawPercentRead() = 0; |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 74 | |
| 75 | protected: |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 76 | bool m_isOpened; |
| 77 | }; |
| 78 | |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 79 | inline bool File::isOpened() const |
| 80 | { |
| 81 | return m_isOpened; |
| 82 | } |
| 83 | |
Jose Fonseca | ce2ed37 | 2015-11-07 23:01:11 +0000 | [diff] [blame^] | 84 | inline bool File::open(const std::string &filename) |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 85 | { |
| 86 | if (m_isOpened) { |
| 87 | close(); |
| 88 | } |
Jose Fonseca | ce2ed37 | 2015-11-07 23:01:11 +0000 | [diff] [blame^] | 89 | m_isOpened = rawOpen(filename); |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 90 | |
| 91 | return m_isOpened; |
| 92 | } |
| 93 | |
José Fonseca | 76d6c05 | 2011-11-27 12:15:32 +0000 | [diff] [blame] | 94 | inline size_t File::read(void *buffer, size_t length) |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 95 | { |
Jose Fonseca | ce2ed37 | 2015-11-07 23:01:11 +0000 | [diff] [blame^] | 96 | if (!m_isOpened) { |
José Fonseca | 76d6c05 | 2011-11-27 12:15:32 +0000 | [diff] [blame] | 97 | return 0; |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 98 | } |
| 99 | return rawRead(buffer, length); |
| 100 | } |
| 101 | |
Zack Rusin | 2b1bd4f | 2011-09-04 16:14:22 -0400 | [diff] [blame] | 102 | inline int File::percentRead() |
| 103 | { |
Jose Fonseca | ce2ed37 | 2015-11-07 23:01:11 +0000 | [diff] [blame^] | 104 | if (!m_isOpened) { |
Zack Rusin | 2b1bd4f | 2011-09-04 16:14:22 -0400 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | return rawPercentRead(); |
| 108 | } |
| 109 | |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 110 | inline void File::close() |
| 111 | { |
| 112 | if (m_isOpened) { |
| 113 | rawClose(); |
| 114 | m_isOpened = false; |
| 115 | } |
| 116 | } |
| 117 | |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 118 | inline int File::getc() |
| 119 | { |
Jose Fonseca | ce2ed37 | 2015-11-07 23:01:11 +0000 | [diff] [blame^] | 120 | if (!m_isOpened) { |
José Fonseca | 2d0d838 | 2011-08-26 11:38:36 +0100 | [diff] [blame] | 121 | return -1; |
Zack Rusin | 124cd34 | 2011-08-24 21:54:56 -0400 | [diff] [blame] | 122 | } |
| 123 | return rawGetc(); |
| 124 | } |
| 125 | |
José Fonseca | 46c0d85 | 2011-09-03 13:45:52 +0100 | [diff] [blame] | 126 | inline bool File::skip(size_t length) |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 127 | { |
Jose Fonseca | ce2ed37 | 2015-11-07 23:01:11 +0000 | [diff] [blame^] | 128 | if (!m_isOpened) { |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 129 | return false; |
| 130 | } |
| 131 | return rawSkip(length); |
| 132 | } |
| 133 | |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 134 | |
Zack Rusin | e0df952 | 2011-09-01 01:50:56 -0400 | [diff] [blame] | 135 | inline bool |
| 136 | operator<(const File::Offset &one, const File::Offset &two) |
| 137 | { |
| 138 | return one.chunk < two.chunk || |
| 139 | (one.chunk == two.chunk && one.offsetInChunk < two.offsetInChunk); |
| 140 | } |
| 141 | |
| 142 | inline bool |
| 143 | operator==(const File::Offset &one, const File::Offset &two) |
| 144 | { |
| 145 | return one.chunk == two.chunk && |
| 146 | one.offsetInChunk == two.offsetInChunk; |
| 147 | } |
| 148 | |
| 149 | inline bool |
| 150 | operator>=(const File::Offset &one, const File::Offset &two) |
| 151 | { |
| 152 | return one.chunk > two.chunk || |
| 153 | (one.chunk == two.chunk && one.offsetInChunk >= two.offsetInChunk); |
| 154 | } |
| 155 | |
| 156 | inline bool |
| 157 | operator>(const File::Offset &one, const File::Offset &two) |
| 158 | { |
| 159 | return two < one; |
| 160 | } |
| 161 | |
| 162 | inline bool |
| 163 | operator<=(const File::Offset &one, const File::Offset &two) |
| 164 | { |
| 165 | return two >= one; |
| 166 | } |
| 167 | |
| 168 | |
José Fonseca | ef70139 | 2012-11-18 15:45:27 +0000 | [diff] [blame] | 169 | } /* namespace trace */ |