Author: sayer
Date: 2010-03-05 12:45:59 +0100 (Fri, 05 Mar 2010)
New Revision: 1651
Modified:
trunk/apps/dsm/DSMCoreModule.cpp
trunk/apps/dsm/DSMModule.cpp
trunk/apps/dsm/DSMModule.h
Log:
adds simple evaluation of + and - arithmetic ops
(slightly fixed) patch by Andreas Granig agranig at sipwise
Modified: trunk/apps/dsm/DSMCoreModule.cpp
===================================================================
--- trunk/apps/dsm/DSMCoreModule.cpp 2010-03-04 16:36:23 UTC (rev 1650)
+++ trunk/apps/dsm/DSMCoreModule.cpp 2010-03-05 11:45:59 UTC (rev 1651)
@@ -443,7 +443,7 @@
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;
Modified: trunk/apps/dsm/DSMModule.cpp
===================================================================
--- trunk/apps/dsm/DSMModule.cpp 2010-03-04 16:36:23 UTC (rev 1650)
+++ trunk/apps/dsm/DSMModule.cpp 2010-03-05 11:45:59 UTC (rev 1651)
@@ -25,6 +25,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#include <sstream>
#include "DSMModule.h"
#include "DSMSession.h"
#include "AmSession.h"
@@ -51,9 +52,51 @@
str.substr(first, str.find_last_not_of(sepSet)-first+1);
}
-string resolveVars(const string s, AmSession* sess,
- DSMSession* sc_sess, map<string,string>* 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<string,string>* 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 '#':
Modified: trunk/apps/dsm/DSMModule.h
===================================================================
--- trunk/apps/dsm/DSMModule.h 2010-03-04 16:36:23 UTC (rev 1650)
+++ trunk/apps/dsm/DSMModule.h 2010-03-05 11:45:59 UTC (rev 1651)
@@ -214,7 +214,8 @@
return false;
string resolveVars(const string s, AmSession* sess,
- DSMSession* sc_sess, map<string,string>* event_params);
+ DSMSession* sc_sess, map<string,string>* event_params,
+ bool eval_ops = false);
void splitCmd(const string& from_str,
string& cmd, string& params);
_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev