The Gaudi Framework  master (da3d77e1)
Environment.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
12 #include <GaudiKernel/System.h>
13 #include <cstdlib>
14 
15 namespace {
16  std::string i_resolve( std::string& source, int recursions ) {
17  if ( recursions > 0 ) {
18  int lvl = 0, mx_lvl = 0;
19  for ( const char *c = source.c_str(), *beg = nullptr; *c != 0; ++c ) {
20  switch ( *c ) {
21  case '$':
22  if ( *( c + 1 ) == '{' ) {
23  lvl++;
24  if ( lvl > mx_lvl ) {
25  mx_lvl = lvl;
26  beg = c;
27  }
28  c += 2;
29  }
30  break;
31  case '}':
32  if ( lvl == mx_lvl ) {
33  std::string env( beg + 2, c - beg - 2 );
34  std::string rep;
35  if ( !System::getEnv( env.c_str(), rep ) ) rep = i_resolve( env, --recursions );
36  if ( rep.length() ) {
37  std::string e( beg, c - beg + 1 );
38  size_t idx = std::string::npos;
39  while ( ( idx = source.find( e ) ) != std::string::npos ) { source.replace( idx, e.length(), rep ); }
40  return i_resolve( source, --recursions );
41  } else {
42  // error: environment cannot be resolved....
43  // Try to continue, but there is not too much hope.
44  }
45  }
46  lvl--;
47  break;
48  default:
49  break;
50  }
51  }
52  }
53  return source;
54  }
55 } // namespace
56 
57 StatusCode System::resolveEnv( const std::string& var, std::string& res, int recursions ) {
58  std::string source = var;
59  res = i_resolve( source, recursions );
60  if ( res.find( "${" ) == std::string::npos ) { return StatusCode::SUCCESS; }
61  return StatusCode::FAILURE;
62 }
63 
65  // Return the user's home directory.
66  std::string home_dir = "./";
67  // Try to replace the current value with the content of several
68  // environment variables
69  if ( !( System::getEnv( "home", home_dir ) || System::getEnv( "HOME", home_dir ) ) ) {
70  // for Windows NT HOME might be defined as either $(HOMESHARE)/$(HOMEPATH)
71  // or $(HOMEDRIVE)/$(HOMEPATH)
72  if ( System::getEnv( "HOMESHARE", home_dir ) || System::getEnv( "HOMEDRIVE", home_dir ) ) {
74  if ( System::getEnv( "HOMEPATH", path ) ) home_dir += path;
75  }
76  }
77  return home_dir;
78 }
79 
81  // Return a user configured or systemwide directory to create
82  // temporary files in.
83  std::string dir;
84  if ( System::getEnv( "TEMP", dir ) || System::getEnv( "TEMPDIR", dir ) || System::getEnv( "TEMP_DIR", dir ) ||
85  System::getEnv( "TMP", dir ) || System::getEnv( "TMPDIR", dir ) || System::getEnv( "TMP_DIR", dir ) )
86  return dir;
87  else
88  return homeDirectory();
89 }
std::string
STL class.
System::homeDirectory
GAUDI_API std::string homeDirectory()
Definition: Environment.cpp:64
AtlasMCRecoFullPrecedenceDump.path
path
Definition: AtlasMCRecoFullPrecedenceDump.py:49
System.h
System::getEnv
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:390
std::string::find
T find(T... args)
std::string::length
T length(T... args)
gaudirun.c
c
Definition: gaudirun.py:525
std::string::replace
T replace(T... args)
StatusCode
Definition: StatusCode.h:65
System::tempDirectory
GAUDI_API std::string tempDirectory()
Definition: Environment.cpp:80
std::string::c_str
T c_str(T... args)
Environment.h
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
System::resolveEnv
GAUDI_API StatusCode resolveEnv(const std::string &var, std::string &res, int recusions=124)
Definition: Environment.cpp:57