Wed Aug 18 22:34:24 2010

Asterisk developer's documentation


func_math.c File Reference

Math related dialplan function. More...

#include "asterisk.h"
#include <math.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/config.h"

Go to the source code of this file.

Enumerations

enum  TypeOfFunctions {
  ADDFUNCTION, DIVIDEFUNCTION, MULTIPLYFUNCTION, SUBTRACTFUNCTION,
  MODULUSFUNCTION, POWFUNCTION, SHLEFTFUNCTION, SHRIGHTFUNCTION,
  BITWISEANDFUNCTION, BITWISEXORFUNCTION, BITWISEORFUNCTION, GTFUNCTION,
  LTFUNCTION, GTEFUNCTION, LTEFUNCTION, EQFUNCTION
}
enum  TypeOfResult { FLOAT_RESULT, INT_RESULT, HEX_RESULT, CHAR_RESULT }

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int load_module (void)
static int math (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Mathematical dialplan function" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_custom_function math_function


Detailed Description

Math related dialplan function.

Author:
Andy Powell

Mark Spencer <markster@digium.com>

Definition in file func_math.c.


Enumeration Type Documentation

enum TypeOfFunctions

Enumerator:
ADDFUNCTION 
DIVIDEFUNCTION 
MULTIPLYFUNCTION 
SUBTRACTFUNCTION 
MODULUSFUNCTION 
POWFUNCTION 
SHLEFTFUNCTION 
SHRIGHTFUNCTION 
BITWISEANDFUNCTION 
BITWISEXORFUNCTION 
BITWISEORFUNCTION 
GTFUNCTION 
LTFUNCTION 
GTEFUNCTION 
LTEFUNCTION 
EQFUNCTION 

Definition at line 42 of file func_math.c.

enum TypeOfResult

Enumerator:
FLOAT_RESULT 
INT_RESULT 
HEX_RESULT 
CHAR_RESULT 

Definition at line 61 of file func_math.c.

00061                   {
00062    FLOAT_RESULT,
00063    INT_RESULT,
00064    HEX_RESULT,
00065    CHAR_RESULT
00066 };


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 337 of file func_math.c.

static void __unreg_module ( void   )  [static]

Definition at line 337 of file func_math.c.

static int load_module ( void   )  [static]

Definition at line 332 of file func_math.c.

References ast_custom_function_register, and math_function.

00333 {
00334    return ast_custom_function_register(&math_function);
00335 }

static int math ( struct ast_channel chan,
const char *  cmd,
char *  parse,
char *  buf,
size_t  len 
) [static]

Definition at line 68 of file func_math.c.

References ADDFUNCTION, AST_APP_ARG, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_log(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), BITWISEANDFUNCTION, BITWISEORFUNCTION, BITWISEXORFUNCTION, DIVIDEFUNCTION, FLOAT_RESULT, GTEFUNCTION, GTFUNCTION, HEX_RESULT, INT_RESULT, LOG_WARNING, LTEFUNCTION, LTFUNCTION, MODULUSFUNCTION, MULTIPLYFUNCTION, POWFUNCTION, SHLEFTFUNCTION, SHRIGHTFUNCTION, and SUBTRACTFUNCTION.

00070 {
00071    double fnum1;
00072    double fnum2;
00073    double ftmp = 0;
00074    char *op;
00075    int iaction = -1;
00076    int type_of_result = FLOAT_RESULT;
00077    char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
00078    int negvalue1 = 0;
00079    AST_DECLARE_APP_ARGS(args,
00080               AST_APP_ARG(argv0);
00081               AST_APP_ARG(argv1);
00082    );
00083 
00084    if (ast_strlen_zero(parse)) {
00085       ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
00086       return -1;
00087    }
00088 
00089    AST_STANDARD_APP_ARGS(args, parse);
00090 
00091    if (args.argc < 1) {
00092       ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
00093       return -1;
00094    }
00095 
00096    mvalue1 = args.argv0;
00097 
00098    if (mvalue1[0] == '-') {
00099       negvalue1 = 1;
00100       mvalue1++;
00101    }
00102 
00103    if ((op = strchr(mvalue1, '*'))) {
00104       iaction = MULTIPLYFUNCTION;
00105       *op = '\0';
00106    } else if ((op = strchr(mvalue1, '/'))) {
00107       iaction = DIVIDEFUNCTION;
00108       *op = '\0';
00109    } else if ((op = strchr(mvalue1, '%'))) {
00110       iaction = MODULUSFUNCTION;
00111       *op = '\0';
00112    } else if ((op = strchr(mvalue1, '^'))) {
00113       iaction = POWFUNCTION;
00114       *op = '\0';
00115    } else if ((op = strstr(mvalue1, "AND"))) {
00116       iaction = BITWISEANDFUNCTION;
00117       *op = '\0';
00118       op += 2;
00119    } else if ((op = strstr(mvalue1, "XOR"))) {
00120       iaction = BITWISEXORFUNCTION;
00121       *op = '\0';
00122       op += 2;
00123    } else if ((op = strstr(mvalue1, "OR"))) {
00124       iaction = BITWISEORFUNCTION;
00125       *op = '\0';
00126       ++op;
00127    } else if ((op = strchr(mvalue1, '>'))) {
00128       iaction = GTFUNCTION;
00129       *op = '\0';
00130       if (*(op + 1) == '=') {
00131          iaction = GTEFUNCTION;
00132          ++op;
00133       } else if (*(op + 1) == '>') {
00134          iaction = SHRIGHTFUNCTION;
00135          ++op;
00136       }
00137    } else if ((op = strchr(mvalue1, '<'))) {
00138       iaction = LTFUNCTION;
00139       *op = '\0';
00140       if (*(op + 1) == '=') {
00141          iaction = LTEFUNCTION;
00142          ++op;
00143       } else if (*(op + 1) == '<') {
00144          iaction = SHLEFTFUNCTION;
00145          ++op;
00146       }
00147    } else if ((op = strchr(mvalue1, '='))) {
00148       *op = '\0';
00149       if (*(op + 1) == '=') {
00150          iaction = EQFUNCTION;
00151          ++op;
00152       } else
00153          op = NULL;
00154    } else if ((op = strchr(mvalue1, '+'))) {
00155       iaction = ADDFUNCTION;
00156       *op = '\0';
00157    } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative second number */
00158       iaction = SUBTRACTFUNCTION;
00159       *op = '\0';
00160    }
00161 
00162    if (op)
00163       mvalue2 = op + 1;
00164 
00165    /* detect wanted type of result */
00166    mtype_of_result = args.argv1;
00167    if (mtype_of_result) {
00168       if (!strcasecmp(mtype_of_result, "float")
00169           || !strcasecmp(mtype_of_result, "f"))
00170          type_of_result = FLOAT_RESULT;
00171       else if (!strcasecmp(mtype_of_result, "int")
00172           || !strcasecmp(mtype_of_result, "i"))
00173          type_of_result = INT_RESULT;
00174       else if (!strcasecmp(mtype_of_result, "hex")
00175           || !strcasecmp(mtype_of_result, "h"))
00176          type_of_result = HEX_RESULT;
00177       else if (!strcasecmp(mtype_of_result, "char")
00178           || !strcasecmp(mtype_of_result, "c"))
00179          type_of_result = CHAR_RESULT;
00180       else {
00181          ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
00182                mtype_of_result);
00183          return -1;
00184       }
00185    }
00186 
00187    if (!mvalue1 || !mvalue2) {
00188       ast_log(LOG_WARNING,
00189             "Supply all the parameters - just this once, please\n");
00190       return -1;
00191    }
00192 
00193    if (sscanf(mvalue1, "%30lf", &fnum1) != 1) {
00194       ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
00195       return -1;
00196    }
00197 
00198    if (sscanf(mvalue2, "%30lf", &fnum2) != 1) {
00199       ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
00200       return -1;
00201    }
00202 
00203    if (negvalue1)
00204       fnum1 = 0 - fnum1;
00205 
00206    switch (iaction) {
00207    case ADDFUNCTION:
00208       ftmp = fnum1 + fnum2;
00209       break;
00210    case DIVIDEFUNCTION:
00211       if (fnum2 <= 0)
00212          ftmp = 0;         /* can't do a divide by 0 */
00213       else
00214          ftmp = (fnum1 / fnum2);
00215       break;
00216    case MULTIPLYFUNCTION:
00217       ftmp = (fnum1 * fnum2);
00218       break;
00219    case SUBTRACTFUNCTION:
00220       ftmp = (fnum1 - fnum2);
00221       break;
00222    case MODULUSFUNCTION:
00223       {
00224          int inum1 = fnum1;
00225          int inum2 = fnum2;
00226 
00227          if (inum2 == 0) {
00228             ftmp = 0;
00229          } else {
00230             ftmp = (inum1 % inum2);
00231          }
00232 
00233          break;
00234       }
00235    case POWFUNCTION:
00236       ftmp = pow(fnum1, fnum2);
00237       break;
00238    case SHLEFTFUNCTION:
00239       {
00240          int inum1 = fnum1;
00241          int inum2 = fnum2;
00242 
00243          ftmp = (inum1 << inum2);
00244          break;
00245       }
00246    case SHRIGHTFUNCTION:
00247       {
00248          int inum1 = fnum1;
00249          int inum2 = fnum2;
00250 
00251          ftmp = (inum1 >> inum2);
00252          break;
00253       }
00254    case BITWISEANDFUNCTION:
00255       {
00256          int inum1 = fnum1;
00257          int inum2 = fnum2;
00258          ftmp = (inum1 & inum2);
00259          break;
00260       }
00261    case BITWISEXORFUNCTION:
00262       {
00263          int inum1 = fnum1;
00264          int inum2 = fnum2;
00265          ftmp = (inum1 ^ inum2);
00266          break;
00267       }
00268    case BITWISEORFUNCTION:
00269       {
00270          int inum1 = fnum1;
00271          int inum2 = fnum2;
00272          ftmp = (inum1 | inum2);
00273          break;
00274       }
00275    case GTFUNCTION:
00276       ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
00277       break;
00278    case LTFUNCTION:
00279       ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
00280       break;
00281    case GTEFUNCTION:
00282       ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
00283       break;
00284    case LTEFUNCTION:
00285       ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
00286       break;
00287    case EQFUNCTION:
00288       ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
00289       break;
00290    default:
00291       ast_log(LOG_WARNING,
00292             "Something happened that neither of us should be proud of %d\n",
00293             iaction);
00294       return -1;
00295    }
00296 
00297    if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
00298       if (type_of_result == FLOAT_RESULT)
00299          snprintf(buf, len, "%f", ftmp);
00300       else if (type_of_result == INT_RESULT)
00301          snprintf(buf, len, "%i", (int) ftmp);
00302       else if (type_of_result == HEX_RESULT)
00303          snprintf(buf, len, "%x", (unsigned int) ftmp);
00304       else if (type_of_result == CHAR_RESULT)
00305          snprintf(buf, len, "%c", (unsigned char) ftmp);
00306    }
00307 
00308    return 0;
00309 }

static int unload_module ( void   )  [static]

Definition at line 327 of file func_math.c.

References ast_custom_function_unregister(), and math_function.

00328 {
00329    return ast_custom_function_unregister(&math_function);
00330 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Mathematical dialplan function" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, } [static]

Definition at line 337 of file func_math.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 337 of file func_math.c.

struct ast_custom_function math_function [static]

Definition at line 311 of file func_math.c.

Referenced by load_module(), and unload_module().


Generated on Wed Aug 18 22:34:24 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7