]> git.lyx.org Git - lyx.git/blobdiff - src/support/environment.cpp
improve language flag for Objective-C compiler call
[lyx.git] / src / support / environment.cpp
index 5bce59916d1a4b43101768eed3ce04a03f83d19c..ceaca60f547d86aebc21b295a424b4f08395fb13 100644 (file)
@@ -4,8 +4,8 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Angus Leeming
- * \author João Luis M. Assirati
- * \author Lars Gullik Bjønnes
+ * \author João Luis M. Assirati
+ * \author Lars Gullik Bjønnes
  *
  * Full author contact details are available in file CREDITS.
  */
 #include "support/os.h"
 
 #include <boost/tokenizer.hpp>
-#include <boost/shared_array.hpp>
 
 #include <cstdlib>
 #include <map>
 #include <sstream>
 
-using std::string;
-using std::vector;
-
+using namespace std;
 
 namespace lyx {
 namespace support {
 
-string const getEnv(string const & envname)
+
+bool hasEnv(string const & name)
+{
+       return getenv(name.c_str());
+}
+
+
+string const getEnv(string const & name)
 {
        // f.ex. what about error checking?
-       char const * const ch = getenv(envname.c_str());
+       char const * const ch = getenv(name.c_str());
        return ch ? to_utf8(from_local8bit(ch)) : string();
 }
 
@@ -50,7 +54,7 @@ vector<string> const getEnvPath(string const & name)
        Tokenizer::const_iterator it = tokens.begin();
        Tokenizer::const_iterator const end = tokens.end();
 
-       std::vector<string> vars;
+       vector<string> vars;
        for (; it != end; ++it)
                vars.push_back(os::internal_path(*it));
 
@@ -65,18 +69,16 @@ bool setEnv(string const & name, string const & value)
 
        string const encoded = to_local8bit(from_utf8(value));
 #if defined (HAVE_SETENV)
-       return ::setenv(name.c_str(), encoded.c_str(), true);
+       return ::setenv(name.c_str(), encoded.c_str(), 1) == 0;
 #elif defined (HAVE_PUTENV)
-       static std::map<string, boost::shared_array<char> > varmap;
-
-       string envstr = name + '=' + encoded;
-       boost::shared_array<char> newptr(new char[envstr.size() + 1]);
-       envstr.copy(newptr.get(), envstr.length());
-       newptr.get()[envstr.length()] = '\0';
-       bool const retval = ::putenv(newptr.get()) == 0;
-
-       varmap[name] = newptr;
-       return retval;
+       // According to http://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html
+       // the argument of putenv() needs to be static, because changing its
+       // value will change the environment. Therefore we need a different static
+       // storage for each variable.
+       // FIXME THREAD
+       static map<string, string> varmap;
+       varmap[name] = name + '=' + encoded;
+       return ::putenv(const_cast<char*>(varmap[name].c_str())) == 0;
 #else
 #error No environment-setting function has been defined.
 #endif
@@ -87,7 +89,7 @@ bool setEnv(string const & name, string const & value)
 void setEnvPath(string const & name, vector<string> const & env)
 {
        char const separator(os::path_separator());
-       std::ostringstream ss;
+       ostringstream ss;
        vector<string>::const_iterator const begin = env.begin();
        vector<string>::const_iterator const end = env.end();
        vector<string>::const_iterator it = begin;
@@ -119,7 +121,7 @@ void prependEnvPath(string const & name, string const & prefix)
        token_iterator const end = reversed_tokens.rend();
        for (; it != end; ++it) {
                vector<string>::iterator remove_it =
-                       std::remove(env_var.begin(), env_var.end(), *it);
+                       remove(env_var.begin(), env_var.end(), *it);
                env_var.erase(remove_it, env_var.end());
                env_var.insert(env_var.begin(), *it);
        }
@@ -127,5 +129,22 @@ void prependEnvPath(string const & name, string const & prefix)
        setEnvPath(name, env_var);
 }
 
+
+bool unsetEnv(string const & name)
+{
+#if defined(HAVE_UNSETENV)
+       // FIXME: does it leak?
+       return ::unsetenv(name.c_str()) == 0;
+#elif defined(HAVE_PUTENV)
+       // This is OK with MSVC and MinGW at least.
+       // The argument of putenv() does not need to be a static variable in this
+       // case, since the variable is removed from the environment.
+       return ::putenv(const_cast<char*>((name + "=").c_str())) == 0;
+#else
+#error No environment-unsetting function has been defined.
+#endif
+}
+
+
 } // namespace support
 } // namespace lyx