summaryrefslogtreecommitdiff
path: root/src/post.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/post.cc')
-rw-r--r--src/post.cc163
1 files changed, 85 insertions, 78 deletions
diff --git a/src/post.cc b/src/post.cc
index e182a731..fa8bab4f 100644
--- a/src/post.cc
+++ b/src/post.cc
@@ -50,7 +50,7 @@ bool post_t::has_tag(const string& tag) const
}
bool post_t::has_tag(const mask_t& tag_mask,
- const optional<mask_t>& value_mask) const
+ const optional<mask_t>& value_mask) const
{
if (item_t::has_tag(tag_mask, value_mask))
return true;
@@ -59,19 +59,19 @@ bool post_t::has_tag(const mask_t& tag_mask,
return false;
}
-optional<string> post_t::get_tag(const string& tag) const
+optional<value_t> post_t::get_tag(const string& tag) const
{
- if (optional<string> value = item_t::get_tag(tag))
+ if (optional<value_t> value = item_t::get_tag(tag))
return value;
if (xact)
return xact->get_tag(tag);
return none;
}
-optional<string> post_t::get_tag(const mask_t& tag_mask,
- const optional<mask_t>& value_mask) const
+optional<value_t> post_t::get_tag(const mask_t& tag_mask,
+ const optional<mask_t>& value_mask) const
{
- if (optional<string> value = item_t::get_tag(tag_mask, value_mask))
+ if (optional<value_t> value = item_t::get_tag(tag_mask, value_mask))
return value;
if (xact)
return xact->get_tag(tag_mask, value_mask);
@@ -197,20 +197,27 @@ namespace {
return post.has_xdata() && post.xdata().has_flags(POST_EXT_DIRECT_AMT);
}
- value_t get_commodity(post_t& post) {
- if (post.has_xdata() &&
- post.xdata().has_flags(POST_EXT_COMPOUND))
- return string_value(post.xdata().compound_value.to_amount()
- .commodity().symbol());
- else
- return string_value(post.amount.commodity().symbol());
+ value_t get_commodity(call_scope_t& scope)
+ {
+ in_context_t<post_t> env(scope, "&v");
+ if (env.has(0)) {
+ return string_value(env.value_at(0).to_amount().commodity().symbol());
+ } else {
+ post_t& post(find_scope<post_t>(scope));
+ if (post.has_xdata() &&
+ post.xdata().has_flags(POST_EXT_COMPOUND))
+ return string_value(post.xdata().compound_value.to_amount()
+ .commodity().symbol());
+ else
+ return string_value(post.amount.commodity().symbol());
+ }
}
value_t get_commodity_is_primary(post_t& post) {
if (post.has_xdata() &&
- post.xdata().has_flags(POST_EXT_COMPOUND))
+ post.xdata().has_flags(POST_EXT_COMPOUND))
return post.xdata().compound_value.to_amount()
- .commodity().has_flags(COMMODITY_PRIMARY);
+ .commodity().has_flags(COMMODITY_PRIMARY);
else
return post.amount.commodity().has_flags(COMMODITY_PRIMARY);
}
@@ -223,7 +230,7 @@ namespace {
if (post.cost)
return *post.cost;
else if (post.has_xdata() &&
- post.xdata().has_flags(POST_EXT_COMPOUND))
+ post.xdata().has_flags(POST_EXT_COMPOUND))
return post.xdata().compound_value;
else if (post.amount.is_null())
return 0L;
@@ -255,37 +262,37 @@ namespace {
if (env.has(0)) {
if (env.value_at(0).is_long()) {
- if (env.get<long>(0) > 2)
- name = format_t::truncate(env->reported_account()->fullname(),
- env.get<long>(0) - 2,
- 2 /* account_abbrev_length */);
- else
- name = env->reported_account()->fullname();
+ if (env.get<long>(0) > 2)
+ name = format_t::truncate(env->reported_account()->fullname(),
+ env.get<long>(0) - 2,
+ 2 /* account_abbrev_length */);
+ else
+ name = env->reported_account()->fullname();
} else {
- account_t * account = NULL;
- account_t * master = env->account;
- while (master->parent)
- master = master->parent;
-
- if (env.value_at(0).is_string()) {
- name = env.get<string>(0);
- account = master->find_account(name, false);
- }
- else if (env.value_at(0).is_mask()) {
- name = env.get<mask_t>(0).str();
- account = master->find_account_re(name);
- }
- else {
- throw_(std::runtime_error,
- _("Expected string or mask for argument 1, but received %1")
- << env.value_at(0).label());
- }
-
- if (! account)
- throw_(std::runtime_error,
- _("Could not find an account matching ") << env.value_at(0));
- else
- return value_t(static_cast<scope_t *>(account));
+ account_t * account = NULL;
+ account_t * master = env->account;
+ while (master->parent)
+ master = master->parent;
+
+ if (env.value_at(0).is_string()) {
+ name = env.get<string>(0);
+ account = master->find_account(name, false);
+ }
+ else if (env.value_at(0).is_mask()) {
+ name = env.get<mask_t>(0).str();
+ account = master->find_account_re(name);
+ }
+ else {
+ throw_(std::runtime_error,
+ _("Expected string or mask for argument 1, but received %1")
+ << env.value_at(0).label());
+ }
+
+ if (! account)
+ throw_(std::runtime_error,
+ _("Could not find an account matching ") << env.value_at(0));
+ else
+ return value_t(static_cast<scope_t *>(account));
}
} else {
name = env->reported_account()->fullname();
@@ -300,10 +307,10 @@ namespace {
value_t acct = account_name(scope);
if (acct.is_string()) {
if (env->has_flags(POST_VIRTUAL)) {
- if (env->must_balance())
- acct = string_value(string("[") + acct.as_string() + "]");
- else
- acct = string_value(string("(") + acct.as_string() + ")");
+ if (env->must_balance())
+ acct = string_value(string("[") + acct.as_string() + "]");
+ else
+ acct = string_value(string("(") + acct.as_string() + ")");
}
}
return acct;
@@ -330,7 +337,7 @@ namespace {
if (post.has_xdata()) {
post_t::xdata_t& xdata(post.xdata());
if (! xdata.value_date.is_not_a_date())
- return xdata.value_date;
+ return xdata.value_date;
}
return post.date();
}
@@ -353,13 +360,13 @@ namespace {
foreach (post_t * p, post.xact->posts) {
bind_scope_t bound_scope(scope, *p);
if (p == &post && args.has(1) &&
- ! args.get<expr_t&>(1).calc(bound_scope).to_boolean()) {
- // If the user specifies any(EXPR, false), and the context is a
- // posting, then that posting isn't considered by the test.
- ; // skip it
+ ! args.get<expr_t&>(1).calc(bound_scope).to_boolean()) {
+ // If the user specifies any(EXPR, false), and the context is a
+ // posting, then that posting isn't considered by the test.
+ ; // skip it
}
else if (expr.calc(bound_scope).to_boolean()) {
- return true;
+ return true;
}
}
return false;
@@ -375,13 +382,13 @@ namespace {
foreach (post_t * p, post.xact->posts) {
bind_scope_t bound_scope(scope, *p);
if (p == &post && args.has(1) &&
- ! args.get<expr_t&>(1).calc(bound_scope).to_boolean()) {
- // If the user specifies any(EXPR, false), and the context is a
- // posting, then that posting isn't considered by the test.
- ; // skip it
+ ! args.get<expr_t&>(1).calc(bound_scope).to_boolean()) {
+ // If the user specifies any(EXPR, false), and the context is a
+ // posting, then that posting isn't considered by the test.
+ ; // skip it
}
else if (! expr.calc(bound_scope).to_boolean()) {
- return false;
+ return false;
}
}
return true;
@@ -389,7 +396,7 @@ namespace {
}
expr_t::ptr_op_t post_t::lookup(const symbol_t::kind_t kind,
- const string& name)
+ const string& name)
{
if (kind != symbol_t::FUNCTION)
return item_t::lookup(kind, name);
@@ -427,7 +434,7 @@ expr_t::ptr_op_t post_t::lookup(const symbol_t::kind_t kind,
else if (name == "calculated")
return WRAP_FUNCTOR(get_wrapper<&get_is_calculated>);
else if (name == "commodity")
- return WRAP_FUNCTOR(get_wrapper<&get_commodity>);
+ return WRAP_FUNCTOR(&get_commodity);
break;
case 'd':
@@ -533,7 +540,7 @@ amount_t post_t::resolve_expr(scope_t& scope, expr_t& expr)
} else {
if (! result.is_amount())
throw_(amount_error,
- _("Amount expressions must result in a simple amount"));
+ _("Amount expressions must result in a simple amount"));
return result.as_amount();
}
}
@@ -571,7 +578,7 @@ bool post_t::valid() const
posts_list::const_iterator i =
std::find(xact->posts.begin(),
- xact->posts.end(), this);
+ xact->posts.end(), this);
if (i == xact->posts.end()) {
DEBUG("ledger.validate", "post_t: ! found");
return false;
@@ -608,7 +615,7 @@ void post_t::add_to_value(value_t& value, const optional<expr_t&>& expr) const
}
else if (expr) {
bind_scope_t bound_scope(*expr->get_context(),
- const_cast<post_t&>(*this));
+ const_cast<post_t&>(*this));
#if 1
value_t temp(expr->calc(bound_scope));
add_or_set_value(value, temp);
@@ -621,7 +628,7 @@ void post_t::add_to_value(value_t& value, const optional<expr_t&>& expr) const
#endif
}
else if (xdata_ && xdata_->has_flags(POST_EXT_VISITED) &&
- ! xdata_->visited_value.is_null()) {
+ ! xdata_->visited_value.is_null()) {
add_or_set_value(value, xdata_->visited_value);
}
else {
@@ -708,18 +715,18 @@ void to_xml(std::ostream& out, const post_t& post)
push_xml y(out, "metadata");
foreach (const item_t::string_map::value_type& pair, *post.metadata) {
if (pair.second.first) {
- push_xml z(out, "variable");
- {
- push_xml z(out, "key");
- out << y.guard(pair.first);
- }
- {
- push_xml z(out, "value");
- out << y.guard(*pair.second.first);
- }
+ push_xml z(out, "variable");
+ {
+ push_xml z(out, "key");
+ out << y.guard(pair.first);
+ }
+ {
+ push_xml z(out, "value");
+ to_xml(out, *pair.second.first);
+ }
} else {
- push_xml z(out, "tag");
- out << y.guard(pair.first);
+ push_xml z(out, "tag");
+ out << y.guard(pair.first);
}
}
}