logic: Fixed FilePath to not expand environment variables in constructor
This commit is contained in:
@@ -26,14 +26,8 @@ void FileManager::setPaths(
|
||||
std::vector<std::string> sourceExtensions,
|
||||
std::vector<std::string> includeExtensions
|
||||
){
|
||||
for ( FilePath path : sourcePaths )
|
||||
{
|
||||
m_sourcePaths.push_back(path.expandEnvironmentVariables());
|
||||
}
|
||||
for ( FilePath path : headerPaths )
|
||||
{
|
||||
m_headerPaths.push_back(path.expandEnvironmentVariables());
|
||||
}
|
||||
m_sourcePaths = sourcePaths;
|
||||
m_headerPaths = headerPaths;
|
||||
m_sourceExtensions = sourceExtensions;
|
||||
m_includeExtensions = includeExtensions;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,14 +27,13 @@ public:
|
||||
FilePath relativeTo(const FilePath& other) const;
|
||||
FilePath concat(const FilePath& other) const;
|
||||
FilePath expandEnvironmentVariables() const;
|
||||
std::string expandEnvironmentVariables(const std::string & path) const;
|
||||
|
||||
std::string str() const;
|
||||
std::string fileName() const;
|
||||
|
||||
std::string extension() const;
|
||||
FilePath withoutExtension() const;
|
||||
FilePath replaceExtension(const std::string& extension);
|
||||
FilePath replaceExtension(const std::string& extension) const;
|
||||
bool hasExtension(const std::vector<std::string>& extensions) const;
|
||||
|
||||
bool operator==(const FilePath& other) const;
|
||||
@@ -42,10 +41,10 @@ public:
|
||||
bool operator<(const FilePath& other) const;
|
||||
|
||||
private:
|
||||
void init();
|
||||
|
||||
boost::filesystem::path m_path;
|
||||
bool m_exists;
|
||||
|
||||
mutable bool m_exists;
|
||||
mutable bool m_checkedExists;
|
||||
};
|
||||
|
||||
#endif // FILE_PATH_H
|
||||
|
||||
Reference in New Issue
Block a user