removed all uses of std::vector; simplified some code

This commit is contained in:
John Wiegley 2004-08-24 20:01:51 -04:00
parent 2943af2761
commit c6860970f2
6 changed files with 84 additions and 70 deletions

View file

@ -9,13 +9,17 @@
namespace ledger { namespace ledger {
const unsigned long binary_magic_number = 0xFFEED765; const unsigned long binary_magic_number = 0xFFEED765;
static const unsigned long format_version = 0x00020014; static const unsigned long format_version = 0x00020015;
static std::deque<account_t *> accounts; static account_t ** accounts;
static unsigned int account_index; static account_t ** accounts_next;
static std::deque<commodity_t *> commodities; static unsigned int account_index;
static unsigned int commodity_index;
std::deque<amount_t::bigint_t *> bigints; static commodity_t ** commodities;
static commodity_t ** commodities_next;
static unsigned int commodity_index;
std::deque<amount_t::bigint_t *> bigints;
#if DEBUG_LEVEL >= ALPHA #if DEBUG_LEVEL >= ALPHA
#define read_binary_guard(in, id) { \ #define read_binary_guard(in, id) { \
@ -158,10 +162,9 @@ inline void read_binary_entry(std::istream& in, entry_t * entry,
inline commodity_t * read_binary_commodity(std::istream& in) inline commodity_t * read_binary_commodity(std::istream& in)
{ {
commodity_t * commodity = new commodity_t; commodity_t * commodity = new commodity_t;
commodities.push_back(commodity); *commodities_next++ = commodity;
commodity->ident = read_binary_number<commodity_t::ident_t>(in); commodity->ident = read_binary_number<commodity_t::ident_t>(in);
assert(commodity->ident == commodities.size());
read_binary_string(in, commodity->symbol); read_binary_string(in, commodity->symbol);
read_binary_string(in, commodity->name); read_binary_string(in, commodity->name);
@ -189,10 +192,9 @@ inline
account_t * read_binary_account(std::istream& in, account_t * master = NULL) account_t * read_binary_account(std::istream& in, account_t * master = NULL)
{ {
account_t * acct = new account_t(NULL); account_t * acct = new account_t(NULL);
accounts.push_back(acct); *accounts_next++ = acct;
acct->ident = read_binary_number<account_t::ident_t>(in); acct->ident = read_binary_number<account_t::ident_t>(in);
assert(acct->ident == accounts.size());
account_t::ident_t id; account_t::ident_t id;
read_binary_number(in, id); // parent id read_binary_number(in, id); // parent id
@ -254,12 +256,13 @@ unsigned int read_binary_journal(std::istream& in,
} }
} }
account_t::ident_t a_count = read_binary_number<account_t::ident_t>(in);
accounts = accounts_next = new (account_t *)[a_count];
journal->master = read_binary_account(in, master); journal->master = read_binary_account(in, master);
for (account_t::ident_t i = 0, commodity_t::ident_t c_count = read_binary_number<commodity_t::ident_t>(in);
count = read_binary_number<account_t::ident_t>(in); commodities = commodities_next = new (commodity_t *)[c_count];
i < count; for (commodity_t::ident_t i = 0; i < c_count; i++) {
i++) {
commodity_t * commodity = read_binary_commodity(in); commodity_t * commodity = read_binary_commodity(in);
std::pair<commodities_map::iterator, bool> result std::pair<commodities_map::iterator, bool> result
= commodity_t::commodities.insert(commodities_pair(commodity->symbol, = commodity_t::commodities.insert(commodities_pair(commodity->symbol,
@ -299,12 +302,12 @@ unsigned int read_binary_journal(std::istream& in,
journal->item_pool = item_pool; journal->item_pool = item_pool;
journal->item_pool_end = item_pool + pool_size; journal->item_pool_end = item_pool + pool_size;
accounts.clear(); delete[] accounts;
commodities.clear(); delete[] commodities;
bigints.clear();
delete[] string_pool; delete[] string_pool;
bigints.clear();
return count; return count;
} }
@ -418,6 +421,18 @@ void write_binary_commodity(std::ostream& out, commodity_t * commodity)
write_binary_amount(out, commodity->conversion); write_binary_amount(out, commodity->conversion);
} }
static inline account_t::ident_t count_accounts(account_t * account)
{
account_t::ident_t count = 1;
for (accounts_map::iterator i = account->accounts.begin();
i != account->accounts.end();
i++)
count += count_accounts((*i).second);
return count;
}
void write_binary_account(std::ostream& out, account_t * account) void write_binary_account(std::ostream& out, account_t * account)
{ {
account->ident = ++account_index; account->ident = ++account_index;
@ -459,6 +474,7 @@ void write_binary_journal(std::ostream& out, journal_t * journal,
} }
} }
write_binary_number<account_t::ident_t>(out, count_accounts(journal->master));
write_binary_account(out, journal->master); write_binary_account(out, journal->master);
write_binary_number<commodity_t::ident_t>(out, commodity_t::commodities.size() - 1); write_binary_number<commodity_t::ident_t>(out, commodity_t::commodities.size() - 1);

View file

@ -51,7 +51,7 @@ AC_HEADER_STDC
#AC_FUNC_MKTIME #AC_FUNC_MKTIME
#AC_FUNC_STAT #AC_FUNC_STAT
#AC_FUNC_STRFTIME #AC_FUNC_STRFTIME
AC_CHECK_FUNCS([memset strchr strstr]) AC_CHECK_FUNCS([memset strchr strstr access mktime stat strftime])
AC_CONFIG_FILES([Makefile]) AC_CONFIG_FILES([Makefile])
AC_OUTPUT AC_OUTPUT

81
main.cc
View file

@ -21,6 +21,7 @@ using namespace ledger;
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <list>
#include <memory> #include <memory>
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
@ -82,7 +83,7 @@ regexps_to_predicate(std::list<std::string>::const_iterator begin,
const bool account_regexp = false, const bool account_regexp = false,
const bool add_account_short_masks = false) const bool add_account_short_masks = false)
{ {
std::vector<std::string> regexps(2); std::string regexps[2];
// Treat the remaining command-line arguments as regular // Treat the remaining command-line arguments as regular
// expressions, used for refining report results. // expressions, used for refining report results.
@ -106,37 +107,37 @@ regexps_to_predicate(std::list<std::string>::const_iterator begin,
regexps[0] += *i; regexps[0] += *i;
} }
for (std::vector<std::string>::const_iterator i = regexps.begin(); for (int i = 0; i < 2; i++) {
i != regexps.end(); if (regexps[i].empty())
i++) continue;
if (! (*i).empty()) {
if (! config->predicate.empty())
config->predicate += "&";
if (i != regexps.begin()) { if (! config->predicate.empty())
config->predicate += "!"; config->predicate += "&";
}
else if (add_account_short_masks) {
if ((*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 += "///(?:"; if (i == 1) {
config->display_predicate += *i; config->predicate += "!";
config->display_predicate += ")/";
}
}
if (! account_regexp)
config->predicate += "/";
config->predicate += "/(?:";
config->predicate += *i;
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 += ")/";
}
} }
int main(int argc, char * argv[], char * envp[]) int main(int argc, char * argv[], char * envp[])
@ -208,21 +209,23 @@ int main(int argc, char * argv[], char * envp[])
int entry_count = 0; int entry_count = 0;
try { try {
if (! config->init_file.empty()) if (! config->init_file.empty()) {
if (parser_t::parse_file(config->init_file, journal.get())) if (parser_t::parse_file(config->init_file, journal.get()))
throw error("Entries not allowed in initialization file"); throw error("Entries not allowed in initialization file");
journal->sources.pop_front(); // remove init file
}
if (use_cache && ! config->cache_file.empty() && if (use_cache && ! config->cache_file.empty() &&
! config->data_file.empty()) { ! config->data_file.empty()) {
entry_count += parser_t::parse_file(config->cache_file, journal.get(), cache_dirty = true;
NULL, &config->data_file); if (access(config->cache_file.c_str(), R_OK) != -1) {
journal->sources.pop_front(); // remove cache_file std::ifstream stream(config->cache_file.c_str());
if (bin_parser->test(stream)) {
if (entry_count == 0) { entry_count += bin_parser->parse(stream, journal.get(), NULL,
journal.reset(new journal_t); &config->data_file);
cache_dirty = true; if (entry_count > 0)
} else { cache_dirty = false;
cache_dirty = false; }
} }
} }

View file

@ -83,8 +83,7 @@ void process_arguments(int argc, char ** argv, const bool anywhere,
if ((*i)[2] == '\0') if ((*i)[2] == '\0')
break; break;
for (std::vector<option_t>::iterator j for (std::list<option_t>::iterator j = option_handler::options.begin();
= option_handler::options.begin();
j != option_handler::options.end(); j != option_handler::options.end();
j++) j++)
if ((*j).wants_arg) { if ((*j).wants_arg) {
@ -108,8 +107,7 @@ void process_arguments(int argc, char ** argv, const bool anywhere,
std::cerr << "Error: illegal option " << *i << std::endl; std::cerr << "Error: illegal option " << *i << std::endl;
std::exit(1); std::exit(1);
} else { } else {
for (std::vector<option_t>::iterator j for (std::list<option_t>::iterator j = option_handler::options.begin();
= option_handler::options.begin();
j != option_handler::options.end(); j != option_handler::options.end();
j++) j++)
if ((*i)[1] == (*j).short_opt) { if ((*i)[1] == (*j).short_opt) {

View file

@ -3,7 +3,6 @@
#include <map> #include <map>
#include <list> #include <list>
#include <vector>
#include <string> #include <string>
struct option_handler; struct option_handler;
@ -23,8 +22,8 @@ typedef std::pair<const std::string, option_handler *> option_handler_pair;
struct option_handler { struct option_handler {
bool handled; bool handled;
static std::vector<option_t> options; static std::list<option_t> options;
static option_handler_map handlers; static option_handler_map handlers;
option_handler(const std::string& label, option_handler(const std::string& label,
const std::string& opt_chars); const std::string& opt_chars);
@ -37,9 +36,9 @@ void process_arguments(int argc, char ** argv, const bool anywhere,
std::list<std::string>& args); std::list<std::string>& args);
void process_environment(char ** envp, const std::string& tag); void process_environment(char ** envp, const std::string& tag);
#define DEF_OPT_HANDLERS() \ #define DEF_OPT_HANDLERS() \
std::vector<option_t> option_handler::options; \ std::list<option_t> option_handler::options; \
option_handler_map option_handler::handlers option_handler_map option_handler::handlers
#define OPT_BEGIN(tag, chars) \ #define OPT_BEGIN(tag, chars) \
static struct opt_ ## tag ## _handler : public option_handler { \ static struct opt_ ## tag ## _handler : public option_handler { \

View file

@ -6,8 +6,6 @@
#include "debug.h" #include "debug.h"
#include "util.h" #include "util.h"
#include <vector>
#include <pcre.h> #include <pcre.h>
namespace ledger { namespace ledger {