From 3a3227298dcd4faabd93e6f4bcaede10f29b13fd Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sat, 25 Sep 2004 02:51:55 -0400 Subject: [PATCH] if a commodity price cannot be downloaded, report it as an error --- binary.cc | 2 +- main.cc | 3 ++- option.cc | 6 ++---- option.h | 12 ++++++++++++ quotes.cc | 15 ++++++++++----- scripts/getquote | 6 ++---- 6 files changed, 29 insertions(+), 15 deletions(-) diff --git a/binary.cc b/binary.cc index 23fdbce3..1c33df7f 100644 --- a/binary.cc +++ b/binary.cc @@ -10,7 +10,7 @@ namespace ledger { static unsigned long binary_magic_number = 0xFFEED765; -static unsigned long format_version = 0x0002001b; +static unsigned long format_version = 0x00020020; static account_t ** accounts; static account_t ** accounts_next; diff --git a/main.cc b/main.cc index c2b780b9..509676cb 100644 --- a/main.cc +++ b/main.cc @@ -8,6 +8,7 @@ using namespace ledger; #include #include #include +#include #include #include #include @@ -349,7 +350,7 @@ int main(int argc, char * argv[], char * envp[]) try { return parse_and_report(argc, argv, envp); } - catch (error& err) { + catch (const std::exception& err) { std::cerr << "Error: " << err.what() << std::endl; return 1; } diff --git a/option.cc b/option.cc index 1f652643..d20f9de1 100644 --- a/option.cc +++ b/option.cc @@ -111,8 +111,7 @@ void process_arguments(std::list& options, goto next; } - std::cerr << "Error: illegal option " << *i << std::endl; - std::exit(1); + throw option_error(std::string("illegal option ") + *i); } else { for (std::list::iterator j = options.begin(); j != options.end(); @@ -132,8 +131,7 @@ void process_arguments(std::list& options, } } - std::cerr << "Error: illegal option -- " << (*i)[1] << std::endl; - std::exit(1); + throw option_error(std::string("illegal option -- ") + (*i)[1]); } next: diff --git a/option.h b/option.h index 49f228cd..2e4d7599 100644 --- a/option.h +++ b/option.h @@ -3,6 +3,7 @@ #include #include +#include struct option_handler { bool handled; @@ -19,6 +20,17 @@ struct option_t { option_t() : short_opt(0), wants_arg(false), handler(NULL) {} }; +class option_error : public std::exception { + std::string reason; + public: + option_error(const std::string& _reason) throw() : reason(_reason) {} + virtual ~option_error() throw() {} + + virtual const char* what() const throw() { + return reason.c_str(); + } +}; + void add_option_handler(std::list& options, const std::string& label, const std::string& opt_chars, option_handler& option); bool process_option(std::list& options, diff --git a/quotes.cc b/quotes.cc index 4b1596ea..9f43352e 100644 --- a/quotes.cc +++ b/quotes.cc @@ -1,5 +1,6 @@ #include "quotes.h" #include "datetime.h" +#include "error.h" #include "debug.h" #include @@ -32,10 +33,6 @@ void quotes_by_script::operator()(commodity_t& commodity, DEBUG_PRINT_("downloading quote for symbol " << commodity.symbol); - // Only consult the Internet once for any commodity - commodity.last_lookup = now; - cache_dirty = true; - char buf[256]; buf[0] = '\0'; @@ -45,7 +42,8 @@ void quotes_by_script::operator()(commodity_t& commodity, commodity.symbol).c_str(), "r")) { if (feof(fp) || ! fgets(buf, 255, fp)) success = false; - fclose(fp); + if (pclose(fp) != 0) + success = false; } if (success && buf[0]) { @@ -57,6 +55,9 @@ void quotes_by_script::operator()(commodity_t& commodity, price.parse(buf); commodity.add_price(now, price); + commodity.last_lookup = now; + cache_dirty = true; + if (price && ! price_db.empty()) { char buf[128]; strftime(buf, 127, "%Y/%m/%d %H:%M:%S", localtime(&now)); @@ -64,6 +65,10 @@ void quotes_by_script::operator()(commodity_t& commodity, database << "P " << buf << " " << commodity.symbol << " " << price << endl; } + } else { + throw error(std::string("Failed to download price for '") + + commodity.symbol + "' (command: \"getquote " + + commodity.symbol + "\")"); } } diff --git a/scripts/getquote b/scripts/getquote index cf8c8abd..8daf6633 100755 --- a/scripts/getquote +++ b/scripts/getquote @@ -1,16 +1,14 @@ #!/usr/bin/perl -exit 0 if $ARGV[0] eq "\$"; - use Finance::Quote; $q = Finance::Quote->new; - $q->timeout(60); $q->require_labels(qw/price/); %quotes = $q->fetch("nasdaq", $ARGV[0]); - if ($quotes{$ARGV[0], "price"}) { print "\$", $quotes{$ARGV[0], "price"}, "\n"; +} else { + exit 1; }