diff options
author | John Wiegley <johnw@newartisans.com> | 2009-01-20 20:05:41 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-01-20 20:05:41 -0400 |
commit | 3682ea6f8c5b2ef47a17897bd3dc4aca64b76c2f (patch) | |
tree | f4ba45e441d1c828fee1205a9d4db51df28686f9 /src | |
parent | 887828a40c54ef41a6e8e09f639539d99bb31639 (diff) | |
download | fork-ledger-3682ea6f8c5b2ef47a17897bd3dc4aca64b76c2f.tar.gz fork-ledger-3682ea6f8c5b2ef47a17897bd3dc4aca64b76c2f.tar.bz2 fork-ledger-3682ea6f8c5b2ef47a17897bd3dc4aca64b76c2f.zip |
Added support for "anonymizing" any report with --anon.
Diffstat (limited to 'src')
-rw-r--r-- | src/filters.cc | 56 | ||||
-rw-r--r-- | src/filters.h | 25 | ||||
-rw-r--r-- | src/report.cc | 8 | ||||
-rw-r--r-- | src/report.h | 7 |
4 files changed, 94 insertions, 2 deletions
diff --git a/src/filters.cc b/src/filters.cc index bd0a9f30..7801a853 100644 --- a/src/filters.cc +++ b/src/filters.cc @@ -35,6 +35,7 @@ #include "session.h" #include "format.h" #include "textual.h" +#include "SHA1.h" namespace ledger { @@ -124,6 +125,61 @@ void sort_xacts::post_accumulated_xacts() xacts.clear(); } +namespace { + string to_hex(unsigned int * message_digest) + { + std::ostringstream buf; + + for(int i = 0; i < 5 ; i++) + buf << std::hex << message_digest[i]; + + return buf.str(); + } +} + +void anonymize_xacts::operator()(xact_t& xact) +{ + SHA1 sha; + unsigned int message_digest[5]; + bool copy_entry_details = false; + + if (last_entry != xact.entry) { + entry_temps.push_back(*xact.entry); + last_entry = xact.entry; + copy_entry_details = true; + } + entry_t& entry = entry_temps.back(); + + if (copy_entry_details) { + entry.copy_details(*xact.entry); + + sha.Reset(); + sha << xact.entry->payee.c_str(); + sha.Result(message_digest); + + entry.payee = to_hex(message_digest); + entry.note = none; + } + + xact_temps.push_back(xact); + xact_t& temp = xact_temps.back(); + temp.entry = &entry; + + sha.Reset(); + sha << xact.account->fullname().c_str(); + sha.Result(message_digest); + + temp.copy_details(xact); + + temp.account = xact.entry->journal->find_account(to_hex(message_digest)); + temp.note = none; + temp.add_flags(ITEM_TEMP); + + entry.add_xact(&temp); + + (*handler)(temp); +} + void calc_xacts::operator()(xact_t& xact) { try { diff --git a/src/filters.h b/src/filters.h index 6955cd91..3f3c28f0 100644 --- a/src/filters.h +++ b/src/filters.h @@ -132,8 +132,8 @@ class sort_xacts : public item_handler<xact_t> { typedef std::deque<xact_t *> xacts_deque; - xacts_deque xacts; - const expr_t sort_order; + xacts_deque xacts; + const expr_t sort_order; sort_xacts(); @@ -239,6 +239,27 @@ public: } }; +class anonymize_xacts : public item_handler<xact_t> +{ + std::list<entry_t> entry_temps; + std::list<xact_t> xact_temps; + + entry_t * last_entry; + + anonymize_xacts(); + +public: + anonymize_xacts(xact_handler_ptr handler) + : item_handler<xact_t>(handler), last_entry(NULL) { + TRACE_CTOR(anonymize_xacts, "xact_handler_ptr"); + } + virtual ~anonymize_xacts() { + TRACE_DTOR(anonymize_xacts); + } + + virtual void operator()(xact_t& xact); +}; + class calc_xacts : public item_handler<xact_t> { xact_t * last_xact; diff --git a/src/report.cc b/src/report.cc index b5357038..49deeb44 100644 --- a/src/report.cc +++ b/src/report.cc @@ -163,6 +163,12 @@ report_t::chain_xact_handlers(xact_handler_ptr base_handler, if (show_related) handler.reset(new related_xacts(handler, show_all_related)); + // anonymize_xacts removes all meaningful information from entry + // payee's and account names, for the sake of creating useful bug + // reports. + if (anonymize) + handler.reset(new anonymize_xacts(handler)); + // This filter_xacts will only pass through xacts // matching the `predicate'. if (! predicate.empty()) { @@ -521,6 +527,8 @@ expr_t::ptr_op_t report_t::lookup(const string& name) return MAKE_FUNCTOR(report_t::option_ansi); else if (std::strcmp(p, "ansi-invert") == 0) return MAKE_FUNCTOR(report_t::option_ansi_invert); + else if (std::strcmp(p, "anon") == 0) + return MAKE_FUNCTOR(report_t::option_anon); break; case 'b': diff --git a/src/report.h b/src/report.h index 48090f91..61720a0a 100644 --- a/src/report.h +++ b/src/report.h @@ -129,6 +129,7 @@ public: bool keep_tag; bool entry_sort; bool sort_all; + bool anonymize; string account; optional<path> pager; @@ -163,6 +164,7 @@ public: keep_tag(false), entry_sort(false), sort_all(false), + anonymize(false), raw_mode(false), @@ -777,6 +779,11 @@ public: } #endif + value_t option_anon(call_scope_t& args) { + anonymize = true; + return true; + } + // // Scope members // |