]> git.lyx.org Git - lyx.git/blob - src/support/environment.cpp
DocBook: simplify code to handle abstracts.
[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 #include "support/debug.h"
21
22 #include <algorithm> // for remove
23 #include <cstdlib>
24 #include <map>
25 #include <sstream>
26
27 using namespace std;
28
29 namespace lyx {
30 namespace support {
31
32
33 bool hasEnv(string const & name)
34 {
35         return getenv(name.c_str());
36 }
37
38
39 string const getEnv(string const & name)
40 {
41         // f.ex. what about error checking?
42         char const * const ch = getenv(name.c_str());
43         return ch ? to_utf8(from_local8bit(ch)) : string();
44 }
45
46
47 vector<string> const getEnvPath(string const & name)
48 {
49         string const env_var = getEnv(name);
50         string const separator(1, os::path_separator());
51
52         return getVectorFromString(env_var, separator);
53 }
54
55
56 bool setEnv(string const & name, string const & value)
57 {
58         // CHECK Look at and fix this.
59         // f.ex. what about error checking?
60
61         string encoded;
62         try {
63                 encoded = to_local8bit(from_utf8(value));
64         } catch (...) {
65                 return false;
66         }
67
68 #if defined (HAVE_SETENV)
69         return ::setenv(name.c_str(), encoded.c_str(), 1) == 0;
70 #elif defined (HAVE_PUTENV)
71         // According to http://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html
72         // the argument of putenv() needs to be static, because changing its
73         // value will change the environment. Therefore we need a different static
74         // storage for each variable.
75         // FIXME THREAD
76         static map<string, string> varmap;
77         varmap[name] = name + '=' + encoded;
78         return ::putenv(const_cast<char*>(varmap[name].c_str())) == 0;
79 #else
80 #error No environment-setting function has been defined.
81 #endif
82         return false;
83 }
84
85
86 void setEnvPath(string const & name, vector<string> const & env)
87 {
88         char const separator(os::path_separator());
89         ostringstream ss;
90         vector<string>::const_iterator const begin = env.begin();
91         vector<string>::const_iterator const end = env.end();
92         vector<string>::const_iterator it = begin;
93         for (; it != end; ++it) {
94                 if (it != begin)
95                         ss << separator;
96                 ss << os::external_path(*it);
97         }
98         setEnv(name, ss.str());
99 }
100
101
102 void prependEnvPath(string const & name, string const & prefix)
103 {
104         string const separator(1, os::path_separator());
105         vector<string> reversed_tokens
106                 = getVectorFromString(prefix, separator);
107         vector<string> env_var = getEnvPath(name);
108
109         // Prepend each new element to the list, removing identical elements
110         // that occur later in the list.
111         LYXERR(Debug::INIT, "Prepending \"" << prefix << "\" to PATH");
112         typedef vector<string>::const_reverse_iterator token_iterator;
113         token_iterator it = reversed_tokens.rbegin();
114         token_iterator const end = reversed_tokens.rend();
115         for (; it != end; ++it) {
116                 vector<string>::iterator remove_it =
117                         remove(env_var.begin(), env_var.end(), *it);
118                 env_var.erase(remove_it, env_var.end());
119                 env_var.insert(env_var.begin(), *it);
120         }
121
122         setEnvPath(name, env_var);
123 }
124
125
126 bool unsetEnv(string const & name)
127 {
128 #if defined(HAVE_UNSETENV)
129         // FIXME: does it leak?
130         return ::unsetenv(name.c_str()) == 0;
131 #elif defined(HAVE_PUTENV)
132         // This is OK with MSVC and MinGW at least.
133         // The argument of putenv() does not need to be a static variable in this
134         // case, since the variable is removed from the environment.
135         return ::putenv(const_cast<char*>((name + "=").c_str())) == 0;
136 #else
137 #error No environment-unsetting function has been defined.
138 #endif
139 }
140
141
142 } // namespace support
143 } // namespace lyx