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