X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2Fenvironment.cpp;h=091fa278c015cfa4bc204f2d3b8d2271151a3fcc;hb=bf56e2c8e1afa857cd5e313c19948040e41b8227;hp=a364725c21173c137c00960c0ba3ea2cc3bb9270;hpb=b0edde64a0b480861cf84e9bad5cfafd0d525a8b;p=lyx.git diff --git a/src/support/environment.cpp b/src/support/environment.cpp index a364725c21..091fa278c0 100644 --- a/src/support/environment.cpp +++ b/src/support/environment.cpp @@ -15,10 +15,10 @@ #include "support/environment.h" #include "support/docstring.h" +#include "support/lstrings.h" #include "support/os.h" -#include - +#include // for remove #include #include #include @@ -28,30 +28,27 @@ 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(); } 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); } @@ -64,6 +61,11 @@ bool setEnv(string const & name, string const & value) #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; @@ -92,18 +94,13 @@ 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()); - typedef vector::const_reverse_iterator token_iterator; token_iterator it = reversed_tokens.rbegin(); token_iterator const end = reversed_tokens.rend(); @@ -117,5 +114,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((name + "=").c_str())) == 0; +#else +#error No environment-unsetting function has been defined. +#endif +} + + } // namespace support } // namespace lyx