Moved amount_t::right_justify to simply ::justify

This commit is contained in:
John Wiegley 2009-02-19 01:22:22 -04:00
parent ec08dee745
commit 1fa3c1956f
6 changed files with 42 additions and 25 deletions

View file

@ -1001,20 +1001,6 @@ void amount_t::print(std::ostream& _out) const
_out << out.str(); _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 bool amount_t::valid() const
{ {
if (quantity) { if (quantity) {

View file

@ -640,7 +640,6 @@ public:
of its commodity's display precision. of its commodity's display precision.
*/ */
void print(std::ostream& out) const; void print(std::ostream& out) const;
void right_justify(std::ostream& out, int width) const;
/*@}*/ /*@}*/

View file

@ -30,6 +30,7 @@
*/ */
#include "balance.h" #include "balance.h"
#include "unistring.h"
namespace ledger { namespace ledger {
@ -240,11 +241,17 @@ void balance_t::print(std::ostream& out,
first = false; first = false;
width = first_width; width = first_width;
} }
amount->right_justify(out, width);
std::ostringstream buf;
buf << *amount;
justify(out, buf.str(), width, true);
} }
if (first) if (first) {
amount_t(0L).right_justify(out, first_width); std::ostringstream buf;
buf << amount_t(0L);
justify(out, buf.str(), first_width, true);
}
} }
} // namespace ledger } // namespace ledger

View file

@ -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 #endif // _UNISTRING_H

View file

@ -30,6 +30,7 @@
*/ */
#include "value.h" #include "value.h"
#include "unistring.h"
namespace ledger { namespace ledger {
@ -1253,8 +1254,11 @@ void value_t::print(std::ostream& out,
const int latter_width, const int latter_width,
const optional<string>& date_format) const 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.width(first_width);
out << std::left;
}
switch (type()) { switch (type()) {
case VOID: case VOID:
@ -1283,15 +1287,18 @@ void value_t::print(std::ostream& out,
out << as_long(); out << as_long();
break; break;
case AMOUNT: case AMOUNT: {
std::ostringstream buf;
if (as_amount().is_zero()) if (as_amount().is_zero())
out << 0L; buf << 0L;
else else
as_amount().right_justify(out, first_width); buf << as_amount();
justify(out, buf.str(), first_width, true);
break; break;
}
case STRING: case STRING:
out << as_string(); justify(out, as_string(), first_width);
break; break;
case MASK: case MASK:

View file

@ -886,7 +886,7 @@ public:
* Printing methods. * Printing methods.
*/ */
void print(std::ostream& out, void print(std::ostream& out,
const int first_width, const int first_width = - 1,
const int latter_width = -1, const int latter_width = -1,
const optional<string>& date_format = none) const; const optional<string>& date_format = none) const;
void dump(std::ostream& out, const bool relaxed = true) 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) { inline std::ostream& operator<<(std::ostream& out, const value_t& val) {
val.print(out, 12); val.print(out);
return out; return out;
} }