Getting Environment Variables
Is there a Boost way to get environment variables? I _thought_ I had seen a reference to that in the program options libarary, but I can't find it now. Merrill
On 1/19/06, Merrill Cornish
Is there a Boost way to get environment variables? I _thought_ I had seen a reference to that in the program options libarary, but I can't find it now.
Merrill
How about: std::string get_env(std::string const& name) { char* var = getenv(name.c_str()); //getenv can return NULL, std::string(NULL) throws if(var) return var; else return ""; }
Aaron Griffin wrote:
std::string get_env(std::string const& name) { char* var = getenv(name.c_str()); //getenv can return NULL, std::string(NULL) throws if(var) return var; else return ""; }
An empty environment variable is not exactly the same as a non-existent one. Perhaps like this: boost::optionalstd::string get_env(std::string const &name) { char *var = getenv(name.c_str()); if(var) return var; else return boost::nothing; }
Sebastian Redl wrote:
Aaron Griffin wrote:
std::string get_env(std::string const& name) { char* var = getenv(name.c_str()); //getenv can return NULL, std::string(NULL) throws if(var) return var; else return ""; }
An empty environment variable is not exactly the same as a non-existent one. Perhaps like this: boost::optionalstd::string get_env(std::string const &name) { char *var = getenv(name.c_str()); if(var) return var; else return boost::nothing; }
Here's some GPL-ed code to set and get environment variables that works on both *nix and Windows: http://www.lyx.org/cgi-bin/viewcvs.cgi/lyx-devel/src/support/environment.C?rev=HEAD&content-type=text/vnd.viewcvs-markup Equivalent url: http://tinyurl.co.uk/o5jo Of course, as Sebastian points out, boost::optional would be more elegant/correct for the getenv wrapper. HTH, Angus
Angus Leeming wrote:
Here's some GPL-ed code to set and get environment variables that works on both *nix and Windows:
http://www.lyx.org/cgi-bin/viewcvs.cgi/lyx-devel/src/support/environment.C?rev=HEAD&content-type=text/vnd.viewcvs-markup Equivalent url: http://tinyurl.co.uk/o5jo
That's nice. Another interesting addition would be replacing of referenced environment variables within the variable - does getenv() do that? Sebastian Redl
Angus Leeming wrote:
Sebastian Redl wrote:
Another interesting addition would be replacing of referenced environment variables within the variable - does getenv() do that?
Sorry, I don't follow.
Angus
Especially in Windows, many environment variables contain a sequence %something%, which tells the shell to substitute this sequence with the environment variable something. An example is the PATH variable, which contains, I believe, %WINDIR% as an early entry. I wonder whether the Win32 implementation of getenv() does this, or if it should be done by an external function. Sebastian Redl
On 1/20/06, Sebastian Redl
Angus Leeming wrote:
Sebastian Redl wrote:
Another interesting addition would be replacing of referenced environment variables within the variable - does getenv() do that?
Sorry, I don't follow.
Angus
Especially in Windows, many environment variables contain a sequence %something%, which tells the shell to substitute this sequence with the environment variable something. An example is the PATH variable, which contains, I believe, %WINDIR% as an early entry. I wonder whether the Win32 implementation of getenv() does this, or if it should be done by an external function.
Sebastian Redl
Well, ExpandEnvironmentStrings (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/bas...) does what you want. A cursory look at the man pages on my iBook doesn't reveal anything obviously similar for OS X (and, I suspect, other Unix based operating systems). In addition, a Boost implementation would likely have to cater for the two flavours of embedded environment string, i.e. "%WINDIR%\some_file" as used in Windows and "$HOME/.bashrc" in Unix. And then, do you want to support the various expansion operations the two OSs give you. And what about other OSs like VMS... Anyway - there's a solution under Windows... Stuart Dootson
participants (5)
-
Aaron Griffin
-
Angus Leeming
-
Merrill Cornish
-
Sebastian Redl
-
Stuart Dootson