summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.cc2
-rw-r--r--gnucash.cc28
-rw-r--r--journal.cc107
-rw-r--r--main.cc7
-rw-r--r--main.py3
-rw-r--r--textual.cc7
-rw-r--r--valexpr.cc26
7 files changed, 93 insertions, 87 deletions
diff --git a/config.cc b/config.cc
index 0dff2a47..8e86c5a2 100644
--- a/config.cc
+++ b/config.cc
@@ -392,7 +392,7 @@ OPT_BEGIN(set_price, "z:") {
if (std::strchr(optarg, '='))
config.price_settings.push_back(optarg);
else
- std::cerr << "Error: Invalid price setting: " << optarg << std::endl;
+ throw error(std::string("Invalid price setting: ") + optarg);
} OPT_END(set_price);
OPT_BEGIN(account, "a:") {
diff --git a/gnucash.cc b/gnucash.cc
index 424ea1ae..a0dc263c 100644
--- a/gnucash.cc
+++ b/gnucash.cc
@@ -1,5 +1,6 @@
#include "gnucash.h"
#include "ledger.h"
+#include "error.h"
#include <iostream>
#include <sstream>
@@ -218,21 +219,17 @@ static void dataHandler(void *userData, const char *s, int len)
case XACT_ACCOUNT: {
accounts_map::iterator i = accounts_by_id.find(std::string(s, len));
- if (i == accounts_by_id.end()) {
- std::cerr << "Could not find account " << std::string(s, len)
- << std::endl;
- std::exit(1);
- }
+ if (i == accounts_by_id.end())
+ throw error(std::string("Could not find account ") + std::string(s, len));
transaction_t * xact = curr_entry->transactions.back();
xact->account = (*i).second;
account_comm_map::iterator ac = account_comms.find(xact->account);
- if (ac == account_comms.end()) {
- std::cerr << "Could not find account " << *(xact->account)
- << std::endl;
- std::exit(1);
- }
+ if (ac == account_comms.end())
+ throw error(std::string("Could not find account ") +
+ std::string(*(xact->account)));
+
commodity_t * default_commodity = (*ac).second;
curr_quant.set_commodity(*default_commodity);
@@ -300,13 +297,10 @@ unsigned int gnucash_parser_t::parse(std::istream& in,
while (! in.eof()) {
in.getline(buf, BUFSIZ - 1);
-
- if (! XML_Parse(parser, buf, std::strlen(buf), in.eof())) {
- std::cerr << XML_ErrorString(XML_GetErrorCode(parser))
- << " at line " << XML_GetCurrentLineNumber(parser)
- << std::endl;
- return NULL;
- }
+ if (! XML_Parse(parser, buf, std::strlen(buf), in.eof()))
+ throw parse_error(original_file ? *original_file : "<gnucash>",
+ XML_GetCurrentLineNumber(parser),
+ XML_ErrorString(XML_GetErrorCode(parser)));
}
XML_ParserFree(parser);
diff --git a/journal.cc b/journal.cc
index 2ef19555..3806e411 100644
--- a/journal.cc
+++ b/journal.cc
@@ -267,16 +267,11 @@ entry_t * journal_t::derive_entry(strings_list::iterator i,
entry_t * matching = NULL;
- if (! parse_date((*i).c_str(), &added->date)) {
- std::cerr << "Error: Bad entry date: " << *i << std::endl;
- return false;
- }
- ++i;
+ if (! parse_date((*i).c_str(), &added->date))
+ throw error("Bad date passed to 'entry'");
- if (i == end) {
- std::cerr << "Error: Too few arguments to 'entry'." << std::endl;
- return false;
- }
+ if (++i == end)
+ throw error("Too few arguments to 'entry'");
mask_t regexp(*i++);
@@ -290,17 +285,12 @@ entry_t * journal_t::derive_entry(strings_list::iterator i,
added->payee = matching ? matching->payee : regexp.pattern;
- if (i == end) {
- std::cerr << "Error: Too few arguments to 'entry'." << std::endl;
- return false;
- }
+ if (i == end)
+ throw error("Too few arguments to 'entry'");
if ((*i)[0] == '-' || std::isdigit((*i)[0])) {
- if (! matching) {
- std::cerr << "Error: Missing account name for non-matching entry."
- << std::endl;
- return false;
- }
+ if (! matching)
+ throw error("Missing account name for non-matching entry");
transaction_t * m_xact, * xact, * first;
m_xact = matching->transactions.front();
@@ -343,16 +333,12 @@ entry_t * journal_t::derive_entry(strings_list::iterator i,
if (! acct)
acct = find_account(acct_regex.pattern);
- if (! acct) {
- std::cerr << "Error: Could not find account name '"
- << acct_regex.pattern << "'." << std::endl;
- return false;
- }
+ if (! acct)
+ throw error(std::string("Could not find account name '") +
+ acct_regex.pattern + "'");
- if (i == end) {
- std::cerr << "Error: Too few arguments to 'entry'." << std::endl;
- return false;
- }
+ if (i == end)
+ throw error("Too few arguments to 'entry'");
amount_t amt(*i++);
transaction_t * xact = new transaction_t(acct, amt);
@@ -365,12 +351,11 @@ entry_t * journal_t::derive_entry(strings_list::iterator i,
if (i != end && std::string(*i++) == "-from" && i != end) {
if (account_t * acct = find_account(*i++))
added->add_transaction(new transaction_t(acct));
- } else {
- if (! matching) {
- std::cerr << "Error: Could not figure out the account to draw from."
- << std::endl;
- std::exit(1);
- }
+ }
+ else if (! matching) {
+ throw error("Could not figure out the account to draw from");
+ }
+ else {
transaction_t * xact
= new transaction_t(matching->transactions.back()->account);
added->add_transaction(xact);
@@ -419,6 +404,10 @@ unsigned int transactions_len(entry_t& entry)
transaction_t& transactions_getitem(entry_t& entry, int i)
{
+ static int last_index = 0;
+ static entry_t * last_entry = NULL;
+ static transactions_list::iterator elem;
+
std::size_t len = entry.transactions.size();
if (abs(i) >= len) {
@@ -426,11 +415,19 @@ transaction_t& transactions_getitem(entry_t& entry, int i)
throw_error_already_set();
}
+ if (&entry == last_entry && i == last_index + 1) {
+ last_index = i;
+ return **++elem;
+ }
+
int x = i < 0 ? len + i : i;
- transactions_list::iterator elem = entry.transactions.begin();
+ elem = entry.transactions.begin();
while (--x >= 0)
elem++;
+ last_entry = &entry;
+ last_index = i;
+
return **elem;
}
@@ -441,6 +438,10 @@ unsigned int entries_len(journal_t& journal)
entry_t& entries_getitem(journal_t& journal, int i)
{
+ static int last_index = 0;
+ static journal_t * last_journal = NULL;
+ static entries_list::iterator elem;
+
std::size_t len = journal.entries.size();
if (abs(i) >= len) {
@@ -448,11 +449,19 @@ entry_t& entries_getitem(journal_t& journal, int i)
throw_error_already_set();
}
+ if (&journal == last_journal && i == last_index + 1) {
+ last_index = i;
+ return **++elem;
+ }
+
int x = i < 0 ? len + i : i;
- entries_list::iterator elem = journal.entries.begin();
+ elem = journal.entries.begin();
while (--x >= 0)
elem++;
+ last_journal = &journal;
+ last_index = i;
+
return **elem;
}
@@ -461,21 +470,33 @@ unsigned int accounts_len(account_t& account)
return account.accounts.size();
}
-account_t& accounts_getitem(account_t& account, int index)
+account_t& accounts_getitem(account_t& account, int i)
{
+ static int last_index = 0;
+ static account_t * last_account = NULL;
+ static accounts_map::iterator elem;
+
std::size_t len = account.accounts.size();
- if (abs(index) >= len) {
+ if (abs(i) >= len) {
PyErr_SetString(PyExc_IndexError, "Index out of range");
throw_error_already_set();
}
- int x = 0;
- for (accounts_map::iterator i = account.accounts.begin();
- i != account.accounts.end();
- i++)
- if (x++ == index)
- return *((*i).second);
+ if (&account == last_account && i == last_index + 1) {
+ last_index = i;
+ return *(*++elem).second;
+ }
+
+ int x = i < 0 ? len + i : i;
+ elem = account.accounts.begin();
+ while (--x >= 0)
+ elem++;
+
+ last_account = &account;
+ last_index = i;
+
+ return *(*elem).second;
}
void export_journal()
diff --git a/main.cc b/main.cc
index 42501544..ab26d0d7 100644
--- a/main.cc
+++ b/main.cc
@@ -290,11 +290,8 @@ int parse_and_report(int argc, char * argv[], char * envp[])
command = "e";
else if (command == "equity")
command = "E";
- else {
- std::ostringstream msg;
- msg << "Unrecognized command '" << command << "'";
- throw error(msg.str());
- }
+ else
+ throw error(std::string("Unrecognized command '") + command + "'");
config.process_options(command, arg, args.end());
diff --git a/main.py b/main.py
index 4fc59c91..4103e5a3 100644
--- a/main.py
+++ b/main.py
@@ -19,4 +19,5 @@ register_parser (parser)
journal = Journal ()
parse_journal_file (args[0], journal)
-print journal[-1].payee
+for entry in journal:
+ print entry.payee
diff --git a/textual.cc b/textual.cc
index b066fdd0..8f231b5e 100644
--- a/textual.cc
+++ b/textual.cc
@@ -576,11 +576,8 @@ unsigned int textual_parser_t::parse(std::istream& in,
time_commodity->flags |= COMMODITY_STYLE_NOMARKET;
}
- if (errors > 0) {
- std::ostringstream msg;
- msg << "Errors parsing file '" << path << "'";
- throw error(msg.str());
- }
+ if (errors > 0)
+ throw error(std::string("Errors parsing file '") + path + "'");
return count;
}
diff --git a/valexpr.cc b/valexpr.cc
index 7e2a6da0..d868f9b6 100644
--- a/valexpr.cc
+++ b/valexpr.cc
@@ -642,11 +642,10 @@ value_expr_t * parse_logic_expr(std::istream& in)
}
default:
- if (! in.eof()) {
- std::ostringstream err;
- err << "Unexpected character '" << c << "'";
- throw value_expr_error(err.str());
- }
+ if (! in.eof())
+ throw value_expr_error(std::string("Unexpected character '") +
+ c + "'");
+ break;
}
}
}
@@ -687,22 +686,19 @@ value_expr_t * parse_value_expr(std::istream& in)
node->right = choices = new value_expr_t(value_expr_t::O_COL);
choices->left = parse_logic_expr(in);
c = peek_next_nonws(in);
- if (c != ':') {
- std::ostringstream err;
- err << "Unexpected character '" << c << "'";
- throw value_expr_error(err.str());
- }
+ if (c != ':')
+ throw value_expr_error(std::string("Unexpected character '") +
+ c + "'");
in.get(c);
choices->right = parse_logic_expr(in);
break;
}
default:
- if (! in.eof()) {
- std::ostringstream err;
- err << "Unexpected character '" << c << "'";
- throw value_expr_error(err.str());
- }
+ if (! in.eof())
+ throw value_expr_error(std::string("Unexpected character '") +
+ c + "'");
+ break;
}
c = peek_next_nonws(in);
}