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/lstrings.h"
19 #include "support/os.h"
21 #include <algorithm> // for remove
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 string const env_var = getEnv(name);
49 string const separator(1, os::path_separator());
51 return getVectorFromString(env_var, separator);
55 bool setEnv(string const & name, string const & value)
57 // CHECK Look at and fix this.
58 // f.ex. what about error checking?
60 string const encoded = to_local8bit(from_utf8(value));
61 #if defined (HAVE_SETENV)
62 return ::setenv(name.c_str(), encoded.c_str(), 1) == 0;
63 #elif defined (HAVE_PUTENV)
64 // According to http://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html
65 // the argument of putenv() needs to be static, because changing its
66 // value will change the environment. Therefore we need a different static
67 // storage for each variable.
69 static map<string, string> varmap;
70 varmap[name] = name + '=' + encoded;
71 return ::putenv(const_cast<char*>(varmap[name].c_str())) == 0;
73 #error No environment-setting function has been defined.
79 void setEnvPath(string const & name, vector<string> const & env)
81 char const separator(os::path_separator());
83 vector<string>::const_iterator const begin = env.begin();
84 vector<string>::const_iterator const end = env.end();
85 vector<string>::const_iterator it = begin;
86 for (; it != end; ++it) {
89 ss << os::external_path(*it);
91 setEnv(name, ss.str());
95 void prependEnvPath(string const & name, string const & prefix)
97 string const separator(1, os::path_separator());
98 vector<string> reversed_tokens
99 = getVectorFromString(prefix, separator);
100 vector<string> env_var = getEnvPath(name);
102 // Prepend each new element to the list, removing identical elements
103 // that occur later in the list.
104 typedef vector<string>::const_reverse_iterator token_iterator;
105 token_iterator it = reversed_tokens.rbegin();
106 token_iterator const end = reversed_tokens.rend();
107 for (; it != end; ++it) {
108 vector<string>::iterator remove_it =
109 remove(env_var.begin(), env_var.end(), *it);
110 env_var.erase(remove_it, env_var.end());
111 env_var.insert(env_var.begin(), *it);
114 setEnvPath(name, env_var);
118 bool unsetEnv(string const & name)
120 #if defined(HAVE_UNSETENV)
121 // FIXME: does it leak?
122 return ::unsetenv(name.c_str()) == 0;
123 #elif defined(HAVE_PUTENV)
124 // This is OK with MSVC and MinGW at least.
125 // The argument of putenv() does not need to be a static variable in this
126 // case, since the variable is removed from the environment.
127 return ::putenv(const_cast<char*>((name + "=").c_str())) == 0;
129 #error No environment-unsetting function has been defined.
134 } // namespace support