finish const-ifying ast_func_read()
[asterisk/asterisk.git] / funcs / func_math.c
index 7ad4765..4dbfac8 100644 (file)
  * \author Mark Spencer <markster@digium.com>
  */
 
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <math.h>
 #include <sys/types.h>
 
-#include "asterisk.h"
-
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include "asterisk/module.h"
 #include "asterisk/channel.h"
 #include "asterisk/pbx.h"
@@ -47,6 +48,9 @@ enum TypeOfFunctions {
        MULTIPLYFUNCTION,
        SUBTRACTFUNCTION,
        MODULUSFUNCTION,
+       POWFUNCTION,
+       SHLEFTFUNCTION,
+       SHRIGHTFUNCTION,
        GTFUNCTION,
        LTFUNCTION,
        GTEFUNCTION,
@@ -61,7 +65,7 @@ enum TypeOfResult {
        CHAR_RESULT
 };
 
-static int math(struct ast_channel *chan, char *cmd, char *parse,
+static int math(struct ast_channel *chan, const char *cmd, char *parse,
                char *buf, size_t len)
 {
        float fnum1;
@@ -105,12 +109,18 @@ static int math(struct ast_channel *chan, char *cmd, char *parse,
        } else if ((op = strchr(mvalue1, '%'))) {
                iaction = MODULUSFUNCTION;
                *op = '\0';
+       } else if ((op = strchr(mvalue1, '^'))) {
+               iaction = POWFUNCTION;
+               *op = '\0';
        } else if ((op = strchr(mvalue1, '>'))) {
                iaction = GTFUNCTION;
                *op = '\0';
                if (*(op + 1) == '=') {
                        *++op = '\0';
                        iaction = GTEFUNCTION;
+               } else if (*(op + 1) == '>') {
+                       *++op = '\0';
+                       iaction = SHRIGHTFUNCTION;
                }
        } else if ((op = strchr(mvalue1, '<'))) {
                iaction = LTFUNCTION;
@@ -118,9 +128,11 @@ static int math(struct ast_channel *chan, char *cmd, char *parse,
                if (*(op + 1) == '=') {
                        *++op = '\0';
                        iaction = LTEFUNCTION;
+               } else if (*(op + 1) == '<') {
+                       *++op = '\0';
+                       iaction = SHLEFTFUNCTION;
                }
        } else if ((op = strchr(mvalue1, '='))) {
-               iaction = GTFUNCTION;
                *op = '\0';
                if (*(op + 1) == '=') {
                        *++op = '\0';
@@ -195,6 +207,25 @@ static int math(struct ast_channel *chan, char *cmd, char *parse,
 
                        break;
                }
+       case POWFUNCTION:
+               ftmp = pow(fnum1, fnum2);
+               break;
+       case SHLEFTFUNCTION:
+               {
+                       int inum1 = fnum1;
+                       int inum2 = fnum2;
+
+                       ftmp = (inum1 << inum2);
+                       break;
+               }
+       case SHRIGHTFUNCTION:
+               {
+                       int inum1 = fnum1;
+                       int inum2 = fnum2;
+
+                       ftmp = (inum1 >> inum2);
+                       break;
+               }
        case GTFUNCTION:
                ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
                break;
@@ -234,9 +265,9 @@ static int math(struct ast_channel *chan, char *cmd, char *parse,
 static struct ast_custom_function math_function = {
        .name = "MATH",
        .synopsis = "Performs Mathematical Functions",
-       .syntax = "MATH(<number1><op><number 2>[,<type_of_result>])",
-       .desc = "Perform calculation on number 1 to number 2. Valid ops are: \n"
-               "    +,-,/,*,%,<,>,>=,<=,==\n"
+       .syntax = "MATH(<number1><op><number2>[,<type_of_result>])",
+       .desc = "Perform calculation on number1 to number2. Valid ops are: \n"
+               "    +,-,/,*,%,<<,>>,^,<,>,>=,<=,==\n"
                "and behave as their C equivalents.\n"
                "<type_of_result> - wanted type of result:\n"
                "       f, float - float(default)\n"
@@ -247,29 +278,14 @@ static struct ast_custom_function math_function = {
        .read = math
 };
 
-static char *tdesc = "Mathematical dialplan function";
-
-int unload_module(void)
+static int unload_module(void)
 {
        return ast_custom_function_unregister(&math_function);
 }
 
-int load_module(void)
+static int load_module(void)
 {
        return ast_custom_function_register(&math_function);
 }
 
-char *description(void)
-{
-       return tdesc;
-}
-
-int usecount(void)
-{
-       return 0;
-}
-
-char *key()
-{
-       return ASTERISK_GPL_KEY;
-}
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");