]> git.lyx.org Git - lyx.git/blob - src/support/environment.cpp
Add default output format for pLaTeX
[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 encoded;
61         try {
62                 encoded = to_local8bit(from_utf8(value));
63         } catch (...) {
64                 return false;
65         }
66
67 #if defined (HAVE_SETENV)
68         return ::setenv(name.c_str(), encoded.c_str(), 1) == 0;
69 #elif defined (HAVE_PUTENV)
70         // According to http://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html
71         // the argument of putenv() needs to be static, because changing its
72         // value will change the environment. Therefore we need a different static
73         // storage for each variable.
74         // FIXME THREAD
75         static map<string, string> varmap;
76         varmap[name] = name + '=' + encoded;
77         return ::putenv(const_cast<char*>(varmap[name].c_str())) == 0;
78 #else
79 #error No environment-setting function has been defined.
80 #endif
81         return false;
82 }
83
84
85 void setEnvPath(string const & name, vector<string> const & env)
86 {
87         char const separator(os::path_separator());
88         ostringstream ss;
89         vector<string>::const_iterator const begin = env.begin();
90         vector<string>::const_iterator const end = env.end();
91         vector<string>::const_iterator it = begin;
92         for (; it != end; ++it) {
93                 if (it != begin)
94                         ss << separator;
95                 ss << os::external_path(*it);
96         }
97         setEnv(name, ss.str());
98 }
99
100
101 void prependEnvPath(string const & name, string const & prefix)
102 {
103         string const separator(1, os::path_separator());
104         vector<string> reversed_tokens
105                 = getVectorFromString(prefix, separator);
106         vector<string> env_var = getEnvPath(name);
107
108         // Prepend each new element to the list, removing identical elements
109         // that occur later in the list.
110         typedef vector<string>::const_reverse_iterator token_iterator;
111         token_iterator it = reversed_tokens.rbegin();
112         token_iterator const end = reversed_tokens.rend();
113         for (; it != end; ++it) {
114                 vector<string>::iterator remove_it =
115                         remove(env_var.begin(), env_var.end(), *it);
116                 env_var.erase(remove_it, env_var.end());
117                 env_var.insert(env_var.begin(), *it);
118         }
119
120         setEnvPath(name, env_var);
121 }
122
123
124 bool unsetEnv(string const & name)
125 {
126 #if defined(HAVE_UNSETENV)
127         // FIXME: does it leak?
128         return ::unsetenv(name.c_str()) == 0;
129 #elif defined(HAVE_PUTENV)
130         // This is OK with MSVC and MinGW at least.
131         // The argument of putenv() does not need to be a static variable in this
132         // case, since the variable is removed from the environment.
133         return ::putenv(const_cast<char*>((name + "=").c_str())) == 0;
134 #else
135 #error No environment-unsetting function has been defined.
136 #endif
137 }
138
139
140 } // namespace support
141 } // namespace lyx