Got date, payee and accounts back into the register report.

This commit is contained in:
John Wiegley 2008-08-03 00:22:55 -04:00
parent dfc14dfff3
commit 363fb6d558
10 changed files with 82 additions and 50 deletions

View file

@ -32,6 +32,8 @@
#include "entry.h" #include "entry.h"
#include "journal.h" #include "journal.h"
#include "format.h" #include "format.h"
#include "session.h"
#include "report.h"
namespace ledger { namespace ledger {
@ -361,18 +363,13 @@ namespace {
return entry.date(); return entry.date();
} }
value_t get_payee(entry_t& entry) value_t get_payee(entry_t& entry) {
{
return string_value(entry.payee); return string_value(entry.payee);
} }
typedef value_t (*entry_func_t)(entry_t&); template <value_t (*Func)(entry_t&)>
value_t get_wrapper(call_scope_t& scope) {
template <entry_func_t Func> return (*Func)(find_scope<entry_t>(scope));
value_t get_wrapper(call_scope_t& scope)
{
xact_t& xact(downcast<xact_t>(*scope.parent));
return (*Func)(*xact.entry);
} }
} }
@ -383,12 +380,24 @@ expr_t::ptr_op_t entry_t::lookup(const string& name)
if (name[1] == '\0' || name == "date") if (name[1] == '\0' || name == "date")
return WRAP_FUNCTOR(get_wrapper<&get_date>); return WRAP_FUNCTOR(get_wrapper<&get_date>);
break; break;
case 'f':
if (name.find("fmt_") == 0) {
switch (name[4]) {
case 'D':
return WRAP_FUNCTOR(get_wrapper<&get_date>);
case 'P':
return WRAP_FUNCTOR(get_wrapper<&get_payee>);
}
}
break;
case 'p': case 'p':
if (name[1] == '\0' || name == "payee") if (name[1] == '\0' || name == "payee")
return WRAP_FUNCTOR(get_wrapper<&get_payee>); return WRAP_FUNCTOR(get_wrapper<&get_payee>);
break; break;
} }
return expr_t::ptr_op_t(); return journal->owner->current_report->lookup(name);
} }
bool entry_t::valid() const bool entry_t::valid() const

View file

@ -247,18 +247,9 @@ void format_t::format(std::ostream& out_str, scope_t& scope)
if (elem->expr.is_function()) { if (elem->expr.is_function()) {
call_scope_t args(scope); call_scope_t args(scope);
args.push_back(long(elem->max_width)); args.push_back(long(elem->max_width));
if (elem->max_width == 0)
elem->expr.get_function()(args).dump(out, elem->min_width); elem->expr.get_function()(args).dump(out, elem->min_width);
else
out << truncate(elem->expr.get_function()(args).as_string(),
elem->max_width);
} else { } else {
if (elem->max_width == 0)
elem->expr.calc(scope).dump(out, elem->min_width); elem->expr.calc(scope).dump(out, elem->min_width);
else
out << truncate(elem->expr.calc(scope).as_string(),
elem->max_width);
} }
} }
catch (const calc_error&) { catch (const calc_error&) {

12
main.cc
View file

@ -462,16 +462,14 @@ int main(int argc, char * argv[], char * envp[])
session->register_parser(new ledger::qif_parser_t); session->register_parser(new ledger::qif_parser_t);
session->register_parser(new ledger::textual_parser_t); session->register_parser(new ledger::textual_parser_t);
std::auto_ptr<ledger::report_t> report(new ledger::report_t(*session.get())); session->current_report.reset(new ledger::report_t(*session.get()));
status = read_and_report(*report.get(), argc, argv, envp); status = read_and_report(*session->current_report.get(), argc, argv, envp);
if (DO_VERIFY()) { if (DO_VERIFY())
ledger::set_session_context(); ledger::set_session_context();
} else { else
report.release(); session.release(); // don't free anything!
session.release();
}
} }
catch (const std::exception& err) { catch (const std::exception& err) {
std::cout.flush(); std::cout.flush();

View file

@ -521,6 +521,16 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
case 'f': case 'f':
if (std::strcmp(p, "format") == 0) if (std::strcmp(p, "format") == 0)
return MAKE_FUNCTOR(report_t::option_format); return MAKE_FUNCTOR(report_t::option_format);
else if (name.find("fmt_") == 0) {
switch (name[4]) {
case 't':
return MAKE_FUNCTOR(report_t::get_amount_expr);
#if 0
case 'T':
return MAKE_FUNCTOR(report_t::get_total_expr);
#endif
}
}
break; break;
case 't': case 't':

View file

@ -242,6 +242,14 @@ public:
return args[0]; return args[0];
} }
//
// Formatting functions
//
value_t get_amount_expr(call_scope_t&) {
return NULL_VALUE;
}
// //
// Scope members // Scope members
// //

16
scope.h
View file

@ -66,14 +66,14 @@ template <typename T>
inline T& find_scope(scope_t& scope, bool skip_this = true) { inline T& find_scope(scope_t& scope, bool skip_this = true) {
optional<scope_t&> found = scope.find_scope(typeid(T), skip_this); optional<scope_t&> found = scope.find_scope(typeid(T), skip_this);
assert(found); assert(found);
return downcast<T>(*found); return static_cast<T&>(*found);
} }
template <typename T> template <typename T>
inline optional<T&> maybe_find_scope(scope_t& scope, bool skip_this = true) { inline optional<T&> maybe_find_scope(scope_t& scope, bool skip_this = true) {
optional<scope_t&> found = scope.find_scope(typeid(T), skip_this); optional<scope_t&> found = scope.find_scope(typeid(T), skip_this);
if (found) if (found)
return optional<T&>(downcast<T>(*found)); return optional<T&>(static_cast<T&>(*found));
else else
return none; return none;
} }
@ -102,12 +102,14 @@ public:
virtual optional<scope_t&> find_scope(const std::type_info& type, virtual optional<scope_t&> find_scope(const std::type_info& type,
bool skip_this = true) { bool skip_this = true) {
for (scope_t * ptr = (skip_this ? parent : this); for (scope_t * ptr = (skip_this ? parent : this); ptr; ) {
ptr; if (typeid(*ptr) == type)
ptr = polymorphic_downcast<child_scope_t *>(ptr)->parent)
if (typeid(ptr) == type)
return *ptr; return *ptr;
if (child_scope_t * scope = dynamic_cast<child_scope_t *>(ptr))
ptr = scope->parent;
else
ptr = NULL;
}
return none; return none;
} }
}; };

View file

@ -30,6 +30,7 @@
*/ */
#include "session.h" #include "session.h"
#include "report.h"
#include "walk.h" #include "walk.h"
namespace ledger { namespace ledger {
@ -113,6 +114,11 @@ session_t::session_t()
TRACE_CTOR(session_t, ""); TRACE_CTOR(session_t, "");
} }
session_t::~session_t()
{
TRACE_DTOR(session_t);
}
std::size_t session_t::read_journal(journal_t& journal, std::size_t session_t::read_journal(journal_t& journal,
std::istream& in, std::istream& in,
const path& pathname, const path& pathname,

View file

@ -39,6 +39,8 @@
namespace ledger { namespace ledger {
class report_t;
class session_t : public noncopyable, public scope_t class session_t : public noncopyable, public scope_t
{ {
static void initialize(); static void initialize();
@ -50,6 +52,8 @@ class session_t : public noncopyable, public scope_t
public: public:
static session_t * current; static session_t * current;
scoped_ptr<report_t> current_report;
path data_file; path data_file;
optional<path> init_file; optional<path> init_file;
optional<path> cache_file; optional<path> cache_file;
@ -89,10 +93,7 @@ public:
mutable accounts_map accounts_cache; mutable accounts_map accounts_cache;
session_t(); session_t();
virtual ~session_t();
virtual ~session_t() {
TRACE_DTOR(session_t);
}
journal_t * create_journal() { journal_t * create_journal() {
journal_t * journal = new journal_t(this); journal_t * journal = new journal_t(this);

View file

@ -1574,11 +1574,11 @@ void value_t::dump(std::ostream& out, const int first_width,
break; break;
case DATETIME: case DATETIME:
out << as_datetime(); out << format_datetime(as_datetime());
break; break;
case DATE: case DATE:
out << as_date(); out << format_date(as_date());
break; break;
case INTEGER: case INTEGER:

25
xact.cc
View file

@ -78,6 +78,10 @@ namespace {
return xact.date(); return xact.date();
} }
value_t get_payee(xact_t& xact) {
return string_value(xact.entry->payee);
}
value_t get_amount(xact_t& xact) { value_t get_amount(xact_t& xact) {
return xact.amount; return xact.amount;
} }
@ -90,19 +94,16 @@ namespace {
return string_value(xact.note ? *xact.note : ":NOTELESS:"); return string_value(xact.note ? *xact.note : ":NOTELESS:");
} }
value_t get_account(call_scope_t& scope) { value_t get_account(call_scope_t& scope)
{
xact_t& xact(downcast<xact_t>(*scope.parent)); xact_t& xact(downcast<xact_t>(*scope.parent));
long width = 0; var_t<long> width(scope, 0);
if (scope.size() == 1)
width = var_t<long>(scope, 0);
// jww (2008-08-02): Accept a width here so that we can abbreviate the
// string.
string name = xact.reported_account()->fullname(); string name = xact.reported_account()->fullname();
if (width > 2) if (width && *width > 2)
name = format_t::truncate(name, width - 2, true); name = format_t::truncate(name, *width - 2, true);
if (xact.has_flags(XACT_VIRTUAL)) { if (xact.has_flags(XACT_VIRTUAL)) {
if (xact.must_balance()) if (xact.must_balance())
@ -172,8 +173,14 @@ expr_t::ptr_op_t xact_t::lookup(const string& name)
case 'f': case 'f':
if (name.find("fmt_") == 0) { if (name.find("fmt_") == 0) {
if (name == "fmt_A") switch (name[4]) {
case 'A':
return WRAP_FUNCTOR(get_account); return WRAP_FUNCTOR(get_account);
case 'D':
return WRAP_FUNCTOR(get_wrapper<&get_date>);
case 'P':
return WRAP_FUNCTOR(get_wrapper<&get_payee>);
}
} }
break; break;