diff options
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | amount.cc | 8 | ||||
-rw-r--r-- | debug.cc | 2 | ||||
-rw-r--r-- | format.cc | 5 | ||||
-rw-r--r-- | format.h | 13 | ||||
-rw-r--r-- | main.cc | 59 | ||||
-rw-r--r-- | textual.cc | 4 | ||||
-rw-r--r-- | valexpr.cc | 3 | ||||
-rw-r--r-- | walk.h | 1 |
9 files changed, 85 insertions, 16 deletions
@@ -21,9 +21,9 @@ OBJS = $(patsubst %.cc,%.o,$(CODE)) CXX = g++ CFLAGS = -Wall -ansi -pedantic -#DFLAGS = -O3 -fomit-frame-pointer -DRELEASE_LEVEL=0 -DFLAGS = -g -DRELEASE_LEVEL=4 -#DFLAGS = -g -DRELEASE_LEVEL=2 -pg +#DFLAGS = -O3 -fomit-frame-pointer -DRELEASE_LEVEL=0 -DNO_CLEANUP +DFLAGS = -g -DRELEASE_LEVEL=4 -DNO_CLEANUP +#DFLAGS = -g -DRELEASE_LEVEL=2 -pg -DNO_CLEANUP INCS = -I/sw/include \ -I/usr/include/gcc/darwin/3.3/c++ \ @@ -13,19 +13,19 @@ namespace ledger { static mpz_t full_divisor; static mpz_t true_value; -static class init_amounts -{ - public: +static struct init_amounts { init_amounts() { mpz_init(full_divisor); mpz_init(true_value); mpz_ui_pow_ui(full_divisor, 10, MAX_PRECISION); mpz_mul_ui(true_value, full_divisor, 1); } +#ifndef NO_CLEANUP ~init_amounts() { mpz_clear(full_divisor); mpz_clear(true_value); } +#endif } initializer; static void mpz_round(mpz_t out, mpz_t value, int precision) @@ -781,6 +781,7 @@ commodities_map commodity_t::commodities; commodity_t * commodity_t::null_commodity = commodity_t::find_commodity("", true); +#ifndef NO_CLEANUP static struct cleanup_commodities { ~cleanup_commodities() { @@ -794,6 +795,7 @@ static struct cleanup_commodities delete (*i).second; } } _cleanup; +#endif commodity_t * commodity_t::find_commodity(const std::string& symbol, bool auto_create) @@ -90,12 +90,14 @@ static struct init_streams { free_debug_stream = true; } } +#ifndef NO_CLEANUP ~init_streams() { if (free_debug_stream && debug_stream) { delete debug_stream; debug_stream = NULL; } } +#endif } _debug_init; } // namespace ledger @@ -35,8 +35,13 @@ std::string partial_account_name(const account_t * account) std::string format_t::date_format = "%Y/%m/%d"; +#ifdef NO_CLEANUP +value_expr_t * format_t::value_expr = NULL; +value_expr_t * format_t::total_expr = NULL; +#else std::auto_ptr<value_expr_t> format_t::value_expr; std::auto_ptr<value_expr_t> format_t::total_expr; +#endif element_t * format_t::parse_elements(const std::string& fmt) { @@ -54,8 +54,13 @@ struct format_t static std::string date_format; +#ifdef NO_CLEANUP + static value_expr_t * value_expr; + static value_expr_t * total_expr; +#else static std::auto_ptr<value_expr_t> value_expr; static std::auto_ptr<value_expr_t> total_expr; +#endif format_t(const std::string& _format) : elements(NULL) { reset(_format); @@ -75,12 +80,20 @@ struct format_t void format_elements(std::ostream& out, const details_t& details) const; static void compute_value(balance_t& result, const details_t& details) { +#ifdef NO_CLEANUP + if (value_expr) +#else if (value_expr.get()) +#endif value_expr->compute(result, details); } static void compute_total(balance_t& result, const details_t& details) { +#ifdef NO_CLEANUP + if (total_expr) +#else if (total_expr.get()) +#endif total_expr->compute(result, details); } }; @@ -31,6 +31,40 @@ namespace { TIMER_DEF(read_cache, "reading cache file"); } +#ifdef NO_CLEANUP + +#define auto_ptr bogus_auto_ptr + +// This version of auto_ptr does not delete on deconstruction. +namespace std { + template <typename T> + struct bogus_auto_ptr { + T * ptr; + bogus_auto_ptr() : ptr(NULL) {} + explicit bogus_auto_ptr(T * _ptr) : ptr(_ptr) {} + T& operator*() const throw() { + return *ptr; + } + T * operator->() const throw() { + return ptr; + } + T * get() const throw() { return ptr; } + T * release() throw() { + T * tmp = ptr; + ptr = 0; + return tmp; + } + void reset(T * p = 0) throw() { + if (p != ptr) { + delete ptr; + ptr = p; + } + } + }; +} + +#endif // NO_CLEANUP + int main(int argc, char * argv[], char * envp[]) { std::auto_ptr<journal_t> journal(new journal_t); @@ -242,7 +276,11 @@ int main(int argc, char * argv[], char * envp[]) // Setup the meaning of %t and %T, used in format strings try { +#ifdef NO_CLEANUP + format_t::value_expr = parse_value_expr(config->value_expr); +#else format_t::value_expr.reset(parse_value_expr(config->value_expr)); +#endif } catch (const value_expr_error& err) { std::cerr << "Error in amount (-t) specifier: " << err.what() @@ -251,7 +289,11 @@ int main(int argc, char * argv[], char * envp[]) } try { +#ifdef NO_CLEANUP + format_t::total_expr = parse_value_expr(config->total_expr); +#else format_t::total_expr.reset(parse_value_expr(config->total_expr)); +#endif } catch (const value_expr_error& err) { std::cerr << "Error in total (-T) specifier: " << err.what() @@ -321,7 +363,7 @@ int main(int argc, char * argv[], char * envp[]) if (! config->output_file.empty()) output_stream.reset(new std::ofstream(config->output_file.c_str())); -#define OUT() (output_stream.get() ? *output_stream.get() : std::cout) +#define OUT() (output_stream.get() ? *output_stream : std::cout) if (! config->interval_text.empty()) { std::istringstream stream(config->interval_text); @@ -356,12 +398,14 @@ int main(int argc, char * argv[], char * envp[]) show_all_related)); formatter.reset(new filter_transactions(formatter.release(), config->predicate)); - walk_entries(journal->entries, *formatter.get()); + walk_entries(journal->entries, *formatter); + formatter->flush(); format_account acct_formatter(OUT(), format, config->display_predicate); if (config->show_subtotals) sum_accounts(journal->master); walk_accounts(journal->master, acct_formatter, sort_order.get()); + acct_formatter.flush(); if (format_account::disp_subaccounts_p(journal->master)) { std::string end_format = "--------------------\n"; @@ -374,16 +418,19 @@ int main(int argc, char * argv[], char * envp[]) formatter.reset(new add_to_account_value); formatter.reset(new filter_transactions(formatter.release(), config->predicate)); - walk_entries(journal->entries, *formatter.get()); + walk_entries(journal->entries, *formatter); + formatter->flush(); format_equity acct_formatter(OUT(), format, nformat, config->display_predicate); sum_accounts(journal->master); walk_accounts(journal->master, acct_formatter, sort_order.get()); + acct_formatter.flush(); } else if (command == "e") { format_transactions formatter(OUT(), format, nformat); walk_transactions(new_entry->transactions, formatter); + formatter.flush(); } else { std::auto_ptr<item_handler<transaction_t> > formatter; @@ -441,7 +488,7 @@ int main(int argc, char * argv[], char * envp[]) formatter.reset(new subtotal_transactions(formatter.release())); else if (report_interval.get()) formatter.reset(new interval_transactions(formatter.release(), - *report_interval.get(), + *report_interval, interval_begin)); else if (config->days_of_the_week) formatter.reset(new dow_transactions(formatter.release())); @@ -462,7 +509,8 @@ int main(int argc, char * argv[], char * envp[]) // Once the filters are chained, walk `journal's entries and start // feeding each transaction that matches `predicate' to the chain. - walk_entries(journal->entries, *formatter.get()); + walk_entries(journal->entries, *formatter); + formatter->flush(); #ifdef DEBUG_ENABLED // The transaction display flags (dflags) are not recorded in the @@ -470,6 +518,7 @@ int main(int argc, char * argv[], char * envp[]) // are to be displayed a second time. clear_display_flags cleanup; walk_entries(journal->entries, cleanup); + cleanup.flush(); #endif } @@ -366,7 +366,7 @@ unsigned int parse_textual_journal(std::istream& in, journal_t * journal, char * n = next_element(p, true); last_desc = n ? n : ""; - static struct std::tm when; + struct std::tm when; if (strptime(date.c_str(), "%Y/%m/%d %H:%M:%S", &when)) { time_in = std::mktime(&when); last_account = account_stack.front()->find_account(p); @@ -392,7 +392,7 @@ unsigned int parse_textual_journal(std::istream& in, journal_t * journal, date += " "; date += time; - static struct std::tm when; + struct std::tm when; if (strptime(date.c_str(), "%Y/%m/%d %H:%M:%S", &when)) { entry_t * curr = new entry_t; curr->date = std::mktime(&when); @@ -886,8 +886,7 @@ void dump_value_expr(std::ostream& out, const value_expr_t * node) int main(int argc, char *argv[]) { - std::auto_ptr<ledger::value_expr_t> expr(ledger::parse_value_expr(argv[1])); - ledger::dump_value_expr(std::cout, expr.get()); + ledger::dump_value_expr(std::cout, ledger::parse_value_expr(argv[1])); std::cout << std::endl; } @@ -20,7 +20,6 @@ struct item_handler { item_handler(item_handler * _handler) : handler(_handler) {} virtual ~item_handler() {} virtual void close() { - flush(); if (handler) { delete handler; handler = NULL; |