From abffaa184b37dfe62c6ba631c9dc7f9daac74dd5 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 16 Jan 2010 17:45:45 -0500 Subject: [PATCH] Enhancement - Path integration - Obsoleted util.cpp spit, slurp calls --- src/TDB.cpp | 21 ++++----- src/edit.cpp | 6 +-- src/import.cpp | 3 +- src/report.cpp | 2 +- src/tests/stringtable.t.cpp | 3 +- src/tests/util.t.cpp | 4 -- src/util.cpp | 86 ------------------------------------- src/util.h | 4 -- 8 files changed, 19 insertions(+), 110 deletions(-) diff --git a/src/TDB.cpp b/src/TDB.cpp index c2b9cabb7..f2c8d4a4a 100644 --- a/src/TDB.cpp +++ b/src/TDB.cpp @@ -36,6 +36,7 @@ #include "util.h" #include "TDB.h" #include "Directory.h" +#include "File.h" #include "Table.h" #include "Timer.h" #include "Color.h" @@ -513,7 +514,7 @@ void TDB::undo () // load undo.data std::vector u; - slurp (undoFile, u); + File::read (undoFile, u); if (u.size () < 3) throw std::string ("There are no recorded transactions to undo."); @@ -645,7 +646,7 @@ void TDB::undo () // load pending.data std::vector p; - slurp (pendingFile, p); + File::read (pendingFile, p); // is 'current' in pending? foreach (task, p) @@ -667,15 +668,15 @@ void TDB::undo () } // Rewrite files. - spit (pendingFile, p); - spit (undoFile, u); + File::write (pendingFile, p); + File::write (undoFile, u); return; } } // load completed.data std::vector c; - slurp (completedFile, c); + File::read (completedFile, c); // is 'current' in completed? foreach (task, c) @@ -691,17 +692,17 @@ void TDB::undo () { c.erase (task); p.push_back (prior); - spit (completedFile, c); - spit (pendingFile, p); - spit (undoFile, u); + File::write (completedFile, c); + File::write (pendingFile, p); + File::write (undoFile, u); std::cout << "Modified task reverted." << std::endl; context.debug ("TDB::undo - task belongs in pending.data"); } else { *task = prior; - spit (completedFile, c); - spit (undoFile, u); + File::write (completedFile, c); + File::write (undoFile, u); std::cout << "Modified task reverted." << std::endl; context.debug ("TDB::undo - task belongs in completed.data"); } diff --git a/src/edit.cpp b/src/edit.cpp index f24b920a0..14ec35bd0 100644 --- a/src/edit.cpp +++ b/src/edit.cpp @@ -533,7 +533,7 @@ void editFile (Task& task) // Format the contents, T -> text, write to a file. std::string before = formatTask (task); - spit (file.str (), before); + File::write (file.str (), before); // Determine correct editor: .taskrc:editor > $VISUAL > $EDITOR > vi std::string editor = context.config.get ("editor"); @@ -557,7 +557,7 @@ ARE_THESE_REALLY_HARMFUL: // Slurp file. std::string after; - slurp (file.str (), after, false); + File::read (file.str (), after); // Update task based on what can be parsed back out of the file, but only // if changes were made. @@ -584,7 +584,7 @@ ARE_THESE_REALLY_HARMFUL: // Preserve the edits. before = after; - spit (file.str (), before); + File::write (file.str (), before); if (confirm ("Task couldn't handle your edits. Would you like to try again?")) goto ARE_THESE_REALLY_HARMFUL; diff --git a/src/import.cpp b/src/import.cpp index 926fe135e..c6389ae63 100644 --- a/src/import.cpp +++ b/src/import.cpp @@ -28,6 +28,7 @@ #include #include #include +#include "File.h" #include "Date.h" #include "text.h" #include "util.h" @@ -1155,7 +1156,7 @@ int handleImport (std::string &outs) { // Load the file. std::vector all; - slurp (file, all, true); + File::read (file, all); std::vector lines; std::vector ::iterator it; diff --git a/src/report.cpp b/src/report.cpp index f13018295..1333d5cec 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -1728,7 +1728,7 @@ int handleReportStats (std::string &outs) dataSize += undo.size (); std::vector undoTxns; - slurp (undo, undoTxns, false); + File::read (undo, undoTxns); int undoCount = 0; foreach (tx, undoTxns) if (tx->substr (0, 3) == "---") diff --git a/src/tests/stringtable.t.cpp b/src/tests/stringtable.t.cpp index 625d283b7..b97c72811 100644 --- a/src/tests/stringtable.t.cpp +++ b/src/tests/stringtable.t.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -39,7 +40,7 @@ int main (int argc, char** argv) // Create a string file. std::string file = "./strings.xx-XX"; - spit (file, "# comment\n1 found"); + File::write (file, "# comment\n1 found"); t.is (access (file.c_str (), F_OK), 0, "strings.xx-XX created."); // Load the string file. diff --git a/src/tests/util.t.cpp b/src/tests/util.t.cpp index ecc6b796c..946eddbdc 100644 --- a/src/tests/util.t.cpp +++ b/src/tests/util.t.cpp @@ -514,10 +514,6 @@ int main (int argc, char** argv) // TODO const std::string uuid (); - // TODO bool slurp (const std::string&, std::vector &, bool trimLines = false); - // TODO bool slurp (const std::string&, std::string&, bool trimLines = false); - // TODO void spit (const std::string&, const std::string&); - // std::string taskDiff (const Task&, const Task&); Task left; left.set ("zero", "0"); diff --git a/src/util.cpp b/src/util.cpp index 69df5360b..a10e9289c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -375,92 +375,6 @@ int flock (int fd, int operation) } #endif -//////////////////////////////////////////////////////////////////////////////// -bool slurp ( - const std::string& file, - std::vector & contents, - bool trimLines /* = false */) -{ - contents.clear (); - - std::ifstream in (file.c_str ()); - if (in.good ()) - { - std::string line; - while (getline (in, line)) - { - if (trimLines) line = trim (line); - contents.push_back (line); - } - - in.close (); - return true; - } - - return false; -} - -//////////////////////////////////////////////////////////////////////////////// -bool slurp ( - const std::string& file, - std::string& contents, - bool trimLines /* = false */) -{ - contents = ""; - - std::ifstream in (file.c_str ()); - if (in.good ()) - { - std::string line; - while (getline (in, line)) - { - if (trimLines) line = trim (line); - contents += line + "\n"; - } - - in.close (); - return true; - } - - return false; -} - -//////////////////////////////////////////////////////////////////////////////// -void spit (const std::string& file, const std::string& contents) -{ - std::ofstream out (file.c_str ()); - if (out.good ()) - { - out << contents; - out.close (); - } - else - throw std::string ("Could not write file '") + file + "'"; // TODO i18n -} - -//////////////////////////////////////////////////////////////////////////////// -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 - throw std::string ("Could not write file '") + file + "'"; // TODO i18n -} - //////////////////////////////////////////////////////////////////////////////// bool taskDiff (const Task& before, const Task& after) { diff --git a/src/util.h b/src/util.h index 3dbd8b2ea..50fef817e 100644 --- a/src/util.h +++ b/src/util.h @@ -70,10 +70,6 @@ const std::string uuid (); int flock (int, int); #endif -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 &, bool addNewlines = true); bool taskDiff (const Task&, const Task&); std::string taskDifferences (const Task&, const Task&); std::string renderAttribute (const std::string&, const std::string&);