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; return false;
#if 0 #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()) { if (! account_masks.empty()) {
bool match = false; bool match = false;

35
item.cc
View file

@ -8,8 +8,7 @@ namespace ledger {
// subaccounts, empty balanced or no // subaccounts, empty balanced or no
item_t * walk_accounts(const account_t * account, item_t * walk_accounts(const account_t * account,
const constraints_t& constraints, const constraints_t& constraints)
const bool compute_subtotals)
{ {
item_t * item = new item_t; item_t * item = new item_t;
item->account = account; item->account = account;
@ -21,21 +20,20 @@ item_t * walk_accounts(const account_t * account,
i != account->transactions.end(); i != account->transactions.end();
i++) { i++) {
item->value += *(*i); item->value += *(*i);
if (compute_subtotals) if (constraints.show_subtotals)
item->total += *(*i); item->total += *(*i);
} }
for (accounts_map::const_iterator i = account->accounts.begin(); for (accounts_map::const_iterator i = account->accounts.begin();
i != account->accounts.end(); i != account->accounts.end();
i++) { i++) {
item_t * subitem = walk_accounts((*i).second, constraints, item_t * subitem = walk_accounts((*i).second, constraints);
compute_subtotals);
subitem->parent = item; subitem->parent = item;
if (compute_subtotals) if (constraints.show_subtotals)
item->total += subitem->total; item->total += subitem->total;
if (compute_subtotals ? subitem->total : subitem->value) if (constraints.show_subtotals ? subitem->total : subitem->value)
item->subitems.push_back(subitem); 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, static inline void sum_items(const item_t * top,
item_t * item, const constraints_t& constraints,
const bool compute_subtotals) item_t * item)
{ {
if (top->account == item->account) { if (top->account == item->account) {
item->value += top->value; item->value += top->value;
if (compute_subtotals) if (constraints.show_subtotals)
item->total += top->value; item->total += top->value;
} }
for (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++)
sum_items(*i, item, compute_subtotals); sum_items(*i, constraints, item);
} }
item_t * walk_items(const item_t * top, item_t * walk_items(const item_t * top, const account_t * account,
const account_t * account, const constraints_t& constraints)
const constraints_t& constraints,
const bool compute_subtotals)
{ {
item_t * item = new item_t; item_t * item = new item_t;
item->account = account; item->account = account;
sum_items(top, item, compute_subtotals); sum_items(top, constraints, item);
for (accounts_map::const_iterator i = account->accounts.begin(); for (accounts_map::const_iterator i = account->accounts.begin();
i != account->accounts.end(); i != account->accounts.end();
i++) { i++) {
item_t * subitem = walk_items(top, (*i).second, constraints, item_t * subitem = walk_items(top, (*i).second, constraints);
compute_subtotals);
subitem->parent = item; subitem->parent = item;
if (compute_subtotals) if (constraints.show_subtotals)
item->total += subitem->total; item->total += subitem->total;
if (compute_subtotals ? subitem->total : subitem->value) if (constraints.show_subtotals ? subitem->total : subitem->value)
item->subitems.push_back(subitem); 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
{ {
struct item_t * parent; struct item_t * parent;
items_deque subitems;
unsigned int index; unsigned int index;
std::time_t date; std::time_t date;
std::string payee; std::string payee;
@ -22,8 +24,6 @@ struct item_t
balance_pair_t value; balance_pair_t value;
balance_pair_t total; balance_pair_t total;
items_deque subitems;
item_t() : parent(NULL), index(0), date(-1), account(NULL) {} item_t() : parent(NULL), index(0), date(-1), account(NULL) {}
~item_t() { ~item_t() {
@ -39,13 +39,10 @@ struct item_t
class constraints_t; class constraints_t;
item_t * walk_accounts(const account_t * account, item_t * walk_accounts(const account_t * account,
const constraints_t& constraints, const constraints_t& constraints);
const bool compute_subtotals);
item_t * walk_items(const item_t * top, item_t * walk_items(const item_t * top, const account_t * account,
const account_t * account, const constraints_t& constraints);
const constraints_t& constraints,
const bool compute_subtotals);
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,

View file

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

View file

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