Enhancement - FF4 Parsing

- Implemented TDB::load
- Changed Record to inherit from std::map <std::string, Att>
- Changed Filter to inherit from std::vector <Att>
This commit is contained in:
Paul Beckingham 2009-05-23 23:29:47 -04:00
parent 2e5e20e3e5
commit b7866b7434
9 changed files with 87 additions and 45 deletions

View file

@ -38,7 +38,6 @@ Att::Att ()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Att::Att (const std::string& name, const std::string& value) Att::Att (const std::string& name, const std::string& value)
{ {
throw std::string ("unimplemented Att::Att");
mName = name; mName = name;
mValue = value; mValue = value;
@ -48,7 +47,6 @@ Att::Att (const std::string& name, const std::string& value)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Att::Att (const Att& other) Att::Att (const Att& other)
{ {
throw std::string ("unimplemented Att::Att");
mName = other.mName; mName = other.mName;
mValue = other.mValue; mValue = other.mValue;
mMods = other.mMods; mMods = other.mMods;

View file

@ -38,7 +38,7 @@ Record::Record ()
Record::Record (const Record& other) Record::Record (const Record& other)
{ {
throw std::string ("unimplemented Record::Record"); 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="); throw std::string ("unimplemented Record:operator=");
if (this != &other) if (this != &other)
{ {
mAtts = other.mAtts; *this = other;
} }
return *this; return *this;
@ -59,16 +59,26 @@ Record::~Record ()
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
//
// start --> [ --> name --> : --> " --> value --> " --> ] --> end
// ^ |
// |________________________________|
//
void Record::parse (const std::string& input) 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 <Att> Record::all () std::vector <Att> Record::all ()
{ {
std::vector <Att> all; std::vector <Att> all;
foreach (a, mAtts) foreach (a, (*this))
all.push_back (a->second); all.push_back (a->second);
return all; return all;
@ -77,17 +87,17 @@ std::vector <Att> Record::all ()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
const std::string Record::get (const std::string& name) const std::string Record::get (const std::string& name)
{ {
if (mAtts.find (name) != mAtts.end ()) if (this->find (name) != this->end ())
return mAtts[name].value (); return (*this)[name].value ();
return ""; return "";
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int Record::getInt (const std::string& name) int Record::get_int (const std::string& name)
{ {
if (mAtts.find (name) != mAtts.end ()) if (this->find (name) != this->end ())
return ::atoi (mAtts[name].value ().c_str ()); return ::atoi ((*this)[name].value ().c_str ());
return 0; return 0;
} }
@ -95,7 +105,7 @@ int Record::getInt (const std::string& name)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Record::set (const std::string& name, const std::string& value) 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; std::stringstream svalue;
svalue << value; svalue << value;
mAtts[name] = Att (name, svalue.str ()); (*this)[name] = Att (name, svalue.str ());
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Record::remove (const std::string& name) void Record::remove (const std::string& name)
{ {
std::map <std::string, Att> copy = mAtts; std::map <std::string, Att> copy = *this;
mAtts.clear (); this->clear ();
foreach (i, copy) foreach (i, copy)
if (i->first != name) if (i->first != name)
mAtts[i->first] = i->second; (*this)[i->first] = i->second;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -31,7 +31,7 @@
#include <map> #include <map>
#include "Att.h" #include "Att.h"
class Record class Record : public std::map <std::string, Att>
{ {
public: public:
Record (); // Default constructor Record (); // Default constructor
@ -45,13 +45,10 @@ public:
std::vector <Att> all (); std::vector <Att> all ();
const std::string get (const std::string&); 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&, const std::string&);
void set (const std::string&, int); void set (const std::string&, int);
void remove (const std::string&); void remove (const std::string&);
private:
std::map <std::string, Att> mAtts;
}; };
#endif #endif

View file

@ -104,9 +104,6 @@ void Context::initialize (int argc, char** argv)
foreach (path, all) foreach (path, all)
tdb.location (expandPath (*path)); 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 appropriate stringtable.
// TODO Load pending.data. // TODO Load pending.data.
// TODO Load completed.data. // TODO Load completed.data.

View file

@ -36,7 +36,7 @@ Filter::Filter ()
Filter::Filter (const Filter& other) Filter::Filter (const Filter& other)
{ {
throw std::string ("unimplemented Filter::Filter"); 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="); throw std::string ("unimplemented Filter::operator=");
if (this != &other) if (this != &other)
{ {
mAtts = other.mAtts; *this = other;
} }
return *this; return *this;
@ -56,18 +56,12 @@ Filter::~Filter ()
{ {
} }
////////////////////////////////////////////////////////////////////////////////
void Filter::add (Att& att)
{
mAtts.push_back (att);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool Filter::pass (Record& record) bool Filter::pass (Record& record)
{ {
throw std::string ("unimplemented Filter::pass"); throw std::string ("unimplemented Filter::pass");
/* /*
foreach (att, mAtts) foreach (att, (*this))
if (! att->match (record)) if (! att->match (record))
return false; return false;
*/ */

View file

@ -31,7 +31,7 @@
#include "Att.h" #include "Att.h"
#include "Record.h" #include "Record.h"
class Filter class Filter : public std::vector <Att>
{ {
public: public:
Filter (); // Default constructor Filter (); // Default constructor
@ -39,11 +39,7 @@ public:
Filter& operator= (const Filter&); // Assignment operator Filter& operator= (const Filter&); // Assignment operator
~Filter (); // Destructor ~Filter (); // Destructor
void add (Att&);
bool pass (Record&); bool pass (Record&);
private:
std::vector <Att> mAtts;
}; };
#endif #endif

View file

@ -147,12 +147,28 @@ void TDB::unlock ()
// TODO Returns number of filtered tasks. // TODO Returns number of filtered tasks.
int TDB::load (std::vector <T>& tasks, Filter& filter) int TDB::load (std::vector <T>& 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. T task (line);
// TODO Let T::parse disassemble it.
// TODO If task passes filter, add to tasks. if (filter.pass (task))
return 0; {
// TODO Add hidden attribute indicating source.
tasks.push_back (task);
}
}
}
}
return tasks.size ();
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -204,6 +220,21 @@ void TDB::getCompletedFiles (std::vector <std::string> files)
files.push_back (location->first + "/completed.data"); files.push_back (location->first + "/completed.data");
} }
////////////////////////////////////////////////////////////////////////////////
void TDB::getContactFiles (std::vector <std::string> 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) 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) while (flock (fileno (in), LOCK_EX) && ++retry <= 3)
delay (0.1); delay (0.1);
if (!in) if (retry > 3)
throw std::string ("Could not lock '") + file + "'."; throw std::string ("Could not lock '") + file + "'.";
return in; return in;

View file

@ -33,6 +33,9 @@
#include "Filter.h" #include "Filter.h"
#include "T.h" #include "T.h"
// Length of longest line.
#define T_LINE_MAX 32768
class TDB class TDB
{ {
public: public:
@ -55,6 +58,8 @@ public:
private: private:
void getPendingFiles (std::vector <std::string>); void getPendingFiles (std::vector <std::string>);
void getCompletedFiles (std::vector <std::string>); void getCompletedFiles (std::vector <std::string>);
void getContactFiles (std::vector <std::string>);
void getUndoStack (std::string&);
FILE* openAndLock (const std::string&); FILE* openAndLock (const std::string&);
private: private:

View file

@ -9,7 +9,21 @@ int main (int argc, char** argv)
{ {
Context c; Context c;
c.initialize (argc, argv); 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 <T> tasks;
int quantity = c.tdb.load (tasks, c.filter);
std::cout << "# " << quantity << " <-- c.tdb.load" << std::endl;
c.tdb.unlock ();
////////////////////////////////////////////////////////////////////////////////
return 0; return 0;
} }