Module: sems
Branch: master
Commit: feb02617f4ba1c049c644ffddd87be80e14a5888
URL:    
http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=feb02617f4ba1c049c644ffddd87be80e14a5888

Author: Stefan Sayer <[email protected]>
Committer: Stefan Sayer <[email protected]>
Date:   Thu Sep 16 18:29:56 2010 +0200

DSM: utils.splitStringCR splits string on \n

example:
 sys.popen($myresult="/bin/ls wav/*");
 utils.splitStringCR($myresult);

---

 apps/dsm/mods/mod_utils/ModUtils.cpp |   26 ++++++++++++++++++++++++++
 apps/dsm/mods/mod_utils/ModUtils.h   |    1 +
 doc/dsm/mods/Readme.mod_utils.txt    |    9 ++++++++-
 3 files changed, 35 insertions(+), 1 deletions(-)

diff --git a/apps/dsm/mods/mod_utils/ModUtils.cpp 
b/apps/dsm/mods/mod_utils/ModUtils.cpp
index 3b637cb..7c9876b 100644
--- a/apps/dsm/mods/mod_utils/ModUtils.cpp
+++ b/apps/dsm/mods/mod_utils/ModUtils.cpp
@@ -47,6 +47,7 @@ MOD_ACTIONEXPORT_BEGIN(MOD_CLS_NAME) {
   DEF_CMD("utils.add", SCUSAddAction);
   DEF_CMD("utils.sub", SCUSSubAction);
   DEF_CMD("utils.int", SCUIntAction);
+  DEF_CMD("utils.splitStringCR", SCUSplitStringAction);
 } MOD_ACTIONEXPORT_END;
 
 MOD_CONDITIONEXPORT_NONE(MOD_CLS_NAME);
@@ -212,3 +213,28 @@ EXEC_ACTION_START(SCUIntAction) {
       varname.c_str(), sc_sess->var[varname].c_str());
 
 } EXEC_ACTION_END;
+
+CONST_ACTION_2P(SCUSplitStringAction, ',', true);
+EXEC_ACTION_START(SCUSplitStringAction) {
+  size_t cntr = 0;
+  string str = resolveVars(par1, sess, sc_sess, event_params);
+  string dst_array = par2;
+  if (!dst_array.length())
+    dst_array = par1;
+  if (dst_array.length() && dst_array[0]=='$')
+    dst_array = dst_array.substr(1);
+  
+  size_t p = 0, last_p = 0;
+  while (true) {
+    p = str.find("\n", last_p);
+    if (p==string::npos) {
+      if (last_p < str.length())
+       sc_sess->var[dst_array+"["+int2str(cntr)+"]"] = str.substr(last_p);
+      break;
+    }
+    
+    sc_sess->var[dst_array+"["+int2str(cntr++)+"]"] = str.substr(last_p, 
p-last_p);
+
+    last_p = p+1;    
+  }
+} EXEC_ACTION_END;
diff --git a/apps/dsm/mods/mod_utils/ModUtils.h 
b/apps/dsm/mods/mod_utils/ModUtils.h
index ccd9a64..d90b42f 100644
--- a/apps/dsm/mods/mod_utils/ModUtils.h
+++ b/apps/dsm/mods/mod_utils/ModUtils.h
@@ -41,4 +41,5 @@ DEF_ACTION_1P(SCUSRandomAction);
 DEF_ACTION_2P(SCUSAddAction);
 DEF_ACTION_2P(SCUSSubAction);
 DEF_ACTION_2P(SCUIntAction);
+DEF_ACTION_2P(SCUSplitStringAction);
 #endif
diff --git a/doc/dsm/mods/Readme.mod_utils.txt 
b/doc/dsm/mods/Readme.mod_utils.txt
index 25e29fa..0ee2868 100644
--- a/doc/dsm/mods/Readme.mod_utils.txt
+++ b/doc/dsm/mods/Readme.mod_utils.txt
@@ -33,4 +33,11 @@ Actions:
  utils.int($var, val)
    get integer part of val into var
 
-
+ utils.splitStringCR($var [, $dstvar])
+ utils.splitStringCR(val, $dstvar])
+   split a string on newline (carriage return, \n) 
+   into an array ($var[0]..$var[n])
+
+   example: 
+    sys.popen($myresult="/bin/ls wav/*");    
+    utils.splitStringCR($myresult);

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

Reply via email to