diff options
-rw-r--r-- | src/amount.cc | 14 | ||||
-rw-r--r-- | src/amount.h | 1 | ||||
-rw-r--r-- | src/balance.cc | 13 | ||||
-rw-r--r-- | src/unistring.h | 18 | ||||
-rw-r--r-- | src/value.cc | 17 | ||||
-rw-r--r-- | src/value.h | 4 |
6 files changed, 42 insertions, 25 deletions
diff --git a/src/amount.cc b/src/amount.cc index 1f273e9f..6fc049b9 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -1001,20 +1001,6 @@ void amount_t::print(std::ostream& _out) const _out << out.str(); } -void amount_t::right_justify(std::ostream& out, int width) const -{ - std::ostringstream buf; - buf << *this; - - unistring temp(buf.str()); - - int spacing = width - int(temp.length()); - while (spacing-- > 0) - out << ' '; - - out << temp.extract(); -} - bool amount_t::valid() const { if (quantity) { diff --git a/src/amount.h b/src/amount.h index d07fd18e..f0d0e92c 100644 --- a/src/amount.h +++ b/src/amount.h @@ -640,7 +640,6 @@ public: of its commodity's display precision. */ void print(std::ostream& out) const; - void right_justify(std::ostream& out, int width) const; /*@}*/ diff --git a/src/balance.cc b/src/balance.cc index 7f8ed572..92a6c767 100644 --- a/src/balance.cc +++ b/src/balance.cc @@ -30,6 +30,7 @@ */ #include "balance.h" +#include "unistring.h" namespace ledger { @@ -240,11 +241,17 @@ void balance_t::print(std::ostream& out, first = false; width = first_width; } - amount->right_justify(out, width); + + std::ostringstream buf; + buf << *amount; + justify(out, buf.str(), width, true); } - if (first) - amount_t(0L).right_justify(out, first_width); + if (first) { + std::ostringstream buf; + buf << amount_t(0L); + justify(out, buf.str(), first_width, true); + } } } // namespace ledger diff --git a/src/unistring.h b/src/unistring.h index 97156fde..b1c46cb3 100644 --- a/src/unistring.h +++ b/src/unistring.h @@ -90,4 +90,22 @@ public: } }; +inline void justify(std::ostream& out, + const std::string& str, + int width, + bool right = false) +{ + if (! right) + out << str; + + unistring temp(str); + + int spacing = width - int(temp.length()); + while (spacing-- > 0) + out << ' '; + + if (right) + out << str; +} + #endif // _UNISTRING_H diff --git a/src/value.cc b/src/value.cc index 3a8d1f14..96fb6647 100644 --- a/src/value.cc +++ b/src/value.cc @@ -30,6 +30,7 @@ */ #include "value.h" +#include "unistring.h" namespace ledger { @@ -1253,8 +1254,11 @@ void value_t::print(std::ostream& out, const int latter_width, const optional<string>& date_format) const { - if (first_width > 0 && ! is_amount() && ! is_balance()) + if (first_width > 0 && + ! is_amount() && ! is_balance() && ! is_string()) { out.width(first_width); + out << std::left; + } switch (type()) { case VOID: @@ -1283,15 +1287,18 @@ void value_t::print(std::ostream& out, out << as_long(); break; - case AMOUNT: + case AMOUNT: { + std::ostringstream buf; if (as_amount().is_zero()) - out << 0L; + buf << 0L; else - as_amount().right_justify(out, first_width); + buf << as_amount(); + justify(out, buf.str(), first_width, true); break; + } case STRING: - out << as_string(); + justify(out, as_string(), first_width); break; case MASK: diff --git a/src/value.h b/src/value.h index 299afa82..e3190ef4 100644 --- a/src/value.h +++ b/src/value.h @@ -886,7 +886,7 @@ public: * Printing methods. */ void print(std::ostream& out, - const int first_width, + const int first_width = - 1, const int latter_width = -1, const optional<string>& date_format = none) const; void dump(std::ostream& out, const bool relaxed = true) const; @@ -908,7 +908,7 @@ inline value_t mask_value(const string& str) { } inline std::ostream& operator<<(std::ostream& out, const value_t& val) { - val.print(out, 12); + val.print(out); return out; } |