A GZIP compressed CSV writer.
More...
#include <GZCSVFileWriter.h>
|
|
static const char | DefaultDelimiter = 0x1e |
| | The default delimiter used between the fields of a record; the ASCII record seperator character.
|
| |
|
static const char | DefaultSeperator = 0x1d |
| | The default seperator used between the records of a file; the ASCII group seperator character.
|
| |
|
| void | write (uint8_t *input, ssize_t length, int flush) THROWS(IOException) |
| |
A GZIP compressed CSV writer.
Definition at line 31 of file GZCSVFileWriter.h.
The base constructor for a file writer.
- Parameters
-
| [in] | file | The name of the file to write. |
| [in] | delimiter | The field delimiter character for this file. |
| [in] | seperator | The record seperator character for this file. |
- Exceptions
-
| IOException | if the file cannot be opened for writing. |
Definition at line 26 of file GZCSVFileWriter.cpp.
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) {
45 THROW(IOException,
"failed to initilize zlib instance for writing file '%s'", this->filename.c_str());
void close(const std::string &rename=std::string()) THROWS(IOException)
| GZCSVFileWriter::~GZCSVFileWriter |
( |
| ) |
|
|
virtual |
The class destructor; this destructor closes the file opened by this instance if it was not explicitly closed by the class user.
Definition at line 53 of file GZCSVFileWriter.cpp.
References close().
54 if (this->descriptor != 0) {
55 ::deflateEnd(&(this->stream));
void close(const std::string &rename=std::string()) THROWS(IOException)
| void GZCSVFileWriter::close |
( |
const std::string & |
rename = std::string() | ) |
|
Close the file opened by this instance. Failure to explicitly call this function will not cause a file descriptor leak if the class destructor is called: the destructor will call close() automatically if necessary.
- Parameters
-
| [in] | rename | When set to a non-zero length string, the file will be renamed to the value in rename after closing. |
Definition at line 165 of file GZCSVFileWriter.cpp.
Referenced by ~GZCSVFileWriter().
169 this->
write(buffer, 0, Z_FINISH);
173 if (rename.length() > 0) {
174 ::rename(this->filename.c_str(), rename.c_str());
176 this->filename.clear();
177 this->descriptor = 0;
void write(const std::vector< std::string > &data) THROWS(IOException)
void close(const std::string &rename=std::string()) THROWS(IOException)
| void GZCSVFileWriter::write |
( |
const std::vector< std::string > & |
data | ) |
|
Append the next record to the file.
- Parameters
-
| [in] | data | The vector which contains all fields of the record. |
- Exceptions
-
| IOException | if unabled to write the next record for any reason. |
Definition at line 68 of file GZCSVFileWriter.cpp.
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);
void write(const std::vector< std::string > &data) THROWS(IOException)
| void GZCSVFileWriter::write |
( |
uint8_t * |
input, |
|
|
ssize_t |
length, |
|
|
int |
flush |
|
) |
| |
|
protected |
Append a character sequence to the file after compressing it.
- Parameters
-
| [in] | data | The bytes to compress before writing. |
| [in] | size | The number of bytes to compress. |
| [in] | flush | The zlib flush flag to utilize for the compression. |
- Exceptions
-
| IOException | if unabled to write the supplied bytes for any reason. |
Definition at line 124 of file GZCSVFileWriter.cpp.
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());
void write(const std::vector< std::string > &data) THROWS(IOException)
The documentation for this class was generated from the following files:
- /home/sella/digitalgenesis/libutil++/releases/rpm/BUILD/libutil++-1.9.3/src/sella/file/GZCSVFileWriter.h
- /home/sella/digitalgenesis/libutil++/releases/rpm/BUILD/libutil++-1.9.3/src/sella/file/GZCSVFileWriter.cpp