diff options
author | John Wiegley <johnw@newartisans.com> | 2009-11-01 21:14:05 -0500 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-11-01 21:14:05 -0500 |
commit | 6b6ca6d4c70df0e4a3009ee093cf2f83ecee2675 (patch) | |
tree | d883948c90dd7d6dd096458a96c6b3e2b66b312a /src | |
parent | 48f024a42aa02f36220cf5e06ed005c65cc3fa76 (diff) | |
download | fork-ledger-6b6ca6d4c70df0e4a3009ee093cf2f83ecee2675.tar.gz fork-ledger-6b6ca6d4c70df0e4a3009ee093cf2f83ecee2675.tar.bz2 fork-ledger-6b6ca6d4c70df0e4a3009ee093cf2f83ecee2675.zip |
Improved the way account temporaries are managed
Diffstat (limited to 'src')
-rw-r--r-- | src/account.cc | 7 | ||||
-rw-r--r-- | src/filters.cc | 54 | ||||
-rw-r--r-- | src/temps.cc | 8 | ||||
-rw-r--r-- | src/temps.h | 2 |
4 files changed, 54 insertions, 17 deletions
diff --git a/src/account.cc b/src/account.cc index 31d86bd2..b14520d3 100644 --- a/src/account.cc +++ b/src/account.cc @@ -42,10 +42,9 @@ account_t::~account_t() { TRACE_DTOR(account_t); - foreach (accounts_map::value_type& pair, accounts) { - assert(! pair.second->has_flags(ACCOUNT_TEMP)); - checked_delete(pair.second); - } + foreach (accounts_map::value_type& pair, accounts) + if (! pair.second->has_flags(ACCOUNT_TEMP)) + checked_delete(pair.second); } account_t * account_t::find_account(const string& name, 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; diff --git a/src/temps.cc b/src/temps.cc index f2dee159..e44aaaa7 100644 --- a/src/temps.cc +++ b/src/temps.cc @@ -48,6 +48,11 @@ temporaries_t::~temporaries_t() if (! post.account->has_flags(ACCOUNT_TEMP)) post.account->remove_post(&post); } + + foreach (account_t& acct, *acct_temps) { + if (! acct.has_flags(ACCOUNT_TEMP)) + acct.remove_account(&acct); + } } } @@ -120,6 +125,9 @@ account_t& temporaries_t::create_account(const string& name, acct_temps->push_back(account_t(parent, name)); account_t& temp(acct_temps->back()); + if (parent) + parent->add_account(&temp); + temp.add_flags(ACCOUNT_TEMP); return temp; } diff --git a/src/temps.h b/src/temps.h index 646f4dcd..34494ba8 100644 --- a/src/temps.h +++ b/src/temps.h @@ -68,7 +68,7 @@ public: post_t& last_post() { return post_temps->back(); } - account_t& create_account(const string& name, + account_t& create_account(const string& name = "", account_t * parent = NULL); account_t& last_account() { return acct_temps->back(); |