Line data Source code
1 : // DUNE DAQ modification notice:
2 : // This file has been modified from the original ATLAS system source for the DUNE DAQ project.
3 : // Fork baseline commit: system-00-00-20 (2020-09-25).
4 : // Renamed since fork: yes (from src/Environment.cxx to src/Environment.cpp).
5 :
6 : /*
7 : * Environment.cxx
8 : * ers
9 : *
10 : * Created by Matthias Wiesmann on 11.01.05.
11 : * Copyright 2005 CERN. All rights reserved.
12 : *
13 : */
14 :
15 : #include <stdlib.h>
16 :
17 : #include "okssystem/Environment.hpp"
18 : #include "okssystem/exceptions.hpp"
19 :
20 : using namespace OksSystem;
21 :
22 : /** Sets an environnement variable
23 : * \param key the name of the variable
24 : * \param value the value of the variable
25 : * \exception PosixIssue unable to set environnement variable
26 : */
27 :
28 : void
29 0 : OksSystem::Environment::set(const std::string& key, const std::string& value)
30 : {
31 0 : const char* c_key = key.c_str();
32 0 : const char* c_value = value.c_str();
33 0 : const int status = ::setenv(c_key, c_value, 1);
34 0 : if (status < 0) {
35 0 : std::string message = "while setting " + key + " to " + value;
36 0 : throw OksSystemCallIssue(ERS_HERE, errno, "setenv", message.c_str());
37 0 : }
38 0 : } // set
39 :
40 : /** Sets a collection of environnement variables.
41 : * \param values The map of string string pairs containing the name / values pairs
42 : * \exception PosixIssue unable to set environnement variable
43 : */
44 :
45 : void
46 0 : OksSystem::Environment::set(const std::map<std::string, std::string>& values)
47 : {
48 0 : for (std::map<std::string, std::string>::const_iterator pos = values.begin(); pos != values.end(); ++pos) {
49 0 : set(pos->first, pos->second);
50 : } // for
51 0 : } // set
52 :
53 : /** Gets an environnement variable
54 : * \param key the name of the variable
55 : * \return the value of the variable, of the string \c NO_VALUE if the variable is unknown.
56 : */
57 :
58 : std::string
59 0 : OksSystem::Environment::get(const std::string& key)
60 : {
61 0 : const char* c_value = ::getenv(key.c_str());
62 0 : if (c_value == 0)
63 0 : return std::string();
64 0 : return std::string(c_value);
65 : } // get
|