--- app_math.c.orig 2005-06-06 11:05:51.000000000 +0400 +++ app_math.c 2005-06-06 13:38:24.000000000 +0400 @@ -47,11 +47,17 @@ static char *math_synopsis = "Performs Mathematical Functions"; static char *math_descrip = -"Math(returnvar,\n\n" +"Math(returnvar,,[])\n\n" "Perform floating point calculation on number 1 to number 2 and \n" "store the result in returnvar. Valid ops are: \n" " +,-,/,*,%,<,>,>=,<=,==\n" -"and behave as their C equivalents. Always returns 0.\n"; +"and behave as their C equivalents. Always returns 0.\n" +" - wanted type of result:\n" +" f, float - float(default)\n" +" i, int - integer,\n" +" h, hex - hex,\n" +" c, char - char\n" +"Example: Math(i,123%16,int)\n"; #define ADDFUNCTION 0 #define DIVIDEFUNCTION 1 @@ -65,6 +71,14 @@ #define LTEFUNCTION 8 #define EQFUNCTION 9 +enum TypeOfResult +{ + FLOAT_RESULT, + INT_RESULT, + HEX_RESULT, + CHAR_RESULT +}; + STANDARD_LOCAL_USER; LOCAL_USER_DECL; @@ -76,12 +90,13 @@ float ftmp = 0; char *op; int iaction=-1; + int type_of_result=FLOAT_RESULT; /* dunno, big calulations :D */ char user_result[30]; char *s; - char *mvar, *mvalue1, *mvalue2=NULL; + char *mvar, *mvalue1, *mvalue2=NULL, *mtype_of_result; struct localuser *u; @@ -109,6 +124,9 @@ } else if ((op = strchr(mvalue1, '/'))) { iaction = DIVIDEFUNCTION; *op = '\0'; + } else if ((op = strchr(mvalue1, '%'))) { + iaction = MODULUSFUNCTION; + *op = '\0'; } else if ((op = strchr(mvalue1, '>'))) { iaction = GTFUNCTION; *op = '\0'; @@ -138,6 +156,29 @@ if (op) mvalue2 = op + 1; + + /* detect wanted type of result */ + mtype_of_result = strsep(&s, "|"); + if (mtype_of_result) + { + if (strcmp(mtype_of_result,"float")==0 || strcmp(mtype_of_result,"f")==0) + type_of_result=FLOAT_RESULT; + else + if (strcmp(mtype_of_result,"int")==0 || strcmp(mtype_of_result,"i")==0) + type_of_result=INT_RESULT; + else + if (strcmp(mtype_of_result,"hex")==0 || strcmp(mtype_of_result,"h")==0) + type_of_result=HEX_RESULT; + else + if (strcmp(mtype_of_result,"char")==0 || strcmp(mtype_of_result,"c")==0) + type_of_result=CHAR_RESULT; + else + { + ast_log(LOG_WARNING, "Unknown type of result '%s'.\n", mtype_of_result); + LOCAL_USER_REMOVE(u); + return -1; + } + } if (!mvar || !mvalue1 || !mvalue2) { ast_log(LOG_WARNING, "Supply all the parameters - just this once, please\n"); @@ -224,7 +265,19 @@ } if (iaction < GTFUNCTION || iaction > EQFUNCTION) + { + if (type_of_result==FLOAT_RESULT) snprintf(user_result,sizeof(user_result),"%f",ftmp); + else + if (type_of_result==INT_RESULT) + snprintf(user_result,sizeof(user_result),"%i",(int)ftmp); + else + if (type_of_result==HEX_RESULT) + snprintf(user_result,sizeof(user_result),"%x",(unsigned int)ftmp); + else + if (type_of_result==CHAR_RESULT) + snprintf(user_result,sizeof(user_result),"%c",(unsigned char)ftmp); + } pbx_builtin_setvar_helper(chan, mvar, user_result);