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 {
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 unsigned int account_index;
static std::deque<commodity_t *> commodities;
static unsigned int commodity_index;
std::deque<amount_t::bigint_t *> bigints;
static account_t ** accounts;
static account_t ** accounts_next;
static unsigned int account_index;
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
#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)
{
commodity_t * commodity = new commodity_t;
commodities.push_back(commodity);
*commodities_next++ = commodity;
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->name);
@ -189,10 +192,9 @@ inline
account_t * read_binary_account(std::istream& in, account_t * master = 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);
assert(acct->ident == accounts.size());
account_t::ident_t 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);
for (account_t::ident_t i = 0,
count = read_binary_number<account_t::ident_t>(in);
i < count;
i++) {
commodity_t::ident_t c_count = read_binary_number<commodity_t::ident_t>(in);
commodities = commodities_next = new (commodity_t *)[c_count];
for (commodity_t::ident_t i = 0; i < c_count; i++) {
commodity_t * commodity = read_binary_commodity(in);
std::pair<commodities_map::iterator, bool> result
= 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_end = item_pool + pool_size;
accounts.clear();
commodities.clear();
bigints.clear();
delete[] accounts;
delete[] commodities;
delete[] string_pool;
bigints.clear();
return count;
}
@ -418,6 +421,18 @@ void write_binary_commodity(std::ostream& out, commodity_t * commodity)
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)
{
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_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_STAT
#AC_FUNC_STRFTIME
AC_CHECK_FUNCS([memset strchr strstr])
AC_CHECK_FUNCS([memset strchr strstr access mktime stat strftime])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

81
main.cc
View file

@ -21,6 +21,7 @@ using namespace ledger;
#include <iostream>
#include <fstream>
#include <sstream>
#include <list>
#include <memory>
#include <algorithm>
#include <iterator>
@ -82,7 +83,7 @@ regexps_to_predicate(std::list<std::string>::const_iterator begin,
const bool account_regexp = 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
// expressions, used for refining report results.
@ -106,37 +107,37 @@ regexps_to_predicate(std::list<std::string>::const_iterator begin,
regexps[0] += *i;
}
for (std::vector<std::string>::const_iterator i = regexps.begin();
i != regexps.end();
i++)
if (! (*i).empty()) {
if (! config->predicate.empty())
config->predicate += "&";
for (int i = 0; i < 2; i++) {
if (regexps[i].empty())
continue;
if (i != regexps.begin()) {
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&";
if (! config->predicate.empty())
config->predicate += "&";
config->display_predicate += "///(?:";
config->display_predicate += *i;
config->display_predicate += ")/";
}
}
if (! account_regexp)
config->predicate += "/";
config->predicate += "/(?:";
config->predicate += *i;
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 += ")/";
}
}
int main(int argc, char * argv[], char * envp[])
@ -208,21 +209,23 @@ int main(int argc, char * argv[], char * envp[])
int entry_count = 0;
try {
if (! config->init_file.empty())
if (! config->init_file.empty()) {
if (parser_t::parse_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()) {
entry_count += parser_t::parse_file(config->cache_file, journal.get(),
NULL, &config->data_file);
journal->sources.pop_front(); // remove cache_file
if (entry_count == 0) {
journal.reset(new journal_t);
cache_dirty = true;
} else {
cache_dirty = false;
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;
}
}
}

View file

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

View file

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

View file

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