[icinga-checkins] icinga.org: icinga2/master: Refactor String-Escape in repositoryutility

git at icinga.org git at icinga.org
Tue Feb 9 09:39:38 CET 2016


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

Author: Jean Flach <jean-marcel.flach at netways.de>
Date:   Tue Feb  9 09:38:12 2016 +0100

Refactor String-Escape in repositoryutility

fixes #10989

---

 lib/base/configwriter.hpp     |    3 +--
 lib/cli/repositoryutility.cpp |   20 ++++----------------
 lib/cli/repositoryutility.hpp |    1 -
 3 files changed, 5 insertions(+), 19 deletions(-)

diff --git a/lib/base/configwriter.hpp b/lib/base/configwriter.hpp
index 7f746e6..bc7862a 100644
--- a/lib/base/configwriter.hpp
+++ b/lib/base/configwriter.hpp
@@ -73,10 +73,9 @@ public:
 	static void EmitFunctionCall(std::ostream& fp, const String& name, const Array::Ptr& arguments);
 
 	static const std::vector<String>& GetKeywords(void);
+	static String EscapeIcingaString(const String& str);
 private:
 	ConfigWriter(void);
-
-	static String EscapeIcingaString(const String& str);
 };
 
 }
diff --git a/lib/cli/repositoryutility.cpp b/lib/cli/repositoryutility.cpp
index eabe0bb..1f028a9 100644
--- a/lib/cli/repositoryutility.cpp
+++ b/lib/cli/repositoryutility.cpp
@@ -22,6 +22,7 @@
 #include "base/logger.hpp"
 #include "base/application.hpp"
 #include "base/convert.hpp"
+#include "base/configwriter.hpp"
 #include "base/scriptglobal.hpp"
 #include "base/json.hpp"
 #include "base/netstring.hpp"
@@ -683,7 +684,7 @@ void RepositoryUtility::FormatChangelogEntry(std::ostream& fp, const Dictionary:
  */
 void RepositoryUtility::SerializeObject(std::ostream& fp, const String& name, const String& type, const Dictionary::Ptr& object)
 {
-	fp << "object " << type << " \"" << EscapeIcingaString(name) << "\" {\n";
+	fp << "object " << type << " \"" << ConfigWriter::EscapeIcingaString(name) << "\" {\n";
 
 	if (!object) {
 		fp << "}\n";
@@ -712,19 +713,6 @@ void RepositoryUtility::SerializeObject(std::ostream& fp, const String& name, co
 	fp << "}\n";
 }
 
-String RepositoryUtility::EscapeIcingaString(const String& str)
-{
-	String result = str;
-	boost::algorithm::replace_all(result, "\\", "\\\\");
-	boost::algorithm::replace_all(result, "\n", "\\n");
-	boost::algorithm::replace_all(result, "\t", "\\t");
-	boost::algorithm::replace_all(result, "\r", "\\r");
-	boost::algorithm::replace_all(result, "\b", "\\b");
-	boost::algorithm::replace_all(result, "\f", "\\f");
-	boost::algorithm::replace_all(result, "\"", "\\\"");
-	return result;
-}
-
 void RepositoryUtility::FormatValue(std::ostream& fp, const Value& val)
 {
 	if (val.IsObjectType<Array>()) {
@@ -733,11 +721,11 @@ void RepositoryUtility::FormatValue(std::ostream& fp, const Value& val)
 	}
 
 	if (val.IsString()) {
-		fp << "\"" << EscapeIcingaString(val) << "\"";
+		fp << "\"" << ConfigWriter::EscapeIcingaString(val) << "\"";
 		return;
 	}
 
-	fp << EscapeIcingaString(val);
+	fp << ConfigWriter::EscapeIcingaString(val);
 }
 
 void RepositoryUtility::FormatArray(std::ostream& fp, const Array::Ptr& arr)
diff --git a/lib/cli/repositoryutility.hpp b/lib/cli/repositoryutility.hpp
index 02ea03d..23d2a69 100644
--- a/lib/cli/repositoryutility.hpp
+++ b/lib/cli/repositoryutility.hpp
@@ -96,7 +96,6 @@ private:
 
 	/* config print helpers */
 	static void SerializeObject(std::ostream& fp, const String& name, const String& type, const Dictionary::Ptr& object);
-	static String EscapeIcingaString(const String& str);
 	static void FormatValue(std::ostream& fp, const Value& val);
 	static void FormatArray(std::ostream& fp, const Array::Ptr& arr);
 



More information about the icinga-checkins mailing list