Discussion:
Getting Environment Variables
Merrill Cornish
2006-01-19 20:13:28 UTC
Permalink
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
Aaron Griffin
2006-01-20 00:36:51 UTC
Permalink
Post by 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 "";
}
Sebastian Redl
2006-01-20 01:26:36 UTC
Permalink
Post by Aaron Griffin
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::optional<std::string> get_env(std::string const &name)
{
char *var = getenv(name.c_str());
if(var) return var;
else return boost::nothing;
}
Angus Leeming
2006-01-20 12:59:10 UTC
Permalink
Post by Sebastian Redl
Post by Aaron Griffin
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
boost::optional<std::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
Sebastian Redl
2006-01-20 13:47:15 UTC
Permalink
Post by Angus Leeming
Here's some GPL-ed code to set and get environment variables that works on
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
2006-01-20 14:19:06 UTC
Permalink
Another interesting addition would be replacing of referenced
environment variables within the variable - does getenv() do
that?
Sorry, I don't follow.

Angus
Sebastian Redl
2006-01-20 17:48:41 UTC
Permalink
Post by Angus Leeming
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
Stuart Dootson
2006-01-20 19:12:35 UTC
Permalink
Post by Sebastian Redl
Post by Angus Leeming
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/base/expandenvironmentstrings.asp)
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

Merrill Cornish
2006-01-20 02:13:30 UTC
Permalink
Sebastian,

boost::nothing sounds interesting, but I've never run across it before.
Where is it defined?

Merrill
Sebastian Redl
2006-01-20 03:55:43 UTC
Permalink
Post by Merrill Cornish
Sebastian,
boost::nothing sounds interesting, but I've never run across it before.
Where is it defined?
It's a wrong memory on my part. The Boost.Optional library defines
boost::none, which is a global instance of boost::none_t. Any
optional<T> can be instantiated from a none_t; the resulting object will
be empty.

http://www.boost.org/libs/optional/doc/optional.html -> Look for the
constructor optional<T>::optional( none_t )

Sebastian
Loading...