libutil++  1.9.3
 All Classes Functions Variables
Public Member Functions | Static Public Attributes | List of all members
sella::file::GZCSVPathWriter Class Reference

A GZIP compressed CSV path writer. More...

#include <GZCSVPathWriter.h>

Public Member Functions

 STANDARD_TYPEDEF (sella::file::GZCSVPathWriter)
 
 GZCSVPathWriter (const std::string &path, char delimiter=DefaultDelimiter, char seperator=DefaultSeperator) THROWS(IOException)
 
void write (const std::vector< std::vector< std::string >> &data) THROWS(IOException)
 

Static Public Attributes

static const char * FileSuffix = ".csv.gz"
 The file suffix for matching files in the directory for reading.
 
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.
 

Detailed Description

A GZIP compressed CSV path writer.

Definition at line 31 of file GZCSVPathWriter.h.

Constructor & Destructor Documentation

GZCSVPathWriter::GZCSVPathWriter ( const std::string &  path,
char  delimiter = DefaultDelimiter,
char  seperator = DefaultSeperator 
)

The base constructor for a path writer.

Parameters
[in]pathThe full path to write files to.
[in]delimiterThe field delimiter character for files.
[in]seperatorThe record seperator character for files.
Exceptions
IOExceptionif the path cannot be opened for writing.

Definition at line 30 of file GZCSVPathWriter.cpp.

30  :
31  path(path),
32 
33  delimiter(delimiter),
34  seperator(seperator),
35 
36  sequence(::time(NULL) << 16)
37 {
38  /* Ensure the supplied path is valid. */
39  DIR * directory = ::opendir(this->path.c_str());
40 
41  if (directory == NULL) {
42  THROW(IOException, "failed to open directory '%s' for writing", this->path.c_str());
43  }
44  ::closedir(directory);
45 
46  /* Remove any trailing slash characters from the path. */
47  while (*(this->path.rbegin()) == '/') {
48  this->path.erase(this->path.length() - 1, 1);
49  }
50 }

Member Function Documentation

void GZCSVPathWriter::write ( const std::vector< std::vector< std::string >> &  data)

Create a database file and write all supplied records to it.

Parameters
[in]dataThe vector which contains all records to write to a file.
Exceptions
IOExceptionif unabled to write any record for any reason.

Definition at line 59 of file GZCSVPathWriter.cpp.

References FileSuffix.

59  {
60  char file[2][4096];
61 
62  snprintf(file[0], sizeof(file[0]), "%s/%08lx.%s-incomplete", this->path.c_str(), this->sequence, GZCSVPathWriter::FileSuffix);
63  snprintf(file[1], sizeof(file[1]), "%s/%08lx.%s", this->path.c_str(), this->sequence++, GZCSVPathWriter::FileSuffix);
64 
65  GZCSVFileWriter writer(file[0], this->delimiter, this->seperator);
66 
67  for (auto iter = data.begin(); iter != data.end(); ++iter) {
68  writer.write(*iter);
69  }
70  writer.close(file[1]);
71 }
A GZIP compressed CSV writer.
static const char * FileSuffix
The file suffix for matching files in the directory for reading.

The documentation for this class was generated from the following files: