summaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorAndy Cedilnik <andy.cedilnik@kitware.com>2005-10-17 13:10:20 (GMT)
committerAndy Cedilnik <andy.cedilnik@kitware.com>2005-10-17 13:10:20 (GMT)
commit6e5cdd6de72de0d9d8f91c9971f0c13e10511c73 (patch)
tree03b8912732fb9070de978fc883cfb8e70ade305d /Source
parent33ac18891ff73863b088ca3f373eb90620637c11 (diff)
downloadCMake-6e5cdd6de72de0d9d8f91c9971f0c13e10511c73.zip
CMake-6e5cdd6de72de0d9d8f91c9971f0c13e10511c73.tar.gz
CMake-6e5cdd6de72de0d9d8f91c9971f0c13e10511c73.tar.bz2
ENH: Add regular string replace (not regex), and relative path command. Also add tests
Diffstat (limited to 'Source')
-rw-r--r--Source/cmFileCommand.cxx39
-rw-r--r--Source/cmFileCommand.h5
-rw-r--r--Source/cmStringCommand.cxx30
-rw-r--r--Source/cmStringCommand.h10
4 files changed, 83 insertions, 1 deletions
diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx
index 8b9daa2..4a06cb4 100644
--- a/Source/cmFileCommand.cxx
+++ b/Source/cmFileCommand.cxx
@@ -58,6 +58,10 @@ bool cmFileCommand::InitialPass(std::vector<std::string> const& args)
{
return this->HandleInstallCommand(args);
}
+ else if ( subCommand == "RELATIVE_PATH" )
+ {
+ return this->HandleRelativePathCommand(args);
+ }
std::string e = "does not recognize sub-command "+subCommand;
this->SetError(e.c_str());
@@ -696,3 +700,38 @@ bool cmFileCommand::HandleInstallCommand(
return true;
}
+
+//----------------------------------------------------------------------------
+bool cmFileCommand::HandleRelativePathCommand(
+ std::vector<std::string> const& args)
+{
+ if(args.size() != 4 )
+ {
+ this->SetError("called with incorrect number of arguments");
+ return false;
+ }
+
+ const std::string& outVar = args[1];
+ const std::string& directoryName = args[2];
+ const std::string& fileName = args[3];
+
+ if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
+ {
+ std::string errstring = "RelativePath must be passed a full path to the directory: " + directoryName;
+ this->SetError(errstring.c_str());
+ return false;
+ }
+ if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
+ {
+ std::string errstring = "RelativePath must be passed a full path to the directory: " + directoryName;
+ this->SetError(errstring.c_str());
+ return false;
+ }
+
+ std::string res = cmSystemTools::RelativePath(directoryName.c_str(), fileName.c_str());
+ m_Makefile->AddDefinition(outVar.c_str(),
+ res.c_str());
+ return true;
+}
+
+
diff --git a/Source/cmFileCommand.h b/Source/cmFileCommand.h
index 71bca90..9243180 100644
--- a/Source/cmFileCommand.h
+++ b/Source/cmFileCommand.h
@@ -70,6 +70,7 @@ public:
" FILE(GLOB variable [globbing expressions]...)\n"
" FILE(GLOB_RECURSE variable [globbing expressions]...)\n"
" FILE(MAKE_DIRECTORY [directory]...)\n"
+ " FILE(RELATIVE_PATH variable directory file)\n"
"WRITE will write a message into a file called 'filename'. It "
"overwrites the file if it already exists, and creates the file "
"if it does not exists.\n"
@@ -93,7 +94,8 @@ public:
"match the files.\n"
"Example of recursive globbing:\n"
" /dir/*.py - match all python files /dir and subdirectories\n"
- "MAKE_DIRECTORY will create a directory at the specified location";
+ "MAKE_DIRECTORY will create a directory at the specified location\n"
+ "RELATIVE_PATH will determine relative path from directory to the given file";
}
cmTypeMacro(cmFileCommand, cmCommand);
@@ -104,6 +106,7 @@ protected:
bool HandleGlobCommand(std::vector<std::string> const& args, bool recurse);
bool HandleMakeDirectoryCommand(std::vector<std::string> const& args);
bool HandleInstallCommand(std::vector<std::string> const& args);
+ bool HandleRelativePathCommand(std::vector<std::string> const& args);
};
diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx
index 76d60e0..dc592ba 100644
--- a/Source/cmStringCommand.cxx
+++ b/Source/cmStringCommand.cxx
@@ -16,6 +16,7 @@
=========================================================================*/
#include "cmStringCommand.h"
#include <cmsys/RegularExpression.hxx>
+#include <cmsys/SystemTools.hxx>
#include <stdlib.h> // required for atoi
#include <ctype.h>
@@ -33,6 +34,10 @@ bool cmStringCommand::InitialPass(std::vector<std::string> const& args)
{
return this->HandleRegexCommand(args);
}
+ else if(subCommand == "REPLACE")
+ {
+ return this->HandleReplaceCommand(args);
+ }
else if(subCommand == "TOLOWER")
{
return this->HandleToUpperLowerCommand(args, false);
@@ -492,3 +497,28 @@ bool cmStringCommand::HandleCompareCommand(std::vector<std::string> const& args)
this->SetError(e.c_str());
return false;
}
+
+//----------------------------------------------------------------------------
+bool cmStringCommand::HandleReplaceCommand(std::vector<std::string> const& args)
+{
+ if(args.size() < 5)
+ {
+ this->SetError("sub-command REPLACE requires four arguments.");
+ return false;
+ }
+
+ const std::string& matchExpression = args[1];
+ const std::string& replaceExpression = args[2];
+ const std::string& variableName = args[3];
+
+ std::string input = args[4];
+ for(unsigned int i=5; i < args.size(); ++i)
+ {
+ input += args[i];
+ }
+
+ cmsys::SystemTools::ReplaceString(input, matchExpression.c_str(), replaceExpression.c_str());
+
+ m_Makefile->AddDefinition(variableName.c_str(), input.c_str());
+ return true;
+}
diff --git a/Source/cmStringCommand.h b/Source/cmStringCommand.h
index 91463c4..cbdf811 100644
--- a/Source/cmStringCommand.h
+++ b/Source/cmStringCommand.h
@@ -71,6 +71,9 @@ public:
" STRING(REGEX REPLACE <regular_expression>\n"
" <replace_expression> <output variable>\n"
" <input> [<input>...])\n"
+ " STRING(REPLACE <match_expression>\n"
+ " <replace_expression> <output variable>\n"
+ " <input> [<input>...])\n"
" STRING(COMPARE EQUAL <string1> <string2> <output variable>)\n"
" STRING(COMPARE NOTEQUAL <string1> <string2> <output variable>)\n"
" STRING(COMPARE LESS <string1> <string2> <output variable>)\n"
@@ -90,6 +93,12 @@ public:
"subexpressions of the match using \\1, \\2, ..., \\9. Note that "
"two backslashes (\\\\1) are required in CMake code to get a "
"backslash through argument parsing.\n"
+ "REPLACE will match the given expression "
+ "and substitute the replacement expression for the match "
+ "in the output. The replace expression may refer to paren-delimited "
+ "subexpressions of the match using \\1, \\2, ..., \\9. Note that "
+ "two backslashes (\\\\1) are required in CMake code to get a "
+ "backslash through argument parsing.\n"
"COMPARE EQUAL/NOTEQUAL/LESS/GREATER will compare the strings and "
"store true or false in the output variable.\n"
"ASCII will convert all numbers into corresponding ASCII characters.\n"
@@ -108,6 +117,7 @@ protected:
bool RegexReplace(std::vector<std::string> const& args);
bool HandleToUpperLowerCommand(std::vector<std::string> const& args, bool toUpper);
bool HandleCompareCommand(std::vector<std::string> const& args);
+ bool HandleReplaceCommand(std::vector<std::string> const& args);
class RegexReplacement
{