From d02f74efea3e6b631810bfd3c3d8adcaa4299902 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sat, 4 Mar 2006 18:34:29 +0000 Subject: *** empty log message *** --- amount.cc | 39 +++++++++++++++++++++++---------------- amount.h | 30 +++++++++++++++--------------- binary.cc | 18 ++++++++++++++---- qif.cc | 4 ++-- 4 files changed, 54 insertions(+), 37 deletions(-) diff --git a/amount.cc b/amount.cc index 3a8c7e2a..31187a1c 100644 --- a/amount.cc +++ b/amount.cc @@ -15,11 +15,11 @@ namespace ledger { class amount_t::bigint_t { public: - mpz_t val; - unsigned short prec; - unsigned short flags; - unsigned int ref; - unsigned int index; + mpz_t val; + unsigned char prec; + unsigned char flags; + unsigned int ref; + unsigned int index; bigint_t() : prec(0), flags(0), ref(1), index(0) { mpz_init(val); @@ -631,8 +631,8 @@ std::string amount_t::quantity_string() const // Ensure the value is rounded to the commodity's precision before // outputting it. NOTE: `rquotient' is used here as a temp variable! - commodity_t& comm(commodity()); - unsigned short precision; + commodity_t& comm(commodity()); + unsigned char precision; if (! comm || quantity->flags & BIGINT_KEEP_PREC) { mpz_ui_pow_ui(divisor, 10, quantity->prec); @@ -737,8 +737,8 @@ std::ostream& operator<<(std::ostream& _out, const amount_t& amt) // Ensure the value is rounded to the commodity's precision before // outputting it. NOTE: `rquotient' is used here as a temp variable! - commodity_t& comm(base.commodity()); - unsigned short precision; + commodity_t& comm(base.commodity()); + unsigned char precision; if (! comm || base.quantity->flags & BIGINT_KEEP_PREC) { mpz_ui_pow_ui(divisor, 10, base.quantity->prec); @@ -1007,7 +1007,7 @@ void parse_annotations(std::istream& in, const std::string& symbol, << " tag " << tag); } -void amount_t::parse(std::istream& in, unsigned short flags) +void amount_t::parse(std::istream& in, unsigned char flags) { // The possible syntax for an amount is: // @@ -1168,7 +1168,7 @@ void amount_t::reduce() } } -void amount_t::parse(const std::string& str, unsigned short flags) +void amount_t::parse(const std::string& str, unsigned char flags) { std::istringstream stream(str); parse(stream, flags); @@ -1209,7 +1209,6 @@ void amount_t::read_quantity(char *& data) else if (byte == 1) { quantity = new((bigint_t *)bigints_next) bigint_t; bigints_next += sizeof(bigint_t); - quantity->flags |= BIGINT_BULK_ALLOC; unsigned short len = *((unsigned short *) data); data += sizeof(unsigned short); @@ -1221,8 +1220,11 @@ void amount_t::read_quantity(char *& data) if (negative) mpz_neg(MPZ(quantity), MPZ(quantity)); - quantity->prec = *((unsigned short *) data); - data += sizeof(unsigned short); + quantity->prec = *((unsigned char *) data); + data += sizeof(unsigned char); + quantity->flags = *((unsigned char *) data); + data += sizeof(unsigned char); + quantity->flags |= BIGINT_BULK_ALLOC; } else { unsigned int index = *((unsigned int *) data); data += sizeof(unsigned int); @@ -1258,6 +1260,7 @@ void amount_t::read_quantity(std::istream& in) mpz_neg(MPZ(quantity), MPZ(quantity)); in.read((char *)&quantity->prec, sizeof(quantity->prec)); + in.read((char *)&quantity->flags, sizeof(quantity->flags)); } else { assert(0); @@ -1294,6 +1297,9 @@ void amount_t::write_quantity(std::ostream& out) const out.write(&byte, sizeof(byte)); out.write((char *)&quantity->prec, sizeof(quantity->prec)); + unsigned char flags = quantity->flags & ~BIGINT_BULK_ALLOC; + assert(sizeof(flags) == sizeof(quantity->flags)); + out.write((char *)&flags, sizeof(flags)); } else { assert(quantity->ref > 1); @@ -1330,6 +1336,7 @@ void amount_t::annotate_commodity(const amount_t& price, } else { this_base = &commodity(); } + assert(this_base); DEBUG_PRINT("amounts.commodities", "Annotating commodity for amount " << *this << std::endl @@ -1364,6 +1371,7 @@ amount_t amount_t::reduce_commodity(const bool keep_price, annotated_commodity_t& ann_comm(static_cast(commodity())); + assert(ann_comm.base); commodity_t * new_comm; @@ -1377,7 +1385,6 @@ amount_t amount_t::reduce_commodity(const bool keep_price, } else { new_comm = commodity_t::find_or_create(ann_comm.base_symbol()); } - assert(new_comm); amount_t temp(*this); @@ -1694,7 +1701,7 @@ int py_amount_quantity(amount_t& amount) } void py_parse_1(amount_t& amount, const std::string& str, - unsigned short flags) { + unsigned char flags) { amount.parse(str, flags); } void py_parse_2(amount_t& amount, const std::string& str) { diff --git a/amount.h b/amount.h index 3fb9eed3..017f5472 100644 --- a/amount.h +++ b/amount.h @@ -249,8 +249,8 @@ class amount_t #define AMOUNT_PARSE_NO_MIGRATE 0x01 #define AMOUNT_PARSE_NO_REDUCE 0x02 - void parse(std::istream& in, unsigned short flags = 0); - void parse(const std::string& str, unsigned short flags = 0); + void parse(std::istream& in, unsigned char flags = 0); + void parse(const std::string& str, unsigned char flags = 0); void reduce(); void read_quantity(char *& data); @@ -349,13 +349,13 @@ class commodity_base_t typedef unsigned long ident_t; - ident_t ident; - std::string name; - std::string note; - unsigned short precision; - unsigned short flags; - amount_t * smaller; - amount_t * larger; + ident_t ident; + std::string name; + std::string note; + unsigned char precision; + unsigned char flags; + amount_t * smaller; + amount_t * larger; commodity_base_t() : precision(0), flags(COMMODITY_STYLE_DEFAULTS), history(NULL), smaller(NULL), larger(NULL) {} @@ -479,23 +479,23 @@ class commodity_t ptr->note = arg; } - unsigned short precision() const { + unsigned char precision() const { return ptr->precision; } - void set_precision(unsigned short arg) { + void set_precision(unsigned char arg) { ptr->precision = arg; } - unsigned short flags() const { + unsigned char flags() const { return ptr->flags; } - void set_flags(unsigned short arg) { + void set_flags(unsigned char arg) { ptr->flags = arg; } - void add_flags(unsigned short arg) { + void add_flags(unsigned char arg) { ptr->flags |= arg; } - void drop_flags(unsigned short arg) { + void drop_flags(unsigned char arg) { ptr->flags &= ~arg; } diff --git a/binary.cc b/binary.cc index f5d9c467..8816078d 100644 --- a/binary.cc +++ b/binary.cc @@ -12,9 +12,9 @@ namespace ledger { static unsigned long binary_magic_number = 0xFFEED765; #ifdef DEBUG_ENABLED -static unsigned long format_version = 0x00020605; +static unsigned long format_version = 0x00020607; #else -static unsigned long format_version = 0x00020604; +static unsigned long format_version = 0x00020606; #endif static account_t ** accounts; @@ -475,6 +475,8 @@ inline commodity_t * read_binary_commodity_annotated(char *& data) read_binary_string(data, commodity->qualified_symbol); commodity->annotated = true; + commodity->base = + commodities[read_binary_long(data) - 1]; read_binary_amount(data, commodity->price); read_binary_long(data, commodity->date); read_binary_string(data, commodity->tag); @@ -998,6 +1000,7 @@ void write_binary_commodity_annotated(std::ostream& out, annotated_commodity_t * ann_comm = static_cast(commodity); + write_binary_long(out, ann_comm->base->ident); write_binary_amount(out, ann_comm->price); write_binary_long(out, ann_comm->date); write_binary_string(out, ann_comm->tag); @@ -1116,10 +1119,17 @@ void write_binary_journal(std::ostream& out, journal_t * journal) for (commodities_map::const_iterator i = commodity_t::commodities.begin(); i != commodity_t::commodities.end(); i++) { - write_binary_number(out, (*i).second->annotated ? 1 : 0); if (! (*i).second->annotated) { + write_binary_number(out, 0); write_binary_commodity(out, (*i).second); - } else { + } + } + + for (commodities_map::const_iterator i = commodity_t::commodities.begin(); + i != commodity_t::commodities.end(); + i++) { + if ((*i).second->annotated) { + write_binary_number(out, 1); write_binary_string(out, (*i).first); // the mapping key write_binary_commodity_annotated(out, (*i).second); } diff --git a/qif.cc b/qif.cc index 5d4eb3a5..cd368dc2 100644 --- a/qif.cc +++ b/qif.cc @@ -115,8 +115,8 @@ unsigned int qif_parser_t::parse(std::istream& in, get_line(in); xact->amount.parse(line); - unsigned long flags = xact->amount.commodity().flags(); - unsigned short prec = xact->amount.commodity().precision(); + unsigned char flags = xact->amount.commodity().flags(); + unsigned char prec = xact->amount.commodity().precision(); if (! def_commodity) { def_commodity = commodity_t::find_or_create("$"); -- cgit v1.2.3