00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "GZCSVFileReader.h"
00010 #include "GZCSVPathReader.h"
00011
00012 #include <sys/types.h>
00013 #include <fcntl.h>
00014 #include <dirent.h>
00015
00016 using namespace sella::file;
00017
00018 const char * GZCSVPathReader::FileSuffix = ".csv.gz";
00019
00029 GZCSVPathReader::GZCSVPathReader(const std::string & path, char delimiter, char seperator) THROWS(IOException) :
00030 path(path),
00031
00032 delimiter(delimiter),
00033 seperator(seperator)
00034 {
00035
00036 DIR * directory = ::opendir(this->path.c_str());
00037
00038 if (directory == NULL) {
00039 THROW(IOException, "failed to open directory '%s' for reading", this->path.c_str());
00040 }
00041 ::closedir(directory);
00042
00043
00044 while (*(this->path.rbegin()) == '/') {
00045 this->path.erase(this->path.length() - 1, 1);
00046 }
00047 }
00048
00057 void GZCSVPathReader::read(std::vector<std::vector<std::string>> & data) THROWS(IOException) {
00058 struct dirent ** file;
00059 int n;
00060
00061
00062 if (data.empty() == false) {
00063 data.clear();
00064 }
00065
00066
00067 if ((n = ::scandir(this->path.c_str(), &file, GZCSVPathReader::filter, ::alphasort)) >= 0) {
00068 for (int i = 0; i < n; i++) {
00069 try {
00070 GZCSVFileReader reader(std::string(this->path).append("/").append(file[i]->d_name), this->delimiter, this->seperator);
00071
00072 reader.read(data);
00073 reader.close();
00074 } catch (IOException & e) { }
00075
00076 ::free(file[i]);
00077 }
00078 ::free(file);
00079 } else {
00080 THROW(IOException, "failed to open directory '%s' for reading", this->path.c_str());
00081 }
00082 }
00083
00091 int GZCSVPathReader::filter(const struct dirent * file) NO_EXCEPTIONS {
00092 static size_t base = ::strlen(GZCSVPathReader::FileSuffix);
00093 size_t size = ::strlen(file->d_name);
00094
00095 if ((size >= base) && (::strncmp(&(file->d_name[size - base]), GZCSVPathReader::FileSuffix, base) == 0)) {
00096 return 1;
00097 }
00098 return 0;
00099 }
00100
00101
00102
00103