diff options
author | John Wiegley <johnw@newartisans.com> | 2012-03-23 16:46:11 -0500 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2012-03-23 16:46:11 -0500 |
commit | 4b057599626538a0d1463c63f2110d2fe5baa2e6 (patch) | |
tree | 1934a6b1bd6c47ff3599c9aaaeb211fb2303c86f /src/balance.cc | |
parent | 38813bc635e587f0979ef142511c5e7655fd88f1 (diff) | |
download | fork-ledger-4b057599626538a0d1463c63f2110d2fe5baa2e6.tar.gz fork-ledger-4b057599626538a0d1463c63f2110d2fe5baa2e6.tar.bz2 fork-ledger-4b057599626538a0d1463c63f2110d2fe5baa2e6.zip |
Fixed sorting of equity output
Diffstat (limited to 'src/balance.cc')
-rw-r--r-- | src/balance.cc | 102 |
1 files changed, 71 insertions, 31 deletions
diff --git a/src/balance.cc b/src/balance.cc index 5c57cd99..cd25eede 100644 --- a/src/balance.cc +++ b/src/balance.cc @@ -240,17 +240,8 @@ balance_t::strip_annotations(const keep_details_t& what_to_keep) const return temp; } -void balance_t::print(std::ostream& out, - const int first_width, - const int latter_width, - const uint_least8_t flags) const +void balance_t::map_sorted_amounts(function<void(const amount_t&)> fn) const { - bool first = true; - int lwidth = latter_width; - - if (lwidth == -1) - lwidth = first_width; - typedef std::vector<const amount_t *> amounts_array; amounts_array sorted; @@ -261,30 +252,79 @@ void balance_t::print(std::ostream& out, std::stable_sort(sorted.begin(), sorted.end(), commodity_t::compare_by_commodity()); - foreach (const amount_t * amount, sorted) { - int width; - if (! first) { - out << std::endl; - width = lwidth; - } else { - first = false; - width = first_width; + foreach (const amount_t * amount, sorted) + fn(*amount); +} + +namespace { + struct print_amount_from_balance + { + std::ostream& out; + bool& first; + int fwidth; + int lwidth; + uint_least8_t flags; + + explicit print_amount_from_balance(std::ostream& _out, + bool& _first, + int _fwidth, int _lwidth, + uint_least8_t _flags) + : out(_out), first(_first), fwidth(_fwidth), lwidth(_lwidth), + flags(_flags) { + TRACE_CTOR(print_amount_from_balance, + "ostream&, int, int, uint_least8_t"); + } + print_amount_from_balance(const print_amount_from_balance& other) + : out(other.out), first(other.first), fwidth(other.fwidth), + lwidth(other.lwidth), flags(other.flags) { + TRACE_CTOR(print_amount_from_balance, "copy"); + } + ~print_amount_from_balance() throw() { + TRACE_DTOR(print_amount_from_balance); } - std::ostringstream buf; - amount->print(buf, flags); - justify(out, buf.str(), width, flags & AMOUNT_PRINT_RIGHT_JUSTIFY, - flags & AMOUNT_PRINT_COLORIZE && amount->sign() < 0); - } + void operator()(const amount_t& amount) { + int width; + if (! first) { + out << std::endl; + width = lwidth; + } else { + first = false; + width = fwidth; + } + + std::ostringstream buf; + amount.print(buf, flags); + + justify(out, buf.str(), width, + flags & AMOUNT_PRINT_RIGHT_JUSTIFY, + flags & AMOUNT_PRINT_COLORIZE && amount.sign() < 0); + } - if (first) { - out.width(first_width); - if (flags & AMOUNT_PRINT_RIGHT_JUSTIFY) - out << std::right; - else - out << std::left; - out << 0; - } + void close() { + out.width(fwidth); + if (flags & AMOUNT_PRINT_RIGHT_JUSTIFY) + out << std::right; + else + out << std::left; + out << 0; + } + }; +} + +void balance_t::print(std::ostream& out, + const int first_width, + const int latter_width, + const uint_least8_t flags) const +{ + bool first = true; + print_amount_from_balance + amount_printer(out, first, first_width, + latter_width == 1 ? first_width : latter_width, flags); + map_sorted_amounts(amount_printer); + + if (first) + amount_printer.close(); } void to_xml(std::ostream& out, const balance_t& bal) |