diff options
author | John Wiegley <johnw@newartisans.com> | 2003-10-01 05:39:06 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2003-10-01 05:39:06 +0000 |
commit | 68e6b8538e9c6fd39cb5d6ff80c65f3181763413 (patch) | |
tree | 50e4e14056459b22d0f923b0ab409f080157b694 | |
parent | 9d5172792af05321a05e63b186f477c2af93b2ea (diff) | |
download | fork-ledger-68e6b8538e9c6fd39cb5d6ff80c65f3181763413.tar.gz fork-ledger-68e6b8538e9c6fd39cb5d6ff80c65f3181763413.tar.bz2 fork-ledger-68e6b8538e9c6fd39cb5d6ff80c65f3181763413.zip |
*** empty log message ***
-rw-r--r-- | balance.cc | 87 | ||||
-rw-r--r-- | ledger.cc | 8 | ||||
-rw-r--r-- | ledger.h | 4 |
3 files changed, 56 insertions, 43 deletions
@@ -16,48 +16,55 @@ static bool show_empty; static bool no_subtotals; static bool full_names; +static bool account_matches(const account * acct, + const std::list<mask>& regexps, + bool * true_match) +{ + bool match = true; + + if (show_children) { + match = false; + for (const account * a = acct; a; a = a->parent) { + bool exclude = false; + if (matches(regexps, a->name, &exclude)) { + match = true; + *true_match = a == acct; + break; + } + if (exclude) + break; + } + } else { + match = matches(regexps, acct->as_str()); + *true_match = matches(regexps, acct->name); + } + return match; +} + static void display_total(std::ostream& out, totals& total_balance, - const account * acct, + 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)) { - bool match = true; - bool true_match = false; - if (! regexps.empty()) { - if (show_children) { - match = false; - for (const account * a = acct; a; a = a->parent) { - if (matches(regexps, a->name)) { - match = true; - true_match = a == acct; - break; - } - } - } else { - match = matches(regexps, acct->as_str()); - true_match = matches(regexps, acct->name); - } - } - - if (match) { - out << *balance; - - if (! acct->parent || true_match) - 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; - } + 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; } } } @@ -67,7 +74,8 @@ 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, balances, regexps); + display_total(out, total_balance, (*i).second, ! displayed, + balances, regexps); } ////////////////////////////////////////////////////////////////////// @@ -125,8 +133,11 @@ void report_balances(int argc, char **argv, std::ostream& out) account * acct = (*x)->acct; for (; acct; acct = no_subtotals ? NULL : acct->parent) { - if (! show_children && acct->parent && - (regexps.empty() || ! matches(regexps, acct->name))) + bool true_match = false; + if (! (regexps.empty() || + account_matches(acct, regexps, &true_match))) + break; + else if (! (true_match || show_children || ! acct->parent)) continue; totals * balance = NULL; @@ -140,14 +151,12 @@ void report_balances(int argc, char **argv, std::ostream& out) } 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; @@ -164,7 +173,7 @@ void report_balances(int argc, char **argv, std::ostream& out) for (accounts_iterator i = main_ledger.accounts.begin(); i != main_ledger.accounts.end(); i++) - display_total(out, total_balance, (*i).second, balances, regexps); + display_total(out, total_balance, (*i).second, true, balances, regexps); // Print the total of all the balances shown @@ -212,7 +212,8 @@ void read_regexps(const char * path, std::list<mask>& regexps) } } -bool matches(const std::list<mask>& regexps, const std::string& str) +bool matches(const std::list<mask>& regexps, const std::string& str, + bool * exclude) { // If the first pattern is an exclude, we assume all patterns match // if they don't match the exclude. If the first pattern is an @@ -225,8 +226,11 @@ bool matches(const std::list<mask>& regexps, const std::string& str) r++) { int ovec[3]; if (pcre_exec((*r).regexp, NULL, str.c_str(), str.length(), - 0, 0, ovec, 3) >= 0) + 0, 0, ovec, 3) >= 0) { + if (exclude) + *exclude = (*r).exclude; match = ! (*r).exclude; + } } return match; @@ -1,5 +1,5 @@ #ifndef _LEDGER_H -#define _LEDGER_H "$Revision: 1.10 $" +#define _LEDGER_H "$Revision: 1.11 $" ////////////////////////////////////////////////////////////////////// // @@ -159,7 +159,7 @@ extern std::list<mask> regexps; extern void record_regexp(char * pattern, std::list<mask>& regexps); extern void read_regexps(const char * path, std::list<mask>& regexps); extern bool matches(const std::list<mask>& regexps, - const std::string& str); + const std::string& str, bool * exclude = NULL); struct account; |