summaryrefslogtreecommitdiff
path: root/format.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2008-08-02 06:42:36 -0400
committerJohn Wiegley <johnw@newartisans.com>2008-08-02 06:42:36 -0400
commit9a9e06554eb9f57be8c839fb0af49a0473614172 (patch)
treec31019afc5a482ff3daeb8cf672af22a0910d5a3 /format.cc
parent5bf3f536b37e77b5dd663fffbd32e71b403d2c7a (diff)
downloadfork-ledger-9a9e06554eb9f57be8c839fb0af49a0473614172.tar.gz
fork-ledger-9a9e06554eb9f57be8c839fb0af49a0473614172.tar.bz2
fork-ledger-9a9e06554eb9f57be8c839fb0af49a0473614172.zip
Formatting now relies exclusively on value expressions.
What this means is that the utility code, basic math, value expressions, string formatting and option handling are now entirely decoupled from the rest of the code. This decoupling not only greatly simplifies the more basic parts of Ledger, but makes it much easier to test and verify its completeness. For example, when the formatting code %X is seen by the format parser, it turns into a call to the expression function fmt_X, which must be defined when the format string is first compiled against an object. If that object is a transaction, the transaction's scope will be the first to have a chance at providing a definition. If an account is being reported, it will. If neither does, the next scope in sequence -- soon to be the current report -- will, and then the session object that "owns" the current Ledger session. In 2.6, the formatting code new everything about transaction and accounts, and relied on flags to communicate special details between them. Now the transaction will offer the details for its own reporting, while the formatter worries only about strings and how to output them.
Diffstat (limited to 'format.cc')
-rw-r--r--format.cc34
1 files changed, 24 insertions, 10 deletions
diff --git a/format.cc b/format.cc
index 70683e48..1255d25b 100644
--- a/format.cc
+++ b/format.cc
@@ -90,7 +90,6 @@ format_t::element_t * format_t::parse_elements(const string& fmt)
// T: TOTAL
// N: NOTE
// n: OPT_NOTE
- // |: SPACER
// _: DEPTH_SPACER
//
// xB: XACT_BEG_POS
@@ -173,6 +172,11 @@ format_t::element_t * format_t::parse_elements(const string& fmt)
current->chars = "%";
break;
+ case '|':
+ current->type = element_t::STRING;
+ current->chars = " ";
+ break;
+
case '(':
case '[': {
std::istringstream str(p);
@@ -183,11 +187,16 @@ format_t::element_t * format_t::parse_elements(const string& fmt)
break;
}
- default:
- current->type = element_t::EXPR;
+ default: {
+ current->type = element_t::EXPR;
+ char buf[2];
+ buf[0] = *p;
+ buf[1] = '\0';
+ current->chars = buf;
current->expr.parse(string("fmt_") + *p);
break;
}
+ }
}
if (q != buf) {
@@ -211,9 +220,9 @@ namespace {
}
}
-void format_t::format(std::ostream& out_str, scope_t& scope) const
+void format_t::format(std::ostream& out_str, scope_t& scope)
{
- for (const element_t * elem = elements.get(); elem; elem = elem->next.get()) {
+ for (element_t * elem = elements.get(); elem; elem = elem->next.get()) {
std::ostringstream out;
string name;
bool ignore_max_width = false;
@@ -232,7 +241,16 @@ void format_t::format(std::ostream& out_str, scope_t& scope) const
break;
case element_t::EXPR:
- out << elem->expr.calc(scope);
+ try {
+ if (elem->max_width == 0)
+ elem->expr.calc(scope).dump(out, elem->min_width);
+ else
+ out << truncate(elem->expr.calc(scope).as_string(),
+ elem->max_width);
+ }
+ catch (const calc_error&) {
+ out << (string("%") + elem->chars);
+ }
break;
#if 0
@@ -622,10 +640,6 @@ void format_t::format(std::ostream& out_str, scope_t& scope) const
}
break;
- case element_t::SPACER:
- out << " ";
- break;
-
case element_t::DEPTH_SPACER:
for (const account_t * acct = details.account;
acct;