]> granicus.if.org Git - icinga2/commitdiff
Implement len() and the operators >, >=, < and <=.
authorGunnar Beutner <gunnar.beutner@netways.de>
Wed, 19 Mar 2014 12:25:06 +0000 (13:25 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Wed, 19 Mar 2014 12:25:06 +0000 (13:25 +0100)
Refs #5789

doc/4.1-configuration-syntax.md
lib/config/aexpression.cpp
lib/config/aexpression.h
lib/config/config_lexer.ll
lib/config/config_parser.yy
lib/methods/utilityfuncs.cpp
lib/methods/utilityfuncs.h

index d3bdf231140dfefdb51919654d1724e8c562deee..cd0b3313662d73c5d012cc17f6d11fc92353f697 100644 (file)
@@ -320,7 +320,7 @@ Simple calculations can be performed using the constant expression syntax:
       check_interval = 30 + 60
     }
 
-Valid operators include ~, !, +, -, *, /, ==,  !=, in and !in. The default precedence rules can be
+Valid operators include ~, !, +, -, *, /, >, >=, <, <=, ==,  !=, in and !in. The default precedence rules can be
 overridden by grouping expressions using parentheses:
 
     {
index 40dbadea8ec11ce8434c9974eccb2d3cbd830fa6..5ef8d49d68c48bf9a95ea5f3bdc03c16b4363525 100644 (file)
@@ -139,6 +139,14 @@ Value AExpression::Evaluate(const Dictionary::Ptr& locals) const
                        }
 
                        return arr2;
+               case AELessThan:
+                       return (long)left < (long)right;
+               case AEGreaterThan:
+                       return (long)left > (long)right;
+               case AELessThanOrEqual:
+                       return (long)left <= (long)right;
+               case AEGreaterThanOrEqual:
+                       return (long)left >= (long)right;
                default:
                        ASSERT(!"Invalid operator.");
        }
index a2dd99c12060b89777ce13b5dada8f6515796e8a..56c16c33d46d615e2fd4a5eaa60fb8351f5a2db2 100644 (file)
@@ -50,7 +50,11 @@ enum AOperator
        AELogicalAnd,
        AELogicalOr,
        AEFunctionCall,
-       AEArray
+       AEArray,
+       AELessThan,
+       AEGreaterThan,
+       AELessThanOrEqual,
+       AEGreaterThanOrEqual
 };
 
 /**
index f397a5ab77e90b2ef5d1483bcf9bd8bf3241a1f7..54cb1f561474d296e312acda036165883b9a9962 100644 (file)
@@ -227,6 +227,8 @@ to                          return T_TO;
 where                          return T_WHERE;
 \<\<                           return T_SHIFT_LEFT;
 \>\>                           return T_SHIFT_RIGHT;
+\<=                            return T_LESS_THAN_OR_EQUAL;
+\>=                            return T_GREATER_THAN_OR_EQUAL;
 ==                             return T_EQUAL;
 !=                             return T_NOT_EQUAL;
 !in                            return T_NOT_IN;
index f508747a6595cf366bef21d7ab408b0265b3abc7..ce426bd228e52020bf2a487da68097413373eca8 100644 (file)
@@ -86,6 +86,8 @@ using namespace icinga;
 %token T_NOT_IN "!in (T_NOT_IN)"
 %token T_LOGICAL_AND "&& (T_LOGICAL_AND)"
 %token T_LOGICAL_OR "|| (T_LOGICAL_OR)"
+%token T_LESS_THAN_OR_EQUAL "<= (T_LESS_THAN_OR_EQUAL)"
+%token T_GREATER_THAN_OR_EQUAL ">= (T_GREATER_THAN_OR_EQUAL)"
 %token <type> T_TYPE_DICTIONARY "dictionary (T_TYPE_DICTIONARY)"
 %token <type> T_TYPE_ARRAY "array (T_TYPE_ARRAY)"
 %token <type> T_TYPE_NUMBER "number (T_TYPE_NUMBER)"
@@ -627,6 +629,30 @@ aexpression: T_STRING
                delete $1;
                delete $3;
        }
+       | aexpression T_LESS_THAN_OR_EQUAL aexpression
+       {
+               $$ = new Value(make_shared<AExpression>(AELessThanOrEqual, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), yylloc));
+               delete $1;
+               delete $3;
+       }
+       | aexpression T_GREATER_THAN_OR_EQUAL aexpression
+       {
+               $$ = new Value(make_shared<AExpression>(AEGreaterThanOrEqual, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), yylloc));
+               delete $1;
+               delete $3;
+       }
+       | aexpression '<' aexpression
+       {
+               $$ = new Value(make_shared<AExpression>(AELessThan, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), yylloc));
+               delete $1;
+               delete $3;
+       }
+       | aexpression '>' aexpression
+       {
+               $$ = new Value(make_shared<AExpression>(AEGreaterThan, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), yylloc));
+               delete $1;
+               delete $3;
+       }
        | aexpression T_EQUAL aexpression
        {
                $$ = new Value(make_shared<AExpression>(AEEqual, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), yylloc));
index 298b597e867492fccf6c9fe9dfd7d1bdde294cc3..71a143691e8534ac262b05abaad3ec0252fe149f 100644 (file)
 #include "methods/utilityfuncs.h"
 #include "base/scriptfunction.h"
 #include "base/utility.h"
+#include "base/convert.h"
+#include "base/array.h"
+#include "base/dictionary.h"
 #include <boost/regex.hpp>
 
 using namespace icinga;
 
 REGISTER_SCRIPTFUNCTION(regex, &UtilityFuncs::Regex);
 REGISTER_SCRIPTFUNCTION(match, &Utility::Match);
+REGISTER_SCRIPTFUNCTION(len, &UtilityFuncs::Len);
 
 bool UtilityFuncs::Regex(const String& pattern, const String& text)
 {
@@ -33,3 +37,16 @@ bool UtilityFuncs::Regex(const String& pattern, const String& text)
        boost::smatch what;
        return boost::regex_search(text.GetData(), what, expr);
 }
+
+int UtilityFuncs::Len(const Value& value)
+{
+       if (value.IsObjectType<Dictionary>()) {
+               Dictionary::Ptr dict = value;
+               return dict->GetLength();
+       } else if (value.IsObjectType<Array>()) {
+               Array::Ptr array = value;
+               return array->GetLength();
+       } else {
+               return Convert::ToString(value).GetLength();
+       }
+}
\ No newline at end of file
index 7612672be1407c8de43bf77b7bed81de30a9b740..cdaa51da4e5f52815b821b8ea1973ca395b0e5ab 100644 (file)
@@ -33,6 +33,7 @@ class I2_METHODS_API UtilityFuncs
 {
 public:
        static bool Regex(const String& pattern, const String& text);
+       static int Len(const Value& value);
 
 private:
        UtilityFuncs(void);