| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Environment.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/Environment.h"
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
61 std::string System::homeDirectory()
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 ) ) {
71 std::string path;
72 if ( System::getEnv( "HOMEPATH", path ) ) home_dir += path;
73 }
74 }
75 return home_dir;
76 }
77
78 std::string System::tempDirectory()
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 }
System::getEnv
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:581
System.h
StatusCode::SUCCESS
Definition: StatusCode.h:31
System::homeDirectory
GAUDI_API std::string homeDirectory()
Definition: Environment.cpp:61
gaudirun.c
c
Definition: gaudirun.py:407
System::resolveEnv
GAUDI_API StatusCode resolveEnv(const std::string &var, std::string &res, int recusions=124)
Definition: Environment.cpp:51
GaudiPython.HistoUtils.path
path
Definition: HistoUtils.py:895
StatusCode::FAILURE
Definition: StatusCode.h:31
std::string
STL class.
System::tempDirectory
GAUDI_API std::string tempDirectory()
Definition: Environment.cpp:78
std::string::replace
T replace(T...args)
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
std::string::find
T find(T...args)
std::string::length
T length(T...args)
std::string::c_str
T c_str(T...args)
Environment.h