From fb6dc5058fb82021e7346c49c5569a00e284e840 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 25 Aug 2011 00:29:54 -0400 Subject: [PATCH] TDB2 - Full implementation of TDB2::add, which includes pending/completed, undo and backlog. - Removed obsolete TF2::commitUndo, which is no longer necessary. - Added File::truncate method, to allow a TDB2 shortcut. --- src/File.cpp | 10 ++++ src/File.h | 2 + src/TDB2.cpp | 127 +++++++++++++++++++++++---------------------------- src/TDB2.h | 1 - 4 files changed, 69 insertions(+), 71 deletions(-) diff --git a/src/File.cpp b/src/File.cpp index fbe003d73..55bf44e94 100644 --- a/src/File.cpp +++ b/src/File.cpp @@ -280,6 +280,16 @@ void File::append (const std::vector & lines) } } +//////////////////////////////////////////////////////////////////////////////// +void File::truncate () +{ + if (!fh) + open (); + + if (fh) + ftruncate (h, 0); +} + //////////////////////////////////////////////////////////////////////////////// // S_IFMT 0170000 type of file // S_IFIFO 0010000 named pipe (fifo) diff --git a/src/File.h b/src/File.h index 5a60bbd8f..c5c41c5f3 100644 --- a/src/File.h +++ b/src/File.h @@ -65,6 +65,8 @@ public: void append (const std::string&); void append (const std::vector &); + void truncate (); + virtual mode_t mode (); virtual size_t size () const; virtual time_t mtime () const; diff --git a/src/TDB2.cpp b/src/TDB2.cpp index 29404b5c3..5224c6d90 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -28,6 +28,7 @@ #include // TODO Remove. #include #include +#include #include #include @@ -186,81 +187,41 @@ void TF2::commit () _added_lines.clear (); _file.close (); + _dirty = false; } } else { - // TODO _file.truncate (); - // TODO only write out _tasks, because any deltas have already been applied. - // TODO append _added_lines. + if (_file.open ()) + { + // Truncate the file and rewrite. + _file.truncate (); + + // only write out _tasks, because any deltas have already been applied. + std::vector ::iterator task; + for (task = _tasks.begin (); + task != _tasks.end (); + ++task) + { + _file.append (task->composeF4 ()); + } + + // Write out all the added lines. + std::vector ::iterator line; + for (line = _added_lines.begin (); + line != _added_lines.end (); + ++line) + { + _file.append (*line); + } + + _added_lines.clear (); + _file.close (); + _dirty = false; + } } - _dirty = false; } - - - // --------------------------- old implementation ------------------------- -/* - // Load the lowest form, to allow - if (_dirty) - { - load_contents (); - - if (_modified_tasks.size ()) - { - std::map modified; - std::vector ::iterator it; - for (it = _modified_tasks.begin (); it != _modified_tasks.end (); ++it) - modified[it->get ("uuid")] = *it; - -// for (it = _ - - _modified_tasks.clear (); - } - - if (_added_tasks.size ()) - { - std::vector ::iterator it; - for (it = _added_tasks.begin (); it != _added_tasks.end (); ++it) - _lines.push_back (it->composeF4 ()); - - _added_tasks.clear (); - } - - if (_added_lines.size ()) - { - //_lines += _added_lines; - _added_lines.clear (); - } - -// TODO This clobbers minimal case. - - _contents = ""; // TODO Verify no resize. - join (_contents, "\n", _lines); - _file.write (_contents); - - _dirty = false; - } -*/ -} - -//////////////////////////////////////////////////////////////////////////////// -void TF2::commitUndo () -{ -/* - for each _added_task - fprintf (file, - "time %u\nnew %s---\n", - (unsigned int) time (NULL), - after.composeF4 ().c_str ()); - - for each _modified_task - fprintf (file, - "time %u\nold %snew %s---\n", - (unsigned int) time (NULL), - before.composeF4 ().c_str (), - after.composeF4 ().c_str ()); -*/ } //////////////////////////////////////////////////////////////////////////////// @@ -470,6 +431,7 @@ void TDB2::add (const Task& task) if (!verifyUniqueUUID (task.get ("uuid"))) throw format ("Cannot add task because the uuid '{1}' is not unique.", task.get ("uuid")); + // Add new task to either pending or completed. std::string status = task.get ("status"); if (status == "completed" || status == "deleted") @@ -477,7 +439,15 @@ void TDB2::add (const Task& task) else pending.add_task (task); - undo.add_task (task); + // Add undo data lines: + // time