[icinga-checkins] icinga.org: icinga2/master: Config: Implement dumping the config in text form

git at icinga.org git at icinga.org
Wed Jan 30 23:02:58 CET 2013


Module: icinga2
Branch: master
Commit: eab64118925d86751fbff9e83c90583f37bc36b5
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=eab64118925d86751fbff9e83c90583f37bc36b5

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Wed Jan 30 23:02:46 2013 +0100

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(+), 0 deletions(-)

diff --git a/lib/config/configitem.cpp b/lib/config/configitem.cpp
index 898ad53..2d01b5d 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 8bce294..e68c858 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 fe88d25..a8f74a7 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<ExpressionList>()) {
+		if (!inlineDict)
+			fp << "{ " << "\n";
+
+		static_cast<ExpressionList::Ptr>(value)->Dump(fp, indent);
+
+		if (!inlineDict) {
+			fp << "\n";
+
+			for (int i = 0; i < indent - 1; i++)
+				fp << "\t";
+
+			fp << "}";
+		}
+
+		return;
+	}
+
+	if (value.IsObjectType<Dictionary>()) {
+		if (!inlineDict)
+			fp << "{ " << "\n";
+
+		String k;
+		Value v;
+		BOOST_FOREACH(tie(k, v), static_cast<Dictionary::Ptr>(value)) {
+			fp << "\"" << k << "\" = ";
+			DumpValue(fp, indent, v);
+			fp << "," << "\n";
+		}
+
+		if (!inlineDict)
+			fp << "\n" << "}";
+
+		return;
+	}
+
+	if (value.IsScalar()) {
+		fp << "\"" << static_cast<String>(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 378f1c9..1fff1ae 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 54782f1..651d114 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 a4f29fc..1ce75d6 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;
 





More information about the icinga-checkins mailing list