Fri Jul 3 20:35:38 2009

Asterisk developer's documentation


func_math.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2004 - 2006, Andy Powell 
00005  *
00006  * Updated by Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Math related dialplan function
00022  *
00023  * \author Andy Powell
00024  * \author Mark Spencer <markster@digium.com>
00025  */
00026 
00027 #include "asterisk.h"
00028 
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00030 
00031 #include <stdlib.h>
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <math.h>
00035 #include <sys/types.h>
00036 
00037 #include "asterisk/module.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/logger.h"
00041 #include "asterisk/utils.h"
00042 #include "asterisk/app.h"
00043 #include "asterisk/config.h"
00044 
00045 enum TypeOfFunctions {
00046    ADDFUNCTION,
00047    DIVIDEFUNCTION,
00048    MULTIPLYFUNCTION,
00049    SUBTRACTFUNCTION,
00050    MODULUSFUNCTION,
00051    POWFUNCTION,
00052    SHLEFTFUNCTION,
00053    SHRIGHTFUNCTION,
00054    GTFUNCTION,
00055    LTFUNCTION,
00056    GTEFUNCTION,
00057    LTEFUNCTION,
00058    EQFUNCTION
00059 };
00060 
00061 enum TypeOfResult {
00062    FLOAT_RESULT,
00063    INT_RESULT,
00064    HEX_RESULT,
00065    CHAR_RESULT
00066 };
00067 
00068 static int math(struct ast_channel *chan, char *cmd, char *parse,
00069       char *buf, size_t len)
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 = strchr(mvalue1, '>'))) {
00116       iaction = GTFUNCTION;
00117       *op = '\0';
00118       if (*(op + 1) == '=') {
00119          *++op = '\0';
00120          iaction = GTEFUNCTION;
00121       } else if (*(op + 1) == '>') {
00122          *++op = '\0';
00123          iaction = SHRIGHTFUNCTION;
00124       }
00125    } else if ((op = strchr(mvalue1, '<'))) {
00126       iaction = LTFUNCTION;
00127       *op = '\0';
00128       if (*(op + 1) == '=') {
00129          *++op = '\0';
00130          iaction = LTEFUNCTION;
00131       } else if (*(op + 1) == '<') {
00132          *++op = '\0';
00133          iaction = SHLEFTFUNCTION;
00134       }
00135    } else if ((op = strchr(mvalue1, '='))) {
00136       *op = '\0';
00137       if (*(op + 1) == '=') {
00138          *++op = '\0';
00139          iaction = EQFUNCTION;
00140       } else
00141          op = NULL;
00142    } else if ((op = strchr(mvalue1, '+'))) {
00143       iaction = ADDFUNCTION;
00144       *op = '\0';
00145    } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative first number */
00146       iaction = SUBTRACTFUNCTION;
00147       *op = '\0';
00148    }
00149 
00150    if (op)
00151       mvalue2 = op + 1;
00152 
00153    /* detect wanted type of result */
00154    mtype_of_result = args.argv1;
00155    if (mtype_of_result) {
00156       if (!strcasecmp(mtype_of_result, "float")
00157           || !strcasecmp(mtype_of_result, "f"))
00158          type_of_result = FLOAT_RESULT;
00159       else if (!strcasecmp(mtype_of_result, "int")
00160           || !strcasecmp(mtype_of_result, "i"))
00161          type_of_result = INT_RESULT;
00162       else if (!strcasecmp(mtype_of_result, "hex")
00163           || !strcasecmp(mtype_of_result, "h"))
00164          type_of_result = HEX_RESULT;
00165       else if (!strcasecmp(mtype_of_result, "char")
00166           || !strcasecmp(mtype_of_result, "c"))
00167          type_of_result = CHAR_RESULT;
00168       else {
00169          ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
00170                mtype_of_result);
00171          return -1;
00172       }
00173    }
00174 
00175    if (!mvalue1 || !mvalue2) {
00176       ast_log(LOG_WARNING,
00177             "Supply all the parameters - just this once, please\n");
00178       return -1;
00179    }
00180 
00181    if (sscanf(mvalue1, "%lf", &fnum1) != 1) {
00182       ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
00183       return -1;
00184    }
00185 
00186    if (sscanf(mvalue2, "%lf", &fnum2) != 1) {
00187       ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
00188       return -1;
00189    }
00190 
00191    if (negvalue1)
00192       fnum1 = 0 - fnum1;
00193 
00194    switch (iaction) {
00195    case ADDFUNCTION:
00196       ftmp = fnum1 + fnum2;
00197       break;
00198    case DIVIDEFUNCTION:
00199       if (fnum2 <= 0)
00200          ftmp = 0;         /* can't do a divide by 0 */
00201       else
00202          ftmp = (fnum1 / fnum2);
00203       break;
00204    case MULTIPLYFUNCTION:
00205       ftmp = (fnum1 * fnum2);
00206       break;
00207    case SUBTRACTFUNCTION:
00208       ftmp = (fnum1 - fnum2);
00209       break;
00210    case MODULUSFUNCTION:
00211       {
00212          int inum1 = fnum1;
00213          int inum2 = fnum2;
00214 
00215          ftmp = (inum1 % inum2);
00216 
00217          break;
00218       }
00219    case POWFUNCTION:
00220       ftmp = pow(fnum1, fnum2);
00221       break;
00222    case SHLEFTFUNCTION:
00223       {
00224          int inum1 = fnum1;
00225          int inum2 = fnum2;
00226 
00227          ftmp = (inum1 << inum2);
00228          break;
00229       }
00230    case SHRIGHTFUNCTION:
00231       {
00232          int inum1 = fnum1;
00233          int inum2 = fnum2;
00234 
00235          ftmp = (inum1 >> inum2);
00236          break;
00237       }
00238    case GTFUNCTION:
00239       ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
00240       break;
00241    case LTFUNCTION:
00242       ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
00243       break;
00244    case GTEFUNCTION:
00245       ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
00246       break;
00247    case LTEFUNCTION:
00248       ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
00249       break;
00250    case EQFUNCTION:
00251       ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
00252       break;
00253    default:
00254       ast_log(LOG_WARNING,
00255             "Something happened that neither of us should be proud of %d\n",
00256             iaction);
00257       return -1;
00258    }
00259 
00260    if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
00261       if (type_of_result == FLOAT_RESULT)
00262          snprintf(buf, len, "%f", ftmp);
00263       else if (type_of_result == INT_RESULT)
00264          snprintf(buf, len, "%i", (int) ftmp);
00265       else if (type_of_result == HEX_RESULT)
00266          snprintf(buf, len, "%x", (unsigned int) ftmp);
00267       else if (type_of_result == CHAR_RESULT)
00268          snprintf(buf, len, "%c", (unsigned char) ftmp);
00269    }
00270 
00271    return 0;
00272 }
00273 
00274 static struct ast_custom_function math_function = {
00275    .name = "MATH",
00276    .synopsis = "Performs Mathematical Functions",
00277    .syntax = "MATH(<number1><op><number2>[,<type_of_result>])",
00278    .desc = "Perform calculation on number1 to number2. Valid ops are: \n"
00279       "    +,-,/,*,%,<<,>>,^,<,>,>=,<=,==\n"
00280       "and behave as their C equivalents.\n"
00281       "<type_of_result> - wanted type of result:\n"
00282       "  f, float - float(default)\n"
00283       "  i, int - integer,\n"
00284       "  h, hex - hex,\n"
00285       "  c, char - char\n"
00286       "Example: Set(i=${MATH(123%16,int)}) - sets var i=11",
00287    .read = math
00288 };
00289 
00290 static int unload_module(void)
00291 {
00292    return ast_custom_function_unregister(&math_function);
00293 }
00294 
00295 static int load_module(void)
00296 {
00297    return ast_custom_function_register(&math_function);
00298 }
00299 
00300 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");

Generated on Fri Jul 3 20:35:38 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9