From 2d4fc10ca46ff5a777a16822665b5ee25586113b Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Thu, 26 May 2005 05:53:42 +0000 Subject: [PATCH] (parse_transaction): If an account name matches an alias definition, use the aliased account. (parse): Added an "!alias ALIAS = ACCOUNT" directive, to make it easier to manage long account names in a textual ledger file. --- textual.cc | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/textual.cc b/textual.cc index 79105617..e7189c27 100644 --- a/textual.cc +++ b/textual.cc @@ -36,6 +36,7 @@ namespace ledger { static std::string path; static unsigned int linenum; +static accounts_map account_aliases; #ifdef TIMELOG_SUPPORT static std::time_t time_in; @@ -161,7 +162,13 @@ transaction_t * parse_transaction(char * line, account_t * account) *e = '\0'; } - xact->account = account->find_account(p); + if (account_aliases.size() > 0) { + accounts_map::const_iterator i = account_aliases.find(p); + if (i != account_aliases.end()) + xact->account = (*i).second; + } + if (! xact->account) + xact->account = account->find_account(p); return xact.release(); } @@ -587,6 +594,25 @@ unsigned int textual_parser_t::parse(std::istream& in, python_eval(in, PY_EVAL_MULTI); } #endif + else if (word == "alias") { + char * b = skip_ws(p); + if (char * e = std::strchr(b, '=')) { + char * z = e - 1; + while (std::isspace(*z)) + *z-- = '\0'; + *e++ = '\0'; + e = skip_ws(e); + + // Once we have an alias name (b) and the target account + // name (e), add a reference to the account in the + // `account_aliases' map, which is used by the transaction + // parser to resolve alias references. + account_t * acct = account_stack.front()->find_account(e); + std::pair result + = account_aliases.insert(accounts_pair(b, acct)); + assert(result.second); + } + } break; }