fix compilation warnings
[asterisk-bristuff.git] / funcs / func_math.c
blobcefc94d934357481e8aeb3dad215a2cfe4c70d9e
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2004 - 2006, Andy Powell
6 * Updated by Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Math related dialplan function
23 * \author Andy Powell
24 * \author Mark Spencer <markster@digium.com>
27 #include "asterisk.h"
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <sys/types.h>
36 #include "asterisk/module.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/app.h"
42 #include "asterisk/config.h"
44 enum TypeOfFunctions {
45 ADDFUNCTION,
46 DIVIDEFUNCTION,
47 MULTIPLYFUNCTION,
48 SUBTRACTFUNCTION,
49 MODULUSFUNCTION,
50 GTFUNCTION,
51 LTFUNCTION,
52 GTEFUNCTION,
53 LTEFUNCTION,
54 EQFUNCTION
57 enum TypeOfResult {
58 FLOAT_RESULT,
59 INT_RESULT,
60 HEX_RESULT,
61 CHAR_RESULT
64 static int math(struct ast_channel *chan, char *cmd, char *parse,
65 char *buf, size_t len)
67 double fnum1;
68 double fnum2;
69 double ftmp = 0;
70 char *op;
71 int iaction = -1;
72 int type_of_result = FLOAT_RESULT;
73 char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
74 int negvalue1 = 0;
75 AST_DECLARE_APP_ARGS(args,
76 AST_APP_ARG(argv0);
77 AST_APP_ARG(argv1);
80 if (ast_strlen_zero(parse)) {
81 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
82 return -1;
85 AST_STANDARD_APP_ARGS(args, parse);
87 if (args.argc < 1) {
88 ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
89 return -1;
92 mvalue1 = args.argv0;
94 if (mvalue1[0] == '-') {
95 negvalue1 = 1;
96 mvalue1++;
99 if ((op = strchr(mvalue1, '*'))) {
100 iaction = MULTIPLYFUNCTION;
101 *op = '\0';
102 } else if ((op = strchr(mvalue1, '/'))) {
103 iaction = DIVIDEFUNCTION;
104 *op = '\0';
105 } else if ((op = strchr(mvalue1, '%'))) {
106 iaction = MODULUSFUNCTION;
107 *op = '\0';
108 } else if ((op = strchr(mvalue1, '>'))) {
109 iaction = GTFUNCTION;
110 *op = '\0';
111 if (*(op + 1) == '=') {
112 *++op = '\0';
113 iaction = GTEFUNCTION;
115 } else if ((op = strchr(mvalue1, '<'))) {
116 iaction = LTFUNCTION;
117 *op = '\0';
118 if (*(op + 1) == '=') {
119 *++op = '\0';
120 iaction = LTEFUNCTION;
122 } else if ((op = strchr(mvalue1, '='))) {
123 *op = '\0';
124 if (*(op + 1) == '=') {
125 *++op = '\0';
126 iaction = EQFUNCTION;
127 } else
128 op = NULL;
129 } else if ((op = strchr(mvalue1, '+'))) {
130 iaction = ADDFUNCTION;
131 *op = '\0';
132 } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative first number */
133 iaction = SUBTRACTFUNCTION;
134 *op = '\0';
137 if (op)
138 mvalue2 = op + 1;
140 /* detect wanted type of result */
141 mtype_of_result = args.argv1;
142 if (mtype_of_result) {
143 if (!strcasecmp(mtype_of_result, "float")
144 || !strcasecmp(mtype_of_result, "f"))
145 type_of_result = FLOAT_RESULT;
146 else if (!strcasecmp(mtype_of_result, "int")
147 || !strcasecmp(mtype_of_result, "i"))
148 type_of_result = INT_RESULT;
149 else if (!strcasecmp(mtype_of_result, "hex")
150 || !strcasecmp(mtype_of_result, "h"))
151 type_of_result = HEX_RESULT;
152 else if (!strcasecmp(mtype_of_result, "char")
153 || !strcasecmp(mtype_of_result, "c"))
154 type_of_result = CHAR_RESULT;
155 else {
156 ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
157 mtype_of_result);
158 return -1;
162 if (!mvalue1 || !mvalue2) {
163 ast_log(LOG_WARNING,
164 "Supply all the parameters - just this once, please\n");
165 return -1;
168 if (sscanf(mvalue1, "%lf", &fnum1) != 1) {
169 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
170 return -1;
173 if (sscanf(mvalue2, "%lf", &fnum2) != 1) {
174 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
175 return -1;
178 if (negvalue1)
179 fnum1 = 0 - fnum1;
181 switch (iaction) {
182 case ADDFUNCTION:
183 ftmp = fnum1 + fnum2;
184 break;
185 case DIVIDEFUNCTION:
186 if (fnum2 <= 0)
187 ftmp = 0; /* can't do a divide by 0 */
188 else
189 ftmp = (fnum1 / fnum2);
190 break;
191 case MULTIPLYFUNCTION:
192 ftmp = (fnum1 * fnum2);
193 break;
194 case SUBTRACTFUNCTION:
195 ftmp = (fnum1 - fnum2);
196 break;
197 case MODULUSFUNCTION:
199 int inum1 = fnum1;
200 int inum2 = fnum2;
202 ftmp = (inum1 % inum2);
204 break;
206 case GTFUNCTION:
207 ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
208 break;
209 case LTFUNCTION:
210 ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
211 break;
212 case GTEFUNCTION:
213 ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
214 break;
215 case LTEFUNCTION:
216 ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
217 break;
218 case EQFUNCTION:
219 ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
220 break;
221 default:
222 ast_log(LOG_WARNING,
223 "Something happened that neither of us should be proud of %d\n",
224 iaction);
225 return -1;
228 if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
229 if (type_of_result == FLOAT_RESULT)
230 snprintf(buf, len, "%f", ftmp);
231 else if (type_of_result == INT_RESULT)
232 snprintf(buf, len, "%i", (int) ftmp);
233 else if (type_of_result == HEX_RESULT)
234 snprintf(buf, len, "%x", (unsigned int) ftmp);
235 else if (type_of_result == CHAR_RESULT)
236 snprintf(buf, len, "%c", (unsigned char) ftmp);
239 return 0;
242 static struct ast_custom_function math_function = {
243 .name = "MATH",
244 .synopsis = "Performs Mathematical Functions",
245 .syntax = "MATH(<number1><op><number 2>[,<type_of_result>])",
246 .desc = "Perform calculation on number 1 to number 2. Valid ops are: \n"
247 " +,-,/,*,%,<,>,>=,<=,==\n"
248 "and behave as their C equivalents.\n"
249 "<type_of_result> - wanted type of result:\n"
250 " f, float - float(default)\n"
251 " i, int - integer,\n"
252 " h, hex - hex,\n"
253 " c, char - char\n"
254 "Example: Set(i=${MATH(123%16,int)}) - sets var i=11",
255 .read = math
258 static int unload_module(void)
260 return ast_custom_function_unregister(&math_function);
263 static int load_module(void)
265 return ast_custom_function_register(&math_function);
268 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");