]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- added class AsciiFileReader
authorArvin Schnell <aschnell@suse.de>
Tue, 5 Apr 2011 13:15:52 +0000 (15:15 +0200)
committerArvin Schnell <aschnell@suse.de>
Tue, 5 Apr 2011 13:15:52 +0000 (15:15 +0200)
snapper/AsciiFile.cc
snapper/AsciiFile.h
snapper/File.cc
snapper/Snapper.cc

index 74f69c3f15b80ef05ee62a2ada0e3ee206e1b701..c9bc3406d63ffb030c40e310ff8924a858350dd0 100644 (file)
@@ -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);
index 0c1111ffdf731ce2644c8107d72a8dcee510d6f5..78894397b8482d9db18e11e4250dabae1abbec67 100644 (file)
@@ -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:
index 72d955d188f151827a3d1c8dafbf92eae390967b..2eb0a4bd424e309c5c5fb80f4320dd7af8797656 100644 (file)
@@ -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;
+       }
     }
 
 
index 1423e3cfba559f66056e007ebf244acf290bab55..12b54334280d8e91c93be3f961e6d2f97c0d6e78 100644 (file)
@@ -80,28 +80,17 @@ namespace snapper
        const list<string> files = glob(FILTERSDIR "/*.txt", GLOB_NOSORT);
        for (list<string>::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());