Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
factor env var parsing into common module
  • Loading branch information
jjallaire committed Sep 27, 2011
1 parent ec3eb31 commit e5b22cf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/cpp/core/include/core/system/Environment.hpp
Expand Up @@ -60,6 +60,12 @@ void unsetenv(const std::string& name, Options* pEnvironment);
void addToPath(const std::string& filePath, Options* pEnvironment);


/****************************************************************
Utility functions
*****************************************************************/

bool parseEnvVar(const std::string envVar, Option* pEnvVar);

} // namespace system
} // namespace core

Expand Down
17 changes: 17 additions & 0 deletions src/cpp/core/system/Environment.cpp
Expand Up @@ -72,6 +72,23 @@ void addToPath(const std::string& filePath, Options* pEnvironment)
setenv("PATH", path, pEnvironment);
}

bool parseEnvVar(const std::string envVar, Option* pEnvVar)
{
std::string::size_type pos = envVar.find("=") ;
if ( pos != std::string::npos )
{
std::string key = envVar.substr(0, pos) ;
std::string value;
if ( (pos + 1) < envVar.size() )
value = envVar.substr(pos + 1) ;
*pEnvVar = std::make_pair(key,value);
return true;
}
else
{
return false;
}
}

} // namespace system
} // namespace core
13 changes: 3 additions & 10 deletions src/cpp/core/system/PosixEnvironment.cpp
Expand Up @@ -25,16 +25,9 @@ Options environment()
Options options;
for (char **env = environ; *env; ++env)
{
std::string envVar(*env);
std::string::size_type pos = envVar.find("=") ;
if ( pos != std::string::npos )
{
std::string key = envVar.substr(0, pos) ;
std::string value;
if ( (pos + 1) < envVar.size() )
value = envVar.substr(pos + 1) ;
options.push_back(std::make_pair(key,value));
}
Option envVar;
if (parseEnvVar(std::string(*env), &envVar))
options.push_back(envVar);
}

return options;
Expand Down

0 comments on commit e5b22cf

Please sign in to comment.