Implement Array::ToString and Dictionary::ToString

fixes #10329
This commit is contained in:
Gunnar Beutner 2015-10-26 11:05:24 +01:00
parent de847d0b6f
commit dd77863910
13 changed files with 71 additions and 52 deletions

View File

@ -26,7 +26,7 @@ set(base_SOURCES
application.cpp application.thpp application-version.cpp array.cpp
array-script.cpp boolean.cpp boolean-script.cpp console.cpp context.cpp
convert.cpp debuginfo.cpp dictionary.cpp dictionary-script.cpp
configobject.cpp configobject.thpp configobject-script.cpp configtype.cpp dependencygraph.cpp
configobject.cpp configobject.thpp configobject-script.cpp configtype.cpp configwriter.cpp dependencygraph.cpp
exception.cpp fifo.cpp filelogger.cpp filelogger.thpp initialize.cpp json.cpp
json-script.cpp loader.cpp logger.cpp logger.thpp math-script.cpp
netstring.cpp networkstream.cpp number.cpp number-script.cpp object.cpp

View File

@ -22,6 +22,7 @@
#include "base/debug.hpp"
#include "base/primitivetype.hpp"
#include "base/dictionary.hpp"
#include "base/configwriter.hpp"
#include <boost/foreach.hpp>
using namespace icinga;
@ -211,3 +212,10 @@ Array::Ptr Array::Reverse(void) const
return result;
}
String Array::ToString(void) const
{
std::ostringstream msgbuf;
ConfigWriter::EmitArray(msgbuf, const_cast<Array *>(this));
return msgbuf.str();
}

View File

@ -122,6 +122,8 @@ public:
Array::Ptr Reverse(void) const;
virtual String ToString(void) const override;
private:
std::vector<Value> m_Data; /**< The data for the array. */
};

View File

@ -17,7 +17,7 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "config/configwriter.hpp"
#include "base/configwriter.hpp"
#include "config/configcompiler.hpp"
#include "base/exception.hpp"
#include <boost/foreach.hpp>
@ -145,7 +145,7 @@ void ConfigWriter::EmitIdentifier(std::ostream& fp, const String& identifier, bo
{
static std::set<String> keywords;
if (keywords.empty()) {
const std::vector<String>& vkeywords = ConfigCompiler::GetKeywords();
const std::vector<String>& vkeywords = GetKeywords();
std::copy(vkeywords.begin(), vkeywords.end(), std::inserter(keywords, keywords.begin()));
}
@ -204,3 +204,44 @@ String ConfigWriter::EscapeIcingaString(const String& str)
boost::algorithm::replace_all(result, "\"", "\\\"");
return result;
}
const std::vector<String>& ConfigWriter::GetKeywords(void)
{
static std::vector<String> keywords;
if (keywords.empty()) {
keywords.push_back("object");
keywords.push_back("template");
keywords.push_back("include");
keywords.push_back("include_recursive");
keywords.push_back("include_zones");
keywords.push_back("library");
keywords.push_back("null");
keywords.push_back("true");
keywords.push_back("false");
keywords.push_back("const");
keywords.push_back("var");
keywords.push_back("this");
keywords.push_back("globals");
keywords.push_back("locals");
keywords.push_back("use");
keywords.push_back("ignore_on_error");
keywords.push_back("apply");
keywords.push_back("to");
keywords.push_back("where");
keywords.push_back("import");
keywords.push_back("assign");
keywords.push_back("ignore");
keywords.push_back("function");
keywords.push_back("return");
keywords.push_back("break");
keywords.push_back("continue");
keywords.push_back("for");
keywords.push_back("if");
keywords.push_back("else");
keywords.push_back("while");
keywords.push_back("throw");
}
return keywords;
}

View File

@ -20,7 +20,6 @@
#ifndef CONFIGWRITER_H
#define CONFIGWRITER_H
#include "config/i2-config.hpp"
#include "base/object.hpp"
#include "base/array.hpp"
#include "base/dictionary.hpp"
@ -32,9 +31,9 @@ namespace icinga
/**
* A configuration writer.
*
* @ingroup config
* @ingroup base
*/
class I2_CONFIG_API ConfigWriter
class I2_BASE_API ConfigWriter
{
public:
static void EmitBoolean(std::ostream& fp, bool val);
@ -56,6 +55,7 @@ public:
static void EmitComment(std::ostream& fp, const String& text);
static void EmitFunctionCall(std::ostream& fp, const String& name, const Array::Ptr& arguments);
static const std::vector<String>& GetKeywords(void);
private:
ConfigWriter(void);

View File

@ -21,6 +21,7 @@
#include "base/objectlock.hpp"
#include "base/debug.hpp"
#include "base/primitivetype.hpp"
#include "base/configwriter.hpp"
#include <boost/foreach.hpp>
using namespace icinga;
@ -198,3 +199,10 @@ std::vector<String> Dictionary::GetKeys(void) const
return keys;
}
String Dictionary::ToString(void) const
{
std::ostringstream msgbuf;
ConfigWriter::EmitScope(msgbuf, 0, const_cast<Dictionary *>(this));
return msgbuf.str();
}

View File

@ -115,6 +115,8 @@ public:
virtual Object::Ptr Clone(void) const override;
virtual String ToString(void) const override;
private:
std::map<String, Value> m_Data; /**< The data for the dictionary. */
};

View File

@ -19,6 +19,7 @@
#include "cli/consolecommand.hpp"
#include "config/configcompiler.hpp"
#include "base/configwriter.hpp"
#include "base/json.hpp"
#include "base/console.hpp"
#include "base/application.hpp"
@ -80,7 +81,7 @@ static char *ConsoleCompleteHelper(const char *word, int state)
if (state == 0) {
matches.clear();
BOOST_FOREACH(const String& keyword, ConfigCompiler::GetKeywords()) {
BOOST_FOREACH(const String& keyword, ConfigWriter::GetKeywords()) {
AddSuggestion(matches, word, keyword);
}

View File

@ -37,7 +37,6 @@ set(config_SOURCES
applyrule.cpp
configcompilercontext.cpp configcompiler.cpp configitembuilder.cpp
configitem.cpp ${FLEX_config_lexer_OUTPUTS} ${BISON_config_parser_OUTPUTS}
configwriter.cpp
expression.cpp objectrule.cpp
)

View File

@ -320,43 +320,3 @@ void ConfigCompiler::RegisterZoneDir(const String& tag, const String& ppath, con
m_ZoneDirs[zoneName].push_back(zf);
}
const std::vector<String>& ConfigCompiler::GetKeywords(void)
{
static std::vector<String> keywords;
if (keywords.empty()) {
keywords.push_back("object");
keywords.push_back("template");
keywords.push_back("include");
keywords.push_back("include_recursive");
keywords.push_back("include_zones");
keywords.push_back("library");
keywords.push_back("null");
keywords.push_back("true");
keywords.push_back("false");
keywords.push_back("const");
keywords.push_back("var");
keywords.push_back("this");
keywords.push_back("globals");
keywords.push_back("locals");
keywords.push_back("use");
keywords.push_back("ignore_on_error");
keywords.push_back("apply");
keywords.push_back("to");
keywords.push_back("where");
keywords.push_back("import");
keywords.push_back("assign");
keywords.push_back("ignore");
keywords.push_back("function");
keywords.push_back("return");
keywords.push_back("break");
keywords.push_back("continue");
keywords.push_back("for");
keywords.push_back("if");
keywords.push_back("else");
keywords.push_back("while");
keywords.push_back("throw");
}
return keywords;
}

View File

@ -118,8 +118,6 @@ public:
static std::vector<ZoneFragment> GetZoneDirs(const String& zone);
static void RegisterZoneDir(const String& tag, const String& ppath, const String& zoneName);
static const std::vector<String>& GetKeywords(void);
private:
boost::promise<boost::shared_ptr<Expression> > m_Promise;

View File

@ -21,8 +21,8 @@
#include "icinga/icingaapplication.tcpp"
#include "icinga/cib.hpp"
#include "icinga/macroprocessor.hpp"
#include "config/configwriter.hpp"
#include "config/configcompiler.hpp"
#include "base/configwriter.hpp"
#include "base/configtype.hpp"
#include "base/logger.hpp"
#include "base/objectlock.hpp"

View File

@ -21,7 +21,7 @@
#include "remote/configpackageutility.hpp"
#include "config/configcompiler.hpp"
#include "config/configitem.hpp"
#include "config/configwriter.hpp"
#include "base/configwriter.hpp"
#include "base/exception.hpp"
#include "base/serializer.hpp"
#include "base/dependencygraph.hpp"