00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "GZCSVPathWriter.h"
00010 #include "GZCSVFileWriter.h"
00011
00012 #include <fcntl.h>
00013 #include <dirent.h>
00014
00015 using namespace sella::file;
00016
00017 const char * GZCSVPathWriter::FileSuffix = ".csv.gz";
00018
00028 GZCSVPathWriter::GZCSVPathWriter(const std::string & path, char delimiter, char seperator) THROWS(IOException) :
00029 path(path),
00030
00031 delimiter(delimiter),
00032 seperator(seperator),
00033
00034 sequence(::time(NULL) << 16)
00035 {
00036
00037 DIR * directory = ::opendir(this->path.c_str());
00038
00039 if (directory == NULL) {
00040 THROW(IOException, "failed to open directory '%s' for writing", this->path.c_str());
00041 }
00042 ::closedir(directory);
00043
00044
00045 while (*(this->path.rbegin()) == '/') {
00046 this->path.erase(this->path.length() - 1, 1);
00047 }
00048 }
00049
00057 void GZCSVPathWriter::write(const std::vector<std::vector<std::string>> & data) THROWS(IOException) {
00058 char file[2][4096];
00059
00060 snprintf(file[0], sizeof(file[0]), "%s/%08lx.%s-incomplete", this->path.c_str(), this->sequence, GZCSVPathWriter::FileSuffix);
00061 snprintf(file[1], sizeof(file[1]), "%s/%08lx.%s", this->path.c_str(), this->sequence++, GZCSVPathWriter::FileSuffix);
00062
00063 GZCSVFileWriter writer(file[0], this->delimiter, this->seperator);
00064
00065 for (auto iter = data.begin(); iter != data.end(); ++iter) {
00066 writer.write(*iter);
00067 }
00068 writer.close(file[1]);
00069 }
00070
00071
00072
00073