summaryrefslogtreecommitdiff
path: root/src/commodity.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-06-18 18:50:49 +0100
committerJohn Wiegley <johnw@newartisans.com>2009-06-18 18:50:49 +0100
commit86dfc1e0be5dbfab195cba8a8be31d3dbf0f68c9 (patch)
tree55965512c0a1abf8f4bbb1519817c7b96fdcab12 /src/commodity.cc
parent1dc21c2d344aa52833e516fea831ff45d8bf7aa1 (diff)
downloadfork-ledger-86dfc1e0be5dbfab195cba8a8be31d3dbf0f68c9.tar.gz
fork-ledger-86dfc1e0be5dbfab195cba8a8be31d3dbf0f68c9.tar.bz2
fork-ledger-86dfc1e0be5dbfab195cba8a8be31d3dbf0f68c9.zip
The -X option now accepts price settings
For example, if you had 100 AU (onces of gold) and wanted to report it in dollars, but at a price of $997 per ounce, you could now easily say: ledger bal -X '$,AU=$997'
Diffstat (limited to 'src/commodity.cc')
-rw-r--r--src/commodity.cc33
1 files changed, 21 insertions, 12 deletions
diff --git a/src/commodity.cc b/src/commodity.cc
index d826feb6..e69758f0 100644
--- a/src/commodity.cc
+++ b/src/commodity.cc
@@ -1026,21 +1026,30 @@ commodity_t * commodity_pool_t::find_or_create(commodity_t& comm,
return create(comm, details, name);
}
-void commodity_pool_t::parse_commodity_price(char * optarg)
+commodity_t *
+commodity_pool_t::parse_commodity_prices(const std::string& str,
+ const bool add_prices,
+ const optional<datetime_t>& moment)
{
- char * equals = std::strchr(optarg, '=');
- if (! equals)
- return;
-
- optarg = skip_ws(optarg);
- while (equals > optarg && std::isspace(*(equals - 1)))
- equals--;
+ scoped_array<char> buf(new char[str.length() + 1]);
- std::string symbol(optarg, 0, equals - optarg);
- amount_t price(equals + 1);
+ std::strcpy(buf.get(), str.c_str());
- if (commodity_t * commodity = find_or_create(symbol))
- commodity->add_price(CURRENT_TIME(), price);
+ char * price = std::strchr(buf.get(), '=');
+ if (price)
+ *price++ = '\0';
+
+ if (commodity_t * commodity = find_or_create(trim_ws(buf.get()))) {
+ if (price && add_prices) {
+ for (char * p = std::strtok(price, ";");
+ p;
+ p = std::strtok(NULL, ";")) {
+ commodity->add_price(moment ? *moment : CURRENT_TIME(), amount_t(p));
+ }
+ }
+ return commodity;
+ }
+ return NULL;
}
} // namespace ledger