TSystem::Exec - how to source a file in the environment of macro

Is it possible somehow to add environment variables from within the running macro by sourcing a file?
Or do i have to parse the file line by line, match on term1 = term2 and and use SetEnv(term1,term2)
Thank you!

We do something similar when building the root ref guide.
in a .C macros we do something like:

 gSystem->Exec(Form("grep %s $ROOTSYS/lib/*.rootmap ", classname.Data()));

I am not sure this answer your question.

not really … i would like to do :

gSystem->Exec("source ./job.steer")
TString myvar1 = gSystem->Getenv("MYVAR1")
TString myvar2 = gSystem->Getenv("MYVAR"2)

the reason is that i do not control the generated script for job running but i have control over the files in input box and the macro that is saved in a root file.
and the choice of variables collection as a bash file is that i would like the solution to be also compatible with root5.
rephrasing the question would be like this : What is the best method for storing key/value pairs as text that is compatible also with root5?
Thank you!!

well, i put something together that seems to work:

void load_config(const char* file) {
std::ifstream filestream(file);
std::string str;
TPRegexp regex ("[a-zA-Z0-9_]+=[^\r^\n^\t^\f^\v^ ]+");

while (std::getline(filestream, str)) {
    TString line (str);
    TString pair = line(regex);
    if (!pair.IsNull()) {
        TObjArray* decl = pair.Tokenize("=");
        TString key   = ((TObjString*)decl->At(0))->GetString();
        TString value = ((TObjString*)decl->At(1))->GetString();
        value = value.Strip(TString::EStripType::kBoth, '\"');
        value = value.Strip(TString::EStripType::kBoth, '\'');
        gSystem->Setenv(key.Data(), value.Data());

it will select any key=value pair from a file and then setup them in the environment

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.