diff options
author | Daniel Franke <franke@edf-online.de> | 2018-05-18 19:59:46 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2018-07-03 13:55:58 (GMT) |
commit | 5dbee9d2d0f68e1fc343d04ac00a4a35d43df6fa (patch) | |
tree | 85375da2fabdfbc7982fe4ba58a76acc3167c71e /Source/cmMathCommand.cxx | |
parent | 8661e7052c4f711f13e7168231276e23c4c0defd (diff) | |
download | CMake-5dbee9d2d0f68e1fc343d04ac00a4a35d43df6fa.zip CMake-5dbee9d2d0f68e1fc343d04ac00a4a35d43df6fa.tar.gz CMake-5dbee9d2d0f68e1fc343d04ac00a4a35d43df6fa.tar.bz2 |
math: Add options to calculate and format output as hexadecimal
Diffstat (limited to 'Source/cmMathCommand.cxx')
-rw-r--r-- | Source/cmMathCommand.cxx | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/Source/cmMathCommand.cxx b/Source/cmMathCommand.cxx index ab77dc3..5d1b099 100644 --- a/Source/cmMathCommand.cxx +++ b/Source/cmMathCommand.cxx @@ -28,16 +28,59 @@ bool cmMathCommand::InitialPass(std::vector<std::string> const& args, bool cmMathCommand::HandleExprCommand(std::vector<std::string> const& args) { - if (args.size() != 3) { + if ((args.size() != 3) && (args.size() != 5)) { this->SetError("EXPR called with incorrect arguments."); return false; } + enum class NumericFormat + { + UNINITIALIZED, + DECIMAL, + HEXADECIMAL, + }; + const std::string& outputVariable = args[1]; const std::string& expression = args[2]; + size_t argumentIndex = 3; + NumericFormat outputFormat = NumericFormat::UNINITIALIZED; this->Makefile->AddDefinition(outputVariable, "ERROR"); + if (argumentIndex < args.size()) { + const std::string messageHint = "sub-command EXPR "; + const std::string option = args[argumentIndex++]; + if (option == "OUTPUT_FORMAT") { + if (argumentIndex < args.size()) { + const std::string argument = args[argumentIndex++]; + if (argument == "DECIMAL") { + outputFormat = NumericFormat::DECIMAL; + } else if (argument == "HEXADECIMAL") { + outputFormat = NumericFormat::HEXADECIMAL; + } else { + std::string error = messageHint + "value \"" + argument + + "\" for option \"" + option + "\" is invalid."; + this->SetError(error); + return false; + } + } else { + std::string error = + messageHint + "missing argument for option \"" + option + "\"."; + this->SetError(error); + return false; + } + } else { + std::string error = + messageHint + "option \"" + option + "\" is unknown."; + this->SetError(error); + return false; + } + } + + if (outputFormat == NumericFormat::UNINITIALIZED) { + outputFormat = NumericFormat::DECIMAL; + } + cmExprParserHelper helper; if (!helper.ParseString(expression.c_str(), 0)) { this->SetError(helper.GetError()); @@ -45,7 +88,18 @@ bool cmMathCommand::HandleExprCommand(std::vector<std::string> const& args) } char buffer[1024]; - sprintf(buffer, "%" KWIML_INT_PRId64, helper.GetResult()); + const char* fmt; + switch (outputFormat) { + case NumericFormat::HEXADECIMAL: + fmt = "0x%" KWIML_INT_PRIx64; + break; + case NumericFormat::DECIMAL: + CM_FALLTHROUGH; + default: + fmt = "%" KWIML_INT_PRId64; + break; + } + sprintf(buffer, fmt, helper.GetResult()); this->Makefile->AddDefinition(outputVariable, buffer); return true; |