From ec1d1de937adcba32d34cebf2eb7b398b385dc50 Mon Sep 17 00:00:00 2001
From: Stefan Sayer <stefan.sayer@googlemail.com>
Date: Mon, 24 Nov 2014 22:03:44 +0100
Subject: [PATCH 1/2] dsm: arrayIndex action: get the index of a string in a
 var array

---
 apps/dsm/DSMCoreModule.cpp | 30 ++++++++++++++++++++++++++++++
 apps/dsm/DSMCoreModule.h   |  1 +
 doc/dsm/dsm_syntax.txt     |  3 +++
 3 files changed, 34 insertions(+)

diff --git a/apps/dsm/DSMCoreModule.cpp b/apps/dsm/DSMCoreModule.cpp
index 95453ec..fc9e43b 100644
--- a/apps/dsm/DSMCoreModule.cpp
+++ b/apps/dsm/DSMCoreModule.cpp
@@ -99,6 +99,7 @@ DSMAction* DSMCoreModule::getAction(const string& from_str) {
   DEF_CMD("clear", SCClearAction);
   DEF_CMD("clearArray", SCClearArrayAction);
   DEF_CMD("size", SCSizeAction);
+  DEF_CMD("arrayIndex", SCArrayIndexAction);
   DEF_CMD("logVars", SCLogVarsAction);
   DEF_CMD("logParams", SCLogParamsAction);
   DEF_CMD("logSelects", SCLogSelectsAction);
@@ -881,6 +882,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 b5eefc3..a65616e 100644
--- a/apps/dsm/DSMCoreModule.h
+++ b/apps/dsm/DSMCoreModule.h
@@ -94,6 +94,7 @@ DEF_ACTION_1P(SCIncAction);
 DEF_ACTION_1P(SCClearAction);
 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 c104654..8bb6bb6 100644
--- a/doc/dsm/dsm_syntax.txt
+++ b/doc/dsm/dsm_syntax.txt
@@ -109,6 +109,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)
  clearArray($var)
-- 
1.9.3 (Apple Git-50)

