logic: Fixed FilePath to not expand environment variables in constructor

This commit is contained in:
Eberhard Graether
2016-04-19 00:13:02 +02:00
parent a57f24b2b2
commit 7cd38f8e56
13 changed files with 92 additions and 73 deletions
+20 -24
View File
@@ -1,32 +1,34 @@
#include "utility/file/FilePath.h"
#include <regex>
#include "utility/logging/logging.h"
FilePath::FilePath()
: m_exists(false)
, m_checkedExists(false)
{
}
FilePath::FilePath(const char* filePath)
: m_path(filePath)
, m_exists(false)
, m_checkedExists(false)
{
init();
}
FilePath::FilePath(const std::string& filePath)
: m_path(filePath)
, m_exists(false)
, m_checkedExists(false)
{
init();
}
FilePath::FilePath(const boost::filesystem::path& filePath)
: m_path(filePath)
, m_exists(false)
, m_checkedExists(false)
{
init();
}
boost::filesystem::path FilePath::path() const
@@ -41,6 +43,12 @@ bool FilePath::empty() const
bool FilePath::exists() const
{
if (!m_checkedExists)
{
m_exists = boost::filesystem::exists(m_path);
m_checkedExists = true;
}
return m_exists;
}
@@ -103,25 +111,22 @@ FilePath FilePath::canonical() const
FilePath FilePath::expandEnvironmentVariables() const
{
return FilePath(expandEnvironmentVariables(str()));
}
std::string text = str();
std::string FilePath::expandEnvironmentVariables(const std::string& path) const
{
std::string text = path;
static std::regex env( "\\$\\{([^}]+)\\}|%([0-9A-Za-z\\/]*)%" );
static std::regex env("\\$\\{([^}]+)\\}|%([0-9A-Za-z\\/]*)%");
std::smatch match;
while ( std::regex_search( text, match, env ) ) {
const char * s = getenv( match[1].str().c_str() );
while (std::regex_search(text, match, env))
{
const char * s = getenv(match[1].str().c_str());
if (s == nullptr)
{
LOG_ERROR(match[1].str() + " is no a environment variable");
return path;
return FilePath();
}
text.replace( match[0].first, match[0].second, s);
}
return text;
return FilePath(text);
}
FilePath FilePath::relativeTo(const FilePath& other) const
@@ -188,7 +193,7 @@ FilePath FilePath::withoutExtension() const
return FilePath(boost::filesystem::path(m_path).replace_extension());
}
FilePath FilePath::replaceExtension(const std::string& extension)
FilePath FilePath::replaceExtension(const std::string& extension) const
{
return FilePath(boost::filesystem::path(m_path).replace_extension(extension));
}
@@ -225,12 +230,3 @@ bool FilePath::operator<(const FilePath& other) const
{
return m_path.compare(other.m_path) < 0;
}
void FilePath::init()
{
boost::filesystem::path p(expandEnvironmentVariables(m_path.generic_string()));
if (boost::filesystem::exists(p))
{
m_exists = true;
}
}