From eab64118925d86751fbff9e83c90583f37bc36b5 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 30 Jan 2013 23:02:46 +0100 Subject: [PATCH] Config: Implement dumping the config in text form Fixes #2754 --- lib/config/configitem.cpp | 23 +++++++++++ lib/config/configitem.h | 2 + lib/config/expression.cpp | 78 +++++++++++++++++++++++++++++++++++ lib/config/expression.h | 3 ++ lib/config/expressionlist.cpp | 13 ++++++ lib/config/expressionlist.h | 1 + 6 files changed, 120 insertions(+) diff --git a/lib/config/configitem.cpp b/lib/config/configitem.cpp index 898ad53c1..2d01b5d71 100644 --- a/lib/config/configitem.cpp +++ b/lib/config/configitem.cpp @@ -224,3 +224,26 @@ ConfigItem::Ptr ConfigItem::GetObject(const String& type, const String& name) return it->second; } + +void ConfigItem::Dump(ostream& fp) const +{ + fp << "object \"" << m_Type << "\" \"" << m_Name << "\""; + + if (m_Parents.size() > 0) { + fp << " inherits"; + + bool first = true; + BOOST_FOREACH(const String& name, m_Parents) { + if (!first) + fp << ","; + else + first = false; + + fp << " \"" << name << "\""; + } + } + + fp << " {" << "\n"; + m_ExpressionList->Dump(fp, 1); + fp << "}" << "\n"; +} \ No newline at end of file diff --git a/lib/config/configitem.h b/lib/config/configitem.h index 8bce29493..e68c8583b 100644 --- a/lib/config/configitem.h +++ b/lib/config/configitem.h @@ -48,6 +48,8 @@ public: DynamicObject::Ptr Commit(void); void Unregister(void); + void Dump(ostream& fp) const; + DynamicObject::Ptr GetDynamicObject(void) const; DebugInfo GetDebugInfo(void) const; diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index fe88d2536..a8f74a74e 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -105,3 +105,81 @@ void Expression::Execute(const Dictionary::Ptr& dictionary) const dictionary->Set(m_Key, newValue); } + +void Expression::DumpValue(ostream& fp, int indent, const Value& value, bool inlineDict) +{ + ExpressionList::Ptr valueExprl; + Dictionary::Ptr valueDict; + if (value.IsObjectType()) { + if (!inlineDict) + fp << "{ " << "\n"; + + static_cast(value)->Dump(fp, indent); + + if (!inlineDict) { + fp << "\n"; + + for (int i = 0; i < indent - 1; i++) + fp << "\t"; + + fp << "}"; + } + + return; + } + + if (value.IsObjectType()) { + if (!inlineDict) + fp << "{ " << "\n"; + + String k; + Value v; + BOOST_FOREACH(tie(k, v), static_cast(value)) { + fp << "\"" << k << "\" = "; + DumpValue(fp, indent, v); + fp << "," << "\n"; + } + + if (!inlineDict) + fp << "\n" << "}"; + + return; + } + + if (value.IsScalar()) { + fp << "\"" << static_cast(value) << "\""; + return; + } + + throw_exception(runtime_error("Encountered unknown type while dumping value.")); +} + +void Expression::Dump(ostream& fp, int indent) const +{ + if (m_Operator == OperatorExecute) { + DumpValue(fp, indent, m_Value, true); + return; + } + + for (int i = 0; i < indent; i++) + fp << "\t"; + + fp << "\"" << m_Key << "\" "; + + switch (m_Operator) { + case OperatorSet: + fp << "="; + break; + case OperatorPlus: + fp << "+="; + break; + default: + throw_exception(runtime_error("Not yet implemented.")); + } + + fp << " "; + + DumpValue(fp, indent + 1, m_Value); + + fp << ", " << "\n"; +} diff --git a/lib/config/expression.h b/lib/config/expression.h index 378f1c94b..1fff1aeb6 100644 --- a/lib/config/expression.h +++ b/lib/config/expression.h @@ -50,12 +50,15 @@ public: const DebugInfo& debuginfo); void Execute(const Dictionary::Ptr& dictionary) const; + void Dump(ostream& fp, int indent = 0) const; private: String m_Key; ExpressionOperator m_Operator; Value m_Value; DebugInfo m_DebugInfo; + + static void DumpValue(ostream& fp, int indent, const Value& value, bool inlineDict = false); }; } diff --git a/lib/config/expressionlist.cpp b/lib/config/expressionlist.cpp index 54782f130..651d11413 100644 --- a/lib/config/expressionlist.cpp +++ b/lib/config/expressionlist.cpp @@ -53,3 +53,16 @@ void ExpressionList::Execute(const Dictionary::Ptr& dictionary) const expression.Execute(dictionary); } } + +/** + * Dumps the expression list to the specified stream. + * + * @param fp The stream. + * @param indent The indentation level. + */ +void ExpressionList::Dump(ostream& fp, int indent) const +{ + BOOST_FOREACH(const Expression& expression, m_Expressions) { + expression.Dump(fp, indent); + } +} \ No newline at end of file diff --git a/lib/config/expressionlist.h b/lib/config/expressionlist.h index a4f29fcf0..1ce75d688 100644 --- a/lib/config/expressionlist.h +++ b/lib/config/expressionlist.h @@ -37,6 +37,7 @@ public: void AddExpression(const Expression& expression); void Execute(const Dictionary::Ptr& dictionary) const; + void Dump(ostream& fp, int indent) const; size_t GetLength(void) const; -- 2.40.0