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