summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--format.cc67
-rw-r--r--format.h37
-rw-r--r--main.cc3
-rw-r--r--walk.h86
4 files changed, 128 insertions, 65 deletions
diff --git a/format.cc b/format.cc
index 94a325d3..6922c601 100644
--- a/format.cc
+++ b/format.cc
@@ -293,4 +293,71 @@ void format_t::format_elements(std::ostream& out,
}
}
+void format_transaction::operator()(transaction_t * xact)
+{
+ if (inverted) {
+ xact->total.quantity += - xact->amount;
+ xact->total.cost += - xact->cost;
+ } else {
+ xact->total += *xact;
+ }
+ xact->index = index++;
+
+ // This makes the assumption that transactions from a single entry
+ // will always be grouped together.
+
+ if (last_entry != xact->entry)
+ first_line_format.format_elements(output_stream, details_t(xact));
+ else
+ next_lines_format.format_elements(output_stream, details_t(xact));
+
+ last_entry = xact->entry;
+}
+
+void format_account::operator()(const account_t * account,
+ const unsigned int max_depth,
+ const bool report_top)
+{
+ // Don't output the account if only one child will be displayed
+ // which shows the exact same amount. jww (2004-08-03): How do
+ // compute the right figure? It should a value expression specified
+ // by the user, to say, "If this expression is equivalent between a
+ // parent account and a lone displayed child, then don't display the
+ // parent."
+
+ if (bool output = report_top || account->parent != NULL) {
+ int counted = 0;
+ bool display = false;
+
+ for (accounts_map::const_iterator i = account->accounts.begin();
+ i != account->accounts.end();
+ i++) {
+ if (! (*i).second->total)
+ continue;
+
+ if ((*i).second->total != account->total || counted > 0) {
+ display = true;
+ break;
+ }
+ counted++;
+ }
+
+ if (counted == 1 && ! display)
+ output = false;
+
+ if (output) {
+ unsigned int depth = account->depth;
+ if (max_depth == 0 || depth <= max_depth) {
+ for (const account_t * acct = account;
+ depth > 0 && acct && acct != last_account;
+ acct = acct->parent)
+ depth--;
+
+ format.format_elements(output_stream, details_t(account, depth));
+ last_account = account;
+ }
+ }
+ }
+}
+
} // namespace ledger
diff --git a/format.h b/format.h
index c61518be..71ebdf5e 100644
--- a/format.h
+++ b/format.h
@@ -82,6 +82,43 @@ struct format_t
}
};
+class format_transaction
+{
+ std::ostream& output_stream;
+ const format_t& first_line_format;
+ const format_t& next_lines_format;
+ const bool inverted;
+ unsigned int index;
+ entry_t * last_entry;
+
+ public:
+ format_transaction(std::ostream& _output_stream,
+ const format_t& _first_line_format,
+ const format_t& _next_lines_format,
+ const bool _inverted)
+ : output_stream(_output_stream),
+ first_line_format(_first_line_format),
+ next_lines_format(_next_lines_format),
+ inverted(_inverted), index(0), last_entry(NULL) {}
+
+ void operator()(transaction_t * xact);
+};
+
+class format_account
+{
+ std::ostream& output_stream;
+ const format_t& format;
+ const account_t * last_account;
+
+ public:
+ format_account(std::ostream& _output_stream, const format_t& _format)
+ : output_stream(_output_stream), format(_format) {}
+
+ void operator()(const account_t * account,
+ const unsigned int max_depth = 1,
+ const bool report_top = false);
+};
+
} // namespace ledger
#endif // _REPORT_H
diff --git a/main.cc b/main.cc
index 31bd32e1..23b29cc3 100644
--- a/main.cc
+++ b/main.cc
@@ -765,7 +765,8 @@ int main(int argc, char * argv[])
format_t format(f);
walk_accounts(journal->master, format_account(std::cout, format),
predicate.get(), xact_display_flags, show_subtotals,
- display_predicate.get(), sort_order.get());
+ show_expanded ? 0 : 1, display_predicate.get(),
+ sort_order.get());
if (! display_predicate.get() ||
item_predicate<account_t>(display_predicate.get())(journal->master)) {
diff --git a/walk.h b/walk.h
index 2cc6f2e3..b3c39f88 100644
--- a/walk.h
+++ b/walk.h
@@ -3,7 +3,6 @@
#include "ledger.h"
#include "balance.h"
-#include "format.h"
#include "valexpr.h"
#include <iostream>
@@ -30,40 +29,6 @@ class item_predicate
}
};
-inline void add_to_balance_pair(balance_pair_t& balance,
- transaction_t * xact,
- const bool inverted = false)
-{
- if (inverted) {
- balance.quantity += - xact->amount;
- balance.cost += - xact->cost;
- } else {
- balance += *xact;
- }
-}
-
-class format_transaction
-{
- std::ostream& output_stream;
- const format_t& first_line_format;
- const format_t& next_lines_format;
- const bool inverted;
- unsigned int index;
- entry_t * last_entry;
-
- public:
- format_transaction(std::ostream& _output_stream,
- const format_t& _first_line_format,
- const format_t& _next_lines_format,
- const bool _inverted)
- : output_stream(_output_stream),
- first_line_format(_first_line_format),
- next_lines_format(_next_lines_format),
- inverted(_inverted), index(0), last_entry(NULL) {}
-
- void operator()(transaction_t * xact);
-};
-
template <typename T>
struct compare_items {
const node_t * sort_order;
@@ -205,19 +170,6 @@ void walk_transactions(transactions_deque::iterator begin,
functor(*i);
}
-class format_account
-{
- std::ostream& output_stream;
- const format_t& format;
- const account_t * last_account;
-
- public:
- format_account(std::ostream& _output_stream, const format_t& _format)
- : output_stream(_output_stream), format(_format) {}
-
- void operator()(const account_t * account, bool report_top = false);
-};
-
typedef std::deque<account_t *> accounts_deque;
inline void sort_accounts(account_t * account,
@@ -234,25 +186,29 @@ inline void sort_accounts(account_t * account,
}
template <typename Function>
-void walk__accounts(const account_t * account, Function functor,
+void walk__accounts(const account_t * account,
+ Function functor,
+ const unsigned int max_depth,
item_predicate<account_t>& disp_pred_functor)
{
if (disp_pred_functor(account))
- functor(account);
+ functor(account, max_depth);
for (accounts_map::const_iterator i = account->accounts.begin();
i != account->accounts.end();
i++)
- walk__accounts((*i).second, functor, disp_pred_functor);
+ walk__accounts((*i).second, functor, max_depth, disp_pred_functor);
}
template <typename Function>
-void walk__accounts_sorted(const account_t * account, Function functor,
- const node_t * sort_order,
+void walk__accounts_sorted(const account_t * account,
+ Function functor,
+ const unsigned int max_depth,
+ const node_t * sort_order,
item_predicate<account_t>& disp_pred_functor)
{
if (disp_pred_functor(account))
- functor(account);
+ functor(account, max_depth);
accounts_deque accounts;
@@ -267,7 +223,8 @@ void walk__accounts_sorted(const account_t * account, Function functor,
for (accounts_deque::const_iterator i = accounts.begin();
i != accounts.end();
i++)
- walk__accounts_sorted(*i, functor, sort_order, disp_pred_functor);
+ walk__accounts_sorted(*i, functor, max_depth, sort_order,
+ disp_pred_functor);
}
template <typename Function>
@@ -297,13 +254,14 @@ inline void sum__accounts(account_t * account)
}
template <typename Function>
-void walk_accounts(account_t * account,
- Function functor,
- const node_t * predicate,
- unsigned int flags,
- const bool calc_subtotals,
- const node_t * display_predicate = NULL,
- const node_t * sort_order = NULL)
+void walk_accounts(account_t * account,
+ Function functor,
+ const node_t * predicate,
+ unsigned int flags,
+ const bool calc_subtotals,
+ const unsigned int max_depth,
+ const node_t * display_predicate = NULL,
+ const node_t * sort_order = NULL)
{
item_predicate<transaction_t> pred_functor(predicate);
item_predicate<account_t> disp_pred_functor(display_predicate);
@@ -313,10 +271,10 @@ void walk_accounts(account_t * account,
sum__accounts(account);
if (sort_order)
- walk__accounts_sorted<Function>(account, functor, sort_order,
+ walk__accounts_sorted<Function>(account, functor, max_depth, sort_order,
disp_pred_functor);
else
- walk__accounts<Function>(account, functor, disp_pred_functor);
+ walk__accounts<Function>(account, functor, max_depth, disp_pred_functor);
}
} // namespace ledger