Made the id function available in post contexts

This commit is contained in:
John Wiegley 2009-10-27 04:00:59 -04:00
parent 5ddb0e9bfb
commit ddfd00afe1
4 changed files with 65 additions and 22 deletions

View file

@ -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>);

View file

@ -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();
}

View file

@ -351,32 +351,48 @@ void xact_t::add_post(post_t * post)
xact_base_t::add_post(post);
}
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;
}
string xact_t::idstring() const
{
std::ostringstream buf;
buf << *_date;
buf << payee;
magnitude().print(buf);
return buf.str();
}
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);
}
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;
return xact.magnitude();
}
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());
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) {

View file

@ -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;