diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/TODO | 12 | ||||
-rw-r--r-- | src/amount.cc | 46 | ||||
-rw-r--r-- | src/amount.h | 22 | ||||
-rw-r--r-- | src/balance.h | 19 | ||||
-rw-r--r-- | src/balpair.h | 49 | ||||
-rw-r--r-- | src/binary.cc | 951 | ||||
-rw-r--r-- | src/binary.h | 170 | ||||
-rw-r--r-- | src/data/builder.h (renamed from src/builder.h) | 0 | ||||
-rw-r--r-- | src/data/compile.cc (renamed from src/compile.cc) | 0 | ||||
-rw-r--r-- | src/data/compile.h (renamed from src/compile.h) | 0 | ||||
-rw-r--r-- | src/data/document.cc (renamed from src/document.cc) | 0 | ||||
-rw-r--r-- | src/data/document.h (renamed from src/document.h) | 0 | ||||
-rw-r--r-- | src/data/jbuilder.cc (renamed from src/jbuilder.cc) | 0 | ||||
-rw-r--r-- | src/data/jbuilder.h (renamed from src/jbuilder.h) | 0 | ||||
-rw-r--r-- | src/data/journal.cc (renamed from src/journal.cc) | 0 | ||||
-rw-r--r-- | src/data/journal.h (renamed from src/journal.h) | 0 | ||||
-rw-r--r-- | src/data/node.cc (renamed from src/node.cc) | 0 | ||||
-rw-r--r-- | src/data/node.h (renamed from src/node.h) | 0 | ||||
-rw-r--r-- | src/data/parser.h (renamed from src/parser.h) | 0 | ||||
-rw-r--r-- | src/data/textual.cc (renamed from src/textual.cc) | 0 | ||||
-rw-r--r-- | src/data/textual.h (renamed from src/textual.h) | 0 | ||||
-rw-r--r-- | src/driver/fdstream.hpp (renamed from src/fdstream.hpp) | 0 | ||||
-rw-r--r-- | src/driver/main.cc (renamed from src/main.cc) | 0 | ||||
-rw-r--r-- | src/driver/option.cc (renamed from src/option.cc) | 0 | ||||
-rw-r--r-- | src/driver/option.h (renamed from src/option.h) | 0 | ||||
-rw-r--r-- | src/driver/report.cc (renamed from src/report.cc) | 0 | ||||
-rw-r--r-- | src/driver/report.h (renamed from src/report.h) | 0 | ||||
-rw-r--r-- | src/driver/session.cc (renamed from src/session.cc) | 0 | ||||
-rw-r--r-- | src/driver/session.h (renamed from src/session.h) | 0 | ||||
-rw-r--r-- | src/numerics/balance.cc (renamed from src/balance.cc) | 0 | ||||
-rw-r--r-- | src/numerics/commodity.cc (renamed from src/commodity.cc) | 0 | ||||
-rw-r--r-- | src/numerics/commodity.h (renamed from src/commodity.h) | 0 | ||||
-rw-r--r-- | src/numerics/value.cc (renamed from src/value.cc) | 0 | ||||
-rw-r--r-- | src/numerics/value.h (renamed from src/value.h) | 0 | ||||
-rw-r--r-- | src/python/py_amount.cc (renamed from src/py_amount.cc) | 0 | ||||
-rw-r--r-- | src/python/py_commodity.cc (renamed from src/py_commodity.cc) | 0 | ||||
-rw-r--r-- | src/python/py_times.cc (renamed from src/py_times.cc) | 0 | ||||
-rw-r--r-- | src/python/py_utils.cc (renamed from src/py_utils.cc) | 0 | ||||
-rw-r--r-- | src/python/pyfstream.h (renamed from src/pyfstream.h) | 0 | ||||
-rw-r--r-- | src/python/pyinterp.cc (renamed from src/pyinterp.cc) | 0 | ||||
-rw-r--r-- | src/python/pyinterp.h (renamed from src/pyinterp.h) | 0 | ||||
-rw-r--r-- | src/python/pyledger.cc (renamed from src/pyledger.cc) | 0 | ||||
-rw-r--r-- | src/python/pyledger.h (renamed from src/pyledger.h) | 0 | ||||
-rw-r--r-- | src/python/pyutils.h (renamed from src/pyutils.h) | 0 | ||||
-rw-r--r-- | src/python/tuples.hpp (renamed from src/tuples.hpp) | 0 | ||||
-rw-r--r-- | src/traversal/abbrev.cc (renamed from src/abbrev.cc) | 0 | ||||
-rw-r--r-- | src/traversal/abbrev.h (renamed from src/abbrev.h) | 0 | ||||
-rw-r--r-- | src/traversal/transform.cc (renamed from src/transform.cc) | 0 | ||||
-rw-r--r-- | src/traversal/transform.h (renamed from src/transform.h) | 0 | ||||
-rw-r--r-- | src/traversal/xpath.cc (renamed from src/xpath.cc) | 0 | ||||
-rw-r--r-- | src/traversal/xpath.h (renamed from src/xpath.h) | 0 | ||||
-rw-r--r-- | src/utility/context.h (renamed from src/context.h) | 0 | ||||
-rw-r--r-- | src/utility/flags.h (renamed from src/flags.h) | 0 | ||||
-rw-r--r-- | src/utility/mask.cc (renamed from src/mask.cc) | 0 | ||||
-rw-r--r-- | src/utility/mask.h (renamed from src/mask.h) | 0 | ||||
-rw-r--r-- | src/utility/system.hh (renamed from src/system.hh) | 0 | ||||
-rw-r--r-- | src/utility/times.cc (renamed from src/times.cc) | 0 | ||||
-rw-r--r-- | src/utility/times.h (renamed from src/times.h) | 0 | ||||
-rw-r--r-- | src/utility/utils.cc (renamed from src/utils.cc) | 0 | ||||
-rw-r--r-- | src/utils.h | 4 |
60 files changed, 210 insertions, 1063 deletions
@@ -1,14 +1,12 @@ - What does SEQUENCE + VALUE mean in XPath? Does it add VALUE to every member of SEQUENCE? -- Add tracing code for functions that records call count and total - time spent, as well as average time per call. This would implement - selective profiling. + Answer: No, it doesn't; this should throw an error -- Make sure that if any constructors cause memory to be allocated, - that the memory is held by an auto_ptr until the constructor is - done; otherwise, an exception raised from within the constructor - will not call the destructor to free the memory. +- Make sure that if any constructors cause memory to be allocated, the + memory is held by an auto_ptr until the constructor is done; + otherwise, an exception raised from within the constructor will not + call the destructor to free the memory. - Using mmap for the binary reader; or determine if the performance is even worth the maintenance headaches of that code altogether. diff --git a/src/amount.cc b/src/amount.cc index 187200c3..4c771cc7 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -585,29 +585,35 @@ amount_t& amount_t::in_place_negate() return *this; } -amount_t amount_t::round(precision_t prec) const +amount_t amount_t::round(const optional<precision_t>& prec) const { if (! quantity) throw_(amount_error, "Cannot round an uninitialized amount"); - amount_t t(*this); + if (! prec) { + if (! has_commodity()) + return *this; + return round(commodity().precision()); + } else { + amount_t t(*this); - if (quantity->prec <= prec) { - if (quantity && quantity->has_flags(BIGINT_KEEP_PREC)) { - t._dup(); - t.quantity->drop_flags(BIGINT_KEEP_PREC); + if (quantity->prec <= *prec) { + if (quantity && quantity->has_flags(BIGINT_KEEP_PREC)) { + t._dup(); + t.quantity->drop_flags(BIGINT_KEEP_PREC); + } + return t; } - return t; - } - t._dup(); + t._dup(); - mpz_round(MPZ(t.quantity), MPZ(t.quantity), t.quantity->prec, prec); + mpz_round(MPZ(t.quantity), MPZ(t.quantity), t.quantity->prec, *prec); - t.quantity->prec = prec; - t.quantity->drop_flags(BIGINT_KEEP_PREC); + t.quantity->prec = *prec; + t.quantity->drop_flags(BIGINT_KEEP_PREC); - return t; + return t; + } } amount_t amount_t::unround() const @@ -1212,10 +1218,12 @@ namespace { void amount_t::read(std::istream& in) { + using ledger::binary; + // Read in the commodity for this amount commodity_t::ident_t ident; - read_binary_long(in, ident); + read_long(in, ident); if (ident == 0xffffffff) commodity_ = NULL; else if (ident == 0) @@ -1258,10 +1266,12 @@ void amount_t::read(std::istream& in) void amount_t::read(const char *& data) { + using ledger::binary; + // Read in the commodity for this amount commodity_t::ident_t ident; - read_binary_long(data, ident); + read_long(data, ident); if (ident == 0xffffffff) commodity_ = NULL; else if (ident == 0) @@ -1313,15 +1323,17 @@ void amount_t::read(const char *& data) void amount_t::write(std::ostream& out, bool optimized) const { + using ledger::binary; + // Write out the commodity for this amount if (! quantity) throw_(amount_error, "Cannot serialize an uninitialized amount"); if (commodity_) - write_binary_long(out, commodity_->ident); + write_long(out, commodity_->ident); else - write_binary_long<commodity_t::ident_t>(out, 0xffffffff); + write_long<commodity_t::ident_t>(out, 0xffffffff); // Write out the quantity diff --git a/src/amount.h b/src/amount.h index 0a460d98..f4dbc9bd 100644 --- a/src/amount.h +++ b/src/amount.h @@ -294,11 +294,11 @@ public: * abs() returns the absolute value of an amount. It is equivalent * to: `(x < 0) ? - x : x'. * - * round(precision_t) rounds an amount's internal value to the given - * precision. - * - * round() rounds an amount to its commodity's current display - * precision. This also changes the internal value of the amount. + * round(optional<precision_t>) rounds an amount's internal value to + * the given precision, or to the commodity's current display + * precision if no precision value is given. This method changes + * the internal value of the amount, if it's internal precision was + * greater than the rounding precision. * * unround() yields an amount whose display precision is never * truncated, even though its commodity normally displays only @@ -347,8 +347,7 @@ public: return *this; } - amount_t round(precision_t prec) const; - amount_t round() const; + amount_t round(const optional<precision_t>& prec) const; amount_t unround() const; amount_t reduce() const { @@ -700,16 +699,7 @@ inline bool amount_t::operator==(const amount_t& amt) const { return compare(amt) == 0; } -inline amount_t amount_t::round() const { - if (! quantity) - throw_(amount_error, "Cannot round an uninitialized amount"); - if (! has_commodity()) - return *this; - return round(commodity().precision()); -} - inline commodity_t& amount_t::commodity() const { - // jww (2007-05-02): Should be a way to access null_commodity better return has_commodity() ? *commodity_ : *current_pool->null_commodity; } diff --git a/src/balance.h b/src/balance.h index 003e29f9..1bab4b6b 100644 --- a/src/balance.h +++ b/src/balance.h @@ -234,7 +234,26 @@ public: balance_t& operator-=(const amount_t& amt); balance_t& operator*=(const amount_t& amt); + balance_t& operator*=(const double val) { + return *this *= amount_t(val); + } + balance_t& operator*=(const unsigned long val) { + return *this *= amount_t(val); + } + balance_t& operator*=(const long val) { + return *this *= amount_t(val); + } + balance_t& operator/=(const amount_t& amt); + balance_t& operator/=(const double val) { + return *this /= amount_t(val); + } + balance_t& operator/=(const unsigned long val) { + return *this /= amount_t(val); + } + balance_t& operator/=(const long val) { + return *this /= amount_t(val); + } /** * Unary arithmetic operators. There are only a few unary methods diff --git a/src/balpair.h b/src/balpair.h index 1198a82f..13e4857b 100644 --- a/src/balpair.h +++ b/src/balpair.h @@ -167,21 +167,50 @@ public: return *this = balance_t(str); } - // in-place arithmetic + /** + * Binary arithmetic operators. Balances support addition and + * subtraction of other balance pairs, balances or amounts, but + * multiplication and division are restricted to uncommoditized + * amounts only. + */ balance_pair_t& operator+=(const balance_pair_t& bal_pair) { - if (bal_pair.cost && ! cost) - cost = quantity; - quantity += bal_pair.quantity; - if (cost) - *cost += bal_pair.cost ? *bal_pair.cost : bal_pair.quantity; + balance_t::operator+=(bal_pair); + if (bal_pair.cost) { + if (! cost) + *cost = quantity(); + *cost += *bal_pair.cost; + } return *this; } balance_pair_t& operator-=(const balance_pair_t& bal_pair) { - if (bal_pair.cost && ! cost) - cost = quantity; - quantity -= bal_pair.quantity; + balance_t::operator+=(bal_pair); + if (bal_pair.cost) { + if (! cost) + *cost = quantity(); + *cost += *bal_pair.cost; + } + return *this; + } + + balance_pair_t& operator*=(const amount_t& amt) { + balance_t::operator*=(amt); + if (cost) + *cost *= amt; + return *this; + } + balance_pair_t& operator*=(const double val) { + return *this *= amount_t(val); + } + balance_pair_t& operator*=(const unsigned long val) { + return *this *= amount_t(val); + } + balance_pair_t& operator*=(const long val) { + return *this *= amount_t(val); + } + balance_pair_t& operator/=(const amount_t& amt) { + balance_t::operator/=(amt); if (cost) - *cost -= bal_pair.cost ? *bal_pair.cost : bal_pair.quantity; + *cost /= amt; return *this; } diff --git a/src/binary.cc b/src/binary.cc index d29f4d26..9ddbb144 100644 --- a/src/binary.cc +++ b/src/binary.cc @@ -32,60 +32,35 @@ #include "binary.h" namespace ledger { +namespace binary { -#if 0 -static unsigned long binary_magic_number = 0xFFEED765; -#if defined(DEBUG_ON) -static unsigned long format_version = 0x00030000; -#else -static unsigned long format_version = 0x00030000; -#endif - -static account_t ** accounts; -static account_t ** accounts_next; -static unsigned int account_index; - -static commodity_base_t ** base_commodities; -static commodity_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; -#endif - -void read_binary_bool(std::istream& in, bool& num) +void read_bool(std::istream& in, bool& num) { - read_binary_guard(in, 0x2005); + read_guard(in, 0x2005); unsigned char val; in.read((char *)&val, sizeof(val)); num = val == 1; - read_binary_guard(in, 0x2006); + read_guard(in, 0x2006); } -void read_binary_bool(const char *& data, bool& num) +void read_bool(const char *& data, bool& num) { - read_binary_guard(data, 0x2005); + read_guard(data, 0x2005); unsigned char val = *((unsigned char *) data); data += sizeof(unsigned char); num = val == 1; - read_binary_guard(data, 0x2006); + read_guard(data, 0x2006); } -void read_binary_string(std::istream& in, string& str) +void read_string(std::istream& in, string& str) { - read_binary_guard(in, 0x3001); + read_guard(in, 0x3001); unsigned char len; - read_binary_number_nocheck(in, len); + read_number_nocheck(in, len); if (len == 0xff) { unsigned short slen; - read_binary_number_nocheck(in, slen); + read_number_nocheck(in, slen); char * buf = new char[slen + 1]; in.read(buf, slen); buf[slen] = '\0'; @@ -101,18 +76,18 @@ void read_binary_string(std::istream& in, string& str) str = ""; } - read_binary_guard(in, 0x3002); + read_guard(in, 0x3002); } -void read_binary_string(const char *& data, string& str) +void read_string(const char *& data, string& str) { - read_binary_guard(data, 0x3001); + read_guard(data, 0x3001); unsigned char len; - read_binary_number_nocheck(data, len); + read_number_nocheck(data, len); if (len == 0xff) { unsigned short slen; - read_binary_number_nocheck(data, slen); + read_number_nocheck(data, slen); str = string(data, slen); data += slen; } @@ -124,18 +99,18 @@ void read_binary_string(const char *& data, string& str) str = ""; } - read_binary_guard(data, 0x3002); + read_guard(data, 0x3002); } -void read_binary_string(const char *& data, string * str) +void read_string(const char *& data, string * str) { - read_binary_guard(data, 0x3001); + read_guard(data, 0x3001); unsigned char len; - read_binary_number_nocheck(data, len); + read_number_nocheck(data, len); if (len == 0xff) { unsigned short slen; - read_binary_number_nocheck(data, slen); + read_number_nocheck(data, slen); new(str) string(data, slen); data += slen; } @@ -147,898 +122,36 @@ void read_binary_string(const char *& data, string * str) new(str) string(""); } - read_binary_guard(data, 0x3002); -} - -#if 0 -inline void read_binary_value(const char *& data, value_t& val) -{ - val.type = static_cast<value_t::type_t>(read_binary_long<int>(data)); - - switch (val.type) { - case value_t::BOOLEAN: - read_binary_bool(data, *((bool *) val.data)); - break; - case value_t::INTEGER: - read_binary_long(data, *((long *) val.data)); - break; - case value_t::DATETIME: - read_binary_number(data, *((moment_t *) val.data)); - break; - case value_t::AMOUNT: - read_binary_amount(data, *((amount_t *) val.data)); - break; - - case value_t::BALANCE: - case value_t::BALANCE_PAIR: - assert(false); - break; - } -} - -inline void read_binary_mask(const char *& data, mask_t *& mask) -{ - bool exclude; - read_binary_number(data, exclude); - string pattern; - read_binary_string(data, pattern); - - mask = new mask_t(pattern); - mask->exclude = exclude; -} - -inline void read_binary_transaction(const char *& data, transaction_t * xact) -{ - read_binary_number(data, xact->_date); - read_binary_number(data, xact->_date_eff); - xact->account = accounts[read_binary_long<account_t::ident_t>(data) - 1]; - - unsigned char flag = read_binary_number<unsigned char>(data); - if (flag == 0) { - read_binary_amount(data, xact->amount); - } - else if (flag == 1) { - string expr; - read_binary_string(data, expr); - xact->amount_expr = expr; - - repitem_t * item = - repitem_t::wrap(xact, static_cast<repitem_t *>(xact->entry->data)); - xact->data = item; - - xact->amount = valexpr_t(xact->amount_expr).calc(item).amount(); - } - - if (read_binary_bool(data)) { - xact->cost = new amount_t; - read_binary_amount(data, *xact->cost); - read_binary_string(data, xact->cost_expr); - } else { - xact->cost = NULL; - } - - read_binary_number(data, xact->state); - read_binary_number(data, xact->flags); - xact->flags |= TRANSACTION_BULK_ALLOC; - read_binary_string(data, &xact->note); - - xact->beg_pos = read_binary_long<unsigned long>(data); - read_binary_long(data, xact->beg_line); - xact->end_pos = read_binary_long<unsigned long>(data); - read_binary_long(data, xact->end_line); - - xact->data = NULL; -} - -inline void read_binary_entry_base(const char *& data, entry_base_t * entry, - transaction_t *& xact_pool, bool& finalize) -{ - read_binary_long(data, entry->src_idx); - entry->beg_pos = read_binary_long<unsigned long>(data); - read_binary_long(data, entry->beg_line); - entry->end_pos = read_binary_long<unsigned long>(data); - read_binary_long(data, entry->end_line); - - bool ignore_calculated = read_binary_bool(data); - - for (unsigned long i = 0, count = read_binary_long<unsigned long>(data); - i < count; - i++) { - new(xact_pool) transaction_t; - xact_pool->entry = static_cast<entry_t *>(entry); - read_binary_transaction(data, xact_pool); - if (ignore_calculated && xact_pool->flags & TRANSACTION_CALCULATED) - finalize = true; - entry->add_transaction(xact_pool++); - } -} - -inline void read_binary_entry(const char *& data, entry_t * entry, - transaction_t *& xact_pool, bool& finalize) -{ - entry->data = - repitem_t::wrap(entry, static_cast<repitem_t *>(entry->journal->data)); - - read_binary_entry_base(data, entry, xact_pool, finalize); - read_binary_number(data, entry->_date); - read_binary_number(data, entry->_date_eff); - read_binary_string(data, &entry->code); - read_binary_string(data, &entry->payee); -} - -inline void read_binary_auto_entry(const char *& data, auto_entry_t * entry, - transaction_t *& xact_pool) -{ - bool ignore; - read_binary_entry_base(data, entry, xact_pool, ignore); - - string pred_str; - read_binary_string(data, &pred_str); - entry->predicate.parse(pred_str); -} - -inline void read_binary_period_entry(const char *& data, period_entry_t * entry, - transaction_t *& xact_pool, bool& finalize) -{ - read_binary_entry_base(data, entry, xact_pool, finalize); - read_binary_string(data, &entry->period_string); - std::istringstream stream(entry->period_string); - entry->period.parse(stream); -} - -inline commodity_base_t * read_binary_commodity_base(const char *& data) -{ - commodity_base_t * commodity = new commodity_base_t; - *base_commodities_next++ = commodity; - - read_binary_string(data, commodity->symbol); - read_binary_string(data, commodity->name); - read_binary_string(data, commodity->note); - read_binary_number(data, commodity->precision); - read_binary_number(data, commodity->flags); - - return commodity; -} - -inline void read_binary_commodity_base_extra(const char *& data, - commodity_t::ident_t ident) -{ - commodity_base_t * commodity = base_commodities[ident]; - - bool read_history = false; - for (unsigned long i = 0, count = read_binary_long<unsigned long>(data); - i < count; - i++) { - moment_t when; - read_binary_number(data, when); - amount_t amt; - read_binary_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 = new commodity_base_t::history_t; - commodity->history->prices.insert(history_pair(when, amt)); - - read_history = true; - } - if (read_history) - read_binary_number(data, commodity->history->last_lookup); - - if (read_binary_bool(data)) { - amount_t amt; - read_binary_amount(data, amt); - commodity->smaller = new amount_t(amt); - } - - if (read_binary_bool(data)) { - amount_t amt; - read_binary_amount(data, amt); - commodity->larger = new amount_t(amt); - } -} - -inline commodity_t * read_binary_commodity(const char *& data) -{ - commodity_t * commodity = new commodity_t; - *commodities_next++ = commodity; - - commodity->base = - base_commodities[read_binary_long<commodity_base_t::ident_t>(data) - 1]; - - read_binary_string(data, commodity->qualified_symbol); - commodity->annotated = false; - - return commodity; + read_guard(data, 0x3002); } -inline commodity_t * read_binary_commodity_annotated(const char *& data) -{ - annotated_commodity_t * commodity = new annotated_commodity_t; - *commodities_next++ = commodity; - - commodity->base = - base_commodities[read_binary_long<commodity_base_t::ident_t>(data) - 1]; - - read_binary_string(data, commodity->qualified_symbol); - commodity->annotated = true; - - commodity->ptr = - commodities[read_binary_long<commodity_t::ident_t>(data) - 1]; - - // 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_binary_amount(data, amt); - commodity->price = amt; - - read_binary_number(data, commodity->date); - read_binary_string(data, commodity->tag); - return commodity; -} - -inline -account_t * read_binary_account(const char *& data, journal_t * journal, - account_t * master = NULL) +void write_bool(std::ostream& out, bool num) { - account_t * acct = new account_t(NULL); - *accounts_next++ = acct; - - acct->journal = journal; - - account_t::ident_t id; - read_binary_long(data, id); // parent id - if (id == 0xffffffff) - acct->parent = NULL; - else - acct->parent = accounts[id - 1]; - - read_binary_string(data, acct->name); - read_binary_string(data, acct->note); - read_binary_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_binary_long<account_t::ident_t>(data); - i < count; - i++) { - account_t * child = read_binary_account(data, journal); - child->parent = acct; - assert(acct != child); - acct->add_account(child); - } - - return acct; -} - -unsigned int read_binary_journal(std::istream& in, - journal_t * journal, - account_t * master, - const string& original_file) -{ - 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 (! original_file.empty()) { - for (unsigned short i = 0, - count = read_binary_number<unsigned short>(in); - i < count; - i++) { - string path = read_binary_string(in); - std::time_t old_mtime; - read_binary_number(in, old_mtime); - struct stat info; - stat(path.c_str(), &info); - if (std::difftime(info.st_mtime, old_mtime) > 0) - return 0; - - journal->sources.push_back(path); - } - - // 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_binary_string(in) != journal->price_db) - 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_binary_number<unsigned long>(in); - - char * data_pool = new char[data_size]; - char * data = data_pool; - in.read(data, data_size); - - // Read in the accounts - - account_t::ident_t a_count = read_binary_long<account_t::ident_t>(data); - accounts = accounts_next = new account_t *[a_count]; - - assert(journal->master); - checked_delete(journal->master); - journal->master = read_binary_account(data, journal, master); - - if (read_binary_bool(data)) - journal->basket = accounts[read_binary_long<account_t::ident_t>(data) - 1]; - - // Allocate the memory needed for the entries and transactions in - // one large block, which is then chopped up and custom constructed - // as necessary. - - unsigned long count = read_binary_long<unsigned long>(data); - unsigned long auto_count = read_binary_long<unsigned long>(data); - unsigned long period_count = read_binary_long<unsigned long>(data); - unsigned long xact_count = read_binary_number<unsigned long>(data); - unsigned long bigint_count = read_binary_number<unsigned long>(data); - - std::size_t pool_size = (sizeof(entry_t) * count + - sizeof(transaction_t) * xact_count + - sizeof_bigint_t() * bigint_count); - - char * item_pool = new char[pool_size]; - - journal->item_pool = item_pool; - journal->item_pool_end = item_pool + pool_size; - - entry_t * entry_pool = (entry_t *) item_pool; - transaction_t * xact_pool = (transaction_t *) (item_pool + - sizeof(entry_t) * count); - bigints_index = 0; - bigints = bigints_next = (item_pool + sizeof(entry_t) * count + - sizeof(transaction_t) * xact_count); - - // Read in the base commodities and then derived commodities - - commodity_base_t::ident_t bc_count = - read_binary_long<commodity_base_t::ident_t>(data); - base_commodities = base_commodities_next = new commodity_base_t *[bc_count]; - - for (commodity_base_t::ident_t i = 0; i < bc_count; i++) { - commodity_base_t * commodity = read_binary_commodity_base(data); - - 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_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_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_t::ident_t c_count = read_binary_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_binary_bool(data)) { - commodity = read_binary_commodity(data); - mapping_key = commodity->base->symbol; - } else { - read_binary_string(data, mapping_key); - commodity = read_binary_commodity_annotated(data); - } - - std::pair<commodities_map::iterator, bool> result = - commodity_t::commodities.insert(commodities_pair - (mapping_key, 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 (commodity_base_t::ident_t i = 0; i < bc_count; i++) - read_binary_commodity_base_extra(data, i); - - commodity_t::ident_t ident; - read_binary_long(data, ident); - if (ident == 0xffffffff || ident == 0) - commodity_t::default_commodity = NULL; - else - commodity_t::default_commodity = commodities[ident - 1]; - - // Read in the entries and transactions - - for (unsigned long i = 0; i < count; i++) { - new(entry_pool) entry_t; - bool finalize = false; - entry_pool->journal = journal; - read_binary_entry(data, entry_pool, xact_pool, finalize); - if (finalize && ! entry_pool->finalize()) - continue; - journal->entries.push_back(entry_pool++); - } - - for (unsigned long i = 0; i < auto_count; i++) { - auto_entry_t * auto_entry = new auto_entry_t; - read_binary_auto_entry(data, auto_entry, xact_pool); - auto_entry->journal = journal; - journal->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_binary_period_entry(data, period_entry, xact_pool, finalize); - period_entry->journal = journal; - if (finalize && ! period_entry->finalize()) - continue; - journal->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); - - VALIDATE(journal->valid()); - - return count; -} -#endif - -#if 0 -bool binary_parser_t::test(std::istream& in) const -{ - if (read_binary_number_nocheck<unsigned long>(in) == binary_magic_number && - read_binary_number_nocheck<unsigned long>(in) == format_version) - return true; - - in.clear(); - in.seekg(0, std::ios::beg); - return false; -} - -unsigned int binary_parser_t::parse(std::istream& in, - journal_t * journal, - account_t * master, - const string * original_file) -{ -#if 0 - return read_binary_journal(in, journal, master, - original_file ? *original_file : ""); -#endif -} -#endif - - -void write_binary_bool(std::ostream& out, bool num) -{ - write_binary_guard(out, 0x2005); + write_guard(out, 0x2005); unsigned char val = num ? 1 : 0; out.write((char *)&val, sizeof(val)); - write_binary_guard(out, 0x2006); + write_guard(out, 0x2006); } -void write_binary_string(std::ostream& out, const string& str) +void write_string(std::ostream& out, const string& str) { - write_binary_guard(out, 0x3001); + write_guard(out, 0x3001); unsigned long len = str.length(); if (len > 255) { assert(len < 65536); - write_binary_number_nocheck<unsigned char>(out, 0xff); - write_binary_number_nocheck<unsigned short>(out, len); + write_number_nocheck<unsigned char>(out, 0xff); + write_number_nocheck<unsigned short>(out, len); } else { - write_binary_number_nocheck<unsigned char>(out, len); + write_number_nocheck<unsigned char>(out, len); } if (len) out.write(str.c_str(), len); - write_binary_guard(out, 0x3002); -} - -#if 0 -void write_binary_value(std::ostream& out, const value_t& val) -{ - write_binary_long(out, (int)val.type); - - switch (val.type) { - case value_t::BOOLEAN: - write_binary_bool(out, *((bool *) val.data)); - break; - case value_t::INTEGER: - write_binary_long(out, *((long *) val.data)); - break; - case value_t::DATETIME: - write_binary_number(out, *((moment_t *) val.data)); - break; - case value_t::AMOUNT: - write_binary_amount(out, *((amount_t *) val.data)); - break; - - case value_t::BALANCE: - case value_t::BALANCE_PAIR: - throw new error("Cannot write a balance to the binary cache"); - } -} - -void write_binary_mask(std::ostream& out, mask_t * mask) -{ - write_binary_number(out, mask->exclude); - write_binary_string(out, mask->pattern); -} - -void write_binary_transaction(std::ostream& out, transaction_t * xact, - bool ignore_calculated) -{ - write_binary_number(out, xact->_date); - write_binary_number(out, xact->_date_eff); - write_binary_long(out, xact->account->ident); - - if (ignore_calculated && xact->flags & TRANSACTION_CALCULATED) { - write_binary_number<unsigned char>(out, 0); - write_binary_amount(out, amount_t()); - } - else if (! xact->amount_expr.empty()) { - write_binary_number<unsigned char>(out, 1); - write_binary_string(out, xact->amount_expr); - } - else { - write_binary_number<unsigned char>(out, 0); - write_binary_amount(out, xact->amount); - } - - if (xact->cost && - (! (ignore_calculated && xact->flags & TRANSACTION_CALCULATED))) { - write_binary_bool(out, true); - write_binary_amount(out, *xact->cost); - write_binary_string(out, xact->cost_expr); - } else { - write_binary_bool(out, false); - } - - write_binary_number(out, xact->state); - write_binary_number(out, xact->flags); - write_binary_string(out, xact->note); - - write_binary_long(out, xact->beg_pos); - write_binary_long(out, xact->beg_line); - write_binary_long(out, xact->end_pos); - write_binary_long(out, xact->end_line); -} - -void write_binary_entry_base(std::ostream& out, entry_base_t * entry) -{ - write_binary_long(out, entry->src_idx); - write_binary_long(out, entry->beg_pos); - write_binary_long(out, entry->beg_line); - write_binary_long(out, entry->end_pos); - write_binary_long(out, entry->end_line); - - bool ignore_calculated = false; - for (transactions_list::const_iterator i = entry->transactions.begin(); - i != entry->transactions.end(); - i++) - if (! (*i)->amount_expr.empty()) { - ignore_calculated = true; - break; - } - - write_binary_bool(out, ignore_calculated); - - write_binary_long(out, entry->transactions.size()); - for (transactions_list::const_iterator i = entry->transactions.begin(); - i != entry->transactions.end(); - i++) - write_binary_transaction(out, *i, ignore_calculated); -} - -void write_binary_entry(std::ostream& out, entry_t * entry) -{ - write_binary_entry_base(out, entry); - write_binary_number(out, entry->_date); - write_binary_number(out, entry->_date_eff); - write_binary_string(out, entry->code); - write_binary_string(out, entry->payee); -} - -void write_binary_auto_entry(std::ostream& out, auto_entry_t * entry) -{ - write_binary_entry_base(out, entry); - write_binary_string(out, entry->predicate.expr); -} - -void write_binary_period_entry(std::ostream& out, period_entry_t * entry) -{ - write_binary_entry_base(out, entry); - write_binary_string(out, entry->period_string); -} - -void write_binary_commodity_base(std::ostream& out, commodity_base_t * commodity) -{ - commodity->ident = ++base_commodity_index; - - write_binary_string(out, commodity->symbol); - write_binary_string(out, commodity->name); - write_binary_string(out, commodity->note); - write_binary_number(out, commodity->precision); - write_binary_number(out, commodity->flags); -} - -void write_binary_commodity_base_extra(std::ostream& out, - commodity_base_t * commodity) -{ - if (commodity->history && commodity->history->bogus_time) - commodity->remove_price(commodity->history->bogus_time); - - if (! commodity->history) { - write_binary_long<unsigned long>(out, 0); - } else { - write_binary_long<unsigned long>(out, commodity->history->prices.size()); - for (history_map::const_iterator i = commodity->history->prices.begin(); - i != commodity->history->prices.end(); - i++) { - write_binary_number(out, (*i).first); - write_binary_amount(out, (*i).second); - } - write_binary_number(out, commodity->history->last_lookup); - } - - if (commodity->smaller) { - write_binary_bool(out, true); - write_binary_amount(out, *commodity->smaller); - } else { - write_binary_bool(out, false); - } - - if (commodity->larger) { - write_binary_bool(out, true); - write_binary_amount(out, *commodity->larger); - } else { - write_binary_bool(out, false); - } -} - -void write_binary_commodity(std::ostream& out, commodity_t * commodity) -{ - commodity->ident = ++commodity_index; - - write_binary_long(out, commodity->base->ident); - write_binary_string(out, commodity->qualified_symbol); -} - -void write_binary_commodity_annotated(std::ostream& out, - commodity_t * commodity) -{ - commodity->ident = ++commodity_index; - - write_binary_long(out, commodity->base->ident); - write_binary_string(out, commodity->qualified_symbol); - - annotated_commodity_t * ann_comm = - static_cast<annotated_commodity_t *>(commodity); - - write_binary_long(out, ann_comm->base->ident); - write_binary_amount(out, ann_comm->price); - write_binary_number(out, ann_comm->date); - write_binary_string(out, ann_comm->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_binary_account(std::ostream& out, account_t * account) -{ - account->ident = ++account_index; - - if (account->parent) - write_binary_long(out, account->parent->ident); - else - write_binary_long<account_t::ident_t>(out, 0xffffffff); - - write_binary_string(out, account->name); - write_binary_string(out, account->note); - write_binary_number(out, account->depth); - - write_binary_long<account_t::ident_t>(out, account->accounts.size()); - for (accounts_map::iterator i = account->accounts.begin(); - i != account->accounts.end(); - i++) - write_binary_account(out, (*i).second); -} - -void write_binary_journal(std::ostream& out, journal_t * journal) -{ - account_index = - base_commodity_index = - commodity_index = 0; - - write_binary_number_nocheck(out, binary_magic_number); - write_binary_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 (journal->sources.empty()) { - write_binary_number<unsigned short>(out, 0); - } else { - write_binary_number<unsigned short>(out, journal->sources.size()); - for (strings_list::const_iterator i = journal->sources.begin(); - i != journal->sources.end(); - i++) { - write_binary_string(out, *i); - struct stat info; - stat((*i).c_str(), &info); - write_binary_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. - write_binary_string(out, journal->price_db); - } - - ostream_pos_type data_val = out.tellp(); - write_binary_number<unsigned long>(out, 0); - - // Write out the accounts - - write_binary_long<account_t::ident_t>(out, count_accounts(journal->master)); - write_binary_account(out, journal->master); - - if (journal->basket) { - write_binary_bool(out, true); - write_binary_long(out, journal->basket->ident); - } else { - write_binary_bool(out, false); - } - - // Write out the number of entries, transactions, and amounts - - write_binary_long<unsigned long>(out, journal->entries.size()); - write_binary_long<unsigned long>(out, journal->auto_entries.size()); - write_binary_long<unsigned long>(out, journal->period_entries.size()); - - ostream_pos_type xacts_val = out.tellp(); - write_binary_number<unsigned long>(out, 0); - - ostream_pos_type bigints_val = out.tellp(); - write_binary_number<unsigned long>(out, 0); - - bigints_count = 0; - - // Write out the commodities - - write_binary_long<commodity_t::ident_t> - (out, commodity_base_t::commodities.size()); - - for (base_commodities_map::const_iterator i = - commodity_base_t::commodities.begin(); - i != commodity_base_t::commodities.end(); - i++) - write_binary_commodity_base(out, (*i).second); - - write_binary_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_binary_bool(out, false); - write_binary_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_binary_bool(out, true); - write_binary_string(out, (*i).first); // the mapping key - write_binary_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_base_t::commodities.begin(); - i != commodity_base_t::commodities.end(); - i++) - write_binary_commodity_base_extra(out, (*i).second); - - if (commodity_t::default_commodity) - write_binary_long(out, commodity_t::default_commodity->ident); - else - write_binary_long<commodity_t::ident_t>(out, 0xffffffff); - - // Write out the entries and transactions - - unsigned long xact_count = 0; - - for (entries_list::const_iterator i = journal->entries.begin(); - i != journal->entries.end(); - i++) { - write_binary_entry(out, *i); - xact_count += (*i)->transactions.size(); - } - - for (auto_entries_list::const_iterator i = journal->auto_entries.begin(); - i != journal->auto_entries.end(); - i++) { - write_binary_auto_entry(out, *i); - xact_count += (*i)->transactions.size(); - } - - for (period_entries_list::const_iterator i = journal->period_entries.begin(); - i != journal->period_entries.end(); - i++) { - write_binary_period_entry(out, *i); - xact_count += (*i)->transactions.size(); - } - - // Back-patch the count for amounts - - unsigned long data_size = (((unsigned long) out.tellp()) - - ((unsigned long) data_val) - - sizeof(unsigned long)); - out.seekp(data_val); - write_binary_number<unsigned long>(out, data_size); - out.seekp(xacts_val); - write_binary_number<unsigned long>(out, xact_count); - out.seekp(bigints_val); - write_binary_number<unsigned long>(out, bigints_count); + write_guard(out, 0x3002); } -#endif +} // namespace binary } // namespace ledger diff --git a/src/binary.h b/src/binary.h index 5d699f7b..74199c89 100644 --- a/src/binary.h +++ b/src/binary.h @@ -29,217 +29,203 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _BINARY_H -#define _BINARY_H - -#include "parser.h" +#ifndef BINARY_H +#define BINARY_H namespace ledger { - -#if 0 -class binary_parser_t : public parser_t -{ - public: - virtual bool test(std::istream& in) const; - - virtual unsigned int parse(std::istream& in, - journal_t * journal, - account_t * master = NULL, - const string * original_file = NULL); -}; -#endif +namespace binary { template <typename T> -inline void read_binary_number_nocheck(std::istream& in, T& num) { +inline void read_number_nocheck(std::istream& in, T& num) { in.read((char *)&num, sizeof(num)); } template <typename T> -inline void read_binary_number_nocheck(const char *& data, T& num) { +inline void read_number_nocheck(const char *& data, T& num) { num = *((T *) data); data += sizeof(T); } template <typename T> -inline T read_binary_number_nocheck(std::istream& in) { +inline T read_number_nocheck(std::istream& in) { T num; - read_binary_number_nocheck(in, num); + read_number_nocheck(in, num); return num; } template <typename T> -inline T read_binary_number_nocheck(const char *& data) { +inline T read_number_nocheck(const char *& data) { T num; - read_binary_number_nocheck(data, num); + read_number_nocheck(data, num); return num; } #if DEBUG_LEVEL >= ALPHA -#define read_binary_guard(in, id) \ - if (read_binary_number_nocheck<unsigned short>(in) != id) \ +#define read_guard(in, id) \ + if (read_number_nocheck<unsigned short>(in) != id) \ assert(false); #else -#define read_binary_guard(in, id) +#define read_guard(in, id) #endif template <typename T> -inline void read_binary_number(std::istream& in, T& num) { - read_binary_guard(in, 0x2003); +inline void read_number(std::istream& in, T& num) { + read_guard(in, 0x2003); in.read((char *)&num, sizeof(num)); - read_binary_guard(in, 0x2004); + read_guard(in, 0x2004); } template <typename T> -inline void read_binary_number(const char *& data, T& num) { - read_binary_guard(data, 0x2003); +inline void read_number(const char *& data, T& num) { + read_guard(data, 0x2003); num = *((T *) data); data += sizeof(T); - read_binary_guard(data, 0x2004); + read_guard(data, 0x2004); } template <typename T> -inline T read_binary_number(std::istream& in) { +inline T read_number(std::istream& in) { T num; - read_binary_number(in, num); + read_number(in, num); return num; } template <typename T> -inline T read_binary_number(const char *& data) { +inline T read_number(const char *& data) { T num; - read_binary_number(data, num); + read_number(data, num); return num; } -void read_binary_bool(std::istream& in, bool& num); -void read_binary_bool(const char *& data, bool& num); +void read_bool(std::istream& in, bool& num); +void read_bool(const char *& data, bool& num); -inline bool read_binary_bool(std::istream& in) { +inline bool read_bool(std::istream& in) { bool num; - read_binary_bool(in, num); + read_bool(in, num); return num; } -inline bool read_binary_bool(const char *& data) { +inline bool read_bool(const char *& data) { bool num; - read_binary_bool(data, num); + read_bool(data, num); return num; } template <typename T> -void read_binary_long(std::istream& in, T& num) +void read_long(std::istream& in, T& num) { - read_binary_guard(in, 0x2001); + read_guard(in, 0x2001); unsigned char len; - read_binary_number_nocheck(in, len); + read_number_nocheck(in, len); num = 0; unsigned char temp; if (len > 3) { - read_binary_number_nocheck(in, temp); + read_number_nocheck(in, temp); num |= ((unsigned long)temp) << 24; } if (len > 2) { - read_binary_number_nocheck(in, temp); + read_number_nocheck(in, temp); num |= ((unsigned long)temp) << 16; } if (len > 1) { - read_binary_number_nocheck(in, temp); + read_number_nocheck(in, temp); num |= ((unsigned long)temp) << 8; } - read_binary_number_nocheck(in, temp); + read_number_nocheck(in, temp); num |= ((unsigned long)temp); - read_binary_guard(in, 0x2002); + read_guard(in, 0x2002); } template <typename T> -void read_binary_long(const char *& data, T& num) +void read_long(const char *& data, T& num) { - read_binary_guard(data, 0x2001); + read_guard(data, 0x2001); unsigned char len; - read_binary_number_nocheck(data, len); + read_number_nocheck(data, len); num = 0; unsigned char temp; if (len > 3) { - read_binary_number_nocheck(data, temp); + read_number_nocheck(data, temp); num |= ((unsigned long)temp) << 24; } if (len > 2) { - read_binary_number_nocheck(data, temp); + read_number_nocheck(data, temp); num |= ((unsigned long)temp) << 16; } if (len > 1) { - read_binary_number_nocheck(data, temp); + read_number_nocheck(data, temp); num |= ((unsigned long)temp) << 8; } - read_binary_number_nocheck(data, temp); + read_number_nocheck(data, temp); num |= ((unsigned long)temp); - read_binary_guard(data, 0x2002); + read_guard(data, 0x2002); } template <typename T> -inline T read_binary_long(std::istream& in) { +inline T read_long(std::istream& in) { T num; - read_binary_long(in, num); + read_long(in, num); return num; } template <typename T> -inline T read_binary_long(const char *& data) { +inline T read_long(const char *& data) { T num; - read_binary_long(data, num); + read_long(data, num); return num; } -void read_binary_string(std::istream& in, string& str); -void read_binary_string(const char *& data, string& str); -void read_binary_string(const char *& data, string * str); +void read_string(std::istream& in, string& str); +void read_string(const char *& data, string& str); +void read_string(const char *& data, string * str); -inline string read_binary_string(std::istream& in) { +inline string read_string(std::istream& in) { string temp; - read_binary_string(in, temp); + read_string(in, temp); return temp; } -inline string read_binary_string(const char *& data) { +inline string read_string(const char *& data) { string temp; - read_binary_string(data, temp); + read_string(data, temp); return temp; } template <typename T> -inline void write_binary_number_nocheck(std::ostream& out, T num) { +inline void write_number_nocheck(std::ostream& out, T num) { out.write((char *)&num, sizeof(num)); } #if DEBUG_LEVEL >= ALPHA -#define write_binary_guard(out, id) \ - write_binary_number_nocheck<unsigned short>(out, id) +#define write_guard(out, id) \ + write_number_nocheck<unsigned short>(out, id) #else -#define write_binary_guard(in, id) +#define write_guard(in, id) #endif template <typename T> -inline void write_binary_number(std::ostream& out, T num) { - write_binary_guard(out, 0x2003); +inline void write_number(std::ostream& out, T num) { + write_guard(out, 0x2003); out.write((char *)&num, sizeof(num)); - write_binary_guard(out, 0x2004); + write_guard(out, 0x2004); } -void write_binary_bool(std::ostream& out, bool num); +void write_bool(std::ostream& out, bool num); template <typename T> -void write_binary_long(std::ostream& out, T num) +void write_long(std::ostream& out, T num) { - write_binary_guard(out, 0x2001); + write_guard(out, 0x2001); unsigned char len = 4; if (((unsigned long)num) < 0x00000100UL) @@ -248,36 +234,36 @@ void write_binary_long(std::ostream& out, T num) len = 2; else if (((unsigned long)num) < 0x01000000UL) len = 3; - write_binary_number_nocheck<unsigned char>(out, len); + write_number_nocheck<unsigned char>(out, len); unsigned char temp; if (len > 3) { temp = (((unsigned long)num) & 0xFF000000UL) >> 24; - write_binary_number_nocheck(out, temp); + write_number_nocheck(out, temp); } if (len > 2) { temp = (((unsigned long)num) & 0x00FF0000UL) >> 16; - write_binary_number_nocheck(out, temp); + write_number_nocheck(out, temp); } if (len > 1) { temp = (((unsigned long)num) & 0x0000FF00UL) >> 8; - write_binary_number_nocheck(out, temp); + write_number_nocheck(out, temp); } temp = (((unsigned long)num) & 0x000000FFUL); - write_binary_number_nocheck(out, temp); + write_number_nocheck(out, temp); - write_binary_guard(out, 0x2002); + write_guard(out, 0x2002); } -void write_binary_string(std::ostream& out, const string& str); - +void write_string(std::ostream& out, const string& str); +template <typename T> +inline void write_object(std::ostream& out, const T& journal) { + assert(false); +} -#if 0 -void write_binary_journal(std::ostream& out, journal_t * journal); -#endif - +} // namespace binary } // namespace ledger -#endif // _BINARY_H +#endif // BINARY_H diff --git a/src/builder.h b/src/data/builder.h index a193879e..a193879e 100644 --- a/src/builder.h +++ b/src/data/builder.h diff --git a/src/compile.cc b/src/data/compile.cc index 0b924418..0b924418 100644 --- a/src/compile.cc +++ b/src/data/compile.cc diff --git a/src/compile.h b/src/data/compile.h index d8b9f536..d8b9f536 100644 --- a/src/compile.h +++ b/src/data/compile.h diff --git a/src/document.cc b/src/data/document.cc index 120440b0..120440b0 100644 --- a/src/document.cc +++ b/src/data/document.cc diff --git a/src/document.h b/src/data/document.h index c1dcf88e..c1dcf88e 100644 --- a/src/document.h +++ b/src/data/document.h diff --git a/src/jbuilder.cc b/src/data/jbuilder.cc index f37abca5..f37abca5 100644 --- a/src/jbuilder.cc +++ b/src/data/jbuilder.cc diff --git a/src/jbuilder.h b/src/data/jbuilder.h index 4e109a35..4e109a35 100644 --- a/src/jbuilder.h +++ b/src/data/jbuilder.h diff --git a/src/journal.cc b/src/data/journal.cc index 32e45697..32e45697 100644 --- a/src/journal.cc +++ b/src/data/journal.cc diff --git a/src/journal.h b/src/data/journal.h index ed92bffb..ed92bffb 100644 --- a/src/journal.h +++ b/src/data/journal.h diff --git a/src/node.cc b/src/data/node.cc index 0ca0a04c..0ca0a04c 100644 --- a/src/node.cc +++ b/src/data/node.cc diff --git a/src/node.h b/src/data/node.h index b0324ca0..b0324ca0 100644 --- a/src/node.h +++ b/src/data/node.h diff --git a/src/parser.h b/src/data/parser.h index ecc73a6f..ecc73a6f 100644 --- a/src/parser.h +++ b/src/data/parser.h diff --git a/src/textual.cc b/src/data/textual.cc index 8fdd4db1..8fdd4db1 100644 --- a/src/textual.cc +++ b/src/data/textual.cc diff --git a/src/textual.h b/src/data/textual.h index f4d81f19..f4d81f19 100644 --- a/src/textual.h +++ b/src/data/textual.h diff --git a/src/fdstream.hpp b/src/driver/fdstream.hpp index ffcf5709..ffcf5709 100644 --- a/src/fdstream.hpp +++ b/src/driver/fdstream.hpp diff --git a/src/main.cc b/src/driver/main.cc index 469bb5ee..469bb5ee 100644 --- a/src/main.cc +++ b/src/driver/main.cc diff --git a/src/option.cc b/src/driver/option.cc index 0b7f7ef9..0b7f7ef9 100644 --- a/src/option.cc +++ b/src/driver/option.cc diff --git a/src/option.h b/src/driver/option.h index d26c8417..d26c8417 100644 --- a/src/option.h +++ b/src/driver/option.h diff --git a/src/report.cc b/src/driver/report.cc index ee3382da..ee3382da 100644 --- a/src/report.cc +++ b/src/driver/report.cc diff --git a/src/report.h b/src/driver/report.h index b435341b..b435341b 100644 --- a/src/report.h +++ b/src/driver/report.h diff --git a/src/session.cc b/src/driver/session.cc index 71f5f349..71f5f349 100644 --- a/src/session.cc +++ b/src/driver/session.cc diff --git a/src/session.h b/src/driver/session.h index 43d7d722..43d7d722 100644 --- a/src/session.h +++ b/src/driver/session.h diff --git a/src/balance.cc b/src/numerics/balance.cc index 80637221..80637221 100644 --- a/src/balance.cc +++ b/src/numerics/balance.cc diff --git a/src/commodity.cc b/src/numerics/commodity.cc index 8ab518ee..8ab518ee 100644 --- a/src/commodity.cc +++ b/src/numerics/commodity.cc diff --git a/src/commodity.h b/src/numerics/commodity.h index 6212e743..6212e743 100644 --- a/src/commodity.h +++ b/src/numerics/commodity.h diff --git a/src/value.cc b/src/numerics/value.cc index a938b1bc..a938b1bc 100644 --- a/src/value.cc +++ b/src/numerics/value.cc diff --git a/src/value.h b/src/numerics/value.h index 28024767..28024767 100644 --- a/src/value.h +++ b/src/numerics/value.h diff --git a/src/py_amount.cc b/src/python/py_amount.cc index 07be1512..07be1512 100644 --- a/src/py_amount.cc +++ b/src/python/py_amount.cc diff --git a/src/py_commodity.cc b/src/python/py_commodity.cc index f857a448..f857a448 100644 --- a/src/py_commodity.cc +++ b/src/python/py_commodity.cc diff --git a/src/py_times.cc b/src/python/py_times.cc index f7672dcc..f7672dcc 100644 --- a/src/py_times.cc +++ b/src/python/py_times.cc diff --git a/src/py_utils.cc b/src/python/py_utils.cc index 73961d43..73961d43 100644 --- a/src/py_utils.cc +++ b/src/python/py_utils.cc diff --git a/src/pyfstream.h b/src/python/pyfstream.h index f4650e0a..f4650e0a 100644 --- a/src/pyfstream.h +++ b/src/python/pyfstream.h diff --git a/src/pyinterp.cc b/src/python/pyinterp.cc index d521a0ee..d521a0ee 100644 --- a/src/pyinterp.cc +++ b/src/python/pyinterp.cc diff --git a/src/pyinterp.h b/src/python/pyinterp.h index 3d69d972..3d69d972 100644 --- a/src/pyinterp.h +++ b/src/python/pyinterp.h diff --git a/src/pyledger.cc b/src/python/pyledger.cc index ebbdc82e..ebbdc82e 100644 --- a/src/pyledger.cc +++ b/src/python/pyledger.cc diff --git a/src/pyledger.h b/src/python/pyledger.h index 3ab82558..3ab82558 100644 --- a/src/pyledger.h +++ b/src/python/pyledger.h diff --git a/src/pyutils.h b/src/python/pyutils.h index 216af8b7..216af8b7 100644 --- a/src/pyutils.h +++ b/src/python/pyutils.h diff --git a/src/tuples.hpp b/src/python/tuples.hpp index 18cbdb83..18cbdb83 100644 --- a/src/tuples.hpp +++ b/src/python/tuples.hpp diff --git a/src/abbrev.cc b/src/traversal/abbrev.cc index 089b8342..089b8342 100644 --- a/src/abbrev.cc +++ b/src/traversal/abbrev.cc diff --git a/src/abbrev.h b/src/traversal/abbrev.h index ad880e45..ad880e45 100644 --- a/src/abbrev.h +++ b/src/traversal/abbrev.h diff --git a/src/transform.cc b/src/traversal/transform.cc index 3331c2f3..3331c2f3 100644 --- a/src/transform.cc +++ b/src/traversal/transform.cc diff --git a/src/transform.h b/src/traversal/transform.h index 158b9b6a..158b9b6a 100644 --- a/src/transform.h +++ b/src/traversal/transform.h diff --git a/src/xpath.cc b/src/traversal/xpath.cc index ed4c0544..ed4c0544 100644 --- a/src/xpath.cc +++ b/src/traversal/xpath.cc diff --git a/src/xpath.h b/src/traversal/xpath.h index c9f299fc..c9f299fc 100644 --- a/src/xpath.h +++ b/src/traversal/xpath.h diff --git a/src/context.h b/src/utility/context.h index 934c5aee..934c5aee 100644 --- a/src/context.h +++ b/src/utility/context.h diff --git a/src/flags.h b/src/utility/flags.h index 5ae8b60f..5ae8b60f 100644 --- a/src/flags.h +++ b/src/utility/flags.h diff --git a/src/mask.cc b/src/utility/mask.cc index 959df8ea..959df8ea 100644 --- a/src/mask.cc +++ b/src/utility/mask.cc diff --git a/src/mask.h b/src/utility/mask.h index daae014f..daae014f 100644 --- a/src/mask.h +++ b/src/utility/mask.h diff --git a/src/system.hh b/src/utility/system.hh index 96c6575c..96c6575c 100644 --- a/src/system.hh +++ b/src/utility/system.hh diff --git a/src/times.cc b/src/utility/times.cc index fc6f2f1b..fc6f2f1b 100644 --- a/src/times.cc +++ b/src/utility/times.cc diff --git a/src/times.h b/src/utility/times.h index 949d8031..949d8031 100644 --- a/src/times.h +++ b/src/utility/times.h diff --git a/src/utils.cc b/src/utility/utils.cc index e9c41cc9..e9c41cc9 100644 --- a/src/utils.cc +++ b/src/utility/utils.cc diff --git a/src/utils.h b/src/utils.h index 7ad1033e..1420f9ca 100644 --- a/src/utils.h +++ b/src/utils.h @@ -49,7 +49,7 @@ * - exception framework * - date/time type * - supports_flags<> for objects that use flags - * - scoped_execute<> for guaranteeing code execution + * - push_variable<> for restoring variable values */ #ifndef _UTILS_H @@ -512,7 +512,7 @@ inline void throw_unexpected_error(char, char) { #include "times.h" #include "flags.h" -#include "scoped_execute.h" +#include "pushvar.h" /********************************************************************** * |