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

A GZIP compressed CSV path reader class. More...

#include <GZCSVPathReader.h>

Public Member Functions

 STANDARD_TYPEDEF (sella::file::GZCSVPathReader)
 
 GZCSVPathReader (const std::string &path, char delimiter=DefaultDelimiter, char seperator=DefaultSeperator) THROWS(IOException)
 
void read (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.
 

Static Protected Member Functions

static int filter (const struct dirent *file) NO_EXCEPTIONS
 

Detailed Description

A GZIP compressed CSV path reader class.

Definition at line 34 of file GZCSVPathReader.h.

Constructor & Destructor Documentation

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

The base constructor for a path reader.

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 reading.

Definition at line 31 of file GZCSVPathReader.cpp.

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

Member Function Documentation

int GZCSVPathReader::filter ( const struct dirent *  file)
staticprotected

Filter files from the path based on their name.

Parameters
[in]fileThe direct structure for a single file.
Returns
1 if the file matches the desired patter, or 0 if it does not.

Definition at line 93 of file GZCSVPathReader.cpp.

References FileSuffix.

Referenced by read().

93  {
94  static size_t base = ::strlen(GZCSVPathReader::FileSuffix);
95  size_t size = ::strlen(file->d_name);
96 
97  if ((size >= base) && (::strncmp(&(file->d_name[size - base]), GZCSVPathReader::FileSuffix, base) == 0)) {
98  return 1;
99  }
100  return 0;
101 }
static const char * FileSuffix
The file suffix for matching files in the directory for reading.
void GZCSVPathReader::read ( std::vector< std::vector< std::string >> &  data)

Read all files in the path defined for this instance.

Parameters
[in]dataThe vector to which all data will be written; if not empty this function will clear the vector before writing data.
Exceptions
IOExceptionif unabled to read the directory for any reason.

Definition at line 59 of file GZCSVPathReader.cpp.

References sella::file::GZCSVFileReader::close(), filter(), and sella::file::GZCSVFileReader::read().

59  {
60  struct dirent ** file;
61  int n;
62 
63  /* Clear the result set before reading. */
64  if (data.empty() == false) {
65  data.clear();
66  }
67 
68  /* Read all record entries from all files. */
69  if ((n = ::scandir(this->path.c_str(), &file, GZCSVPathReader::filter, ::alphasort)) >= 0) {
70  for (int i = 0; i < n; i++) {
71  try {
72  GZCSVFileReader reader(std::string(this->path).append("/").append(file[i]->d_name), this->delimiter, this->seperator);
73 
74  reader.read(data);
75  reader.close();
76  } catch (IOException & e) { /* IGNORE */ }
77 
78  ::free(file[i]);
79  }
80  ::free(file);
81  } else {
82  THROW(IOException, "failed to open directory '%s' for reading", this->path.c_str());
83  }
84 }
A GZIP compressed CSV reader.
static int filter(const struct dirent *file) NO_EXCEPTIONS

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