Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Environment.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 
64 std::string System::homeDirectory() {
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  System::getEnv( "home", home_dir ) || System::getEnv( "HOME", home_dir );
70  return home_dir;
71 }
72 
73 std::string System::tempDirectory() {
74  // Return a user configured or systemwide directory to create
75  // temporary files in.
76  std::string dir;
77  if ( System::getEnv( "TEMP", dir ) || System::getEnv( "TEMPDIR", dir ) || System::getEnv( "TEMP_DIR", dir ) ||
78  System::getEnv( "TMP", dir ) || System::getEnv( "TMPDIR", dir ) || System::getEnv( "TMP_DIR", dir ) )
79  return dir;
80  else
81  return homeDirectory();
82 }
System::homeDirectory
GAUDI_API std::string homeDirectory()
Definition: Environment.cpp:64
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:329
gaudirun.c
c
Definition: gaudirun.py:525
StatusCode
Definition: StatusCode.h:64
System::tempDirectory
GAUDI_API std::string tempDirectory()
Definition: Environment.cpp:73
Environment.h
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:99
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:100
System::resolveEnv
GAUDI_API StatusCode resolveEnv(const std::string &var, std::string &res, int recusions=124)
Definition: Environment.cpp:57