summaryrefslogtreecommitdiffstats
path: root/Source/cmStringCommand.cxx
diff options
context:
space:
mode:
authorAndy Cedilnik <andy.cedilnik@kitware.com>2005-10-17 13:56:42 (GMT)
committerAndy Cedilnik <andy.cedilnik@kitware.com>2005-10-17 13:56:42 (GMT)
commit12ef4edf64d2dd26cd03bd355b0f710b410f8d29 (patch)
tree97fa87a793899869df2c1915b6d2ea5c8f0e174c /Source/cmStringCommand.cxx
parent6e5cdd6de72de0d9d8f91c9971f0c13e10511c73 (diff)
downloadCMake-12ef4edf64d2dd26cd03bd355b0f710b410f8d29.zip
CMake-12ef4edf64d2dd26cd03bd355b0f710b410f8d29.tar.gz
CMake-12ef4edf64d2dd26cd03bd355b0f710b410f8d29.tar.bz2
ENH: Add String length and substring
Diffstat (limited to 'Source/cmStringCommand.cxx')
-rw-r--r--Source/cmStringCommand.cxx64
1 files changed, 64 insertions, 0 deletions
diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx
index dc592ba..d7b6a91 100644
--- a/Source/cmStringCommand.cxx
+++ b/Source/cmStringCommand.cxx
@@ -58,6 +58,14 @@ bool cmStringCommand::InitialPass(std::vector<std::string> const& args)
{
return this->HandleConfigureCommand(args);
}
+ else if(subCommand == "LENGTH")
+ {
+ return this->HandleLengthCommand(args);
+ }
+ else if(subCommand == "SUBSTRING")
+ {
+ return this->HandleSubstringCommand(args);
+ }
std::string e = "does not recognize sub-command "+subCommand;
this->SetError(e.c_str());
@@ -522,3 +530,59 @@ bool cmStringCommand::HandleReplaceCommand(std::vector<std::string> const& args)
m_Makefile->AddDefinition(variableName.c_str(), input.c_str());
return true;
}
+
+//----------------------------------------------------------------------------
+bool cmStringCommand::HandleSubstringCommand(std::vector<std::string> const& args)
+{
+ if(args.size() != 5)
+ {
+ this->SetError("sub-command REPLACE requires four arguments.");
+ return false;
+ }
+
+ const std::string& stringValue = args[1];
+ int begin = atoi(args[2].c_str());
+ int end = atoi(args[3].c_str());
+ const std::string& variableName = args[4];
+
+ size_t stringLength = stringValue.size();
+ int intStringLength = static_cast<int>(stringLength);
+ if ( begin < 0 || begin > intStringLength )
+ {
+ cmOStringStream ostr;
+ ostr << "begin index: " << begin << " is out of range 0 - " << stringLength;
+ this->SetError(ostr.str().c_str());
+ return false;
+ }
+ int leftOverLength = intStringLength - begin;
+ if ( end < 0 || end > intStringLength )
+ {
+ cmOStringStream ostr;
+ ostr << "end index: " << end << " is out of range " << 0 << " - " << leftOverLength;
+ this->SetError(ostr.str().c_str());
+ return false;
+ }
+
+ m_Makefile->AddDefinition(variableName.c_str(), stringValue.substr(begin, end).c_str());
+ return true;
+}
+
+//----------------------------------------------------------------------------
+bool cmStringCommand::HandleLengthCommand(std::vector<std::string> const& args)
+{
+ if(args.size() != 3)
+ {
+ this->SetError("sub-command LENGTH requires two arguments.");
+ return false;
+ }
+
+ const std::string& stringValue = args[1];
+ const std::string& variableName = args[2];
+
+ size_t length = stringValue.size();
+ char buffer[1024];
+ sprintf(buffer, "%d", length);
+
+ m_Makefile->AddDefinition(variableName.c_str(), buffer);
+ return true;
+}