cmMathCommand.cxx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmMathCommand.h"
  11. #include "cmExprParserHelper.h"
  12. //----------------------------------------------------------------------------
  13. bool cmMathCommand
  14. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  15. {
  16. if ( args.size() < 1 )
  17. {
  18. this->SetError("must be called with at least one argument.");
  19. return false;
  20. }
  21. const std::string &subCommand = args[0];
  22. if(subCommand == "EXPR")
  23. {
  24. return this->HandleExprCommand(args);
  25. }
  26. std::string e = "does not recognize sub-command "+subCommand;
  27. this->SetError(e.c_str());
  28. return false;
  29. }
  30. //----------------------------------------------------------------------------
  31. bool cmMathCommand::HandleExprCommand(std::vector<std::string> const& args)
  32. {
  33. if ( args.size() != 3 )
  34. {
  35. this->SetError("EXPR called with incorrect arguments.");
  36. return false;
  37. }
  38. const std::string& outputVariable = args[1];
  39. const std::string& expression = args[2];
  40. cmExprParserHelper helper;
  41. if ( !helper.ParseString(expression.c_str(), 0) )
  42. {
  43. std::string e = "cannot parse the expression: \""+expression+"\": ";
  44. e += helper.GetError();
  45. this->SetError(e.c_str());
  46. return false;
  47. }
  48. char buffer[1024];
  49. sprintf(buffer, "%d", helper.GetResult());
  50. this->Makefile->AddDefinition(outputVariable.c_str(), buffer);
  51. return true;
  52. }