Hi,

Attached is a patch to allow evaluation of "+" and "-" operators in set(...) when writing dsm files. It's quite simple, but works fine with things like set($foo=$foo-1) or set($foo=$bar+3) etc. It doesn't support multiple operators like set($foo=$bar-3+17) at the moment though. Hope it's still useful. For me for example, it allows to create "loops" with a configurable number of iterations.


Andreas
Index: apps/dsm/DSMModule.h
===================================================================
--- apps/dsm/DSMModule.h	(revision 1647)
+++ apps/dsm/DSMModule.h	(working copy)
@@ -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 evalOps = false);
 
 void splitCmd(const string& from_str, 
 		string& cmd, string& params);
Index: apps/dsm/DSMModule.cpp
===================================================================
--- apps/dsm/DSMModule.cpp	(revision 1647)
+++ apps/dsm/DSMModule.cpp	(working copy)
@@ -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,48 @@
     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) {
+  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 evalOps) {
+  string s = ts;
   if (s.length()) {
+
+    if(evalOps) {
+      // 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 '#': 
Index: apps/dsm/DSMCoreModule.cpp
===================================================================
--- apps/dsm/DSMCoreModule.cpp	(revision 1647)
+++ apps/dsm/DSMCoreModule.cpp	(working copy)
@@ -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;
_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev

Reply via email to