2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2005-10-18 00:42:47 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2005-10-18 00:42:47 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
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-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 1) {
|
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;
|
|
|
|
}
|