9 static const char rcsid[] __attribute__((used)) =
"$Id: GZCSVFileReader.cpp 1653 2016-02-28 19:54:59Z sella $";
11 #include "GZCSVFileReader.h"
15 using namespace sella::file;
30 descriptor(::open(file.c_str(), O_RDONLY | O_LARGEFILE))
33 if (this->descriptor <= 0) {
34 THROW(IOException,
"failed to open file '%s' for reading", this->filename.c_str());
36 memset(&(this->stream), 0,
sizeof(this->stream));
37 memset(&(this->buffer), 0,
sizeof(this->buffer));
40 if (::inflateInit2(&(this->stream), 16 + MAX_WBITS) != Z_OK) {
41 ::close(this->descriptor);
43 THROW(IOException,
"failed to initilize zlib instance for reading file '%s'", this->filename.c_str());
45 this->stream.avail_out =
sizeof(this->buffer[1]);
53 if (this->descriptor != 0) {
54 ::inflateEnd(&(this->stream));
69 if ((data.empty() ==
false) && data.back().empty()) {
72 data.push_back(std::vector<std::string>());
73 }
while (this->
read(data.back()) ==
true);
75 if ((data.empty() ==
false) && data.back().empty()) {
93 while (this->read(data, std::string()) ==
false) {
96 return data.empty() ?
false :
true;
107 ::close(this->descriptor);
109 if (unlink ==
true) {
110 ::unlink(this->filename.c_str());
112 this->filename.clear();
113 this->descriptor = 0;
134 uint8_t * head = this->stream.next_out - (
sizeof(this->buffer[1]) - this->stream.avail_out);
137 if (this->stream.avail_out ==
sizeof(this->buffer[1])) {
139 if (this->stream.avail_in == 0) {
141 if ((this->stream.avail_in = ::read(this->descriptor, this->buffer[0],
sizeof(this->buffer[0]))) < 0) {
142 THROW(IOException,
"failed to read from file '%s'", this->filename.c_str());
143 }
else if (this->stream.avail_in == 0) {
146 this->stream.next_in = this->buffer[0];
150 this->stream.avail_out =
sizeof(this->buffer[1]);
151 this->stream.next_out = this->buffer[1];
153 switch (::inflate(&(this->stream), Z_SYNC_FLUSH)) {
155 THROW(IOException,
"zlib decompression state was destroyed");
158 THROW(IOException,
"zlib decompression failed due to missing dictionary");
161 THROW(IOException,
"zlib decompression failed due to bad data in stream");
164 THROW(IOException,
"zlib decompression failed due to a memory error");
166 head = this->stream.next_out - (
sizeof(this->buffer[1]) - this->stream.avail_out);
170 for (uint8_t * i = head; i != this->stream.next_out; i++) {
171 if ((*i) == this->seperator) {
172 data.push_back(prepend.append(std::string(reinterpret_cast<char *>(head), i - head)));
173 this->stream.avail_out += (i - head + 1);
176 }
else if (*(i) == this->delimiter) {
177 data.push_back(prepend.append(std::string(reinterpret_cast<char *>(head), i - head)));
178 this->stream.avail_out += (i - head + 1);
183 this->stream.avail_out =
sizeof(this->buffer[1]);
186 return this->read(data, std::string(reinterpret_cast<char *>(head), this->stream.next_out - head));
virtual ~GZCSVFileReader() NO_EXCEPTIONS
GZCSVFileReader(const std::string &file, char delimiter=DefaultDelimiter, char seperator=DefaultSeperator) THROWS(IOException)
void close(bool unlink=false) NO_EXCEPTIONS
void read(std::vector< std::vector< std::string >> &data) THROWS(IOException)