libutil++  1.9.3
 All Classes Functions Variables
GZCSVPathReader.cpp
1 /*
2 ** libutil++
3 ** $Id: GZCSVPathReader.cpp 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 static const char rcsid[] __attribute__((used)) = "$Id: GZCSVPathReader.cpp 1653 2016-02-28 19:54:59Z sella $";
10 
11 #include "GZCSVFileReader.h"
12 #include "GZCSVPathReader.h"
13 
14 #include <sys/types.h>
15 #include <fcntl.h>
16 #include <dirent.h>
17 
18 using namespace sella::file;
19 
20 const char * GZCSVPathReader::FileSuffix = ".csv.gz";
21 
31 GZCSVPathReader::GZCSVPathReader(const std::string & path, char delimiter, char seperator) THROWS(IOException) :
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 }
50 
59 void GZCSVPathReader::read(std::vector<std::vector<std::string>> & data) THROWS(IOException) {
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 }
85 
93 int GZCSVPathReader::filter(const struct dirent * file) NO_EXCEPTIONS {
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 }
102 
103 /*
104 ** vim: noet ts=3 sw=3
105 */
A GZIP compressed CSV reader.
void read(std::vector< std::vector< std::string >> &data) THROWS(IOException)
void close(bool unlink=false) NO_EXCEPTIONS
void read(std::vector< std::vector< std::string >> &data) THROWS(IOException)
static int filter(const struct dirent *file) NO_EXCEPTIONS
GZCSVPathReader(const std::string &path, char delimiter=DefaultDelimiter, char seperator=DefaultSeperator) THROWS(IOException)
static const char * FileSuffix
The file suffix for matching files in the directory for reading.