diff options
author | John Wiegley <johnw@newartisans.com> | 2009-10-27 04:00:59 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-10-27 04:00:59 -0400 |
commit | ddfd00afe17393f5b33a9288990a649e3cfcc0ab (patch) | |
tree | d9562fba018f0a80809894e939b4026b235f4e37 /src | |
parent | 5ddb0e9bfbf3432a1078adc550484e5b8a151f34 (diff) | |
download | fork-ledger-ddfd00afe17393f5b33a9288990a649e3cfcc0ab.tar.gz fork-ledger-ddfd00afe17393f5b33a9288990a649e3cfcc0ab.tar.bz2 fork-ledger-ddfd00afe17393f5b33a9288990a649e3cfcc0ab.zip |
Made the id function available in post contexts
Diffstat (limited to 'src')
-rw-r--r-- | src/post.cc | 22 | ||||
-rw-r--r-- | src/utils.h | 5 | ||||
-rw-r--r-- | src/xact.cc | 54 | ||||
-rw-r--r-- | src/xact.h | 4 |
4 files changed, 64 insertions, 21 deletions
diff --git a/src/post.cc b/src/post.cc index 90c10a3b..89d69ec1 100644 --- a/src/post.cc +++ b/src/post.cc @@ -141,6 +141,16 @@ namespace { return string_value(post.xact->payee); } + value_t get_magnitude(post_t& post) { + return post.xact->magnitude(); + } + value_t get_idstring(post_t& post) { + return string_value(post.xact->idstring()); + } + value_t get_id(post_t& post) { + return string_value(post.xact->id()); + } + value_t get_amount(post_t& post) { if (post.has_xdata() && post.xdata().has_flags(POST_EXT_COMPOUND)) return post.xdata().compound_value; @@ -296,6 +306,18 @@ expr_t::ptr_op_t post_t::lookup(const string& name) return WRAP_FUNCTOR(get_wrapper<&get_has_cost>); 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 == "post") return WRAP_FUNCTOR(get_wrapper<&get_this>); diff --git a/src/utils.h b/src/utils.h index 98bdf9af..e3ae5dda 100644 --- a/src/utils.h +++ b/src/utils.h @@ -596,7 +596,7 @@ inline char peek_next_nonws(std::istream& in) { *_p = '\0'; \ } -inline string to_hex(uint_least32_t * message_digest) +inline string to_hex(uint_least32_t * message_digest, const int len = 1) { std::ostringstream buf; @@ -604,7 +604,8 @@ inline string to_hex(uint_least32_t * message_digest) buf.width(8); buf.fill('0'); buf << std::hex << message_digest[i]; - break; // only output the first dword + if (i + 1 >= len) + break; // only output the first LEN dwords } return buf.str(); } diff --git a/src/xact.cc b/src/xact.cc index 175a1467..13faedba 100644 --- a/src/xact.cc +++ b/src/xact.cc @@ -351,32 +351,48 @@ void xact_t::add_post(post_t * post) xact_base_t::add_post(post); } -namespace { - value_t get_magnitude(xact_t& xact) { - balance_t halfbal; - foreach (post_t * post, xact.posts) - if (post->amount.sign() > 0) +value_t xact_t::magnitude() const +{ + value_t halfbal = 0L; + foreach (const post_t * post, posts) { + if (post->amount.sign() > 0) { + if (post->cost) + halfbal += post->cost->number(); + else halfbal += post->amount.number(); - return halfbal; + } } + return halfbal; +} - value_t get_idstring(xact_t& xact) { - std::ostringstream buf; - buf << *xact._date; - buf << xact.payee; +string xact_t::idstring() const +{ + std::ostringstream buf; + buf << *_date; + buf << payee; + magnitude().print(buf); + return buf.str(); +} - get_magnitude(xact).print(buf); +string xact_t::id() const +{ + SHA1 sha; + sha.Reset(); + sha << idstring().c_str(); + uint_least32_t message_digest[5]; + sha.Result(message_digest); + return to_hex(message_digest, 5); +} - return string_value(buf.str()); +namespace { + value_t get_magnitude(xact_t& xact) { + return xact.magnitude(); + } + value_t get_idstring(xact_t& xact) { + return string_value(xact.idstring()); } 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)); + return string_value(xact.id()); } value_t get_code(xact_t& xact) { @@ -104,6 +104,10 @@ public: virtual void add_post(post_t * post); + value_t magnitude() const; + string idstring() const; + string id() const; + virtual expr_t::ptr_op_t lookup(const string& name); virtual bool valid() const; |