From 3a9c98d342c08cb846e95d63b790cc86c4bd42c6 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 17 May 2009 23:31:25 -0400 Subject: [PATCH] FF4 - snapshot --- src/rewrite/Att.h | 2 +- src/rewrite/Context.cpp | 75 ++++++++++++++++++++++++++++++++++------- src/rewrite/Context.h | 8 +++-- src/rewrite/Makefile | 2 +- src/rewrite/Record.cpp | 5 +++ src/rewrite/Record.h | 6 +++- src/rewrite/T.cpp | 8 ++--- src/rewrite/T.h | 6 ++-- src/rewrite/TDB.cpp | 34 ++++++++++++++++++- src/rewrite/TDB.h | 8 +++++ src/rewrite/main.cpp | 3 +- 11 files changed, 127 insertions(+), 30 deletions(-) diff --git a/src/rewrite/Att.h b/src/rewrite/Att.h index e496a9261..708e82952 100644 --- a/src/rewrite/Att.h +++ b/src/rewrite/Att.h @@ -34,7 +34,7 @@ class Att { public: Att (); // Default constructor - Att (const std::string&, const std::string&); + Att (const std::string&, const std::string&); // Simple constructor Att (const Att&); // Copy constructor Att& operator= (const Att&); // Assignment operator ~Att (); // Destructor diff --git a/src/rewrite/Context.cpp b/src/rewrite/Context.cpp index 0f8b14c95..8c16ccfe4 100644 --- a/src/rewrite/Context.cpp +++ b/src/rewrite/Context.cpp @@ -25,11 +25,22 @@ // //////////////////////////////////////////////////////////////////////////////// +#include #include "Context.h" +#include "text.h" +#include "util.h" +#include "task.h" +#include "../auto.h" //////////////////////////////////////////////////////////////////////////////// Context::Context () { + // Set up randomness. +#ifdef HAVE_SRANDOM + srandom (time (NULL)); +#else + srand (time (NULL)); +#endif } //////////////////////////////////////////////////////////////////////////////// @@ -67,24 +78,39 @@ Context::~Context () } //////////////////////////////////////////////////////////////////////////////// -void Context::initialize () +void Context::initialize (int argc, char** argv) { - throw std::string ("unimplemented Context::initialize"); - // TODO Load config. + // Load the config file from the home directory. If the file cannot be + // found, offer to create a sample one. + loadCorrectConfigFile (argc, argv); + + // When redirecting output to a file, do not use color, curses. + if (!isatty (fileno (stdout))) + { + config.set ("curses", "off"); + + if (! config.get (std::string ("_forcecolor"), false)) + config.set ("color", "off"); + } + + // TODO Handle "--version, -v" right here. + + // init TDB. + std::string location = config.get ("data.location"); + std::vector all; + split (all, location, ','); + foreach (path, all) + tdb.location (expandPath (*path)); + + // Allow user override of file locking. Solaris/NFS machines may want this. + if (! config.get ("locking", true)) + tdb.noLock (); + // TODO Load pending.data. // TODO Load completed.data. // TODO Load deleted.data. } -//////////////////////////////////////////////////////////////////////////////// -int Context::commandLine (int argc, char** argv) -{ - throw std::string ("unimplemented Context::commandLine"); - // TODO Support rc: override. - // TODO Handle "--version, -v" right here. - return 0; -} - //////////////////////////////////////////////////////////////////////////////// int Context::run () { @@ -97,4 +123,29 @@ int Context::run () } //////////////////////////////////////////////////////////////////////////////// +void Context::loadCorrectConfigFile (int argc, char** argv) +{ + for (int i = 1; i < argc; ++i) + { + if (! strncmp (argv[i], "rc:", 3)) + { + if (! access (&(argv[i][3]), F_OK)) + { + std::string file = &(argv[i][3]); + config.load (file); + return; + } + else + throw std::string ("Could not read configuration file '") + &(argv[i][3]) + "'"; + } + } + struct passwd* pw = getpwuid (getuid ()); + if (!pw) + throw std::string ("Could not read home directory from passwd file."); + + std::string file = pw->pw_dir; + config.createDefault (file); +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/rewrite/Context.h b/src/rewrite/Context.h index 26753a5eb..c0d50c68a 100644 --- a/src/rewrite/Context.h +++ b/src/rewrite/Context.h @@ -31,8 +31,8 @@ #include "Keymap.h" #include "Config.h" #include "Sequence.h" -#include "TDB.h" #include "T.h" +#include "TDB.h" class Context @@ -43,10 +43,12 @@ public: Context& operator= (const Context&); // Assignment operator ~Context (); // Destructor - void initialize (); - int commandLine (int, char**); + void initialize (int, char**); int run (); +private: + void loadCorrectConfigFile (int, char**); + public: Config config; Filter filter; diff --git a/src/rewrite/Makefile b/src/rewrite/Makefile index f4021e3ba..fd26bdde4 100644 --- a/src/rewrite/Makefile +++ b/src/rewrite/Makefile @@ -3,7 +3,7 @@ CFLAGS = -I. -I.. -Wall -pedantic -ggdb3 -fno-rtti -fstack-check LFLAGS = LIBS = OBJECTS = main.o Context.o TDB.o T.o Sequence.o Filter.o Att.o Keymap.o \ - ../util.o ../text.o ../Config.o ../Date.o + Record.o ../util.o ../text.o ../Config.o ../Date.o all: $(PROJECT) diff --git a/src/rewrite/Record.cpp b/src/rewrite/Record.cpp index f74517d9d..4249ac7e0 100644 --- a/src/rewrite/Record.cpp +++ b/src/rewrite/Record.cpp @@ -57,4 +57,9 @@ Record::~Record () } //////////////////////////////////////////////////////////////////////////////// +void Record::parse (const std::string& input) +{ + throw std::string ("unimplemented Record::parse"); +} +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/rewrite/Record.h b/src/rewrite/Record.h index 20c10977d..d35e8a02e 100644 --- a/src/rewrite/Record.h +++ b/src/rewrite/Record.h @@ -36,7 +36,11 @@ public: Record (); // Default constructor Record (const Record&); // Copy constructor Record& operator= (const Record&); // Assignment operator - ~Record (); // Destructor + virtual ~Record (); // Destructor + + virtual std::string composeF4 () = 0; + virtual std::string composeCSV () = 0; + void parse (const std::string&); private: std::vector mAtts; diff --git a/src/rewrite/T.cpp b/src/rewrite/T.cpp index f88d4b179..af5f9c234 100644 --- a/src/rewrite/T.cpp +++ b/src/rewrite/T.cpp @@ -34,11 +34,13 @@ T::T () } //////////////////////////////////////////////////////////////////////////////// +/* T::T (const T& other) { throw std::string ("unimplemented T::T"); // mOne = other.mOne; } +*/ //////////////////////////////////////////////////////////////////////////////// T::T (const std::string& input) @@ -78,9 +80,3 @@ std::string T::composeCSV () } //////////////////////////////////////////////////////////////////////////////// -void T::parse (const std::string& input) -{ - throw std::string ("unimplemented T::parse"); -} - -//////////////////////////////////////////////////////////////////////////////// diff --git a/src/rewrite/T.h b/src/rewrite/T.h index 1453c7347..8275956fb 100644 --- a/src/rewrite/T.h +++ b/src/rewrite/T.h @@ -28,19 +28,19 @@ #define INCLUDED_T #include +#include "Record.h" -class T +class T : public Record { public: T (); // Default constructor - T (const T&); // Copy constructor +// T (const T&); // Copy constructor T (const std::string&); // Parse T& operator= (const T&); // Assignment operator ~T (); // Destructor std::string composeF4 (); std::string composeCSV (); - void parse (const std::string&); private: }; diff --git a/src/rewrite/TDB.cpp b/src/rewrite/TDB.cpp index 5526a5611..3f2d658be 100644 --- a/src/rewrite/TDB.cpp +++ b/src/rewrite/TDB.cpp @@ -26,10 +26,14 @@ //////////////////////////////////////////////////////////////////////////////// #include +#include "text.h" +#include "util.h" #include "TDB.h" +#include "task.h" //////////////////////////////////////////////////////////////////////////////// TDB::TDB () +: mLock (true) { } @@ -60,7 +64,11 @@ TDB::~TDB () //////////////////////////////////////////////////////////////////////////////// void TDB::location (const std::string& path) { - throw std::string ("unimplemented TDB::location"); + if (access (expandPath (path).c_str (), F_OK)) + throw std::string ("Data location '") + + path + + "' does not exist, or is not readable and writable."; + mLocations.push_back (path); } @@ -94,3 +102,27 @@ void TDB::upgrade () } //////////////////////////////////////////////////////////////////////////////// +void TDB::noLock () +{ + mLock = false; +} + +//////////////////////////////////////////////////////////////////////////////// +void TDB::getPendingFiles (std::vector files) +{ + files.clear (); + + foreach (location, mLocations) + files.push_back (*location + "/pending.data"); +} + +//////////////////////////////////////////////////////////////////////////////// +void TDB::getCompletedFiles (std::vector files) +{ + files.clear (); + + foreach (location, mLocations) + files.push_back (*location + "/completed.data"); +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/rewrite/TDB.h b/src/rewrite/TDB.h index 468fefb47..0e6721fb1 100644 --- a/src/rewrite/TDB.h +++ b/src/rewrite/TDB.h @@ -46,8 +46,16 @@ public: int commit (); void upgrade (); + void noLock (); + +private: + void getPendingFiles (std::vector ); + void getCompletedFiles (std::vector ); + private: std::vector mLocations; + bool mLock; + // TODO Need cache of raw file contents. }; diff --git a/src/rewrite/main.cpp b/src/rewrite/main.cpp index b145dae1d..3b1cc6c5b 100644 --- a/src/rewrite/main.cpp +++ b/src/rewrite/main.cpp @@ -8,8 +8,7 @@ int main (int argc, char** argv) try { Context c; - c.initialize (); - c.commandLine (argc, argv); + c.initialize (argc, argv); c.run (); return 0;