#include #include #include #include #include #include "functions.h" #include "common.h" #include "db.h" /* * Return 0 if arguments are equal (differs less than 0.000001), 1 - otherwise */ int cmp_double(double a,double b) { if(fabs(a-b)<0.000001) { return 0; } return 1; } /* * Return SUCCEED if parameter has format X.X or X, where X is [0..9]{1,n} * In other words, parameter is float number :) */ int is_float(char *c) { int i; int dot=-1; syslog(LOG_DEBUG, "Starting is_float:%s", c ); for(i=0;i='0')&&(c[i]<='9')) { continue; } if((c[i]=='.')&&(dot==-1)) { dot=i; if((dot!=0)&&(dot!=strlen(c)-1)) { continue; } } syslog(LOG_DEBUG, "It is NOT float" ); return FAIL; } syslog(LOG_DEBUG, "It is float" ); return SUCCEED; } /* * Delete all spaces from given string */ void delete_spaces(char *c) { int i,j; syslog( LOG_DEBUG, "Before deleting spaces:%s", c ); j=0; for(i=0;i */ int evaluate_simple (float *result,char *exp) { float value1,value2; char first[1024],second[1024]; int i,j,l; syslog( LOG_DEBUG, "Evaluating simple expression [%s]", exp ); if( is_float(exp) == SUCCEED ) { *result=atof(exp); return SUCCEED; } if( find_char(exp,'|') != FAIL ) { syslog( LOG_DEBUG, "| is found" ); l=find_char(exp,'|'); strcpy( first, exp ); first[l]=0; j=0; for(i=l+1;i') != FAIL ) { syslog(LOG_DEBUG, "> is found" ); l=find_char(exp,'>'); strcpy(first, exp); first[l]=0; j=0; for(i=l+1;i value2 ) { *result=1; } else { *result=0; } return SUCCEED; } else if( find_char(exp,'<') != FAIL ) { syslog(LOG_DEBUG, "< is found" ); l=find_char(exp,'<'); strcpy(first, exp); first[l]=0; j=0; for(i=l+1;i10)|({123}=1) */ int evaluate(int *result,char *exp) { float value; char res[1024]; char simple[1024]; int i,l,r; strcpy( res,exp ); while( find_char( exp, ')' ) != FAIL ) { l=-1; r=find_char(exp,')'); for(i=r;i>=0;i--) { if( exp[i] == '(' ) { l=i; break; } } if( r == -1 ) { syslog(LOG_WARNING, "Cannot find left bracket [(]. Expression:%s", exp ); return FAIL; } for(i=l+1;i=l+3) && (i<=r) ) continue; exp[j]=exp[i]; if(i==l) exp[j]='%'; if(i==l+1) exp[j]='f'; j++; } exp[j]=0; syslog( LOG_DEBUG, "Expression3:%s", exp ); sprintf(res,exp,value); strcpy(exp,res); // delete_spaces(exp); syslog( LOG_DEBUG, "Expression4:%s", exp ); } syslog( LOG_DEBUG, "Result expression:%s", exp ); return SUCCEED; } /* * Translate "({15}>10)|({123}=0)" to "(6.456>10)|(0=0)" */ int substitute_functions(char *exp) { float value; char functionid[1024]; char res[1024]; int i,l,r; syslog(LOG_DEBUG, "BEGIN substitute_functions" ); while( find_char(exp,'{') != FAIL ) { l=find_char(exp,'{'); r=find_char(exp,'}'); if( r == FAIL ) { syslog( LOG_WARNING, "Cannot find right bracket. Expression:%s", exp ); return FAIL; } if( r < l ) { syslog( LOG_WARNING, "Right bracket is before left one. Expression:%s", exp ); return FAIL; } for(i=l+1;i1)|({127.0.0.1:system[procload].max(300)}>3) */ int evaluate_expression (int *result,char *expression) { delete_spaces(expression); if( substitute_functions(expression) == SUCCEED) { if( evaluate(result, expression) == SUCCEED) { return SUCCEED; } } return FAIL; }