Browse Source

string: Add MAKE_C_IDENTIFIER subcommand

Stephen Kelly 12 years ago
parent
commit
0ab50aea4c
3 changed files with 33 additions and 1 deletions
  1. 22 0
      Source/cmStringCommand.cxx
  2. 5 1
      Source/cmStringCommand.h
  3. 6 0
      Tests/StringFileTest/CMakeLists.txt

+ 22 - 0
Source/cmStringCommand.cxx

@@ -93,6 +93,10 @@ bool cmStringCommand
     {
     {
     return this->HandleTimestampCommand(args);
     return this->HandleTimestampCommand(args);
     }
     }
+  else if(subCommand == "MAKE_C_IDENTIFIER")
+    {
+    return this->HandleMakeCIdentifierCommand(args);
+    }
 
 
   std::string e = "does not recognize sub-command "+subCommand;
   std::string e = "does not recognize sub-command "+subCommand;
   this->SetError(e.c_str());
   this->SetError(e.c_str());
@@ -754,6 +758,24 @@ bool cmStringCommand
   return true;
   return true;
 }
 }
 
 
+//----------------------------------------------------------------------------
+bool cmStringCommand
+::HandleMakeCIdentifierCommand(std::vector<std::string> const& args)
+{
+  if(args.size() != 3)
+    {
+    this->SetError("sub-command MAKE_C_IDENTIFIER requires two arguments.");
+    return false;
+    }
+
+  const std::string& input = args[1];
+  const std::string& variableName = args[2];
+
+  this->Makefile->AddDefinition(variableName.c_str(),
+                      cmSystemTools::MakeCidentifier(input.c_str()).c_str());
+  return true;
+}
+
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
 bool cmStringCommand::HandleStripCommand(
 bool cmStringCommand::HandleStripCommand(
   std::vector<std::string> const& args)
   std::vector<std::string> const& args)

+ 5 - 1
Source/cmStringCommand.h

@@ -94,6 +94,7 @@ public:
       "         [RANDOM_SEED <seed>] <output variable>)\n"
       "         [RANDOM_SEED <seed>] <output variable>)\n"
       "  string(FIND <string> <substring> <output variable> [REVERSE])\n"
       "  string(FIND <string> <substring> <output variable> [REVERSE])\n"
       "  string(TIMESTAMP <output variable> [<format string>] [UTC])\n"
       "  string(TIMESTAMP <output variable> [<format string>] [UTC])\n"
+      "  string(MAKE_C_IDENTIFIER <input string> <output variable>)\n"
       "REGEX MATCH will match the regular expression once and store the "
       "REGEX MATCH will match the regular expression once and store the "
       "match in the output variable.\n"
       "match in the output variable.\n"
       "REGEX MATCHALL will match the regular expression as many times as "
       "REGEX MATCHALL will match the regular expression as many times as "
@@ -176,7 +177,9 @@ public:
       "and copied to the output as-is.\n"
       "and copied to the output as-is.\n"
       "If no explicit <format string> is given it will default to:\n"
       "If no explicit <format string> is given it will default to:\n"
       "   %Y-%m-%dT%H:%M:%S    for local time.\n"
       "   %Y-%m-%dT%H:%M:%S    for local time.\n"
-      "   %Y-%m-%dT%H:%M:%SZ   for UTC.";
+      "   %Y-%m-%dT%H:%M:%SZ   for UTC.\n"
+      "MAKE_C_IDENTIFIER will write a string which can be used as an "
+      "identifier in C.";
     }
     }
 
 
   cmTypeMacro(cmStringCommand, cmCommand);
   cmTypeMacro(cmStringCommand, cmCommand);
@@ -200,6 +203,7 @@ protected:
   bool HandleRandomCommand(std::vector<std::string> const& args);
   bool HandleRandomCommand(std::vector<std::string> const& args);
   bool HandleFindCommand(std::vector<std::string> const& args);
   bool HandleFindCommand(std::vector<std::string> const& args);
   bool HandleTimestampCommand(std::vector<std::string> const& args);
   bool HandleTimestampCommand(std::vector<std::string> const& args);
+  bool HandleMakeCIdentifierCommand(std::vector<std::string> const& args);
 
 
   class RegexReplacement
   class RegexReplacement
   {
   {

+ 6 - 0
Tests/StringFileTest/CMakeLists.txt

@@ -280,3 +280,9 @@ endif()
 if(NOT ST_NINE STREQUAL "9")
 if(NOT ST_NINE STREQUAL "9")
   message(SEND_ERROR "SUBSTRING does not return the tail when selected with -1")
   message(SEND_ERROR "SUBSTRING does not return the tail when selected with -1")
 endif()
 endif()
+
+string(MAKE_C_IDENTIFIER "1one-two$" MCI_1)
+
+if(NOT MCI_1 STREQUAL _1one_two_)
+  message(SEND_ERROR "MAKE_C_IDENTIFIER did not create expected result.")
+endif()