further reorganization

This commit is contained in:
John Wiegley 2004-07-27 02:23:02 -04:00
parent dd5680c267
commit d7dd02276c
5 changed files with 230 additions and 242 deletions

View file

@ -200,6 +200,10 @@ bool constraints_t::operator ()(const item_t * item) const
return false;
#if 0
// jww (2004-07-26): It shouldn't be necessary to check against the
// account here, since this is always done during initial compiling
// of the item_t tree.
if (! account_masks.empty()) {
bool match = false;

35
item.cc
View file

@ -8,8 +8,7 @@ namespace ledger {
// subaccounts, empty balanced or no
item_t * walk_accounts(const account_t * account,
const constraints_t& constraints,
const bool compute_subtotals)
const constraints_t& constraints)
{
item_t * item = new item_t;
item->account = account;
@ -21,21 +20,20 @@ item_t * walk_accounts(const account_t * account,
i != account->transactions.end();
i++) {
item->value += *(*i);
if (compute_subtotals)
if (constraints.show_subtotals)
item->total += *(*i);
}
for (accounts_map::const_iterator i = account->accounts.begin();
i != account->accounts.end();
i++) {
item_t * subitem = walk_accounts((*i).second, constraints,
compute_subtotals);
item_t * subitem = walk_accounts((*i).second, constraints);
subitem->parent = item;
if (compute_subtotals)
if (constraints.show_subtotals)
item->total += subitem->total;
if (compute_subtotals ? subitem->total : subitem->value)
if (constraints.show_subtotals ? subitem->total : subitem->value)
item->subitems.push_back(subitem);
}
@ -43,42 +41,39 @@ item_t * walk_accounts(const account_t * account,
}
static inline void sum_items(const item_t * top,
item_t * item,
const bool compute_subtotals)
const constraints_t& constraints,
item_t * item)
{
if (top->account == item->account) {
item->value += top->value;
if (compute_subtotals)
if (constraints.show_subtotals)
item->total += top->value;
}
for (items_deque::const_iterator i = top->subitems.begin();
i != top->subitems.end();
i++)
sum_items(*i, item, compute_subtotals);
sum_items(*i, constraints, item);
}
item_t * walk_items(const item_t * top,
const account_t * account,
const constraints_t& constraints,
const bool compute_subtotals)
item_t * walk_items(const item_t * top, const account_t * account,
const constraints_t& constraints)
{
item_t * item = new item_t;
item->account = account;
sum_items(top, item, compute_subtotals);
sum_items(top, constraints, item);
for (accounts_map::const_iterator i = account->accounts.begin();
i != account->accounts.end();
i++) {
item_t * subitem = walk_items(top, (*i).second, constraints,
compute_subtotals);
item_t * subitem = walk_items(top, (*i).second, constraints);
subitem->parent = item;
if (compute_subtotals)
if (constraints.show_subtotals)
item->total += subitem->total;
if (compute_subtotals ? subitem->total : subitem->value)
if (constraints.show_subtotals ? subitem->total : subitem->value)
item->subitems.push_back(subitem);
}

13
item.h
View file

@ -15,6 +15,8 @@ typedef std::deque<item_t *> items_deque;
struct item_t
{
struct item_t * parent;
items_deque subitems;
unsigned int index;
std::time_t date;
std::string payee;
@ -22,8 +24,6 @@ struct item_t
balance_pair_t value;
balance_pair_t total;
items_deque subitems;
item_t() : parent(NULL), index(0), date(-1), account(NULL) {}
~item_t() {
@ -39,13 +39,10 @@ struct item_t
class constraints_t;
item_t * walk_accounts(const account_t * account,
const constraints_t& constraints,
const bool compute_subtotals);
const constraints_t& constraints);
item_t * walk_items(const item_t * top,
const account_t * account,
const constraints_t& constraints,
const bool compute_subtotals);
item_t * walk_items(const item_t * top, const account_t * account,
const constraints_t& constraints);
item_t * walk_entries(entries_list::const_iterator begin,
entries_list::const_iterator end,

View file

@ -826,8 +826,7 @@ int main(int argc, char * argv[])
else if (command == "equity") {
#if 0
ledger::item_t * top
= ledger::walk_accounts(book->master, constraints,
constraints.show_subtotals);
= ledger::walk_accounts(book->master, constraints);
ledger::entry_report(std::cout, top, constraints, format);
@ -843,8 +842,7 @@ int main(int argc, char * argv[])
format.format_string = ledger::bal_fmt;
if (ledger::item_t * top
= ledger::walk_accounts(book->master, constraints,
constraints.show_subtotals)) {
= ledger::walk_accounts(book->master, constraints)) {
ledger::balance_report(std::cout, top, constraints, format);
#ifdef DEBUG
delete top;
@ -859,8 +857,7 @@ int main(int argc, char * argv[])
= ledger::walk_entries(book->entries.begin(),
book->entries.end(), constraints))
if (ledger::item_t * top
= ledger::walk_items(list, book->master, constraints,
constraints.show_subtotals)) {
= ledger::walk_items(list, book->master, constraints)) {
ledger::balance_report(std::cout, top, constraints, format);
#ifdef DEBUG
delete top;

View file

@ -1,6 +1,6 @@
#include "ledger.h"
#include "constraint.h"
#include "textual.h"
#include "constraint.h"
#include "error.h"
#include <vector>
#include <fstream>
@ -14,7 +14,7 @@
namespace ledger {
#if 0
static const std::string entry1_fmt = "%?10d %p";
static const std::string entry1_fmt = "%10d %p";
static const std::string entryn_fmt = " %-30a %15t";
#endif
@ -360,15 +360,13 @@ void parse_automated_transactions(std::istream& in, ledger_t * ledger,
while (! in.eof() && (in.peek() == ' ' || in.peek() == '\t')) {
if (transaction_t * xact = parse_transaction(in, ledger, account, NULL)) {
if (! xact->amount) {
std::cerr << "Error in " << path << ", line " << (linenum - 1)
<< ": All automated transactions must have a value."
<< std::endl;
} else {
if (! xact->amount)
throw parse_error(path, linenum,
"All automated transactions must have a value");
else
xacts.push_back(xact);
}
}
}
if (! masks.empty() && ! xacts.empty()) {
automated_transaction_t * auto_xact
@ -461,11 +459,8 @@ entry_t * parse_entry(std::istream& in, ledger_t * ledger,
char * next = next_element(line);
if (! quick_parse_date(line, &curr->date)) {
std::cerr << "Error in " << path << ", line " << (linenum - 1)
<< ": Failed to parse date: " << line << std::endl;
return NULL;
}
if (! quick_parse_date(line, &curr->date))
throw parse_error(path, linenum, "Failed to parse date");
// Parse the optional cleared flag: *
@ -532,6 +527,7 @@ unsigned int parse_textual_ledger(std::istream& in, ledger_t *& ledger,
linenum = 1;
while (! in.eof()) {
try {
switch (in.peek()) {
case -1: // end of file
goto done;
@ -539,12 +535,10 @@ unsigned int parse_textual_ledger(std::istream& in, ledger_t *& ledger,
case ' ':
case '\t':
if (peek_next_nonws(in) != '\n') {
std::cerr << "Error in " << path << ", line " << (linenum - 1)
<< ": Ignoring entry beginning with whitespace."
<< std::endl;
in.getline(line, MAX_LINE);
linenum++;
break;
throw parse_error(path, linenum,
"Ignoring entry beginning with whitespace");
}
// fall through...
@ -577,9 +571,8 @@ unsigned int parse_textual_ledger(std::istream& in, ledger_t *& ledger,
time_in = std::mktime(&when);
last_account = account_stack.front()->find_account(p);
} else {
std::cerr << "Error in " << path << ", line " << (linenum - 1)
<< ": Cannot parse timelog entry date." << std::endl;
last_account = NULL;
throw parse_error(path, linenum, "Cannot parse timelog entry date");
}
break;
}
@ -623,8 +616,7 @@ unsigned int parse_textual_ledger(std::istream& in, ledger_t *& ledger,
count++;
} else {
std::cerr << "Error in " << path << ", line " << (linenum - 1)
<< ": Cannot parse timelog entry date." << std::endl;
throw parse_error(path, linenum, "Cannot parse timelog entry date");
}
last_account = NULL;
@ -642,11 +634,8 @@ unsigned int parse_textual_ledger(std::istream& in, ledger_t *& ledger,
std::string symbol;
in >> line; // the date
if (! quick_parse_date(line, &date)) {
std::cerr << "Error in " << path << ", line " << (linenum - 1)
<< ": Failed to parse date: " << line << std::endl;
break;
}
if (! quick_parse_date(line, &date))
throw parse_error(path, linenum, "Failed to parse date");
int hour, min, sec;
@ -739,14 +728,18 @@ unsigned int parse_textual_ledger(std::istream& in, ledger_t *& ledger,
in.getline(line, MAX_LINE);
linenum++;
char * path = skip_ws(line);
std::ifstream stream(path);
char * p = skip_ws(line);
std::ifstream stream(p);
ledger->sources.push_back(path);
ledger->sources.push_back(p);
unsigned int curr_linenum = linenum;
std::string curr_path = path;
count += parse_textual_ledger(stream, ledger, account_stack.front());
linenum = curr_linenum;
path = curr_path;
}
break;
@ -759,16 +752,18 @@ unsigned int parse_textual_ledger(std::istream& in, ledger_t *& ledger,
if (ledger->add_entry(entry))
count++;
else
std::cerr << "Error in " << path << ", line " << first_line
<< ": Entry does not balance." << std::endl;
throw parse_error(path, first_line, "Entry does not balance");
} else {
std::cerr << "Error in " << path << ", line " << first_line
<< ": Failed to parse entry." << std::endl;
throw parse_error(path, first_line, "Failed to parse entry");
}
break;
}
}
}
catch (const parse_error& err) {
std::cerr << err.what() << std::endl;
}
}
done:
if (time_commodity) {