reorganized report.cc into item.cc, expr.cc and format.cc
This commit is contained in:
parent
161d6f79bd
commit
dd5680c267
10 changed files with 172 additions and 159 deletions
|
|
@ -1,9 +1,16 @@
|
||||||
#include "constraint.h"
|
#include "constraint.h"
|
||||||
|
#include "expr.h"
|
||||||
|
|
||||||
#include <pcre.h>
|
#include <pcre.h>
|
||||||
|
|
||||||
namespace ledger {
|
namespace ledger {
|
||||||
|
|
||||||
|
constraints_t::~constraints_t()
|
||||||
|
{
|
||||||
|
if (predicate) delete predicate;
|
||||||
|
if (sort_order) delete sort_order;
|
||||||
|
}
|
||||||
|
|
||||||
mask_t::mask_t(const std::string& pat) : exclude(false)
|
mask_t::mask_t(const std::string& pat) : exclude(false)
|
||||||
{
|
{
|
||||||
const char * p = pat.c_str();
|
const char * p = pat.c_str();
|
||||||
|
|
@ -183,14 +190,13 @@ bool constraints_t::operator ()(const entry_t * entry) const
|
||||||
|
|
||||||
bool constraints_t::operator ()(const item_t * item) const
|
bool constraints_t::operator ()(const item_t * item) const
|
||||||
{
|
{
|
||||||
if (predicate && ! predicate->compute(begin(), end(), item))
|
if (predicate && ! predicate->compute(item, begin(), end()))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (! matches_date_range(item->date))
|
if (! matches_date_range(item->date))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (! payee_masks.empty() &&
|
if (! payee_masks.empty() && ! matches(payee_masks, item->payee))
|
||||||
! (matches(payee_masks, item->payee)))
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ bool matches(const masks_list& regexps, const std::string& str,
|
||||||
bool * by_exclusion = NULL);
|
bool * by_exclusion = NULL);
|
||||||
|
|
||||||
|
|
||||||
struct item_t;
|
struct node_t;
|
||||||
|
|
||||||
enum periodicity_t {
|
enum periodicity_t {
|
||||||
PERIOD_NONE,
|
PERIOD_NONE,
|
||||||
|
|
@ -137,10 +137,7 @@ class constraints_t
|
||||||
sort_order = NULL;
|
sort_order = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
~constraints_t() {
|
~constraints_t();
|
||||||
if (predicate) delete predicate;
|
|
||||||
if (sort_order) delete sort_order;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::time_t begin() const {
|
std::time_t begin() const {
|
||||||
return have_beginning ? begin_date : 0;
|
return have_beginning ? begin_date : 0;
|
||||||
|
|
|
||||||
27
expr.cc
27
expr.cc
|
|
@ -1,10 +1,11 @@
|
||||||
#include "expr.h"
|
#include "expr.h"
|
||||||
|
#include "textual.h"
|
||||||
|
|
||||||
namespace ledger {
|
namespace ledger {
|
||||||
|
|
||||||
balance_t node_t::compute(const std::time_t begin,
|
balance_t node_t::compute(const item_t * item,
|
||||||
const std::time_t end,
|
const std::time_t begin,
|
||||||
const item_t * item) const
|
const std::time_t end) const
|
||||||
{
|
{
|
||||||
balance_t temp;
|
balance_t temp;
|
||||||
|
|
||||||
|
|
@ -56,18 +57,18 @@ balance_t node_t::compute(const std::time_t begin,
|
||||||
|
|
||||||
case F_ARITH_MEAN:
|
case F_ARITH_MEAN:
|
||||||
assert(left);
|
assert(left);
|
||||||
temp = left->compute(begin, end, item);
|
temp = left->compute(item, begin, end);
|
||||||
temp /= amount_t(item->index + 1);
|
temp /= amount_t(item->index + 1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case F_NEG:
|
case F_NEG:
|
||||||
assert(left);
|
assert(left);
|
||||||
temp = left->compute(begin, end, item).negated();
|
temp = left->compute(item, begin, end).negated();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case F_ABS:
|
case F_ABS:
|
||||||
assert(left);
|
assert(left);
|
||||||
temp = abs(left->compute(begin, end, item));
|
temp = abs(left->compute(item, begin, end));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case F_REGEXP:
|
case F_REGEXP:
|
||||||
|
|
@ -78,7 +79,7 @@ balance_t node_t::compute(const std::time_t begin,
|
||||||
|
|
||||||
case F_VALUE: {
|
case F_VALUE: {
|
||||||
assert(left);
|
assert(left);
|
||||||
temp = left->compute(begin, end, item);
|
temp = left->compute(item, begin, end);
|
||||||
|
|
||||||
std::time_t moment = -1;
|
std::time_t moment = -1;
|
||||||
if (right) {
|
if (right) {
|
||||||
|
|
@ -94,15 +95,15 @@ balance_t node_t::compute(const std::time_t begin,
|
||||||
}
|
}
|
||||||
|
|
||||||
case O_NOT:
|
case O_NOT:
|
||||||
temp = left->compute(begin, end, item) ? 0 : 1;
|
temp = left->compute(item, begin, end) ? 0 : 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case O_QUES:
|
case O_QUES:
|
||||||
temp = left->compute(begin, end, item);
|
temp = left->compute(item, begin, end);
|
||||||
if (temp)
|
if (temp)
|
||||||
temp = right->left->compute(begin, end, item);
|
temp = right->left->compute(item, begin, end);
|
||||||
else
|
else
|
||||||
temp = right->right->compute(begin, end, item);
|
temp = right->right->compute(item, begin, end);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case O_AND:
|
case O_AND:
|
||||||
|
|
@ -118,8 +119,8 @@ balance_t node_t::compute(const std::time_t begin,
|
||||||
case O_DIV: {
|
case O_DIV: {
|
||||||
assert(left);
|
assert(left);
|
||||||
assert(right);
|
assert(right);
|
||||||
balance_t left_bal = left->compute(begin, end, item);
|
balance_t left_bal = left->compute(item, begin, end);
|
||||||
balance_t right_bal = right->compute(begin, end, item);
|
balance_t right_bal = right->compute(item, begin, end);
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case O_AND: temp = (left_bal && right_bal) ? 1 : 0; break;
|
case O_AND: temp = (left_bal && right_bal) ? 1 : 0; break;
|
||||||
case O_OR: temp = (left_bal || right_bal) ? 1 : 0; break;
|
case O_OR: temp = (left_bal || right_bal) ? 1 : 0; break;
|
||||||
|
|
|
||||||
11
expr.h
11
expr.h
|
|
@ -1,9 +1,9 @@
|
||||||
#ifndef _REPORT_H
|
#ifndef _EXPR_H
|
||||||
#define _REPORT_H
|
#define _EXPR_H
|
||||||
|
|
||||||
#include "ledger.h"
|
#include "ledger.h"
|
||||||
#include "constraint.h"
|
|
||||||
#include "balance.h"
|
#include "balance.h"
|
||||||
|
#include "constraint.h"
|
||||||
|
|
||||||
namespace ledger {
|
namespace ledger {
|
||||||
|
|
||||||
|
|
@ -76,11 +76,6 @@ struct node_t
|
||||||
balance_t compute(const item_t * item,
|
balance_t compute(const item_t * item,
|
||||||
const std::time_t begin = -1,
|
const std::time_t begin = -1,
|
||||||
const std::time_t end = -1) const;
|
const std::time_t end = -1) const;
|
||||||
|
|
||||||
balance_t compute(const item_t * item,
|
|
||||||
const constraint_t& constraints) const {
|
|
||||||
return compute(item, constraints.begin(), constraints.end());
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
node_t * parse_expr(std::istream& in, ledger_t * ledger);
|
node_t * parse_expr(std::istream& in, ledger_t * ledger);
|
||||||
|
|
|
||||||
31
format.cc
31
format.cc
|
|
@ -24,24 +24,18 @@ std::string maximal_account_name(const item_t * item,
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string format_string(const item_t * item, const format_t& format,
|
std::string format_t::report_line(const item_t * item,
|
||||||
const item_t * displayed_parent)
|
const item_t * displayed_parent) const
|
||||||
{
|
{
|
||||||
std::string result;
|
std::string result;
|
||||||
|
|
||||||
for (const char * p = format.format_string.c_str(); *p; p++) {
|
for (const char * p = format_string.c_str(); *p; p++) {
|
||||||
if (*p == '%') {
|
if (*p == '%') {
|
||||||
bool leftalign = false;
|
bool leftalign = false;
|
||||||
bool ignore = false;
|
|
||||||
int width = 0;
|
int width = 0;
|
||||||
int strict_width = 0;
|
int strict_width = 0;
|
||||||
|
|
||||||
++p;
|
++p;
|
||||||
if (*p == '?') {
|
|
||||||
ignore = false; //subsequent_line;
|
|
||||||
++p;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*p == '-') {
|
if (*p == '-') {
|
||||||
leftalign = true;
|
leftalign = true;
|
||||||
++p;
|
++p;
|
||||||
|
|
@ -75,12 +69,6 @@ std::string format_string(const item_t * item, const format_t& format,
|
||||||
if (width > 0)
|
if (width > 0)
|
||||||
out.width(width);
|
out.width(width);
|
||||||
|
|
||||||
if (ignore) {
|
|
||||||
out << " ";
|
|
||||||
result += out.str();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (*p) {
|
switch (*p) {
|
||||||
case '%':
|
case '%':
|
||||||
out << "%";
|
out << "%";
|
||||||
|
|
@ -94,8 +82,9 @@ std::string format_string(const item_t * item, const format_t& format,
|
||||||
assert(*p == ')');
|
assert(*p == ')');
|
||||||
|
|
||||||
node_t * style = parse_expr(num, NULL);
|
node_t * style = parse_expr(num, NULL);
|
||||||
balance_t value = style->compute(format.begin(), format.end(), item);
|
balance_t value = style->compute(item);
|
||||||
value.write(out, width, strict_width > 0 ? strict_width : width);
|
value.write(out, width, strict_width > 0 ? strict_width : width);
|
||||||
|
delete style;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -122,7 +111,7 @@ std::string format_string(const item_t * item, const format_t& format,
|
||||||
std::strftime(buf, 31, "%Y/%m/%d", std::gmtime(&item->date));
|
std::strftime(buf, 31, "%Y/%m/%d", std::gmtime(&item->date));
|
||||||
out << (strict_width == 0 ? buf : truncated(buf, strict_width));
|
out << (strict_width == 0 ? buf : truncated(buf, strict_width));
|
||||||
} else {
|
} else {
|
||||||
out << " ";
|
out << " ";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -151,15 +140,15 @@ std::string format_string(const item_t * item, const format_t& format,
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 't':
|
case 't':
|
||||||
if (format.value_style) {
|
if (value_style) {
|
||||||
balance_t value = format.compute_value(item);
|
balance_t value = compute_value(item);
|
||||||
value.write(out, width, strict_width > 0 ? strict_width : width);
|
value.write(out, width, strict_width > 0 ? strict_width : width);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'T':
|
case 'T':
|
||||||
if (format.total_style) {
|
if (total_style) {
|
||||||
balance_t value = format.compute_total(item);
|
balance_t value = compute_total(item);
|
||||||
value.write(out, width, strict_width > 0 ? strict_width : width);
|
value.write(out, width, strict_width > 0 ? strict_width : width);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
33
format.h
33
format.h
|
|
@ -1,9 +1,10 @@
|
||||||
#ifndef _REPORT_H
|
#ifndef _FORMAT_H
|
||||||
#define _REPORT_H
|
#define _FORMAT_H
|
||||||
|
|
||||||
#include "ledger.h"
|
#include "ledger.h"
|
||||||
#include "constraint.h"
|
|
||||||
#include "balance.h"
|
#include "balance.h"
|
||||||
|
#include "constraint.h"
|
||||||
|
#include "expr.h"
|
||||||
|
|
||||||
namespace ledger {
|
namespace ledger {
|
||||||
|
|
||||||
|
|
@ -12,8 +13,6 @@ std::string maximal_account_name(const item_t * item, const item_t * parent);
|
||||||
|
|
||||||
struct format_t
|
struct format_t
|
||||||
{
|
{
|
||||||
constraints_t constraints;
|
|
||||||
|
|
||||||
std::string format_string;
|
std::string format_string;
|
||||||
node_t * value_style;
|
node_t * value_style;
|
||||||
node_t * total_style;
|
node_t * total_style;
|
||||||
|
|
@ -28,22 +27,40 @@ struct format_t
|
||||||
if (total_style) delete total_style;
|
if (total_style) delete total_style;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if 1
|
||||||
balance_t compute_value(const item_t * item) const {
|
balance_t compute_value(const item_t * item) const {
|
||||||
if (value_style)
|
if (value_style)
|
||||||
return value_style->compute(begin(), end(), item);
|
return value_style->compute(item);
|
||||||
else
|
else
|
||||||
return balance_t();
|
return balance_t();
|
||||||
}
|
}
|
||||||
|
|
||||||
balance_t compute_total(const item_t * item) const {
|
balance_t compute_total(const item_t * item) const {
|
||||||
if (total_style)
|
if (total_style)
|
||||||
return total_style->compute(begin(), end(), item);
|
return total_style->compute(item);
|
||||||
|
else
|
||||||
|
return balance_t();
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
balance_t compute_value(const item_t * item,
|
||||||
|
const constraints_t& constraints) const {
|
||||||
|
if (value_style)
|
||||||
|
return value_style->compute(item, constraints.begin(), constraints.end());
|
||||||
else
|
else
|
||||||
return balance_t();
|
return balance_t();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
balance_t compute_total(const item_t * item,
|
||||||
|
const constraints_t& constraints) const {
|
||||||
|
if (total_style)
|
||||||
|
return total_style->compute(item, constraints.begin(), constraints.end());
|
||||||
|
else
|
||||||
|
return balance_t();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
std::string report_line(const item_t * item,
|
std::string report_line(const item_t * item,
|
||||||
const item_t * displayed_parent = NULL);
|
const item_t * displayed_parent = NULL) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ledger
|
} // namespace ledger
|
||||||
|
|
|
||||||
13
item.cc
13
item.cc
|
|
@ -1,4 +1,6 @@
|
||||||
#include "item.h"
|
#include "item.h"
|
||||||
|
#include "constraint.h"
|
||||||
|
#include "expr.h"
|
||||||
|
|
||||||
namespace ledger {
|
namespace ledger {
|
||||||
|
|
||||||
|
|
@ -11,7 +13,7 @@ item_t * walk_accounts(const account_t * account,
|
||||||
{
|
{
|
||||||
item_t * item = new item_t;
|
item_t * item = new item_t;
|
||||||
item->account = account;
|
item->account = account;
|
||||||
item->date = end_date(constraints);
|
item->date = constraints.end();
|
||||||
|
|
||||||
for (constrained_transactions_list_const_iterator
|
for (constrained_transactions_list_const_iterator
|
||||||
i(account->transactions.begin(),
|
i(account->transactions.begin(),
|
||||||
|
|
@ -85,8 +87,7 @@ item_t * walk_items(const item_t * top,
|
||||||
|
|
||||||
item_t * walk_entries(entries_list::const_iterator begin,
|
item_t * walk_entries(entries_list::const_iterator begin,
|
||||||
entries_list::const_iterator end,
|
entries_list::const_iterator end,
|
||||||
const constraints_t& constraints,
|
const constraints_t& constraints)
|
||||||
const format_t& format)
|
|
||||||
{
|
{
|
||||||
#if 0
|
#if 0
|
||||||
int last_mon = -1;
|
int last_mon = -1;
|
||||||
|
|
@ -113,7 +114,7 @@ item_t * walk_entries(entries_list::const_iterator begin,
|
||||||
item->payee = (*i)->payee;
|
item->payee = (*i)->payee;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! format.show_inverted) {
|
if (! constraints.show_inverted) {
|
||||||
item_t * subitem = new item_t;
|
item_t * subitem = new item_t;
|
||||||
subitem->parent = item;
|
subitem->parent = item;
|
||||||
subitem->date = item->date;
|
subitem->date = item->date;
|
||||||
|
|
@ -122,7 +123,7 @@ item_t * walk_entries(entries_list::const_iterator begin,
|
||||||
item->subitems.push_back(subitem);
|
item->subitems.push_back(subitem);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (format.show_related)
|
if (constraints.show_related)
|
||||||
for (transactions_list::iterator k = (*i)->transactions.begin();
|
for (transactions_list::iterator k = (*i)->transactions.begin();
|
||||||
k != (*i)->transactions.end();
|
k != (*i)->transactions.end();
|
||||||
k++)
|
k++)
|
||||||
|
|
@ -132,7 +133,7 @@ item_t * walk_entries(entries_list::const_iterator begin,
|
||||||
subitem->date = item->date;
|
subitem->date = item->date;
|
||||||
subitem->account = (*k)->account;
|
subitem->account = (*k)->account;
|
||||||
subitem->value = *(*k);
|
subitem->value = *(*k);
|
||||||
if (format.show_inverted)
|
if (constraints.show_inverted)
|
||||||
subitem->value.negate();
|
subitem->value.negate();
|
||||||
item->subitems.push_back(subitem);
|
item->subitems.push_back(subitem);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
7
item.h
7
item.h
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef _REPORT_H
|
#ifndef _ITEM_H
|
||||||
#define _REPORT_H
|
#define _ITEM_H
|
||||||
|
|
||||||
#include "ledger.h"
|
#include "ledger.h"
|
||||||
#include "balance.h"
|
#include "balance.h"
|
||||||
|
|
@ -49,8 +49,7 @@ item_t * walk_items(const item_t * top,
|
||||||
|
|
||||||
item_t * walk_entries(entries_list::const_iterator begin,
|
item_t * walk_entries(entries_list::const_iterator begin,
|
||||||
entries_list::const_iterator end,
|
entries_list::const_iterator end,
|
||||||
const constraints_t& constraints,
|
const constraints_t& constraints);
|
||||||
const format_t& format);
|
|
||||||
|
|
||||||
} // namespace report
|
} // namespace report
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
#include "ledger.h"
|
#include "ledger.h"
|
||||||
#include "report.h"
|
|
||||||
#include "textual.h"
|
#include "textual.h"
|
||||||
#include "binary.h"
|
#include "binary.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <deque>
|
|
||||||
|
|
||||||
namespace ledger {
|
namespace ledger {
|
||||||
|
|
||||||
|
|
|
||||||
188
main.cc
188
main.cc
|
|
@ -1,9 +1,11 @@
|
||||||
#include "ledger.h"
|
#include "ledger.h"
|
||||||
#include "constraint.h"
|
#include "balance.h"
|
||||||
#include "textual.h"
|
#include "textual.h"
|
||||||
#include "binary.h"
|
#include "binary.h"
|
||||||
#include "balance.h"
|
#include "constraint.h"
|
||||||
#include "report.h"
|
#include "item.h"
|
||||||
|
#include "expr.h"
|
||||||
|
#include "format.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
@ -20,23 +22,20 @@ namespace ledger {
|
||||||
|
|
||||||
static const std::string bal_fmt = "%20T%2_%-n\n";
|
static const std::string bal_fmt = "%20T%2_%-n\n";
|
||||||
|
|
||||||
void show_balances(std::ostream& out,
|
void show_balances(std::ostream& out,
|
||||||
report::items_deque& items,
|
items_deque& items,
|
||||||
const constraints_t& constraints,
|
const constraints_t& constraints,
|
||||||
const report::format_t& format,
|
const format_t& format,
|
||||||
const report::item_t * displayed_parent)
|
const item_t * displayed_parent)
|
||||||
{
|
{
|
||||||
if (format.sort_order)
|
for (items_deque::const_iterator i = items.begin();
|
||||||
std::sort(items.begin(), items.end(), report::cmp_items(format));
|
|
||||||
|
|
||||||
for (report::items_deque::const_iterator i = items.begin();
|
|
||||||
i != items.end();
|
i != items.end();
|
||||||
i++) {
|
i++) {
|
||||||
const report::item_t * parent = displayed_parent;
|
const item_t * parent = displayed_parent;
|
||||||
|
|
||||||
bool by_exclusion = false;
|
bool by_exclusion = false;
|
||||||
std::string name = maximal_account_name(*i, parent);
|
std::string name = maximal_account_name(*i, parent);
|
||||||
const bool match = (format.show_expanded ||
|
const bool match = (constraints.show_expanded ||
|
||||||
(! constraints.account_masks.empty() &&
|
(! constraints.account_masks.empty() &&
|
||||||
matches(constraints.account_masks, name,
|
matches(constraints.account_masks, name,
|
||||||
&by_exclusion) &&
|
&by_exclusion) &&
|
||||||
|
|
@ -45,27 +44,33 @@ void show_balances(std::ostream& out,
|
||||||
(constraints.account_masks.empty() &&
|
(constraints.account_masks.empty() &&
|
||||||
displayed_parent->parent == NULL));
|
displayed_parent->parent == NULL));
|
||||||
|
|
||||||
if (match && reportable(format, *i, true) &&
|
if (match && constraints(*i) &&
|
||||||
((*i)->subitems.size() != 1 ||
|
((*i)->subitems.size() != 1 ||
|
||||||
(*i)->total != (*i)->subitems[0]->total)) {
|
(*i)->total != (*i)->subitems[0]->total)) {
|
||||||
out << format_string(*i, format, parent);
|
out << format.report_line(*i, parent);
|
||||||
parent = *i;
|
parent = *i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (constraints.sort_order)
|
||||||
|
(*i)->sort(constraints.sort_order);
|
||||||
|
|
||||||
show_balances(out, (*i)->subitems, constraints, format, parent);
|
show_balances(out, (*i)->subitems, constraints, format, parent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void balance_report(std::ostream& out,
|
void balance_report(std::ostream& out,
|
||||||
report::item_t * top,
|
item_t * top,
|
||||||
const constraints_t& constraints,
|
const constraints_t& constraints,
|
||||||
const report::format_t& format)
|
const format_t& format)
|
||||||
{
|
{
|
||||||
|
if (constraints.sort_order)
|
||||||
|
top->sort(constraints.sort_order);
|
||||||
|
|
||||||
show_balances(out, top->subitems, constraints, format, top);
|
show_balances(out, top->subitems, constraints, format, top);
|
||||||
|
|
||||||
if (format.show_subtotals && top->subitems.size() > 1 && top->total)
|
if (constraints.show_subtotals && top->subitems.size() > 1 && top->total)
|
||||||
std::cout << "--------------------\n"
|
std::cout << "--------------------\n"
|
||||||
<< report::format_string(top, format);
|
<< format.report_line(top);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -75,7 +80,7 @@ void balance_report(std::ostream& out,
|
||||||
//
|
//
|
||||||
|
|
||||||
static const std::string reg_fmt
|
static const std::string reg_fmt
|
||||||
= "%?10d %?-.20p %/%-.22N %12.66t %12.80T\n";
|
= "%10d %-.20p %/%-.22N %12.66t %12.80T\n";
|
||||||
|
|
||||||
static bool show_commodities_revalued = false;
|
static bool show_commodities_revalued = false;
|
||||||
static bool show_commodities_revalued_only = false;
|
static bool show_commodities_revalued_only = false;
|
||||||
|
|
@ -84,7 +89,8 @@ static void report_value_change(std::ostream& out,
|
||||||
const std::time_t date,
|
const std::time_t date,
|
||||||
const balance_pair_t& balance,
|
const balance_pair_t& balance,
|
||||||
const balance_pair_t& prev_balance,
|
const balance_pair_t& prev_balance,
|
||||||
report::format_t& format,
|
const constraints_t& constraints,
|
||||||
|
const format_t& format,
|
||||||
const std::string& first_line_format,
|
const std::string& first_line_format,
|
||||||
const std::string& next_lines_format)
|
const std::string& next_lines_format)
|
||||||
{
|
{
|
||||||
|
|
@ -95,7 +101,7 @@ static void report_value_change(std::ostream& out,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
report::item_t temp;
|
item_t temp;
|
||||||
temp.date = prev_date;
|
temp.date = prev_date;
|
||||||
temp.total = prev_balance;
|
temp.total = prev_balance;
|
||||||
balance_t prev_bal = format.compute_total(&temp);
|
balance_t prev_bal = format.compute_total(&temp);
|
||||||
|
|
@ -109,26 +115,33 @@ static void report_value_change(std::ostream& out,
|
||||||
temp.total = balance;
|
temp.total = balance;
|
||||||
temp.payee = "Commodities revalued";
|
temp.payee = "Commodities revalued";
|
||||||
|
|
||||||
if (reportable(format, &temp)) {
|
if (constraints(&temp)) {
|
||||||
format.format_string = first_line_format;
|
format_t copy = format;
|
||||||
out << format_string(&temp, format, NULL);
|
|
||||||
|
|
||||||
format.format_string = next_lines_format;
|
copy.format_string = first_line_format;
|
||||||
out << format_string(&temp, format, NULL);
|
out << copy.report_line(&temp);
|
||||||
|
|
||||||
|
copy.format_string = next_lines_format;
|
||||||
|
out << copy.report_line(&temp);
|
||||||
|
|
||||||
|
// Prevent double-deletion
|
||||||
|
copy.value_style = NULL;
|
||||||
|
copy.total_style = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
prev_date = date;
|
prev_date = date;
|
||||||
}
|
}
|
||||||
|
|
||||||
void register_report(std::ostream& out, report::item_t * top,
|
void register_report(std::ostream& out,
|
||||||
const report::format_t& format)
|
item_t * top,
|
||||||
|
const constraints_t& constraints,
|
||||||
|
const format_t& format)
|
||||||
{
|
{
|
||||||
if (format.sort_order)
|
if (constraints.sort_order)
|
||||||
std::sort(top->subitems.begin(), top->subitems.end(),
|
top->sort(constraints.sort_order);
|
||||||
report::cmp_items(format));
|
|
||||||
|
|
||||||
report::format_t copy = format;
|
format_t copy = format;
|
||||||
|
|
||||||
std::string first_line_format;
|
std::string first_line_format;
|
||||||
std::string next_lines_format;
|
std::string next_lines_format;
|
||||||
|
|
@ -146,53 +159,55 @@ void register_report(std::ostream& out, report::item_t * top,
|
||||||
balance_pair_t last_reported;
|
balance_pair_t last_reported;
|
||||||
account_t splits(NULL, "<Total>");
|
account_t splits(NULL, "<Total>");
|
||||||
|
|
||||||
for (report::items_deque::const_iterator i = top->subitems.begin();
|
for (items_deque::const_iterator i = top->subitems.begin();
|
||||||
i != top->subitems.end();
|
i != top->subitems.end();
|
||||||
i++) {
|
i++) {
|
||||||
copy.format_string = first_line_format;
|
copy.format_string = first_line_format;
|
||||||
|
|
||||||
std::string header = format_string(*i, copy, top);
|
std::string header = copy.report_line(*i, top);
|
||||||
unsigned int header_len = header.length();
|
unsigned int header_len = header.length();
|
||||||
|
|
||||||
copy.format_string = next_lines_format;
|
copy.format_string = next_lines_format;
|
||||||
|
|
||||||
bool first = true;
|
bool first = true;
|
||||||
|
|
||||||
if ((*i)->subitems.size() > 1 && ! format.show_expanded) {
|
if ((*i)->subitems.size() > 1 && ! constraints.show_expanded) {
|
||||||
report::item_t summary;
|
item_t summary;
|
||||||
summary.date = (*i)->date;
|
summary.date = (*i)->date;
|
||||||
summary.parent = *i;
|
summary.parent = *i;
|
||||||
summary.account = &splits;
|
summary.account = &splits;
|
||||||
|
|
||||||
for (report::items_deque::const_iterator j = (*i)->subitems.begin();
|
for (items_deque::const_iterator j = (*i)->subitems.begin();
|
||||||
j != (*i)->subitems.end();
|
j != (*i)->subitems.end();
|
||||||
j++)
|
j++)
|
||||||
summary.value += (*j)->value;
|
summary.value += (*j)->value;
|
||||||
summary.total = balance + summary.value;
|
summary.total = balance + summary.value;
|
||||||
|
|
||||||
bool show = reportable(format, &summary);
|
bool show = constraints(&summary);
|
||||||
if (show && show_commodities_revalued)
|
if (show && show_commodities_revalued)
|
||||||
report_value_change(out, summary.date, balance, last_reported, copy,
|
report_value_change(out, summary.date, balance, last_reported,
|
||||||
first_line_format, next_lines_format);
|
constraints, copy, first_line_format,
|
||||||
|
next_lines_format);
|
||||||
|
|
||||||
balance += summary.value;
|
balance += summary.value;
|
||||||
|
|
||||||
if (show) {
|
if (show) {
|
||||||
if (! show_commodities_revalued_only)
|
if (! show_commodities_revalued_only)
|
||||||
out << header << format_string(&summary, copy, *i);
|
out << header << copy.report_line(&summary, *i);
|
||||||
if (show_commodities_revalued)
|
if (show_commodities_revalued)
|
||||||
last_reported = balance;
|
last_reported = balance;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (report::items_deque::const_iterator j = (*i)->subitems.begin();
|
for (items_deque::const_iterator j = (*i)->subitems.begin();
|
||||||
j != (*i)->subitems.end();
|
j != (*i)->subitems.end();
|
||||||
j++) {
|
j++) {
|
||||||
(*j)->total = balance + (*j)->value;
|
(*j)->total = balance + (*j)->value;
|
||||||
|
|
||||||
bool show = reportable(format, *j);
|
bool show = constraints(*j);
|
||||||
if (show && first && show_commodities_revalued) {
|
if (show && first && show_commodities_revalued) {
|
||||||
report_value_change(out, (*i)->date, balance, last_reported, copy,
|
report_value_change(out, (*i)->date, balance, last_reported,
|
||||||
first_line_format, next_lines_format);
|
constraints, copy, first_line_format,
|
||||||
|
next_lines_format);
|
||||||
if (show_commodities_revalued_only)
|
if (show_commodities_revalued_only)
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
|
|
@ -208,7 +223,7 @@ void register_report(std::ostream& out, report::item_t * top,
|
||||||
out.width(header_len);
|
out.width(header_len);
|
||||||
out << " ";
|
out << " ";
|
||||||
}
|
}
|
||||||
out << format_string(*j, copy, *i);
|
out << copy.report_line(*j, *i);
|
||||||
}
|
}
|
||||||
if (show_commodities_revalued)
|
if (show_commodities_revalued)
|
||||||
last_reported = balance;
|
last_reported = balance;
|
||||||
|
|
@ -218,12 +233,11 @@ void register_report(std::ostream& out, report::item_t * top,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (show_commodities_revalued)
|
if (show_commodities_revalued)
|
||||||
report_value_change(out, copy.end(), balance, last_reported, copy,
|
report_value_change(out, constraints.end(), balance, last_reported,
|
||||||
first_line_format, next_lines_format);
|
constraints, copy, first_line_format,
|
||||||
|
next_lines_format);
|
||||||
|
|
||||||
// To stop these from getting deleted when copy goes out of scope
|
// To stop these from getting deleted when copy goes out of scope
|
||||||
copy.predicate = NULL;
|
|
||||||
copy.sort_order = NULL;
|
|
||||||
copy.value_style = NULL;
|
copy.value_style = NULL;
|
||||||
copy.total_style = NULL;
|
copy.total_style = NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -493,7 +507,7 @@ int main(int argc, char * argv[])
|
||||||
std::list<std::string> files;
|
std::list<std::string> files;
|
||||||
ledger::ledger_t * book = NULL;
|
ledger::ledger_t * book = NULL;
|
||||||
ledger::constraints_t constraints;
|
ledger::constraints_t constraints;
|
||||||
ledger::report::format_t format;
|
ledger::format_t format;
|
||||||
|
|
||||||
std::string sort_order;
|
std::string sort_order;
|
||||||
std::string value_style = "a";
|
std::string value_style = "a";
|
||||||
|
|
@ -623,19 +637,19 @@ int main(int argc, char * argv[])
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'M':
|
case 'M':
|
||||||
format.period = ledger::report::PERIOD_MONTHLY;
|
constraints.period = ledger::PERIOD_MONTHLY;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'E':
|
case 'E':
|
||||||
format.show_empty = true;
|
constraints.show_empty = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'n':
|
case 'n':
|
||||||
format.show_subtotals = false;
|
constraints.show_subtotals = false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 's':
|
case 's':
|
||||||
format.show_expanded = true;
|
constraints.show_expanded = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'S':
|
case 'S':
|
||||||
|
|
@ -643,11 +657,11 @@ int main(int argc, char * argv[])
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'o':
|
case 'o':
|
||||||
format.show_related = true;
|
constraints.show_related = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'l':
|
case 'l':
|
||||||
format.predicate = ledger::report::parse_expr(optarg, book);
|
constraints.predicate = ledger::parse_expr(optarg, book);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Commodity reporting
|
// Commodity reporting
|
||||||
|
|
@ -791,20 +805,18 @@ int main(int argc, char * argv[])
|
||||||
// Copy the constraints to the format object, and compile the value
|
// Copy the constraints to the format object, and compile the value
|
||||||
// and total style strings
|
// and total style strings
|
||||||
|
|
||||||
format.constraints = constraints;
|
|
||||||
if (! sort_order.empty())
|
if (! sort_order.empty())
|
||||||
format.sort_order = ledger::report::parse_expr(sort_order, book);
|
constraints.sort_order = ledger::parse_expr(sort_order, book);
|
||||||
format.value_style = ledger::report::parse_expr(value_style, book);
|
format.value_style = ledger::parse_expr(value_style, book);
|
||||||
format.total_style = ledger::report::parse_expr(total_style, book);
|
format.total_style = ledger::parse_expr(total_style, book);
|
||||||
|
|
||||||
// Now handle the command that was identified above.
|
// Now handle the command that was identified above.
|
||||||
|
|
||||||
if (command == "print") {
|
if (command == "print") {
|
||||||
#if 0
|
#if 0
|
||||||
ledger::report::item_t * top
|
ledger::item_t * top
|
||||||
= ledger::report::walk_entries(book->entries.begin(),
|
= ledger::walk_entries(book->entries.begin(), book->entries.end(),
|
||||||
book->entries.end(),
|
constraints, format);
|
||||||
constraints, format);
|
|
||||||
ledger::entry_report(std::cout, top, format);
|
ledger::entry_report(std::cout, top, format);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
delete top;
|
delete top;
|
||||||
|
|
@ -813,9 +825,9 @@ int main(int argc, char * argv[])
|
||||||
}
|
}
|
||||||
else if (command == "equity") {
|
else if (command == "equity") {
|
||||||
#if 0
|
#if 0
|
||||||
ledger::report::item_t * top
|
ledger::item_t * top
|
||||||
= ledger::report::walk_accounts(book->master, constraints,
|
= ledger::walk_accounts(book->master, constraints,
|
||||||
format.show_subtotals);
|
constraints.show_subtotals);
|
||||||
|
|
||||||
ledger::entry_report(std::cout, top, constraints, format);
|
ledger::entry_report(std::cout, top, constraints, format);
|
||||||
|
|
||||||
|
|
@ -824,15 +836,15 @@ int main(int argc, char * argv[])
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
else if (format.period == ledger::report::PERIOD_NONE &&
|
else if (constraints.period == ledger::PERIOD_NONE &&
|
||||||
! format.sort_order && ! format.show_related &&
|
! constraints.sort_order && ! constraints.show_related &&
|
||||||
(command == "balance" || command == "bal")) {
|
(command == "balance" || command == "bal")) {
|
||||||
if (format.format_string.empty())
|
if (format.format_string.empty())
|
||||||
format.format_string = ledger::bal_fmt;
|
format.format_string = ledger::bal_fmt;
|
||||||
|
|
||||||
if (ledger::report::item_t * top
|
if (ledger::item_t * top
|
||||||
= ledger::report::walk_accounts(book->master, constraints,
|
= ledger::walk_accounts(book->master, constraints,
|
||||||
format.show_subtotals)) {
|
constraints.show_subtotals)) {
|
||||||
ledger::balance_report(std::cout, top, constraints, format);
|
ledger::balance_report(std::cout, top, constraints, format);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
delete top;
|
delete top;
|
||||||
|
|
@ -843,13 +855,12 @@ int main(int argc, char * argv[])
|
||||||
if (format.format_string.empty())
|
if (format.format_string.empty())
|
||||||
format.format_string = ledger::bal_fmt;
|
format.format_string = ledger::bal_fmt;
|
||||||
|
|
||||||
if (ledger::report::item_t * list
|
if (ledger::item_t * list
|
||||||
= ledger::report::walk_entries(book->entries.begin(),
|
= ledger::walk_entries(book->entries.begin(),
|
||||||
book->entries.end(),
|
book->entries.end(), constraints))
|
||||||
constraints, format))
|
if (ledger::item_t * top
|
||||||
if (ledger::report::item_t * top
|
= ledger::walk_items(list, book->master, constraints,
|
||||||
= ledger::report::walk_items(list, book->master, constraints,
|
constraints.show_subtotals)) {
|
||||||
format.show_subtotals)) {
|
|
||||||
ledger::balance_report(std::cout, top, constraints, format);
|
ledger::balance_report(std::cout, top, constraints, format);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
delete top;
|
delete top;
|
||||||
|
|
@ -861,14 +872,13 @@ int main(int argc, char * argv[])
|
||||||
if (format.format_string.empty())
|
if (format.format_string.empty())
|
||||||
format.format_string = ledger::reg_fmt;
|
format.format_string = ledger::reg_fmt;
|
||||||
|
|
||||||
if (format.show_related)
|
if (constraints.show_related)
|
||||||
format.show_inverted = true;
|
constraints.show_inverted = true;
|
||||||
|
|
||||||
if (ledger::report::item_t * top
|
if (ledger::item_t * top
|
||||||
= ledger::report::walk_entries(book->entries.begin(),
|
= ledger::walk_entries(book->entries.begin(),
|
||||||
book->entries.end(),
|
book->entries.end(), constraints)) {
|
||||||
constraints, format)) {
|
ledger::register_report(std::cout, top, constraints, format);
|
||||||
ledger::register_report(std::cout, top, format);
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
delete top;
|
delete top;
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue