blob: 26b525c3e04d1e1ed0751259a14f5bcc56c283f4 [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);
Jose Fonsecaa77f2922016-03-23 12:59:48 +000048 static File *createBrotli(void);
José Fonseca4159a612011-10-26 23:37:01 +010049 static File *createSnappy(void);
José Fonsecaa3285532011-11-27 12:32:00 +000050 static File *createForRead(const char *filename);
Zack Rusin14b78f82011-08-06 19:26:46 -040051public:
Jose Fonsecae1dd9152016-03-23 11:01:39 +000052 File(void);
Zack Rusin5ce45e72011-08-05 13:43:46 -040053 virtual ~File();
54
Jose Fonseca3a372a32016-03-23 12:11:04 +000055 bool isOpened(void) const;
Zack Rusin124cd342011-08-24 21:54:56 -040056
Jose Fonsecae1dd9152016-03-23 11:01:39 +000057 bool open(const char *filename);
José Fonseca76d6c052011-11-27 12:15:32 +000058 size_t read(void *buffer, size_t length);
Jose Fonseca3a372a32016-03-23 12:11:04 +000059 void close(void);
60 int getc(void);
José Fonseca46c0d852011-09-03 13:45:52 +010061 bool skip(size_t length);
Jose Fonseca3a372a32016-03-23 12:11:04 +000062 int percentRead(void);
Zack Rusin5ce45e72011-08-05 13:43:46 -040063
Jose Fonseca9c8e9952016-03-25 09:19:31 +000064 virtual bool supportsOffsets(void) const;
65 virtual File::Offset currentOffset(void) const;
Zack Rusin712429a2011-08-25 23:22:30 -040066 virtual void setCurrentOffset(const File::Offset &offset);
Zack Rusin5ce45e72011-08-05 13:43:46 -040067protected:
Jose Fonsecae1dd9152016-03-23 11:01:39 +000068 virtual bool rawOpen(const char *filename) = 0;
José Fonseca76d6c052011-11-27 12:15:32 +000069 virtual size_t rawRead(void *buffer, size_t length) = 0;
Jose Fonseca3a372a32016-03-23 12:11:04 +000070 virtual int rawGetc(void) = 0;
71 virtual void rawClose(void) = 0;
José Fonseca46c0d852011-09-03 13:45:52 +010072 virtual bool rawSkip(size_t length) = 0;
Jose Fonseca3a372a32016-03-23 12:11:04 +000073 virtual int rawPercentRead(void) = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040074
75protected:
Jose Fonsecae1dd9152016-03-23 11:01:39 +000076 bool m_isOpened = false;
Zack Rusin5ce45e72011-08-05 13:43:46 -040077};
78
Jose Fonseca3a372a32016-03-23 12:11:04 +000079inline bool File::isOpened(void) const
Zack Rusin124cd342011-08-24 21:54:56 -040080{
81 return m_isOpened;
82}
83
Jose Fonsecae1dd9152016-03-23 11:01:39 +000084inline bool File::open(const char *filename)
Zack Rusin124cd342011-08-24 21:54:56 -040085{
86 if (m_isOpened) {
87 close();
88 }
Jose Fonsecace2ed372015-11-07 23:01:11 +000089 m_isOpened = rawOpen(filename);
Zack Rusin124cd342011-08-24 21:54:56 -040090
91 return m_isOpened;
92}
93
José Fonseca76d6c052011-11-27 12:15:32 +000094inline size_t File::read(void *buffer, size_t length)
Zack Rusin124cd342011-08-24 21:54:56 -040095{
Jose Fonsecace2ed372015-11-07 23:01:11 +000096 if (!m_isOpened) {
José Fonseca76d6c052011-11-27 12:15:32 +000097 return 0;
Zack Rusin124cd342011-08-24 21:54:56 -040098 }
99 return rawRead(buffer, length);
100}
101
Jose Fonseca3a372a32016-03-23 12:11:04 +0000102inline int File::percentRead(void)
Zack Rusin2b1bd4f2011-09-04 16:14:22 -0400103{
Jose Fonsecace2ed372015-11-07 23:01:11 +0000104 if (!m_isOpened) {
Zack Rusin2b1bd4f2011-09-04 16:14:22 -0400105 return 0;
106 }
107 return rawPercentRead();
108}
109
Jose Fonseca3a372a32016-03-23 12:11:04 +0000110inline void File::close(void)
Zack Rusin124cd342011-08-24 21:54:56 -0400111{
112 if (m_isOpened) {
113 rawClose();
114 m_isOpened = false;
115 }
116}
117
Jose Fonseca3a372a32016-03-23 12:11:04 +0000118inline int File::getc(void)
Zack Rusin124cd342011-08-24 21:54:56 -0400119{
Jose Fonsecace2ed372015-11-07 23:01:11 +0000120 if (!m_isOpened) {
José Fonseca2d0d8382011-08-26 11:38:36 +0100121 return -1;
Zack Rusin124cd342011-08-24 21:54:56 -0400122 }
123 return rawGetc();
124}
125
José Fonseca46c0d852011-09-03 13:45:52 +0100126inline bool File::skip(size_t length)
Zack Rusin46c4a322011-09-02 01:08:49 -0400127{
Jose Fonsecace2ed372015-11-07 23:01:11 +0000128 if (!m_isOpened) {
Zack Rusin46c4a322011-09-02 01:08:49 -0400129 return false;
130 }
131 return rawSkip(length);
132}
133
Zack Rusin5ce45e72011-08-05 13:43:46 -0400134
Zack Rusine0df9522011-09-01 01:50:56 -0400135inline bool
136operator<(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
142inline bool
143operator==(const File::Offset &one, const File::Offset &two)
144{
145 return one.chunk == two.chunk &&
146 one.offsetInChunk == two.offsetInChunk;
147}
148
149inline bool
150operator>=(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
156inline bool
157operator>(const File::Offset &one, const File::Offset &two)
158{
159 return two < one;
160}
161
162inline bool
163operator<=(const File::Offset &one, const File::Offset &two)
164{
165 return two >= one;
166}
167
168
José Fonsecaef701392012-11-18 15:45:27 +0000169} /* namespace trace */