From 14ae585a329e48d4dc49bd367e8abe3bbfbe9432 Mon Sep 17 00:00:00 2001
From: Simon Praetorius <simon.praetorius@tu-dresden.de>
Date: Thu, 9 Sep 2010 22:16:52 +0000
Subject: [PATCH] read vector-parameters from init-files by %E,%F,%G or %D

---
 AMDiS/src/AdaptInfo.h   |  6 ++++
 AMDiS/src/Parameters.cc | 63 +++++++++++++++++++++++++++++++++++++++++
 AMDiS/src/Parameters.h  | 16 +++++++++--
 3 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/AMDiS/src/AdaptInfo.h b/AMDiS/src/AdaptInfo.h
index ab86c458..0c10da50 100644
--- a/AMDiS/src/AdaptInfo.h
+++ b/AMDiS/src/AdaptInfo.h
@@ -369,6 +369,12 @@ namespace AMDiS {
       return nTimesteps;
     }
 
+    /// Sets \ref nTimesteps.
+    inline void setNumberOfTimesteps(int num) 
+    {
+      nTimesteps = num;
+    }
+
     /// Increments \ref timestepNumber by 1;
     inline void incTimestepNumber() 
     { 
diff --git a/AMDiS/src/Parameters.cc b/AMDiS/src/Parameters.cc
index 92a1149e..6d303836 100644
--- a/AMDiS/src/Parameters.cc
+++ b/AMDiS/src/Parameters.cc
@@ -13,6 +13,7 @@
 namespace AMDiS {
 
   const char Parameters::comment='%';
+  const std::string Parameters::separators=",; ";
   Parameters* Parameters::singlett = NULL;
   const char* Parameters::param_call_fct = NULL;
   const char* Parameters::param_call_file = NULL;
@@ -158,6 +159,68 @@ namespace AMDiS {
 	    (*vecVal)[ind] = atof(sstr.c_str());	
 	}
 	break;
+      case 'E': // list of double values, example parameter: {a,b,c,d,e}
+      case 'F':
+      case 'G':
+	{
+	  std::vector<double> *vecVal = va_arg(arg, std::vector<double> *);
+	  std::string sstr = std::string(word);
+
+       std::string brackets1 = "[{(", brackets2 = "]})";
+	  int bracketIdx1 = sstr.find_first_of(brackets1);
+       int bracket = brackets1.find_first_of(sstr[bracketIdx1]);
+       int bracketIdx2 = sstr.find_first_of(brackets2[bracket]);
+       TEST_EXIT(bracket>=0 && bracketIdx1>=0 && bracketIdx2>=0 && bracketIdx1<bracketIdx2)
+		("no enclosing brackets found in '%s' \n",key.data());
+       sstr = sstr.substr(bracketIdx1+1, bracketIdx2-bracketIdx1);
+
+	  int found = sstr.find_first_of(separators);
+	  std::string seperator;
+	  seperator = sstr[found];
+	  
+	  while (found != static_cast<int>(std::string::npos)) {
+	    if (found > 0) {
+	      vecVal->push_back(atof(sstr.substr(0, found).c_str()));
+	    }
+	    sstr = sstr.substr(found + 1);
+	    found = sstr.find_first_of(seperator);
+	  }
+	  if (sstr.length() > 0)
+	    vecVal->push_back(atof(sstr.c_str()));
+       if(vecVal->size()==0)
+         WARNING("no values in parameter vector!\n");
+	}
+	break;
+      case 'D': // list of int values, example parameter: {a,b,c,d,e}
+	{
+	  std::vector<int> *vecVal = va_arg(arg, std::vector<int> *);
+	  std::string sstr = std::string(word);
+
+       std::string brackets1 = "[{(", brackets2 = "]})";
+	  int bracketIdx1 = sstr.find_first_of(brackets1);
+       int bracket = brackets1.find_first_of(sstr[bracketIdx1]);
+       int bracketIdx2 = sstr.find_first_of(brackets2[bracket]);
+       TEST_EXIT(bracket>=0 && bracketIdx1>=0 && bracketIdx2>=0 && bracketIdx1<bracketIdx2)
+		("no enclosing brackets found in '%s' \n",key.data());
+       sstr = sstr.substr(bracketIdx1+1, bracketIdx2-bracketIdx1);
+
+	  int found = sstr.find_first_of(separators);
+	  std::string seperator;
+	  seperator = sstr[found];
+	  
+	  while (found != static_cast<int>(std::string::npos)) {
+	    if (found > 0) {
+	      vecVal->push_back(atoi(sstr.substr(0, found).c_str()));
+	    }
+	    sstr = sstr.substr(found + 1);
+	    found = sstr.find_first_of(seperator);
+	  }
+	  if (sstr.length() > 0)
+	    vecVal->push_back(atoi(sstr.c_str()));
+       if(vecVal->size()==0)
+         WARNING("no values in parameter vector!\n");
+	}
+	break;
       case '*':
 	break;
       default: 
diff --git a/AMDiS/src/Parameters.h b/AMDiS/src/Parameters.h
index e331285b..575cfb64 100644
--- a/AMDiS/src/Parameters.h
+++ b/AMDiS/src/Parameters.h
@@ -122,7 +122,8 @@ namespace AMDiS {
      * The return value is the number of converted arguments.
      *
      * The control string must only contain the following characters used as 
-     * conversion specification: \%s, \%c, \%d, \%e, \%f, \%g, \%U, \%S, or \%*. 
+     * conversion specification: \%s, \%c, \%d, \%D, \%e, \%E, \%f, \%F, \%g, \%G,
+     * \%U, \%S, or \%*. 
      * All other characters are ignored. In contrast to scanf(), a numerical 
      * value for a field width is not allowed. For each element of the control 
      * string the next word of the parameter string is converted as follows:
@@ -143,11 +144,21 @@ namespace AMDiS {
      *       the subject sequence of the atoi() function; the corresponding 
      *       argument should be a pointer to an int variable;
      *
-     * -\%e,%f,%g: matches an optionally signed floating point number, whose 
+     * -\%D: matches a list of %d types; the corresponding argument should
+     *       be a pointer to a std::vector&lt;int&gt; variable; the
+     *       parameter should be enclosed in brackets, like {...}, [...] or (...)
+     *       and the values separated by comma, semicolon or space;
+     *
+     * -\%e,\%f,\%g: matches an optionally signed floating point number, whose 
      *       format is the same as expected for the subject string of the atof()
      *       function; the corresponding argument should be a pointer to a double
      *       variable;
      *
+     * -\%E,\%F,\%G: matches a list of %e,%f or %g types; the corresponding argument
+     *       should be a pointer to a std::vector&lt;double&gt; variable; the
+     *       parameter should be enclosed in brackets, like {...}, [...] or (...)
+     *       and the values separated by comma, semicolon or space; 
+     *
      * -\%U: matches an unsigned decimal integer in the range [0,255], whose 
      *       format is the same as expected for the subject sequence of the 
      *       atoi() function; the corresponding argument should be a pointer to 
@@ -279,6 +290,7 @@ namespace AMDiS {
     };
 
     static const char comment;
+    static const std::string separators;
 
     std::string buffer;
 
-- 
GitLab