summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2004-08-29 03:23:55 -0400
committerJohn Wiegley <johnw@newartisans.com>2004-08-29 03:23:55 -0400
commit032afa5657bd600476cde256b6d7db84fde7d038 (patch)
tree3d9839f92ce2f99fda85583388dd258f376fdbbb
parent3034529d54759f0fd3bcc6191f33a5e93a1988ca (diff)
downloadfork-ledger-032afa5657bd600476cde256b6d7db84fde7d038.tar.gz
fork-ledger-032afa5657bd600476cde256b6d7db84fde7d038.tar.bz2
fork-ledger-032afa5657bd600476cde256b6d7db84fde7d038.zip
a bit of a reorg; still much more to go
-rwxr-xr-xacprep2
-rw-r--r--amount.cc2
-rw-r--r--config.cc406
-rw-r--r--config.h40
-rw-r--r--format.h1
-rw-r--r--journal.cc6
-rw-r--r--main.cc615
-rw-r--r--main.py13
-rw-r--r--option.cc133
-rw-r--r--option.h37
-rw-r--r--python.cc2
11 files changed, 685 insertions, 572 deletions
diff --git a/acprep b/acprep
index 174d4767..191636d3 100755
--- a/acprep
+++ b/acprep
@@ -1,6 +1,6 @@
#!/bin/sh
-libtoolize -f
+libtoolize --automake -fc
aclocal
autoheader
touch AUTHORS ChangeLog
diff --git a/amount.cc b/amount.cc
index 4d39f29a..9ebf0c80 100644
--- a/amount.cc
+++ b/amount.cc
@@ -1048,7 +1048,7 @@ using namespace ledger;
void (amount_t::*parse_1)(std::istream& in) = &amount_t::parse;
void (amount_t::*parse_2)(const std::string& str) = &amount_t::parse;
-struct commodity_updater_wrap : commodity_t::updater_t
+struct commodity_updater_wrap : public commodity_t::updater_t
{
PyObject * self;
commodity_updater_wrap(PyObject * self_) : self(self_) {}
diff --git a/config.cc b/config.cc
index 3d682395..f090a09e 100644
--- a/config.cc
+++ b/config.cc
@@ -1,9 +1,12 @@
#include "config.h"
#include "option.h"
+#include "quotes.h"
+
+#include <fstream>
namespace ledger {
-config_t * config = NULL;
+config_t config;
std::string bal_fmt = "%20T %2_%-n\n";
std::string reg_fmt
@@ -30,12 +33,255 @@ config_t::config_t()
show_collapsed = false;
show_subtotal = false;
show_related = false;
+ show_all_related = false;
show_inverted = false;
show_empty = false;
days_of_the_week = false;
show_revalued = false;
show_revalued_only = false;
download_quotes = false;
+ use_cache = false;
+ cache_dirty = false;
+ interval_begin = 0;
+}
+
+static void
+regexps_to_predicate(config_t& config,
+ std::deque<std::string>::const_iterator begin,
+ std::deque<std::string>::const_iterator end,
+ const bool account_regexp = false,
+ const bool add_account_short_masks = false)
+{
+ std::string regexps[2];
+
+ // Treat the remaining command-line arguments as regular
+ // expressions, used for refining report results.
+
+ for (std::deque<std::string>::const_iterator i = begin;
+ i != end;
+ i++)
+ if ((*i)[0] == '-') {
+ if (! regexps[1].empty())
+ regexps[1] += "|";
+ regexps[1] += (*i).substr(1);
+ }
+ else if ((*i)[0] == '+') {
+ if (! regexps[0].empty())
+ regexps[0] += "|";
+ regexps[0] += (*i).substr(1);
+ }
+ else {
+ if (! regexps[0].empty())
+ regexps[0] += "|";
+ regexps[0] += *i;
+ }
+
+ for (int i = 0; i < 2; i++) {
+ if (regexps[i].empty())
+ continue;
+
+ if (! config.predicate.empty())
+ config.predicate += "&";
+
+ if (i == 1) {
+ config.predicate += "!";
+ }
+ else if (add_account_short_masks) {
+ if (regexps[i].find(':') != std::string::npos) {
+ config.show_subtotal = true;
+ } else {
+ if (! config.display_predicate.empty())
+ config.display_predicate += "&";
+ else if (! config.show_empty)
+ config.display_predicate += "T&";
+
+ config.display_predicate += "///(?:";
+ config.display_predicate += regexps[i];
+ config.display_predicate += ")/";
+ }
+ }
+
+ if (! account_regexp)
+ config.predicate += "/";
+ config.predicate += "/(?:";
+ config.predicate += regexps[i];
+ config.predicate += ")/";
+ }
+}
+
+void config_t::process_options(const std::string& command,
+ strings_list::iterator arg,
+ strings_list::iterator args_end)
+{
+ // Configure some other options depending on report type
+
+ if (command == "p" || command == "e") {
+ show_related =
+ show_all_related = true;
+ }
+ else if (command == "E") {
+ show_subtotal = true;
+ }
+ else if (show_related) {
+ if (command == "r") {
+ show_inverted = true;
+ } else {
+ show_subtotal = true;
+ show_all_related = true;
+ }
+ }
+
+ // Process remaining command-line arguments
+
+ if (command != "e") {
+ // Treat the remaining command-line arguments as regular
+ // expressions, used for refining report results.
+
+ std::deque<std::string>::iterator i = arg;
+ for (; i != args_end; i++)
+ if (*i == "--")
+ break;
+
+ regexps_to_predicate(*this, arg, i, true,
+ (command == "b" && ! show_subtotal &&
+ display_predicate.empty()));
+ if (i != args_end)
+ regexps_to_predicate(*this, i, args_end);
+ }
+
+ // Setup the default value for the display predicate
+
+ if (display_predicate.empty()) {
+ if (command == "b") {
+ if (! show_empty)
+ display_predicate = "T";
+ if (! show_subtotal) {
+ if (! display_predicate.empty())
+ display_predicate += "&";
+ display_predicate += "l<=1";
+ }
+ }
+ else if (command == "E") {
+ display_predicate = "t";
+ }
+ }
+
+ // Parse the sort_string
+
+ if (! sort_order.get() && ! sort_string.empty()) {
+ try {
+ std::istringstream stream(sort_string);
+ sort_order.reset(parse_value_expr(stream));
+ if (stream.peek() != -1) {
+ throw value_expr_error(std::string("Unexpected character '") +
+ char(stream.peek()) + "'");
+ }
+ else if (! sort_order.get()) {
+ throw error("Failed to parse sort criteria!");
+ }
+ }
+ catch (const value_expr_error& err) {
+ throw error(std::string("In sort criteria: ") + err.what());
+ }
+ }
+
+ // Setup the values of %t and %T, used in format strings
+
+ try {
+ if (! format_t::value_expr)
+ format_t::value_expr = parse_value_expr(value_expr);
+ if (! format_t::value_expr)
+ throw value_expr_error(std::string("Failed to parse '") +
+ value_expr + "'");
+ }
+ catch (const value_expr_error& err) {
+ throw error(std::string("In value expression to -t: ") + err.what());
+ }
+
+ try {
+ if (! format_t::total_expr)
+ format_t::total_expr = parse_value_expr(total_expr);
+ if (! format_t::total_expr)
+ throw value_expr_error(std::string("Failed to parse '") +
+ total_expr + "'");
+ }
+ catch (const value_expr_error& err) {
+ throw error(std::string("In value expression to -T: ") + err.what());
+ }
+
+ // If downloading is to be supported, configure the updater
+
+ if (! commodity_t::updater && download_quotes)
+ commodity_t::updater = new quotes_by_script(price_db,
+ pricing_leeway,
+ cache_dirty);
+
+ // Configure the output stream
+
+ if (! output_stream.get() && ! output_file.empty())
+ output_stream.reset(new std::ofstream(output_file.c_str()));
+
+ // Parse the interval specifier, if provided
+
+ if (! report_interval && ! interval_text.empty()) {
+ try {
+ std::istringstream stream(interval_text);
+ std::time_t begin = -1, end = -1;
+
+ report_interval = interval_t::parse(stream, &begin, &end);
+
+ if (begin != -1) {
+ interval_begin = begin;
+
+ if (! predicate.empty())
+ predicate += "&";
+ char buf[32];
+ std::sprintf(buf, "d>=%lu", begin);
+ predicate += buf;
+ }
+
+ if (end != -1) {
+ if (! predicate.empty())
+ predicate += "&";
+ char buf[32];
+ std::sprintf(buf, "d<%lu", end);
+ predicate += buf;
+ }
+ }
+ catch (const interval_expr_error& err) {
+ throw error(std::string("In interval (-z) specifier: ") + err.what());
+ }
+ }
+
+ if (format_t::date_format.empty() && ! date_format.empty())
+ format_t::date_format = date_format;
+
+ // Compile the format strings
+
+ const char * f;
+ if (! format_string.empty())
+ f = format_string.c_str();
+ else if (command == "b")
+ f = bal_fmt.c_str();
+ else if (command == "r")
+ f = reg_fmt.c_str();
+ else if (command == "E")
+ f = equity_fmt.c_str();
+ else
+ f = print_fmt.c_str();
+
+ std::string first_line_format;
+ std::string next_lines_format;
+
+ if (const char * p = std::strstr(f, "%/")) {
+ first_line_format = std::string(f, 0, p - f);
+ next_lines_format = std::string(p + 2);
+ } else {
+ first_line_format = next_lines_format = f;
+ }
+
+ format.reset(first_line_format);
+ nformat.reset(next_lines_format);
}
static void show_version(std::ostream& out)
@@ -115,8 +361,6 @@ Commands:\n\
//
// Basic options
-DEF_OPT_HANDLERS();
-
OPT_BEGIN(help, "h") {
option_help(std::cout);
throw 0;
@@ -128,31 +372,31 @@ OPT_BEGIN(version, "v") {
} OPT_END(version);
OPT_BEGIN(init, "i:") {
- config->init_file = optarg;
+ config.init_file = optarg;
} OPT_END(init);
OPT_BEGIN(file, "f:") {
- config->data_file = optarg;
+ config.data_file = optarg;
} OPT_END(file);
OPT_BEGIN(cache, ":") {
- config->cache_file = optarg;
+ config.cache_file = optarg;
} OPT_END(cache);
OPT_BEGIN(output, "o:") {
if (std::string(optarg) != "-")
- config->output_file = optarg;
+ config.output_file = optarg;
} OPT_END(output);
OPT_BEGIN(set_price, "z:") {
if (std::strchr(optarg, '='))
- config->price_settings.push_back(optarg);
+ config.price_settings.push_back(optarg);
else
std::cerr << "Error: Invalid price setting: " << optarg << std::endl;
} OPT_END(set_price);
OPT_BEGIN(account, "a:") {
- config->account = optarg;
+ config.account = optarg;
} OPT_END(account);
//////////////////////////////////////////////////////////////////////
@@ -160,43 +404,43 @@ OPT_BEGIN(account, "a:") {
// Report filtering
OPT_BEGIN(begin_date, "b:") {
- if (! config->predicate.empty())
- config->predicate += "&";
- config->predicate += "d>=[";
- config->predicate += optarg;
- config->predicate += "]";
+ if (! config.predicate.empty())
+ config.predicate += "&";
+ config.predicate += "d>=[";
+ config.predicate += optarg;
+ config.predicate += "]";
} OPT_END(begin_date);
OPT_BEGIN(end_date, "e:") {
- if (! config->predicate.empty())
- config->predicate += "&";
- config->predicate += "d<[";
- config->predicate += optarg;
- config->predicate += "]";
+ if (! config.predicate.empty())
+ config.predicate += "&";
+ config.predicate += "d<[";
+ config.predicate += optarg;
+ config.predicate += "]";
} OPT_END(end_date);
OPT_BEGIN(current, "c") {
- if (! config->predicate.empty())
- config->predicate += "&";
- config->predicate += "d<=m";
+ if (! config.predicate.empty())
+ config.predicate += "&";
+ config.predicate += "d<=m";
} OPT_END(current);
OPT_BEGIN(cleared, "C") {
- if (! config->predicate.empty())
- config->predicate += "&";
- config->predicate += "X";
+ if (! config.predicate.empty())
+ config.predicate += "&";
+ config.predicate += "X";
} OPT_END(cleared);
OPT_BEGIN(uncleared, "U") {
- if (! config->predicate.empty())
- config->predicate += "&";
- config->predicate += "!X";
+ if (! config.predicate.empty())
+ config.predicate += "&";
+ config.predicate += "!X";
} OPT_END(uncleared);
OPT_BEGIN(real, "R") {
- if (! config->predicate.empty())
- config->predicate += "&";
- config->predicate += "R";
+ if (! config.predicate.empty())
+ config.predicate += "&";
+ config.predicate += "R";
} OPT_END(real);
//////////////////////////////////////////////////////////////////////
@@ -204,11 +448,11 @@ OPT_BEGIN(real, "R") {
// Output customization
OPT_BEGIN(format, "F:") {
- config->format_string = optarg;
+ config.format_string = optarg;
} OPT_END(format);
OPT_BEGIN(date_format, "y:") {
- config->date_format = optarg;
+ config.date_format = optarg;
} OPT_END(date_format);
OPT_BEGIN(balance_format, ":") {
@@ -236,77 +480,77 @@ OPT_BEGIN(equity_format, ":") {
} OPT_END(equity_format);
OPT_BEGIN(empty, "E") {
- config->show_empty = true;
+ config.show_empty = true;
} OPT_END(empty);
OPT_BEGIN(collapse, "n") {
- config->show_collapsed = true;
+ config.show_collapsed = true;
} OPT_END(collapse);
OPT_BEGIN(subtotal, "s") {
- config->show_subtotal = true;
+ config.show_subtotal = true;
} OPT_END(subtotal);
OPT_BEGIN(sort, "S:") {
- config->sort_string = optarg;
+ config.sort_string = optarg;
} OPT_END(sort);
OPT_BEGIN(related, "r") {
- config->show_related = true;
+ config.show_related = true;
} OPT_END(related);
OPT_BEGIN(interval, "p:") {
- config->interval_text = optarg;
+ config.interval_text = optarg;
} OPT_END(interval);
OPT_BEGIN(weekly, "W") {
- config->interval_text = "weekly";
+ config.interval_text = "weekly";
} OPT_END(weekly);
OPT_BEGIN(dow, "") {
- config->days_of_the_week = true;
+ config.days_of_the_week = true;
} OPT_END(dow);
OPT_BEGIN(monthly, "M") {
- config->interval_text = "monthly";
+ config.interval_text = "monthly";
} OPT_END(monthly);
OPT_BEGIN(yearly, "Y") {
- config->interval_text = "yearly";
+ config.interval_text = "yearly";
} OPT_END(yearly);
OPT_BEGIN(limit, "l:") {
- if (! config->predicate.empty())
- config->predicate += "&";
- config->predicate += "(";
- config->predicate += optarg;
- config->predicate += ")";
+ if (! config.predicate.empty())
+ config.predicate += "&";
+ config.predicate += "(";
+ config.predicate += optarg;
+ config.predicate += ")";
} OPT_END(limit);
OPT_BEGIN(display, "d:") {
- if (! config->display_predicate.empty())
- config->display_predicate += "&";
- config->display_predicate += "(";
- config->display_predicate += optarg;
- config->display_predicate += ")";
+ if (! config.display_predicate.empty())
+ config.display_predicate += "&";
+ config.display_predicate += "(";
+ config.display_predicate += optarg;
+ config.display_predicate += ")";
} OPT_END(display);
OPT_BEGIN(value, "t:") {
- config->value_expr = optarg;
+ config.value_expr = optarg;
} OPT_END(value);
OPT_BEGIN(total, "T:") {
- config->total_expr = optarg;
+ config.total_expr = optarg;
} OPT_END(total);
OPT_BEGIN(value_data, "j") {
- config->value_expr = "S" + config->value_expr;
- config->format_string = plot_value_fmt;
+ config.value_expr = "S" + config.value_expr;
+ config.format_string = plot_value_fmt;
} OPT_END(value_data);
OPT_BEGIN(total_data, "J") {
- config->total_expr = "S" + config->total_expr;
- config->format_string = plot_total_fmt;
+ config.total_expr = "S" + config.total_expr;
+ config.format_string = plot_total_fmt;
} OPT_END(total_data);
//////////////////////////////////////////////////////////////////////
@@ -314,60 +558,60 @@ OPT_BEGIN(total_data, "J") {
// Commodity reporting
OPT_BEGIN(price_db, "P:") {
- config->price_db = optarg;
+ config.price_db = optarg;
} OPT_END(price_db);
OPT_BEGIN(price_exp, "L:") {
- config->pricing_leeway = std::atol(optarg) * 60;
+ config.pricing_leeway = std::atol(optarg) * 60;
} OPT_END(price_exp);
OPT_BEGIN(download, "Q") {
- config->download_quotes = true;
+ config.download_quotes = true;
} OPT_END(download);
OPT_BEGIN(quantity, "O") {
- config->value_expr = "a";
- config->total_expr = "O";
+ config.value_expr = "a";
+ config.total_expr = "O";
} OPT_END(quantity);
OPT_BEGIN(basis, "B") {
- config->value_expr = "c";
- config->total_expr = "C";
+ config.value_expr = "c";
+ config.total_expr = "C";
} OPT_END(basis);
OPT_BEGIN(market, "V") {
- config->show_revalued = true;
+ config.show_revalued = true;
- config->value_expr = "v";
- config->total_expr = "V";
+ config.value_expr = "v";
+ config.total_expr = "V";
} OPT_END(market);
OPT_BEGIN(gain, "G") {
- config->show_revalued =
- config->show_revalued_only = true;
+ config.show_revalued =
+ config.show_revalued_only = true;
- config->value_expr = "a";
- config->total_expr = "G";
+ config.value_expr = "a";
+ config.total_expr = "G";
} OPT_END(gain);
OPT_BEGIN(average, "A") {
- config->value_expr = "a";
- config->total_expr = "MO";
+ config.value_expr = "a";
+ config.total_expr = "MO";
} OPT_END(average);
OPT_BEGIN(deviation, "D") {
- config->value_expr = "a";
- config->total_expr = "DMO";
+ config.value_expr = "a";
+ config.total_expr = "DMO";
} OPT_END(deviation);
OPT_BEGIN(trend, "X") {
- config->value_expr = "a";
- config->total_expr = "MDMO";
+ config.value_expr = "a";
+ config.total_expr = "MDMO";
} OPT_END(trend);
OPT_BEGIN(weighted_trend, "Z") {
- config->value_expr = "a";
- config->total_expr
+ config.value_expr = "a";
+ config.total_expr
= "MD(MO/(1+(((m-d)/(30*86400))<0?0:((m-d)/(30*86400)))))";
} OPT_END(weighted_trend);
diff --git a/config.h b/config.h
index 7da0f73e..2bfee4bd 100644
--- a/config.h
+++ b/config.h
@@ -2,6 +2,10 @@
#define _CONFIG_H
#include "ledger.h"
+#include "option.h"
+#include "valexpr.h"
+#include "datetime.h"
+#include "format.h"
#include <iostream>
#include <memory>
@@ -17,6 +21,8 @@ extern std::string equity_fmt;
struct config_t
{
+ // These options can all be set used text fields.
+
strings_list price_settings;
std::string init_file;
std::string data_file;
@@ -36,6 +42,7 @@ struct config_t
bool show_collapsed;
bool show_subtotal;
bool show_related;
+ bool show_all_related;
bool show_inverted;
bool show_empty;
bool days_of_the_week;
@@ -43,13 +50,44 @@ struct config_t
bool show_revalued_only;
bool download_quotes;
+ // These settings require processing of the above.
+
+ bool use_cache;
+ bool cache_dirty;
+ interval_t report_interval;
+ std::time_t interval_begin;
+ format_t format;
+ format_t nformat;
+
+ std::auto_ptr<value_expr_t> sort_order;
+ std::auto_ptr<std::ostream> output_stream;
+
config_t();
+
+ void process_options(const std::string& command,
+ strings_list::iterator arg,
+ strings_list::iterator args_end);
};
-extern config_t * config;
+extern config_t config;
void option_help(std::ostream& out);
+struct declared_option_handler : public option_handler {
+ declared_option_handler(const std::string& label,
+ const std::string& opt_chars) {
+ register_option(label, opt_chars, *this);
+ }
+};
+
+#define OPT_BEGIN(tag, chars) \
+ static struct opt_ ## tag ## _handler \
+ : public declared_option_handler { \
+ opt_ ## tag ## _handler() : declared_option_handler(#tag, chars) {} \
+ virtual void operator()(const char * optarg)
+
+#define OPT_END(tag) } opt_ ## tag ## _handler_obj
+
} // namespace ledger
#endif // _CONFIG_H
diff --git a/format.h b/format.h
index 9bbd789b..c8047528 100644
--- a/format.h
+++ b/format.h
@@ -57,6 +57,7 @@ struct format_t
static value_expr_t * value_expr;
static value_expr_t * total_expr;
+ format_t() : elements(NULL) {}
format_t(const std::string& _format) : elements(NULL) {
reset(_format);
}
diff --git a/journal.cc b/journal.cc
index 5922cd34..30d258f1 100644
--- a/journal.cc
+++ b/journal.cc
@@ -139,8 +139,12 @@ account_t * account_t::find_account(const std::string& name,
if (i == accounts.end()) {
if (! auto_create)
return NULL;
+
account = new account_t(this, first);
- accounts.insert(accounts_pair(first, account));
+
+ std::pair<accounts_map::iterator, bool> result
+ = accounts.insert(accounts_pair(first, account));
+ assert(result.second);
} else {
account = (*i).second;
}
diff --git a/main.cc b/main.cc
index 7a0b8141..f852564c 100644
--- a/main.cc
+++ b/main.cc
@@ -31,14 +31,10 @@ using namespace ledger;
#include <ctime>
namespace {
- bool cache_dirty = false;
-
TIMER_DEF(write_cache, "writing cache file");
TIMER_DEF(report_gen, "generation of final report");
- TIMER_DEF(handle_options, "configuring based on options");
TIMER_DEF(parse_files, "parsing ledger files");
- TIMER_DEF(process_env, "processing environment");
- TIMER_DEF(process_args, "processing command-line arguments");
+ TIMER_DEF(process_opts, "processing args and environment");
TIMER_DEF(read_cache, "reading cache file");
}
@@ -76,85 +72,166 @@ namespace std {
#endif
-static void
-regexps_to_predicate(std::deque<std::string>::const_iterator begin,
- std::deque<std::string>::const_iterator end,
- config_t * config,
- const bool account_regexp = false,
- const bool add_account_short_masks = false)
+void parse_ledger_data(journal_t * journal,
+ parser_t * text_parser,
+ parser_t * cache_parser)
{
- std::string regexps[2];
-
- // Treat the remaining command-line arguments as regular
- // expressions, used for refining report results.
-
- for (std::deque<std::string>::const_iterator i = begin;
- i != end;
- i++)
- if ((*i)[0] == '-') {
- if (! regexps[1].empty())
- regexps[1] += "|";
- regexps[1] += (*i).substr(1);
+ TIMER_START(parse_files);
+
+ int entry_count = 0;
+
+ if (! config.init_file.empty()) {
+ if (parse_journal_file(config.init_file, journal))
+ throw error("Entries not allowed in initialization file");
+ journal->sources.pop_front(); // remove init file
+ }
+
+ if (config.use_cache && ! config.cache_file.empty() &&
+ ! config.data_file.empty()) {
+ config.cache_dirty = true;
+ if (access(config.cache_file.c_str(), R_OK) != -1) {
+ std::ifstream stream(config.cache_file.c_str());
+ if (cache_parser->test(stream)) {
+ entry_count += cache_parser->parse(stream, journal, NULL,
+ &config.data_file);
+ if (entry_count > 0)
+ config.cache_dirty = false;
+ }
}
- else if ((*i)[0] == '+') {
- if (! regexps[0].empty())
- regexps[0] += "|";
- regexps[0] += (*i).substr(1);
+ }
+
+ if (entry_count == 0 && ! config.data_file.empty()) {
+ account_t * account = NULL;
+ if (! config.account.empty())
+ account = journal->find_account(config.account);
+
+ if (config.data_file == "-") {
+ config.use_cache = false;
+ entry_count += parse_journal(std::cin, journal, account);
+ } else {
+ entry_count += parse_journal_file(config.data_file, journal, account);
}
- else {
- if (! regexps[0].empty())
- regexps[0] += "|";
- regexps[0] += *i;
+
+ if (! config.price_db.empty())
+ if (parse_journal_file(config.price_db, journal))
+ throw error("Entries not allowed in price history file");
+ }
+
+ for (strings_list::iterator i = config.price_settings.begin();
+ i != config.price_settings.end();
+ i++) {
+ std::string conversion = "C ";
+ conversion += *i;
+ int i = conversion.find('=');
+ if (i != -1) {
+ conversion[i] = ' ';
+ std::istringstream stream(conversion);
+ text_parser->parse(stream, journal, journal->master);
}
+ }
- for (int i = 0; i < 2; i++) {
- if (regexps[i].empty())
- continue;
+ if (entry_count == 0)
+ throw error("Please specify ledger file using -f,"
+ " or LEDGER_FILE environment variable.");
- if (! config->predicate.empty())
- config->predicate += "&";
+ VALIDATE(journal->valid());
- if (i == 1) {
- config->predicate += "!";
- }
- else if (add_account_short_masks) {
- if (regexps[i].find(':') != std::string::npos) {
- config->show_subtotal = true;
- } else {
- if (! config->display_predicate.empty())
- config->display_predicate += "&";
- else if (! config->show_empty)
- config->display_predicate += "T&";
-
- config->display_predicate += "///(?:";
- config->display_predicate += regexps[i];
- config->display_predicate += ")/";
- }
- }
+ TIMER_STOP(parse_files);
+}
+
+item_handler<transaction_t> *
+chain_formatters(const std::string& command,
+ item_handler<transaction_t> * base_formatter)
+{
+ std::auto_ptr<item_handler<transaction_t> > formatter;
+
+ // format_transactions write each transaction received to the
+ // output stream.
+ if (command == "b" || command == "E") {
+ formatter.reset(base_formatter);
+ } else {
+ formatter.reset(base_formatter);
- if (! account_regexp)
- config->predicate += "/";
- config->predicate += "/(?:";
- config->predicate += regexps[i];
- config->predicate += ")/";
+ // filter_transactions will only pass through transactions
+ // matching the `display_predicate'.
+ if (! config.display_predicate.empty())
+ formatter.reset(new filter_transactions(formatter.release(),
+ config.display_predicate));
+
+ // calc_transactions computes the running total. When this
+ // appears will determine, for example, whether filtered
+ // transactions are included or excluded from the running total.
+ formatter.reset(new calc_transactions(formatter.release(),
+ config.show_inverted));
+
+ // sort_transactions will sort all the transactions it sees, based
+ // on the `sort_order' value expression.
+ if (config.sort_order.get())
+ formatter.reset(new sort_transactions(formatter.release(),
+ config.sort_order.get()));
+
+ // changed_value_transactions adds virtual transactions to the
+ // list to account for changes in market value of commodities,
+ // which otherwise would affect the running total unpredictably.
+ if (config.show_revalued)
+ formatter.reset(new changed_value_transactions(formatter.release(),
+ config.show_revalued_only));
+
+ // collapse_transactions causes entries with multiple transactions
+ // to appear as entries with a subtotaled transaction for each
+ // commodity used.
+ if (config.show_collapsed)
+ formatter.reset(new collapse_transactions(formatter.release()));
+
+ // subtotal_transactions combines all the transactions it receives
+ // into one subtotal entry, which has one transaction for each
+ // commodity in each account.
+ //
+ // interval_transactions is like subtotal_transactions, but it
+ // subtotals according to time intervals rather than totalling
+ // everything.
+ //
+ // dow_transactions is like interval_transactions, except that it
+ // reports all the transactions that fall on each subsequent day
+ // of the week.
+ if (config.show_subtotal)
+ formatter.reset(new subtotal_transactions(formatter.release()));
+ else if (config.report_interval)
+ formatter.reset(new interval_transactions(formatter.release(),
+ config.report_interval,
+ config.interval_begin));
+ else if (config.days_of_the_week)
+ formatter.reset(new dow_transactions(formatter.release()));
}
+
+ // related_transactions will pass along all transactions related
+ // to the transaction received. If `show_all_related' is true,
+ // then all the entry's transactions are passed; meaning that if
+ // one transaction of an entry is to be printed, all the
+ // transaction for that entry will be printed.
+ if (config.show_related)
+ formatter.reset(new related_transactions(formatter.release(),
+ config.show_all_related));
+
+ // This filter_transactions will only pass through transactions
+ // matching the `predicate'.
+ if (! config.predicate.empty())
+ formatter.reset(new filter_transactions(formatter.release(),
+ config.predicate));
+
+ return formatter.release();
}
int parse_and_report(int argc, char * argv[], char * envp[])
{
std::auto_ptr<journal_t> journal(new journal_t);
- // Initialize the global configuration object for this run
-
- std::auto_ptr<config_t> global_config(new config_t);
- config = global_config.get();
+ // Parse command-line arguments, and those set in the environment
- // Parse command-line arguments
-
- TIMER_START(process_args);
+ TIMER_START(process_opts);
std::deque<std::string> args;
- process_arguments(argc, argv, false, args);
+ process_arguments(argc - 1, argv + 1, false, args);
if (args.empty()) {
option_help(std::cerr);
@@ -162,13 +239,7 @@ int parse_and_report(int argc, char * argv[], char * envp[])
}
strings_list::iterator arg = args.begin();
- TIMER_STOP(process_args);
-
- bool use_cache = config->data_file.empty();
-
- // Process options from the environment
-
- TIMER_START(process_env);
+ config.use_cache = config.data_file.empty();
process_environment(envp, "LEDGER_");
@@ -183,13 +254,10 @@ int parse_and_report(int argc, char * argv[], char * envp[])
process_option("price-exp", p);
#endif
- TIMER_STOP(process_env);
+ TIMER_STOP(process_opts);
- // Parse ledger files
-
- TIMER_START(parse_files);
+ // Parse initialization files, ledger data, price database, etc.
- // Setup the parsers
std::auto_ptr<binary_parser_t> bin_parser(new binary_parser_t);
#ifdef READ_GNUCASH
std::auto_ptr<gnucash_parser_t> gnucash_parser(new gnucash_parser_t);
@@ -204,81 +272,14 @@ int parse_and_report(int argc, char * argv[], char * envp[])
register_parser(qif_parser.get());
register_parser(text_parser.get());
- int entry_count = 0;
+ parse_ledger_data(journal.get(), text_parser.get(), bin_parser.get());
- try {
- if (! config->init_file.empty()) {
- if (parse_journal_file(config->init_file, journal.get()))
- throw error("Entries not allowed in initialization file");
- journal->sources.pop_front(); // remove init file
- }
-
- if (use_cache && ! config->cache_file.empty() &&
- ! config->data_file.empty()) {
- cache_dirty = true;
- if (access(config->cache_file.c_str(), R_OK) != -1) {
- std::ifstream stream(config->cache_file.c_str());
- if (bin_parser->test(stream)) {
- entry_count += bin_parser->parse(stream, journal.get(), NULL,
- &config->data_file);
- if (entry_count > 0)
- cache_dirty = false;
- }
- }
- }
-
- if (entry_count == 0 && ! config->data_file.empty()) {
- account_t * account = NULL;
- if (! config->account.empty())
- account = journal->find_account(config->account);
-
- if (config->data_file == "-") {
- use_cache = false;
- entry_count += text_parser->parse(std::cin, journal.get(), account);
- } else {
- entry_count += parse_journal_file(config->data_file, journal.get(),
- account);
- }
-
- if (! config->price_db.empty())
- if (parse_journal_file(config->price_db, journal.get()))
- throw error("Entries not allowed in price history file");
- }
-
- for (strings_list::iterator i = config->price_settings.begin();
- i != config->price_settings.end();
- i++) {
- std::string conversion = "C ";
- conversion += *i;
- int i = conversion.find('=');
- if (i != -1) {
- conversion[i] = ' ';
- std::istringstream stream(conversion);
- text_parser->parse(stream, journal.get(), journal->master);
- }
- }
- }
- catch (error& err) {
- std::cerr << "Fatal: " << err.what() << std::endl;
- return 1;
- }
-
- if (entry_count == 0) {
- std::cerr << "Please specify ledger file(s) using -f option "
- << "or LEDGER environment variable." << std::endl;
- return 1;
- }
-
- VALIDATE(journal->valid());
-
- TIMER_STOP(parse_files);
-
- // Read the command word, and then check and simplify it
+ // Read the command word, canonicalize it to its one letter form,
+ // then configure the system based on the kind of report to be
+ // generated
std::string command = *arg++;
- TIMER_START(handle_options);
-
if (command == "balance" || command == "bal" || command == "b")
command = "b";
else if (command == "register" || command == "reg" || command == "r")
@@ -290,294 +291,35 @@ int parse_and_report(int argc, char * argv[], char * envp[])
else if (command == "equity")
command = "E";
else {
- std::cerr << "Error: Unrecognized command '" << command << "'."
- << std::endl;
- return 1;
+ std::ostringstream msg;
+ msg << "Unrecognized command '" << command << "'";
+ throw error(msg.str());
}
- // Configure some other options depending on report type
-
- bool show_all_related = false;
-
- if (command == "p" || command == "e") {
- config->show_related =
- show_all_related = true;
- }
- else if (command == "E") {
- config->show_subtotal = true;
- }
- else if (config->show_related) {
- if (command == "r") {
- config->show_inverted = true;
- } else {
- config->show_subtotal = true;
- show_all_related = true;
- }
- }
-
- // Process remaining command-line arguments
+ config.process_options(command, arg, args.end());
std::auto_ptr<entry_t> new_entry;
if (command == "e") {
new_entry.reset(journal->derive_entry(arg, args.end()));
if (! new_entry.get())
return 1;
- } else {
- // Treat the remaining command-line arguments as regular
- // expressions, used for refining report results.
-
- std::deque<std::string>::iterator i = args.begin();
- for (; i != args.end(); i++)
- if (*i == "--")
- break;
-
- regexps_to_predicate(arg, i, config, true,
- command == "b" && ! config->show_subtotal &&
- config->display_predicate.empty());
- if (i != args.end())
- regexps_to_predicate(i, args.end(), config);
- }
-
- // Setup default value for the display predicate
-
- if (config->display_predicate.empty()) {
- if (command == "b") {
- if (! config->show_empty)
- config->display_predicate = "T";
- if (! config->show_subtotal) {
- if (! config->display_predicate.empty())
- config->display_predicate += "&";
- config->display_predicate += "l<=1";
- }
- }
- else if (command == "E") {
- config->display_predicate = "t";
- }
- }
-
- // Compile sorting criteria
-
- std::auto_ptr<value_expr_t> sort_order;
-
- if (! config->sort_string.empty()) {
- try {
- std::istringstream stream(config->sort_string);
- sort_order.reset(parse_value_expr(stream));
- if (stream.peek() != -1) {
- std::ostringstream err;
- err << "Unexpected character '" << char(stream.peek()) << "'";
- throw value_expr_error(err.str());
- }
- else if (! sort_order.get()) {
- std::cerr << "Failed to parse sort criteria!" << std::endl;
- return 1;
- }
- }
- catch (const value_expr_error& err) {
- std::cerr << "Error in sort criteria: " << err.what() << std::endl;
- return 1;
- }
}
- // Setup the values of %t and %T, used in format strings
-
- try {
- format_t::value_expr = parse_value_expr(config->value_expr);
- }
- catch (const value_expr_error& err) {
- std::cerr << "Error in amount (-t) specifier: " << err.what()
- << std::endl;
- return 1;
- }
-
- try {
- format_t::total_expr = parse_value_expr(config->total_expr);
- }
- catch (const value_expr_error& err) {
- std::cerr << "Error in total (-T) specifier: " << err.what()
- << std::endl;
- return 1;
- }
-
- // Setup local and global variables, depending on config settings.
-
- std::auto_ptr<std::ostream> output_stream;
-
- interval_t report_interval;
- std::time_t interval_begin = 0;
-
- if (config->download_quotes)
- commodity_t::updater = new quotes_by_script(config->price_db,
- config->pricing_leeway,
- cache_dirty);
-
- if (! config->output_file.empty())
- output_stream.reset(new std::ofstream(config->output_file.c_str()));
-
-#define OUT() (output_stream.get() ? *output_stream : std::cout)
-
- if (! config->interval_text.empty()) {
- try {
- std::istringstream stream(config->interval_text);
- std::time_t begin = -1, end = -1;
-
- report_interval = interval_t::parse(stream, &begin, &end);
-
- if (begin != -1) {
- interval_begin = begin;
-
- if (! config->predicate.empty())
- config->predicate += "&";
- char buf[32];
- std::sprintf(buf, "d>=%lu", begin);
- config->predicate += buf;
- }
-
- if (end != -1) {
- if (! config->predicate.empty())
- config->predicate += "&";
- char buf[32];
- std::sprintf(buf, "d<%lu", end);
- config->predicate += buf;
- }
- }
- catch (const interval_expr_error& err) {
- std::cerr << "Error in interval (-z) specifier: " << err.what()
- << std::endl;
- return 1;
- }
- }
-
- if (! config->date_format.empty())
- format_t::date_format = config->date_format;
-
-#ifdef DEBUG_ENABLED
- DEBUG_PRINT("ledger.main.predicates", "predicate: " << config->predicate);
- DEBUG_PRINT("ledger.main.predicates",
- "disp-pred: " << config->display_predicate);
-#endif
-
- // Compile the format strings
-
- const char * f;
- if (! config->format_string.empty())
- f = config->format_string.c_str();
- else if (command == "b")
- f = bal_fmt.c_str();
- else if (command == "r")
- f = reg_fmt.c_str();
- else if (command == "E")
- f = equity_fmt.c_str();
- else
- f = print_fmt.c_str();
-
- std::string first_line_format;
- std::string next_lines_format;
-
- if (const char * p = std::strstr(f, "%/")) {
- first_line_format = std::string(f, 0, p - f);
- next_lines_format = std::string(p + 2);
- } else {
- first_line_format = next_lines_format = f;
- }
-
- format_t format(first_line_format);
- format_t nformat(next_lines_format);
-
- TIMER_STOP(handle_options);
-
// Walk the entries based on the report type and the options
TIMER_START(report_gen);
- // Stack up all the formatter needed to fulfills the user's
- // requests. Some of these are order dependent, in terms of
- // whether calc_transactions occurs before or after them.
-
std::auto_ptr<item_handler<transaction_t> > formatter;
- // format_transactions write each transaction received to the
- // output stream.
if (command == "b" || command == "E") {
-#ifdef DEBUG_ENABLED
- if (DEBUG("ledger.balance.items")) {
- formatter.reset(new format_transactions(OUT(), format, nformat));
- formatter.reset(new set_account_value(formatter.release()));
- } else
-#endif
- formatter.reset(new set_account_value);
+ formatter.reset(chain_formatters(command, new set_account_value));
} else {
- formatter.reset(new format_transactions(OUT(), format, nformat));
-
- // filter_transactions will only pass through transactions
- // matching the `display_predicate'.
- if (! config->display_predicate.empty())
- formatter.reset(new filter_transactions(formatter.release(),
- config->display_predicate));
-
- // calc_transactions computes the running total. When this
- // appears will determine, for example, whether filtered
- // transactions are included or excluded from the running total.
- formatter.reset(new calc_transactions(formatter.release(),
- config->show_inverted));
-
- // sort_transactions will sort all the transactions it sees, based
- // on the `sort_order' value expression.
- if (sort_order.get())
- formatter.reset(new sort_transactions(formatter.release(),
- sort_order.get()));
-
- // changed_value_transactions adds virtual transactions to the
- // list to account for changes in market value of commodities,
- // which otherwise would affect the running total unpredictably.
- if (config->show_revalued)
- formatter.reset(new changed_value_transactions(formatter.release(),
- config->show_revalued_only));
-
- // collapse_transactions causes entries with multiple transactions
- // to appear as entries with a subtotaled transaction for each
- // commodity used.
- if (config->show_collapsed)
- formatter.reset(new collapse_transactions(formatter.release()));
-
- // subtotal_transactions combines all the transactions it receives
- // into one subtotal entry, which has one transaction for each
- // commodity in each account.
- //
- // interval_transactions is like subtotal_transactions, but it
- // subtotals according to time intervals rather than totalling
- // everything.
- //
- // dow_transactions is like interval_transactions, except that it
- // reports all the transactions that fall on each subsequent day
- // of the week.
- if (config->show_subtotal)
- formatter.reset(new subtotal_transactions(formatter.release()));
- else if (report_interval)
- formatter.reset(new interval_transactions(formatter.release(),
- report_interval,
- interval_begin));
- else if (config->days_of_the_week)
- formatter.reset(new dow_transactions(formatter.release()));
+ std::ostream& out(config.output_stream.get() ?
+ *config.output_stream : std::cout);
+ formatter.reset(chain_formatters(command,
+ new format_transactions(out, config.format, config.nformat)));
}
- // related_transactions will pass along all transactions related
- // to the transaction received. If `show_all_related' is true,
- // then all the entry's transactions are passed; meaning that if
- // one transaction of an entry is to be printed, all the
- // transaction for that entry will be printed.
- if (config->show_related)
- formatter.reset(new related_transactions(formatter.release(),
- show_all_related));
-
- // This filter_transactions will only pass through transactions
- // matching the `predicate'.
- if (! config->predicate.empty())
- formatter.reset(new filter_transactions(formatter.release(),
- config->predicate));
-
- // Once the filters are chained, walk `journal's entries and start
- // feeding each transaction that matches `predicate' to the chain.
if (command == "e")
walk_transactions(new_entry->transactions, *formatter);
else
@@ -585,32 +327,32 @@ int parse_and_report(int argc, char * argv[], char * envp[])
formatter->flush();
- // At this point all printing is finished if doing a register
- // report; but if it's a balance or equity report, we've only
- // finished calculating the totals and there is still reporting to
- // be done.
+ // For the balance and equity reports, output the sum totals.
+
+ std::ostream& out(config.output_stream.get() ?
+ *config.output_stream : std::cout);
if (command == "b") {
- format_account acct_formatter(OUT(), format, config->display_predicate);
+ format_account acct_formatter(out, config.format,
+ config.display_predicate);
sum_accounts(journal->master);
- walk_accounts(journal->master, acct_formatter, sort_order.get());
+ walk_accounts(journal->master, acct_formatter, config.sort_order.get());
acct_formatter.flush();
if (journal->master->data) {
ACCT_DATA(journal->master)->value = ACCT_DATA(journal->master)->total;
if (ACCT_DATA(journal->master)->dflags & ACCOUNT_TO_DISPLAY) {
- std::string end_format = "--------------------\n";
- format.reset(end_format + f);
- format.format_elements(OUT(), details_t(journal->master));
+ out << "--------------------\n";
+ config.format.format_elements(out, details_t(journal->master));
}
}
}
else if (command == "E") {
- format_equity acct_formatter(OUT(), format, nformat,
- config->display_predicate);
+ format_equity acct_formatter(out, config.format, config.nformat,
+ config.display_predicate);
sum_accounts(journal->master);
- walk_accounts(journal->master, acct_formatter, sort_order.get());
+ walk_accounts(journal->master, acct_formatter, config.sort_order.get());
acct_formatter.flush();
}
@@ -626,12 +368,12 @@ int parse_and_report(int argc, char * argv[], char * envp[])
TIMER_STOP(report_gen);
- // Save the cache, if need be
+ // Write out the binary cache, if need be
TIMER_START(write_cache);
- if (use_cache && cache_dirty && ! config->cache_file.empty()) {
- std::ofstream stream(config->cache_file.c_str());
+ if (config.use_cache && config.cache_dirty && ! config.cache_file.empty()) {
+ std::ofstream stream(config.cache_file.c_str());
write_binary_journal(stream, journal.get(), &journal->sources);
}
@@ -649,11 +391,12 @@ int main(int argc, char * argv[], char * envp[])
try {
status = parse_and_report(argc, argv, envp);
}
+ catch (error& err) {
+ std::cerr << "Error: " << err.what() << std::endl;
+ status = 1;
+ }
catch (int& val) {
-#if DEBUG_LEVEL >= BETA
- shutdown();
-#endif
- return val;
+ status = val;
}
#if DEBUG_LEVEL >= BETA
diff --git a/main.py b/main.py
index d1e6cb10..52852476 100644
--- a/main.py
+++ b/main.py
@@ -1,7 +1,20 @@
import sys
+import os
from ledger import *
+def foo (str):
+ print "Hello:", str
+def bar (str):
+ print "Goodbye:", str
+
+register_option ("hello", "h:", foo)
+register_option ("goodbye", "g:", bar)
+print process_arguments (sys.argv[1:])
+process_environment (os.environ, "TEST_")
+
+sys.exit(0)
+
parser = TextualParser ()
register_parser (parser)
diff --git a/option.cc b/option.cc
index 98b4a1b6..7e0a74f3 100644
--- a/option.cc
+++ b/option.cc
@@ -6,9 +6,11 @@
#include "util.h"
-option_handler::option_handler(const std::string& label,
- const std::string& opt_chars)
- : handled(false)
+static std::deque<option_t> options;
+
+void register_option(const std::string& label,
+ const std::string& opt_chars,
+ option_handler& option)
{
DEBUG_PRINT("ledger.memory.ctors", "ctor option_handler");
@@ -25,8 +27,6 @@ option_handler::option_handler(const std::string& label,
*p = '\0';
opt.long_opt = buf;
- handlers.insert(option_handler_pair(opt.long_opt, this));
-
if (! opt_chars.empty()) {
if (opt_chars[0] != ':')
opt.short_opt = opt_chars[0];
@@ -35,7 +35,7 @@ option_handler::option_handler(const std::string& label,
opt.wants_arg = true;
}
- opt.handler = this;
+ opt.handler = &option;
options.push_back(opt);
}
@@ -43,29 +43,33 @@ option_handler::option_handler(const std::string& label,
static inline void process_option(const option_t& opt,
const char * arg = NULL) {
if (! opt.handler->handled) {
- opt.handler->handle_option(arg);
+ (*opt.handler)(arg);
opt.handler->handled = true;
}
}
bool process_option(const std::string& opt, const char * arg)
{
- option_handler_map::iterator handler = option_handler::handlers.find(opt);
- if (handler != option_handler::handlers.end()) {
- if (! (*handler).second->handled) {
- (*handler).second->handle_option(arg);
- (*handler).second->handled = true;
+ for (std::deque<option_t>::iterator i = options.begin();
+ i != options.end();
+ i++)
+ if ((*i).long_opt == opt) {
+ if (! (*i).handler->handled) {
+ (*(*i).handler)(arg);
+ (*i).handler->handled = true;
+ return true;
+ }
+ break;
}
- return true;
- }
+
return false;
}
void process_arguments(int argc, char ** argv, const bool anywhere,
std::deque<std::string>& args)
{
- int index = 1;
- for (char ** i = argv + 1; index < argc; i++, index++) {
+ int index = 0;
+ for (char ** i = argv; index < argc; i++, index++) {
if ((*i)[0] != '-') {
if (anywhere) {
args.push_back(*i);
@@ -83,8 +87,8 @@ void process_arguments(int argc, char ** argv, const bool anywhere,
if ((*i)[2] == '\0')
break;
- for (std::deque<option_t>::iterator j = option_handler::options.begin();
- j != option_handler::options.end();
+ for (std::deque<option_t>::iterator j = options.begin();
+ j != options.end();
j++)
if ((*j).wants_arg) {
if (const char * p = std::strchr(*i + 2, '=')) {
@@ -107,8 +111,8 @@ void process_arguments(int argc, char ** argv, const bool anywhere,
std::cerr << "Error: illegal option " << *i << std::endl;
std::exit(1);
} else {
- for (std::deque<option_t>::iterator j = option_handler::options.begin();
- j != option_handler::options.end();
+ for (std::deque<option_t>::iterator j = options.begin();
+ j != options.end();
j++)
if ((*i)[1] == (*j).short_opt) {
if ((*j).wants_arg) {
@@ -136,12 +140,15 @@ void process_arguments(int argc, char ** argv, const bool anywhere,
void process_environment(char ** envp, const std::string& tag)
{
+ const char * tag_p = tag.c_str();
+ int tag_len = tag.length();
+
for (char ** p = envp; *p; p++)
- if (std::strncmp(*p, tag.c_str(), 7) == 0) {
+ if (std::strncmp(*p, tag_p, tag_len) == 0) {
char * q;
static char buf[128];
char * r = buf;
- for (q = *p + 7; *q && *q != '='; q++)
+ for (q = *p + tag_len; *q && *q != '='; q++)
if (*q == '_')
*r++ += '-';
else
@@ -153,3 +160,85 @@ void process_environment(char ** envp, const std::string& tag)
process_option(buf, q + 1);
}
}
+
+#ifdef USE_BOOST_PYTHON
+
+#include <boost/python.hpp>
+#include <boost/python/detail/api_placeholder.hpp>
+#include <Python.h>
+#include <vector>
+
+using namespace boost::python;
+
+struct func_option_wrapper : public option_handler
+{
+ object self;
+ func_option_wrapper(object _self) : self(_self) {}
+
+ virtual void operator()(const char * arg) {
+ call<void>(self.ptr(), arg);
+ }
+};
+
+static std::deque<func_option_wrapper> wrappers;
+
+void py_register_option(const std::string& long_opt,
+ const std::string& short_opt, object func)
+{
+ wrappers.push_back(func_option_wrapper(func));
+ register_option(long_opt, short_opt, wrappers.back());
+}
+
+bool (*process_option_1)(const std::string& opt, const char * arg)
+ = process_option;
+
+list py_process_arguments(list args, bool anywhere = false)
+{
+ std::vector<char *> strs;
+
+ int l = len(args);
+ for (int i = 0; i < l; i++)
+ strs.push_back(extract<char *>(args[i]));
+
+ std::deque<std::string> newargs;
+ process_arguments(strs.size(), &strs.front(), anywhere, newargs);
+
+ list py_newargs;
+ for (std::deque<std::string>::iterator i = newargs.begin();
+ i != newargs.end();
+ i++)
+ py_newargs.append(*i);
+ return py_newargs;
+}
+
+void py_process_environment(object env, const std::string& tag)
+{
+ std::vector<char *> strs;
+ std::vector<std::string> storage;
+
+ list items = call_method<list>(env.ptr(), "items");
+ int l = len(items);
+ for (int i = 0; i < l; i++) {
+ tuple pair = extract<tuple>(items[i]);
+ std::string s = extract<std::string>(pair[0]);
+ s += "=";
+ s += extract<std::string>(pair[1]);
+ storage.push_back(s);
+ strs.push_back(const_cast<char *>(storage.back().c_str()));
+ }
+
+ process_environment(&strs.front(), tag);
+}
+
+BOOST_PYTHON_FUNCTION_OVERLOADS(py_proc_args_overloads,
+ py_process_arguments, 1, 2)
+
+void export_option()
+{
+ def("register_option", py_register_option);
+ def("process_option", process_option_1);
+ def("process_arguments", py_process_arguments, py_proc_args_overloads());
+ def("process_environment", py_process_environment);
+}
+
+#endif // USE_BOOST_PYTHON
diff --git a/option.h b/option.h
index b93f50ab..44ca5b3b 100644
--- a/option.h
+++ b/option.h
@@ -1,11 +1,14 @@
#ifndef _OPTION_H
#define _OPTION_H
-#include <map>
#include <deque>
#include <string>
-struct option_handler;
+struct option_handler {
+ bool handled;
+ option_handler() : handled(false) {}
+ virtual void operator()(const char * arg = NULL) = 0;
+};
struct option_t {
char short_opt;
@@ -13,38 +16,14 @@ struct option_t {
bool wants_arg;
option_handler * handler;
- option_t() : short_opt(0), wants_arg(false) {}
-};
-
-typedef std::map<const std::string, option_handler *> option_handler_map;
-typedef std::pair<const std::string, option_handler *> option_handler_pair;
-
-struct option_handler {
- bool handled;
-
- static std::deque<option_t> options;
- static option_handler_map handlers;
-
- option_handler(const std::string& label,
- const std::string& opt_chars);
-
- virtual void handle_option(const char * arg = NULL) = 0;
+ option_t() : short_opt(0), wants_arg(false), handler(NULL) {}
};
+void register_option(const std::string& label,
+ const std::string& opt_chars, option_handler& option);
bool process_option(const std::string& opt, const char * arg = NULL);
void process_arguments(int argc, char ** argv, const bool anywhere,
std::deque<std::string>& args);
void process_environment(char ** envp, const std::string& tag);
-#define DEF_OPT_HANDLERS() \
- std::deque<option_t> option_handler::options; \
- option_handler_map option_handler::handlers
-
-#define OPT_BEGIN(tag, chars) \
- static struct opt_ ## tag ## _handler : public option_handler { \
- opt_ ## tag ## _handler() : option_handler(#tag, chars) {} \
- virtual void handle_option(const char * optarg)
-
-#define OPT_END(tag) } opt_ ## tag ## _handler_obj
-
#endif // _OPTION_H
diff --git a/python.cc b/python.cc
index 36d80c7c..5ed9be31 100644
--- a/python.cc
+++ b/python.cc
@@ -22,6 +22,7 @@ void export_qif();
#ifdef READ_GNUCASH
void export_gnucash();
#endif
+void export_option();
BOOST_PYTHON_MODULE(ledger) {
export_amount();
@@ -35,5 +36,6 @@ BOOST_PYTHON_MODULE(ledger) {
#ifdef READ_GNUCASH
export_gnucash();
#endif
+ export_option();
ledger::initialize();
}