From da2f415f148b69898d71405235eaa8b36aa21b3b Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 9 Jun 2010 06:36:40 -0400 Subject: In value_t::print, pass along display flags --- src/value.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/value.cc b/src/value.cc index e5ced56e..64193db6 100644 --- a/src/value.cc +++ b/src/value.cc @@ -1726,7 +1726,7 @@ void value_t::print(std::ostream& out, out << 0; } else { std::ostringstream buf; - as_amount().print(buf, flags & AMOUNT_PRINT_NO_COMPUTED_ANNOTATIONS); + as_amount().print(buf, flags); justify(out, buf.str(), first_width, flags & AMOUNT_PRINT_RIGHT_JUSTIFY, flags & AMOUNT_PRINT_COLORIZE && as_amount().sign() < 0); } -- cgit v1.2.3 From 8637dd6ccf7afc71d911cd66c4a55a1308596dbb Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 9 Jun 2010 06:45:07 -0400 Subject: New display flag AMOUNT_PRINT_ELIDE_COMMODITY_QUOTES This is used by reports like register and balance so that separated commodities without spaces in them needed be surrounded by quotes. It will still occur in most other places. Fixes #200 / F82CF11F-BFD9-4512-A562-202B04B68051 --- src/amount.cc | 4 ++-- src/amount.h | 1 + src/commodity.cc | 11 +++++++++++ src/commodity.h | 5 +---- src/report.cc | 2 +- test/regress/25A099C9.test | 12 ++++++------ 6 files changed, 22 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/amount.cc b/src/amount.cc index 105b54ef..01caf4ac 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -1233,7 +1233,7 @@ void amount_t::print(std::ostream& _out, const uint_least8_t flags) const commodity_t& comm(commodity()); if (! comm.has_flags(COMMODITY_STYLE_SUFFIXED)) { - comm.print(out); + comm.print(out, flags & AMOUNT_PRINT_ELIDE_COMMODITY_QUOTES); if (comm.has_flags(COMMODITY_STYLE_SEPARATED)) out << " "; } @@ -1244,7 +1244,7 @@ void amount_t::print(std::ostream& _out, const uint_least8_t flags) const if (comm.has_flags(COMMODITY_STYLE_SUFFIXED)) { if (comm.has_flags(COMMODITY_STYLE_SEPARATED)) out << " "; - comm.print(out); + comm.print(out, flags & AMOUNT_PRINT_ELIDE_COMMODITY_QUOTES); } // If there are any annotations associated with this commodity, output them diff --git a/src/amount.h b/src/amount.h index 09c9dc49..be4c5e45 100644 --- a/src/amount.h +++ b/src/amount.h @@ -680,6 +680,7 @@ public: #define AMOUNT_PRINT_RIGHT_JUSTIFY 0x01 #define AMOUNT_PRINT_COLORIZE 0x02 #define AMOUNT_PRINT_NO_COMPUTED_ANNOTATIONS 0x04 +#define AMOUNT_PRINT_ELIDE_COMMODITY_QUOTES 0x08 void print(std::ostream& out, const uint_least8_t flags = AMOUNT_PRINT_NO_FLAGS) const; diff --git a/src/commodity.cc b/src/commodity.cc index 9a757395..44d14c56 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -642,6 +642,17 @@ void commodity_t::parse_symbol(char *& p, string& symbol) throw_(amount_error, _("Failed to parse commodity")); } +void commodity_t::print(std::ostream& out, bool elide_quotes) const +{ + string sym = symbol(); + if (elide_quotes && has_flags(COMMODITY_STYLE_SEPARATED) && + ! sym.empty() && sym[0] == '"' && ! std::strchr(sym.c_str(), ' ')) { + DEBUG("foo", "contracting " << sym << " to " << string(sym, 1, sym.length() - 2)); + out << string(sym, 1, sym.length() - 2); + } else + out << sym; +} + bool commodity_t::valid() const { if (symbol().empty() && this != pool().null_commodity) { diff --git a/src/commodity.h b/src/commodity.h index ae7d9d66..42f15f33 100644 --- a/src/commodity.h +++ b/src/commodity.h @@ -391,10 +391,7 @@ public: return temp; } - void print(std::ostream& out) const { - out << symbol(); - } - + void print(std::ostream& out, bool elide_quotes = false) const; bool valid() const; struct compare_by_commodity { diff --git a/src/report.cc b/src/report.cc index 662db746..bed3ef5c 100644 --- a/src/report.cc +++ b/src/report.cc @@ -598,7 +598,7 @@ value_t report_t::fn_justify(call_scope_t& scope) { interactive_t args(scope, "vl&lbb"); - uint_least8_t flags(AMOUNT_PRINT_NO_FLAGS); + uint_least8_t flags(AMOUNT_PRINT_ELIDE_COMMODITY_QUOTES); if (args.has(3) && args.get(3)) flags |= AMOUNT_PRINT_RIGHT_JUSTIFY; diff --git a/test/regress/25A099C9.test b/test/regress/25A099C9.test index 345eb45f..b3e23a6c 100644 --- a/test/regress/25A099C9.test +++ b/test/regress/25A099C9.test @@ -4,16 +4,16 @@ >>>2 While parsing file "$sourcepath/src/amount.h", line 66: Error: No quantity specified for amount -While parsing file "$sourcepath/src/amount.h", line 725: +While parsing file "$sourcepath/src/amount.h", line 726: Error: Invalid date/time: line amount_t amoun -While parsing file "$sourcepath/src/amount.h", line 731: +While parsing file "$sourcepath/src/amount.h", line 732: Error: Invalid date/time: line string amount_ -While parsing file "$sourcepath/src/amount.h", line 737: +While parsing file "$sourcepath/src/amount.h", line 738: Error: Invalid date/time: line string amount_ -While parsing file "$sourcepath/src/amount.h", line 743: +While parsing file "$sourcepath/src/amount.h", line 744: Error: Invalid date/time: line string amount_ -While parsing file "$sourcepath/src/amount.h", line 749: +While parsing file "$sourcepath/src/amount.h", line 750: Error: Invalid date/time: line std::ostream& -While parsing file "$sourcepath/src/amount.h", line 756: +While parsing file "$sourcepath/src/amount.h", line 757: Error: Invalid date/time: line std::istream& === 7 -- cgit v1.2.3 From c28fad384c28fb929fc8ea69854496141bdf306a Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 9 Jun 2010 07:38:45 -0400 Subject: The --anon option now anonymizes commodities Fixes #227 / 1C90D8AF-830E-43C2-A5B7-D382F68EBDE3 --- src/filters.cc | 52 +++++++++++++++++++++++++++++++++++++++++++++ src/filters.h | 12 ++++++++--- src/pool.cc | 16 +++++++------- test/baseline/opt-anon.test | 6 +++--- 4 files changed, 72 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/filters.cc b/src/filters.cc index 1385a3f0..b818c7d0 100644 --- a/src/filters.cc +++ b/src/filters.cc @@ -36,6 +36,7 @@ #include "journal.h" #include "report.h" #include "compare.h" +#include "pool.h" namespace ledger { @@ -216,6 +217,43 @@ namespace { } } +void anonymize_posts::render_commodity(amount_t& amt) +{ + commodity_t& comm(amt.commodity()); + + std::size_t id; + bool newly_added = false; + + commodity_index_map::iterator i = comms.find(&comm); + if (i == comms.end()) { + id = next_comm_id++; + newly_added = true; + comms.insert(commodity_index_map::value_type(&comm, id)); + } else { + id = (*i).second; + } + + std::ostringstream buf; + do { + buf << static_cast('A' + (id % 26)); + id /= 26; + } + while (id > 0); + + if (amt.has_annotation()) + amt.set_commodity + (*commodity_pool_t::current_pool->find_or_create(buf.str(), + amt.annotation())); + else + amt.set_commodity + (*commodity_pool_t::current_pool->find_or_create(buf.str())); + + if (newly_added) { + amt.commodity().set_flags(comm.flags()); + amt.commodity().set_precision(comm.precision()); + } +} + void anonymize_posts::operator()(post_t& post) { SHA1 sha; @@ -258,6 +296,20 @@ void anonymize_posts::operator()(post_t& post) temp.note = none; temp.add_flags(POST_ANONYMIZED); + DEBUG("foo", "1.rendering amount: " << temp.amount); + render_commodity(temp.amount); + DEBUG("foo", "2.rendering amount: " << temp.amount); + if (temp.amount.has_annotation()) { + temp.amount.annotation().tag = none; + if (temp.amount.annotation().price) + render_commodity(*temp.amount.annotation().price); + } + + if (temp.cost) + render_commodity(*temp.cost); + if (temp.assigned_amount) + render_commodity(*temp.assigned_amount); + (*handler)(temp); } diff --git a/src/filters.h b/src/filters.h index 3f2f2145..ffca9d1e 100644 --- a/src/filters.h +++ b/src/filters.h @@ -334,20 +334,26 @@ public: class anonymize_posts : public item_handler { - temporaries_t temps; - xact_t * last_xact; + typedef std::map commodity_index_map; + + temporaries_t temps; + commodity_index_map comms; + std::size_t next_comm_id; + xact_t * last_xact; anonymize_posts(); public: anonymize_posts(post_handler_ptr handler) - : item_handler(handler), last_xact(NULL) { + : item_handler(handler), next_comm_id(0), last_xact(NULL) { TRACE_CTOR(anonymize_posts, "post_handler_ptr"); } virtual ~anonymize_posts() { TRACE_DTOR(anonymize_posts); } + void render_commodity(amount_t& amt); + virtual void operator()(post_t& post); virtual void clear() { diff --git a/src/pool.cc b/src/pool.cc index ad97a9c6..5285be92 100644 --- a/src/pool.cc +++ b/src/pool.cc @@ -57,7 +57,7 @@ commodity_t * commodity_pool_t::create(const string& symbol) base_commodity(new commodity_t::base_t(symbol)); std::auto_ptr commodity(new commodity_t(this, base_commodity)); - DEBUG("amounts.commodities", "Creating base commodity " << symbol); + DEBUG("pool.commodities", "Creating base commodity " << symbol); // Create the "qualified symbol" version of this commodity's symbol if (commodity_t::symbol_needs_quotes(symbol)) { @@ -66,7 +66,7 @@ commodity_t * commodity_pool_t::create(const string& symbol) *commodity->qualified_symbol += "\""; } - DEBUG("amounts.commodities", + DEBUG("pool.commodities", "Creating commodity '" << commodity->symbol() << "'"); std::pair result @@ -79,7 +79,7 @@ commodity_t * commodity_pool_t::create(const string& symbol) commodity_t * commodity_pool_t::find_or_create(const string& symbol) { - DEBUG("amounts.commodities", "Find-or-create commodity " << symbol); + DEBUG("pool.commodities", "Find-or-create commodity " << symbol); commodity_t * commodity = find(symbol); if (commodity) @@ -89,7 +89,7 @@ commodity_t * commodity_pool_t::find_or_create(const string& symbol) commodity_t * commodity_pool_t::find(const string& symbol) { - DEBUG("amounts.commodities", "Find commodity " << symbol); + DEBUG("pool.commodities", "Find commodity " << symbol); commodities_map::const_iterator i = commodities.find(symbol); if (i != commodities.end()) @@ -124,10 +124,10 @@ string commodity_pool_t::make_qualified_name(const commodity_t& comm, #if defined(DEBUG_ON) if (comm.qualified_symbol) - DEBUG("amounts.commodities", "make_qualified_name for " + DEBUG("pool.commodities", "make_qualified_name for " << *comm.qualified_symbol << std::endl << details); #endif - DEBUG("amounts.commodities", "qualified_name is " << name.str()); + DEBUG("pool.commodities", "qualified_name is " << name.str()); return name.str(); } @@ -156,7 +156,7 @@ commodity_t * commodity_pool_t::find_or_create(const string& symbol, const annotation_t& details) { - commodity_t * comm = find(symbol); + commodity_t * comm = find_or_create(symbol); if (! comm) return NULL; @@ -181,7 +181,7 @@ commodity_pool_t::create(commodity_t& comm, commodity->qualified_symbol = comm.symbol(); assert(! commodity->qualified_symbol->empty()); - DEBUG("amounts.commodities", "Creating annotated commodity " + DEBUG("pool.commodities", "Creating annotated commodity " << "symbol " << commodity->symbol() << " key " << mapping_key << std::endl << details); diff --git a/test/baseline/opt-anon.test b/test/baseline/opt-anon.test index 6fe6b75f..345d981f 100644 --- a/test/baseline/opt-anon.test +++ b/test/baseline/opt-anon.test @@ -4,8 +4,8 @@ reg --anon Assets:Investments:Vanguard:VMMXX 0.350 VMMXX @ $1.00 Income:Dividends:Vanguard:VMMXX $-0.35 >>>1 -07-Feb-02 6a93dcb3 da:20:5d:27:988a9c3a 0.350 VMMXX 0.350 VMMXX - da:1c:b6:27:988a9c3a $-0.35 $-0.35 - 0.350 VMMXX +07-Feb-02 6a93dcb3 da:20:5d:27:988a9c3a 0.350 A 0.350 A + da:1c:b6:27:988a9c3a B-0.35 0.350 A + B-0.35 >>>2 === 0 -- cgit v1.2.3