From 394c7bd8dfbe12e09b7fdee8d5c4072f4336d545 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sun, 8 Nov 2009 14:59:11 -0500 Subject: Removed a bunch of empty comments --- src/annotate.h | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'src/annotate.h') diff --git a/src/annotate.h b/src/annotate.h index 0f940849..9ca994c9 100644 --- a/src/annotate.h +++ b/src/annotate.h @@ -48,11 +48,6 @@ namespace ledger { -/** - * @brief Brief - * - * Long. - */ struct annotation_t : public supports_flags<>, public equality_comparable { @@ -174,11 +169,6 @@ inline std::ostream& operator<<(std::ostream& out, return out; } -/** - * @brief Brief - * - * Long. - */ class annotated_commodity_t : public commodity_t, public equality_comparable Date: Sun, 25 Oct 2009 05:13:21 -0400 Subject: Added basic foundation for XML reporting --- src/amount.cc | 13 +++++ src/amount.h | 2 + src/annotate.h | 23 +++++++++ src/balance.cc | 8 +++ src/balance.h | 2 + src/commodity.cc | 11 ++++ src/commodity.h | 2 + src/mask.h | 6 +++ src/report.cc | 1 + src/times.h | 12 +++++ src/utils.h | 34 +++++++++++++ src/value.cc | 54 ++++++++++++++++++++ src/value.h | 2 + src/xml.cc | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/xml.h | 90 ++++++++++++++++++++++++++++++++ tools/Makefile.am | 2 + 16 files changed, 412 insertions(+) create mode 100644 src/xml.cc create mode 100644 src/xml.h (limited to 'src/annotate.h') diff --git a/src/amount.cc b/src/amount.cc index 8c78a86c..456ef8cf 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -1125,6 +1125,19 @@ bool amount_t::valid() const return true; } +void to_xml(std::ostream& out, const amount_t& amt) +{ + push_xml x(out, "amount"); + + if (amt.has_commodity()) + to_xml(out, amt.commodity()); + + { + push_xml y(out, "number"); + out << amt.number(); + } +} + #if defined(HAVE_BOOST_SERIALIZATION) template diff --git a/src/amount.h b/src/amount.h index add32b03..d8a19f91 100644 --- a/src/amount.h +++ b/src/amount.h @@ -745,6 +745,8 @@ inline std::istream& operator>>(std::istream& in, amount_t& amt) { return in; } +void to_xml(std::ostream& out, const amount_t& amt); + } // namespace ledger #endif // _AMOUNT_H diff --git a/src/annotate.h b/src/annotate.h index 9ca994c9..37810fb9 100644 --- a/src/annotate.h +++ b/src/annotate.h @@ -110,6 +110,29 @@ private: #endif // HAVE_BOOST_SERIALIZATION }; +inline void to_xml(std::ostream& out, const annotation_t& details) +{ + push_xml x(out, "annotation"); + + if (details.price) + { + push_xml y(out, "ann-price"); + to_xml(out, *details.price); + } + + if (details.date) + { + push_xml y(out, "ann-date"); + to_xml(out, *details.date); + } + + if (details.tag) + { + push_xml y(out, "ann-tag"); + out << y.guard(*details.tag); + } +} + struct keep_details_t { bool keep_price; diff --git a/src/balance.cc b/src/balance.cc index 1c096e01..59eb4d92 100644 --- a/src/balance.cc +++ b/src/balance.cc @@ -299,4 +299,12 @@ void balance_t::print(std::ostream& out, } } +void to_xml(std::ostream& out, const balance_t& bal) +{ + push_xml x(out, "balance"); + + foreach (const balance_t::amounts_map::value_type& pair, bal.amounts) + to_xml(out, pair.second); +} + } // namespace ledger diff --git a/src/balance.h b/src/balance.h index a4f922a8..81a7ff13 100644 --- a/src/balance.h +++ b/src/balance.h @@ -574,6 +574,8 @@ inline std::ostream& operator<<(std::ostream& out, const balance_t& bal) { return out; } +void to_xml(std::ostream& out, const balance_t& amt); + } // namespace ledger #endif // _BALANCE_H diff --git a/src/commodity.cc b/src/commodity.cc index 0d6c11c6..7669b3db 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -654,4 +654,15 @@ bool compare_amount_commodities::operator()(const amount_t * left, } } +void to_xml(std::ostream& out, const commodity_t& comm) +{ + push_xml x(out, "commodity"); + { + push_xml y(out, "symbol"); + out << y.guard(comm.symbol()); + } + if (comm.is_annotated()) + to_xml(out, as_annotated_commodity(comm).details); +} + } // namespace ledger diff --git a/src/commodity.h b/src/commodity.h index 0d31b2bd..ef9ef5c4 100644 --- a/src/commodity.h +++ b/src/commodity.h @@ -418,6 +418,8 @@ struct compare_amount_commodities { bool operator()(const amount_t * left, const amount_t * right) const; }; +void to_xml(std::ostream& out, const commodity_t& comm); + } // namespace ledger #endif // _COMMODITY_H diff --git a/src/mask.h b/src/mask.h index a7dea7cf..18f1893d 100644 --- a/src/mask.h +++ b/src/mask.h @@ -146,6 +146,12 @@ inline std::ostream& operator<<(std::ostream& out, const mask_t& mask) { return out; } +inline void to_xml(std::ostream& out, const mask_t& mask) +{ + push_xml x(out, "mask"); + out << x.guard(mask.expr.str()); +} + } // namespace ledger #endif // _MASK_H diff --git a/src/report.cc b/src/report.cc index 3da71616..1443dd91 100644 --- a/src/report.cc +++ b/src/report.cc @@ -43,6 +43,7 @@ #include "stats.h" #include "generate.h" #include "draft.h" +#include "xml.h" #include "emacs.h" namespace ledger { diff --git a/src/times.h b/src/times.h index 9387320e..c2c2530e 100644 --- a/src/times.h +++ b/src/times.h @@ -116,6 +116,18 @@ std::string format_date(const date_t& when, void set_date_format(const char * format); void set_input_date_format(const char * format); +inline void to_xml(std::ostream& out, const datetime_t& when) +{ + push_xml x(out, "datetime"); + out << format_datetime(when, FMT_WRITTEN); +} + +inline void to_xml(std::ostream& out, const date_t& when) +{ + push_xml x(out, "date"); + out << format_date(when, FMT_WRITTEN); +} + class date_interval_t : public equality_comparable { public: diff --git a/src/utils.h b/src/utils.h index 48435844..6bd67146 100644 --- a/src/utils.h +++ b/src/utils.h @@ -646,6 +646,40 @@ inline string to_hex(uint_least32_t * message_digest, const int len = 1) return buf.str(); } +class push_xml +{ + std::ostream& out; + string tag; +public: + push_xml(std::ostream& _out, const string& _tag) : out(_out), tag(_tag) { + out << '<' << tag << '>'; + } + ~push_xml() { + out << "'; + } + + string guard(const string& str) { + std::ostringstream buf; + foreach (const char& ch, str) { + switch (ch) { + case '<': + buf << "<"; + break; + case '>': + buf << ">"; + break; + case '&': + buf << "&"; + break; + default: + buf << ch; + break; + } + } + return buf.str(); + } +}; + extern const string version; } // namespace ledger diff --git a/src/value.cc b/src/value.cc index e2c9dc8b..2029b9c5 100644 --- a/src/value.cc +++ b/src/value.cc @@ -1746,4 +1746,58 @@ bool sort_value_is_less_than(const std::list& left_values, return false; } +void to_xml(std::ostream& out, const value_t& value) +{ + push_xml x(out, "value"); + + switch (value.type()) { + case value_t::VOID: + out << ""; + break; + case value_t::BOOLEAN: { + push_xml y(out, "boolean"); + out << (value.as_boolean() ? "true" : "false"); + break; + } + case value_t::INTEGER: { + push_xml y(out, "integer"); + out << value.as_long(); + break; + } + case value_t::DATETIME: + to_xml(out, value.as_datetime()); + break; + case value_t::DATE: + to_xml(out, value.as_date()); + break; + case value_t::STRING: { + push_xml y(out, "string"); + out << y.guard(value.as_string()); + break; + } + case value_t::MASK: + to_xml(out, value.as_mask()); + break; + + case value_t::SEQUENCE: { + push_xml y(out, "sequence"); + foreach (const value_t& member, value.as_sequence()) + to_xml(out, member); + break; + } + + case value_t::AMOUNT: + to_xml(out, value.as_amount()); + break; + case value_t::BALANCE: + to_xml(out, value.as_balance()); + break; + + case value_t::SCOPE: + default: + assert(false); + break; + } +} + } // namespace ledger diff --git a/src/value.h b/src/value.h index db45e593..94002bef 100644 --- a/src/value.h +++ b/src/value.h @@ -982,6 +982,8 @@ struct sort_value_t bool sort_value_is_less_than(const std::list& left_values, const std::list& right_values); +void to_xml(std::ostream& out, const value_t& value); + } // namespace ledger #endif // _VALUE_H diff --git a/src/xml.cc b/src/xml.cc new file mode 100644 index 00000000..02fa7137 --- /dev/null +++ b/src/xml.cc @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2003-2009, John Wiegley. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of New Artisans LLC nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include "xml.h" +#include "xact.h" +#include "post.h" +#include "account.h" +#include "session.h" +#include "report.h" + +namespace ledger { + +namespace { + void xml_account(std::ostream& out, const account_t * acct) { + if ((acct->has_xdata() && + acct->xdata().has_flags(ACCOUNT_EXT_VISITED)) || + acct->children_with_flags(ACCOUNT_EXT_VISITED)) { + out << "(acct); + out << "\">\n"; + + out << "" << acct->name << "\n"; + value_t total = acct->amount(); + if (! total.is_null()) { + out << "\n"; + to_xml(out, total); + out << "\n"; + } + total = acct->total(); + if (! total.is_null()) { + out << "\n"; + to_xml(out, total); + out << "\n"; + } + out << "\n"; + } + + foreach (const accounts_map::value_type& pair, acct->accounts) + xml_account(out, pair.second); + } + + void xml_transaction(std::ostream& out, const xact_t * xact) { + out << "\n"; + out << "" << xact->payee << "\n"; + + foreach (const post_t * post, xact->posts) { + if (post->has_xdata() && + post->xdata().has_flags(POST_EXT_VISITED)) { + out << "\n"; + out << "(post->account); + out << "\">" << post->account->fullname() << "\n"; + + out << "\n"; + to_xml(out, post->amount); + out << "\n"; + + out << "\n"; + if (post->cost) + to_xml(out, *post->cost); + else + to_xml(out, post->amount); + out << "\n"; + + if (post->assigned_amount) { + out << "\n"; + to_xml(out, *post->assigned_amount); + out << "\n"; + } + + out << "\n"; + } + } + + out << "\n"; + } +} + +void format_xml::flush() +{ + std::ostream& out(report.output_stream); + + out << "\n\n\n"; + + out << "\n"; + foreach (const commodities_pair& pair, commodities) { + to_xml(out, *pair.second); + out << '\n'; + } + out << "\n"; + + out << "\n"; + xml_account(out, report.session.journal->master); + out << "\n"; + + out << "\n"; + foreach (const xact_t * xact, transactions) + xml_transaction(out, xact); + out << "\n"; + + out << "\n\n"; + out.flush(); +} + +void format_xml::operator()(post_t& post) +{ + assert(post.xdata().has_flags(POST_EXT_VISITED)); + + commodities.insert(commodities_pair(post.amount.commodity().symbol(), + &post.amount.commodity())); + + if (transactions_set.find(post.xact) == transactions_set.end()) + transactions.push_back(post.xact); +} + +} // namespace ledger diff --git a/src/xml.h b/src/xml.h new file mode 100644 index 00000000..30a7b1b1 --- /dev/null +++ b/src/xml.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2003-2009, John Wiegley. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of New Artisans LLC nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @addtogroup report + */ + +/** + * @file xml.h + * @author John Wiegley + * + * @ingroup report + * + * @brief Brief + * + * Long. + */ +#ifndef _XML_H +#define _XML_H + +#include "chain.h" + +namespace ledger { + +class xact_t; +class account_t; +class commodity_t; +class post_t; +class report_t; + +/** + * @brief Brief + * + * Long. + */ +class format_xml : public item_handler +{ +protected: + report_t& report; + + typedef std::map commodities_map; + typedef std::pair commodities_pair; + + commodities_map commodities; + std::set transactions_set; + std::deque transactions; + +public: + format_xml(report_t& _report) : report(_report) { + TRACE_CTOR(format_xml, "report&"); + } + virtual ~format_xml() { + TRACE_DTOR(format_xml); + } + + virtual void flush(); + virtual void operator()(post_t& post); +}; + +} // namespace ledger + +#endif // _XML_H diff --git a/tools/Makefile.am b/tools/Makefile.am index a41c47ce..c0404606 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -73,6 +73,7 @@ libledger_report_la_SOURCES = \ src/generate.cc \ src/draft.cc \ src/emacs.cc \ + src/xml.cc \ src/output.cc \ src/precmd.cc \ src/chain.cc \ @@ -136,6 +137,7 @@ pkginclude_HEADERS = \ src/generate.h \ src/stats.h \ src/output.h \ + src/xml.h \ src/emacs.h \ \ src/global.h \ -- cgit v1.2.3 From 6cdb79e2a611d99fa6e13dd224a92dc8badaf2ac Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Mon, 9 Nov 2009 03:42:06 -0500 Subject: XML reporting now works via the "xml" command --- acprep | 4 +-- src/amount.cc | 8 ++--- src/amount.h | 3 +- src/annotate.h | 8 ++--- src/commodity.cc | 44 +++++++++++++++++++++++--- src/commodity.h | 8 ++++- src/post.cc | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/post.h | 2 ++ src/report.cc | 2 ++ src/times.h | 22 +++++++++---- src/utils.h | 21 ++++++++++--- src/value.cc | 17 +++++----- src/xact.cc | 59 +++++++++++++++++++++++++++++++++++ src/xact.h | 2 ++ src/xml.cc | 42 +++++-------------------- 15 files changed, 267 insertions(+), 70 deletions(-) (limited to 'src/annotate.h') diff --git a/acprep b/acprep index 7335e17d..055d2306 100755 --- a/acprep +++ b/acprep @@ -195,7 +195,7 @@ class PrepareBuild(CommandLineApp): self.envvars = { 'PYTHON_HOME': '/usr', - 'PYTHON_VERSION': '2.5', + 'PYTHON_VERSION': '2.6', 'BOOST_SUFFIX': None, 'BOOST_HOME': '/usr', 'LEDGER_PRODUCTS': None, @@ -394,7 +394,7 @@ class PrepareBuild(CommandLineApp): tag = self.get_stdout('git', 'describe', '--all', '--long') self.current_ver = re.sub('heads/', '', tag) else: - self.current_ver = "3.0a" + self.current_ver = "unknown" return self.current_ver def need_to_prepare_autotools(self): diff --git a/src/amount.cc b/src/amount.cc index 456ef8cf..6fb0056b 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -1125,16 +1125,16 @@ bool amount_t::valid() const return true; } -void to_xml(std::ostream& out, const amount_t& amt) +void to_xml(std::ostream& out, const amount_t& amt, bool commodity_details) { push_xml x(out, "amount"); if (amt.has_commodity()) - to_xml(out, amt.commodity()); + to_xml(out, amt.commodity(), commodity_details); { - push_xml y(out, "number"); - out << amt.number(); + push_xml y(out, "quantity"); + out << y.guard(amt.quantity_string()); } } diff --git a/src/amount.h b/src/amount.h index d8a19f91..505e2ea7 100644 --- a/src/amount.h +++ b/src/amount.h @@ -745,7 +745,8 @@ inline std::istream& operator>>(std::istream& in, amount_t& amt) { return in; } -void to_xml(std::ostream& out, const amount_t& amt); +void to_xml(std::ostream& out, const amount_t& amt, + bool commodity_details = false); } // namespace ledger diff --git a/src/annotate.h b/src/annotate.h index 37810fb9..38ebaeae 100644 --- a/src/annotate.h +++ b/src/annotate.h @@ -116,19 +116,19 @@ inline void to_xml(std::ostream& out, const annotation_t& details) if (details.price) { - push_xml y(out, "ann-price"); + push_xml y(out, "price"); to_xml(out, *details.price); } if (details.date) { - push_xml y(out, "ann-date"); - to_xml(out, *details.date); + push_xml y(out, "date"); + to_xml(out, *details.date, false); } if (details.tag) { - push_xml y(out, "ann-tag"); + push_xml y(out, "tag"); out << y.guard(*details.tag); } } diff --git a/src/commodity.cc b/src/commodity.cc index 7669b3db..49e82b53 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -654,15 +654,51 @@ bool compare_amount_commodities::operator()(const amount_t * left, } } -void to_xml(std::ostream& out, const commodity_t& comm) +void to_xml(std::ostream& out, const commodity_t& comm, + bool commodity_details) { - push_xml x(out, "commodity"); + push_xml x(out, "commodity", true); + + out << " flags=\""; + if (! (comm.has_flags(COMMODITY_STYLE_SUFFIXED))) out << 'P'; + if (comm.has_flags(COMMODITY_STYLE_SEPARATED)) out << 'S'; + if (comm.has_flags(COMMODITY_STYLE_THOUSANDS)) out << 'T'; + if (comm.has_flags(COMMODITY_STYLE_EUROPEAN)) out << 'E'; + out << '"'; + + x.close_attrs(); + { push_xml y(out, "symbol"); out << y.guard(comm.symbol()); } - if (comm.is_annotated()) - to_xml(out, as_annotated_commodity(comm).details); + + if (commodity_details) { + if (comm.is_annotated()) + to_xml(out, as_annotated_commodity(comm).details); + + if (comm.varied_history()) { + push_xml y(out, "varied-history"); + + foreach (const commodity_t::history_by_commodity_map::value_type& pair, + comm.varied_history()->histories) { + { + push_xml z(out, "symbol"); + out << y.guard(pair.first->symbol()); + } + { + push_xml z(out, "history"); + + foreach (const commodity_t::history_map::value_type& inner_pair, + pair.second.prices) { + push_xml w(out, "price-point"); + to_xml(out, inner_pair.first); + to_xml(out, inner_pair.second); + } + } + } + } + } } } // namespace ledger diff --git a/src/commodity.h b/src/commodity.h index ef9ef5c4..42cc6d8f 100644 --- a/src/commodity.h +++ b/src/commodity.h @@ -318,6 +318,11 @@ public: return *base->varied_history; return none; } + optional varied_history() const { + if (base->varied_history) + return *base->varied_history; + return none; + } optional history(const optional& commodity); @@ -418,7 +423,8 @@ struct compare_amount_commodities { bool operator()(const amount_t * left, const amount_t * right) const; }; -void to_xml(std::ostream& out, const commodity_t& comm); +void to_xml(std::ostream& out, const commodity_t& comm, + bool commodity_details = false); } // namespace ledger diff --git a/src/post.cc b/src/post.cc index 0fd763a9..24705323 100644 --- a/src/post.cc +++ b/src/post.cc @@ -452,4 +452,99 @@ void post_t::set_reported_account(account_t * account) account->xdata().reported_posts.push_back(this); } +void to_xml(std::ostream& out, const post_t& post) +{ + push_xml x(out, "posting", true); + + if (post.state() == item_t::CLEARED) + out << " state=\"cleared\""; + else if (post.state() == item_t::PENDING) + out << " state=\"pending\""; + + if (post.has_flags(POST_VIRTUAL)) + out << " virtual=\"true\""; + if (post.has_flags(ITEM_GENERATED)) + out << " generated=\"true\""; + + x.close_attrs(); + + if (post._date) { + push_xml y(out, "date"); + to_xml(out, *post._date, false); + } + if (post._date_eff) { + push_xml y(out, "effective-date"); + to_xml(out, *post._date_eff, false); + } + + if (post.account) { + push_xml y(out, "account", true); + + out << " ref=\""; + out.width(sizeof(unsigned long) * 2); + out.fill('0'); + out << std::hex << reinterpret_cast(post.account); + out << '"'; + y.close_attrs(); + + { + push_xml z(out, "name"); + out << z.guard(post.account->fullname()); + } + } + + { + push_xml y(out, "post-amount"); + if (post.has_xdata() && post.xdata().has_flags(POST_EXT_COMPOUND)) + to_xml(out, post.xdata().compound_value); + else + to_xml(out, post.amount); + } + + if (post.cost) { + push_xml y(out, "cost"); + to_xml(out, *post.cost); + } + + if (post.assigned_amount) { + if (post.has_flags(POST_CALCULATED)) { + push_xml y(out, "balance-assertion"); + to_xml(out, *post.assigned_amount); + } else { + push_xml y(out, "balance-assignment"); + to_xml(out, *post.assigned_amount); + } + } + + if (post.note) { + push_xml y(out, "note"); + out << y.guard(*post.note); + } + + if (post.metadata) { + push_xml y(out, "metadata"); + foreach (const item_t::string_map::value_type& pair, *post.metadata) { + if (pair.second) { + push_xml z(out, "variable"); + { + push_xml z(out, "key"); + out << y.guard(pair.first); + } + { + push_xml z(out, "value"); + out << y.guard(*pair.second); + } + } else { + push_xml z(out, "tag"); + out << y.guard(pair.first); + } + } + } + + if (post.xdata_ && ! post.xdata_->total.is_null()) { + push_xml y(out, "total"); + to_xml(out, post.xdata_->total); + } +} + } // namespace ledger diff --git a/src/post.h b/src/post.h index 8f51ca84..addf0629 100644 --- a/src/post.h +++ b/src/post.h @@ -218,6 +218,8 @@ private: #endif // HAVE_BOOST_SERIALIZATION }; +void to_xml(std::ostream& out, const post_t& post); + } // namespace ledger #endif // _POST_H diff --git a/src/report.cc b/src/report.cc index 1443dd91..24f0c054 100644 --- a/src/report.cc +++ b/src/report.cc @@ -982,6 +982,8 @@ expr_t::ptr_op_t report_t::lookup(const symbol_t::kind_t kind, case 'x': if (is_eq(p, "xact")) return WRAP_FUNCTOR(xact_command); + else if (is_eq(p, "xml")) + return WRAP_FUNCTOR(reporter<>(new format_xml(*this), *this, "#xml")); break; } break; diff --git a/src/times.h b/src/times.h index c2c2530e..3cd359d2 100644 --- a/src/times.h +++ b/src/times.h @@ -116,16 +116,26 @@ std::string format_date(const date_t& when, void set_date_format(const char * format); void set_input_date_format(const char * format); -inline void to_xml(std::ostream& out, const datetime_t& when) +inline void to_xml(std::ostream& out, const datetime_t& when, + bool wrap = true) { - push_xml x(out, "datetime"); - out << format_datetime(when, FMT_WRITTEN); + if (wrap) { + push_xml x(out, "datetime"); + out << format_datetime(when, FMT_WRITTEN); + } else { + out << format_datetime(when, FMT_WRITTEN); + } } -inline void to_xml(std::ostream& out, const date_t& when) +inline void to_xml(std::ostream& out, const date_t& when, + bool wrap = true) { - push_xml x(out, "date"); - out << format_date(when, FMT_WRITTEN); + if (wrap) { + push_xml x(out, "date"); + out << format_date(when, FMT_WRITTEN); + } else { + out << format_date(when, FMT_WRITTEN); + } } class date_interval_t : public equality_comparable diff --git a/src/utils.h b/src/utils.h index 6bd67146..bfdee0b2 100644 --- a/src/utils.h +++ b/src/utils.h @@ -649,16 +649,27 @@ inline string to_hex(uint_least32_t * message_digest, const int len = 1) class push_xml { std::ostream& out; - string tag; + string tag; + bool leave_open; + public: - push_xml(std::ostream& _out, const string& _tag) : out(_out), tag(_tag) { - out << '<' << tag << '>'; + push_xml(std::ostream& _out, const string& _tag, bool has_attrs = false, + bool _leave_open = false) + : out(_out), tag(_tag), leave_open(_leave_open) { + out << '<' << tag; + if (! has_attrs) + out << '>'; } ~push_xml() { - out << "'; + if (! leave_open) + out << "'; + } + + void close_attrs() { + out << '>'; } - string guard(const string& str) { + static string guard(const string& str) { std::ostringstream buf; foreach (const char& ch, str) { switch (ch) { diff --git a/src/value.cc b/src/value.cc index 2029b9c5..b4060a1c 100644 --- a/src/value.cc +++ b/src/value.cc @@ -1748,8 +1748,6 @@ bool sort_value_is_less_than(const std::list& left_values, void to_xml(std::ostream& out, const value_t& value) { - push_xml x(out, "value"); - switch (value.type()) { case value_t::VOID: out << ""; @@ -1764,6 +1762,14 @@ void to_xml(std::ostream& out, const value_t& value) out << value.as_long(); break; } + + case value_t::AMOUNT: + to_xml(out, value.as_amount()); + break; + case value_t::BALANCE: + to_xml(out, value.as_balance()); + break; + case value_t::DATETIME: to_xml(out, value.as_datetime()); break; @@ -1786,13 +1792,6 @@ void to_xml(std::ostream& out, const value_t& value) break; } - case value_t::AMOUNT: - to_xml(out, value.as_amount()); - break; - case value_t::BALANCE: - to_xml(out, value.as_balance()); - break; - case value_t::SCOPE: default: assert(false); diff --git a/src/xact.cc b/src/xact.cc index 68b607f4..f4331a29 100644 --- a/src/xact.cc +++ b/src/xact.cc @@ -560,4 +560,63 @@ void extend_xact_base(journal_t * journal, xact->extend_xact(base, post_handler); } +void to_xml(std::ostream& out, const xact_t& xact) +{ + push_xml x(out, "transaction", true, true); + + if (xact.state() == item_t::CLEARED) + out << " state=\"cleared\""; + else if (xact.state() == item_t::PENDING) + out << " state=\"pending\""; + + if (xact.has_flags(ITEM_GENERATED)) + out << " generated=\"true\""; + + x.close_attrs(); + + if (xact._date) { + push_xml y(out, "date"); + to_xml(out, *xact._date, false); + } + if (xact._date_eff) { + push_xml y(out, "effective-date"); + to_xml(out, *xact._date_eff, false); + } + + if (xact.code) { + push_xml y(out, "code"); + out << y.guard(*xact.code); + } + + { + push_xml y(out, "payee"); + out << y.guard(xact.payee); + } + + if (xact.note) { + push_xml y(out, "note"); + out << y.guard(*xact.note); + } + + if (xact.metadata) { + push_xml y(out, "metadata"); + foreach (const item_t::string_map::value_type& pair, *xact.metadata) { + if (pair.second) { + push_xml z(out, "variable"); + { + push_xml w(out, "key"); + out << y.guard(pair.first); + } + { + push_xml w(out, "value"); + out << y.guard(*pair.second); + } + } else { + push_xml z(out, "tag"); + out << y.guard(pair.first); + } + } + } +} + } // namespace ledger diff --git a/src/xact.h b/src/xact.h index 31422b3a..9a52fafe 100644 --- a/src/xact.h +++ b/src/xact.h @@ -289,6 +289,8 @@ typedef std::list xacts_list; typedef std::list auto_xacts_list; typedef std::list period_xacts_list; +void to_xml(std::ostream& out, const xact_t& xact); + } // namespace ledger #endif // _XACT_H diff --git a/src/xml.cc b/src/xml.cc index 02fa7137..9cff980a 100644 --- a/src/xml.cc +++ b/src/xml.cc @@ -72,39 +72,12 @@ namespace { } void xml_transaction(std::ostream& out, const xact_t * xact) { - out << "\n"; - out << "" << xact->payee << "\n"; + to_xml(out, *xact); - foreach (const post_t * post, xact->posts) { + foreach (const post_t * post, xact->posts) if (post->has_xdata() && - post->xdata().has_flags(POST_EXT_VISITED)) { - out << "\n"; - out << "(post->account); - out << "\">" << post->account->fullname() << "\n"; - - out << "\n"; - to_xml(out, post->amount); - out << "\n"; - - out << "\n"; - if (post->cost) - to_xml(out, *post->cost); - else - to_xml(out, post->amount); - out << "\n"; - - if (post->assigned_amount) { - out << "\n"; - to_xml(out, *post->assigned_amount); - out << "\n"; - } - - out << "\n"; - } - } + post->xdata().has_flags(POST_EXT_VISITED)) + to_xml(out, *post); out << "\n"; } @@ -114,11 +87,12 @@ void format_xml::flush() { std::ostream& out(report.output_stream); - out << "\n\n\n"; + out << "\n"; + out << "\n"; out << "\n"; foreach (const commodities_pair& pair, commodities) { - to_xml(out, *pair.second); + to_xml(out, *pair.second, true); out << '\n'; } out << "\n"; @@ -132,7 +106,7 @@ void format_xml::flush() xml_transaction(out, xact); out << "\n"; - out << "\n\n"; + out << "\n"; out.flush(); } -- cgit v1.2.3