diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ecfbf14a..926935df 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,6 +14,7 @@ set (timew_SRCS CLI.cpp CLI.h Range.cpp Range.h Rules.cpp Rules.h Transaction.cpp Transaction.h + TransactionsFactory.cpp TransactionsFactory.h UndoAction.cpp UndoAction.h data.cpp dom.cpp diff --git a/src/Database.cpp b/src/Database.cpp index 59ca681c..604c3fcc 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include //////////////////////////////////////////////////////////////////////////////// @@ -236,6 +237,45 @@ void Database::recordIntervalAction ( recordUndoAction ("interval", before, after); } +//////////////////////////////////////////////////////////////////////////////// +Transaction Database::popLastTransaction () +{ + File undo (_location + "/undo.data"); + + std::vector read_lines; + undo.read (read_lines); + undo.close (); + + TransactionsFactory transactionsFactory; + + for (auto& line: read_lines) + { + transactionsFactory.parseLine (line); + } + + std::vector transactions = transactionsFactory.get (); + + if (transactions.empty ()) + { + return Transaction {}; + } + + Transaction last = transactions.back (); + transactions.pop_back (); + + File::remove (undo._data); + undo.open (); + + for (auto& transaction : transactions) + { + undo.append (transaction.toString ()); + } + + undo.close (); + + return last; +} + //////////////////////////////////////////////////////////////////////////////// std::string Database::dump () const { diff --git a/src/Database.h b/src/Database.h index 3139ba38..9f9373b4 100644 --- a/src/Database.h +++ b/src/Database.h @@ -54,6 +54,8 @@ public: void recordConfigAction(const std::string&, const std::string&); void recordIntervalAction(const std::string&, const std::string&); + Transaction popLastTransaction (); + std::string dump () const; private: diff --git a/src/Transaction.cpp b/src/Transaction.cpp index 194d8416..63257594 100644 --- a/src/Transaction.cpp +++ b/src/Transaction.cpp @@ -35,6 +35,11 @@ void Transaction::addUndoAction ( _actions.emplace_back (type, before, after); } +std::vector Transaction::getActions () +{ + return _actions; +} + std::string Transaction::toString () { std::string output = "txn:\n"; diff --git a/src/Transaction.h b/src/Transaction.h index 4a1dce86..5265789f 100644 --- a/src/Transaction.h +++ b/src/Transaction.h @@ -37,6 +37,8 @@ public: std::string toString(); + std::vector getActions (); + private: std::vector _actions {}; }; diff --git a/src/TransactionsFactory.cpp b/src/TransactionsFactory.cpp new file mode 100644 index 00000000..4a6068c7 --- /dev/null +++ b/src/TransactionsFactory.cpp @@ -0,0 +1,59 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Copyright 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// +// https://www.opensource.org/licenses/mit-license.php +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include "TransactionsFactory.h" +#include "Transaction.h" + +void TransactionsFactory::parseLine (const std::string& line) +{ + if (!line.compare(0, 4, "txn:")) + { + _transactions.emplace_back (); + } + else if (!line.compare(0, 7, " type:")) + { + _type = line.substr (8, line.size ()); + } + else if (!line.compare(0, 9, " before:")) + { + _before = line.substr (10, line.size ()); + } + else if (!line.compare(0, 8, " after:")) + { + _after = line.substr (9, line.size ()); + _transactions.back ().addUndoAction (_type, _before, _after); + } + else + { + throw "Cannot handle line '" + line + "'"; + } +} + +std::vector TransactionsFactory::get () +{ + return _transactions; +} diff --git a/src/TransactionsFactory.h b/src/TransactionsFactory.h new file mode 100644 index 00000000..173ec97c --- /dev/null +++ b/src/TransactionsFactory.h @@ -0,0 +1,49 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Copyright 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// +// https://www.opensource.org/licenses/mit-license.php +// +//////////////////////////////////////////////////////////////////////////////// + +#ifndef INCLUDED_TRANSACTIONSFACTORY +#define INCLUDED_TRANSACTIONSFACTORY + +#include +#include + +class TransactionsFactory +{ +public: + void parseLine(const std::string& line); + + std::vector< Transaction > get(); + +private: + std::string _type; + std::string _before; + std::string _after; + + std::vector< Transaction > _transactions {}; +}; + + +#endif //INCLUDED_TRANSACTIONSFACTORY diff --git a/src/commands/CmdUndo.cpp b/src/commands/CmdUndo.cpp index 76ceae96..d8beade1 100644 --- a/src/commands/CmdUndo.cpp +++ b/src/commands/CmdUndo.cpp @@ -31,6 +31,23 @@ //////////////////////////////////////////////////////////////////////////////// int CmdUndo (Database& database) { + Transaction transaction = database.popLastTransaction (); + + std::vector actions = transaction.getActions (); + + if (actions.empty ()) + { + // No (more) undoing... + } + else + { + for (auto& action : actions) + { + // Select database... + // Rollback action... + } + } + return 0; }