diff options
author | John Wiegley <johnw@newartisans.com> | 2012-03-07 05:20:42 -0600 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2012-03-07 05:20:42 -0600 |
commit | 76f97a63da2deb8b6f9f8129bb11f5608d4a5518 (patch) | |
tree | ae46855b1573cdc4d0441def3b126aae702d8064 /src | |
parent | 69b25d367ed2317a537aefa9744ca315b5422fa5 (diff) | |
download | fork-ledger-76f97a63da2deb8b6f9f8129bb11f5608d4a5518.tar.gz fork-ledger-76f97a63da2deb8b6f9f8129bb11f5608d4a5518.tar.bz2 fork-ledger-76f97a63da2deb8b6f9f8129bb11f5608d4a5518.zip |
Added parsing support for the many value directives
Diffstat (limited to 'src')
-rw-r--r-- | src/account.h | 1 | ||||
-rw-r--r-- | src/journal.cc | 6 | ||||
-rw-r--r-- | src/journal.h | 7 | ||||
-rw-r--r-- | src/post.cc | 38 | ||||
-rw-r--r-- | src/post.h | 3 | ||||
-rw-r--r-- | src/textual.cc | 22 | ||||
-rw-r--r-- | src/xact.cc | 2 |
7 files changed, 74 insertions, 5 deletions
diff --git a/src/account.h b/src/account.h index 8f0f915f..95e04079 100644 --- a/src/account.h +++ b/src/account.h @@ -67,6 +67,7 @@ public: unsigned short depth; accounts_map accounts; posts_list posts; + optional<expr_t> value_expr; mutable string _fullname; diff --git a/src/journal.cc b/src/journal.cc index 71492ed2..37eacdaf 100644 --- a/src/journal.cc +++ b/src/journal.cc @@ -322,10 +322,12 @@ bool journal_t::add_xact(xact_t * xact) } extend_xact(xact); - check_all_metadata(*this, xact); - foreach (post_t * post, xact->posts) + + foreach (post_t * post, xact->posts) { + extend_post(*post, *this); check_all_metadata(*this, post); + } // If a transaction with this UUID has already been seen, simply do // not add this one to the journal. However, all automated checks diff --git a/src/journal.h b/src/journal.h index 8b750993..ca73c415 100644 --- a/src/journal.h +++ b/src/journal.h @@ -127,16 +127,17 @@ public: bool fixed_payees; bool fixed_commodities; bool fixed_metadata; + bool was_loaded; + bool force_checking; + bool check_payees; payee_mappings_t payee_mappings; account_mappings_t account_mappings; accounts_map account_aliases; account_mappings_t payees_for_unknown_accounts; checksum_map_t checksum_map; tag_check_exprs_map tag_check_exprs; + optional<expr_t> value_expr; parse_context_t * current_context; - bool was_loaded; - bool force_checking; - bool check_payees; enum checking_style_t { CHECK_PERMISSIVE, diff --git a/src/post.cc b/src/post.cc index 191a9142..7a6667f2 100644 --- a/src/post.cc +++ b/src/post.cc @@ -36,6 +36,7 @@ #include "account.h" #include "journal.h" #include "format.h" +#include "pool.h" namespace ledger { @@ -638,6 +639,43 @@ void post_t::set_reported_account(account_t * acct) acct->xdata().reported_posts.push_back(this); } +void extend_post(post_t& post, journal_t& journal) +{ + commodity_t& comm(post.amount.commodity()); + + annotation_t * details = + (comm.has_annotation() ? + &as_annotated_commodity(comm).details : NULL); + + if (! details || ! details->value_expr) { + optional<expr_t> value_expr; + + if (optional<value_t> data = post.get_tag(_("Value"))) + value_expr = expr_t(data->to_string()); + + if (! value_expr) + value_expr = post.account->value_expr; + + if (! value_expr) + value_expr = post.amount.commodity().value_expr(); + + if (! value_expr) + value_expr = journal.value_expr; + + if (value_expr) { + if (! details) { + annotation_t new_details; + new_details.value_expr = value_expr; + commodity_t * new_comm = + commodity_pool_t::current_pool->find_or_create(comm, new_details); + post.amount.set_commodity(*new_comm); + } else { + details->value_expr = value_expr; + } + } + } +} + void to_xml(std::ostream& out, const post_t& post) { push_xml x(out, "posting", true); @@ -259,6 +259,9 @@ private: #endif // HAVE_BOOST_SERIALIZATION }; +class journal_t; +void extend_post(post_t& post, journal_t& journal); + void to_xml(std::ostream& out, const post_t& post); } // namespace ledger diff --git a/src/textual.cc b/src/textual.cc index fa416d06..1d1835e3 100644 --- a/src/textual.cc +++ b/src/textual.cc @@ -126,6 +126,7 @@ namespace { void account_directive(char * line); void account_alias_directive(account_t * account, string alias); void account_payee_directive(account_t * account, string payee); + void account_value_directive(account_t * account, string expr_str); void account_default_directive(account_t * account); void default_account_directive(char * line); @@ -166,6 +167,7 @@ namespace { void eval_directive(char * line); void assert_directive(char * line); void check_directive(char * line); + void value_directive(char * line); void import_directive(char * line); void python_directive(char * line); @@ -887,6 +889,9 @@ void instance_t::account_directive(char * line) else if (keyword == "payee") { account_payee_directive(account, b); } + else if (keyword == "value") { + account_value_directive(account, b); + } else if (keyword == "default") { account_default_directive(account); } @@ -974,6 +979,11 @@ void instance_t::account_default_directive(account_t * account) context.journal->bucket = account; } +void instance_t::account_value_directive(account_t * account, string expr_str) +{ + account->value_expr = expr_t(expr_str); +} + void instance_t::payee_directive(char * line) { string payee = context.journal->register_payee(line, NULL); @@ -1108,6 +1118,11 @@ void instance_t::check_directive(char * line) context.warning(STR(_("Check failed: %1") << line)); } +void instance_t::value_directive(char * line) +{ + context.journal->value_expr = expr_t(line); +} + void instance_t::comment_directive(char * line) { while (in.good() && ! in.eof()) { @@ -1288,6 +1303,13 @@ bool instance_t::general_directive(char * line) return true; } break; + + case 'v': + if (std::strcmp(p, "value") == 0) { + value_directive(arg); + return true; + } + break; } if (expr_t::ptr_op_t op = lookup(symbol_t::DIRECTIVE, p)) { diff --git a/src/xact.cc b/src/xact.cc index 082eacc8..5e4b4c64 100644 --- a/src/xact.cc +++ b/src/xact.cc @@ -779,6 +779,8 @@ void auto_xact_t::extend_xact(xact_base_t& xact, parse_context_t& context) journal->register_account(account->fullname(), new_post, journal->master); + extend_post(*new_post, *journal); + xact.add_post(new_post); new_post->account->add_post(new_post); |