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)
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);
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:
#include "snapper/SystemCmd.h"
#include "snapper/SnapperDefines.h"
#include "snapper/Compare.h"
+#include "snapper/AsciiFile.h"
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;
+ }
}
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());