Moved commodity_quote_from_script to quotes.cc

This commit is contained in:
John Wiegley 2009-06-24 16:53:50 +01:00
parent ba6254fd45
commit 0663ac0a2a
2 changed files with 45 additions and 78 deletions

View file

@ -31,46 +31,39 @@
#include <system.hh> #include <system.hh>
#include "amount.h"
#include "commodity.h"
#include "pool.h"
#include "quotes.h" #include "quotes.h"
namespace ledger { namespace ledger {
#if 0 optional<price_point_t>
void quotes_by_script::operator()(commodity_base_t& commodity, commodity_quote_from_script(commodity_t& commodity,
const datetime_t& moment, const optional<commodity_t&>& exchange_commodity)
const datetime_t& date,
const datetime_t& last,
amount_t& price)
{ {
DEBUG_CLASS("ledger.quotes.download"); DEBUG("commodity.download", "downloading quote for symbol " << commodity.symbol());
#if defined(DEBUG_ON)
DEBUG_("commodity: " << commodity.symbol); if (exchange_commodity)
DEBUG_TIME_(current_moment); DEBUG("commodity.download",
DEBUG_TIME_(moment); " in terms of commodity " << exchange_commodity->symbol());
DEBUG_TIME_(date); #endif
DEBUG_TIME_(last);
if (commodity.history)
DEBUG_TIME_(commodity.history->last_lookup);
DEBUG_("pricing_leeway is " << pricing_leeway);
if ((commodity.history &&
(current_moment - commodity.history->last_lookup) < pricing_leeway) ||
(current_moment - last) < pricing_leeway ||
(price && moment > date && (moment - date) <= pricing_leeway))
return;
using namespace std;
DEBUG_("downloading quote for symbol " << commodity.symbol);
char buf[256]; char buf[256];
buf[0] = '\0'; buf[0] = '\0';
bool success = true; string getquote_cmd("getquote \"");
getquote_cmd += commodity.symbol();
getquote_cmd += "\" \"";
if (exchange_commodity)
getquote_cmd += exchange_commodity->symbol();
getquote_cmd += "\"";
if (FILE * fp = popen((string("getquote \"") + DEBUG("commodity.download", "invoking command: " << getquote_cmd);
commodity.symbol + "\"").c_str(), "r")) {
if (feof(fp) || ! fgets(buf, 255, fp)) bool success = true;
if (FILE * fp = popen(getquote_cmd.c_str(), "r")) {
if (std::feof(fp) || ! std::fgets(buf, 255, fp))
success = false; success = false;
if (pclose(fp) != 0) if (pclose(fp) != 0)
success = false; success = false;
@ -79,31 +72,34 @@ void quotes_by_script::operator()(commodity_base_t& commodity,
} }
if (success && buf[0]) { if (success && buf[0]) {
char * p = strchr(buf, '\n'); if (char * p = std::strchr(buf, '\n')) *p = '\0';
if (p) *p = '\0'; DEBUG("commodity.download", "downloaded quote: " << buf);
DEBUG_("downloaded quote: " << buf); if (optional<price_point_t> point =
amount_t::current_pool->parse_price_directive(buf)) {
price.parse(buf); if (amount_t::current_pool->price_db) {
commodity.add_price(current_moment, price);
commodity.history->last_lookup = current_moment;
if (price && ! price_db.empty()) {
#if defined(__GNUG__) && __GNUG__ < 3 #if defined(__GNUG__) && __GNUG__ < 3
ofstream database(price_db.c_str(), ios::out | ios::app); ofstream database(*amount_t::current_pool->price_db,
ios::out | ios::app);
#else #else
ofstream database(price_db.c_str(), ios_base::out | ios_base::app); ofstream database(*amount_t::current_pool->price_db,
std::ios_base::out | std::ios_base::app);
#endif #endif
database << "P " << current_moment.to_string("%Y/%m/%d %H:%M:%S") database << "P "
<< " " << commodity.symbol << " " << price << endl; << format_datetime(point->when, string("%Y/%m/%d %H:%M:%S"))
<< " " << commodity.symbol()
<< " " << point->price
<< std::endl;
}
return point;
} }
} else { } else {
throw_(std::runtime_error, throw_(std::runtime_error,
_("Failed to download price for '%1' (command: \"getquote %1\")") _("Failed to download price for '%1' (command: \"getquote %2 %3\")")
<< commodity.symbol); << commodity.symbol() << commodity.symbol()
<< (exchange_commodity ? exchange_commodity->symbol() : "''"));
} }
return none;
} }
#endif
} // namespace ledger } // namespace ledger

View file

@ -46,39 +46,10 @@
#ifndef _QUOTES_H #ifndef _QUOTES_H
#define _QUOTES_H #define _QUOTES_H
namespace ledger { namespace ledger {
#if 0 optional<price_point_t>
/** commodity_quote_from_script(const optional<commodity_t&>& exchange_commodity);
* @brief Brief
*
* Long.
*/
class quotes_by_script : public noncopyable, public commodity_t::base_t::updater_t
{
string price_db;
std::size_t pricing_leeway;
quotes_by_script();
public:
quotes_by_script(path _price_db,
std::size_t _pricing_leeway)
: price_db(_price_db), pricing_leeway(_pricing_leeway) {
TRACE_CTOR(quotes_by_script, "path, std::size_t, bool&");
}
~quotes_by_script() throw() {
TRACE_DTOR(quotes_by_script);
}
virtual void operator()(commodity_base_t& commodity,
const datetime_t& moment,
const datetime_t& date,
const datetime_t& last,
amount_t& price);
};
#endif
} // namespace ledger } // namespace ledger