From dc4c2b8d50fa7014bccfc5251c7a90d62926ddb6 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Mon, 14 Feb 2005 07:45:09 +0000 Subject: [PATCH] Added the new transaction handler `truncate_entries' which can be used to trim a number of entries from the beginning or end of a transction stream. (push_to_transactions_list): Removed unneeded "handler" argument. --- walk.cc | 47 +++++++++++++++++++++++++++++++++++++++++++++++ walk.h | 24 +++++++++++++++++++++--- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/walk.cc b/walk.cc index da7db316..a402ebf5 100644 --- a/walk.cc +++ b/walk.cc @@ -56,6 +56,53 @@ void add_transaction_to(const transaction_t& xact, value_t& value) value = xact.amount; } +void truncate_entries::flush() +{ + if (! xacts.size()) + return; + + entry_t * last_entry = (*xacts.begin())->entry; + + int l = 0; + for (transactions_list::iterator x = xacts.begin(); + x != xacts.end(); + x++) + if (last_entry != (*x)->entry) { + l++; + last_entry = (*x)->entry; + } + l++; + + last_entry = (*xacts.begin())->entry; + + int i = 0; + for (transactions_list::iterator x = xacts.begin(); + x != xacts.end(); + x++) { + if (last_entry != (*x)->entry) { + last_entry = (*x)->entry; + i++; + } + + bool print = false; + if (tailwise) { + if (count > 0 && l - i <= count) + print = true; + else if (count < 0 && l - i > - count) + print = true; + } else { + if (count > 0 && i < count) + print = true; + else if (count < 0 && i >= - count) + print = true; + } + + if (print) + item_handler::operator()(**x); + } + item_handler::flush(); +} + void set_account_value::operator()(transaction_t& xact) { add_transaction_to(xact, account_xdata(*xact.account).value); diff --git a/walk.h b/walk.h index 871a9639..c8e15a88 100644 --- a/walk.h +++ b/walk.h @@ -145,6 +145,25 @@ class ignore_transactions : public item_handler virtual void operator()(transaction_t& xact) {} }; +class truncate_entries : public item_handler +{ + int count; + bool tailwise; + + transactions_list xacts; + + public: + truncate_entries(item_handler * handler, + int _count, bool _tailwise = false) + : item_handler(handler), + count(_count), tailwise(_tailwise) {} + + virtual void flush(); + virtual void operator()(transaction_t& xact) { + xacts.push_back(&xact); + } +}; + class set_account_value : public item_handler { public: @@ -159,9 +178,8 @@ class push_to_transactions_list : public item_handler public: transactions_list& xact_list; - push_to_transactions_list(transactions_list& _xact_list, - item_handler * handler = NULL) - : item_handler(handler), xact_list(_xact_list) {} + push_to_transactions_list(transactions_list& _xact_list) + : xact_list(_xact_list) {} virtual void operator()(transaction_t& xact) { xact_list.push_back(&xact);