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.
This commit is contained in:
parent
5a970554b8
commit
26ae1fdfad
3 changed files with 52 additions and 15 deletions
|
|
@ -133,21 +133,6 @@ void sort_posts::post_accumulated_posts()
|
||||||
posts.clear();
|
posts.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
|
||||||
string to_hex(uint_least32_t * message_digest)
|
|
||||||
{
|
|
||||||
std::ostringstream buf;
|
|
||||||
|
|
||||||
for(int i = 0; i < 5 ; i++) {
|
|
||||||
buf.width(8);
|
|
||||||
buf.fill('0');
|
|
||||||
buf << std::hex << message_digest[i];
|
|
||||||
break; // only output the first dword
|
|
||||||
}
|
|
||||||
return buf.str();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void anonymize_posts::operator()(post_t& post)
|
void anonymize_posts::operator()(post_t& post)
|
||||||
{
|
{
|
||||||
SHA1 sha;
|
SHA1 sha;
|
||||||
|
|
|
||||||
13
src/utils.h
13
src/utils.h
|
|
@ -594,6 +594,19 @@ inline char peek_next_nonws(std::istream& in) {
|
||||||
*_p = '\0'; \
|
*_p = '\0'; \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline string to_hex(uint_least32_t * message_digest)
|
||||||
|
{
|
||||||
|
std::ostringstream buf;
|
||||||
|
|
||||||
|
for(int i = 0; i < 5 ; i++) {
|
||||||
|
buf.width(8);
|
||||||
|
buf.fill('0');
|
||||||
|
buf << std::hex << message_digest[i];
|
||||||
|
break; // only output the first dword
|
||||||
|
}
|
||||||
|
return buf.str();
|
||||||
|
}
|
||||||
|
|
||||||
extern const string version;
|
extern const string version;
|
||||||
|
|
||||||
} // namespace ledger
|
} // namespace ledger
|
||||||
|
|
|
||||||
39
src/xact.cc
39
src/xact.cc
|
|
@ -352,6 +352,33 @@ void xact_t::add_post(post_t * post)
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
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) {
|
value_t get_code(xact_t& xact) {
|
||||||
if (xact.code)
|
if (xact.code)
|
||||||
return string_value(*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>);
|
return WRAP_FUNCTOR(get_wrapper<&get_code>);
|
||||||
break;
|
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':
|
case 'p':
|
||||||
if (name[1] == '\0' || name == "payee")
|
if (name[1] == '\0' || name == "payee")
|
||||||
return WRAP_FUNCTOR(get_wrapper<&get_payee>);
|
return WRAP_FUNCTOR(get_wrapper<&get_payee>);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue