diff --git a/apps/dsm/DSMCoreModule.cpp b/apps/dsm/DSMCoreModule.cpp index a6e3353b..9d8ec599 100644 --- a/apps/dsm/DSMCoreModule.cpp +++ b/apps/dsm/DSMCoreModule.cpp @@ -443,7 +443,7 @@ EXEC_ACTION_START(SCSetAction) { string var_name = (par1.length() && par1[0] == '$')? par1.substr(1) : par1; - sc_sess->var[var_name] = resolveVars(par2, sess, sc_sess, event_params); + sc_sess->var[var_name] = resolveVars(par2, sess, sc_sess, event_params, true); DBG("set $%s='%s'\n", var_name.c_str(), sc_sess->var[var_name].c_str()); } EXEC_ACTION_END; diff --git a/apps/dsm/DSMModule.cpp b/apps/dsm/DSMModule.cpp index 31f181c1..8ad90155 100644 --- a/apps/dsm/DSMModule.cpp +++ b/apps/dsm/DSMModule.cpp @@ -25,6 +25,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include #include "DSMModule.h" #include "DSMSession.h" #include "AmSession.h" @@ -51,9 +52,51 @@ string trim(string const& str,char const* sepSet) str.substr(first, str.find_last_not_of(sepSet)-first+1); } -string resolveVars(const string s, AmSession* sess, - DSMSession* sc_sess, map* event_params) { +bool isNumber(const std::string& s) { + if (s.empty()) + return false; + + for (string::size_type i = 0; i < s.length(); i++) { + if (!std::isdigit(s[i])) + return false; + } + return true; +} + +string resolveVars(const string ts, AmSession* sess, + DSMSession* sc_sess, map* event_params, + bool eval_ops) { + string s = ts; if (s.length()) { + + if(eval_ops) { + // remove all spaces + string::size_type p; + for (p = s.find (" ", 0 ); + p != string::npos; p = s.find(" ", p)) { + s.erase (p, 1); + } + + // evaluate operators + string a,b; + if((p = s.find("-")) != string::npos) { + a = resolveVars(s.substr(0, p), sess, sc_sess, event_params, true); + b = resolveVars(s.substr(p+1, string::npos), sess, sc_sess, event_params, true); + if(isNumber(a) && isNumber(b)) { + std::stringstream res; res << atoi(a.c_str()) - atoi(b.c_str()); + return res.str(); + } + } + else if((p = s.find("+")) != string::npos) { + a = resolveVars(s.substr(0, p), sess, sc_sess, event_params, true); + b = resolveVars(s.substr(p+1, string::npos), sess, sc_sess, event_params, true); + if(isNumber(a) && isNumber(b)) { + std::stringstream res; res << atoi(a.c_str()) + atoi(b.c_str()); + return res.str(); + } + } + } + switch(s[0]) { case '$': return sc_sess->var[s.substr(1)]; case '#': diff --git a/apps/dsm/DSMModule.h b/apps/dsm/DSMModule.h index e1010c31..182da864 100644 --- a/apps/dsm/DSMModule.h +++ b/apps/dsm/DSMModule.h @@ -214,7 +214,8 @@ class SCStrArgAction return false; string resolveVars(const string s, AmSession* sess, - DSMSession* sc_sess, map* event_params); + DSMSession* sc_sess, map* event_params, + bool eval_ops = false); void splitCmd(const string& from_str, string& cmd, string& params);