From: Arvin Schnell Date: Tue, 5 Apr 2011 13:15:52 +0000 (+0200) Subject: - added class AsciiFileReader X-Git-Tag: v0.1.3~424 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=377e816f307b8ac286f6f237a50f2c72727c7b64;p=thirdparty%2Fsnapper.git - added class AsciiFileReader --- diff --git a/snapper/AsciiFile.cc b/snapper/AsciiFile.cc index 74f69c3f..c9bc3406 100644 --- a/snapper/AsciiFile.cc +++ b/snapper/AsciiFile.cc @@ -34,6 +34,41 @@ namespace snapper using namespace std; + AsciiFileReader::AsciiFileReader(const string& filename) + : file(NULL), buffer(NULL), len(0) + { + file = fopen(filename.c_str(), "r"); + if (file == NULL) + { + y2err("open for '" << filename << "' failed"); + throw exception(); // TODO + } + } + + + AsciiFileReader::~AsciiFileReader() + { + free(buffer); + fclose(file); + } + + + bool + AsciiFileReader::getline(string& line) + { + ssize_t n = ::getline(&buffer, &len, file); + if (n == -1) + return false; + + if (buffer[n - 1] != '\n') + line = string(buffer, 0, n); + else + line = string(buffer, 0, n - 1); + + return true; + } + + AsciiFile::AsciiFile(const char* Name_Cv, bool remove_empty) : Name_C(Name_Cv), remove_empty(remove_empty) @@ -53,43 +88,29 @@ AsciiFile::AsciiFile(const string& Name_Cv, bool remove_empty) bool AsciiFile::reload() { - if (Name_C.empty()) - { - y2err("trying to load nameless AsciiFile"); - return false; - } - y2mil("loading file " << Name_C); clear(); - ifstream File_Ci(Name_C.c_str()); - classic(File_Ci); - string Line_Ci; + try + { + AsciiFileReader file(Name_C); - if (!File_Ci.good()) - throw; + string line; + while (file.getline(line)) + Lines_C.push_back(line); - bool Ret_bi = File_Ci.good(); - File_Ci.unsetf(ifstream::skipws); - getline( File_Ci, Line_Ci ); - while( File_Ci.good() ) + return true; + } + catch (...) // TODO { - Lines_C.push_back( Line_Ci ); - getline( File_Ci, Line_Ci ); + return false; } - return Ret_bi; } bool AsciiFile::save() { - if (Name_C.empty()) - { - y2err("trying to save nameless AsciiFile"); - return false; - } - if (remove_empty && Lines_C.empty()) { y2mil("deleting file " << Name_C); diff --git a/snapper/AsciiFile.h b/snapper/AsciiFile.h index 0c1111ff..78894397 100644 --- a/snapper/AsciiFile.h +++ b/snapper/AsciiFile.h @@ -35,6 +35,24 @@ namespace snapper using std::vector; + class AsciiFileReader + { + public: + + AsciiFileReader(const string& filename); + ~AsciiFileReader(); + + bool getline(string& line); + + private: + + FILE* file; + char* buffer; + size_t len; + + }; + + class AsciiFile { public: diff --git a/snapper/File.cc b/snapper/File.cc index 72d955d1..2eb0a4bd 100644 --- a/snapper/File.cc +++ b/snapper/File.cc @@ -37,6 +37,7 @@ #include "snapper/SystemCmd.h" #include "snapper/SnapperDefines.h" #include "snapper/Compare.h" +#include "snapper/AsciiFile.h" namespace snapper @@ -154,40 +155,36 @@ namespace snapper string input = getSnapper()->snapshotsDir() + "/" + decString(num2) + "/filelist-" + decString(num1) + ".txt"; - FILE* file = fopen(input.c_str(), "r"); - if (file == NULL) + try { - y2mil("file not found"); - return false; - } - - char* line = NULL; - size_t len = 0; - - while (getline(&line, &len, file) != -1) - { - // TODO: more robust splitting + AsciiFileReader file(input); - string name = string(line, 5, strlen(line) - 6); - - unsigned int status = stringToStatus(string(line, 0, 4)); + string line; + while (file.getline(line)) + { + // TODO: more robust splitting - if (invert) - status = invertStatus(status); + string name = string(line, 5); - File file(comparison, name, status); - entries.push_back(file); - } + unsigned int status = stringToStatus(string(line, 0, 4)); - free(line); + if (invert) + status = invertStatus(status); - fclose(file); + File file(comparison, name, status); + entries.push_back(file); + } - sort(entries.begin(), entries.end()); + sort(entries.begin(), entries.end()); - y2mil("read " << entries.size() << " lines"); + y2mil("read " << entries.size() << " lines"); - return true; + return true; + } + catch (...) // TODO + { + return false; + } } diff --git a/snapper/Snapper.cc b/snapper/Snapper.cc index 1423e3cf..12b54334 100644 --- a/snapper/Snapper.cc +++ b/snapper/Snapper.cc @@ -80,28 +80,17 @@ namespace snapper const list files = glob(FILTERSDIR "/*.txt", GLOB_NOSORT); for (list::const_iterator it = files.begin(); it != files.end(); ++it) { - FILE* file = fopen(it->c_str(), "r"); - if (file == NULL) + try { - y2err("file not found"); - continue; - } + AsciiFileReader file(*it); - char* line = NULL; - size_t len = 0; - - while (getline(&line, &len, file) != -1) + string line; + while (file.getline(line)) + ignore_patterns.push_back(line); + } + catch (...) // TODO { - // TODO: more robust - - string ignore_pattern = string(line, 0, strlen(line) - 1); - - ignore_patterns.push_back(ignore_pattern); } - - free(line); - - fclose(file); } y2mil("number of ignore patterns:" << ignore_patterns.size());