summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.am2
-rw-r--r--amount.cc23
-rw-r--r--binary.cc979
-rw-r--r--binary.h5
-rw-r--r--cache.cc894
-rw-r--r--cache.h140
-rw-r--r--entry.cc16
-rw-r--r--entry.h6
-rw-r--r--journal.cc27
-rw-r--r--journal.h27
-rw-r--r--main.cc4
-rw-r--r--mask.cc25
-rw-r--r--mask.h6
-rw-r--r--session.cc2
-rw-r--r--session.h9
-rw-r--r--value.cc56
-rw-r--r--value.h9
-rw-r--r--walk.cc22
-rw-r--r--walk.h4
-rw-r--r--xact.h15
20 files changed, 1203 insertions, 1068 deletions
diff --git a/Makefile.am b/Makefile.am
index 3a01dd5f..6fe575be 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -72,6 +72,7 @@ libledger_la_SOURCES = \
account.cc \
\
textual.cc \
+ cache.cc \
emacs.cc \
qif.cc \
xml.cc \
@@ -131,6 +132,7 @@ pkginclude_HEADERS = \
account.h \
\
textual.h \
+ cache.h \
emacs.h \
qif.h \
xml.h \
diff --git a/amount.cc b/amount.cc
index 617402fd..c15fbd42 100644
--- a/amount.cc
+++ b/amount.cc
@@ -1243,20 +1243,6 @@ void amount_t::print(std::ostream& _out, bool omit_commodity,
_out << out.str();
}
-
-#if 0
-// jww (2008-07-29): Should these be static?
-namespace {
-#endif
- char * bigints;
- char * bigints_next;
- uint_fast32_t bigints_index;
- uint_fast32_t bigints_count;
- char buf[4096];
-#if 0
-}
-#endif
-
void amount_t::read(std::istream& in)
{
using namespace ledger::binary;
@@ -1285,6 +1271,7 @@ void amount_t::read(std::istream& in)
unsigned short len;
in.read(reinterpret_cast<char *>(&len), sizeof(len));
assert(len < 4096);
+ static char buf[4096];
in.read(buf, len);
mpz_import(MPZ(quantity), len / sizeof(short), 1, sizeof(short),
0, 0, buf);
@@ -1328,8 +1315,10 @@ void amount_t::read(const char *& data)
if (byte < 3) {
if (byte == 2) {
+#if 0
quantity = new(reinterpret_cast<bigint_t *>(bigints_next)) bigint_t;
bigints_next += sizeof(bigint_t);
+#endif
} else {
quantity = new bigint_t;
}
@@ -1353,10 +1342,12 @@ void amount_t::read(const char *& data)
if (byte == 2)
quantity->add_flags(BIGINT_BULK_ALLOC);
} else {
+#if 0
uint_fast32_t index = *reinterpret_cast<uint_fast32_t *>(const_cast<char *>(data));
data += sizeof(uint_fast32_t);
quantity = reinterpret_cast<bigint_t *>(bigints + (index - 1) * sizeof(bigint_t));
+#endif
DEBUG("amounts.refs",
quantity << " ref++, now " << (quantity->ref + 1));
quantity->ref++;
@@ -1383,15 +1374,18 @@ void amount_t::write(std::ostream& out, bool optimized) const
if (! optimized || quantity->index == 0) {
if (optimized) {
+#if 0
quantity->index = ++bigints_index; // if !optimized, this is garbage
bigints_count++;
byte = 2;
+#endif
} else {
byte = 1;
}
out.write(&byte, sizeof(byte));
std::size_t size;
+ static char buf[4096];
mpz_export(buf, &size, 1, sizeof(short), 0, 0, MPZ(quantity));
unsigned short len = size * sizeof(short);
out.write(reinterpret_cast<char *>(&len), sizeof(len));
@@ -1418,7 +1412,6 @@ void amount_t::write(std::ostream& out, bool optimized) const
}
}
-
bool amount_t::valid() const
{
if (quantity) {
diff --git a/binary.cc b/binary.cc
index b9ec7142..1744bb5d 100644
--- a/binary.cc
+++ b/binary.cc
@@ -30,62 +30,8 @@
*/
#include "binary.h"
-#include "journal.h"
-#include "session.h"
namespace ledger {
-
-static unsigned long binary_magic_number = 0xFFEED765;
-#ifdef DEBUG_ENABLED
-static unsigned long format_version = 0x00020701;
-#else
-static unsigned long format_version = 0x00020700;
-#endif
-
-static account_t ** accounts;
-static account_t ** accounts_next;
-static unsigned int account_index;
-
-static commodity_t::base_t ** base_commodities;
-static commodity_t::base_t ** base_commodities_next;
-static unsigned int base_commodity_index;
-
-static commodity_t ** commodities;
-static commodity_t ** commodities_next;
-static unsigned int commodity_index;
-
-extern char * bigints;
-extern char * bigints_next;
-extern unsigned int bigints_index;
-extern unsigned int bigints_count;
-
-bool journal_t::binary_parser_t::test(std::istream& in) const
-{
- if (binary::read_number_nocheck<unsigned long>(in) == binary_magic_number &&
- binary::read_number_nocheck<unsigned long>(in) == format_version)
- return true;
-
- in.clear();
- in.seekg(0, std::ios::beg);
- return false;
-}
-
-namespace binary {
- unsigned int read_journal(std::istream& in,
- const path& file,
- journal_t& journal,
- account_t * master);
-}
-
-unsigned int journal_t::binary_parser_t::parse(std::istream& in,
- session_t& session,
- journal_t& journal,
- account_t * master,
- const path * original_file)
-{
- return journal.read(in, original_file ? *original_file : "", master);
-}
-
namespace binary {
void read_bool(std::istream& in, bool& num)
@@ -201,7 +147,6 @@ void read_string(const char *& data, optional<string>& str)
}
}
-
void write_bool(std::ostream& out, bool num)
{
write_guard(out, 0x2005);
@@ -239,929 +184,5 @@ void write_string(std::ostream& out, const optional<string>& str)
}
}
-inline void read_amount(const char *& data, amount_t& amt)
-{
- commodity_t::ident_t ident;
- read_long(data, ident);
- if (ident == 0xffffffff)
- amt.commodity_ = NULL;
- else if (ident == 0)
- amt.commodity_ = amount_t::current_pool->null_commodity;
- else
- amt.commodity_ = commodities[ident - 1];
-
- amt.read(data);
-}
-
-inline void read_value(const char *& data, value_t& val)
-{
- switch (static_cast<value_t::type_t>(read_long<int>(data))) {
- case value_t::BOOLEAN:
- val.set_boolean(read_bool(data));
- break;
- case value_t::INTEGER:
- val.set_long(read_number<unsigned long>(data));
- break;
- case value_t::DATETIME:
- // jww (2008-04-22): I need to record and read a datetime_t directly
- //val.set_datetime(read_long<unsigned long>(data));
- break;
- case value_t::AMOUNT: {
- amount_t temp;
- read_amount(data, temp);
- val.set_amount(temp);
- break;
- }
-
- //case value_t::BALANCE:
- //case value_t::BALANCE_PAIR:
- default:
- assert(false);
- break;
- }
-}
-
-inline void read_mask(const char *& data, mask_t& mask)
-{
- bool exclude;
- read_number(data, exclude);
- string pattern;
- read_string(data, pattern);
-
- mask = mask_t(pattern);
- mask.exclude = exclude;
-}
-
-inline void read_xact(const char *& data, xact_t * xact)
-{
- read_number(data, xact->_date);
- read_number(data, xact->_date_eff);
- xact->account = accounts[read_long<account_t::ident_t>(data) - 1];
-
- unsigned char flag = read_number<unsigned char>(data);
- if (flag == 0) {
- read_amount(data, xact->amount);
- }
- else if (flag == 1) {
- read_amount(data, xact->amount);
- string str;
- read_string(data, str);
- xact->amount_expr->set_text(str);
- }
- else {
- xact->amount_expr->read(data);
- }
-
- if (read_bool(data)) {
- xact->cost = amount_t();
- read_amount(data, *xact->cost);
-
- xact->cost_expr = expr_t();
- xact->cost_expr->read(data);
- } else {
- xact->cost = none;
- }
-
- read_number(data, xact->state);
- xact->set_flags(read_number<xact_t::flags_t>(data));
- xact->add_flags(XACT_BULK_ALLOC);
- read_string(data, xact->note);
-
- xact->beg_pos = read_long<unsigned long>(data);
- read_long(data, xact->beg_line);
- xact->end_pos = read_long<unsigned long>(data);
- read_long(data, xact->end_line);
-
- xact->data = NULL;
-
-#if 0
- if (xact->amount_expr)
- expr_t::compute_amount(xact->amount_expr.get(), xact->amount, xact);
-#endif
-}
-
-inline void read_entry_base(const char *& data, entry_base_t * entry,
- xact_t *& xact_pool, bool& finalize)
-{
- read_long(data, entry->src_idx);
- entry->beg_pos = read_long<unsigned long>(data);
- read_long(data, entry->beg_line);
- entry->end_pos = read_long<unsigned long>(data);
- read_long(data, entry->end_line);
-
- bool ignore_calculated = read_bool(data);
-
- for (unsigned long i = 0, count = read_long<unsigned long>(data);
- i < count;
- i++) {
- new(xact_pool) xact_t;
- read_xact(data, xact_pool);
- if (ignore_calculated && xact_pool->has_flags(XACT_CALCULATED))
- finalize = true;
- entry->add_xact(xact_pool++);
- }
-}
-
-inline void read_entry(const char *& data, entry_t * entry,
- xact_t *& xact_pool, bool& finalize)
-{
- read_entry_base(data, entry, xact_pool, finalize);
- read_number(data, entry->_date);
- read_number(data, entry->_date_eff);
- read_string(data, entry->code);
- read_string(data, entry->payee);
-}
-
-inline void read_auto_entry(const char *& data, auto_entry_t * entry,
- xact_t *& xact_pool)
-{
- bool ignore;
- read_entry_base(data, entry, xact_pool, ignore);
-
- expr_t expr;
- expr.read(data);
- entry->predicate = item_predicate<xact_t>(expr);
-}
-
-inline void read_period_entry(const char *& data, period_entry_t * entry,
- xact_t *& xact_pool, bool& finalize)
-{
- read_entry_base(data, entry, xact_pool, finalize);
- read_string(data, &entry->period_string);
- std::istringstream stream(entry->period_string);
- entry->period.parse(stream);
-}
-
-inline commodity_t::base_t * read_commodity_base(const char *& data)
-{
- string str;
-
- read_string(data, str);
-
- std::auto_ptr<commodity_t::base_t> commodity(new commodity_t::base_t(str));
-
- read_string(data, str);
- if (! str.empty())
- commodity->name = str;
-
- read_string(data, str);
- if (! str.empty())
- commodity->note = str;
-
- read_number(data, commodity->precision);
- unsigned long flags;
- read_number(data, flags);
- commodity->set_flags(flags);
-
- return *base_commodities_next++ = commodity.release();
-}
-
-inline void read_commodity_base_extra(const char *& data,
- commodity_t::ident_t ident)
-{
- commodity_t::base_t * commodity = base_commodities[ident];
-
- bool read_history = false;
- for (unsigned long i = 0, count = read_long<unsigned long>(data);
- i < count;
- i++) {
- datetime_t when;
- read_number(data, when);
- amount_t amt;
- read_amount(data, amt);
-
- // Upon insertion, amt will be copied, which will cause the amount
- // to be duplicated (and thus not lost when the journal's
- // item_pool is deleted).
- if (! commodity->history)
- commodity->history = commodity_t::history_t();
- commodity->history->prices.insert(commodity_t::base_t::history_pair(when, amt));
-
- read_history = true;
- }
- if (read_history)
- read_number(data, commodity->history->last_lookup);
-
- if (read_bool(data)) {
- amount_t amt;
- read_amount(data, amt);
- commodity->smaller = amount_t(amt);
- }
-
- if (read_bool(data)) {
- amount_t amt;
- read_amount(data, amt);
- commodity->larger = amount_t(amt);
- }
-}
-
-inline commodity_t * read_commodity(const char *& data)
-{
- commodity_t::base_t * base =
- base_commodities[read_long<commodity_t::ident_t>(data) - 1];
-
- commodity_t * commodity =
- new commodity_t(amount_t::current_pool,
- shared_ptr<commodity_t::base_t>(base));
-
- *commodities_next++ = commodity;
-
- string str;
- read_string(data, str);
- if (! str.empty())
- commodity->qualified_symbol = str;
- commodity->annotated = false;
-
- return commodity;
-}
-
-inline commodity_t * read_commodity_annotated(const char *& data)
-{
- commodity_t * commodity =
- commodities[read_long<commodity_t::ident_t>(data) - 1];
-
- annotation_t details;
-
- string str;
- read_string(data, str);
-
- // This read-and-then-assign causes a new amount to be allocated which does
- // not live within the bulk allocation pool, since that pool will be deleted
- // *before* the commodities are destroyed.
- amount_t amt;
- read_amount(data, amt);
- details.price = amt;
-
-#if 0
- // jww (2008-04-22): These are optional members!
- read_number(data, details.date);
- read_string(data, details.tag);
-#endif
-
- annotated_commodity_t * ann_comm =
- new annotated_commodity_t(commodity, details);
- *commodities_next++ = ann_comm;
-
- if (! str.empty())
- ann_comm->qualified_symbol = str;
-
- return ann_comm;
-}
-
-inline
-account_t * read_account(const char *& data, journal_t& journal,
- account_t * master = NULL)
-{
- account_t * acct = new account_t(NULL);
- *accounts_next++ = acct;
-
- account_t::ident_t id;
- read_long(data, id); // parent id
- if (id == 0xffffffff)
- acct->parent = NULL;
- else
- acct->parent = accounts[id - 1];
-
- read_string(data, acct->name);
- read_string(data, acct->note);
- read_number(data, acct->depth);
-
- // If all of the subaccounts will be added to a different master
- // account, throw away what we've learned about the recorded
- // journal's own master account.
-
- if (master && acct != master) {
- checked_delete(acct);
- acct = master;
- }
-
- for (account_t::ident_t i = 0,
- count = read_long<account_t::ident_t>(data);
- i < count;
- i++) {
- account_t * child = read_account(data, journal);
- child->parent = acct;
- assert(acct != child);
- acct->add_account(child);
- }
-
- return acct;
-}
-
-void write_amount(std::ostream& out, const amount_t& amt)
-{
- if (amt.commodity_)
- write_long(out, amt.commodity_->ident);
- else
- write_long<commodity_t::ident_t>(out, 0xffffffff);
-
- amt.write(out);
-}
-
-void write_value(std::ostream& out, const value_t& val)
-{
- write_long(out, static_cast<int>(val.type()));
-
- switch (val.type()) {
- case value_t::BOOLEAN:
- write_bool(out, val.as_boolean());
- break;
- case value_t::INTEGER:
- write_long(out, val.as_long());
- break;
- case value_t::DATETIME:
- write_number(out,val.as_datetime());
- break;
- case value_t::AMOUNT:
- write_amount(out, val.as_amount());
- break;
-
- //case value_t::BALANCE:
- //case value_t::BALANCE_PAIR:
- default:
- throw new error("Cannot write a balance to the binary cache");
- }
-}
-
-void write_mask(std::ostream& out, mask_t& mask)
-{
- write_number(out, mask.exclude);
- write_string(out, mask.expr.str());
-}
-
-void write_xact(std::ostream& out, xact_t * xact,
- bool ignore_calculated)
-{
- write_number(out, xact->_date);
- write_number(out, xact->_date_eff);
- write_long(out, xact->account->ident);
-
- if (ignore_calculated && xact->has_flags(XACT_CALCULATED)) {
- write_number<unsigned char>(out, 0);
- write_amount(out, amount_t());
- }
- else if (xact->amount_expr) {
- write_number<unsigned char>(out, 2);
- xact->amount_expr->write(out);
- }
- else if (! xact->amount_expr->text().empty()) {
- write_number<unsigned char>(out, 1);
- write_amount(out, xact->amount);
- write_string(out, xact->amount_expr->text());
- }
- else {
- write_number<unsigned char>(out, 0);
- write_amount(out, xact->amount);
- }
-
- if (xact->cost &&
- (! (ignore_calculated && xact->has_flags(XACT_CALCULATED)))) {
- write_bool(out, true);
- write_amount(out, *xact->cost);
- xact->cost_expr->write(out);
- } else {
- write_bool(out, false);
- }
-
- write_number(out, xact->state);
- write_number(out, xact->flags());
- write_string(out, xact->note);
-
- write_long(out, xact->beg_pos);
- write_long(out, xact->beg_line);
- write_long(out, xact->end_pos);
- write_long(out, xact->end_line);
-}
-
-void write_entry_base(std::ostream& out, entry_base_t * entry)
-{
- write_long(out, entry->src_idx);
- write_long(out, entry->beg_pos);
- write_long(out, entry->beg_line);
- write_long(out, entry->end_pos);
- write_long(out, entry->end_line);
-
- bool ignore_calculated = false;
- for (xacts_list::const_iterator i = entry->xacts.begin();
- i != entry->xacts.end();
- i++)
- if ((*i)->amount_expr) {
- ignore_calculated = true;
- break;
- }
-
- write_bool(out, ignore_calculated);
-
- write_long(out, entry->xacts.size());
- for (xacts_list::const_iterator i = entry->xacts.begin();
- i != entry->xacts.end();
- i++)
- write_xact(out, *i, ignore_calculated);
-}
-
-void write_entry(std::ostream& out, entry_t * entry)
-{
- write_entry_base(out, entry);
- write_number(out, entry->_date);
- write_number(out, entry->_date_eff);
- write_string(out, entry->code);
- write_string(out, entry->payee);
-}
-
-void write_auto_entry(std::ostream& out, auto_entry_t * entry)
-{
- write_entry_base(out, entry);
- entry->predicate.predicate.write(out);
-}
-
-void write_period_entry(std::ostream& out, period_entry_t * entry)
-{
- write_entry_base(out, entry);
- write_string(out, entry->period_string);
-}
-
-void write_commodity_base(std::ostream& out, commodity_t::base_t * commodity)
-{
- // jww (2008-04-22): Not using this anymore?
- //commodity->ident = ++base_commodity_index;
-
- write_string(out, commodity->symbol);
- // jww (2008-04-22): What to do with optional members?
- write_string(out, *commodity->name);
- write_string(out, *commodity->note);
- write_number(out, commodity->precision);
- write_number(out, commodity->flags());
-}
-
-void write_commodity_base_extra(std::ostream& out,
- commodity_t::base_t * commodity)
-{
-#if 0
- // jww (2008-04-22): What did bogus_time used to do?
- if (commodity->history && commodity->history->bogus_time)
- commodity->remove_price(commodity->history->bogus_time);
-#endif
-
- if (! commodity->history) {
- write_long<unsigned long>(out, 0);
- } else {
- write_long<unsigned long>(out, commodity->history->prices.size());
- for (commodity_t::history_map::const_iterator
- i = commodity->history->prices.begin();
- i != commodity->history->prices.end();
- i++) {
- write_number(out, (*i).first);
- write_amount(out, (*i).second);
- }
- write_number(out, commodity->history->last_lookup);
- }
-
- if (commodity->smaller) {
- write_bool(out, true);
- write_amount(out, *commodity->smaller);
- } else {
- write_bool(out, false);
- }
-
- if (commodity->larger) {
- write_bool(out, true);
- write_amount(out, *commodity->larger);
- } else {
- write_bool(out, false);
- }
-}
-
-void write_commodity(std::ostream& out, commodity_t * commodity)
-{
- commodity->ident = ++commodity_index;
-
- // jww (2008-04-22): Is this used anymore?
- //write_long(out, commodity->base->ident);
- // jww (2008-04-22): Optional!
- write_string(out, *commodity->qualified_symbol);
-}
-
-void write_commodity_annotated(std::ostream& out,
- commodity_t * commodity)
-{
- commodity->ident = ++commodity_index;
-
- // jww (2008-04-22): No longer needed?
- //write_long(out, commodity->base->ident);
- // jww (2008-04-22): Optional!
- write_string(out, *commodity->qualified_symbol);
-
- annotated_commodity_t * ann_comm =
- static_cast<annotated_commodity_t *>(commodity);
-
- // jww (2008-04-22): No longer needed?
- //write_long(out, ann_comm->base->ident);
- // jww (2008-04-22): Make a write_annotation_details function; and optional!
- write_amount(out, *ann_comm->details.price);
- write_number(out, *ann_comm->details.date);
- write_string(out, *ann_comm->details.tag);
-}
-
-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_account(std::ostream& out, account_t * account)
-{
- account->ident = ++account_index;
-
- if (account->parent)
- write_long(out, account->parent->ident);
- else
- write_long<account_t::ident_t>(out, 0xffffffff);
-
- write_string(out, account->name);
- write_string(out, account->note);
- write_number(out, account->depth);
-
- write_long<account_t::ident_t>(out, account->accounts.size());
- for (accounts_map::iterator i = account->accounts.begin();
- i != account->accounts.end();
- i++)
- write_account(out, (*i).second);
-}
-
} // namespace binary
-
-unsigned int journal_t::read(std::istream& in,
- const path& file,
- account_t * master)
-{
- using namespace binary;
-
- account_index =
- base_commodity_index =
- commodity_index = 0;
-
- // Read in the files that participated in this journal, so that they
- // can be checked for changes on reading.
-
- if (! file.empty()) {
- for (unsigned short i = 0,
- count = read_number<unsigned short>(in);
- i < count;
- i++) {
- path pathname = read_string(in);
- std::time_t old_mtime;
- read_number(in, old_mtime);
- struct stat info;
- // jww (2008-04-22): can this be done differently now?
- stat(pathname.string().c_str(), &info);
- if (std::difftime(info.st_mtime, old_mtime) > 0)
- return 0;
-
- sources.push_back(pathname);
- }
-
- // Make sure that the cache uses the same price database,
- // otherwise it means that LEDGER_PRICE_DB has been changed, and
- // we should ignore this cache file.
- if (read_bool(in)) {
- string pathname;
- read_string(in, pathname);
- if (! price_db ||
- price_db->string() != std::string(pathname))
- return 0;
- }
- }
-
- // Read all of the data in at once, so that we're just dealing with
- // a big data buffer.
-
- unsigned long data_size = read_number<unsigned long>(in);
-
- char * data_pool = new char[data_size];
- in.read(data_pool, data_size);
-
- // Read in the accounts
-
- const char * data = data_pool;
-
- account_t::ident_t a_count = read_long<account_t::ident_t>(data);
- accounts = accounts_next = new account_t *[a_count];
-
- // jww (2008-07-29): Does this still apply?
- assert(owner->master);
- checked_delete(owner->master);
- owner->master = read_account(data, *this, master);
-
- if (read_bool(data))
- basket = accounts[read_long<account_t::ident_t>(data) - 1];
-
- // Allocate the memory needed for the entries and xacts in
- // one large block, which is then chopped up and custom constructed
- // as necessary.
-
- unsigned long count = read_long<unsigned long>(data);
- unsigned long auto_count = read_long<unsigned long>(data);
- unsigned long period_count = read_long<unsigned long>(data);
- unsigned long xact_count = read_number<unsigned long>(data);
- unsigned long bigint_count = read_number<unsigned long>(data);
-
- std::size_t pool_size = (sizeof(entry_t) * count +
- sizeof(xact_t) * xact_count +
- amount_t::sizeof_bigint_t() * bigint_count);
-
- char * item_pool = new char[pool_size];
-
- item_pool = item_pool;
- item_pool_end = item_pool + pool_size;
-
- entry_t * entry_pool = reinterpret_cast<entry_t *>(item_pool);
- xact_t * xact_pool = reinterpret_cast<xact_t *>(item_pool +
- (sizeof(entry_t) * count));
- bigints_index = 0;
- bigints = bigints_next = (item_pool + sizeof(entry_t) * count +
- sizeof(xact_t) * xact_count);
-
- // Read in the base commodities and then derived commodities
-
- commodity_t::ident_t bc_count = read_long<commodity_t::ident_t>(data);
- base_commodities = base_commodities_next = new commodity_t::base_t *[bc_count];
-
- for (commodity_t::ident_t i = 0; i < bc_count; i++) {
-#if 0
- commodity_t::base_t * base = read_commodity_base(data);
-
- // jww (2008-04-22): How does the pool get created here?
- amount_t::current_pool->commodities.push_back(commodity);
-
- // jww (2008-04-22): What about this logic here?
- if (! result.second) {
- base_commodities_map::iterator c =
- commodity_t::base_t::commodities.find(commodity->symbol);
-
- // It's possible the user might have used a commodity in a value
- // expression passed to an option, we'll just override the
- // flags, but keep the commodity pointer intact.
- if (c == commodity_t::base_t::commodities.end())
- throw new error(string("Failed to read base commodity from cache: ") +
- commodity->symbol);
-
- (*c).second->name = commodity->name;
- (*c).second->note = commodity->note;
- (*c).second->precision = commodity->precision;
- (*c).second->flags = commodity->flags;
- if ((*c).second->smaller)
- checked_delete((*c).second->smaller);
- (*c).second->smaller = commodity->smaller;
- if ((*c).second->larger)
- checked_delete((*c).second->larger);
- (*c).second->larger = commodity->larger;
-
- *(base_commodities_next - 1) = (*c).second;
- checked_delete(commodity);
- }
-#endif
- }
-
- commodity_t::ident_t c_count = read_long<commodity_t::ident_t>(data);
- commodities = commodities_next = new commodity_t *[c_count];
-
- for (commodity_t::ident_t i = 0; i < c_count; i++) {
- commodity_t * commodity;
- string mapping_key;
-
- if (! read_bool(data)) {
- commodity = read_commodity(data);
- mapping_key = commodity->base->symbol;
- } else {
- read_string(data, mapping_key);
- commodity = read_commodity_annotated(data);
- }
-
- // jww (2008-04-22): What do I do with mapping_key here?
- amount_t::current_pool->commodities.push_back(commodity);
-#if 0
- // jww (2008-04-22): What about the error case?
- if (! result.second) {
- commodities_map::iterator c =
- commodity_t::commodities.find(mapping_key);
- if (c == commodity_t::commodities.end())
- throw new error(string("Failed to read commodity from cache: ") +
- commodity->symbol());
-
- *(commodities_next - 1) = (*c).second;
- checked_delete(commodity);
- }
-#endif
- }
-
- for (commodity_t::ident_t i = 0; i < bc_count; i++)
- read_commodity_base_extra(data, i);
-
- commodity_t::ident_t ident;
- read_long(data, ident);
- if (ident == 0xffffffff || ident == 0)
- amount_t::current_pool->default_commodity = NULL;
- else
- amount_t::current_pool->default_commodity = commodities[ident - 1];
-
- // Read in the entries and xacts
-
- for (unsigned long i = 0; i < count; i++) {
- new(entry_pool) entry_t;
- bool finalize = false;
- read_entry(data, entry_pool, xact_pool, finalize);
- entry_pool->journal = this;
- if (finalize && ! entry_pool->finalize())
- continue;
- entries.push_back(entry_pool++);
- }
-
- for (unsigned long i = 0; i < auto_count; i++) {
- auto_entry_t * auto_entry = new auto_entry_t;
- read_auto_entry(data, auto_entry, xact_pool);
- auto_entry->journal = this;
- auto_entries.push_back(auto_entry);
- }
-
- for (unsigned long i = 0; i < period_count; i++) {
- period_entry_t * period_entry = new period_entry_t;
- bool finalize = false;
- read_period_entry(data, period_entry, xact_pool, finalize);
- period_entry->journal = this;
- if (finalize && ! period_entry->finalize())
- continue;
- period_entries.push_back(period_entry);
- }
-
- // Clean up and return the number of entries read
-
- checked_array_delete(accounts);
- checked_array_delete(commodities);
- checked_array_delete(data_pool);
-
- VERIFY(valid());
-
- return count;
-}
-
-void journal_t::write(std::ostream& out)
-{
- using namespace binary;
-
- account_index =
- base_commodity_index =
- commodity_index = 0;
-
- write_number_nocheck(out, binary_magic_number);
- write_number_nocheck(out, format_version);
-
- // Write out the files that participated in this journal, so that
- // they can be checked for changes on reading.
-
- if (sources.empty()) {
- write_number<unsigned short>(out, 0);
- } else {
- write_number<unsigned short>(out, sources.size());
- for (paths_list::const_iterator i = sources.begin();
- i != sources.end();
- i++) {
- write_string(out, (*i).string());
- struct stat info;
- stat((*i).string().c_str(), &info);
- write_number(out, std::time_t(info.st_mtime));
- }
-
- // Write out the price database that relates to this data file, so
- // that if it ever changes the cache can be invalidated.
- if (price_db) {
- write_bool(out, true);
- write_string(out, price_db->string());
- } else {
- write_bool(out, false);
- }
- }
-
- ostream_pos_type data_val = out.tellp();
- write_number<unsigned long>(out, 0);
-
- // Write out the accounts
-
- write_long<account_t::ident_t>(out, count_accounts(master));
- write_account(out, master);
-
- if (basket) {
- write_bool(out, true);
- write_long(out, basket->ident);
- } else {
- write_bool(out, false);
- }
-
- // Write out the number of entries, xacts, and amounts
-
- write_long<unsigned long>(out, entries.size());
- write_long<unsigned long>(out, auto_entries.size());
- write_long<unsigned long>(out, period_entries.size());
-
- ostream_pos_type xacts_val = out.tellp();
- write_number<unsigned long>(out, 0);
-
- ostream_pos_type bigints_val = out.tellp();
- write_number<unsigned long>(out, 0);
-
- bigints_count = 0;
-
- // Write out the commodities
- // jww (2008-04-22): This whole section needs to be reworked
-
-#if 0
- write_long<commodity_t::ident_t>(out, amount_t::current_pool->commodities.size());
-
- for (base_commodities_map::const_iterator i =
- commodity_t::base_t::commodities.begin();
- i != commodity_t::base_t::commodities.end();
- i++)
- write_commodity_base(out, (*i).second);
-
- write_long<commodity_t::ident_t>
- (out, commodity_t::commodities.size());
-
- for (commodities_map::const_iterator i = commodity_t::commodities.begin();
- i != commodity_t::commodities.end();
- i++) {
- if (! (*i).second->annotated) {
- write_bool(out, false);
- write_commodity(out, (*i).second);
- }
- }
-
- for (commodities_map::const_iterator i = commodity_t::commodities.begin();
- i != commodity_t::commodities.end();
- i++) {
- if ((*i).second->annotated) {
- write_bool(out, true);
- write_string(out, (*i).first); // the mapping key
- write_commodity_annotated(out, (*i).second);
- }
- }
-
- // Write out the history and smaller/larger convertible links after
- // both the base and the main commodities have been written, since
- // the amounts in both will refer to the mains.
-
- for (base_commodities_map::const_iterator i =
- commodity_t::base_t::commodities.begin();
- i != commodity_t::base_t::commodities.end();
- i++)
- write_commodity_base_extra(out, (*i).second);
-
- if (commodity_t::default_commodity)
- write_long(out, commodity_t::default_commodity->ident);
- else
- write_long<commodity_t::ident_t>(out, 0xffffffff);
-#endif
-
- // Write out the entries and xacts
-
- unsigned long xact_count = 0;
-
- for (entries_list::const_iterator i = entries.begin();
- i != entries.end();
- i++) {
- write_entry(out, *i);
- xact_count += (*i)->xacts.size();
- }
-
- for (auto_entries_list::const_iterator i = auto_entries.begin();
- i != auto_entries.end();
- i++) {
- write_auto_entry(out, *i);
- xact_count += (*i)->xacts.size();
- }
-
- for (period_entries_list::const_iterator i = period_entries.begin();
- i != period_entries.end();
- i++) {
- write_period_entry(out, *i);
- xact_count += (*i)->xacts.size();
- }
-
- // Back-patch the count for amounts
-
- unsigned long data_size = (static_cast<unsigned long>(out.tellp()) -
- static_cast<unsigned long>(data_val) -
- sizeof(unsigned long));
- out.seekp(data_val);
- write_number<unsigned long>(out, data_size);
- out.seekp(xacts_val);
- write_number<unsigned long>(out, xact_count);
- out.seekp(bigints_val);
- write_number<unsigned long>(out, bigints_count);
-}
-
} // namespace ledger
diff --git a/binary.h b/binary.h
index 1c09a27b..8a2ef681 100644
--- a/binary.h
+++ b/binary.h
@@ -264,11 +264,6 @@ void write_long(std::ostream& out, T num)
void write_string(std::ostream& out, const string& str);
void write_string(std::ostream& out, const optional<string>& str);
-template <typename T>
-inline void write_object(std::ostream& out, const T& journal) {
- assert(false);
-}
-
} // namespace binary
} // namespace ledger
diff --git a/cache.cc b/cache.cc
new file mode 100644
index 00000000..c04bcf1c
--- /dev/null
+++ b/cache.cc
@@ -0,0 +1,894 @@
+/*
+ * Copyright (c) 2003-2008, John Wiegley. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of New Artisans LLC nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "cache.h"
+#include "binary.h"
+
+namespace ledger {
+
+using namespace binary;
+
+#if 0
+void read_xact(const char *& data, xact_t * xact)
+{
+ read_number(data, xact->_date);
+ read_number(data, xact->_date_eff);
+ xact->account = accounts[read_long<account_t::ident_t>(data) - 1];
+
+ unsigned char flag = read_number<unsigned char>(data);
+ if (flag == 0) {
+ xact->amount.read(data);
+ }
+ else if (flag == 1) {
+ xact->amount.read(data);
+ xact->amount_expr = expr_t();
+ xact->amount_expr->set_text(read_string(data));
+ }
+ else {
+ xact->amount_expr = expr_t();
+ xact->amount_expr->read(data);
+ }
+
+ if (read_bool(data)) {
+ xact->cost = amount_t();
+ xact->cost->read(data);
+
+ xact->cost_expr = expr_t();
+ xact->cost_expr->read(data);
+ } else {
+ xact->cost = none;
+ }
+
+ read_number(data, xact->state);
+ xact->set_flags(read_number<xact_t::flags_t>(data));
+ xact->add_flags(XACT_BULK_ALLOC);
+ read_string(data, xact->note);
+
+ xact->beg_pos = read_long<unsigned long>(data);
+ read_long(data, xact->beg_line);
+ xact->end_pos = read_long<unsigned long>(data);
+ read_long(data, xact->end_line);
+
+ xact->data = NULL;
+
+ if (xact->amount_expr)
+ expr_t::compute_amount(xact->amount_expr.get(), xact->amount, xact);
+}
+
+void write_xact(std::ostream& out, xact_t * xact,
+ bool ignore_calculated)
+{
+ write_number(out, xact->_date);
+ write_number(out, xact->_date_eff);
+ write_long(out, xact->account->ident);
+
+ if (ignore_calculated && xact->has_flags(XACT_CALCULATED)) {
+ write_number<unsigned char>(out, 0);
+ amount_t().write(out);
+ }
+ else if (xact->amount_expr) {
+ write_number<unsigned char>(out, 2);
+ // jww (2008-07-30): Um, is this right?
+ xact->amount_expr->write(out);
+ }
+ else if (! xact->amount_expr->text().empty()) {
+ write_number<unsigned char>(out, 1);
+ xact->amount.write(out);
+ write_string(out, xact->amount_expr->text());
+ }
+ else {
+ write_number<unsigned char>(out, 0);
+ xact->amount.write(out);
+ }
+
+ if (xact->cost &&
+ (! (ignore_calculated && xact->has_flags(XACT_CALCULATED)))) {
+ write_bool(out, true);
+ xact->cost->write(out);
+ // jww (2008-07-30): What if there is no cost expression?
+ xact->cost_expr->write(out);
+ } else {
+ write_bool(out, false);
+ }
+
+ write_number(out, xact->state);
+ write_number(out, xact->flags());
+ write_string(out, xact->note);
+
+ write_long(out, xact->beg_pos);
+ write_long(out, xact->beg_line);
+ write_long(out, xact->end_pos);
+ write_long(out, xact->end_line);
+}
+
+void read_entry_base(const char *& data, entry_base_t * entry,
+ xact_t *& xact_pool, bool& finalize)
+{
+ read_long(data, entry->src_idx);
+ entry->beg_pos = read_long<unsigned long>(data);
+ read_long(data, entry->beg_line);
+ entry->end_pos = read_long<unsigned long>(data);
+ read_long(data, entry->end_line);
+
+ bool ignore_calculated = read_bool(data);
+
+ for (unsigned long i = 0, count = read_long<unsigned long>(data);
+ i < count;
+ i++) {
+ new(xact_pool) xact_t;
+ read_xact(data, xact_pool);
+ if (ignore_calculated && xact_pool->has_flags(XACT_CALCULATED))
+ finalize = true;
+ entry->add_xact(xact_pool++);
+ }
+}
+
+void write_entry_base(std::ostream& out, entry_base_t * entry)
+{
+ write_long(out, entry->src_idx);
+ write_long(out, entry->beg_pos);
+ write_long(out, entry->beg_line);
+ write_long(out, entry->end_pos);
+ write_long(out, entry->end_line);
+
+ bool ignore_calculated = false;
+ for (xacts_list::const_iterator i = entry->xacts.begin();
+ i != entry->xacts.end();
+ i++)
+ if ((*i)->amount_expr) {
+ ignore_calculated = true;
+ break;
+ }
+
+ write_bool(out, ignore_calculated);
+
+ write_long(out, entry->xacts.size());
+ for (xacts_list::const_iterator i = entry->xacts.begin();
+ i != entry->xacts.end();
+ i++)
+ write_xact(out, *i, ignore_calculated);
+}
+
+void read_entry(const char *& data, entry_t * entry,
+ xact_t *& xact_pool, bool& finalize)
+{
+ read_entry_base(data, entry, xact_pool, finalize);
+ read_number(data, entry->_date);
+ read_number(data, entry->_date_eff);
+ read_string(data, entry->code);
+ read_string(data, entry->payee);
+}
+
+void write_entry(std::ostream& out, entry_t * entry)
+{
+ write_entry_base(out, entry);
+ write_number(out, entry->_date);
+ write_number(out, entry->_date_eff);
+ write_string(out, entry->code);
+ write_string(out, entry->payee);
+}
+
+void read_auto_entry(const char *& data, auto_entry_t * entry,
+ xact_t *& xact_pool)
+{
+ bool ignore;
+ read_entry_base(data, entry, xact_pool, ignore);
+
+ expr_t expr;
+ expr.read(data);
+ entry->predicate = item_predicate<xact_t>(expr);
+}
+
+void write_auto_entry(std::ostream& out, auto_entry_t * entry)
+{
+ write_entry_base(out, entry);
+ entry->predicate.predicate.write(out);
+}
+
+void read_period_entry(const char *& data, period_entry_t * entry,
+ xact_t *& xact_pool, bool& finalize)
+{
+ read_entry_base(data, entry, xact_pool, finalize);
+ read_string(data, &entry->period_string);
+ std::istringstream stream(entry->period_string);
+ entry->period.parse(stream);
+}
+
+void write_period_entry(std::ostream& out, period_entry_t * entry)
+{
+ write_entry_base(out, entry);
+ write_string(out, entry->period_string);
+}
+
+commodity_t::base_t * read_commodity_base(const char *& data)
+{
+ string str;
+
+ read_string(data, str);
+
+ std::auto_ptr<commodity_t::base_t> commodity(new commodity_t::base_t(str));
+
+ read_string(data, str);
+ if (! str.empty())
+ commodity->name = str;
+
+ read_string(data, str);
+ if (! str.empty())
+ commodity->note = str;
+
+ read_number(data, commodity->precision);
+ unsigned long flags;
+ read_number(data, flags);
+ commodity->set_flags(flags);
+
+ return commodity.release();
+}
+
+void write_commodity_base(std::ostream& out, commodity_t::base_t * commodity)
+{
+ // jww (2008-04-22): Not using this anymore?
+ //commodity->ident = ++base_commodity_index;
+
+ write_string(out, commodity->symbol);
+ // jww (2008-04-22): What to do with optional members?
+ write_string(out, *commodity->name);
+ write_string(out, *commodity->note);
+ write_number(out, commodity->precision);
+ write_number(out, commodity->flags());
+}
+
+void read_commodity_base_extra(const char *& data,
+ commodity_t::ident_t ident)
+{
+ commodity_t::base_t * commodity = base_commodities[ident];
+
+ bool read_history = false;
+ for (unsigned long i = 0, count = read_long<unsigned long>(data);
+ i < count;
+ i++) {
+ datetime_t when;
+ read_number(data, when);
+ amount_t amt;
+ amt.read(data);
+
+ // Upon insertion, amt will be copied, which will cause the amount to be
+ // duplicated (and thus not lost when the journal's item_pool is deleted).
+ if (! commodity->history)
+ commodity->history = commodity_t::history_t();
+ commodity->history->prices.insert(commodity_t::base_t::history_pair(when, amt));
+
+ read_history = true;
+ }
+ if (read_history)
+ read_number(data, commodity->history->last_lookup);
+
+ if (read_bool(data)) {
+ amount_t amt;
+ amt.read(data);
+ commodity->smaller = amount_t(amt);
+ }
+
+ if (read_bool(data)) {
+ amount_t amt;
+ amt.read(data);
+ commodity->larger = amount_t(amt);
+ }
+}
+
+void write_commodity_base_extra(std::ostream& out,
+ commodity_t::base_t * commodity)
+{
+#if 0
+ // jww (2008-04-22): What did bogus_time used to do?
+ if (commodity->history && commodity->history->bogus_time)
+ commodity->remove_price(commodity->history->bogus_time);
+#endif
+
+ if (! commodity->history) {
+ write_long<unsigned long>(out, 0);
+ } else {
+ write_long<unsigned long>(out, commodity->history->prices.size());
+ for (commodity_t::history_map::const_iterator
+ i = commodity->history->prices.begin();
+ i != commodity->history->prices.end();
+ i++) {
+ write_number(out, (*i).first);
+ (*i).second.write(out);
+ }
+ write_number(out, commodity->history->last_lookup);
+ }
+
+ if (commodity->smaller) {
+ write_bool(out, true);
+ commodity->smaller->write(out);
+ } else {
+ write_bool(out, false);
+ }
+
+ if (commodity->larger) {
+ write_bool(out, true);
+ commodity->larger->write(out);
+ } else {
+ write_bool(out, false);
+ }
+}
+
+commodity_t * read_commodity(const char *& data)
+{
+ commodity_t::base_t * base =
+ base_commodities[read_long<commodity_t::ident_t>(data) - 1];
+
+ commodity_t * commodity =
+ new commodity_t(amount_t::current_pool,
+ shared_ptr<commodity_t::base_t>(base));
+
+ *commodities_next++ = commodity;
+
+ string str;
+ read_string(data, str);
+ if (! str.empty())
+ commodity->qualified_symbol = str;
+ commodity->annotated = false;
+
+ return commodity;
+}
+
+void write_commodity(std::ostream& out, commodity_t * commodity)
+{
+ commodity->ident = ++commodity_index;
+
+ // jww (2008-04-22): Is this used anymore?
+ //write_long(out, commodity->base->ident);
+ // jww (2008-04-22): Optional!
+ write_string(out, *commodity->qualified_symbol);
+}
+
+commodity_t * read_commodity_annotated(const char *& data)
+{
+ commodity_t * commodity =
+ commodities[read_long<commodity_t::ident_t>(data) - 1];
+
+ annotation_t details;
+
+ string str;
+ read_string(data, str);
+
+ // This read-and-then-assign causes a new amount to be allocated which does
+ // not live within the bulk allocation pool, since that pool will be deleted
+ // *before* the commodities are destroyed.
+ amount_t amt;
+ amt.read(data);
+ details.price = amt;
+
+#if 0
+ // jww (2008-04-22): These are optional members!
+ read_number(data, details.date);
+ read_string(data, details.tag);
+#endif
+
+ annotated_commodity_t * ann_comm =
+ new annotated_commodity_t(commodity, details);
+ *commodities_next++ = ann_comm;
+
+ if (! str.empty())
+ ann_comm->qualified_symbol = str;
+
+ return ann_comm;
+}
+
+void write_commodity_annotated(std::ostream& out,
+ commodity_t * commodity)
+{
+ commodity->ident = ++commodity_index;
+
+ // jww (2008-04-22): No longer needed?
+ //write_long(out, commodity->base->ident);
+ // jww (2008-04-22): Optional!
+ write_string(out, *commodity->qualified_symbol);
+
+ annotated_commodity_t * ann_comm =
+ static_cast<annotated_commodity_t *>(commodity);
+
+ // jww (2008-04-22): No longer needed?
+ //write_long(out, ann_comm->base->ident);
+ // jww (2008-04-22): Make a write_annotation_details function; and optional!
+ ann_comm->details.price->write(out);
+ ann_comm->details.date->write(out);
+ ann_comm->details.tag->write(out);
+}
+
+inline
+account_t * read_account(const char *& data, account_t * master = NULL)
+{
+ account_t * acct = new account_t(NULL);
+
+ accounts[account_ident++] = acct;
+
+ account_t::ident_t id;
+ read_long(data, id); // parent id
+ if (id == 0xffffffff)
+ acct->parent = NULL;
+ else
+ acct->parent = accounts[id - 1];
+
+ read_string(data, acct->name);
+ read_string(data, acct->note);
+ read_number(data, acct->depth);
+
+ // If all of the subaccounts will be added to a different master
+ // account, throw away what we've learned about the recorded
+ // journal's own master account.
+
+ if (master && acct != master) {
+ checked_delete(acct);
+ acct = master;
+ }
+
+ for (account_t::ident_t i = 0,
+ count = read_long<account_t::ident_t>(data);
+ i < count;
+ i++) {
+ account_t * child = read_account(data);
+ child->parent = acct;
+ assert(acct != child);
+ acct->add_account(child);
+ }
+
+ return acct;
+}
+
+namespace {
+ 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_account(std::ostream& out, account_t * account)
+{
+ account->ident = ++account_ident;
+
+ if (account->parent)
+ write_long(out, account->parent->ident);
+ else
+ write_long<account_t::ident_t>(out, 0xffffffff);
+
+ write_string(out, account->name);
+ write_string(out, account->note);
+ write_number(out, account->depth);
+
+ write_number<std::size_t>(out, account->accounts.size());
+
+ for (accounts_map::iterator i = account->accounts.begin();
+ i != account->accounts.end();
+ i++)
+ write_account(out, (*i).second);
+}
+
+unsigned int read_journal(std::istream& in,
+ const path& file,
+ journal_t& journal,
+ account_t * master)
+{
+ using namespace binary;
+
+ // Read in the files that participated in this journal, so that they
+ // can be checked for changes on reading.
+
+ if (! file.empty()) {
+ for (unsigned short i = 0,
+ count = read_number<unsigned short>(in);
+ i < count;
+ i++) {
+ path pathname = read_string(in);
+ std::time_t old_mtime;
+ read_number(in, old_mtime);
+ struct stat info;
+ // jww (2008-04-22): can this be done differently now?
+ stat(pathname.string().c_str(), &info);
+ if (std::difftime(info.st_mtime, old_mtime) > 0)
+ return 0;
+
+ sources.push_back(pathname);
+ }
+
+ // Make sure that the cache uses the same price database,
+ // otherwise it means that LEDGER_PRICE_DB has been changed, and
+ // we should ignore this cache file.
+ if (read_bool(in)) {
+ string pathname;
+ read_string(in, pathname);
+ if (! price_db ||
+ price_db->string() != std::string(pathname))
+ return 0;
+ }
+ }
+
+ // jww (2008-07-31): bind master to session.master
+
+ if (read_bool(data))
+ basket = accounts[read_long<account_t::ident_t>(data) - 1];
+
+ // Read in the entries and xacts
+
+ for (std::size_t i = 0; i < count; i++) {
+ new(entry_pool) entry_t;
+ bool finalize = false;
+ read_entry(data, entry_pool, xact_pool, finalize);
+ entry_pool->journal = &journal;
+ if (finalize && ! entry_pool->finalize())
+ continue;
+ entries.push_back(entry_pool++);
+ }
+
+ for (std::size_t i = 0; i < auto_count; i++) {
+ auto_entry_t * auto_entry = new auto_entry_t;
+ read_auto_entry(data, auto_entry, xact_pool);
+ auto_entry->journal = &journal;
+ auto_entries.push_back(auto_entry);
+ }
+
+ for (std::size_t i = 0; i < period_count; i++) {
+ period_entry_t * period_entry = new period_entry_t;
+ bool finalize = false;
+ read_period_entry(data, period_entry, xact_pool, finalize);
+ period_entry->journal = &journal;
+ if (finalize && ! period_entry->finalize())
+ continue;
+ period_entries.push_back(period_entry);
+ }
+
+ VERIFY(journal.valid());
+
+ return count;
+}
+
+std::pair<std::size_t, std::size_t>
+write_journal(std::ostream& out, const journal_t& journal)
+{
+ using namespace binary;
+
+ // Write out the files that participated in this journal, so that
+ // they can be checked for changes on reading.
+
+ if (sources.empty()) {
+ write_number<unsigned short>(out, 0);
+ } else {
+ write_number<unsigned short>(out, sources.size());
+ for (paths_list::const_iterator i = sources.begin();
+ i != sources.end();
+ i++) {
+ write_string(out, (*i).string());
+ struct stat info;
+ stat((*i).string().c_str(), &info);
+ write_number(out, std::time_t(info.st_mtime));
+ }
+
+ // Write out the price database that relates to this data file, so
+ // that if it ever changes the cache can be invalidated.
+ if (price_db) {
+ write_bool(out, true);
+ write_string(out, price_db->string());
+ } else {
+ write_bool(out, false);
+ }
+ }
+
+ // Write out the basket accounts
+
+ if (basket) {
+ write_bool(out, true);
+ write_long(out, basket->ident);
+ } else {
+ write_bool(out, false);
+ }
+
+ // Write out the entries and xacts
+
+ std::size_t this_entry_count = 0;
+ std::size_t this_xact_count = 0;
+
+ for (entries_list::const_iterator i = entries.begin();
+ i != entries.end();
+ i++) {
+ write_entry(out, *i);
+
+ this_entry_count++;
+ this_xact_count += (*i)->xacts.size();
+ }
+
+ for (auto_entries_list::const_iterator i = auto_entries.begin();
+ i != auto_entries.end();
+ i++) {
+ write_auto_entry(out, *i);
+
+ this_entry_count++;
+ this_xact_count += (*i)->xacts.size();
+ }
+
+ for (period_entries_list::const_iterator i = period_entries.begin();
+ i != period_entries.end();
+ i++) {
+ write_period_entry(out, *i);
+
+ this_entry_count++;
+ this_xact_count += (*i)->xacts.size();
+ }
+
+ return std::pair<std::size_t, std::size_t>(this_entry_count,
+ this_xact_count);
+}
+
+std::size_t read_session(std::istream& in,
+ const path& file,
+ session_t& session)
+{
+ using namespace binary;
+
+ // Read all of the data in at once, so that we're just dealing with
+ // a big data buffer.
+
+ std::size_t data_size = read_number<std::size_t>(in);
+
+ scoped_array<char> data_pool(new char[data_size]);
+
+ in.read(data_pool, data_size);
+
+ const char * data = data_pool.get();
+
+ // Read in the accounts
+
+ accounts.resize(read_number<std::size_t>(data));
+ account_ident = 0;
+
+ if (session.master)
+ checked_delete(session.master);
+ session.master = read_account(data);
+
+ // Allocate the memory needed for the entries, xacts and bigints in one
+ // large block, which is then chopped up and custom constructed as
+ // necessary.
+
+ entry_count = read_number<std::size_t>(data);
+ auto_entry_count = read_number<std::size_t>(data);
+ period_entry_count = read_number<std::size_t>(data);
+ xact_count = read_number<std::size_t>(data);
+ bigints_count = read_number<std::size_t>(data);
+
+#define ENTRIES_SIZE (sizeof(entry_t) * entry_count)
+#define XACTS_SIZE (sizeof(xact_t) * xact_count)
+#define BIGINTS_SIZE (amount_t::sizeof_bigint_t() * bigints_count)
+
+#define ENTRIES_OFFSET 0
+#define XACTS_OFFSET ENTRIES_SIZE
+#define BIGINTS_OFFSET (ENTRIES_SIZE + XACTS_SIZE)
+
+ item_pool.reset(new char[ENTRIES_SIZE + XACTS_SIZE + BIGINTS_SIZE]);
+
+ entry_pool = reinterpret_cast<entry_t *>(item_pool.get() + ENTRIES_OFFSET);
+ xact_pool = reinterpret_cast<xact_t *>(item_pool.get() + XACTS_OFFSET);
+ bigints = item_pool.get() + BIGINTS_OFFSET;
+ bigints_next = bigints;
+ bigint_ident = 0;
+
+#if 0
+ // Read in the base commodities and the derived commodities
+
+ base_commodity_count = read_number<std::size_t>(data);
+ base_commodities.resize(base_commodity_count);
+
+ for (std::size_t i = 0; i < base_commodity_count; i++) {
+ commodity_t::base_t * base = read_commodity_base(data);
+ session.commodity_pool->commodities.push_back(base);
+
+ std::pair<base_commodities_map::iterator, bool> result =
+ commodity_base_t::commodities.insert
+ (base_commodities_pair(commodity->symbol, commodity));
+ if (! result.second) {
+ base_commodities_map::iterator c =
+ commodity_t::base_t::commodities.find(commodity->symbol);
+
+ // It's possible the user might have used a commodity in a value
+ // expression passed to an option, we'll just override the flags, but
+ // keep the commodity pointer intact.
+ if (c == commodity_t::base_t::commodities.end())
+ throw new error(string("Failed to read base commodity from cache: ") +
+ commodity->symbol);
+
+ (*c).second->name = commodity->name;
+ (*c).second->note = commodity->note;
+ (*c).second->precision = commodity->precision;
+ (*c).second->flags = commodity->flags;
+
+ if ((*c).second->smaller)
+ checked_delete((*c).second->smaller);
+ (*c).second->smaller = commodity->smaller;
+ if ((*c).second->larger)
+ checked_delete((*c).second->larger);
+ (*c).second->larger = commodity->larger;
+
+ *(base_commodities_next - 1) = (*c).second;
+
+ checked_delete(commodity);
+ }
+ }
+
+ commodity_count = read_number<std::size_t>(data);
+ commodities.resize(commodity_count);
+
+ for (std::size_t i = 0; i < commodity_count; i++) {
+ commodity_t * commodity;
+ string mapping_key;
+
+ if (! read_bool(data)) {
+ commodity = read_commodity(data);
+ mapping_key = commodity->base->symbol;
+ } else {
+ read_string(data, mapping_key);
+ commodity = read_commodity_annotated(data);
+ }
+
+ session.commodity_pool->commodities.push_back(commodity);
+
+ if (! result.second) {
+ commodities_map::iterator c =
+ commodity_t::commodities.find(mapping_key);
+ if (c == commodity_t::commodities.end())
+ throw new error(string("Failed to read commodity from cache: ") +
+ commodity->symbol());
+
+ *(commodities_next - 1) = (*c).second;
+ checked_delete(commodity);
+ }
+ }
+
+ for (std::size_t i = 0; i < base_commodity_count; i++)
+ read_commodity_base_extra(data, i);
+
+ commodity_t::ident_t ident = read_number<commodity_t::ident_t>(data);
+ if (ident == 0xffffffff || ident == 0)
+ session.commodity_pool->default_commodity = NULL;
+ else
+ session.commodity_pool->default_commodity = commodities[ident - 1];
+#endif
+
+ // Clean up and return the number of entries read
+
+ accounts.clear();
+ commodities.clear();
+
+ VERIFY(session.valid());
+
+ return count;
+}
+
+void write_session(std::ostream& out, session_t& session)
+{
+ using namespace binary;
+
+ write_number_nocheck(out, binary_magic_number);
+ write_number_nocheck(out, format_version);
+
+ // This number gets patched at the end of the function
+ ostream_pos_type data_val = out.tellp();
+ write_number<std::size_t>(out, 0);
+
+ // Write out the accounts
+
+ write_number<std::size_t>(out, count_accounts(session.master));
+ write_account(out, session.master);
+
+ // Write out the number of entries, xacts, and amounts
+
+ write_number<std::size_t>(out, entries.size());
+ write_number<std::size_t>(out, auto_entries.size());
+ write_number<std::size_t>(out, period_entries.size());
+
+ // These two numbers get patched at the end of the function
+ ostream_pos_type xacts_val = out.tellp();
+ write_number<std::size_t>(out, 0);
+ ostream_pos_type bigints_val = out.tellp();
+ write_number<std::size_t>(out, 0);
+
+ bigint_ident = 0;
+
+#if 0
+ // Write out the commodities
+ // jww (2008-04-22): This whole section needs to be reworked
+
+ write_number<std::size_t>(out, session.commodity_pool->commodities.size());
+ write_number<std::size_t>(out, session.commodity_pool->commodities.size());
+
+ for (base_commodities_map::const_iterator i =
+ commodity_t::base_t::commodities.begin();
+ i != commodity_t::base_t::commodities.end();
+ i++)
+ write_commodity_base(out, (*i).second);
+
+ write_number<commodity_t::ident_t>
+ (out, commodity_t::commodities.size());
+
+ for (commodities_map::const_iterator i = commodity_t::commodities.begin();
+ i != commodity_t::commodities.end();
+ i++) {
+ if (! (*i).second->annotated) {
+ write_bool(out, false);
+ write_commodity(out, (*i).second);
+ }
+ }
+
+ for (commodities_map::const_iterator i = commodity_t::commodities.begin();
+ i != commodity_t::commodities.end();
+ i++) {
+ if ((*i).second->annotated) {
+ write_bool(out, true);
+ write_string(out, (*i).first); // the mapping key
+ write_commodity_annotated(out, (*i).second);
+ }
+ }
+
+ // Write out the history and smaller/larger convertible links after
+ // both the base and the main commodities have been written, since
+ // the amounts in both will refer to the mains.
+
+ for (base_commodities_map::const_iterator i =
+ commodity_t::base_t::commodities.begin();
+ i != commodity_t::base_t::commodities.end();
+ i++)
+ write_commodity_base_extra(out, (*i).second);
+
+ if (commodity_t::default_commodity)
+ write_number(out, commodity_t::default_commodity->ident);
+ else
+ write_number<commodity_t::ident_t>(out, 0xffffffff);
+#endif
+
+ // Back-patch several counts which were not known beforehand
+
+ out.seekp(data_val);
+ write_number<std::size_t>(out, (static_cast<std::size_t>(out.tellp()) -
+ static_cast<std::size_t>(data_val) -
+ sizeof(std::size_t)));
+ out.seekp(xacts_val);
+ write_number<std::size_t>(out, xact_count);
+ out.seekp(bigints_val);
+ write_number<std::size_t>(out, bigints_count);
+}
+#endif
+
+} // namespace ledger
diff --git a/cache.h b/cache.h
new file mode 100644
index 00000000..0e1005dd
--- /dev/null
+++ b/cache.h
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2003-2008, John Wiegley. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of New Artisans LLC nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef CACHE_H
+#define CACHE_H
+
+#include "utils.h"
+#include "session.h"
+#include "journal.h"
+#include "account.h"
+
+namespace ledger {
+
+class binary_cache_t
+{
+ static const unsigned long binary_magic_number = 0xFFEED765;
+#if defined(DEBUG_ON)
+ static const unsigned long format_version = 0x0002060d;
+#else
+ static const unsigned long format_version = 0x0002060c;
+#endif
+
+ scoped_array<char> item_pool;
+
+ std::vector<account_t *> accounts;
+ account_t::ident_t account_ident;
+
+ entry_t * entry_pool; // points into item_pool
+ std::size_t entry_count;
+ std::size_t auto_entry_count;
+ std::size_t period_entry_count;
+
+ xact_t * xact_pool; // points into item_pool
+ std::size_t xact_count;
+
+#if 0
+ commodity_base_t ** base_commodities; // allocated
+ commodity_base_t ** base_commodities_next;
+ uint_fast32_t base_commodity_index;
+ std::size_t base_commodity_count;
+#endif
+
+ commodity_t ** commodities; // allocated
+ commodity_t ** commodities_next;
+ uint_fast32_t commodity_ident;
+ std::size_t commodity_count;
+
+ char * bigints; // points into item_pool
+ char * bigints_next;
+ uint_fast32_t bigints_index;
+ std::size_t bigints_count;
+
+ void read_xact(const char *& data, xact_t * xact);
+ void write_xact(std::ostream& out, xact_t * xact,
+ bool ignore_calculated);
+
+ void read_entry_base(const char *& data, entry_base_t * entry,
+ xact_t *& xact_pool, bool& finalize);
+ void write_entry_base(std::ostream& out, entry_base_t * entry);
+ void read_entry(const char *& data, entry_t * entry,
+ xact_t *& xact_pool, bool& finalize);
+ void write_entry(std::ostream& out, entry_t * entry);
+ void read_auto_entry(const char *& data, auto_entry_t * entry,
+ xact_t *& xact_pool);
+ void write_auto_entry(std::ostream& out, auto_entry_t * entry);
+ void read_period_entry(const char *& data, period_entry_t * entry,
+ xact_t *& xact_pool, bool& finalize);
+ void write_period_entry(std::ostream& out, period_entry_t * entry);
+
+#if 0
+ commodity_t::base_t * read_commodity_base(const char *& data);
+ void write_commodity_base(std::ostream& out, commodity_t::base_t * commodity);
+ void read_commodity_base_extra(const char *& data,
+ commodity_t::ident_t ident);
+ void write_commodity_base_extra(std::ostream& out,
+ commodity_t::base_t * commodity);
+#endif
+
+ commodity_t * read_commodity(const char *& data);
+ void write_commodity(std::ostream& out, commodity_t * commodity);
+ commodity_t * read_commodity_annotated(const char *& data);
+ void write_commodity_annotated(std::ostream& out,
+ commodity_t * commodity);
+
+ account_t * read_account(const char *& data, account_t * master = NULL);
+ void write_account(std::ostream& out);
+
+ std::size_t read_journal(std::istream& in,
+ const path& file,
+ journal_t& journal,
+ account_t * master);
+ void write_journal(std::ostream& out,
+ const journal_t& journal);
+
+public:
+ binary_cache_t()
+ : account_ident(0),
+#if 0
+ base_commodity_ident(0),
+#endif
+ commodity_ident(0)
+ {
+ }
+
+ std::size_t read_session(std::istream& in, const path& file);
+ void write_session(std::ostream& out, session_t& session);
+
+};
+
+} // namespace ledger
+
+#endif // CACHE_H
diff --git a/entry.cc b/entry.cc
index 0fcd8ff3..4a9537ca 100644
--- a/entry.cc
+++ b/entry.cc
@@ -36,7 +36,8 @@
namespace ledger {
entry_base_t::entry_base_t(const entry_base_t& e)
- : journal(NULL), beg_pos(0), beg_line(0), end_pos(0), end_line(0)
+ : supports_flags<>(), journal(NULL),
+ beg_pos(0), beg_line(0), end_pos(0), end_line(0)
{
TRACE_CTOR(entry_base_t, "copy");
@@ -53,10 +54,15 @@ entry_base_t::~entry_base_t()
for (xacts_list::iterator i = xacts.begin();
i != xacts.end();
i++)
- if (! (*i)->has_flags(XACT_BULK_ALLOC))
- checked_delete(*i);
- else
- (*i)->~xact_t();
+ // If the transaction is a temporary, it will be destructed when the
+ // temporary is. If it's from a binary cache, we can safely destruct it
+ // but its memory will be deallocated with the cache.
+ if (! (*i)->has_flags(XACT_TEMP)) {
+ if (! (*i)->has_flags(XACT_IN_CACHE))
+ checked_delete(*i);
+ else
+ (*i)->~xact_t();
+ }
}
void entry_base_t::add_xact(xact_t * xact)
diff --git a/entry.h b/entry.h
index b092684d..5764624c 100644
--- a/entry.h
+++ b/entry.h
@@ -41,9 +41,11 @@ class journal_t;
typedef std::list<xact_t *> xacts_list;
-class entry_base_t
+class entry_base_t : public supports_flags<>
{
- public:
+public:
+#define ENTRY_IN_CACHE 0x1
+
journal_t * journal;
string note;
unsigned long src_idx;
diff --git a/journal.cc b/journal.cc
index 9b8d47b4..5ad9dc08 100644
--- a/journal.cc
+++ b/journal.cc
@@ -36,11 +36,11 @@ namespace ledger {
const string version = PACKAGE_VERSION;
-journal_t::journal_t(session_t * _owner) :
- owner(_owner), basket(NULL), item_pool(NULL), item_pool_end(NULL)
+journal_t::journal_t(session_t * _owner)
+ : owner(_owner), basket(NULL)
{
TRACE_CTOR(journal_t, "");
- master = owner->master;
+ master = owner->master.get();
}
journal_t::~journal_t()
@@ -52,22 +52,16 @@ journal_t::~journal_t()
// be deleted.
for (entries_list::iterator i = entries.begin();
i != entries.end();
- i++) {
- if (! item_pool ||
- reinterpret_cast<char *>(*i) < item_pool ||
- reinterpret_cast<char *>(*i) >= item_pool_end) {
+ i++)
+ if (! (*i)->has_flags(ENTRY_IN_CACHE))
checked_delete(*i);
- } else {
+ else
(*i)->~entry_t();
- }
- }
for (auto_entries_list::iterator i = auto_entries.begin();
i != auto_entries.end();
i++)
- if (! item_pool ||
- reinterpret_cast<char *>(*i) < item_pool ||
- reinterpret_cast<char *>(*i) >= item_pool_end)
+ if (! (*i)->has_flags(ENTRY_IN_CACHE))
checked_delete(*i);
else
(*i)->~auto_entry_t();
@@ -75,15 +69,10 @@ journal_t::~journal_t()
for (period_entries_list::iterator i = period_entries.begin();
i != period_entries.end();
i++)
- if (! item_pool ||
- reinterpret_cast<char *>(*i) < item_pool ||
- reinterpret_cast<char *>(*i) >= item_pool_end)
+ if (! (*i)->has_flags(ENTRY_IN_CACHE))
checked_delete(*i);
else
(*i)->~period_entry_t();
-
- if (item_pool)
- checked_array_delete(item_pool);
}
void journal_t::add_account(account_t * acct)
diff --git a/journal.h b/journal.h
index cfb52cbe..e61ab114 100644
--- a/journal.h
+++ b/journal.h
@@ -52,8 +52,6 @@ public:
entries_list entries;
paths_list sources;
optional<path> price_db;
- char * item_pool;
- char * item_pool_end;
auto_entries_list auto_entries;
period_entries_list period_entries;
@@ -80,17 +78,15 @@ public:
entry_finalize_hooks.remove_hook(finalizer);
}
- bool valid() const;
-
-/**
- * @class journal_t::parser_t
- *
- * @brief Provides an abstract interface for writing journal parsers.
- *
- * Any data format for Ledger data is possible, as long as it can be parsed
- * into a journal_t data tree. This class provides the basic interface which
- * must be implemented by every such journal parser.
- */
+ /**
+ * @class journal_t::parser_t
+ *
+ * @brief Provides an abstract interface for writing journal parsers.
+ *
+ * Any data format for Ledger data is possible, as long as it can be parsed
+ * into a journal_t data tree. This class provides the basic interface which
+ * must be implemented by every such journal parser.
+ */
class parser_t : public noncopyable
{
public:
@@ -122,9 +118,6 @@ public:
const path * original_file = NULL);
};
- unsigned int read(std::istream& in, const path& file, account_t * master);
- void write(std::ostream& out);
-
class parse_error : public error
{
public:
@@ -132,6 +125,8 @@ public:
: error(reason, ctxt) {}
virtual ~parse_error() throw() {}
};
+
+ bool valid() const;
};
extern const string version;
diff --git a/main.cc b/main.cc
index a891d8fe..4e475620 100644
--- a/main.cc
+++ b/main.cc
@@ -310,6 +310,7 @@ static int read_and_report(ledger::report_t& report, int argc, char * argv[],
if (DO_VERIFY() && report.output_file)
checked_delete(out);
+#if 0
// Write out the binary cache, if need be
if (session.use_cache && session.cache_dirty && session.cache_file) {
@@ -320,6 +321,7 @@ static int read_and_report(ledger::report_t& report, int argc, char * argv[],
TRACE_FINISH(binary_cache, 1);
}
+#endif
// If the user specified a pager, wait for it to exit now
@@ -396,7 +398,9 @@ int main(int argc, char * argv[], char * envp[])
ledger::set_session_context(session.get());
+#if 0
session->register_parser(new ledger::journal_t::binary_parser_t);
+#endif
#if defined(HAVE_EXPAT) || defined(HAVE_XMLPARSE)
session->register_parser(new ledger::xml_parser_t);
session->register_parser(new ledger::gnucash_parser_t);
diff --git a/mask.cc b/mask.cc
index 52074394..d3ad539e 100644
--- a/mask.cc
+++ b/mask.cc
@@ -30,12 +30,19 @@
*/
#include "mask.h"
+#include "binary.h"
namespace ledger {
mask_t::mask_t(const string& pat) : exclude(false), expr()
{
TRACE_CTOR(mask_t, "const string&");
+ *this = pat;
+}
+
+mask_t& mask_t::operator=(const string& pat)
+{
+ exclude = false;
const char * p = pat.c_str();
@@ -52,6 +59,24 @@ mask_t::mask_t(const string& pat) : exclude(false), expr()
}
expr.assign(p);
+
+ return *this;
+}
+
+void mask_t::read(const char *& data)
+{
+ binary::read_number(data, exclude);
+
+ string pattern;
+ binary::read_string(data, pattern);
+
+ *this = pattern;
+}
+
+void mask_t::write(std::ostream& out) const
+{
+ binary::write_number(out, exclude);
+ binary::write_string(out, expr.str());
}
} // namespace ledger
diff --git a/mask.h b/mask.h
index 95ab214a..538316be 100644
--- a/mask.h
+++ b/mask.h
@@ -45,6 +45,7 @@ public:
boost::regex expr;
explicit mask_t(const string& pattern);
+
mask_t(const mask_t& m) : exclude(m.exclude), expr(m.expr) {
TRACE_CTOR(mask_t, "copy");
}
@@ -52,9 +53,14 @@ public:
TRACE_DTOR(mask_t);
}
+ mask_t& operator=(const string& other);
+
bool match(const string& str) const {
return boost::regex_match(str, expr) && ! exclude;
}
+
+ void read(const char *& data);
+ void write(std::ostream& out) const;
};
} // namespace ledger
diff --git a/session.cc b/session.cc
index 18435fe6..bb70862f 100644
--- a/session.cc
+++ b/session.cc
@@ -234,7 +234,7 @@ namespace {
account_t * session_t::find_account_re(const string& regexp)
{
- return find_account_re_(master, mask_t(regexp));
+ return find_account_re_(master.get(), mask_t(regexp));
}
void session_t::clean_xacts()
diff --git a/session.h b/session.h
index 5f064fb2..0eeb1e27 100644
--- a/session.h
+++ b/session.h
@@ -84,17 +84,14 @@ public:
ptr_list<journal_t> journals;
ptr_list<journal_t::parser_t> parsers;
-
- account_t * master;
- mutable accounts_map accounts_cache;
+ scoped_ptr<commodity_pool_t> commdity_pool;
+ scoped_ptr<account_t> master;
+ mutable accounts_map accounts_cache;
session_t();
virtual ~session_t() {
TRACE_DTOR(session_t);
-
- assert(master);
- checked_delete(master);
}
journal_t * create_journal() {
diff --git a/value.cc b/value.cc
index 6c257c00..272fe8d6 100644
--- a/value.cc
+++ b/value.cc
@@ -30,6 +30,7 @@
*/
#include "value.h"
+#include "binary.h"
namespace ledger {
@@ -1601,6 +1602,61 @@ void value_t::print(std::ostream& out, const bool relaxed) const
}
}
+void value_t::read(const char *& data)
+{
+ switch (static_cast<value_t::type_t>(binary::read_long<int>(data))) {
+ case BOOLEAN:
+ set_boolean(binary::read_bool(data));
+ break;
+ case INTEGER:
+ set_long(binary::read_long<unsigned long>(data));
+ break;
+ case DATETIME:
+ // jww (2008-04-22): I need to record and read a datetime_t directly
+ //set_datetime(read_long<unsigned long>(data));
+ break;
+ case AMOUNT: {
+ amount_t temp;
+ temp.read(data);
+ set_amount(temp);
+ break;
+ }
+
+ //case BALANCE:
+ //case BALANCE_PAIR:
+ default:
+ assert(false);
+ break;
+ }
+}
+
+void value_t::write(std::ostream& out) const
+{
+ binary::write_long(out, static_cast<int>(type()));
+
+ switch (type()) {
+ case BOOLEAN:
+ binary::write_bool(out, as_boolean());
+ break;
+ case INTEGER:
+ binary::write_long(out, as_long());
+ break;
+ case DATETIME:
+#if 0
+ binary::write_number(out, as_datetime());
+#endif
+ break;
+ case AMOUNT:
+ as_amount().write(out);
+ break;
+
+ //case BALANCE:
+ //case BALANCE_PAIR:
+ default:
+ throw new error("Cannot write a balance to the binary cache");
+ }
+}
+
bool value_t::valid() const
{
switch (type()) {
diff --git a/value.h b/value.h
index aac48f6f..892be37c 100644
--- a/value.h
+++ b/value.h
@@ -837,6 +837,15 @@ public:
void print(std::ostream& out, const bool relaxed = true) const;
/**
+ * Serialization methods. An amount may be deserialized from an
+ * input stream or a character pointer, and it may be serialized to
+ * an output stream. The methods used are:
+ */
+
+ void read(const char *& data);
+ void write(std::ostream& out) const;
+
+ /**
* Debugging methods.
*/
diff --git a/walk.cc b/walk.cc
index 5cadf1d6..bf93ea92 100644
--- a/walk.cc
+++ b/walk.cc
@@ -240,19 +240,19 @@ void invert_xacts::operator()(xact_t& xact)
static inline
-void handle_value(const value_t& value,
- account_t * account,
- entry_t * entry,
- unsigned int flags,
+void handle_value(const value_t& value,
+ account_t * account,
+ entry_t * entry,
+ unsigned int flags,
std::list<xact_t>& temps,
item_handler<xact_t>& handler,
- const datetime_t& date = datetime_t(),
+ const datetime_t& date = datetime_t(),
xacts_list * component_xacts = NULL)
{
temps.push_back(xact_t(account));
xact_t& xact(temps.back());
xact.entry = entry;
- xact.add_flags(XACT_BULK_ALLOC);
+ xact.add_flags(XACT_TEMP);
entry->add_xact(&xact);
// If there are component xacts to associate with this
@@ -625,7 +625,7 @@ void set_comm_as_payee::operator()(xact_t& xact)
xact_t& temp = xact_temps.back();
temp.entry = &entry;
temp.state = xact.state;
- temp.add_flags(XACT_BULK_ALLOC);
+ temp.add_flags(XACT_TEMP);
entry.add_xact(&temp);
@@ -647,7 +647,7 @@ void set_code_as_payee::operator()(xact_t& xact)
xact_t& temp = xact_temps.back();
temp.entry = &entry;
temp.state = xact.state;
- temp.add_flags(XACT_BULK_ALLOC);
+ temp.add_flags(XACT_TEMP);
entry.add_xact(&temp);
@@ -727,7 +727,7 @@ void budget_xacts::report_budget_items(const datetime_t& moment)
xact_temps.push_back(xact);
xact_t& temp = xact_temps.back();
temp.entry = &entry;
- temp.add_flags(XACT_AUTO | XACT_BULK_ALLOC);
+ temp.add_flags(XACT_AUTO | XACT_TEMP);
temp.amount.negate();
entry.add_xact(&temp);
@@ -817,7 +817,7 @@ void forecast_xacts::flush()
xact_temps.push_back(xact);
xact_t& temp = xact_temps.back();
temp.entry = &entry;
- temp.add_flags(XACT_AUTO | XACT_BULK_ALLOC);
+ temp.add_flags(XACT_AUTO | XACT_TEMP);
entry.add_xact(&temp);
datetime_t next = (*least).first.increment(begin);
@@ -995,7 +995,7 @@ void walk_commodities(commodity_pool_t::commodities_by_ident& commodities,
xact_t& temp = xact_temps.back();
temp.entry = &entry_temps.back();
temp.amount = (*j).second;
- temp.add_flags(XACT_BULK_ALLOC);
+ temp.add_flags(XACT_TEMP);
entry_temps.back().add_xact(&temp);
handler(xact_temps.back());
diff --git a/walk.h b/walk.h
index 176a34bf..cb13a826 100644
--- a/walk.h
+++ b/walk.h
@@ -507,8 +507,8 @@ class collapse_xacts : public item_handler<xact_t>
xact_t * last_xact;
account_t totals_account;
- std::list<entry_t> entry_temps;
- std::list<xact_t> xact_temps;
+ std::list<entry_t> entry_temps;
+ std::list<xact_t> xact_temps;
collapse_xacts();
diff --git a/xact.h b/xact.h
index 319e4ad8..2078dabd 100644
--- a/xact.h
+++ b/xact.h
@@ -38,13 +38,14 @@
namespace ledger {
// These flags persist with the object
-#define XACT_NORMAL 0x0000
-#define XACT_VIRTUAL 0x0001
-#define XACT_BALANCE 0x0002
-#define XACT_AUTO 0x0004
-#define XACT_BULK_ALLOC 0x0008
-#define XACT_CALCULATED 0x0010
-#define XACT_GENERATED 0x0020
+#define XACT_NORMAL 0x0000 // no flags at all, a basic transaction
+#define XACT_VIRTUAL 0x0001 // the account was specified with (parens)
+#define XACT_BALANCE 0x0002 // the account was specified with [brackets]
+#define XACT_AUTO 0x0004 // transaction created by automated entry
+#define XACT_IN_CACHE 0x0008 // transaction allocated by the binary cache
+#define XACT_CALCULATED 0x0010 // transaction's amount was auto-calculated
+#define XACT_GENERATED 0x0020 // transaction was not found in a journal
+#define XACT_TEMP 0x0040 // transaction is a temporary object
class entry_t;
class account_t;