diff --git a/parser/mpFuncCommon.cpp b/parser/mpFuncCommon.cpp index e342f12..7e05733 100644 --- a/parser/mpFuncCommon.cpp +++ b/parser/mpFuncCommon.cpp @@ -43,6 +43,7 @@ #include "mpValue.h" #include "mpParserBase.h" +#include "equationsParser.h" #define ONE_DAY (24 * 60 * 60) @@ -399,6 +400,73 @@ MUP_NAMESPACE_START return new FunMask(*this); } + //------------------------------------------------------------------------------ + // + // Case + // + //------------------------------------------------------------------------------ + + FunCase::FunCase() + :ICallback(cmFUNC, _T("case"), -1) + {} + + Value get_case_value(string_type expression) { + Value result; + if(expression[0] == '@'){ + result = EquationsParser::PureCalc(expression.substr(1)); + } else { + result = expression; + } + return result; + } + + bool compare_variable_to_operand(string_type variable, string_type operand) { + bool comparison_result; + if(operand[0] == '@'){ + Value result = EquationsParser::PureCalc(variable + operand.substr(1)); + if(result.GetType() != 'b'){ + //add error here + } + comparison_result = result.GetBool(); + } else { + comparison_result = (variable.compare(operand) == 0); + } + return comparison_result; + } + + void FunCase::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) + { + if (a_iArgc < 2) { + throw ParserError(ErrorContext(ecTOO_FEW_PARAMS, GetExprPos(), GetIdent())); + } + + string_type variable = a_pArg[0]->GetString(); + string_type operand, result, case_arguments; + for(int i=1; i < a_iArgc ; i++) { + case_arguments = a_pArg[i]->GetString(); + size_t pos = case_arguments.find(';'); + if(pos == std::string::npos || pos == 0 || pos == case_arguments.size()){ + //add error here + } + operand = case_arguments.substr(0, pos); + result = case_arguments.substr(pos + 1); + if(compare_variable_to_operand(variable, operand) || operand.compare("default") == 0){ + *ret = get_case_value(result); + break; + } + } + } + + const char_type* FunCase::GetDesc() const + { + return _T("case(date) - Returns the week day of the date."); + } + + ////------------------------------------------------------------------------------ + IToken* FunCase::Clone() const + { + return new FunCase(*this); + } //------------------------------------------------------------------------------ // | // Below we have the section related to Date functions | diff --git a/parser/mpFuncCommon.h b/parser/mpFuncCommon.h index d80ec49..86e23ee 100644 --- a/parser/mpFuncCommon.h +++ b/parser/mpFuncCommon.h @@ -133,6 +133,19 @@ MUP_NAMESPACE_START virtual IToken* Clone() const override; }; // class FunMask + //------------------------------------------------------------------------------ + /** \brief Returns the result of a case operation. + \ingroup functions + */ + class FunCase: public ICallback + { + public: + FunCase(); + virtual void Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc) override; + virtual const char_type* GetDesc() const override; + virtual IToken* Clone() const override; + }; // class FunCase + //------------------------------------------------------------------------------ /** \brief Determine the difference in days between two dates. \ingroup functions diff --git a/parser/mpPackageCommon.cpp b/parser/mpPackageCommon.cpp index d3cb474..4248b89 100644 --- a/parser/mpPackageCommon.cpp +++ b/parser/mpPackageCommon.cpp @@ -93,6 +93,7 @@ void PackageCommon::AddToParser(ParserXBase *pParser) // Special functions pParser->DefineFun(new FunMask()); + pParser->DefineFun(new FunCase()); // Date functions pParser->DefineFun(new FunDaysDiff());