Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved _getEnviron to deal with multiple variables in the evaluated string #1159

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions DDCore/src/Handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,26 +280,41 @@ namespace dd4hep {
}

/// Evaluate string constant using environment stored in the evaluator
string _getEnviron(const string& env) {
size_t id1 = env.find("${");
size_t id2 = env.rfind("}");
if ( id1 == string::npos || id2 == string::npos ) {
return "";
}
else {
stringstream err;
string v = env.substr(id1,id2-id1+1);
auto ret = eval.getEnviron(v, err);
if ( ret.first != tools::Evaluator::OK) {
cerr << env << ": " << err.str() << endl;
throw runtime_error("dd4hep: Severe error during environment lookup of " + env +
" " + err.str());
string _getEnviron(const string& env) {
// We are trying to deal with the case when several variables are being replaced in the
// string.
size_t current_index = 0;
stringstream processed_variable;
while (true) {
// Looking for the start of a variable use, with the syntax
// "path1/path2/${VAR1}/path3/${VAR2}"
size_t id1 = env.find("${", current_index);
// variable start found, do a greedy search for the variable end
if (id1 != string::npos) {
bcouturi marked this conversation as resolved.
Show resolved Hide resolved
size_t id2 = env.find("}", id1);
if (id2 == string::npos) {
runtime_error("dd4hep: Syntax error, bas variable syntax: " + env);
bcouturi marked this conversation as resolved.
Show resolved Hide resolved
}
processed_variable << env.substr(current_index, id1 -current_index );
string v = env.substr(id1, id2-id1+1);
stringstream err;
auto ret = eval.getEnviron(v, err);
// Checking that the variable lookup worked
if ( ret.first != tools::Evaluator::OK) {
cerr << v << ": " << err.str() << endl;
throw runtime_error("dd4hep: Severe error during environment lookup of " + v + " " + err.str());
}
// Now adding the variable
processed_variable << ret.second;
current_index = id2 + 1;
} else {
// In this case we did not find the ${ to indicate a start of variable,
// we just copy the rest of the variable to the stringstream and exit
processed_variable << env.substr(current_index);
break;
}
v = env.substr(0,id1);
v += ret.second;
v += env.substr(id2+1);
return v;
}
return processed_variable.str();
}

/// String manipulations: Remove unconditionally all white spaces
Expand Down
Loading