diff options
Diffstat (limited to 'src/annotate.cc')
-rw-r--r-- | src/annotate.cc | 120 |
1 files changed, 70 insertions, 50 deletions
diff --git a/src/annotate.cc b/src/annotate.cc index feb3b3ca..33c0aebb 100644 --- a/src/annotate.cc +++ b/src/annotate.cc @@ -47,59 +47,50 @@ void annotation_t::parse(std::istream& in) char c = peek_next_nonws(in); if (c == '{') { if (price) - throw_(amount_error, _("Commodity specifies more than one price")); + throw_(amount_error, _("Commodity specifies more than one price")); in.get(c); c = peek_next_nonws(in); if (c == '=') { - in.get(c); - add_flags(ANNOTATION_PRICE_FIXATED); + in.get(c); + add_flags(ANNOTATION_PRICE_FIXATED); } READ_INTO(in, buf, 255, c, c != '}'); if (c == '}') - in.get(c); + in.get(c); else - throw_(amount_error, _("Commodity price lacks closing brace")); + throw_(amount_error, _("Commodity price lacks closing brace")); amount_t temp; temp.parse(buf, PARSE_NO_MIGRATE); DEBUG("commodity.annotations", "Parsed annotation price: " << temp); - - // Since this price will maintain its own precision, make sure - // it is at least as large as the base commodity, since the user - // may have only specified {$1} or something similar. - - if (temp.has_commodity() && - temp.precision() > temp.commodity().precision()) - temp = temp.rounded(); // no need to retain individual precision - price = temp; } else if (c == '[') { if (date) - throw_(amount_error, _("Commodity specifies more than one date")); + throw_(amount_error, _("Commodity specifies more than one date")); in.get(c); READ_INTO(in, buf, 255, c, c != ']'); if (c == ']') - in.get(c); + in.get(c); else - throw_(amount_error, _("Commodity date lacks closing bracket")); + throw_(amount_error, _("Commodity date lacks closing bracket")); date = parse_date(buf); } else if (c == '(') { if (tag) - throw_(amount_error, _("Commodity specifies more than one tag")); + throw_(amount_error, _("Commodity specifies more than one tag")); in.get(c); READ_INTO(in, buf, 255, c, c != ')'); if (c == ')') - in.get(c); + in.get(c); else - throw_(amount_error, _("Commodity tag lacks closing parenthesis")); + throw_(amount_error, _("Commodity tag lacks closing parenthesis")); tag = buf; } @@ -113,30 +104,34 @@ void annotation_t::parse(std::istream& in) #if defined(DEBUG_ON) if (SHOW_DEBUG("amounts.commodities") && *this) { DEBUG("amounts.commodities", - "Parsed commodity annotations: " << std::endl << *this); + "Parsed commodity annotations: " << std::endl << *this); } #endif } -void annotation_t::print(std::ostream& out, bool keep_base) const +void annotation_t::print(std::ostream& out, bool keep_base, + bool no_computed_annotations) const { - if (price) + if (price && + (! no_computed_annotations || ! has_flags(ANNOTATION_PRICE_CALCULATED))) out << " {" - << (has_flags(ANNOTATION_PRICE_FIXATED) ? "=" : "") - << (keep_base ? *price : price->unreduced()).rounded() - << '}'; + << (has_flags(ANNOTATION_PRICE_FIXATED) ? "=" : "") + << (keep_base ? *price : price->unreduced()) + << '}'; - if (date) + if (date && + (! no_computed_annotations || ! has_flags(ANNOTATION_DATE_CALCULATED))) out << " [" << format_date(*date, FMT_WRITTEN) << ']'; - if (tag) + if (tag && + (! no_computed_annotations || ! has_flags(ANNOTATION_TAG_CALCULATED))) out << " (" << *tag << ')'; } bool keep_details_t::keep_all(const commodity_t& comm) const { return (! comm.has_annotation() || - (keep_price && keep_date && keep_tag && ! only_actuals)); + (keep_price && keep_date && keep_tag && ! only_actuals)); } bool keep_details_t::keep_any(const commodity_t& comm) const @@ -164,22 +159,34 @@ commodity_t& annotated_commodity_t::strip_annotations(const keep_details_t& what_to_keep) { DEBUG("commodity.annotated.strip", - "Reducing commodity " << *this << std::endl - << " keep price " << what_to_keep.keep_price << " " - << " keep date " << what_to_keep.keep_date << " " - << " keep tag " << what_to_keep.keep_tag); + "Reducing commodity " << *this << std::endl + << " keep price " << what_to_keep.keep_price << " " + << " keep date " << what_to_keep.keep_date << " " + << " keep tag " << what_to_keep.keep_tag); commodity_t * new_comm; - bool keep_price = (what_to_keep.keep_price && - (! what_to_keep.only_actuals || - ! details.has_flags(ANNOTATION_PRICE_CALCULATED))); - bool keep_date = (what_to_keep.keep_date && - (! what_to_keep.only_actuals || - ! details.has_flags(ANNOTATION_DATE_CALCULATED))); - bool keep_tag = (what_to_keep.keep_tag && - (! what_to_keep.only_actuals || - ! details.has_flags(ANNOTATION_TAG_CALCULATED))); + bool keep_price = + ((what_to_keep.keep_price || + (details.has_flags(ANNOTATION_PRICE_FIXATED) && + has_flags(COMMODITY_SAW_ANN_PRICE_FLOAT) && + has_flags(COMMODITY_SAW_ANN_PRICE_FIXATED))) && + (! what_to_keep.only_actuals || + ! details.has_flags(ANNOTATION_PRICE_CALCULATED))); + bool keep_date = + (what_to_keep.keep_date && + (! what_to_keep.only_actuals || + ! details.has_flags(ANNOTATION_DATE_CALCULATED))); + bool keep_tag = + (what_to_keep.keep_tag && + (! what_to_keep.only_actuals || + ! details.has_flags(ANNOTATION_TAG_CALCULATED))); + + DEBUG("commodity.annotated.strip", + "Reducing commodity " << *this << std::endl + << " keep price " << keep_price << " " + << " keep date " << keep_date << " " + << " keep tag " << keep_tag); if ((keep_price && details.price) || (keep_date && details.date) || @@ -187,19 +194,32 @@ annotated_commodity_t::strip_annotations(const keep_details_t& what_to_keep) { new_comm = pool().find_or_create (referent(), annotation_t(keep_price ? details.price : none, - keep_date ? details.date : none, - keep_tag ? details.tag : none)); - } else { - new_comm = &referent(); + keep_date ? details.date : none, + keep_tag ? details.tag : none)); + + // Transfer over any relevant annotation flags, as they still apply. + if (new_comm->annotated) { + annotation_t& new_details(as_annotated_commodity(*new_comm).details); + if (keep_price) + new_details.add_flags(details.flags() & + (ANNOTATION_PRICE_CALCULATED | + ANNOTATION_PRICE_FIXATED)); + if (keep_date) + new_details.add_flags(details.flags() & ANNOTATION_DATE_CALCULATED); + if (keep_tag) + new_details.add_flags(details.flags() & ANNOTATION_TAG_CALCULATED); + } + + return *new_comm; } - assert(new_comm); - return *new_comm; + return referent(); } -void annotated_commodity_t::write_annotations(std::ostream& out) const +void annotated_commodity_t::write_annotations + (std::ostream& out, bool no_computed_annotations) const { - details.print(out, pool().keep_base); + details.print(out, pool().keep_base, no_computed_annotations); } } // namespace ledger |