The Gaudi Framework  v29r0 (ff2e7097)
Environment.cpp
Go to the documentation of this file.
2 #include "GaudiKernel/System.h"
3 #include <cstdlib>
4 
5 namespace
6 {
7  std::string i_resolve( std::string& source, int recursions )
8  {
9  if ( recursions > 0 ) {
10  int lvl = 0, mx_lvl = 0;
11  for ( const char *c = source.c_str(), *beg = nullptr; *c != 0; ++c ) {
12  switch ( *c ) {
13  case '$':
14  if ( *( c + 1 ) == '{' ) {
15  lvl++;
16  if ( lvl > mx_lvl ) {
17  mx_lvl = lvl;
18  beg = c;
19  }
20  c += 2;
21  }
22  break;
23  case '}':
24  if ( lvl == mx_lvl ) {
25  std::string env( beg + 2, c - beg - 2 );
26  std::string rep;
27  if ( !System::getEnv( env.c_str(), rep ) ) rep = i_resolve( env, --recursions );
28  if ( rep.length() ) {
29  std::string e( beg, c - beg + 1 );
30  size_t idx = std::string::npos;
31  while ( ( idx = source.find( e ) ) != std::string::npos ) {
32  source.replace( idx, e.length(), rep );
33  }
34  return i_resolve( source, --recursions );
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 {
53  std::string source = var;
54  res = i_resolve( source, recursions );
55  if ( res.find( "${" ) == std::string::npos ) {
56  return StatusCode::SUCCESS;
57  }
58  return StatusCode::FAILURE;
59 }
60 
62 {
63  // Return the user's home directory.
64  std::string home_dir = "./";
65  // Try to replace the current value with the content of several
66  // environment variables
67  if ( !( System::getEnv( "home", home_dir ) || System::getEnv( "HOME", home_dir ) ) ) {
68  // for Windows NT HOME might be defined as either $(HOMESHARE)/$(HOMEPATH)
69  // or $(HOMEDRIVE)/$(HOMEPATH)
70  if ( System::getEnv( "HOMESHARE", home_dir ) || System::getEnv( "HOMEDRIVE", home_dir ) ) {
72  if ( System::getEnv( "HOMEPATH", path ) ) home_dir += path;
73  }
74  }
75  return home_dir;
76 }
77 
79 {
80  // Return a user configured or systemwide directory to create
81  // temporary files in.
82  std::string dir;
83  if ( System::getEnv( "TEMP", dir ) || System::getEnv( "TEMPDIR", dir ) || System::getEnv( "TEMP_DIR", dir ) ||
84  System::getEnv( "TMP", dir ) || System::getEnv( "TMPDIR", dir ) || System::getEnv( "TMP_DIR", dir ) )
85  return dir;
86  else
87  return homeDirectory();
88 }
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:581
GAUDI_API std::string homeDirectory()
Definition: Environment.cpp:61
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:78
T replace(T...args)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
T find(T...args)
T length(T...args)
T c_str(T...args)