Sat Aug 6 00:39:56 2011

Asterisk developer's documentation


func_math.c File Reference

Math related dialplan function. More...

#include "asterisk.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.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, 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, 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 | AST_MODFLAG_BUILDSUM, .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 = "361d7bb937402d51e4658efb5b4d76e4" , .load = load_module, .unload = unload_module, }
static const 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 
GTFUNCTION 
LTFUNCTION 
GTEFUNCTION 
LTEFUNCTION 
EQFUNCTION 

Definition at line 44 of file func_math.c.

00044                      {
00045    ADDFUNCTION,
00046    DIVIDEFUNCTION,
00047    MULTIPLYFUNCTION,
00048    SUBTRACTFUNCTION,
00049    MODULUSFUNCTION,
00050    GTFUNCTION,
00051    LTFUNCTION,
00052    GTEFUNCTION,
00053    LTEFUNCTION,
00054    EQFUNCTION
00055 };

enum TypeOfResult

Enumerator:
FLOAT_RESULT 
INT_RESULT 
HEX_RESULT 
CHAR_RESULT 

Definition at line 57 of file func_math.c.

00057                   {
00058    FLOAT_RESULT,
00059    INT_RESULT,
00060    HEX_RESULT,
00061    CHAR_RESULT
00062 };


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 272 of file func_math.c.

static void __unreg_module ( void   )  [static]

Definition at line 272 of file func_math.c.

static int load_module ( void   )  [static]

Definition at line 267 of file func_math.c.

References ast_custom_function_register(), and math_function.

00268 {
00269    return ast_custom_function_register(&math_function);
00270 }

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

Definition at line 64 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(), DIVIDEFUNCTION, FLOAT_RESULT, GTEFUNCTION, GTFUNCTION, HEX_RESULT, INT_RESULT, LOG_WARNING, LTEFUNCTION, LTFUNCTION, MODULUSFUNCTION, MULTIPLYFUNCTION, and SUBTRACTFUNCTION.

00066 {
00067    double fnum1;
00068    double fnum2;
00069    double ftmp = 0;
00070    char *op;
00071    int iaction = -1;
00072    int type_of_result = FLOAT_RESULT;
00073    char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
00074    int negvalue1 = 0;
00075    AST_DECLARE_APP_ARGS(args,
00076               AST_APP_ARG(argv0);
00077               AST_APP_ARG(argv1);
00078    );
00079 
00080    if (ast_strlen_zero(parse)) {
00081       ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
00082       return -1;
00083    }
00084 
00085    AST_STANDARD_APP_ARGS(args, parse);
00086 
00087    if (args.argc < 1) {
00088       ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
00089       return -1;
00090    }
00091 
00092    mvalue1 = args.argv0;
00093 
00094    if (mvalue1[0] == '-') {
00095       negvalue1 = 1;
00096       mvalue1++;
00097    }
00098 
00099    if ((op = strchr(mvalue1, '*'))) {
00100       iaction = MULTIPLYFUNCTION;
00101       *op = '\0';
00102    } else if ((op = strchr(mvalue1, '/'))) {
00103       iaction = DIVIDEFUNCTION;
00104       *op = '\0';
00105    } else if ((op = strchr(mvalue1, '%'))) {
00106       iaction = MODULUSFUNCTION;
00107       *op = '\0';
00108    } else if ((op = strchr(mvalue1, '>'))) {
00109       iaction = GTFUNCTION;
00110       *op = '\0';
00111       if (*(op + 1) == '=') {
00112          *++op = '\0';
00113          iaction = GTEFUNCTION;
00114       }
00115    } else if ((op = strchr(mvalue1, '<'))) {
00116       iaction = LTFUNCTION;
00117       *op = '\0';
00118       if (*(op + 1) == '=') {
00119          *++op = '\0';
00120          iaction = LTEFUNCTION;
00121       }
00122    } else if ((op = strchr(mvalue1, '='))) {
00123       *op = '\0';
00124       if (*(op + 1) == '=') {
00125          *++op = '\0';
00126          iaction = EQFUNCTION;
00127       } else
00128          op = NULL;
00129    } else if ((op = strchr(mvalue1, '+'))) {
00130       iaction = ADDFUNCTION;
00131       *op = '\0';
00132    } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative first number */
00133       iaction = SUBTRACTFUNCTION;
00134       *op = '\0';
00135    }
00136 
00137    if (op)
00138       mvalue2 = op + 1;
00139 
00140    /* detect wanted type of result */
00141    mtype_of_result = args.argv1;
00142    if (mtype_of_result) {
00143       if (!strcasecmp(mtype_of_result, "float")
00144           || !strcasecmp(mtype_of_result, "f"))
00145          type_of_result = FLOAT_RESULT;
00146       else if (!strcasecmp(mtype_of_result, "int")
00147           || !strcasecmp(mtype_of_result, "i"))
00148          type_of_result = INT_RESULT;
00149       else if (!strcasecmp(mtype_of_result, "hex")
00150           || !strcasecmp(mtype_of_result, "h"))
00151          type_of_result = HEX_RESULT;
00152       else if (!strcasecmp(mtype_of_result, "char")
00153           || !strcasecmp(mtype_of_result, "c"))
00154          type_of_result = CHAR_RESULT;
00155       else {
00156          ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
00157                mtype_of_result);
00158          return -1;
00159       }
00160    }
00161 
00162    if (!mvalue1 || !mvalue2) {
00163       ast_log(LOG_WARNING,
00164             "Supply all the parameters - just this once, please\n");
00165       return -1;
00166    }
00167 
00168    if (sscanf(mvalue1, "%30lf", &fnum1) != 1) {
00169       ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
00170       return -1;
00171    }
00172 
00173    if (sscanf(mvalue2, "%30lf", &fnum2) != 1) {
00174       ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
00175       return -1;
00176    }
00177 
00178    if (negvalue1)
00179       fnum1 = 0 - fnum1;
00180 
00181    switch (iaction) {
00182    case ADDFUNCTION:
00183       ftmp = fnum1 + fnum2;
00184       break;
00185    case DIVIDEFUNCTION:
00186       if (fnum2 <= 0)
00187          ftmp = 0;         /* can't do a divide by 0 */
00188       else
00189          ftmp = (fnum1 / fnum2);
00190       break;
00191    case MULTIPLYFUNCTION:
00192       ftmp = (fnum1 * fnum2);
00193       break;
00194    case SUBTRACTFUNCTION:
00195       ftmp = (fnum1 - fnum2);
00196       break;
00197    case MODULUSFUNCTION:
00198       {
00199          int inum1 = fnum1;
00200          int inum2 = fnum2;
00201 
00202          if (inum2 == 0) {
00203             ftmp = 0;
00204          } else {
00205             ftmp = (inum1 % inum2);
00206          }
00207 
00208          break;
00209       }
00210    case GTFUNCTION:
00211       ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
00212       break;
00213    case LTFUNCTION:
00214       ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
00215       break;
00216    case GTEFUNCTION:
00217       ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
00218       break;
00219    case LTEFUNCTION:
00220       ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
00221       break;
00222    case EQFUNCTION:
00223       ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
00224       break;
00225    default:
00226       ast_log(LOG_WARNING,
00227             "Something happened that neither of us should be proud of %d\n",
00228             iaction);
00229       return -1;
00230    }
00231 
00232    if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
00233       if (type_of_result == FLOAT_RESULT)
00234          snprintf(buf, len, "%f", ftmp);
00235       else if (type_of_result == INT_RESULT)
00236          snprintf(buf, len, "%i", (int) ftmp);
00237       else if (type_of_result == HEX_RESULT)
00238          snprintf(buf, len, "%x", (unsigned int) ftmp);
00239       else if (type_of_result == CHAR_RESULT)
00240          snprintf(buf, len, "%c", (unsigned char) ftmp);
00241    }
00242 
00243    return 0;
00244 }

static int unload_module ( void   )  [static]

Definition at line 262 of file func_math.c.

References ast_custom_function_unregister(), and math_function.

00263 {
00264    return ast_custom_function_unregister(&math_function);
00265 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .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 = "361d7bb937402d51e4658efb5b4d76e4" , .load = load_module, .unload = unload_module, } [static]

Definition at line 272 of file func_math.c.

const struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 272 of file func_math.c.

struct ast_custom_function math_function [static]

Definition at line 246 of file func_math.c.

Referenced by load_module(), and unload_module().


Generated on Sat Aug 6 00:39:56 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7