]> git.lyx.org Git - lyx.git/blob - src/support/environment.cpp
Remove non-copyable idioms
[lyx.git] / src / support / environment.cpp
1 /**
2  * \file environment.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author João Luis M. Assirati
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "support/environment.h"
16
17 #include "support/docstring.h"
18 #include "support/lstrings.h"
19 #include "support/os.h"
20
21 #include <algorithm> // for remove
22 #include <cstdlib>
23 #include <map>
24 #include <sstream>
25
26 using namespace std;
27
28 namespace lyx {
29 namespace support {
30
31
32 bool hasEnv(string const & name)
33 {
34         return getenv(name.c_str());
35 }
36
37
38 string const getEnv(string const & name)
39 {
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();
43 }
44
45
46 vector<string> const getEnvPath(string const & name)
47 {
48         string const env_var = getEnv(name);
49         string const separator(1, os::path_separator());
50
51         return getVectorFromString(env_var, separator);
52 }
53
54
55 bool setEnv(string const & name, string const & value)
56 {
57         // CHECK Look at and fix this.
58         // f.ex. what about error checking?
59
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.
68         // FIXME THREAD
69         static map<string, string> varmap;
70         varmap[name] = name + '=' + encoded;
71         return ::putenv(const_cast<char*>(varmap[name].c_str())) == 0;
72 #else
73 #error No environment-setting function has been defined.
74 #endif
75         return false;
76 }
77
78
79 void setEnvPath(string const & name, vector<string> const & env)
80 {
81         char const separator(os::path_separator());
82         ostringstream ss;
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) {
87                 if (it != begin)
88                         ss << separator;
89                 ss << os::external_path(*it);
90         }
91         setEnv(name, ss.str());
92 }
93
94
95 void prependEnvPath(string const & name, string const & prefix)
96 {
97         string const separator(1, os::path_separator());
98         vector<string> reversed_tokens
99                 = getVectorFromString(prefix, separator);
100         vector<string> env_var = getEnvPath(name);
101
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);
112         }
113
114         setEnvPath(name, env_var);
115 }
116
117
118 bool unsetEnv(string const & name)
119 {
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;
128 #else
129 #error No environment-unsetting function has been defined.
130 #endif
131 }
132
133
134 } // namespace support
135 } // namespace lyx