All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Environment.cpp
Go to the documentation of this file.
2 #include "GaudiKernel/System.h"
3 #include <cstdlib>
4 
5 namespace {
6  std::string i_resolve(std::string& source, int recursions) {
7  if ( recursions > 0 ) {
8  int lvl = 0, mx_lvl = 0;
9  for(const char* c=source.c_str(), *beg=nullptr; *c != 0; ++c) {
10  switch ( *c ) {
11  case '$':
12  if ( *(c+1) == '{' ) {
13  lvl++;
14  if ( lvl > mx_lvl ) {
15  mx_lvl = lvl;
16  beg = c;
17  }
18  c += 2;
19  }
20  break;
21  case '}':
22  if ( lvl == mx_lvl ) {
23  std::string env(beg+2,c-beg-2);
24  std::string rep;
25  if ( ! System::getEnv(env.c_str(), rep) )
26  rep = i_resolve(env, --recursions);
27  if ( rep.length() ) {
28  std::string e(beg,c-beg+1);
29  size_t idx=std::string::npos;
30  while((idx=source.find(e)) != std::string::npos) {
31  source.replace(idx, e.length(), rep);
32  }
33  return i_resolve(source, --recursions);
34  }
35  else {
36  // error: environment cannot be resolved....
37  // Try to continue, but there is not too much hope.
38  }
39  }
40  lvl--;
41  break;
42  default:
43  break;
44  }
45  }
46  }
47  return source;
48  }
49 }
50 
51 StatusCode System::resolveEnv(const std::string& var, std::string& res, int recursions) {
52  std::string source = var;
53  res = i_resolve(source, recursions);
54  if ( res.find("${") == std::string::npos ) {
55  return StatusCode::SUCCESS;
56  }
57  return StatusCode::FAILURE;
58 }
59 
61  // Return the user's home directory.
62  std::string home_dir = "./";
63  // Try to replace the current value with the content of several
64  // environment variables
65  if ( ! (System::getEnv("home", home_dir) ||
66  System::getEnv("HOME", home_dir)) ) {
67  // for Windows NT HOME might be defined as either $(HOMESHARE)/$(HOMEPATH)
68  // or $(HOMEDRIVE)/$(HOMEPATH)
69  if ( System::getEnv("HOMESHARE", home_dir) ||
70  System::getEnv("HOMEDRIVE", home_dir) ) {
72  if (System::getEnv("HOMEPATH", path))
73  home_dir += path;
74  }
75  }
76  return home_dir;
77 }
78 
80  // Return a user configured or systemwide directory to create
81  // temporary files in.
82  std::string dir;
83  if ( System::getEnv("TEMP", dir) ||
84  System::getEnv("TEMPDIR", dir) ||
85  System::getEnv("TEMP_DIR", dir) ||
86  System::getEnv("TMP", dir) ||
87  System::getEnv("TMPDIR", dir) ||
88  System::getEnv("TMP_DIR", dir)
89  )
90  return dir;
91  else
92  return homeDirectory();
93 }
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:550
GAUDI_API std::string homeDirectory()
Definition: Environment.cpp:60
GAUDI_API StatusCode resolveEnv(const std::string &var, std::string &res, int recusions=124)
Definition: Environment.cpp:51
STL class.
GAUDI_API std::string tempDirectory()
Definition: Environment.cpp:79
T replace(T...args)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
T find(T...args)
T length(T...args)
T c_str(T...args)