forked from mathieuchartier/mcm
/
File.hpp
246 lines (195 loc) · 4.63 KB
/
File.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* MCM file compressor
Copyright (C) 2013, Google Inc.
Authors: Mathieu Chartier
LICENSE
This file is part of the MCM file compressor.
MCM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MCM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MCM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _FILE_STREAM_HPP_
#define _FILE_STREAM_HPP_
#include <cassert>
#include <stdio.h>
#include <sstream>
#include "Compressor.hpp"
#include "Stream.hpp"
#ifndef WIN32
extern int __cdecl _fseeki64(FILE *, int64_t, int);
extern int64_t __cdecl _ftelli64(FILE *);
#endif
class File {
FILE* handle;
public:
File() : handle(nullptr) {
}
forceinline size_t write(const byte* bytes, size_t count) {
return fwrite(bytes, 1, count, handle);
}
forceinline int put(byte c) {
return fputc(static_cast<int>(c), handle);
}
int close() {
int ret = 0;
if (handle != nullptr) {
ret = fclose(handle);
handle = nullptr;
}
return ret;
}
void rewind() {
::rewind(handle);
}
bool isOpen() const {
return handle != nullptr;
}
// Return 0 if successful, errno otherwise.
int open(const std::string& fileName, std::ios_base::open_mode mode = std::ios_base::in | std::ios_base::binary) {
std::ostringstream oss;
if (mode & std::ios_base::out) {
oss << "w";
if (mode & std::ios_base::in) {
oss << "+";
}
} else if (mode & std::ios_base::in) {
oss << "r";
}
if (mode & std::ios_base::binary) {
oss << "b";
}
handle = fopen(fileName.c_str(), oss.str().c_str());
return handle != nullptr ? 0 : errno;
}
forceinline size_t read(byte* buffer, size_t bytes) {
return fread(buffer, 1, bytes, handle);
}
forceinline int get() {
return fgetc(handle);
}
forceinline int64_t tell() const {
return _ftelli64(handle);
}
forceinline int seek(int64_t offset, int origin = SEEK_SET) {
return _fseeki64(handle, offset, origin);
}
FILE* getHandle() {
return handle;
}
};
class WriteFileStream : WriteStream {
File file;
public:
int open(const std::string& fileName, std::ios_base::open_mode mode = std::ios_base::binary) {
return file.open(fileName, mode | std::ios_base::out);
}
virtual void put(int c) {
file.put(c);
}
virtual void write(const byte* buf, size_t n) {
file.write(buf, n);
}
virtual ~WriteFileStream() {
}
};
class ReadFileStream : public ReadStream {
File file;
public:
int open(const std::string& fileName, std::ios_base::open_mode mode = std::ios_base::binary) {
return file.open(fileName, mode | std::ios_base::in);
}
virtual int get() {
return file.get();
}
virtual size_t read(byte* buf, size_t n) {
return file.read(buf, n);
}
virtual ~ReadFileStream() {
}
};
template <const size_t size>
class BufferedFileStream {
public:
static const uint64_t mask = size - 1;
byte buffer[size];
uint64_t total, eof_pos;
File file;
private:
void flush() {
if ((total & mask) != 0) {
file.write(buffer, static_cast<size_t>(total & mask));
}
}
void flushWhole() {
assert((total & mask) == 0);
file.write(buffer, static_cast<size_t>(size));
}
public:
inline uint64_t getTotal() const {
return total;
}
inline bool eof() const {
return total >= eof_pos;
}
inline bool good() const {
return !eof();
}
int open(const std::string& fileName, std::ios_base::open_mode mode = std::ios_base::in | std::ios_base::binary) {
close();
return file.open(fileName, mode);
}
inline void write(byte ch) {
uint64_t pos = total++;
buffer[pos & mask] = ch;
if ((total & mask) == 0) {
flushWhole();
}
}
void prefetch() {
uint64_t readCount = file.read(buffer, size);
if (readCount != size) {
eof_pos = total + readCount;
}
}
inline int read() {
if (!(total & mask)) {
prefetch();
}
if (LIKELY(total < eof_pos)) {
return (int)(byte)buffer[total++ & mask];
} else {
return EOF;
}
}
// Go back to the start of the file.
void restart() {
total = 0;
eof_pos = (size_t)-1;
file.rewind();
}
void close() {
if (file.isOpen()) {
flush();
file.close();
total = 0;
}
}
void reset() {
file.close();
total = 0;
eof_pos = (size_t)-1;
}
BufferedFileStream() {
reset();
}
virtual ~BufferedFileStream() {
close();
}
};
#endif