summaryrefslogtreecommitdiff
path: root/amount.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2004-07-30 21:57:02 -0400
committerJohn Wiegley <johnw@newartisans.com>2004-07-30 21:57:02 -0400
commit94e76ae87e883291d13320738fe165c7a2a2415b (patch)
treeb90eff2ee3737ecdfea96dbee52ecd239fcb2578 /amount.cc
parent5087a60deef7c618a07562511e9a1fbf2414776c (diff)
downloadfork-ledger-94e76ae87e883291d13320738fe165c7a2a2415b.tar.gz
fork-ledger-94e76ae87e883291d13320738fe165c7a2a2415b.tar.bz2
fork-ledger-94e76ae87e883291d13320738fe165c7a2a2415b.zip
two major changes
Complete changed the way format strings are handled. They are now compiled first, which is far more efficient than what was being done before. Also, there is now a global ledger::commodity_t::commodities map, which saves me from having to pass the current journal around to a zillion different functions, for the sole purpose of making sure that all commodity symbols that are parsed refer to the same commodity object.
Diffstat (limited to 'amount.cc')
-rw-r--r--amount.cc45
1 files changed, 23 insertions, 22 deletions
diff --git a/amount.cc b/amount.cc
index 5d1348af..80fd3ee9 100644
--- a/amount.cc
+++ b/amount.cc
@@ -10,8 +10,6 @@
namespace ledger {
-commodity_t * amount_t::null_commodity = NULL;
-
static void mpz_round(mpz_t value, int precision)
{
mpz_t divisor;
@@ -544,7 +542,7 @@ void parse_commodity(std::istream& in, std::string& symbol)
}
}
-void amount_t::parse(std::istream& in, ledger_t * ledger)
+void amount_t::parse(std::istream& in)
{
// The possible syntax for an amount is:
//
@@ -607,25 +605,10 @@ void amount_t::parse(std::istream& in, ledger_t * ledger)
assert(precision <= MAX_PRECISION);
// Create the commodity if has not already been seen.
- if (ledger) {
- commodity = ledger->find_commodity(symbol, true);
- commodity->flags |= flags;
- if (precision > commodity->precision)
- commodity->precision = precision;
- }
- else if (symbol.empty()) {
- if (! null_commodity) {
- commodity = null_commodity = new commodity_t(symbol, precision, flags);
- } else {
- commodity = null_commodity;
- commodity->flags |= flags;
- if (precision > commodity->precision)
- commodity->precision = precision;
- }
- }
- else {
- commodity = new commodity_t(symbol, precision, flags);
- }
+ commodity = commodity_t::find_commodity(symbol, true);
+ commodity->flags |= flags;
+ if (precision > commodity->precision)
+ commodity->precision = precision;
// The number is specified as the user desires, with the commodity
// flags telling how to parse it.
@@ -696,6 +679,24 @@ void (*commodity_t::updater)(commodity_t * commodity,
const amount_t& price,
const std::time_t moment) = NULL;
+commodities_map commodity_t::commodities;
+
+commodity_t * commodity_t::find_commodity(const std::string& symbol,
+ bool auto_create)
+{
+ commodities_map::const_iterator i = commodities.find(symbol);
+ if (i != commodities.end())
+ return (*i).second;
+
+ if (auto_create) {
+ commodity_t * commodity = new commodity_t(symbol);
+ add_commodity(commodity);
+ return commodity;
+ }
+
+ return NULL;
+}
+
amount_t commodity_t::value(const std::time_t moment)
{
std::time_t age = 0;