From b7866b7434a7b3a23cdbc58e3485f66d46693bea Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 23 May 2009 23:29:47 -0400 Subject: [PATCH] Enhancement - FF4 Parsing - Implemented TDB::load - Changed Record to inherit from std::map - Changed Filter to inherit from std::vector --- src/Att.cpp | 2 -- src/Record.cpp | 38 ++++++++++++++++++++++-------------- src/Record.h | 7 ++----- src/objects/Context.cpp | 3 --- src/objects/Filter.cpp | 12 +++--------- src/objects/Filter.h | 6 +----- src/objects/TDB.cpp | 43 +++++++++++++++++++++++++++++++++++------ src/objects/TDB.h | 5 +++++ src/objects/main.cpp | 16 ++++++++++++++- 9 files changed, 87 insertions(+), 45 deletions(-) diff --git a/src/Att.cpp b/src/Att.cpp index 27146b10b..a96c16a82 100644 --- a/src/Att.cpp +++ b/src/Att.cpp @@ -38,7 +38,6 @@ Att::Att () //////////////////////////////////////////////////////////////////////////////// Att::Att (const std::string& name, const std::string& value) { - throw std::string ("unimplemented Att::Att"); mName = name; mValue = value; @@ -48,7 +47,6 @@ Att::Att (const std::string& name, const std::string& value) //////////////////////////////////////////////////////////////////////////////// Att::Att (const Att& other) { - throw std::string ("unimplemented Att::Att"); mName = other.mName; mValue = other.mValue; mMods = other.mMods; diff --git a/src/Record.cpp b/src/Record.cpp index 513f7eb08..c42c6dd87 100644 --- a/src/Record.cpp +++ b/src/Record.cpp @@ -38,7 +38,7 @@ Record::Record () Record::Record (const Record& other) { throw std::string ("unimplemented Record::Record"); - mAtts = other.mAtts; + *this = other; } //////////////////////////////////////////////////////////////////////////////// @@ -47,7 +47,7 @@ Record& Record::operator= (const Record& other) throw std::string ("unimplemented Record:operator="); if (this != &other) { - mAtts = other.mAtts; + *this = other; } return *this; @@ -59,16 +59,26 @@ Record::~Record () } //////////////////////////////////////////////////////////////////////////////// +// +// start --> [ --> name --> : --> " --> value --> " --> ] --> end +// ^ | +// |________________________________| +// void Record::parse (const std::string& input) { - throw std::string ("unimplemented Record::parse"); + if (input[0] == '[' && input[input.length () - 1] == ']') + { + throw std::string ("unimplemented Record:parse"); + } + else + throw std::string ("Record not recognized as FF4"); } //////////////////////////////////////////////////////////////////////////////// std::vector Record::all () { std::vector all; - foreach (a, mAtts) + foreach (a, (*this)) all.push_back (a->second); return all; @@ -77,17 +87,17 @@ std::vector Record::all () //////////////////////////////////////////////////////////////////////////////// const std::string Record::get (const std::string& name) { - if (mAtts.find (name) != mAtts.end ()) - return mAtts[name].value (); + if (this->find (name) != this->end ()) + return (*this)[name].value (); return ""; } //////////////////////////////////////////////////////////////////////////////// -int Record::getInt (const std::string& name) +int Record::get_int (const std::string& name) { - if (mAtts.find (name) != mAtts.end ()) - return ::atoi (mAtts[name].value ().c_str ()); + if (this->find (name) != this->end ()) + return ::atoi ((*this)[name].value ().c_str ()); return 0; } @@ -95,7 +105,7 @@ int Record::getInt (const std::string& name) //////////////////////////////////////////////////////////////////////////////// void Record::set (const std::string& name, const std::string& value) { - mAtts[name] = Att (name, value); + (*this)[name] = Att (name, value); } //////////////////////////////////////////////////////////////////////////////// @@ -104,17 +114,17 @@ void Record::set (const std::string& name, int value) std::stringstream svalue; svalue << value; - mAtts[name] = Att (name, svalue.str ()); + (*this)[name] = Att (name, svalue.str ()); } //////////////////////////////////////////////////////////////////////////////// void Record::remove (const std::string& name) { - std::map copy = mAtts; - mAtts.clear (); + std::map copy = *this; + this->clear (); foreach (i, copy) if (i->first != name) - mAtts[i->first] = i->second; + (*this)[i->first] = i->second; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/Record.h b/src/Record.h index a23dee6db..3ba98bb5d 100644 --- a/src/Record.h +++ b/src/Record.h @@ -31,7 +31,7 @@ #include #include "Att.h" -class Record +class Record : public std::map { public: Record (); // Default constructor @@ -45,13 +45,10 @@ public: std::vector all (); const std::string get (const std::string&); - int getInt (const std::string&); + int get_int (const std::string&); void set (const std::string&, const std::string&); void set (const std::string&, int); void remove (const std::string&); - -private: - std::map mAtts; }; #endif diff --git a/src/objects/Context.cpp b/src/objects/Context.cpp index b0d5cff5a..912ec5f4e 100644 --- a/src/objects/Context.cpp +++ b/src/objects/Context.cpp @@ -104,9 +104,6 @@ void Context::initialize (int argc, char** argv) foreach (path, all) tdb.location (expandPath (*path)); - // Allow user override of file locking. Solaris/NFS machines may want this. - tdb.lock (config.get ("locking", true)); - // TODO Load appropriate stringtable. // TODO Load pending.data. // TODO Load completed.data. diff --git a/src/objects/Filter.cpp b/src/objects/Filter.cpp index 69751fb32..5d4985a1d 100644 --- a/src/objects/Filter.cpp +++ b/src/objects/Filter.cpp @@ -36,7 +36,7 @@ Filter::Filter () Filter::Filter (const Filter& other) { throw std::string ("unimplemented Filter::Filter"); - mAtts = other.mAtts; + *this = other; } //////////////////////////////////////////////////////////////////////////////// @@ -45,7 +45,7 @@ Filter& Filter::operator= (const Filter& other) throw std::string ("unimplemented Filter::operator="); if (this != &other) { - mAtts = other.mAtts; + *this = other; } return *this; @@ -56,18 +56,12 @@ Filter::~Filter () { } -//////////////////////////////////////////////////////////////////////////////// -void Filter::add (Att& att) -{ - mAtts.push_back (att); -} - //////////////////////////////////////////////////////////////////////////////// bool Filter::pass (Record& record) { throw std::string ("unimplemented Filter::pass"); /* - foreach (att, mAtts) + foreach (att, (*this)) if (! att->match (record)) return false; */ diff --git a/src/objects/Filter.h b/src/objects/Filter.h index 879956fe3..05b173ce3 100644 --- a/src/objects/Filter.h +++ b/src/objects/Filter.h @@ -31,7 +31,7 @@ #include "Att.h" #include "Record.h" -class Filter +class Filter : public std::vector { public: Filter (); // Default constructor @@ -39,11 +39,7 @@ public: Filter& operator= (const Filter&); // Assignment operator ~Filter (); // Destructor - void add (Att&); bool pass (Record&); - -private: - std::vector mAtts; }; #endif diff --git a/src/objects/TDB.cpp b/src/objects/TDB.cpp index c53e9e9af..903af0b3c 100644 --- a/src/objects/TDB.cpp +++ b/src/objects/TDB.cpp @@ -147,12 +147,28 @@ void TDB::unlock () // TODO Returns number of filtered tasks. int TDB::load (std::vector & tasks, Filter& filter) { - throw std::string ("unimplemented TDB::load"); + char line[T_LINE_MAX]; + foreach (location, mLocations) + { + while (fgets (line, T_LINE_MAX, location->second)) + { + int length = ::strlen (line); + if (length > 1) + { + line[length - 1] = '\0'; // Kill \n - // TODO Read each row. - // TODO Let T::parse disassemble it. - // TODO If task passes filter, add to tasks. - return 0; + T task (line); + + if (filter.pass (task)) + { + // TODO Add hidden attribute indicating source. + tasks.push_back (task); + } + } + } + } + + return tasks.size (); } //////////////////////////////////////////////////////////////////////////////// @@ -204,6 +220,21 @@ void TDB::getCompletedFiles (std::vector files) files.push_back (location->first + "/completed.data"); } +//////////////////////////////////////////////////////////////////////////////// +void TDB::getContactFiles (std::vector files) +{ + files.clear (); + + foreach (location, mLocations) + files.push_back (location->first + "/contact.data"); +} + +//////////////////////////////////////////////////////////////////////////////// +void TDB::getUndoStack (std::string& file) +{ + throw std::string ("unimplemented TDB::getUndoStack"); +} + //////////////////////////////////////////////////////////////////////////////// FILE* TDB::openAndLock (const std::string& file) { @@ -223,7 +254,7 @@ FILE* TDB::openAndLock (const std::string& file) while (flock (fileno (in), LOCK_EX) && ++retry <= 3) delay (0.1); - if (!in) + if (retry > 3) throw std::string ("Could not lock '") + file + "'."; return in; diff --git a/src/objects/TDB.h b/src/objects/TDB.h index c2f94f697..8a9461fb6 100644 --- a/src/objects/TDB.h +++ b/src/objects/TDB.h @@ -33,6 +33,9 @@ #include "Filter.h" #include "T.h" +// Length of longest line. +#define T_LINE_MAX 32768 + class TDB { public: @@ -55,6 +58,8 @@ public: private: void getPendingFiles (std::vector ); void getCompletedFiles (std::vector ); + void getContactFiles (std::vector ); + void getUndoStack (std::string&); FILE* openAndLock (const std::string&); private: diff --git a/src/objects/main.cpp b/src/objects/main.cpp index a4571e89e..4977b15c7 100644 --- a/src/objects/main.cpp +++ b/src/objects/main.cpp @@ -9,7 +9,21 @@ int main (int argc, char** argv) { Context c; c.initialize (argc, argv); - c.run (); +// c.run (); + +//////////////////////////////////////////////////////////////////////////////// + + c.tdb.lock (c.config.get ("locking", true)); + + c.filter.push_back (Att ("priority", "L")); + + std::vector tasks; + int quantity = c.tdb.load (tasks, c.filter); + std::cout << "# " << quantity << " <-- c.tdb.load" << std::endl; + + c.tdb.unlock (); + +//////////////////////////////////////////////////////////////////////////////// return 0; }