You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.9 KiB

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