From c8b1a5dfe4b63b987c2216bdcb12938a34da6ca9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fl=C3=A1vio=20J.=20Saraiva?= <flaviojs2005@gmail.com>
Date: Wed, 20 Jul 2011 10:30:04 +0100
Subject: [PATCH 14/14] added STRING(ESCAPE) command

---
 Source/cmStringCommand.cxx |   33 +++++++++++++++++++++++++++++++++
 Source/cmStringCommand.h   |    4 ++++
 2 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx
index d239c06..ed7c7f2 100644
--- a/Source/cmStringCommand.cxx
+++ b/Source/cmStringCommand.cxx
@@ -56,6 +56,10 @@ bool cmStringCommand
     {
     return this->HandleConfigureCommand(args);
     }
+  else if(subCommand == "ESCAPE")
+    {
+    return this->HandleEscapeCommand(args);
+    }
   else if(subCommand == "LENGTH")
     {
     return this->HandleLengthCommand(args);
@@ -189,6 +193,35 @@ bool cmStringCommand::HandleConfigureCommand(
 }
 
 //----------------------------------------------------------------------------
+bool cmStringCommand::HandleEscapeCommand(
+  std::vector<std::string> const& args)
+{
+  if ( args.size() < 2 )
+    {
+    this->SetError("No input string specified.");
+    return false;
+    }
+  else if ( args.size() < 3 )
+    {
+    this->SetError("No output variable specified.");
+    return false;
+    }
+  
+  std::string outvar = args[2];
+  std::string output;
+  
+  output = cmSystemTools::EscapeChars(args[1].c_str(), "\\\"\n\r\t\v'$@");
+  cmSystemTools::ReplaceChars(const_cast<char *>(output.c_str()), "\n", 'n');
+  cmSystemTools::ReplaceChars(const_cast<char *>(output.c_str()), "\r", 'r');
+  cmSystemTools::ReplaceChars(const_cast<char *>(output.c_str()), "\t", 't');
+  cmSystemTools::ReplaceChars(const_cast<char *>(output.c_str()), "\v", 'v');
+  
+  // Store the output in the provided variable.
+  this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
+  return true;
+}
+
+//----------------------------------------------------------------------------
 bool cmStringCommand::HandleRegexCommand(std::vector<std::string> const& args)
 {
   if(args.size() < 2)
diff --git a/Source/cmStringCommand.h b/Source/cmStringCommand.h
index 52b83d9..f977d3a 100644
--- a/Source/cmStringCommand.h
+++ b/Source/cmStringCommand.h
@@ -83,6 +83,7 @@ public:
       "  string(ASCII <number> [<number> ...] <output variable>)\n"
       "  string(CONFIGURE <string1> <output variable>\n"
       "         [@ONLY] [ESCAPE_QUOTES])\n"
+      "  string(ESCAPE <string1> <output variable>)\n"
       "  string(TOUPPER <string1> <output variable>)\n"
       "  string(TOLOWER <string1> <output variable>)\n"
       "  string(LENGTH <string> <output variable>)\n"
@@ -108,6 +109,8 @@ public:
       "ASCII will convert all numbers into corresponding ASCII characters.\n"
       "CONFIGURE will transform a string like CONFIGURE_FILE transforms "
       "a file.\n"
+      "ESCAPE will escape the characters of a string. The resulting string "
+      "can be used as a string literal in configured/generated cmake files.\n"
       "TOUPPER/TOLOWER will convert string to upper/lower characters.\n"
       "LENGTH will return a given string's length.\n"
       "SUBSTRING will return a substring of a given string. If length is "
@@ -145,6 +148,7 @@ public:
   static void StoreMatches(cmMakefile* mf, cmsys::RegularExpression& re);
 protected:
   bool HandleConfigureCommand(std::vector<std::string> const& args);
+  bool HandleEscapeCommand(std::vector<std::string> const& args);
   bool HandleAsciiCommand(std::vector<std::string> const& args);
   bool HandleRegexCommand(std::vector<std::string> const& args);
   bool RegexMatch(std::vector<std::string> const& args);
-- 
1.7.4.msysgit.0

