blob: 17e6756b74b40a1b644091585eda9bd08d8e7b67 [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
29#include <string>
Zack Rusina26cf3e2011-08-06 16:12:09 -040030#include <fstream>
Zack Rusin712429a2011-08-25 23:22:30 -040031#include <stdint.h>
Zack Rusin5ce45e72011-08-05 13:43:46 -040032
José Fonsecaef701392012-11-18 15:45:27 +000033
José Fonsecab4a3d142011-10-27 07:43:19 +010034namespace trace {
Zack Rusin5ce45e72011-08-05 13:43:46 -040035
36class File {
37public:
Zack Rusin712429a2011-08-25 23:22:30 -040038 struct Offset {
José Fonseca7b1d0132011-09-11 14:12:12 +010039 Offset(uint64_t _chunk = 0, uint32_t _offsetInChunk = 0)
40 : chunk(_chunk),
41 offsetInChunk(_offsetInChunk)
Zack Rusin712429a2011-08-25 23:22:30 -040042 {}
43 uint64_t chunk;
44 uint32_t offsetInChunk;
45 };
46
Zack Rusin5ce45e72011-08-05 13:43:46 -040047public:
José Fonseca4159a612011-10-26 23:37:01 +010048 static File *createZLib(void);
49 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 Fonsecace2ed372015-11-07 23:01:11 +000052 File(const std::string &filename = std::string());
Zack Rusin5ce45e72011-08-05 13:43:46 -040053 virtual ~File();
54
55 bool isOpened() const;
Zack Rusin124cd342011-08-24 21:54:56 -040056
Jose Fonsecace2ed372015-11-07 23:01:11 +000057 bool open(const std::string &filename);
José Fonseca76d6c052011-11-27 12:15:32 +000058 size_t read(void *buffer, size_t length);
Zack Rusin5ce45e72011-08-05 13:43:46 -040059 void close();
Zack Rusinbb130e52011-08-06 18:58:39 -040060 int getc();
José Fonseca46c0d852011-09-03 13:45:52 +010061 bool skip(size_t length);
Zack Rusin2b1bd4f2011-09-04 16:14:22 -040062 int percentRead();
Zack Rusin5ce45e72011-08-05 13:43:46 -040063
Zack Rusin712429a2011-08-25 23:22:30 -040064 virtual bool supportsOffsets() const = 0;
José Fonseca7b1d0132011-09-11 14:12:12 +010065 virtual File::Offset currentOffset() = 0;
Zack Rusin712429a2011-08-25 23:22:30 -040066 virtual void setCurrentOffset(const File::Offset &offset);
Zack Rusin5ce45e72011-08-05 13:43:46 -040067protected:
Jose Fonsecace2ed372015-11-07 23:01:11 +000068 virtual bool rawOpen(const std::string &filename) = 0;
José Fonseca76d6c052011-11-27 12:15:32 +000069 virtual size_t rawRead(void *buffer, size_t length) = 0;
Zack Rusinbb130e52011-08-06 18:58:39 -040070 virtual int rawGetc() = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040071 virtual void rawClose() = 0;
José Fonseca46c0d852011-09-03 13:45:52 +010072 virtual bool rawSkip(size_t length) = 0;
Zack Rusin2b1bd4f2011-09-04 16:14:22 -040073 virtual int rawPercentRead() = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040074
75protected:
Zack Rusin5ce45e72011-08-05 13:43:46 -040076 bool m_isOpened;
77};
78
Zack Rusin124cd342011-08-24 21:54:56 -040079inline bool File::isOpened() const
80{
81 return m_isOpened;
82}
83
Jose Fonsecace2ed372015-11-07 23:01:11 +000084inline bool File::open(const std::string &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
Zack Rusin2b1bd4f2011-09-04 16:14:22 -0400102inline int File::percentRead()
103{
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
Zack Rusin124cd342011-08-24 21:54:56 -0400110inline void File::close()
111{
112 if (m_isOpened) {
113 rawClose();
114 m_isOpened = false;
115 }
116}
117
Zack Rusin124cd342011-08-24 21:54:56 -0400118inline int File::getc()
119{
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 */