Module: sems
Branch: master
Commit: e25a1dbf2e27b3346f4e3369cec8670a19908eb8
URL: 
https://github.com/sems-server/sems/commit/e25a1dbf2e27b3346f4e3369cec8670a19908eb8

Author: Stefan Sayer <[email protected]>
Committer: Stefan Sayer <[email protected]>
Date: 2015-06-09T00:26:28+02:00

dsm: arrayIndex action: get the index of a string in a var array

---

Modified: apps/dsm/DSMCoreModule.cpp
Modified: apps/dsm/DSMCoreModule.h
Modified: doc/dsm/dsm_syntax.txt

---

Diff:  
https://github.com/sems-server/sems/commit/e25a1dbf2e27b3346f4e3369cec8670a19908eb8.diff
Patch: 
https://github.com/sems-server/sems/commit/e25a1dbf2e27b3346f4e3369cec8670a19908eb8.patch

---

diff --git a/apps/dsm/DSMCoreModule.cpp b/apps/dsm/DSMCoreModule.cpp
index f97ff23..e41fdf1 100644
--- a/apps/dsm/DSMCoreModule.cpp
+++ b/apps/dsm/DSMCoreModule.cpp
@@ -105,6 +105,7 @@ DSMAction* DSMCoreModule::getAction(const string& from_str) 
{
   DEF_CMD("clearStruct", SCClearStructAction);
   DEF_CMD("clearArray", SCClearArrayAction);
   DEF_CMD("size", SCSizeAction);
+  DEF_CMD("arrayIndex", SCArrayIndexAction);
   DEF_CMD("logVars", SCLogVarsAction);
   DEF_CMD("logParams", SCLogParamsAction);
   DEF_CMD("logSelects", SCLogSelectsAction);
@@ -921,6 +922,35 @@ EXEC_ACTION_START(SCSizeAction) {
   DBG("set $%s=%s\n", dst_name.c_str(), res.c_str());
 } EXEC_ACTION_END;
 
+CONST_ACTION_2P(SCArrayIndexAction, ',', false);
+EXEC_ACTION_START(SCArrayIndexAction) {
+  string array_name = par1;
+  if (array_name.length() && array_name[0]=='$')
+    array_name.erase(0,1);
+
+  string val = resolveVars(par2, sess, sc_sess, event_params);
+  unsigned int i = 0;
+  bool found = false;
+  while (true) {
+    VarMapT::iterator lb = sc_sess->var.find(array_name+"["+int2str(i)+"]");
+    if (lb == sc_sess->var.end())
+      break;
+    if (val == lb->second) {
+      found = true;
+      break;
+    }
+    i++;
+  }
+
+  string res = found ? int2str(i) : "nil";
+  if (par2[0]=='$') {
+    sc_sess->var[par2.substr(1)+".index"] = res;
+    DBG("set %s=%s\n", (par2+".index").c_str(), res.c_str());
+  } else {
+    sc_sess->var["index"] = res;
+    DBG("set $index=%s\n", res.c_str());
+  }
+} EXEC_ACTION_END;
 
 CONST_ACTION_2P(SCAppendAction,',', false);
 EXEC_ACTION_START(SCAppendAction) {
diff --git a/apps/dsm/DSMCoreModule.h b/apps/dsm/DSMCoreModule.h
index ea8a3ad..6569d24 100644
--- a/apps/dsm/DSMCoreModule.h
+++ b/apps/dsm/DSMCoreModule.h
@@ -95,6 +95,7 @@ DEF_ACTION_1P(SCClearAction);
 DEF_ACTION_1P(SCClearStructAction);
 DEF_ACTION_1P(SCClearArrayAction);
 DEF_ACTION_2P(SCSizeAction);
+DEF_ACTION_2P(SCArrayIndexAction);
 DEF_ACTION_2P(SCSetTimerAction);
 DEF_ACTION_1P(SCRemoveTimerAction);
 DEF_ACTION_1P(SCRemoveTimersAction);
diff --git a/doc/dsm/dsm_syntax.txt b/doc/dsm/dsm_syntax.txt
index b05f0bf..c3124f1 100644
--- a/doc/dsm/dsm_syntax.txt
+++ b/doc/dsm/dsm_syntax.txt
@@ -104,6 +104,9 @@ DSM flow
   set variable $dst to size of array 
   (e.g. $arrayname[0], $arrayname[1] set, $dst set to 2)
 
+ arrayIndex($array, key)  - find key in $array, set $index to its index, or 
"nil" if not found
+ arrayIndex($array, $var) - find $var in $array, set $var.index to its index, 
or "nil" if not found
+
  inc($var)
  clear($var)
  clearStruct($var)

_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev

Reply via email to