summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-02-12 04:57:19 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-02-12 04:57:19 -0400
commit8ee8af3bfba440f6c185aa03b82cb7d0d785b3f3 (patch)
tree7eed4bada75f1d42e705d339fb68ceb80aea7180 /src
parentc328b1b3b2575ce27ce5bfc4d901a7e011d010a8 (diff)
downloadfork-ledger-8ee8af3bfba440f6c185aa03b82cb7d0d785b3f3.tar.gz
fork-ledger-8ee8af3bfba440f6c185aa03b82cb7d0d785b3f3.tar.bz2
fork-ledger-8ee8af3bfba440f6c185aa03b82cb7d0d785b3f3.zip
Restored the "prices" command.
Diffstat (limited to 'src')
-rw-r--r--src/commodity.h1
-rw-r--r--src/iterators.cc66
-rw-r--r--src/iterators.h37
-rw-r--r--src/report.cc22
-rw-r--r--src/report.h4
-rw-r--r--src/system.hh1
6 files changed, 117 insertions, 14 deletions
diff --git a/src/commodity.h b/src/commodity.h
index 0d1bc785..9522bb43 100644
--- a/src/commodity.h
+++ b/src/commodity.h
@@ -276,7 +276,6 @@ public:
base->larger = arg;
}
-protected:
optional<varied_history_t&> varied_history() {
if (base->varied_history)
return *base->varied_history;
diff --git a/src/iterators.cc b/src/iterators.cc
index 6745f971..ac5b2fc8 100644
--- a/src/iterators.cc
+++ b/src/iterators.cc
@@ -72,6 +72,72 @@ xact_t * journal_xacts_iterator::operator()()
return xact;
}
+void xacts_commodities_iterator::reset(journal_t& journal)
+{
+ journal_xacts.reset(journal);
+
+ std::set<commodity_t *> commodities;
+
+ for (xact_t * xact = journal_xacts(); xact; xact = journal_xacts()) {
+ commodity_t& comm(xact->amount.commodity());
+ if (comm.flags() & COMMODITY_NOMARKET)
+ continue;
+ commodities.insert(&comm);
+ }
+
+ foreach (commodity_t * comm, commodities) {
+ optional<commodity_t::varied_history_t&> history = comm->varied_history();
+ if (! history)
+ continue;
+
+ entry_temps.push_back(new entry_t);
+ entry_temps.back()->payee = comm->symbol();
+ entry_temps.back()->_date = CURRENT_DATE();
+
+ foreach (commodity_t::base_t::history_by_commodity_map::value_type pair,
+ history->histories) {
+ commodity_t& price_comm(*pair.first);
+ commodity_t::history_t& price_hist(pair.second);
+
+ acct_temps.push_back(account_t(NULL, price_comm.symbol()));
+
+ foreach (commodity_t::base_t::history_map::value_type hpair,
+ price_hist.prices) {
+ xact_temps.push_back(xact_t(&acct_temps.back()));
+ xact_t& temp = xact_temps.back();
+ temp._date = hpair.first.date();
+ temp.entry = entry_temps.back();
+ temp.amount = hpair.second;
+ temp.set_flags(ITEM_GENERATED | ITEM_TEMP);
+
+ entry_temps.back()->add_xact(&temp);
+ }
+ }
+ }
+
+ entries.entries_i = entry_temps.begin();
+ entries.entries_end = entry_temps.end();
+
+ entries.entries_uninitialized = false;
+
+ entry_t * entry = entries();
+ if (entry != NULL)
+ xacts.reset(*entry);
+}
+
+xact_t * xacts_commodities_iterator::operator()()
+{
+ xact_t * xact = xacts();
+ if (xact == NULL) {
+ entry_t * entry = entries();
+ if (entry != NULL) {
+ xacts.reset(*entry);
+ xact = xacts();
+ }
+ }
+ return xact;
+}
+
account_t * basic_accounts_iterator::operator()()
{
while (! accounts_i.empty() &&
diff --git a/src/iterators.h b/src/iterators.h
index 48abf5fa..8bf05986 100644
--- a/src/iterators.h
+++ b/src/iterators.h
@@ -110,12 +110,12 @@ public:
*/
class entries_iterator : public noncopyable
{
+public:
entries_list::iterator entries_i;
entries_list::iterator entries_end;
bool entries_uninitialized;
-public:
entries_iterator() : entries_uninitialized(true) {
TRACE_CTOR(entries_iterator, "");
}
@@ -164,6 +164,41 @@ public:
*
* Long.
*/
+class xacts_commodities_iterator : public xacts_iterator
+{
+protected:
+ journal_xacts_iterator journal_xacts;
+ entries_iterator entries;
+ entry_xacts_iterator xacts;
+
+ std::list<xact_t> xact_temps;
+ std::list<account_t> acct_temps;
+ entries_list entry_temps;
+
+public:
+ xacts_commodities_iterator() {
+ TRACE_CTOR(xacts_commodities_iterator, "");
+ }
+ xacts_commodities_iterator(journal_t& journal) {
+ TRACE_CTOR(xacts_commodities_iterator, "journal_t&");
+ reset(journal);
+ }
+ virtual ~xacts_commodities_iterator() throw() {
+ TRACE_DTOR(xacts_commodities_iterator);
+ foreach (entry_t * entry, entry_temps)
+ checked_delete(entry);
+ }
+
+ void reset(journal_t& journal);
+
+ virtual xact_t * operator()();
+};
+
+/**
+ * @brief Brief
+ *
+ * Long.
+ */
class accounts_iterator : public noncopyable
{
public:
diff --git a/src/report.cc b/src/report.cc
index 9da1c357..1f8e4743 100644
--- a/src/report.cc
+++ b/src/report.cc
@@ -78,10 +78,9 @@ report_t::report_t(session_t& _session)
HANDLER(plot_amount_format_).on("%D %(S(t))\n");
HANDLER(plot_total_format_).on("%D %(S(T))\n");
- HANDLER(write_hdr_format_).on("%d %Y%C%P\n");
- HANDLER(write_xact_format_).on(" %-34W %12o%n\n");
-
- HANDLER(prices_format_).on("%[%Y/%m/%d %H:%M:%S %Z] %-10A %12t %12T\n");
+ HANDLER(prices_format_).on(
+ "%-.9(date) %-.10(payee) %-10(account) %12(strip(display_amount))\n%/"
+ "%21|%-.9(date) %-.10(payee) %-10(account) %12(strip(display_amount))\n");
HANDLER(pricesdb_format_).on("P %[%Y/%m/%d %H:%M:%S] %A %t\n");
HANDLER(csv_format_).on(
@@ -140,8 +139,11 @@ void report_t::accounts_report(acct_handler_ptr handler)
session.clean_accounts();
}
-void report_t::commodities_report(const string& format)
+void report_t::commodities_report(xact_handler_ptr handler)
{
+ xacts_commodities_iterator walker(*session.journal.get());
+ pass_down_xacts(chain_xact_handlers(*this, handler), walker);
+ session.clean_xacts();
}
value_t report_t::fn_amount_expr(call_scope_t& scope)
@@ -474,8 +476,6 @@ option_t<report_t> * report_t::lookup_option(const char * p)
OPT(weekly);
else OPT_(wide);
else OPT(wide_register_format_);
- else OPT(write_hdr_format_);
- else OPT(write_xact_format_);
break;
case 'x':
OPT_CH(comm_as_payee);
@@ -536,9 +536,13 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
(reporter<>(new format_xacts(*this, HANDLER(print_format_).str()),
*this));
else if (is_eq(p, "prices"))
- return NULL; // jww (2009-02-07): NYI
+ return expr_t::op_t::wrap_functor
+ (reporter<xact_t, xact_handler_ptr, &report_t::commodities_report>
+ (new format_xacts(*this, HANDLER(prices_format_).str()), *this));
else if (is_eq(p, "pricesdb"))
- return NULL; // jww (2009-02-07): NYI
+ return expr_t::op_t::wrap_functor
+ (reporter<xact_t, xact_handler_ptr, &report_t::commodities_report>
+ (new format_xacts(*this, HANDLER(pricesdb_format_).str()), *this));
break;
case 'r':
diff --git a/src/report.h b/src/report.h
index 96965b16..df1186ba 100644
--- a/src/report.h
+++ b/src/report.h
@@ -123,7 +123,7 @@ public:
void xacts_report(xact_handler_ptr handler);
void entry_report(xact_handler_ptr handler, entry_t& entry);
void accounts_report(acct_handler_ptr handler);
- void commodities_report(const string& format);
+ void commodities_report(xact_handler_ptr handler);
void sum_all_accounts();
@@ -394,8 +394,6 @@ public:
OPTION(report_t, weekly); // -W
OPTION(report_t, wide); // -w
OPTION(report_t, wide_register_format_);
- OPTION(report_t, write_hdr_format_);
- OPTION(report_t, write_xact_format_);
OPTION(report_t, yearly); // -Y
};
diff --git a/src/system.hh b/src/system.hh
index 96b574e5..080d187f 100644
--- a/src/system.hh
+++ b/src/system.hh
@@ -78,6 +78,7 @@
#include <map>
#include <memory>
#include <new>
+#include <set>
#include <stack>
#include <string>
#include <vector>