summaryrefslogtreecommitdiff
path: root/src/value.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-02-01 01:48:07 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-02-01 01:48:07 -0400
commit9f8997f1b52f55225c76cf92461a05940d71a9e7 (patch)
tree180e1d7d9e1692234e3242f66f7e324c0ba32907 /src/value.cc
parentcac7d02dd8fa595722faace8aa84be54b49e554b (diff)
downloadledger-9f8997f1b52f55225c76cf92461a05940d71a9e7.tar.gz
ledger-9f8997f1b52f55225c76cf92461a05940d71a9e7.tar.bz2
ledger-9f8997f1b52f55225c76cf92461a05940d71a9e7.zip
Values can now be streamed to XML, and all the types they refer to.
Diffstat (limited to 'src/value.cc')
-rw-r--r--src/value.cc57
1 files changed, 57 insertions, 0 deletions
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("<value>\n", depth);
+
+ switch (type()) {
+ case VOID:
+ out << xml_str("<void />\n", depth + 1);
+ break;
+ case BOOLEAN:
+ out << xml_str("<bool>", depth + 1)
+ << (as_boolean() ? "true" : "false")
+ << "</bool>\n";
+ break;
+ case DATETIME:
+ out << xml_str("<datetime>", depth + 1)
+ << format_datetime(as_datetime())
+ << "</datetime>\n";
+ break;
+ case DATE:
+ out << xml_str("<date>", depth + 1)
+ << format_date(as_date())
+ << "</date>\n";
+ break;
+ case INTEGER:
+ out << xml_str("<integer>", depth + 1)
+ << as_long()
+ << "</integer>\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("<string>", depth + 1)
+ << as_string()
+ << "</string>\n";
+ break;
+ case SEQUENCE:
+ out << xml_str("<sequence>\n", depth + 1);
+ foreach (const value_t& v, as_sequence())
+ v.write_xml(out, depth + 2);
+ out << xml_str("</sequence>\n", depth + 1);
+ break;
+ case POINTER:
+ default:
+ throw_(value_error, "Cannot output " << label() << " as XML");
+ break;
+ }
+
+ out << xml_str("</value>\n", depth);
+}
+
bool value_t::valid() const
{
switch (type()) {