Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
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 ) ) rep = i_resolve( env, --recursions );
26  if ( rep.length() ) {
27  std::string e( beg, c - beg + 1 );
28  size_t idx = std::string::npos;
29  while ( ( idx = source.find( e ) ) != std::string::npos ) { source.replace( idx, e.length(), rep ); }
30  return i_resolve( source, --recursions );
31  } else {
32  // error: environment cannot be resolved....
33  // Try to continue, but there is not too much hope.
34  }
35  }
36  lvl--;
37  break;
38  default:
39  break;
40  }
41  }
42  }
43  return source;
44  }
45 } // namespace
46 
47 StatusCode System::resolveEnv( const std::string& var, std::string& res, int recursions ) {
48  std::string source = var;
49  res = i_resolve( source, recursions );
50  if ( res.find( "${" ) == std::string::npos ) { return StatusCode::SUCCESS; }
51  return StatusCode::FAILURE;
52 }
53 
55  // Return the user's home directory.
56  std::string home_dir = "./";
57  // Try to replace the current value with the content of several
58  // environment variables
59  if ( !( System::getEnv( "home", home_dir ) || System::getEnv( "HOME", home_dir ) ) ) {
60  // for Windows NT HOME might be defined as either $(HOMESHARE)/$(HOMEPATH)
61  // or $(HOMEDRIVE)/$(HOMEPATH)
62  if ( System::getEnv( "HOMESHARE", home_dir ) || System::getEnv( "HOMEDRIVE", home_dir ) ) {
64  if ( System::getEnv( "HOMEPATH", path ) ) home_dir += path;
65  }
66  }
67  return home_dir;
68 }
69 
71  // Return a user configured or systemwide directory to create
72  // temporary files in.
73  std::string dir;
74  if ( System::getEnv( "TEMP", dir ) || System::getEnv( "TEMPDIR", dir ) || System::getEnv( "TEMP_DIR", dir ) ||
75  System::getEnv( "TMP", dir ) || System::getEnv( "TMPDIR", dir ) || System::getEnv( "TMP_DIR", dir ) )
76  return dir;
77  else
78  return homeDirectory();
79 }
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:380
GAUDI_API std::string homeDirectory()
Definition: Environment.cpp:54
GAUDI_API StatusCode resolveEnv(const std::string &var, std::string &res, int recusions=124)
Definition: Environment.cpp:47
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
STL class.
GAUDI_API std::string tempDirectory()
Definition: Environment.cpp:70
T replace(T...args)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
T find(T...args)
T length(T...args)
T c_str(T...args)
constexpr static const auto FAILURE
Definition: StatusCode.h:86