Config: Implement dumping the config in text form

Fixes #2754
This commit is contained in:
Gunnar Beutner 2013-01-30 23:02:46 +01:00
parent b27465feae
commit eab6411892
6 changed files with 120 additions and 0 deletions

View File

@ -224,3 +224,26 @@ ConfigItem::Ptr ConfigItem::GetObject(const String& type, const String& name)
return it->second; 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";
}

View File

@ -48,6 +48,8 @@ public:
DynamicObject::Ptr Commit(void); DynamicObject::Ptr Commit(void);
void Unregister(void); void Unregister(void);
void Dump(ostream& fp) const;
DynamicObject::Ptr GetDynamicObject(void) const; DynamicObject::Ptr GetDynamicObject(void) const;
DebugInfo GetDebugInfo(void) const; DebugInfo GetDebugInfo(void) const;

View File

@ -105,3 +105,81 @@ void Expression::Execute(const Dictionary::Ptr& dictionary) const
dictionary->Set(m_Key, newValue); 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";
}

View File

@ -50,12 +50,15 @@ public:
const DebugInfo& debuginfo); const DebugInfo& debuginfo);
void Execute(const Dictionary::Ptr& dictionary) const; void Execute(const Dictionary::Ptr& dictionary) const;
void Dump(ostream& fp, int indent = 0) const;
private: private:
String m_Key; String m_Key;
ExpressionOperator m_Operator; ExpressionOperator m_Operator;
Value m_Value; Value m_Value;
DebugInfo m_DebugInfo; DebugInfo m_DebugInfo;
static void DumpValue(ostream& fp, int indent, const Value& value, bool inlineDict = false);
}; };
} }

View File

@ -53,3 +53,16 @@ void ExpressionList::Execute(const Dictionary::Ptr& dictionary) const
expression.Execute(dictionary); 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);
}
}

View File

@ -37,6 +37,7 @@ public:
void AddExpression(const Expression& expression); void AddExpression(const Expression& expression);
void Execute(const Dictionary::Ptr& dictionary) const; void Execute(const Dictionary::Ptr& dictionary) const;
void Dump(ostream& fp, int indent) const;
size_t GetLength(void) const; size_t GetLength(void) const;