-
Notifications
You must be signed in to change notification settings - Fork 69
/
gzip.cpp
206 lines (174 loc) · 4.92 KB
/
gzip.cpp
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
/*
* FILE: gzip.cpp
* Created: Feb 22, 2018 Thu
* @copyright 2016-19 Clarity Genomics BVBA
*/
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <cassert>
#include <algorithm>
#include "gzip.hpp"
Gzip::Gzip(bool gzipped)
:
gzipped(gzipped),
line_start(0)
{
if (gzipped)
init();
}
//Gzip::~Gzip() {
// line_start = 0;
// strm.zalloc = Z_NULL;
// strm.zfree = Z_NULL;
// strm.opaque = Z_NULL;
// strm.avail_in = Z_NULL;
// strm.next_in = Z_NULL;
// strm.avail_out = Z_NULL;
//}
/*
* Called from constructor
*/
void Gzip::init()
{
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
int ret = inflateInit2(&strm, 47);
if (ret != Z_OK) {
std::cerr << "Reader::initZstream failed. Error: " << ret << std::endl;
exit(EXIT_FAILURE);;
}
strm.avail_out = 0;
z_in.resize(IN_SIZE);
z_out.resize(OUT_SIZE);
std::fill(z_in.begin(), z_in.end(), 0); // fill IN buffer with 0s
std::fill(z_out.begin(), z_out.end(), 0); // fill OUT buffer wiht 0s
} // ~Gzip::init
/*
* return values: RL_OK (0) | RL_END (1) | RL_ERR (-1)
*
* TODO: Make sure the stream is OK before calling this function.
* std::getline doesn't return error if the stream is not
* readable/closed. It returns the same input it was passed.
*/
int Gzip::getline(std::ifstream & ifs, std::string & line)
{
char* line_end = 0;
int ret = RL_OK;
line.clear();
if (gzipped)
{
bool line_ready = false;
for (; !line_ready; )
{
if (!line_start || !line_start[0])
{
ret = Gzip::inflatez(ifs); // inflate
if (ret == Z_STREAM_END && strm.avail_out == OUT_SIZE)
return RL_END;
if (ret < 0)
return RL_ERR;
if (!line_start)
line_start = (char*)z_out.data();
}
//line_end = strstr(line_start, "\n"); // returns 0 if '\n' not found
line_end = std::find(line_start, (char*)&z_out[0] + OUT_SIZE - strm.avail_out - 1, 10); // '\n'
//line_end = std::find_if(line_start, (char*)&z_out[0] + OUT_SIZE - strm.avail_out - 1, [l = std::locale{}](auto ch) { return ch == 10; });
//line_end = std::find_if(line_start, (char*)&z_out[0] + OUT_SIZE - strm.avail_out - 1, [l = std::locale{}](auto ch) { return std::isspace(ch, l); });
if (line_end && line_end[0] == 10)
{
std::copy(line_start, line_end, std::back_inserter(line));
if (line_end < (char*)&z_out[0] + OUT_SIZE - strm.avail_out - 1) // check there is data after line_end
line_start = line_end + 1; // skip '\n'
else
{
line_start = 0; // no more data in OUT buffer - flag to inflate more
strm.avail_out = 0; // mark OUT buffer as Full to reflush from the beginning [bug 61]
}
line_ready = true; // DEBUG:
//if (line == "@SRR1635864.196 196 length=101")
// std::cout << "HERE";
}
else
{
line_end = (char*)&z_out[0] + OUT_SIZE - strm.avail_out; // end of data in out buffer
std::copy(line_start, line_end, std::back_inserter(line));
line_start = (strm.avail_out == 0) ? 0 : line_end;
line_end = 0;
line_ready = false;
}
} // ~for !line_ready
}
else // non-compressed file
{
if (ifs.eof()) return RL_END;
std::getline(ifs, line);
//if (ifs.fail()) return RL_ERR;
}
return RL_OK;
} // ~Gzip::getline
/*
* Called from getline
*/
int Gzip::inflatez(std::ifstream & ifs)
{
int ret;
std::stringstream ss;
for (;;)
{
if (strm.avail_in == 0 && !ifs.eof()) // in buffer empty
{
std::fill(z_in.begin(), z_in.end(), 0); // reset buffer to 0
ifs.read((char*)z_in.data(), IN_SIZE);
if (!ifs.eof() && ifs.fail())
{
(void)inflateEnd(&strm);
return Z_ERRNO;
}
strm.avail_in = ifs.gcount();
strm.next_in = z_in.data();
}
if (strm.avail_in == 0 && ifs.eof())
{
if (strm.avail_out < OUT_SIZE)
strm.avail_out = OUT_SIZE;
ret = inflateEnd(&strm); // free up the resources
if (ret != Z_STREAM_END)
{
std::cout << STAMP << "xINFO: inflateEnd status is " << ret << std::endl;
}
return Z_STREAM_END;
}
if (strm.avail_out == 0) // out buffer is full - reset
{
std::fill(z_out.begin(), z_out.end(), 0); // reset buffer to 0
strm.avail_out = OUT_SIZE;
strm.next_out = z_out.data();
}
ret = inflate(&strm, Z_NO_FLUSH); // Z_NO_FLUSH Z_SYNC_FLUSH Z_BLOCK
assert(ret != Z_STREAM_ERROR);
switch (ret)
{
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return ret;
case Z_STREAM_END:
break;
}
// OUT buffer holds Inflated data
// IN buffer holds Compressed data
// avail_out == 0 means OUT buffer is Full i.e. no space left
// avail_in == 0 means IN buffer is Empty
// second condition checks if there is still data left in OUT buffer when IN buffer is empty
if ( strm.avail_out == 0 || ( strm.avail_out < OUT_SIZE && strm.avail_in == 0 ) )
break;
} // for(;;)
return ret;// == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
} // ~Gzip::inflatez