Commit 709dcdda authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

Initfileparser extended

parent 7f2fd286
...@@ -86,6 +86,8 @@ namespace AMDiS { ...@@ -86,6 +86,8 @@ namespace AMDiS {
// add parameter to map after variable replacement // add parameter to map after variable replacement
std::string paramName = variableReplacement(InitfileInternal::trim(parser.name)); std::string paramName = variableReplacement(InitfileInternal::trim(parser.name));
std::string paramValue = variableReplacement(InitfileInternal::trim(parser.value)); std::string paramValue = variableReplacement(InitfileInternal::trim(parser.value));
paramValue = variableEvaluation(paramValue);
operator[](paramName) = paramValue; operator[](paramName) = paramValue;
int info = 0; int info = 0;
get("parameter information", info, 0); get("parameter information", info, 0);
...@@ -133,19 +135,25 @@ namespace AMDiS { ...@@ -133,19 +135,25 @@ namespace AMDiS {
std::string Initfile::variableReplacement(const std::string& input) const std::string Initfile::variableReplacement(const std::string& input) const
{ {
std::string whitespaces = " \t\r\f"; std::string whitespaces = " \t\r\f";
std::string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
std::string inputSwap = input; std::string inputSwap = input;
size_t posVar = inputSwap.find_first_of('$'); size_t posVar = inputSwap.find_first_of('$');
while (posVar != string::npos) { while (posVar != string::npos) {
size_t posVarBegin, posVarEnd; size_t posVarBegin, posVarEnd;
if(inputSwap[posVar+1] == '{') { if(inputSwap[posVar+1] == '{') { // ${var_name}
posVarEnd = inputSwap.find_first_of('}',posVar + 2); posVarEnd = inputSwap.find_first_of('}',posVar + 2);
posVarBegin = posVar + 1; posVarBegin = posVar + 1;
} else { } else if (inputSwap[posVar+1] != '(' && inputSwap[posVar+1] != '[') {
posVarEnd = inputSwap.find_first_of(whitespaces, posVar + 1); posVarEnd = inputSwap.find_first_not_of(allowedChars, posVar + 1);
posVarBegin = posVar; posVarBegin = posVar;
} else {
posVar = inputSwap.find_first_of('$',posVar+1);
continue;
} }
std::string varName = inputSwap.substr(posVarBegin + 1 , posVarEnd - posVarBegin - 1); std::string varName = inputSwap.substr(posVarBegin + 1 , posVarEnd - posVarBegin - 1);
// if varname is found in parameter list then replace variable by value // if varname is found in parameter list then replace variable by value
// otherwise throw tagNotFound exception // otherwise throw tagNotFound exception
std::string varParam; std::string varParam;
...@@ -156,12 +164,40 @@ namespace AMDiS { ...@@ -156,12 +164,40 @@ namespace AMDiS {
std::string replaceName = inputSwap.substr(posVar , posVarEnd - posVar + (posVarBegin - posVar)); std::string replaceName = inputSwap.substr(posVar , posVarEnd - posVar + (posVarBegin - posVar));
inputSwap.replace(inputSwap.find(replaceName), replaceName.length(), varParam); inputSwap.replace(inputSwap.find(replaceName), replaceName.length(), varParam);
posVar = inputSwap.find_first_of('$'); posVar = inputSwap.find_first_of('$',posVarEnd);
} }
return inputSwap; return inputSwap;
} }
std::string Initfile::variableEvaluation(const std::string& input) const
{
std::string whitespaces = " \t\r\f";
std::string inputSwap = input;
size_t posVar = inputSwap.find_first_of('$');
while (posVar != string::npos) {
size_t posVarBegin, posVarEnd;
if(inputSwap[posVar+1] == '(') { // $(expr)
posVarEnd = inputSwap.find_first_of(')',posVar + 2);
posVarBegin = posVar + 1;
} else {
posVar = inputSwap.find_first_of('$',posVar+1);
continue;
}
std::string varName = inputSwap.substr(posVarBegin + 1 , posVarEnd - posVarBegin - 1);
double value = 0.0;
InitfileInternal::convert(varName, value); // string -> double (using muparser)
InitfileInternal::convert(value, varName); // double -> string
std::string replaceName = inputSwap.substr(posVar , posVarEnd - posVar + (posVarBegin - posVar));
inputSwap.replace(inputSwap.find(replaceName), replaceName.length(), varName);
posVar = inputSwap.find_first_of('$');
}
return inputSwap;
}
void Initfile::readArgv(int argc, char **argv) void Initfile::readArgv(int argc, char **argv)
{ {
......
...@@ -611,6 +611,8 @@ protected: ...@@ -611,6 +611,8 @@ protected:
/// the last version only for variablenames without whitespaces /// the last version only for variablenames without whitespaces
std::string variableReplacement(const std::string& input) const; std::string variableReplacement(const std::string& input) const;
std::string variableEvaluation(const std::string& input) const;
/** Fill the initfile from an input stream. /** Fill the initfile from an input stream.
* @param in: the stream to fill the data from. * @param in: the stream to fill the data from.
* Current dataformat: tag:value * Current dataformat: tag:value
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment