diff options
author | John Wiegley <johnw@newartisans.com> | 2009-10-25 04:38:43 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-10-25 05:01:47 -0400 |
commit | 26ae1fdfad62a15a0762c052ed9785f3c0d5be29 (patch) | |
tree | 291b803c7795da513661d06273b20f1dac2b0334 /src/xact.cc | |
parent | 5a970554b822d50a6454b8a2e1434e248773adfd (diff) | |
download | fork-ledger-26ae1fdfad62a15a0762c052ed9785f3c0d5be29.tar.gz fork-ledger-26ae1fdfad62a15a0762c052ed9785f3c0d5be29.tar.bz2 fork-ledger-26ae1fdfad62a15a0762c052ed9785f3c0d5be29.zip |
New valexpr functions: id, idstring, magnitude
id returns a unique SHA1 id of a transaction.
idstring is the string that the SHA1 is based on.
magnitude is the sum of the positive side of a transaction.
Diffstat (limited to 'src/xact.cc')
-rw-r--r-- | src/xact.cc | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/xact.cc b/src/xact.cc index bd8a5955..175a1467 100644 --- a/src/xact.cc +++ b/src/xact.cc @@ -352,6 +352,33 @@ void xact_t::add_post(post_t * post) } namespace { + value_t get_magnitude(xact_t& xact) { + balance_t halfbal; + foreach (post_t * post, xact.posts) + if (post->amount.sign() > 0) + halfbal += post->amount.number(); + return halfbal; + } + + value_t get_idstring(xact_t& xact) { + std::ostringstream buf; + buf << *xact._date; + buf << xact.payee; + + get_magnitude(xact).print(buf); + + return string_value(buf.str()); + } + value_t get_id(xact_t& xact) { + SHA1 sha; + sha.Reset(); + sha << get_idstring(xact).as_string().c_str(); + + uint_least32_t message_digest[5]; + sha.Result(message_digest); + return string_value(to_hex(message_digest)); + } + value_t get_code(xact_t& xact) { if (xact.code) return string_value(*xact.code); @@ -377,6 +404,18 @@ expr_t::ptr_op_t xact_t::lookup(const string& name) return WRAP_FUNCTOR(get_wrapper<&get_code>); break; + case 'i': + if (name == "id") + return WRAP_FUNCTOR(get_wrapper<&get_id>); + else if (name == "idstring") + return WRAP_FUNCTOR(get_wrapper<&get_idstring>); + break; + + case 'm': + if (name == "magnitude") + return WRAP_FUNCTOR(get_wrapper<&get_magnitude>); + break; + case 'p': if (name[1] == '\0' || name == "payee") return WRAP_FUNCTOR(get_wrapper<&get_payee>); |