2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2005-10-18 00:42:47 +04:00
|
|
|
#include "cmMathCommand.h"
|
|
|
|
|
|
|
|
#include "cmExprParserHelper.h"
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmMathCommand::InitialPass(std::vector<std::string> const& args,
|
|
|
|
cmExecutionStatus&)
|
2005-10-18 00:42:47 +04:00
|
|
|
{
|
2016-09-16 00:59:29 +03:00
|
|
|
if (args.empty()) {
|
2005-10-18 00:42:47 +04:00
|
|
|
this->SetError("must be called with at least one argument.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
const std::string& subCommand = args[0];
|
|
|
|
if (subCommand == "EXPR") {
|
2005-10-18 00:42:47 +04:00
|
|
|
return this->HandleExprCommand(args);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
std::string e = "does not recognize sub-command " + subCommand;
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2005-10-18 00:42:47 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmMathCommand::HandleExprCommand(std::vector<std::string> const& args)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() != 3) {
|
2005-10-18 00:42:47 +04:00
|
|
|
this->SetError("EXPR called with incorrect arguments.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-10-18 00:42:47 +04:00
|
|
|
|
|
|
|
const std::string& outputVariable = args[1];
|
|
|
|
const std::string& expression = args[2];
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2005-10-18 00:42:47 +04:00
|
|
|
cmExprParserHelper helper;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!helper.ParseString(expression.c_str(), 0)) {
|
|
|
|
std::string e = "cannot parse the expression: \"" + expression + "\": ";
|
2005-10-18 00:42:47 +04:00
|
|
|
e += helper.GetError();
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2005-10-18 00:42:47 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-10-18 00:42:47 +04:00
|
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
sprintf(buffer, "%d", helper.GetResult());
|
|
|
|
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(outputVariable, buffer);
|
2005-10-18 00:42:47 +04:00
|
|
|
return true;
|
|
|
|
}
|