blob: 54df3ea1f24622b608aabf823432e5adf1d3de57 [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
Zack Rusina26cf3e2011-08-06 16:12:09 -040029#include <fstream>
Zack Rusin712429a2011-08-25 23:22:30 -040030#include <stdint.h>
Zack Rusin5ce45e72011-08-05 13:43:46 -040031
José Fonsecaef701392012-11-18 15:45:27 +000032
José Fonsecab4a3d142011-10-27 07:43:19 +010033namespace trace {
Zack Rusin5ce45e72011-08-05 13:43:46 -040034
35class File {
36public:
Zack Rusin712429a2011-08-25 23:22:30 -040037 struct Offset {
José Fonseca7b1d0132011-09-11 14:12:12 +010038 Offset(uint64_t _chunk = 0, uint32_t _offsetInChunk = 0)
39 : chunk(_chunk),
40 offsetInChunk(_offsetInChunk)
Zack Rusin712429a2011-08-25 23:22:30 -040041 {}
42 uint64_t chunk;
43 uint32_t offsetInChunk;
44 };
45
Zack Rusin5ce45e72011-08-05 13:43:46 -040046public:
José Fonseca4159a612011-10-26 23:37:01 +010047 static File *createZLib(void);
48 static File *createSnappy(void);
José Fonsecaa3285532011-11-27 12:32:00 +000049 static File *createForRead(const char *filename);
Zack Rusin14b78f82011-08-06 19:26:46 -040050public:
Jose Fonsecae1dd9152016-03-23 11:01:39 +000051 File(void);
Zack Rusin5ce45e72011-08-05 13:43:46 -040052 virtual ~File();
53
54 bool isOpened() const;
Zack Rusin124cd342011-08-24 21:54:56 -040055
Jose Fonsecae1dd9152016-03-23 11:01:39 +000056 bool open(const char *filename);
José Fonseca76d6c052011-11-27 12:15:32 +000057 size_t read(void *buffer, size_t length);
Zack Rusin5ce45e72011-08-05 13:43:46 -040058 void close();
Zack Rusinbb130e52011-08-06 18:58:39 -040059 int getc();
José Fonseca46c0d852011-09-03 13:45:52 +010060 bool skip(size_t length);
Zack Rusin2b1bd4f2011-09-04 16:14:22 -040061 int percentRead();
Zack Rusin5ce45e72011-08-05 13:43:46 -040062
Zack Rusin712429a2011-08-25 23:22:30 -040063 virtual bool supportsOffsets() const = 0;
José Fonseca7b1d0132011-09-11 14:12:12 +010064 virtual File::Offset currentOffset() = 0;
Zack Rusin712429a2011-08-25 23:22:30 -040065 virtual void setCurrentOffset(const File::Offset &offset);
Zack Rusin5ce45e72011-08-05 13:43:46 -040066protected:
Jose Fonsecae1dd9152016-03-23 11:01:39 +000067 virtual bool rawOpen(const char *filename) = 0;
José Fonseca76d6c052011-11-27 12:15:32 +000068 virtual size_t rawRead(void *buffer, size_t length) = 0;
Zack Rusinbb130e52011-08-06 18:58:39 -040069 virtual int rawGetc() = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040070 virtual void rawClose() = 0;
José Fonseca46c0d852011-09-03 13:45:52 +010071 virtual bool rawSkip(size_t length) = 0;
Zack Rusin2b1bd4f2011-09-04 16:14:22 -040072 virtual int rawPercentRead() = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040073
74protected:
Jose Fonsecae1dd9152016-03-23 11:01:39 +000075 bool m_isOpened = false;
Zack Rusin5ce45e72011-08-05 13:43:46 -040076};
77
Zack Rusin124cd342011-08-24 21:54:56 -040078inline bool File::isOpened() const
79{
80 return m_isOpened;
81}
82
Jose Fonsecae1dd9152016-03-23 11:01:39 +000083inline bool File::open(const char *filename)
Zack Rusin124cd342011-08-24 21:54:56 -040084{
85 if (m_isOpened) {
86 close();
87 }
Jose Fonsecace2ed372015-11-07 23:01:11 +000088 m_isOpened = rawOpen(filename);
Zack Rusin124cd342011-08-24 21:54:56 -040089
90 return m_isOpened;
91}
92
José Fonseca76d6c052011-11-27 12:15:32 +000093inline size_t File::read(void *buffer, size_t length)
Zack Rusin124cd342011-08-24 21:54:56 -040094{
Jose Fonsecace2ed372015-11-07 23:01:11 +000095 if (!m_isOpened) {
José Fonseca76d6c052011-11-27 12:15:32 +000096 return 0;
Zack Rusin124cd342011-08-24 21:54:56 -040097 }
98 return rawRead(buffer, length);
99}
100
Zack Rusin2b1bd4f2011-09-04 16:14:22 -0400101inline int File::percentRead()
102{
Jose Fonsecace2ed372015-11-07 23:01:11 +0000103 if (!m_isOpened) {
Zack Rusin2b1bd4f2011-09-04 16:14:22 -0400104 return 0;
105 }
106 return rawPercentRead();
107}
108
Zack Rusin124cd342011-08-24 21:54:56 -0400109inline void File::close()
110{
111 if (m_isOpened) {
112 rawClose();
113 m_isOpened = false;
114 }
115}
116
Zack Rusin124cd342011-08-24 21:54:56 -0400117inline int File::getc()
118{
Jose Fonsecace2ed372015-11-07 23:01:11 +0000119 if (!m_isOpened) {
José Fonseca2d0d8382011-08-26 11:38:36 +0100120 return -1;
Zack Rusin124cd342011-08-24 21:54:56 -0400121 }
122 return rawGetc();
123}
124
José Fonseca46c0d852011-09-03 13:45:52 +0100125inline bool File::skip(size_t length)
Zack Rusin46c4a322011-09-02 01:08:49 -0400126{
Jose Fonsecace2ed372015-11-07 23:01:11 +0000127 if (!m_isOpened) {
Zack Rusin46c4a322011-09-02 01:08:49 -0400128 return false;
129 }
130 return rawSkip(length);
131}
132
Zack Rusin5ce45e72011-08-05 13:43:46 -0400133
Zack Rusine0df9522011-09-01 01:50:56 -0400134inline bool
135operator<(const File::Offset &one, const File::Offset &two)
136{
137 return one.chunk < two.chunk ||
138 (one.chunk == two.chunk && one.offsetInChunk < two.offsetInChunk);
139}
140
141inline bool
142operator==(const File::Offset &one, const File::Offset &two)
143{
144 return one.chunk == two.chunk &&
145 one.offsetInChunk == two.offsetInChunk;
146}
147
148inline bool
149operator>=(const File::Offset &one, const File::Offset &two)
150{
151 return one.chunk > two.chunk ||
152 (one.chunk == two.chunk && one.offsetInChunk >= two.offsetInChunk);
153}
154
155inline bool
156operator>(const File::Offset &one, const File::Offset &two)
157{
158 return two < one;
159}
160
161inline bool
162operator<=(const File::Offset &one, const File::Offset &two)
163{
164 return two >= one;
165}
166
167
José Fonsecaef701392012-11-18 15:45:27 +0000168} /* namespace trace */