From 9f8997f1b52f55225c76cf92461a05940d71a9e7 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sun, 1 Feb 2009 01:48:07 -0400 Subject: Values can now be streamed to XML, and all the types they refer to. --- src/amount.cc | 12 +++++++----- src/balance.cc | 10 ++++++++++ src/balance.h | 9 +++++++++ src/balpair.cc | 11 +++++++++++ src/balpair.h | 9 +++++++++ src/commodity.cc | 25 +++++++++---------------- src/value.cc | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/value.h | 9 +++++++++ 8 files changed, 121 insertions(+), 21 deletions(-) diff --git a/src/amount.cc b/src/amount.cc index 6f945002..e95f6350 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -1228,14 +1228,16 @@ void amount_t::read_xml(std::istream& in) void amount_t::write_xml(std::ostream& out, const int depth) const { - xml_print(out, "\n", depth); + out << xml_str("\n", depth); - commodity().write_xml(out, depth + 1); + if (has_commodity()) + commodity().write_xml(out, depth + 1); - xml_print(out, "", depth + 1); - out << quantity_string() << "\n"; + out << xml_str("", depth + 1) + << quantity_string() + << "\n"; - xml_print(out, "\n", depth); + out << xml_str("\n", depth); } bool amount_t::valid() const diff --git a/src/balance.cc b/src/balance.cc index 7b1ed4dd..b5c0aed7 100644 --- a/src/balance.cc +++ b/src/balance.cc @@ -252,4 +252,14 @@ void balance_t::print(std::ostream& out, } } +void balance_t::write_xml(std::ostream& out, const int depth) const +{ + out << xml_str("\n", depth); + + foreach (const amounts_map::value_type& pair, amounts) + pair.second.write_xml(out, depth + 1); + + out << xml_str("\n", depth); +} + } // namespace ledger diff --git a/src/balance.h b/src/balance.h index d1e4301b..b65cc3aa 100644 --- a/src/balance.h +++ b/src/balance.h @@ -475,6 +475,15 @@ public: void print(std::ostream& out, const int first_width, const int latter_width = -1) const; + /** @name XML Serialization + */ + /*@{*/ + + void read_xml(std::istream& in); + void write_xml(std::ostream& out, const int depth = 0) const; + + /*@}*/ + /** * Debugging methods. There are two methods defined to help with * debugging: diff --git a/src/balpair.cc b/src/balpair.cc index 822460b3..40d9b9ef 100644 --- a/src/balpair.cc +++ b/src/balpair.cc @@ -53,4 +53,15 @@ balance_pair_t::value(const optional& moment, return none; } +void balance_pair_t::write_xml(std::ostream& out, const int depth) const +{ + out << xml_str("\n", depth); + + quantity().write_xml(out, depth + 1); + if (cost) + cost->write_xml(out, depth + 1); + + out << xml_str("\n", depth); +} + } // namespace ledger diff --git a/src/balpair.h b/src/balpair.h index d96792f0..38e58cf9 100644 --- a/src/balpair.h +++ b/src/balpair.h @@ -343,6 +343,15 @@ public: return balance_t::operator==(val); } + /** @name XML Serialization + */ + /*@{*/ + + void read_xml(std::istream& in); + void write_xml(std::ostream& out, const int depth = 0) const; + + /*@}*/ + /** * Debugging methods. There is only one method specifically for * balance pairs to help with debugging: diff --git a/src/commodity.cc b/src/commodity.cc index 2376e64b..94c35cc4 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -769,32 +769,25 @@ void commodity_t::read_xml(std::istream& in) void commodity_t::write_xml(std::ostream& out, const int depth) const { - xml_print(out, "\n"; + out << xml_str("", depth + 1) << symbol() << "\n"; + #if 0 - // jww (2006-03-02): !!! + // jww (2009-02-01): If this is an annotated commodity, more has to happen if (price) { - for (int i = 0; i < depth + 2; i++) out << ' '; - out << "" << base->symbol << "\n"; - for (int i = 0; i < depth + 2; i++) out << ' '; - out << "\n"; - xml_write_amount(out, *price, depth + 4); - for (int i = 0; i < depth + 2; i++) out << ' '; - out << "\n"; - } else -#endif - { - xml_print(out, "", depth + 1); - out << symbol() << "\n"; + out << xml_str("", depth + 1); + price->write_xml(out, depth + 2); + out << xml_str("\n", depth + 1); } +#endif - xml_print(out, "\n", depth); + out << xml_str("\n", depth); } bool compare_amount_commodities::operator()(const amount_t * left, diff --git a/src/value.cc b/src/value.cc index b5ee6602..7d6f9409 100644 --- a/src/value.cc +++ b/src/value.cc @@ -1804,6 +1804,63 @@ void value_t::write(std::ostream& out) const throw_(value_error, "Cannot read " << label() << " to a stream"); } +void value_t::write_xml(std::ostream& out, const int depth) const +{ + out << xml_str("\n", depth); + + switch (type()) { + case VOID: + out << xml_str("\n", depth + 1); + break; + case BOOLEAN: + out << xml_str("", depth + 1) + << (as_boolean() ? "true" : "false") + << "\n"; + break; + case DATETIME: + out << xml_str("", depth + 1) + << format_datetime(as_datetime()) + << "\n"; + break; + case DATE: + out << xml_str("", depth + 1) + << format_date(as_date()) + << "\n"; + break; + case INTEGER: + out << xml_str("", depth + 1) + << as_long() + << "\n"; + break; + case AMOUNT: + as_amount().write_xml(out, depth + 1); + break; + case BALANCE: + as_balance().write_xml(out, depth + 1); + break; + case BALANCE_PAIR: + as_balance_pair().write_xml(out, depth + 1); + break; + case STRING: + out << xml_str("", depth + 1) + << as_string() + << "\n"; + break; + case SEQUENCE: + out << xml_str("\n", depth + 1); + foreach (const value_t& v, as_sequence()) + v.write_xml(out, depth + 2); + out << xml_str("\n", depth + 1); + break; + case POINTER: + default: + throw_(value_error, "Cannot output " << label() << " as XML"); + break; + } + + out << xml_str("\n", depth); +} + bool value_t::valid() const { switch (type()) { diff --git a/src/value.h b/src/value.h index 069a3c46..ae6efc62 100644 --- a/src/value.h +++ b/src/value.h @@ -897,6 +897,15 @@ public: void read(const char *& data); void write(std::ostream& out) const; + /** @name XML Serialization + */ + /*@{*/ + + void read_xml(std::istream& in); + void write_xml(std::ostream& out, const int depth = 0) const; + + /*@}*/ + /** * Debugging methods. */ -- cgit v1.2.3