The Gaudi Framework  v33r0 (d5ea422b)
Utils.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 \***********************************************************************************/
11 // ===========================================================================
12 // Include files
13 // ===========================================================================
14 #include "Utils.h"
15 // ===========================================================================
16 // Boost
17 // ===========================================================================
18 #include <boost/algorithm/string.hpp>
19 #include <boost/regex.hpp>
20 // ===========================================================================
21 // Gaudi
22 // ===========================================================================
23 #include "GaudiKernel/System.h"
24 // ===========================================================================
25 
26 namespace gpu = Gaudi::Parsers::Utils;
27 
29  static const boost::regex expression( "\\$(([A-Za-z0-9_]+)|\\(([A-Za-z0-9_]+)\\))" );
30 
31  std::string result = input;
32  auto start = input.begin();
33  auto end = input.end();
34  boost::match_results<std::string::const_iterator> what;
35  boost::match_flag_type flags = boost::match_default;
36  while ( boost::regex_search( start, end, what, expression, flags ) ) {
37  std::string var{what[2].first, what[2].second};
38  if ( var.empty() ) var = std::string{what[3].first, what[3].second};
39  std::string env;
40  if ( System::getEnv( var, env ) ) {
41  boost::algorithm::replace_first( result, std::string{what[0].first, what[0].second}, env );
42  }
43  start = what[0].second;
44  // update flags:
45  flags |= boost::match_prev_avail;
46  flags |= boost::match_not_bob;
47  }
48  return result;
49 }
50 // ===========================================================================
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:379
T end(T... args)
std::string replaceEnvironments(const std::string &input)
Definition: Utils.cpp:28
def start
Definition: IOTest.py:108
STL class.
def end
Definition: IOTest.py:123
T begin(T... args)