/** * \file environment.cpp * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * * \author Angus Leeming * \author João Luis M. Assirati * \author Lars Gullik Bjønnes * * Full author contact details are available in file CREDITS. */ #include #include "support/environment.h" #include "support/docstring.h" #include "support/os.h" #include #include #include #include #include using std::string; using std::vector; namespace lyx { namespace support { string const getEnv(string const & envname) { // f.ex. what about error checking? char const * const ch = getenv(envname.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(); std::vector vars; for (; it != end; ++it) vars.push_back(os::internal_path(*it)); return vars; } 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)); #if defined (HAVE_SETENV) return ::setenv(name.c_str(), encoded.c_str(), true); #elif defined (HAVE_PUTENV) static std::map > varmap; string envstr = name + '=' + encoded; boost::shared_array 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; #else #error No environment-setting function has been defined. #endif return false; } void setEnvPath(string const & name, vector const & env) { char const separator(os::path_separator()); std::ostringstream ss; vector::const_iterator const begin = env.begin(); vector::const_iterator const end = env.end(); vector::const_iterator it = begin; for (; it != end; ++it) { if (it != begin) ss << separator; ss << os::external_path(*it); } setEnv(name, ss.str()); } void prependEnvPath(string const & name, string const & prefix) { 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(); for (; it != end; ++it) { vector::iterator remove_it = std::remove(env_var.begin(), env_var.end(), *it); env_var.erase(remove_it, env_var.end()); env_var.insert(env_var.begin(), *it); } setEnvPath(name, env_var); } } // namespace support } // namespace lyx