diff Core/Toolbox.cpp @ 2920:ad0e7def3338

Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 07 Nov 2018 11:13:30 +0100
parents 0204af4ece6a
children 0a4428aad512
line wrap: on
line diff
--- a/Core/Toolbox.cpp	Tue Nov 06 15:41:21 2018 +0100
+++ b/Core/Toolbox.cpp	Wed Nov 07 11:13:30 2018 +0100
@@ -1490,6 +1490,53 @@
 #endif
     return s;
   }
+
+
+  namespace
+  {
+    // Anonymous namespace to avoid clashes between compilation modules
+
+    class VariableFormatter
+    {
+    public:
+      typedef std::map<std::string, std::string>   Dictionary;
+
+    private:
+      const Dictionary& dictionary_;
+
+    public:
+      VariableFormatter(const Dictionary& dictionary) :
+        dictionary_(dictionary)
+      {
+      }
+  
+      template<typename Out>
+      Out operator()(const boost::smatch& what,
+                     Out out) const
+      {
+        Dictionary::const_iterator found = dictionary_.find(what[1]);
+    
+        if (found != dictionary_.end())
+        {
+          const std::string& value = found->second;
+          out = std::copy(value.begin(), value.end(), out);
+        }
+    
+        return out;
+      }
+    };
+  }
+
+  
+  std::string Toolbox::SubstituteVariables(const std::string& source,
+                                           const std::map<std::string, std::string>& dictionary)
+  {
+    const boost::regex pattern("\\${(.*?)}");
+
+    VariableFormatter formatter(dictionary);
+
+    return boost::regex_replace(source, pattern, formatter);
+  }
 }