Changed xpath to use intrusive_ptr; got the xml command working

This commit is contained in:
John Wiegley 2007-05-14 22:58:12 +00:00
parent 0e5035be7d
commit 915ec2f1cc
2 changed files with 97 additions and 171 deletions

View file

@ -46,19 +46,11 @@
#include <fdstream.hpp>
#endif
using namespace ledger;
#if 0
class print_addr : public repitem_t::select_callback_t {
virtual void operator()(repitem_t * item) {
std::cout << item << std::endl;
}
};
#endif
static int read_and_report(report_t * report, int argc, char * argv[],
static int read_and_report(ledger::report_t * report, int argc, char * argv[],
char * envp[])
{
using namespace ledger;
session_t& session(*report->session);
// Handle the command-line arguments
@ -119,52 +111,28 @@ static int read_and_report(report_t * report, int argc, char * argv[],
xml::xpath_t::function_t command;
#if 0
if (verb == "register" || verb == "reg" || verb == "r") {
if (verb == "register" || verb == "reg" || verb == "r")
command = register_command();
#if 0
command = new format_command
("register", either_or(report->format_string,
report->session->register_format));
#endif
}
else if (verb == "balance" || verb == "bal" || verb == "b") {
if (! report->raw_mode) {
report->transforms.push_back(new accounts_transform);
report->transforms.push_back(new clean_transform);
report->transforms.push_back(new compact_transform);
}
command = new format_command
("balance", either_or(report->format_string,
report->session->balance_format));
}
else if (verb == "print" || verb == "p") {
if (! report->raw_mode)
report->transforms.push_back(new optimize_transform);
command = new format_command
("print", either_or(report->format_string,
report->session->print_format));
}
else if (verb == "equity") {
if (! report->raw_mode)
report->transforms.push_back(new accounts_transform);
command = new format_command
("equity", either_or(report->format_string,
report->session->equity_format));
}
else if (verb == "balance" || verb == "bal" || verb == "b")
command = balance_command();
else if (verb == "print" || verb == "p")
command = print_command();
else if (verb == "equity")
command = equity_command();
else if (verb == "entry")
command = new entry_command;
command = entry_command();
else if (verb == "dump")
command = new dump_command;
command = dump_command();
else if (verb == "output")
command = new output_command;
command = output_command();
else if (verb == "prices")
command = new prices_command;
command = prices_command();
else if (verb == "pricesdb")
command = new pricesdb_command;
command = pricesdb_command();
else if (verb == "csv")
command = new csv_command;
command = csv_command();
else if (verb == "emacs" || verb == "lisp")
command = new emacs_command;
command = emacs_command();
else
#endif
if (verb == "xml")
@ -196,8 +164,6 @@ static int read_and_report(report_t * report, int argc, char * argv[],
std::strcpy(buf, "command_");
std::strcat(buf, verb.c_str());
// jww (2007-04-19): This is an error, since command is an
// auto_ptr!
if (xml::xpath_t::ptr_op_t def = report->lookup(buf))
command = def->as_function();
@ -212,9 +178,10 @@ static int read_and_report(report_t * report, int argc, char * argv[],
INFO_START(journal, "Read journal file");
xml::document_t xml_document(xml::LEDGER_NODE);
xml::document_t xml_document(xml::LEDGER_NODE);
journal_t * journal = session.create_journal();
xml::document_builder_t builder(xml_document);
journal_t * journal = session.create_journal();
if (! session.read_data(builder, journal, report->account))
throw_(parse_error, "Failed to locate any journal entries; "
"did you specify a valid file with -f?");
@ -304,84 +271,38 @@ static int read_and_report(report_t * report, int argc, char * argv[],
#if 0
std::auto_ptr<repitem_t> items(repitem_t::wrap(&session, report, true));
print_addr cb;
items->select(path.get(), cb);
items->select(path.get());
#endif
return 0;
}
// Create the an argument scope containing the report command's
// Apply transforms to the hierarchical document structure
INFO_START(transforms, "Applied transforms");
report->apply_transforms(xml_document);
INFO_FINISH(transforms);
// Create an argument scope containing the report command's
// arguments, and then invoke the command.
scoped_ptr<xml::xpath_t::scope_t> locals
(new xml::xpath_t::scope_t(report, xml::xpath_t::scope_t::ARGUMENT));
locals->args = value_t::sequence_t();
locals->args.push_back(out);
locals->args.push_back(&xml_document);
#if 0
if (command->wants_args) {
for (strings_list::iterator i = args.begin();
i != args.end();
i++)
locals->args.push_back(*i);
} else {
string regexps[4];
// Treat the remaining command-line arguments as regular
// expressions, used for refining report results.
int base = 0;
for (strings_list::iterator i = arg; i != args.end(); i++)
if ((*i)[0] == '-') {
if ((*i)[1] == '-') {
if (base == 0)
base += 2;
continue;
}
if (! regexps[base + 1].empty())
regexps[base + 1] += "|";
regexps[base + 1] += (*i).substr(1);
} else {
if (! regexps[base].empty())
regexps[base] += "|";
regexps[base] += *i;
}
#if 0
// jww (2006-09-21): Escape the \ in these strings!
if (! regexps[3].empty())
report->transforms.push_front
(new remove_transform
(string("//entry[payee =~ /(") + regexps[3] + ")/]"));
if (! regexps[2].empty())
report->transforms.push_front
(new select_transform
(string("//entry[payee =~ /(") + regexps[2] + ")/]"));
if (! regexps[1].empty())
report->transforms.push_front
(new remove_transform
(string("//xact[account =~ /(") + regexps[1] + ")/]"));
if (! regexps[0].empty())
report->transforms.push_front
(new select_transform
(string("//xact[account =~ /(") + regexps[0] + ")/]"));
#endif
}
#endif
INFO_START(transforms, "Applied transforms");
report->apply_transforms(xml_document);
INFO_FINISH(transforms);
value_t::sequence_t args_list;
foreach (string& i, args)
args_list.push_back(value_t(i));
locals->args.push_back(value_t(args_list));
INFO_START(command, "Did user command '" << verb << "'");
value_t temp;
command(temp, locals.get());
INFO_FINISH(command);
// Write out the binary cache, if need be
@ -389,21 +310,14 @@ static int read_and_report(report_t * report, int argc, char * argv[],
if (session.use_cache && session.cache_dirty && session.cache_file) {
TRACE_START(binary_cache, 1, "Wrote binary journal file");
ofstream stream(*session.cache_file);
#if 0
ofstream stream(*session.cache_file);
write_binary_journal(stream, journal);
#endif
TRACE_FINISH(binary_cache, 1);
}
#if defined(FREE_MEMORY)
// Cleanup memory -- if this is a beta or development build.
if (report->output_file)
checked_delete(out);
#endif
// If the user specified a pager, wait for it to exit now
#ifdef HAVE_UNIX_PIPES
@ -417,6 +331,9 @@ static int read_and_report(report_t * report, int argc, char * argv[],
throw_(std::logic_error, "Something went wrong in the pager");
}
#endif
else if (DO_VERIFY() && report->output_file) {
checked_delete(out);
}
return 0;
}
@ -427,36 +344,39 @@ int main(int argc, char * argv[], char * envp[])
for (int i = 1; i < argc; i++)
if (argv[i][0] == '-') {
if (std::strcmp(argv[i], "--verify") == 0) {
#if defined(VERIFY_ON)
if (std::strcmp(argv[i], "--verify") == 0)
ledger::verify_enabled = true;
#endif
}
else if (std::strcmp(argv[i], "--verbose") == 0 ||
std::strcmp(argv[i], "-v") == 0) {
#if defined(LOGGING_ON)
if (std::strcmp(argv[i], "--verbose") == 0 ||
std::strcmp(argv[i], "-v") == 0)
ledger::_log_level = LOG_INFO;
ledger::_log_level = ledger::LOG_INFO;
#endif
}
else if (i + 1 < argc && std::strcmp(argv[i], "--debug") == 0) {
#if defined(DEBUG_ON)
if (i + 1 < argc && std::strcmp(argv[i], "--debug") == 0) {
ledger::_log_level = LOG_DEBUG;
ledger::_log_level = ledger::LOG_DEBUG;
ledger::_log_category = argv[i + 1];
i++;
}
#endif
}
else if (i + 1 < argc && std::strcmp(argv[i], "--trace") == 0) {
#if defined(TRACING_ON)
if (i + 1 < argc && std::strcmp(argv[i], "--trace") == 0) {
ledger::_log_level = LOG_TRACE;
ledger::_trace_level = lexical_cast<int>(argv[i + 1]);
ledger::_log_level = ledger::LOG_TRACE;
ledger::_trace_level = boost::lexical_cast<int>(argv[i + 1]);
i++;
}
#endif
}
}
IF_VERIFY()
initialize_memory_tracing();
ledger::initialize_memory_tracing();
try {
std::ios::sync_with_stdio(false);
boost::filesystem::path::default_name_check
(boost::filesystem::portable_posix_name);
@ -477,17 +397,17 @@ int main(int argc, char * argv[], char * envp[])
#endif
session->register_parser(new qif_parser_t);
#endif
session->register_parser(new textual_parser_t);
session->register_parser(new ledger::textual_parser_t);
std::auto_ptr<ledger::report_t> report(new ledger::report_t(session.get()));
status = read_and_report(report.get(), argc, argv, envp);
if (! DO_VERIFY()) {
if (DO_VERIFY()) {
ledger::set_session_context();
} else {
report.release();
session.release();
} else {
ledger::set_session_context();
}
}
#if 0
@ -522,7 +442,7 @@ int main(int argc, char * argv[], char * envp[])
IF_VERIFY() {
INFO("Ledger ended (Boost/libstdc++ may still hold memory)");
shutdown_memory_tracing();
ledger::shutdown_memory_tracing();
} else {
INFO("Ledger ended");
}

View file

@ -72,7 +72,6 @@
#define TRACING_ON 1
#define DEBUG_ON 1
#define TIMERS_ON 1
#define FREE_MEMORY 1
#elif defined(NDEBUG)
#define NO_ASSERTS 1
#define NO_LOGGING 1
@ -284,9 +283,11 @@ bool logger_func(log_level_t level);
extern unsigned int _trace_level;
#define SHOW_TRACE(lvl) \
(_log_level >= LOG_TRACE && lvl <= _trace_level)
(ledger::_log_level >= ledger::LOG_TRACE && lvl <= ledger::_trace_level)
#define TRACE(lvl, msg) \
(SHOW_TRACE(lvl) ? ((_log_buffer << msg), logger_func(LOG_TRACE)) : false)
(SHOW_TRACE(lvl) ? \
((ledger::_log_buffer << msg), \
ledger::logger_func(ledger::LOG_TRACE)) : false)
#else // TRACING_ON
@ -304,11 +305,13 @@ inline bool category_matches(const char * cat) {
}
#define SHOW_DEBUG(cat) \
(_log_level >= LOG_DEBUG && category_matches(cat))
(ledger::_log_level >= ledger::LOG_DEBUG && ledger::category_matches(cat))
#define SHOW_DEBUG_() SHOW_DEBUG(_this_category)
#define DEBUG(cat, msg) \
(SHOW_DEBUG(cat) ? ((_log_buffer << msg), logger_func(LOG_DEBUG)) : false)
(SHOW_DEBUG(cat) ? \
((ledger::_log_buffer << msg), \
ledger::logger_func(ledger::LOG_DEBUG)) : false)
#define DEBUG_(msg) DEBUG(_this_category, msg)
#else // DEBUG_ON
@ -320,22 +323,22 @@ inline bool category_matches(const char * cat) {
#endif // DEBUG_ON
#define LOG_MACRO(level, msg) \
(_log_level >= level ? \
((_log_buffer << msg), logger_func(level)) : false)
#define LOG_MACRO(level, msg) \
(ledger::_log_level >= level ? \
((ledger::_log_buffer << msg), ledger::logger_func(level)) : false)
#define SHOW_INFO() (_log_level >= LOG_INFO)
#define SHOW_WARN() (_log_level >= LOG_WARN)
#define SHOW_ERROR() (_log_level >= LOG_ERROR)
#define SHOW_FATAL() (_log_level >= LOG_FATAL)
#define SHOW_CRITICAL() (_log_level >= LOG_CRIT)
#define SHOW_INFO() (ledger::_log_level >= ledger::LOG_INFO)
#define SHOW_WARN() (ledger::_log_level >= ledger::LOG_WARN)
#define SHOW_ERROR() (ledger::_log_level >= ledger::LOG_ERROR)
#define SHOW_FATAL() (ledger::_log_level >= ledger::LOG_FATAL)
#define SHOW_CRITICAL() (ledger::_log_level >= ledger::LOG_CRIT)
#define INFO(msg) LOG_MACRO(LOG_INFO, msg)
#define WARN(msg) LOG_MACRO(LOG_WARN, msg)
#define ERROR(msg) LOG_MACRO(LOG_ERROR, msg)
#define FATAL(msg) LOG_MACRO(LOG_FATAL, msg)
#define CRITICAL(msg) LOG_MACRO(LOG_CRIT, msg)
#define EXCEPTION(msg) LOG_MACRO(LOG_EXCEPT, msg)
#define INFO(msg) LOG_MACRO(ledger::LOG_INFO, msg)
#define WARN(msg) LOG_MACRO(ledger::LOG_WARN, msg)
#define ERROR(msg) LOG_MACRO(ledger::LOG_ERROR, msg)
#define FATAL(msg) LOG_MACRO(ledger::LOG_FATAL, msg)
#define CRITICAL(msg) LOG_MACRO(ledger::LOG_CRIT, msg)
#define EXCEPTION(msg) LOG_MACRO(ledger::LOG_EXCEPT, msg)
} // namespace ledger
@ -386,13 +389,14 @@ void stop_timer(const char * name);
void finish_timer(const char * name);
#if defined(TRACING_ON)
#define TRACE_START(name, lvl, msg) \
(SHOW_TRACE(lvl) ? \
((_log_buffer << msg), start_timer(#name, LOG_TRACE)) : ((void)0))
#define TRACE_START(name, lvl, msg) \
(SHOW_TRACE(lvl) ? \
((ledger::_log_buffer << msg), \
ledger::start_timer(#name, ledger::LOG_TRACE)) : ((void)0))
#define TRACE_STOP(name, lvl) \
(SHOW_TRACE(lvl) ? stop_timer(#name) : ((void)0))
(SHOW_TRACE(lvl) ? ledger::stop_timer(#name) : ((void)0))
#define TRACE_FINISH(name, lvl) \
(SHOW_TRACE(lvl) ? finish_timer(#name) : ((void)0))
(SHOW_TRACE(lvl) ? ledger::finish_timer(#name) : ((void)0))
#else
#define TRACE_START(name, lvl, msg)
#define TRACE_STOP(name)
@ -400,17 +404,18 @@ void finish_timer(const char * name);
#endif
#if defined(DEBUG_ON)
#define DEBUG_START(name, cat, msg) \
(SHOW_DEBUG(cat) ? \
((_log_buffer << msg), start_timer(#name, LOG_DEBUG)) : ((void)0))
#define DEBUG_START(name, cat, msg) \
(SHOW_DEBUG(cat) ? \
((ledger::_log_buffer << msg), \
ledger::start_timer(#name, ledger::LOG_DEBUG)) : ((void)0))
#define DEBUG_START_(name, msg) \
DEBUG_START_(name, _this_category, msg)
#define DEBUG_STOP(name, cat) \
(SHOW_DEBUG(cat) ? stop_timer(#name) : ((void)0))
(SHOW_DEBUG(cat) ? ledger::stop_timer(#name) : ((void)0))
#define DEBUG_STOP_(name) \
DEBUG_STOP_(name, _this_category)
#define DEBUG_FINISH(name, cat) \
(SHOW_DEBUG(cat) ? finish_timer(#name) : ((void)0))
(SHOW_DEBUG(cat) ? ledger::finish_timer(#name) : ((void)0))
#define DEBUG_FINISH_(name) \
DEBUG_FINISH_(name, _this_category)
#else
@ -420,9 +425,10 @@ void finish_timer(const char * name);
#define DEBUG_FINISH(name)
#endif
#define INFO_START(name, msg) \
(SHOW_INFO() ? \
((_log_buffer << msg), start_timer(#name, LOG_INFO)) : ((void)0))
#define INFO_START(name, msg) \
(SHOW_INFO() ? \
((ledger::_log_buffer << msg), \
ledger::start_timer(#name, ledger::LOG_INFO)) : ((void)0))
#define INFO_STOP(name) \
(SHOW_INFO() ? stop_timer(#name) : ((void)0))
#define INFO_FINISH(name) \