From b03041698277591677f303afd4c0d715ea844ba8 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sat, 21 Aug 2004 03:54:27 -0400 Subject: fixes to how commodities are looked up using quotes.cc --- amount.cc | 3 ++- amount.h | 8 +++++--- binary.cc | 4 +++- quotes.cc | 39 +++++++++++++++++++++++---------------- quotes.h | 1 + textual.cc | 6 ++---- 6 files changed, 36 insertions(+), 25 deletions(-) diff --git a/amount.cc b/amount.cc index ccad248b..1ec23926 100644 --- a/amount.cc +++ b/amount.cc @@ -980,7 +980,8 @@ amount_t commodity_t::value(const std::time_t moment) } if (updater) - (*updater)(this, moment, age, price); + (*updater)(this, moment, age, + history.size() > 0 ? (*history.rbegin()).first : 0, price); return price; } diff --git a/amount.h b/amount.h index 21d5bbfe..8eebdd4e 100644 --- a/amount.h +++ b/amount.h @@ -192,8 +192,7 @@ std::ostream& operator<<(std::ostream& out, const amount_t& amt); #define COMMODITY_STYLE_SEPARATED 0x0002 #define COMMODITY_STYLE_EUROPEAN 0x0004 #define COMMODITY_STYLE_THOUSANDS 0x0008 -#define COMMODITY_STYLE_CONSULTED 0x0010 -#define COMMODITY_STYLE_NOMARKET 0x0020 +#define COMMODITY_STYLE_NOMARKET 0x0010 typedef std::map history_map; typedef std::pair history_pair; @@ -210,6 +209,7 @@ class commodity_t virtual void operator()(commodity_t * commodity, const std::time_t moment, const std::time_t date, + const std::time_t last, amount_t& price) = 0; }; @@ -222,6 +222,7 @@ class commodity_t unsigned short precision; unsigned short flags; history_map history; + std::time_t last_lookup; amount_t conversion; ident_t ident; @@ -254,7 +255,8 @@ class commodity_t commodity_t(const std::string& _symbol = "", unsigned int _precision = 0, unsigned int _flags = COMMODITY_STYLE_DEFAULTS) - : symbol(_symbol), quote(false), precision(_precision), flags(_flags) { + : symbol(_symbol), quote(false), precision(_precision), + flags(_flags), last_lookup(0) { check_symbol(); } diff --git a/binary.cc b/binary.cc index cde30c04..ea98eb2b 100644 --- a/binary.cc +++ b/binary.cc @@ -9,7 +9,7 @@ namespace ledger { const unsigned long binary_magic_number = 0xFFEED765; -static const unsigned long format_version = 0x0002000c; +static const unsigned long format_version = 0x0002000e; bool binary_parser_t::test(std::istream& in) const { @@ -151,6 +151,7 @@ commodity_t * read_binary_commodity(std::istream& in) read_binary_amount(in, amt); commodity->history.insert(history_pair(when, amt)); } + read_binary_number(in, commodity->last_lookup); read_binary_amount(in, commodity->conversion); @@ -351,6 +352,7 @@ void write_binary_commodity(std::ostream& out, commodity_t * commodity) write_binary_number(out, (*i).first); write_binary_amount(out, (*i).second); } + write_binary_number(out, commodity->last_lookup); write_binary_amount(out, commodity->conversion); } diff --git a/quotes.cc b/quotes.cc index 986bfa84..771fd790 100644 --- a/quotes.cc +++ b/quotes.cc @@ -8,47 +8,54 @@ namespace ledger { void quotes_by_script::operator()(commodity_t * commodity, const std::time_t moment, const std::time_t date, + const std::time_t last, amount_t& price) { - if (commodity->flags & COMMODITY_STYLE_CONSULTED || - std::difftime(moment, now) < pricing_leeway || + DEBUG_CLASS("ledger.quotes.download"); + + DEBUG_PRINT_("commodity: " << commodity->symbol); + DEBUG_PRINT_TIME_(now); + DEBUG_PRINT_TIME_(moment); + DEBUG_PRINT_TIME_(date); + DEBUG_PRINT_TIME_(last); + DEBUG_PRINT_TIME_(commodity->last_lookup); + DEBUG_PRINT_("pricing_leeway is " << pricing_leeway); + + if (std::difftime(now, commodity->last_lookup) < pricing_leeway || + std::difftime(now, last) < pricing_leeway || (price && std::difftime(moment, date) <= pricing_leeway)) return; using namespace std; - DEBUG_PRINT("ledger.quotes.download", - "downloading quote for symbol " << commodity->symbol); - DEBUG_PRINT("ledger.quotes.download", - "pricing_leeway is " << pricing_leeway); - DEBUG_PRINT_TIME("ledger.quotes.download", now); - DEBUG_PRINT_TIME("ledger.quotes.download", moment); - DEBUG_PRINT_TIME("ledger.quotes.download", date); + DEBUG_PRINT_("downloading quote for symbol " << commodity->symbol); // Only consult the Internet once for any commodity - commodity->flags |= COMMODITY_STYLE_CONSULTED; + commodity->last_lookup = now; cache_dirty = true; char buf[256]; buf[0] = '\0'; + bool success = true; + if (FILE * fp = popen((string("getquote ") + commodity->symbol).c_str(), "r")) { - if (feof(fp) || ! fgets(buf, 255, fp)) { - fclose(fp); - return; - } + if (feof(fp) || ! fgets(buf, 255, fp)) + success = false; fclose(fp); } - if (buf[0]) { + if (success && buf[0]) { char * p = strchr(buf, '\n'); if (p) *p = '\0'; + DEBUG_PRINT_("downloaded quote: " << buf); + price.parse(buf); commodity->add_price(now, price); - if (! price_db.empty()) { + if (price && ! price_db.empty()) { char buf[128]; strftime(buf, 127, "%Y/%m/%d %H:%M:%S", localtime(&now)); ofstream database(price_db.c_str(), ios_base::out | ios_base::app); diff --git a/quotes.h b/quotes.h index a6e05cb1..8418b074 100644 --- a/quotes.h +++ b/quotes.h @@ -21,6 +21,7 @@ class quotes_by_script : public commodity_t::updater_t virtual void operator()(commodity_t * commodity, const std::time_t moment, const std::time_t date, + const std::time_t last, amount_t& price); }; diff --git a/textual.cc b/textual.cc index 7256aee2..51d221cb 100644 --- a/textual.cc +++ b/textual.cc @@ -462,8 +462,7 @@ unsigned int textual_parser_t::parse(std::istream& in, parse_commodity(in, symbol); commodity_t * commodity = commodity_t::find_commodity(symbol, true); - commodity->flags |= (COMMODITY_STYLE_CONSULTED | - COMMODITY_STYLE_NOMARKET); + commodity->flags |= COMMODITY_STYLE_NOMARKET; break; } @@ -569,8 +568,7 @@ unsigned int textual_parser_t::parse(std::istream& in, done: if (time_commodity) { time_commodity->precision = 2; - time_commodity->flags |= (COMMODITY_STYLE_CONSULTED | - COMMODITY_STYLE_NOMARKET); + time_commodity->flags |= COMMODITY_STYLE_NOMARKET; } if (errors > 0) { -- cgit v1.2.3