diff options
author | John Wiegley <johnw@newartisans.com> | 2010-06-11 16:03:50 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2010-06-11 17:02:25 -0400 |
commit | dea2aed0b509734ec4e1cd163ac2a4f013000da2 (patch) | |
tree | 7908da76c67ae5172882306a319bf26df81b73b4 /src/format.cc | |
parent | d580079df892c30d023b3211d6c4611c17b11f8f (diff) | |
download | fork-ledger-dea2aed0b509734ec4e1cd163ac2a4f013000da2.tar.gz fork-ledger-dea2aed0b509734ec4e1cd163ac2a4f013000da2.tar.bz2 fork-ledger-dea2aed0b509734ec4e1cd163ac2a4f013000da2.zip |
Untabified all source files
Diffstat (limited to 'src/format.cc')
-rw-r--r-- | src/format.cc | 280 |
1 files changed, 140 insertions, 140 deletions
diff --git a/src/format.cc b/src/format.cc index c546926e..bde39882 100644 --- a/src/format.cc +++ b/src/format.cc @@ -37,8 +37,8 @@ namespace ledger { -format_t::elision_style_t format_t::default_style = TRUNCATE_TRAILING; -bool format_t::default_style_changed = false; +format_t::elision_style_t format_t::default_style = TRUNCATE_TRAILING; +bool format_t::default_style_changed = false; void format_t::element_t::dump(std::ostream& out) const { @@ -88,14 +88,14 @@ namespace { // Don't gobble up any whitespace const char * base = p; while (p >= base && std::isspace(*p)) - p--; + p--; } return expr; } } format_t::element_t * format_t::parse_elements(const string& fmt, - const optional<format_t&>& tmpl) + const optional<format_t&>& tmpl) { std::auto_ptr<element_t> result; @@ -147,8 +147,8 @@ format_t::element_t * format_t::parse_elements(const string& fmt, while (*p == '-') { switch (*p) { case '-': - current->add_flags(ELEMENT_ALIGN_LEFT); - break; + current->add_flags(ELEMENT_ALIGN_LEFT); + break; } ++p; } @@ -164,12 +164,12 @@ format_t::element_t * format_t::parse_elements(const string& fmt, ++p; num = 0; while (*p && std::isdigit(*p)) { - num *= 10; - num += *p++ - '0'; + num *= 10; + num += *p++ - '0'; } current->max_width = num; if (current->min_width == 0) - current->min_width = current->max_width; + current->min_width = current->max_width; } switch (*p) { @@ -180,25 +180,25 @@ format_t::element_t * format_t::parse_elements(const string& fmt, case '$': { if (! tmpl) - throw_(format_error, _("Prior field reference, but no template")); + throw_(format_error, _("Prior field reference, but no template")); p++; if (*p == '0' || (! std::isdigit(*p) && - *p != 'A' && *p != 'B' && *p != 'C' && - *p != 'D' && *p != 'E' && *p != 'F')) - throw_(format_error, _("%$ field reference must be a digit from 1-9")); + *p != 'A' && *p != 'B' && *p != 'C' && + *p != 'D' && *p != 'E' && *p != 'F')) + throw_(format_error, _("%$ field reference must be a digit from 1-9")); unsigned int index = std::isdigit(*p) ? *p - '0' : (*p - 'A' + 10); element_t * tmpl_elem = tmpl->elements.get(); for (unsigned int i = 1; i < index && tmpl_elem; i++) { - tmpl_elem = tmpl_elem->next.get(); - while (tmpl_elem && tmpl_elem->type != element_t::EXPR) - tmpl_elem = tmpl_elem->next.get(); + tmpl_elem = tmpl_elem->next.get(); + while (tmpl_elem && tmpl_elem->type != element_t::EXPR) + tmpl_elem = tmpl_elem->next.get(); } if (! tmpl_elem) - throw_(format_error, _("%$ reference to a non-existent prior field")); + throw_(format_error, _("%$ reference to a non-existent prior field")); *current = *tmpl_elem; break; @@ -214,87 +214,87 @@ format_t::element_t * format_t::parse_elements(const string& fmt, // Wrap the subexpression in calls to justify and scrub if (format_amount) { - if (! *p || *(p + 1) != '}') - throw_(format_error, _("Expected closing brace")); - else - p++; + if (! *p || *(p + 1) != '}') + throw_(format_error, _("Expected closing brace")); + else + p++; - expr_t::ptr_op_t op = boost::get<expr_t>(current->data).get_op(); + expr_t::ptr_op_t op = boost::get<expr_t>(current->data).get_op(); - expr_t::ptr_op_t amount_op; - expr_t::ptr_op_t colorize_op; - if (op->kind == expr_t::op_t::O_CONS) { - amount_op = op->left(); - colorize_op = op->right(); - } else { - amount_op = op; - } + expr_t::ptr_op_t amount_op; + expr_t::ptr_op_t colorize_op; + if (op->kind == expr_t::op_t::O_CONS) { + amount_op = op->left(); + colorize_op = op->right(); + } else { + amount_op = op; + } - expr_t::ptr_op_t scrub_node(new expr_t::op_t(expr_t::op_t::IDENT)); - scrub_node->set_ident("scrub"); + expr_t::ptr_op_t scrub_node(new expr_t::op_t(expr_t::op_t::IDENT)); + scrub_node->set_ident("scrub"); - expr_t::ptr_op_t call1_node(new expr_t::op_t(expr_t::op_t::O_CALL)); - call1_node->set_left(scrub_node); - call1_node->set_right(amount_op); + expr_t::ptr_op_t call1_node(new expr_t::op_t(expr_t::op_t::O_CALL)); + call1_node->set_left(scrub_node); + call1_node->set_right(amount_op); - expr_t::ptr_op_t arg1_node(new expr_t::op_t(expr_t::op_t::VALUE)); - expr_t::ptr_op_t arg2_node(new expr_t::op_t(expr_t::op_t::VALUE)); - expr_t::ptr_op_t arg3_node(new expr_t::op_t(expr_t::op_t::VALUE)); + expr_t::ptr_op_t arg1_node(new expr_t::op_t(expr_t::op_t::VALUE)); + expr_t::ptr_op_t arg2_node(new expr_t::op_t(expr_t::op_t::VALUE)); + expr_t::ptr_op_t arg3_node(new expr_t::op_t(expr_t::op_t::VALUE)); - arg1_node->set_value(current->min_width > 0 ? - long(current->min_width) : -1); - arg2_node->set_value(current->max_width > 0 ? - long(current->max_width) : -1); - arg3_node->set_value(! current->has_flags(ELEMENT_ALIGN_LEFT)); + arg1_node->set_value(current->min_width > 0 ? + long(current->min_width) : -1); + arg2_node->set_value(current->max_width > 0 ? + long(current->max_width) : -1); + arg3_node->set_value(! current->has_flags(ELEMENT_ALIGN_LEFT)); - current->min_width = 0; - current->max_width = 0; + current->min_width = 0; + current->max_width = 0; - expr_t::ptr_op_t args1_node(new expr_t::op_t(expr_t::op_t::O_CONS)); - args1_node->set_left(arg2_node); - args1_node->set_right(arg3_node); + expr_t::ptr_op_t args1_node(new expr_t::op_t(expr_t::op_t::O_CONS)); + args1_node->set_left(arg2_node); + args1_node->set_right(arg3_node); - expr_t::ptr_op_t args2_node(new expr_t::op_t(expr_t::op_t::O_CONS)); - args2_node->set_left(arg1_node); - args2_node->set_right(args1_node); + expr_t::ptr_op_t args2_node(new expr_t::op_t(expr_t::op_t::O_CONS)); + args2_node->set_left(arg1_node); + args2_node->set_right(args1_node); - expr_t::ptr_op_t args3_node(new expr_t::op_t(expr_t::op_t::O_CONS)); - args3_node->set_left(call1_node); - args3_node->set_right(args2_node); + expr_t::ptr_op_t args3_node(new expr_t::op_t(expr_t::op_t::O_CONS)); + args3_node->set_left(call1_node); + args3_node->set_right(args2_node); - expr_t::ptr_op_t seq1_node(new expr_t::op_t(expr_t::op_t::O_SEQ)); - seq1_node->set_left(args3_node); + expr_t::ptr_op_t seq1_node(new expr_t::op_t(expr_t::op_t::O_SEQ)); + seq1_node->set_left(args3_node); - expr_t::ptr_op_t justify_node(new expr_t::op_t(expr_t::op_t::IDENT)); - justify_node->set_ident("justify"); + expr_t::ptr_op_t justify_node(new expr_t::op_t(expr_t::op_t::IDENT)); + justify_node->set_ident("justify"); - expr_t::ptr_op_t call2_node(new expr_t::op_t(expr_t::op_t::O_CALL)); - call2_node->set_left(justify_node); - call2_node->set_right(seq1_node); + expr_t::ptr_op_t call2_node(new expr_t::op_t(expr_t::op_t::O_CALL)); + call2_node->set_left(justify_node); + call2_node->set_right(seq1_node); - string prev_expr = boost::get<expr_t>(current->data).text(); + string prev_expr = boost::get<expr_t>(current->data).text(); - if (colorize_op) { - expr_t::ptr_op_t ansify_if_node(new expr_t::op_t(expr_t::op_t::IDENT)); - ansify_if_node->set_ident("ansify_if"); + if (colorize_op) { + expr_t::ptr_op_t ansify_if_node(new expr_t::op_t(expr_t::op_t::IDENT)); + ansify_if_node->set_ident("ansify_if"); - expr_t::ptr_op_t args4_node(new expr_t::op_t(expr_t::op_t::O_CONS)); - args4_node->set_left(call2_node); - args4_node->set_right(colorize_op); + expr_t::ptr_op_t args4_node(new expr_t::op_t(expr_t::op_t::O_CONS)); + args4_node->set_left(call2_node); + args4_node->set_right(colorize_op); - expr_t::ptr_op_t seq2_node(new expr_t::op_t(expr_t::op_t::O_SEQ)); - seq2_node->set_left(args4_node); + expr_t::ptr_op_t seq2_node(new expr_t::op_t(expr_t::op_t::O_SEQ)); + seq2_node->set_left(args4_node); - expr_t::ptr_op_t call3_node(new expr_t::op_t(expr_t::op_t::O_CALL)); - call3_node->set_left(ansify_if_node); - call3_node->set_right(seq2_node); + expr_t::ptr_op_t call3_node(new expr_t::op_t(expr_t::op_t::O_CALL)); + call3_node->set_left(ansify_if_node); + call3_node->set_right(seq2_node); - current->data = expr_t(call3_node); - } else { - current->data = expr_t(call2_node); - } + current->data = expr_t(call3_node); + } else { + current->data = expr_t(call2_node); + } - boost::get<expr_t>(current->data).set_text(prev_expr); + boost::get<expr_t>(current->data).set_text(prev_expr); } break; } @@ -335,7 +335,7 @@ string format_t::real_calc(scope_t& scope) switch (elem->type) { case element_t::STRING: if (elem->min_width > 0) - out.width(elem->min_width); + out.width(elem->min_width); out << boost::get<string>(elem->data); break; @@ -343,28 +343,28 @@ string format_t::real_calc(scope_t& scope) expr_t& expr(boost::get<expr_t>(elem->data)); try { - expr.compile(scope); - - value_t value; - if (expr.is_function()) { - call_scope_t args(scope); - args.push_back(long(elem->max_width)); - value = expr.get_function()(args); - } else { - value = expr.calc(scope); - } - DEBUG("format.expr", "value = (" << value << ")"); - - if (elem->min_width > 0) - value.print(out, static_cast<int>(elem->min_width), -1, - ! elem->has_flags(ELEMENT_ALIGN_LEFT)); - else - out << value.to_string(); + expr.compile(scope); + + value_t value; + if (expr.is_function()) { + call_scope_t args(scope); + args.push_back(long(elem->max_width)); + value = expr.get_function()(args); + } else { + value = expr.calc(scope); + } + DEBUG("format.expr", "value = (" << value << ")"); + + if (elem->min_width > 0) + value.print(out, static_cast<int>(elem->min_width), -1, + ! elem->has_flags(ELEMENT_ALIGN_LEFT)); + else + out << value.to_string(); } catch (const calc_error&) { - add_error_context(_("While calculating format expression:")); - add_error_context(expr.context_to_str()); - throw; + add_error_context(_("While calculating format expression:")); + add_error_context(expr.context_to_str()); + throw; } break; } @@ -376,15 +376,15 @@ string format_t::real_calc(scope_t& scope) if (elem->max_width > 0 || elem->min_width > 0) { unistring temp(out.str()); - string result; + string result; if (elem->max_width > 0 && elem->max_width < temp.length()) { - result = truncate(temp, elem->max_width); + result = truncate(temp, elem->max_width); } else { - result = temp.extract(); - if (elem->min_width > temp.length()) - for (std::size_t i = 0; i < elem->min_width - temp.length(); i++) - result += " "; + result = temp.extract(); + if (elem->min_width > temp.length()) + for (std::size_t i = 0; i < elem->min_width - temp.length(); i++) + result += " "; } out_str << result; } else { @@ -396,8 +396,8 @@ string format_t::real_calc(scope_t& scope) } string format_t::truncate(const unistring& ustr, - const std::size_t width, - const std::size_t account_abbrev_length) + const std::size_t width, + const std::size_t account_abbrev_length) { assert(width < 4095); @@ -420,9 +420,9 @@ string format_t::truncate(const unistring& ustr, case TRUNCATE_MIDDLE: // This method truncates in the middle. buf << ustr.extract(0, (width - 2) / 2) - << ".." - << ustr.extract(len - ((width - 2) / 2 + (width - 2) % 2), - (width - 2) / 2 + (width - 2) % 2); + << ".." + << ustr.extract(len - ((width - 2) / 2 + (width - 2) % 2), + (width - 2) / 2 + (width - 2) % 2); break; case ABBREVIATE: @@ -431,46 +431,46 @@ string format_t::truncate(const unistring& ustr, string::size_type beg = 0; string strcopy(ustr.extract()); for (string::size_type pos = strcopy.find(':'); - pos != string::npos; - beg = pos + 1, pos = strcopy.find(':', beg)) - parts.push_back(string(strcopy, beg, pos - beg)); + pos != string::npos; + beg = pos + 1, pos = strcopy.find(':', beg)) + parts.push_back(string(strcopy, beg, pos - beg)); parts.push_back(string(strcopy, beg)); std::ostringstream result; std::size_t newlen = len; for (std::list<string>::iterator i = parts.begin(); - i != parts.end(); - i++) { - // Don't contract the last element - std::list<string>::iterator x = i; - if (++x == parts.end()) { - result << *i; - break; - } - - if (newlen > width) { - unistring temp(*i); - if (temp.length() > account_abbrev_length) { - result << temp.extract(0, account_abbrev_length) << ":"; - newlen -= temp.length() - account_abbrev_length; - } else { - result << temp.extract() << ":"; - newlen -= temp.length(); - } - } else { - result << *i << ":"; - } + i != parts.end(); + i++) { + // Don't contract the last element + std::list<string>::iterator x = i; + if (++x == parts.end()) { + result << *i; + break; + } + + if (newlen > width) { + unistring temp(*i); + if (temp.length() > account_abbrev_length) { + result << temp.extract(0, account_abbrev_length) << ":"; + newlen -= temp.length() - account_abbrev_length; + } else { + result << temp.extract() << ":"; + newlen -= temp.length(); + } + } else { + result << *i << ":"; + } } if (newlen > width) { - // Even abbreviated its too big to show the last account, so - // abbreviate all but the last and truncate at the beginning. - unistring temp(result.str()); - assert(temp.length() > width - 2); - buf << ".." << temp.extract(temp.length() - (width - 2), width - 2); + // Even abbreviated its too big to show the last account, so + // abbreviate all but the last and truncate at the beginning. + unistring temp(result.str()); + assert(temp.length() > width - 2); + buf << ".." << temp.extract(temp.length() - (width - 2), width - 2); } else { - buf << result.str(); + buf << result.str(); } break; } |