From a4f5abe860025c8f4120b3bfb7aea7e555692153 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Thu, 12 Aug 2004 23:21:57 -0400 Subject: Added a days-of-the-week report, under -w --- Makefile | 7 +++-- main.cc | 24 +++++++++++----- walk.cc | 35 +++++++++++++++++------ walk.h | 98 ++++++++++++++++++++++++++++++++++++++-------------------------- 4 files changed, 106 insertions(+), 58 deletions(-) diff --git a/Makefile b/Makefile index 0ae93fc2..9f21de36 100644 --- a/Makefile +++ b/Makefile @@ -85,12 +85,12 @@ include make.deps # These next rules are for my own use. README.html: README - (cd $(HOME)/src/muse && \ + (cd $(HOME)/Projects/muse && \ ./publish --html $(shell pwd)/README && \ mv README.html $(shell pwd)) ledger.texi: README - (cd $(HOME)/src/muse && \ + (cd $(HOME)/Projects/muse && \ ./publish --texi $(shell pwd)/README && \ cat README.texi | sed 's/README\.info/ledger.info/g' \ > $(shell pwd)/ledger.texi && \ @@ -101,7 +101,8 @@ VERSION = $(shell scripts/version) dist: rm -fr /tmp/ledger-$(VERSION) rsync -av --exclude=".*" --exclude="TAGS" --exclude="version" \ - --exclude="_darcs/" --exclude="ledger.dat" \ + --exclude="_darcs/" --exclude="ledger.dat" --exclude="CVS/" \ + --exclude="1.7/" --exclude="gmon.out" --exclude="prof.out" \ $(shell pwd)/ /tmp/ledger-$(VERSION) (cd /tmp/ledger-$(VERSION) && \ make fullclean && \ diff --git a/main.cc b/main.cc index 661feb25..6400bf11 100644 --- a/main.cc +++ b/main.cc @@ -163,12 +163,12 @@ int main(int argc, char * argv[]) std::string total_expr = "T"; std::time_t interval_begin = 0; - bool show_subtotals = true; - bool show_expanded = false; - bool show_related = false; - bool show_inverted = false; - bool show_empty = false; - + bool show_subtotals = true; + bool show_expanded = false; + bool show_related = false; + bool show_inverted = false; + bool show_empty = false; + bool days_of_the_week = false; bool show_revalued = false; bool show_revalued_only = false; @@ -221,7 +221,7 @@ int main(int argc, char * argv[]) int c, index; while (-1 != (c = getopt(argc, argv, - "+ABb:Ccd:DEe:F:f:Ghi:JjL:l:MnOo:P:p:QRrS:sT:t:UVvWXYy:Zz:"))) { + "+ABb:Ccd:DEe:F:f:Ghi:JjL:l:MnOo:P:p:QRrS:sT:t:UVvWwXYy:Zz:"))) { switch (char(c)) { // Basic options case 'h': @@ -347,6 +347,10 @@ int main(int argc, char * argv[]) report_interval.reset(new interval_t(604800, 0, 0)); break; + case 'w': + days_of_the_week = true; + break; + case 'M': report_interval.reset(new interval_t(0, 1, 0)); break; @@ -745,12 +749,18 @@ int main(int argc, char * argv[]) // interval_transactions is like subtotal_transactions, but it // subtotals according to time intervals rather than totalling // everything. + // + // dow_transactions is like interval_transactions, except that it + // reports all the transactions that fall on each subsequent day + // of the week. if (show_expanded) formatter.reset(new subtotal_transactions(formatter.release())); else if (report_interval.get()) formatter.reset(new interval_transactions(formatter.release(), *report_interval.get(), interval_begin)); + else if (days_of_the_week) + formatter.reset(new dow_transactions(formatter.release())); // related_transactions will pass along all transactions related // to the transaction received. If `show_all_related' is true, diff --git a/walk.cc b/walk.cc index b0769ed6..a13ba06c 100644 --- a/walk.cc +++ b/walk.cc @@ -117,19 +117,24 @@ void changed_value_transactions::operator()(transaction_t * xact) last_xact = xact; } -void subtotal_transactions::flush() +void subtotal_transactions::flush(const char * spec_fmt) { - entry_t * entry = new entry_t; - char buf[256]; - std::string fmt = "- "; - fmt += format_t::date_format; - // Make sure the end date is inclusive - if (start != finish) - finish -= 86400; + if (! spec_fmt) { + std::string fmt = "- "; + fmt += format_t::date_format; + + // Make sure the end date is inclusive + if (start != finish) + finish -= 86400; + + std::strftime(buf, 255, fmt.c_str(), std::gmtime(&finish)); + } else { + std::strftime(buf, 255, spec_fmt, std::gmtime(&finish)); + } - std::strftime(buf, 255, fmt.c_str(), std::gmtime(&finish)); + entry_t * entry = new entry_t; entry->payee = buf; entry_temps.push_back(entry); @@ -211,4 +216,16 @@ void interval_transactions::operator()(transaction_t * xact) last_xact = xact; } +void dow_transactions::flush() +{ + for (int i = 0; i < 7; i++) { + for (transactions_deque::iterator d = days_of_the_week[i].begin(); + d != days_of_the_week[i].end(); + d++) + subtotal_transactions::operator()(*d); + subtotal_transactions::flush("%As"); + days_of_the_week[i].clear(); + } +} + } // namespace ledger diff --git a/walk.h b/walk.h index 145fb10f..b43b8e4d 100644 --- a/walk.h +++ b/walk.h @@ -47,6 +47,44 @@ struct compare_items { typedef std::deque transactions_deque; typedef std::deque entries_deque; +inline void walk_transactions(transactions_list::iterator begin, + transactions_list::iterator end, + item_handler& handler) { + for (transactions_list::iterator i = begin; i != end; i++) + handler(*i); +} + +inline void walk_transactions(transactions_list& list, + item_handler& handler) { + walk_transactions(list.begin(), list.end(), handler); +} + +inline void walk_transactions(transactions_deque::iterator begin, + transactions_deque::iterator end, + item_handler& handler) { + for (transactions_deque::iterator i = begin; i != end; i++) + handler(*i); +} + +inline void walk_transactions(transactions_deque& deque, + item_handler& handler) { + walk_transactions(deque.begin(), deque.end(), handler); +} + +inline void walk_entries(entries_list::iterator begin, + entries_list::iterator end, + item_handler& handler) { + for (entries_list::iterator i = begin; i != end; i++) + walk_transactions((*i)->transactions, handler); +} + +inline void walk_entries(entries_list& list, + item_handler& handler) { + walk_entries(list.begin(), list.end(), handler); +} + +////////////////////////////////////////////////////////////////////// + class ignore_transactions : public item_handler { public: @@ -267,7 +305,7 @@ class subtotal_transactions : public item_handler delete *i; } - virtual void flush(); + virtual void flush(const char * spec_fmt = NULL); virtual void operator()(transaction_t * xact); }; @@ -292,6 +330,26 @@ class interval_transactions : public subtotal_transactions virtual void operator()(transaction_t * xact); }; +class dow_transactions : public subtotal_transactions +{ + transactions_deque days_of_the_week[7]; + + public: + dow_transactions(item_handler * handler) + : subtotal_transactions(handler) {} + + virtual ~dow_transactions() { + flush(); + } + + virtual void flush(); + + virtual void operator()(transaction_t * xact) { + struct std::tm * desc = std::gmtime(&xact->entry->date); + days_of_the_week[desc->tm_wday].push_back(xact); + } +}; + class related_transactions : public item_handler { bool also_matching; @@ -321,44 +379,6 @@ class related_transactions : public item_handler } }; -////////////////////////////////////////////////////////////////////// - -inline void walk_transactions(transactions_list::iterator begin, - transactions_list::iterator end, - item_handler& handler) { - for (transactions_list::iterator i = begin; i != end; i++) - handler(*i); -} - -inline void walk_transactions(transactions_list& list, - item_handler& handler) { - walk_transactions(list.begin(), list.end(), handler); -} - -inline void walk_transactions(transactions_deque::iterator begin, - transactions_deque::iterator end, - item_handler& handler) { - for (transactions_deque::iterator i = begin; i != end; i++) - handler(*i); -} - -inline void walk_transactions(transactions_deque& deque, - item_handler& handler) { - walk_transactions(deque.begin(), deque.end(), handler); -} - -inline void walk_entries(entries_list::iterator begin, - entries_list::iterator end, - item_handler& handler) { - for (entries_list::iterator i = begin; i != end; i++) - walk_transactions((*i)->transactions, handler); -} - -inline void walk_entries(entries_list& list, - item_handler& handler) { - walk_entries(list.begin(), list.end(), handler); -} - ////////////////////////////////////////////////////////////////////// // -- cgit v1.2.3