From 569d31da7a2abce5b780e9ecb077e4ac40604a89 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 1 Jul 2009 00:46:15 -0400 Subject: [PATCH] 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 "". --- src/TDB.cpp | 34 ++++++++++++++++++++++++---------- src/util.cpp | 11 ++++++++++- src/util.h | 2 +- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/TDB.cpp b/src/TDB.cpp index 11610a7b8..d6d6d5765 100644 --- a/src/TDB.cpp +++ b/src/TDB.cpp @@ -507,7 +507,7 @@ void TDB::undo () throw std::string ("There are no recorded transactions to undo."); // pop last tx - u.pop_back (); + u.pop_back (); // separator. std::string current = u.back ().substr (4, std::string::npos); u.pop_back (); @@ -517,6 +517,7 @@ void TDB::undo () if (u.back ().substr (0, 5) == "time ") { when = u.back ().substr (5, std::string::npos); + u.pop_back (); prior = ""; } else @@ -528,12 +529,19 @@ void TDB::undo () } // confirm - Task priorTask (prior); - Task currentTask (current); - std::cout << "The last modification was that " - << taskDifferences (prior, current) - << std::endl - << std::endl; + if (prior != "") + { + Task priorTask (prior); + Task currentTask (current); + std::cout << "The last modification was that " + << taskDifferences (prior, current) + << 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?")) throw std::string ("No changes made."); @@ -546,8 +554,6 @@ void TDB::undo () else throw std::string ("Cannot locate UUID in task to undo."); - std::cout << "# " << uuid << std::endl; - // load pending.data std::vector p; slurp (pendingFile, p); @@ -578,18 +584,24 @@ void TDB::undo () // load completed.data std::vector c; - slurp (pendingFile, p); + slurp (completedFile, c); // is 'current' in completed? foreach (task, c) { + std::cout << "# loop " << *task << std::endl; + if (task->find (uuid) != std::string::npos) { + std::cout << "# found in completed" << std::endl; + // If task now belongs back in pending.data if (prior.find ("status:\"pending\"") != std::string::npos || prior.find ("status:\"waiting\"") != std::string::npos || prior.find ("status:\"recurring\"") != std::string::npos) { + std::cout << "# task belongs in pending.data" << std::endl; + c.erase (task); p.push_back (prior); spit (completedFile, c); @@ -599,6 +611,8 @@ void TDB::undo () } else { + std::cout << "# task belongs in pending.data" << std::endl; + *task = prior; spit (completedFile, c); spit (undoFile, u); diff --git a/src/util.cpp b/src/util.cpp index 922bfb7d6..889104823 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -427,13 +427,22 @@ void spit (const std::string& file, const std::string& contents) } //////////////////////////////////////////////////////////////////////////////// -void spit (const std::string& file, const std::vector & lines) +void spit ( + const std::string& file, + const std::vector & lines, + bool addNewlines /* = true */) { std::ofstream out (file.c_str ()); if (out.good ()) { foreach (line, lines) + { out << *line; + + if (addNewlines) + out << "\n"; + } + out.close (); } else diff --git a/src/util.h b/src/util.h index 02b43e05a..22a917a98 100644 --- a/src/util.h +++ b/src/util.h @@ -73,7 +73,7 @@ std::string expandPath (const std::string&); bool slurp (const std::string&, std::vector &, 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::vector &); +void spit (const std::string&, const std::vector &, bool addNewlines = true); bool taskDiff (const Task&, const Task&); std::string taskDifferences (const Task&, const Task&);