From 4ec54b86f856c6e85446d065d2940b4244d71b8f Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sat, 8 May 2010 02:06:06 -0400 Subject: Added any() and all() value expression macros any() matches an expression against every post in a transaction or account, and returns true if any of them are true. all() tests if all are true. For example: ledger -l 'account =~ /Expense/ & any(account =~ /MasterCard/)' reg This reports every posting affecting an Expense account (regex match), but only if some other posting in the same transaction affects the MasterCard account. Both functions also take a second boolean argument. If it is false, the "source" posting is not considered. For example: ledger -l 'any(/x/, false)' This matches any posting where a *different* posting in the same transaction contains the letter 'x'. --- src/xact.cc | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/xact.cc') diff --git a/src/xact.cc b/src/xact.cc index 1a022387..344f66ea 100644 --- a/src/xact.cc +++ b/src/xact.cc @@ -36,6 +36,7 @@ #include "account.h" #include "journal.h" #include "pool.h" +#include "interactive.h" namespace ledger { @@ -483,6 +484,36 @@ namespace { value_t get_wrapper(call_scope_t& scope) { return (*Func)(find_scope(scope)); } + + value_t fn_any(call_scope_t& scope) + { + interactive_t args(scope, "X&X"); + + post_t& post(find_scope(scope)); + expr_t& expr(args.get(0)); + + foreach (post_t * p, post.xact->posts) { + bind_scope_t bound_scope(scope, *p); + if (expr.calc(bound_scope).to_boolean()) + return true; + } + return false; + } + + value_t fn_all(call_scope_t& scope) + { + interactive_t args(scope, "X&X"); + + post_t& post(find_scope(scope)); + expr_t& expr(args.get(0)); + + foreach (post_t * p, post.xact->posts) { + bind_scope_t bound_scope(scope, *p); + if (! expr.calc(bound_scope).to_boolean()) + return false; + } + return true; + } } expr_t::ptr_op_t xact_t::lookup(const symbol_t::kind_t kind, @@ -492,6 +523,13 @@ expr_t::ptr_op_t xact_t::lookup(const symbol_t::kind_t kind, return item_t::lookup(kind, name); switch (name[0]) { + case 'a': + if (name == "any") + return WRAP_FUNCTOR(&fn_any); + else if (name == "all") + return WRAP_FUNCTOR(&fn_all); + break; + case 'c': if (name == "code") return WRAP_FUNCTOR(get_wrapper<&get_code>); -- cgit v1.2.3