From 094c64b67cedc080bf8dcf218105f31f5b479296 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 28 Jan 2009 20:49:44 -0400 Subject: [PATCH] amount_t and commodity_t objects can now stream themselves to XML. --- src/amount.cc | 16 ++++++++++++++++ src/amount.h | 3 +++ src/commodity.cc | 34 ++++++++++++++++++++++++++++++++++ src/commodity.h | 3 +++ src/utils.h | 12 ++++++++++++ 5 files changed, 68 insertions(+) diff --git a/src/amount.cc b/src/amount.cc index 591b0901..dced0625 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -1396,6 +1396,22 @@ void amount_t::write(std::ostream& out, unsigned int index) const } } +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); + + commodity().write_xml(out, depth + 1); + + xml_print(out, "", depth + 1); + out << quantity_string() << "\n"; + + xml_print(out, "\n", depth); +} + bool amount_t::valid() const { if (quantity) { diff --git a/src/amount.h b/src/amount.h index a9e60be3..f28980f0 100644 --- a/src/amount.h +++ b/src/amount.h @@ -703,6 +703,9 @@ public: char ** pool_next = NULL); void write(std::ostream& out, unsigned int index = 0) const; + 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/commodity.cc b/src/commodity.cc index d674833c..09e9deca 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -739,6 +739,40 @@ void annotated_commodity_t::write_annotations(std::ostream& out, out << " (" << *info.tag << ')'; } +void commodity_t::read_xml(std::istream& in) +{ +} + +void commodity_t::write_xml(std::ostream& out, const int depth) const +{ + xml_print(out, "\n"; + +#if 0 + // jww (2006-03-02): !!! + 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"; + } + + xml_print(out, "\n", depth); +} + bool compare_amount_commodities::operator()(const amount_t * left, const amount_t * right) const { diff --git a/src/commodity.h b/src/commodity.h index 38fdc9ba..7a42ca67 100644 --- a/src/commodity.h +++ b/src/commodity.h @@ -359,6 +359,9 @@ public: void read(char *& data); void write(std::ostream& out) const; + void read_xml(std::istream& in); + void write_xml(std::ostream& out, const int depth = 0) const; + bool valid() const; }; diff --git a/src/utils.h b/src/utils.h index 0d511a38..c761f277 100644 --- a/src/utils.h +++ b/src/utils.h @@ -535,6 +535,18 @@ inline char peek_next_nonws(std::istream& in) { return c; } +inline void xml_space(std::ostream& out, const int depth = 0) { + for (int i = 0; i < depth; i++) + out << " "; +} + +inline void xml_print(std::ostream& out, + const string& str, + const int depth = 0) { + xml_space(out, depth); + out << str; +} + #define READ_INTO(str, targ, size, var, cond) { \ char * _p = targ; \ var = str.peek(); \