[Zrouter-src] ZRouter.org: push to zconf++ zconf++/model.cc
zrouter-src at zrouter.org
zrouter-src at zrouter.org
Thu Feb 23 10:21:54 UTC 2012
details: /rev/ae5be1e0c26f
changeset: 67:ae5be1e0c26f
user: "Nicolai Petri <nicolai at petri.dk>"
date: Wed Feb 22 18:29:10 2012 +0100
description:
Just use stream implementation instead of duplicating logic when asked to return a string.
diffstat:
zconf++/model.cc | 65 +++----------------------------------------------------
1 files changed, 4 insertions(+), 61 deletions(-)
diffs (75 lines):
diff -r be9bc2e311d7 -r ae5be1e0c26f zconf++/model.cc
--- a/zconf++/model.cc Wed Feb 22 16:14:02 2012 +0100
+++ b/zconf++/model.cc Wed Feb 22 18:29:10 2012 +0100
@@ -292,67 +292,10 @@
}
std::string ModelWriter::write(Leaf& l, const std::string &indent) {
- std::string rv;
- // for (int c=0; c < l.numChildren(); c++) {
- if (l.type() == STRING_LEAF) {
- // cout << " dumping " << " => " << l.get_string() << endl;
- //cout << indent << l.get_string() << endl;
- rv += format_json_string(l);
- }
- else if (l.type() == OBJECT_LEAF) {
- rv.reserve(4096); // Reserve just some space to avoid lots of reallocs
- rv += "{\n";
- LeafMap &m = l.map();
- LeafMap::iterator it = m.begin();
- bool first = true;
- while (it != m.end()) {
- if (it->second.type() != ACTION_LEAF) {
- rv += indent;
- if (first == false) {
- rv += ",";
- }
- else {
- first = false;
- }
- rv += format_json_string(it->first) + ": " + write(it->second, indent+" ") + "\n";
- }
- it++;
- }
- rv += indent + "}";
- }
- else if (l.type() == ARRAY_LEAF) {
- rv.reserve(4096); // Reserve just some space to avoid lots of reallocs
- rv += "[\n";
- LeafArray &a = l.array();
- for (int c=0; c < a.size(); c++) {
- rv += indent /*+ Int2String(c) + " => "*/ + (c>0 ? "," : "") + write(a[c], indent+" ") + "\n";
- }
- rv += indent + "]";
- }
- else if (l.type() == REAL_LEAF) {
- rv += format_json_real(l.get_real());
- }
- else if (l.type() == NUMERIC_LEAF) {
- rv += format_json_int(l.get_int());
- /* Actions should only be dumped on request */
- }
- else if (l.type() == ACTION_LEAF) {
- rv += "\"@ACTION@\"";
- }
- else if (l.type() == EMPTY_LEAF) {
- rv += "null";
- }
- else if (l.type() == BOOLEAN_LEAF) {
- rv += l.get_bool() ? "true" : "false";
- }
- else if (l.type() == ENUM_LEAF) {
- rv += format_json_string(format_json_int(l.get_int()) + "::" + l.get_string());
- }
- else {
- throw std::string("FIXME");
- }
- // }
- return rv;
+ std::stringstream dest;
+ dest.str().reserve(8192);
+ write(dest, l, indent);
+ return dest.str();
}
void ModelWriter::write(std::ostream &buf, Leaf& l, const std::string &indent) {
More information about the Zrouter-src
mailing list