blob: 5d70dc829110516b0b1ef9a5adba77933486bf8e [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);
Robert Tarasov67634522020-06-24 16:17:48 -070062 int percentRead(void) const;
63
64 // returns the size of (compressed/serialized) data in the container in bytes
65 virtual size_t containerSizeInBytes(void) const = 0;
66 // returns the amount of bytes read from the container
67 virtual size_t containerBytesRead(void) const = 0;
68 // returns the size of uncompressed data read in bytes
69 virtual size_t dataBytesRead(void) const = 0;
70 // returns the name of the continer type as a user friendly string
71 virtual const char* containerType() const = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040072
Jose Fonseca9c8e9952016-03-25 09:19:31 +000073 virtual bool supportsOffsets(void) const;
74 virtual File::Offset currentOffset(void) const;
Zack Rusin712429a2011-08-25 23:22:30 -040075 virtual void setCurrentOffset(const File::Offset &offset);
Zack Rusin5ce45e72011-08-05 13:43:46 -040076protected:
Jose Fonsecae1dd9152016-03-23 11:01:39 +000077 virtual bool rawOpen(const char *filename) = 0;
José Fonseca76d6c052011-11-27 12:15:32 +000078 virtual size_t rawRead(void *buffer, size_t length) = 0;
Jose Fonseca3a372a32016-03-23 12:11:04 +000079 virtual int rawGetc(void) = 0;
80 virtual void rawClose(void) = 0;
José Fonseca46c0d852011-09-03 13:45:52 +010081 virtual bool rawSkip(size_t length) = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040082
83protected:
Jose Fonsecae1dd9152016-03-23 11:01:39 +000084 bool m_isOpened = false;
Zack Rusin5ce45e72011-08-05 13:43:46 -040085};
86
Jose Fonseca3a372a32016-03-23 12:11:04 +000087inline bool File::isOpened(void) const
Zack Rusin124cd342011-08-24 21:54:56 -040088{
89 return m_isOpened;
90}
91
Jose Fonsecae1dd9152016-03-23 11:01:39 +000092inline bool File::open(const char *filename)
Zack Rusin124cd342011-08-24 21:54:56 -040093{
94 if (m_isOpened) {
95 close();
96 }
Jose Fonsecace2ed372015-11-07 23:01:11 +000097 m_isOpened = rawOpen(filename);
Zack Rusin124cd342011-08-24 21:54:56 -040098
99 return m_isOpened;
100}
101
José Fonseca76d6c052011-11-27 12:15:32 +0000102inline size_t File::read(void *buffer, size_t length)
Zack Rusin124cd342011-08-24 21:54:56 -0400103{
Jose Fonsecace2ed372015-11-07 23:01:11 +0000104 if (!m_isOpened) {
José Fonseca76d6c052011-11-27 12:15:32 +0000105 return 0;
Zack Rusin124cd342011-08-24 21:54:56 -0400106 }
107 return rawRead(buffer, length);
108}
109
Robert Tarasov67634522020-06-24 16:17:48 -0700110inline int File::percentRead(void) const
Zack Rusin2b1bd4f2011-09-04 16:14:22 -0400111{
Robert Tarasov67634522020-06-24 16:17:48 -0700112 size_t size = containerSizeInBytes();
113 size_t read = containerBytesRead();
114 if (size) {
115 return static_cast<int>((100*read)/size);
Zack Rusin2b1bd4f2011-09-04 16:14:22 -0400116 }
Robert Tarasov67634522020-06-24 16:17:48 -0700117 return 0;
Zack Rusin2b1bd4f2011-09-04 16:14:22 -0400118}
119
Jose Fonseca3a372a32016-03-23 12:11:04 +0000120inline void File::close(void)
Zack Rusin124cd342011-08-24 21:54:56 -0400121{
122 if (m_isOpened) {
123 rawClose();
124 m_isOpened = false;
125 }
126}
127
Jose Fonseca3a372a32016-03-23 12:11:04 +0000128inline int File::getc(void)
Zack Rusin124cd342011-08-24 21:54:56 -0400129{
Jose Fonsecace2ed372015-11-07 23:01:11 +0000130 if (!m_isOpened) {
José Fonseca2d0d8382011-08-26 11:38:36 +0100131 return -1;
Zack Rusin124cd342011-08-24 21:54:56 -0400132 }
133 return rawGetc();
134}
135
José Fonseca46c0d852011-09-03 13:45:52 +0100136inline bool File::skip(size_t length)
Zack Rusin46c4a322011-09-02 01:08:49 -0400137{
Jose Fonsecace2ed372015-11-07 23:01:11 +0000138 if (!m_isOpened) {
Zack Rusin46c4a322011-09-02 01:08:49 -0400139 return false;
140 }
141 return rawSkip(length);
142}
143
Zack Rusin5ce45e72011-08-05 13:43:46 -0400144
Zack Rusine0df9522011-09-01 01:50:56 -0400145inline bool
146operator<(const File::Offset &one, const File::Offset &two)
147{
148 return one.chunk < two.chunk ||
149 (one.chunk == two.chunk && one.offsetInChunk < two.offsetInChunk);
150}
151
152inline bool
153operator==(const File::Offset &one, const File::Offset &two)
154{
155 return one.chunk == two.chunk &&
156 one.offsetInChunk == two.offsetInChunk;
157}
158
159inline bool
160operator>=(const File::Offset &one, const File::Offset &two)
161{
162 return one.chunk > two.chunk ||
163 (one.chunk == two.chunk && one.offsetInChunk >= two.offsetInChunk);
164}
165
166inline bool
167operator>(const File::Offset &one, const File::Offset &two)
168{
169 return two < one;
170}
171
172inline bool
173operator<=(const File::Offset &one, const File::Offset &two)
174{
175 return two >= one;
176}
177
178
José Fonsecaef701392012-11-18 15:45:27 +0000179} /* namespace trace */