summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2003-09-30 20:18:52 +0000
committerJohn Wiegley <johnw@newartisans.com>2003-09-30 20:18:52 +0000
commit487ea6a2171ec70b8ee0ff147a44abb6d89679dc (patch)
treea13fbc20b475c75fafe345b0bb29dd266c866b35
parentf6c2e3d51eb8cd0892e5beeae59b74f12d14c027 (diff)
downloadfork-ledger-487ea6a2171ec70b8ee0ff147a44abb6d89679dc.tar.gz
fork-ledger-487ea6a2171ec70b8ee0ff147a44abb6d89679dc.tar.bz2
fork-ledger-487ea6a2171ec70b8ee0ff147a44abb6d89679dc.zip
*** empty log message ***
-rw-r--r--balance.cc65
-rw-r--r--gnucash.cc1
-rw-r--r--ledger.h15
-rw-r--r--parse.cc7
4 files changed, 62 insertions, 26 deletions
diff --git a/balance.cc b/balance.cc
index 6818bf7d..7867f0ab 100644
--- a/balance.cc
+++ b/balance.cc
@@ -5,12 +5,16 @@
namespace ledger {
-static bool show_current = false;
static bool show_cleared = false;
static bool show_children = false;
static bool show_empty = false;
static bool no_subtotals = false;
+static std::time_t begin_date;
+static bool have_beginning;
+static std::time_t end_date;
+static bool have_ending;
+
static void display_total(std::ostream& out, totals& total_balance,
const account * acct,
const std::map<account *, totals *>& balances,
@@ -27,13 +31,13 @@ static void display_total(std::ostream& out, totals& total_balance,
if (show_children) {
match = false;
for (const account * a = acct; a; a = a->parent) {
- if (matches(regexps, a->name)) {
+ if (matches(regexps, a->as_str())) {
match = true;
break;
}
}
} else {
- match = matches(regexps, acct->name);
+ match = matches(regexps, acct->as_str());
}
}
@@ -45,7 +49,7 @@ static void display_total(std::ostream& out, totals& total_balance,
if (! show_children || ! acct->parent)
total_balance.credit(*balance);
- if (acct->parent) {
+ if (acct->parent && ! no_subtotals) {
for (const account * a = acct; a; a = a->parent)
out << " ";
out << acct->name << std::endl;
@@ -97,11 +101,40 @@ void report_balances(int argc, char **argv, std::ostream& out)
}
#endif
+ have_beginning = false;
+ have_ending = false;
+
int c;
optind = 1;
- while (-1 != (c = getopt(argc, argv, "cCsSni:p:G:"))) {
+ while (-1 != (c = getopt(argc, argv, "b:e:cCsSni:p:G:"))) {
switch (char(c)) {
- case 'c': show_current = true; break;
+ case 'b': {
+ struct tm * when = getdate(optarg);
+ if (! when) {
+ std::cerr << "Error: Bad begin date string: " << optarg
+ << std::endl;
+ } else {
+ begin_date = std::mktime(when);
+ have_beginning = true;
+ }
+ break;
+ }
+ case 'e': {
+ struct tm * when = getdate(optarg);
+ if (! when) {
+ std::cerr << "Error: Bad end date string: " << optarg
+ << std::endl;
+ } else {
+ end_date = std::mktime(when);
+ have_ending = true;
+ }
+ break;
+ }
+ case 'c':
+ end_date = std::time(NULL);
+ have_ending = true;
+ break;
+
case 'C': show_cleared = true; break;
case 's': show_children = true; break;
case 'S': show_empty = true; break;
@@ -152,7 +185,6 @@ void report_balances(int argc, char **argv, std::ostream& out)
// totals
std::map<account *, totals *> balances;
- std::time_t now = std::time(NULL);
for (ledger_iterator i = ledger.begin(); i != ledger.end(); i++) {
for (std::list<transaction *>::iterator x = (*i)->xacts.begin();
@@ -174,19 +206,14 @@ void report_balances(int argc, char **argv, std::ostream& out)
balance = (*t).second;
}
- bool do_credit = false;
+ bool do_credit = true;
- if (show_current) {
- if (difftime((*i)->date, now) < 0)
- do_credit = true;
- }
- else if (show_cleared) {
- if ((*i)->cleared)
- do_credit = true;
- }
- else {
- do_credit = true;
- }
+ if (have_beginning && difftime((*i)->date, begin_date) < 0)
+ do_credit = false;
+ else if (have_ending && difftime((*i)->date, end_date) > 0)
+ do_credit = false;
+ else if (show_cleared && ! (*i)->cleared)
+ do_credit = false;
if (! do_credit)
continue;
diff --git a/gnucash.cc b/gnucash.cc
index fb768cd8..19b870b9 100644
--- a/gnucash.cc
+++ b/gnucash.cc
@@ -203,6 +203,7 @@ static void dataHandler(void *userData, const char *s, int len)
std::string value = curr_quant + " " + (*i).second->comm->symbol;
xact->cost = create_amount(value.c_str(), curr_value);
+ xact->acct->balance.credit(xact->cost);
break;
}
diff --git a/ledger.h b/ledger.h
index d189d526..ec82f54a 100644
--- a/ledger.h
+++ b/ledger.h
@@ -1,5 +1,5 @@
#ifndef _LEDGER_H
-#define _LEDGER_H "$Revision: 1.6 $"
+#define _LEDGER_H "$Revision: 1.7 $"
//////////////////////////////////////////////////////////////////////
//
@@ -245,10 +245,11 @@ operator<<(std::basic_ostream<char, Traits>& out, const totals& t) {
struct account
{
+ struct account * parent;
+
std::string name;
commodity * comm; // default commodity for this account
-
- struct account * parent;
+ totals balance;
typedef std::map<const std::string, struct account *> map;
typedef map::iterator iterator;
@@ -258,20 +259,20 @@ struct account
map children;
account(const std::string& _name, struct account * _parent = NULL)
- : name(_name), parent(_parent) {}
+ : parent(_parent), name(_name) {}
- operator std::string() const {
+ const std::string as_str() const {
if (! parent)
return name;
else
- return std::string(*parent) + ":" + name;
+ return parent->as_str() + ":" + name;
}
};
template<class Traits>
std::basic_ostream<char, Traits> &
operator<<(std::basic_ostream<char, Traits>& out, const account& a) {
- return (out << std::string(a));
+ return (out << a.as_str());
}
diff --git a/parse.cc b/parse.cc
index cfca3b5f..3a6430bb 100644
--- a/parse.cc
+++ b/parse.cc
@@ -10,6 +10,11 @@ static char * next_element(char * buf, bool variable = false)
{
char * p;
+ // Convert any tabs to spaces, for simplicity's sake
+ for (p = buf; *p; p++)
+ if (*p == '\t')
+ *p = ' ';
+
if (variable)
p = std::strstr(buf, " ");
else
@@ -226,6 +231,8 @@ bool parse_ledger(std::istream& in)
#endif
xact->acct = find_account(p);
+ xact->acct->balance.credit(xact->cost);
+
curr->xacts.push_back(xact);
#ifdef HUQUQULLAH