The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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
15namespace {
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
57StatusCode 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; }
62}
63
64std::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
73std::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}
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
GAUDI_API StatusCode resolveEnv(const std::string &var, std::string &res, int recusions=124)
GAUDI_API std::vector< std::string > getEnv()
get all environment variables
Definition System.cpp:356
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition System.cpp:329
GAUDI_API std::string tempDirectory()
GAUDI_API std::string homeDirectory()