if a commodity price cannot be downloaded, report it as an error
This commit is contained in:
parent
fbad042178
commit
3a3227298d
6 changed files with 29 additions and 15 deletions
|
|
@ -10,7 +10,7 @@
|
||||||
namespace ledger {
|
namespace ledger {
|
||||||
|
|
||||||
static unsigned long binary_magic_number = 0xFFEED765;
|
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;
|
||||||
static account_t ** accounts_next;
|
static account_t ** accounts_next;
|
||||||
|
|
|
||||||
3
main.cc
3
main.cc
|
|
@ -8,6 +8,7 @@ using namespace ledger;
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <exception>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
@ -349,7 +350,7 @@ int main(int argc, char * argv[], char * envp[])
|
||||||
try {
|
try {
|
||||||
return parse_and_report(argc, argv, envp);
|
return parse_and_report(argc, argv, envp);
|
||||||
}
|
}
|
||||||
catch (error& err) {
|
catch (const std::exception& err) {
|
||||||
std::cerr << "Error: " << err.what() << std::endl;
|
std::cerr << "Error: " << err.what() << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -111,8 +111,7 @@ void process_arguments(std::list<option_t>& options,
|
||||||
goto next;
|
goto next;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cerr << "Error: illegal option " << *i << std::endl;
|
throw option_error(std::string("illegal option ") + *i);
|
||||||
std::exit(1);
|
|
||||||
} else {
|
} else {
|
||||||
for (std::list<option_t>::iterator j = options.begin();
|
for (std::list<option_t>::iterator j = options.begin();
|
||||||
j != options.end();
|
j != options.end();
|
||||||
|
|
@ -132,8 +131,7 @@ void process_arguments(std::list<option_t>& options,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cerr << "Error: illegal option -- " << (*i)[1] << std::endl;
|
throw option_error(std::string("illegal option -- ") + (*i)[1]);
|
||||||
std::exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
next:
|
next:
|
||||||
|
|
|
||||||
12
option.h
12
option.h
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
struct option_handler {
|
struct option_handler {
|
||||||
bool handled;
|
bool handled;
|
||||||
|
|
@ -19,6 +20,17 @@ struct option_t {
|
||||||
option_t() : short_opt(0), wants_arg(false), handler(NULL) {}
|
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<option_t>& options, const std::string& label,
|
void add_option_handler(std::list<option_t>& options, const std::string& label,
|
||||||
const std::string& opt_chars, option_handler& option);
|
const std::string& opt_chars, option_handler& option);
|
||||||
bool process_option(std::list<option_t>& options,
|
bool process_option(std::list<option_t>& options,
|
||||||
|
|
|
||||||
15
quotes.cc
15
quotes.cc
|
|
@ -1,5 +1,6 @@
|
||||||
#include "quotes.h"
|
#include "quotes.h"
|
||||||
#include "datetime.h"
|
#include "datetime.h"
|
||||||
|
#include "error.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
@ -32,10 +33,6 @@ void quotes_by_script::operator()(commodity_t& commodity,
|
||||||
|
|
||||||
DEBUG_PRINT_("downloading quote for symbol " << commodity.symbol);
|
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];
|
char buf[256];
|
||||||
buf[0] = '\0';
|
buf[0] = '\0';
|
||||||
|
|
||||||
|
|
@ -45,7 +42,8 @@ void quotes_by_script::operator()(commodity_t& commodity,
|
||||||
commodity.symbol).c_str(), "r")) {
|
commodity.symbol).c_str(), "r")) {
|
||||||
if (feof(fp) || ! fgets(buf, 255, fp))
|
if (feof(fp) || ! fgets(buf, 255, fp))
|
||||||
success = false;
|
success = false;
|
||||||
fclose(fp);
|
if (pclose(fp) != 0)
|
||||||
|
success = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (success && buf[0]) {
|
if (success && buf[0]) {
|
||||||
|
|
@ -57,6 +55,9 @@ void quotes_by_script::operator()(commodity_t& commodity,
|
||||||
price.parse(buf);
|
price.parse(buf);
|
||||||
commodity.add_price(now, price);
|
commodity.add_price(now, price);
|
||||||
|
|
||||||
|
commodity.last_lookup = now;
|
||||||
|
cache_dirty = true;
|
||||||
|
|
||||||
if (price && ! price_db.empty()) {
|
if (price && ! price_db.empty()) {
|
||||||
char buf[128];
|
char buf[128];
|
||||||
strftime(buf, 127, "%Y/%m/%d %H:%M:%S", localtime(&now));
|
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
|
database << "P " << buf << " " << commodity.symbol
|
||||||
<< " " << price << endl;
|
<< " " << price << endl;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw error(std::string("Failed to download price for '") +
|
||||||
|
commodity.symbol + "' (command: \"getquote " +
|
||||||
|
commodity.symbol + "\")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,14 @@
|
||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
|
|
||||||
exit 0 if $ARGV[0] eq "\$";
|
|
||||||
|
|
||||||
use Finance::Quote;
|
use Finance::Quote;
|
||||||
|
|
||||||
$q = Finance::Quote->new;
|
$q = Finance::Quote->new;
|
||||||
|
|
||||||
$q->timeout(60);
|
$q->timeout(60);
|
||||||
$q->require_labels(qw/price/);
|
$q->require_labels(qw/price/);
|
||||||
|
|
||||||
%quotes = $q->fetch("nasdaq", $ARGV[0]);
|
%quotes = $q->fetch("nasdaq", $ARGV[0]);
|
||||||
|
|
||||||
if ($quotes{$ARGV[0], "price"}) {
|
if ($quotes{$ARGV[0], "price"}) {
|
||||||
print "\$", $quotes{$ARGV[0], "price"}, "\n";
|
print "\$", $quotes{$ARGV[0], "price"}, "\n";
|
||||||
|
} else {
|
||||||
|
exit 1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue