X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2Fenvironment.cpp;h=246e6afce235269b4a25e851ef835503fa7f057d;hb=c6b17b7094c42ff6bf96e3452f69023c724d15b7;hp=6deb23064d9a9d3d5a7ce9d4ae273152fdd1a0d1;hpb=81d863fd4188d55fd8139f7a9d328e88603d9aba;p=lyx.git diff --git a/src/support/environment.cpp b/src/support/environment.cpp index 6deb23064d..246e6afce2 100644 --- a/src/support/environment.cpp +++ b/src/support/environment.cpp @@ -15,10 +15,11 @@ #include "support/environment.h" #include "support/docstring.h" +#include "support/lstrings.h" #include "support/os.h" +#include "support/debug.h" -#include - +#include // for remove #include #include #include @@ -45,20 +46,10 @@ string const getEnv(string const & name) vector const getEnvPath(string const & name) { - typedef boost::char_separator Separator; - typedef boost::tokenizer Tokenizer; - string const env_var = getEnv(name); - Separator const separator(string(1, os::path_separator()).c_str()); - Tokenizer const tokens(env_var, separator); - Tokenizer::const_iterator it = tokens.begin(); - Tokenizer::const_iterator const end = tokens.end(); + string const separator(1, os::path_separator()); - vector vars; - for (; it != end; ++it) - vars.push_back(os::internal_path(*it)); - - return vars; + return getVectorFromString(env_var, separator); } @@ -67,10 +58,21 @@ bool setEnv(string const & name, string const & value) // CHECK Look at and fix this. // f.ex. what about error checking? - string const encoded = to_local8bit(from_utf8(value)); + string encoded; + try { + encoded = to_local8bit(from_utf8(value)); + } catch (...) { + return false; + } + #if defined (HAVE_SETENV) return ::setenv(name.c_str(), encoded.c_str(), 1) == 0; #elif defined (HAVE_PUTENV) + // 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 varmap; varmap[name] = name + '=' + encoded; return ::putenv(const_cast(varmap[name].c_str())) == 0; @@ -99,18 +101,14 @@ void setEnvPath(string const & name, vector const & env) void prependEnvPath(string const & name, string const & prefix) { + string const separator(1, os::path_separator()); + vector reversed_tokens + = getVectorFromString(prefix, separator); vector env_var = getEnvPath(name); - typedef boost::char_separator Separator; - typedef boost::tokenizer Tokenizer; - - Separator const separator(string(1, os::path_separator()).c_str()); - // Prepend each new element to the list, removing identical elements // that occur later in the list. - Tokenizer const tokens(prefix, separator); - vector reversed_tokens(tokens.begin(), tokens.end()); - + LYXERR(Debug::INIT, "Prepending \"" << prefix << "\" to PATH"); typedef vector::const_reverse_iterator token_iterator; token_iterator it = reversed_tokens.rbegin(); token_iterator const end = reversed_tokens.rend(); @@ -129,10 +127,12 @@ bool unsetEnv(string const & name) { #if defined(HAVE_UNSETENV) // FIXME: does it leak? - return unsetenv(name.c_str()) == 0; + return ::unsetenv(name.c_str()) == 0; #elif defined(HAVE_PUTENV) // This is OK with MSVC and MinGW at least. - putenv((name + "=").c_str()) == 0; + // 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((name + "=").c_str())) == 0; #else #error No environment-unsetting function has been defined. #endif