summaryrefslogtreecommitdiff
path: root/src/filters.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-11-01 21:14:05 -0500
committerJohn Wiegley <johnw@newartisans.com>2009-11-01 21:14:05 -0500
commit6b6ca6d4c70df0e4a3009ee093cf2f83ecee2675 (patch)
treed883948c90dd7d6dd096458a96c6b3e2b66b312a /src/filters.cc
parent48f024a42aa02f36220cf5e06ed005c65cc3fa76 (diff)
downloadfork-ledger-6b6ca6d4c70df0e4a3009ee093cf2f83ecee2675.tar.gz
fork-ledger-6b6ca6d4c70df0e4a3009ee093cf2f83ecee2675.tar.bz2
fork-ledger-6b6ca6d4c70df0e4a3009ee093cf2f83ecee2675.zip
Improved the way account temporaries are managed
Diffstat (limited to 'src/filters.cc')
-rw-r--r--src/filters.cc54
1 files changed, 42 insertions, 12 deletions
diff --git a/src/filters.cc b/src/filters.cc
index da9e39ae..76326ac1 100644
--- a/src/filters.cc
+++ b/src/filters.cc
@@ -133,6 +133,40 @@ void sort_posts::post_accumulated_posts()
posts.clear();
}
+namespace {
+ void split_string(const string& str, const char ch,
+ std::list<string>& strings)
+ {
+ const char * b = str.c_str();
+ for (const char * p = b; *p; p++) {
+ if (*p == ch) {
+ strings.push_back(string(b, p - b));
+ b = p + 1;
+ }
+ }
+ strings.push_back(string(b));
+ }
+
+ account_t * create_temp_account_from_path(std::list<string>& account_names,
+ temporaries_t& temps,
+ account_t * master)
+ {
+ account_t * new_account = NULL;
+ foreach (const string& name, account_names) {
+ if (new_account) {
+ new_account = new_account->find_account(name);
+ } else {
+ new_account = master->find_account(name, false);
+ if (! new_account)
+ new_account = &temps.create_account(name, master);
+ }
+ }
+
+ assert(new_account != NULL);
+ return new_account;
+ }
+}
+
void anonymize_posts::operator()(post_t& post)
{
SHA1 sha;
@@ -158,27 +192,19 @@ void anonymize_posts::operator()(post_t& post)
}
std::list<string> account_names;
- account_t * new_account = NULL;
for (account_t * acct = post.account;
acct;
acct = acct->parent) {
- if (! acct->parent) {
- new_account = acct;
- break;
- }
-
sha.Reset();
sha << acct->name.c_str();
sha.Result(message_digest);
account_names.push_front(to_hex(message_digest));
}
- assert(new_account);
-
- foreach (const string& name, account_names)
- new_account = new_account->find_account(name);
+ account_t * new_account =
+ create_temp_account_from_path(account_names, temps, xact.journal->master);
post_t& temp = temps.copy_post(post, xact, new_account);
temp.note = none;
@@ -685,11 +711,15 @@ void transfer_details::operator()(post_t& post)
case SET_PAYEE:
xact.payee = expr.calc(bound_scope).to_string();
break;
- case SET_ACCOUNT:
+ case SET_ACCOUNT: {
+ std::list<string> account_names;
temp.account->remove_post(&temp);
- temp.account = master->find_account(expr.calc(bound_scope).to_string());
+ split_string(expr.calc(bound_scope).to_string(), ':', account_names);
+ temp.account = create_temp_account_from_path(account_names, temps,
+ xact.journal->master);
temp.account->add_post(&temp);
break;
+ }
default:
assert(false);
break;