diff options
author | John Wiegley <johnw@newartisans.com> | 2009-01-28 20:49:44 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-01-28 20:49:44 -0400 |
commit | 094c64b67cedc080bf8dcf218105f31f5b479296 (patch) | |
tree | 661ce7fec77219948703f143ad1f9680cede5973 | |
parent | e851c02d2747be0acc336bc278da9e0460b75738 (diff) | |
download | fork-ledger-094c64b67cedc080bf8dcf218105f31f5b479296.tar.gz fork-ledger-094c64b67cedc080bf8dcf218105f31f5b479296.tar.bz2 fork-ledger-094c64b67cedc080bf8dcf218105f31f5b479296.zip |
amount_t and commodity_t objects can now stream themselves to XML.
-rw-r--r-- | src/amount.cc | 16 | ||||
-rw-r--r-- | src/amount.h | 3 | ||||
-rw-r--r-- | src/commodity.cc | 34 | ||||
-rw-r--r-- | src/commodity.h | 3 | ||||
-rw-r--r-- | src/utils.h | 12 |
5 files changed, 68 insertions, 0 deletions
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, "<amount>\n", depth); + + commodity().write_xml(out, depth + 1); + + xml_print(out, "<quantity>", depth + 1); + out << quantity_string() << "</quantity>\n"; + + xml_print(out, "</amount>\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, "<commodity flags=\"", depth); + + if (! (flags() & COMMODITY_STYLE_SUFFIXED)) out << 'P'; + if (flags() & COMMODITY_STYLE_SEPARATED) out << 'S'; + if (flags() & COMMODITY_STYLE_THOUSANDS) out << 'T'; + if (flags() & COMMODITY_STYLE_EUROPEAN) out << 'E'; + out << "\">\n"; + +#if 0 + // jww (2006-03-02): !!! + if (price) { + for (int i = 0; i < depth + 2; i++) out << ' '; + out << "<symbol>" << base->symbol << "</symbol>\n"; + for (int i = 0; i < depth + 2; i++) out << ' '; + out << "<price>\n"; + xml_write_amount(out, *price, depth + 4); + for (int i = 0; i < depth + 2; i++) out << ' '; + out << "</price>\n"; + } else +#endif + { + xml_print(out, "<symbol>", depth + 1); + out << symbol() << "</symbol>\n"; + } + + xml_print(out, "</commodity>\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(); \ |