Bug Fix - undo

- Fixed bug that didn't properly pop_back off the undo stack.
- Fixed bug that caused an attempt to call taskDifferences when one of
  the tasks was "".
This commit is contained in:
Paul Beckingham 2009-07-01 00:46:15 -04:00
parent 0891d3ea63
commit 569d31da7a
3 changed files with 35 additions and 12 deletions

View file

@ -507,7 +507,7 @@ void TDB::undo ()
throw std::string ("There are no recorded transactions to undo."); throw std::string ("There are no recorded transactions to undo.");
// pop last tx // pop last tx
u.pop_back (); u.pop_back (); // separator.
std::string current = u.back ().substr (4, std::string::npos); std::string current = u.back ().substr (4, std::string::npos);
u.pop_back (); u.pop_back ();
@ -517,6 +517,7 @@ void TDB::undo ()
if (u.back ().substr (0, 5) == "time ") if (u.back ().substr (0, 5) == "time ")
{ {
when = u.back ().substr (5, std::string::npos); when = u.back ().substr (5, std::string::npos);
u.pop_back ();
prior = ""; prior = "";
} }
else else
@ -528,12 +529,19 @@ void TDB::undo ()
} }
// confirm // confirm
if (prior != "")
{
Task priorTask (prior); Task priorTask (prior);
Task currentTask (current); Task currentTask (current);
std::cout << "The last modification was that " std::cout << "The last modification was that "
<< taskDifferences (prior, current) << taskDifferences (prior, current)
<< std::endl << std::endl
<< std::endl; << std::endl;
}
else
std::cout << "This was a new task."
<< std::endl
<< std::endl;
if (!confirm ("Are you sure you want to undo the last update?")) if (!confirm ("Are you sure you want to undo the last update?"))
throw std::string ("No changes made."); throw std::string ("No changes made.");
@ -546,8 +554,6 @@ void TDB::undo ()
else else
throw std::string ("Cannot locate UUID in task to undo."); throw std::string ("Cannot locate UUID in task to undo.");
std::cout << "# " << uuid << std::endl;
// load pending.data // load pending.data
std::vector <std::string> p; std::vector <std::string> p;
slurp (pendingFile, p); slurp (pendingFile, p);
@ -578,18 +584,24 @@ void TDB::undo ()
// load completed.data // load completed.data
std::vector <std::string> c; std::vector <std::string> c;
slurp (pendingFile, p); slurp (completedFile, c);
// is 'current' in completed? // is 'current' in completed?
foreach (task, c) foreach (task, c)
{ {
std::cout << "# loop " << *task << std::endl;
if (task->find (uuid) != std::string::npos) if (task->find (uuid) != std::string::npos)
{ {
std::cout << "# found in completed" << std::endl;
// If task now belongs back in pending.data // If task now belongs back in pending.data
if (prior.find ("status:\"pending\"") != std::string::npos || if (prior.find ("status:\"pending\"") != std::string::npos ||
prior.find ("status:\"waiting\"") != std::string::npos || prior.find ("status:\"waiting\"") != std::string::npos ||
prior.find ("status:\"recurring\"") != std::string::npos) prior.find ("status:\"recurring\"") != std::string::npos)
{ {
std::cout << "# task belongs in pending.data" << std::endl;
c.erase (task); c.erase (task);
p.push_back (prior); p.push_back (prior);
spit (completedFile, c); spit (completedFile, c);
@ -599,6 +611,8 @@ void TDB::undo ()
} }
else else
{ {
std::cout << "# task belongs in pending.data" << std::endl;
*task = prior; *task = prior;
spit (completedFile, c); spit (completedFile, c);
spit (undoFile, u); spit (undoFile, u);

View file

@ -427,13 +427,22 @@ void spit (const std::string& file, const std::string& contents)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void spit (const std::string& file, const std::vector <std::string>& lines) void spit (
const std::string& file,
const std::vector <std::string>& lines,
bool addNewlines /* = true */)
{ {
std::ofstream out (file.c_str ()); std::ofstream out (file.c_str ());
if (out.good ()) if (out.good ())
{ {
foreach (line, lines) foreach (line, lines)
{
out << *line; out << *line;
if (addNewlines)
out << "\n";
}
out.close (); out.close ();
} }
else else

View file

@ -73,7 +73,7 @@ std::string expandPath (const std::string&);
bool slurp (const std::string&, std::vector <std::string>&, bool trimLines = false); bool slurp (const std::string&, std::vector <std::string>&, bool trimLines = false);
bool slurp (const std::string&, std::string&, bool trimLines = false); bool slurp (const std::string&, std::string&, bool trimLines = false);
void spit (const std::string&, const std::string&); void spit (const std::string&, const std::string&);
void spit (const std::string&, const std::vector <std::string>&); void spit (const std::string&, const std::vector <std::string>&, bool addNewlines = true);
bool taskDiff (const Task&, const Task&); bool taskDiff (const Task&, const Task&);
std::string taskDifferences (const Task&, const Task&); std::string taskDifferences (const Task&, const Task&);