2 * \file environment.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author João Luis M. Assirati
8 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
15 #include "support/environment.h"
17 #include "support/docstring.h"
18 #include "support/os.h"
20 #include <boost/tokenizer.hpp>
32 bool hasEnv(string const & name)
34 return getenv(name.c_str());
38 string const getEnv(string const & name)
40 // f.ex. what about error checking?
41 char const * const ch = getenv(name.c_str());
42 return ch ? to_utf8(from_local8bit(ch)) : string();
46 vector<string> const getEnvPath(string const & name)
48 typedef boost::char_separator<char> Separator;
49 typedef boost::tokenizer<Separator> Tokenizer;
51 string const env_var = getEnv(name);
52 Separator const separator(string(1, os::path_separator()).c_str());
53 Tokenizer const tokens(env_var, separator);
54 Tokenizer::const_iterator it = tokens.begin();
55 Tokenizer::const_iterator const end = tokens.end();
58 for (; it != end; ++it)
59 vars.push_back(os::internal_path(*it));
65 bool setEnv(string const & name, string const & value)
67 // CHECK Look at and fix this.
68 // f.ex. what about error checking?
70 string const encoded = to_local8bit(from_utf8(value));
71 #if defined (HAVE_SETENV)
72 return ::setenv(name.c_str(), encoded.c_str(), 1) == 0;
73 #elif defined (HAVE_PUTENV)
74 // According to http://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html
75 // the argument of putenv() needs to be static, because changing its
76 // value will change the environment. Therefore we need a different static
77 // storage for each variable.
79 static map<string, string> varmap;
80 varmap[name] = name + '=' + encoded;
81 return ::putenv(const_cast<char*>(varmap[name].c_str())) == 0;
83 #error No environment-setting function has been defined.
89 void setEnvPath(string const & name, vector<string> const & env)
91 char const separator(os::path_separator());
93 vector<string>::const_iterator const begin = env.begin();
94 vector<string>::const_iterator const end = env.end();
95 vector<string>::const_iterator it = begin;
96 for (; it != end; ++it) {
99 ss << os::external_path(*it);
101 setEnv(name, ss.str());
105 void prependEnvPath(string const & name, string const & prefix)
107 vector<string> env_var = getEnvPath(name);
109 typedef boost::char_separator<char> Separator;
110 typedef boost::tokenizer<Separator> Tokenizer;
112 Separator const separator(string(1, os::path_separator()).c_str());
114 // Prepend each new element to the list, removing identical elements
115 // that occur later in the list.
116 Tokenizer const tokens(prefix, separator);
117 vector<string> reversed_tokens(tokens.begin(), tokens.end());
119 typedef vector<string>::const_reverse_iterator token_iterator;
120 token_iterator it = reversed_tokens.rbegin();
121 token_iterator const end = reversed_tokens.rend();
122 for (; it != end; ++it) {
123 vector<string>::iterator remove_it =
124 remove(env_var.begin(), env_var.end(), *it);
125 env_var.erase(remove_it, env_var.end());
126 env_var.insert(env_var.begin(), *it);
129 setEnvPath(name, env_var);
133 bool unsetEnv(string const & name)
135 #if defined(HAVE_UNSETENV)
136 // FIXME: does it leak?
137 return ::unsetenv(name.c_str()) == 0;
138 #elif defined(HAVE_PUTENV)
139 // This is OK with MSVC and MinGW at least.
140 // The argument of putenv() does not need to be a static variable in this
141 // case, since the variable is removed from the environment.
142 return ::putenv(const_cast<char*>((name + "=").c_str())) == 0;
144 #error No environment-unsetting function has been defined.
149 } // namespace support