9 static const char rcsid[] __attribute__((used)) =
"$Id: GZCSVFileWriter.cpp 1653 2016-02-28 19:54:59Z sella $";
11 #include "GZCSVFileWriter.h"
15 using namespace sella::file;
32 descriptor(::open(file.c_str(), O_WRONLY | O_LARGEFILE | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH))
35 if (this->descriptor <= 0) {
36 THROW(IOException,
"failed to open file '%s' for writing", this->filename.c_str());
38 memset(&(this->stream), 0,
sizeof(this->stream));
39 memset(&(this->buffer), 0,
sizeof(this->buffer));
42 if (::deflateInit2(&(this->stream), 9 , Z_DEFLATED, 16 + MAX_WBITS , 9 , Z_DEFAULT_STRATEGY) != Z_OK) {
43 ::close(this->descriptor);
45 THROW(IOException,
"failed to initilize zlib instance for writing file '%s'", this->filename.c_str());
54 if (this->descriptor != 0) {
55 ::deflateEnd(&(this->stream));
72 for (
auto iter = data.begin(); iter != data.end(); ++iter) {
76 if (iter != data.begin()) {
78 if (
sizeof(this->delimiter) > (
sizeof(this->buffer[1]) - length)) {
79 this->write(this->buffer[1], length, Z_NO_FLUSH);
83 this->buffer[1][length++] = this->delimiter;
88 if ((iter->length() - offset) > (
sizeof(this->buffer[1]) - length)) {
89 ::memcpy(&(this->buffer[1][length]), &(iter->c_str()[offset]),
sizeof(this->buffer[1]) - length);
91 this->write(this->buffer[1],
sizeof(this->buffer[1]), Z_NO_FLUSH);
93 offset += (
sizeof(this->buffer[1]) - length);
96 ::memcpy(&(this->buffer[1][length]), &(iter->c_str()[offset]), iter->length() - offset);
98 length += (iter->length() - offset);
99 offset += (iter->length() - offset);
101 }
while (offset != static_cast<ssize_t>(iter->length()));
105 if (
sizeof(this->seperator) > (
sizeof(this->buffer[1]) - length)) {
106 this->write(this->buffer[1], length, Z_NO_FLUSH);
110 this->buffer[1][length++] = this->seperator;
112 this->write(this->buffer[1], length, Z_NO_FLUSH);
127 this->stream.next_in = input;
128 this->stream.avail_in = length;
131 this->stream.next_out = this->buffer[0];
132 this->stream.avail_out =
sizeof(this->buffer[0]);
135 switch (::deflate(&(this->stream), flush)) {
137 THROW(IOException,
"zlib compression failure when writing buffer to compressed file '%s'", this->filename.c_str());
141 if ((expect = (
sizeof(this->buffer[0]) - this->stream.avail_out)) > 0) {
142 while ((length = ::write(this->descriptor, this->buffer[0],
sizeof(this->buffer[0]) - this->stream.avail_out)) != expect) {
144 THROW(IOException,
"failure writing buffer to compressed file '%s'", this->filename.c_str());
149 }
while (this->stream.avail_out == 0);
152 if (this->stream.avail_in != 0) {
153 THROW(IOException,
"failed to write full buffer to compressed file '%s'", this->filename.c_str());
169 this->write(buffer, 0, Z_FINISH);
171 ::close(this->descriptor);
173 if (rename.length() > 0) {
174 ::rename(this->filename.c_str(), rename.c_str());
176 this->filename.clear();
177 this->descriptor = 0;
virtual ~GZCSVFileWriter() NO_EXCEPTIONS
void write(const std::vector< std::string > &data) THROWS(IOException)
void close(const std::string &rename=std::string()) THROWS(IOException)
GZCSVFileWriter(const std::string &file, char delimiter=DefaultDelimiter, char seperator=DefaultSeperator) THROWS(IOException)