-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_abstractions.h
285 lines (244 loc) · 7.49 KB
/
file_abstractions.h
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
*
* LightStringGraph
*
* Lightweight String Graph Construction.
*
* Copyright (C) 2015 Yuri Pirola
*
* Distributed under the terms of the GNU General Public License (or the Lesser
* GPL).
*
* This file is part of LightStringGraph.
*
* LighStringGraph 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.
*
* LightStringGraph 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 LightStringGraph. If not, see <http://www.gnu.org/licenses/>.
*
**/
#ifndef FILE_ABSTRACTIONS_H
#define FILE_ABSTRACTIONS_H
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <type_traits>
#include <zlib.h>
#include "types.h"
#include "util_log.h"
// We need zlib >= 1.2.5 for transparent writing
#if ZLIB_VERNUM < 0x1250
#error zlib >= 1.2.5 is required
#endif
#define USE_GZBUFFER
#define GZBUFFER_SIZE (128*1024)
// Never write a compressed file ("transparent" writing)
#define GZFILE_WRITE_MODE "wTb"
template <int bytenum>
struct integer_type {
};
template <>
struct integer_type<1> {
typedef uint8_t type;
};
template <>
struct integer_type<2> {
typedef uint16_t type;
};
template <>
struct integer_type<4> {
typedef uint32_t type;
};
// Shift 64 is war
// template <>
// struct integer_type<8> {
// typedef uint64_t type;
// };
template <int byte_num>
void writelen(gzFile fout, BWTPosition len)
{
static char intbuff[byte_num*64];
const uint8_t shift_amount= (byte_num * 8 -1);
const BWTPosition mask= ((1ull << shift_amount) -1);
unsigned int bpos= 0;
do
{
typename integer_type<byte_num>::type towrite=(len & mask);
len >>= shift_amount;
if (len)
towrite |= (1ull << shift_amount);
memcpy(intbuff+bpos, (char *)&towrite, byte_num);
bpos += byte_num;
}
while(len);
gzwrite(fout, intbuff, sizeof(char)*bpos);
}
template <int byte_num>
BWTPosition readlen(gzFile fin)
{
const unsigned int shift_amount= (byte_num * 8 -1);
const BWTPosition mask= ((1ull << shift_amount) -1);
BWTPosition p =0;
typename integer_type<byte_num>::type partialread =0;
uint8_t iter=0;
do{
partialread =0;
const size_t gcount= gzread(fin, (char *)(&partialread), byte_num);
if(gcount != byte_num)
return 0;
p |= ((partialread & mask) << (shift_amount * iter));
++iter;
}while(partialread & (1ull << shift_amount));
return p;
}
class gzip_file_t {
private:
gzFile file;
public:
static gzFile
gzfile_open(const std::string& filename,
const bool write_mode,
const bool fail_if_error= true) {
gzFile file= gzopen(filename.c_str(), (write_mode ? GZFILE_WRITE_MODE : "rb"));
const bool result= (file != Z_NULL);
if (fail_if_error && !result) {
_FAIL_AND_LOG("Impossible to open file '" + filename +
"' for " + (write_mode ? "writing" : "reading"));
}
#ifdef USE_GZBUFFER
if (!write_mode && result) gzbuffer(file, GZBUFFER_SIZE);
#endif
return file;
}
gzip_file_t() : file(Z_NULL) {}
~gzip_file_t() { close(); }
explicit gzip_file_t(const std::string& filename,
const bool write_mode= false)
: file(Z_NULL)
{
open(filename, write_mode);
}
bool open(const std::string& filename, const bool write_mode= false) {
bool result= true;
if (file != Z_NULL) result= close();
if (result) {
file= gzfile_open(filename, write_mode, false);
result= (file != Z_NULL);
}
return result;
}
bool close() {
bool result= true;
if (file != Z_NULL) {
/*
gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a
file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the
last read ended in the middle of a gzip stream, or Z_OK on success. */
const int intresult= gzclose(file);
result= (intresult!=Z_ERRNO) & (intresult!=Z_MEM_ERROR);
if (result) file = Z_NULL;
}
return result;
}
bool is_open() const {
return (file != Z_NULL);
}
template <typename TFileValue>
bool read(TFileValue& value) {
const int byteread= gzread(file, (void*)&value, sizeof(TFileValue) );
return (byteread == sizeof(TFileValue));
}
template <typename TFileValue>
size_t read(TFileValue* value, const size_t len) {
const int byteread= gzread(file, (void*)value, sizeof(TFileValue)*len );
return byteread/sizeof(TFileValue);
}
template <typename TFileValue>
bool write(const TFileValue& value) {
const int bytewritten= gzwrite(file, (void*)&value, sizeof(TFileValue) );
return (bytewritten == sizeof(TFileValue));
}
template <typename TFileValue>
size_t write(const TFileValue * const value, const size_t len) {
const int bytewritten= gzwrite(file, (void*)&value, sizeof(TFileValue)*len );
return bytewritten/sizeof(TFileValue);
}
template <typename TFileValue, int byte_num>
void write_compressed(const TFileValue value) {
static_assert(std::is_integral<TFileValue>::value && sizeof(TFileValue)<=sizeof(BWTPosition),
"write_compressed can be used only for integral types of size at most that of BWTPosition");
writelen<byte_num>(file, value);
}
template <typename TFileValue, int byte_num>
void read_compressed(TFileValue& value) {
static_assert(std::is_integral<TFileValue>::value && sizeof(TFileValue)<=sizeof(BWTPosition),
"read_compressed can be used only for integral types of size at most that of BWTPosition");
value= readlen<byte_num>(file);
}
};
#ifndef BUFFERED_FILE_BUFFERSIZE
#define BUFFERED_FILE_BUFFERSIZE 8192
#endif
template <class TFile, size_t THIS_BUFFERSIZE=BUFFERED_FILE_BUFFERSIZE>
class buffered_file_t {
private:
TFile file;
char* buffer;
size_t bpos;
size_t blen;
void fill_buffer() {
bpos= blen= 0;
blen= file.read(buffer, THIS_BUFFERSIZE);
}
void fill_buffer_if_empty() {
if ((blen == 0) | (bpos == blen))
fill_buffer();
}
public:
buffered_file_t() : file(), buffer(new char[THIS_BUFFERSIZE]) {}
~buffered_file_t() { close(); delete [] buffer;}
bool open(const std::string& filename) {
const bool result= file.open(filename);
fill_buffer();
return result;
}
bool close() {
const bool result= file.close();
bpos= blen= 0;
return result;
}
template <typename TFileValue>
bool read(TFileValue& value, typename std::enable_if< !std::is_same<char,TFileValue>::value>::type* dummy=0) {
size_t byte_copied= 0;
while (byte_copied < sizeof(TFileValue)) {
fill_buffer_if_empty();
const size_t num= std::min(blen-bpos, sizeof(TFileValue)-byte_copied);
if (num == 0) {
return false;
}
memcpy(((char*)(&value))+byte_copied, buffer+bpos, num);
byte_copied += num;
bpos += num;
}
return (byte_copied == sizeof(TFileValue));
}
template <typename TFileValue>
bool read(TFileValue& value, typename std::enable_if< std::is_same<char,TFileValue>::value>::type* dummy=0) {
fill_buffer_if_empty();
if (bpos >= blen)
return false;
value= *(buffer+bpos);
++bpos;
return true;
}
};
typedef buffered_file_t<gzip_file_t> auto_file_t;
#endif