summaryrefslogtreecommitdiff
path: root/balance.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2003-10-01 07:11:57 +0000
committerJohn Wiegley <johnw@newartisans.com>2003-10-01 07:11:57 +0000
commita40813d896fd929e92969c06229844073d58565d (patch)
tree055605364d055b20604c8d1423bede9628c2fcce /balance.cc
parent68e6b8538e9c6fd39cb5d6ff80c65f3181763413 (diff)
downloadfork-ledger-a40813d896fd929e92969c06229844073d58565d.tar.gz
fork-ledger-a40813d896fd929e92969c06229844073d58565d.tar.bz2
fork-ledger-a40813d896fd929e92969c06229844073d58565d.zip
*** empty log message ***
Diffstat (limited to 'balance.cc')
-rw-r--r--balance.cc94
1 files changed, 32 insertions, 62 deletions
diff --git a/balance.cc b/balance.cc
index d006f83e..fe8dbe16 100644
--- a/balance.cc
+++ b/balance.cc
@@ -4,17 +4,17 @@
namespace ledger {
-extern bool show_cleared;
+extern bool show_cleared;
extern std::time_t begin_date;
-extern bool have_beginning;
+extern bool have_beginning;
extern std::time_t end_date;
-extern bool have_ending;
+extern bool have_ending;
-static bool show_children;
-static bool show_empty;
-static bool no_subtotals;
-static bool full_names;
+static bool show_children;
+static bool show_empty;
+static bool no_subtotals;
+static bool full_names;
static bool account_matches(const account * acct,
const std::list<mask>& regexps,
@@ -41,31 +41,25 @@ static bool account_matches(const account * acct,
return match;
}
-static void display_total(std::ostream& out, totals& total_balance,
+static void display_total(std::ostream& out, totals& balance,
const account * acct, bool top_level,
- const std::map<account *, totals *>& balances,
const std::list<mask>& regexps)
{
bool displayed = false;
- std::map<account *, totals *>::const_iterator b =
- balances.find(const_cast<account *>(acct));
- if (b != balances.end()) {
- totals * balance = (*b).second;
- if (balance && (show_empty || *balance)) {
- displayed = true;
-
- out << *balance;
- if (top_level)
- total_balance.credit(*balance);
-
- if (acct->parent && ! no_subtotals && ! full_names) {
- for (const account * a = acct; a; a = a->parent)
- out << " ";
- out << acct->name << std::endl;
- } else {
- out << " " << *acct << std::endl;
- }
+ if (acct->display && (show_empty || acct->balance)) {
+ displayed = true;
+
+ out << acct->balance;
+ if (top_level)
+ balance.credit(acct->balance);
+
+ if (acct->parent && ! no_subtotals && ! full_names) {
+ for (const account * a = acct; a; a = a->parent)
+ out << " ";
+ out << acct->name << std::endl;
+ } else {
+ out << " " << *acct << std::endl;
}
}
@@ -74,8 +68,7 @@ static void display_total(std::ostream& out, totals& total_balance,
for (account::const_iterator i = acct->children.begin();
i != acct->children.end();
i++)
- display_total(out, total_balance, (*i).second, ! displayed,
- balances, regexps);
+ display_total(out, balance, (*i).second, ! displayed, regexps);
}
//////////////////////////////////////////////////////////////////////
@@ -122,11 +115,14 @@ void report_balances(int argc, char **argv, std::ostream& out)
// Walk through all of the ledger entries, computing the account
// totals
- std::map<account *, totals *> balances;
-
for (entries_iterator i = main_ledger.entries.begin();
i != main_ledger.entries.end();
i++) {
+ if ((have_beginning && difftime((*i)->date, begin_date) < 0) ||
+ (have_ending && difftime((*i)->date, end_date) >= 0) ||
+ (show_cleared && ! (*i)->cleared))
+ continue;
+
for (std::list<transaction *>::iterator x = (*i)->xacts.begin();
x != (*i)->xacts.end();
x++) {
@@ -140,27 +136,8 @@ void report_balances(int argc, char **argv, std::ostream& out)
else if (! (true_match || show_children || ! acct->parent))
continue;
- totals * balance = NULL;
-
- std::map<account *, totals *>::iterator t = balances.find(acct);
- if (t == balances.end()) {
- balance = new totals;
- balances.insert(std::pair<account *, totals *>(acct, balance));
- } else {
- balance = (*t).second;
- }
-
- bool do_credit = true;
- if (have_beginning && difftime((*i)->date, begin_date) < 0)
- do_credit = false;
- else if (have_ending && difftime((*i)->date, end_date) > 0)
- do_credit = false;
- else if (show_cleared && ! (*i)->cleared)
- do_credit = false;
- if (! do_credit)
- continue;
-
- balance->credit((*x)->cost->street());
+ acct->display = true;
+ acct->balance.credit((*x)->cost->street());
}
}
}
@@ -168,25 +145,18 @@ void report_balances(int argc, char **argv, std::ostream& out)
// Walk through all the top-level accounts, giving the balance
// report for each, and then for each of their children.
- totals total_balance;
+ totals balance;
for (accounts_iterator i = main_ledger.accounts.begin();
i != main_ledger.accounts.end();
i++)
- display_total(out, total_balance, (*i).second, true, balances, regexps);
+ display_total(out, balance, (*i).second, true, regexps);
// Print the total of all the balances shown
if (! no_subtotals)
out << "--------------------" << std::endl
- << total_balance << std::endl;
-
- // Free up temporary variables created on the heap
-
- for (std::map<account *, totals *>::iterator i = balances.begin();
- i != balances.end();
- i++)
- delete (*i).second;
+ << balance << std::endl;
}
} // namespace ledger