Move path simplifying code to Path class.
This commit is contained in:
parent
0485976c54
commit
dc77bc69a0
|
@ -219,32 +219,7 @@ std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMes
|
|||
std::string ErrorLogger::ErrorMessage::FileLocation::getfile(bool convert) const
|
||||
{
|
||||
std::string f(_file);
|
||||
|
||||
// replace "/ab/../" with "/"..
|
||||
std::string::size_type pos = 0;
|
||||
while ((pos = f.find("..", pos + 1)) != std::string::npos)
|
||||
{
|
||||
// position must be at least 4..
|
||||
if (pos < 4)
|
||||
continue;
|
||||
|
||||
// Previous char must be a separator..
|
||||
if (f[pos-1] != '/')
|
||||
continue;
|
||||
|
||||
// Next char must be a separator..
|
||||
if (f[pos+2] != '/')
|
||||
continue;
|
||||
|
||||
// Locate previous separator..
|
||||
std::string::size_type sep = f.find_last_of("/", pos - 2);
|
||||
if (sep == std::string::npos)
|
||||
continue;
|
||||
|
||||
// Delete substring..
|
||||
f.erase(sep, pos + 2 - sep);
|
||||
pos = sep;
|
||||
}
|
||||
f = Path::simplifyPath(f);
|
||||
|
||||
if (convert)
|
||||
f = Path::toNativeSeparators(f);
|
||||
|
|
32
lib/path.cpp
32
lib/path.cpp
|
@ -41,3 +41,35 @@ std::string Path::fromNativeSeparators(const std::string &path)
|
|||
std::replace(modified.begin(), modified.end(), nonnative, newsepar);
|
||||
return modified;
|
||||
}
|
||||
|
||||
std::string Path::simplifyPath(const std::string &path)
|
||||
{
|
||||
std::string f(path);
|
||||
// replace "/ab/../" with "/"..
|
||||
std::string::size_type pos = 0;
|
||||
while ((pos = f.find("..", pos + 1)) != std::string::npos)
|
||||
{
|
||||
// position must be at least 4..
|
||||
if (pos < 4)
|
||||
continue;
|
||||
|
||||
// Previous char must be a separator..
|
||||
if (f[pos-1] != '/')
|
||||
continue;
|
||||
|
||||
// Next char must be a separator..
|
||||
if (f[pos+2] != '/')
|
||||
continue;
|
||||
|
||||
// Locate previous separator..
|
||||
std::string::size_type sep = f.find_last_of("/", pos - 2);
|
||||
if (sep == std::string::npos)
|
||||
continue;
|
||||
|
||||
// Delete substring..
|
||||
f.erase(sep, pos + 2 - sep);
|
||||
pos = sep;
|
||||
}
|
||||
|
||||
return f;
|
||||
}
|
|
@ -47,6 +47,14 @@ public:
|
|||
* @return converted path.
|
||||
*/
|
||||
static std::string fromNativeSeparators(const std::string &path);
|
||||
|
||||
/**
|
||||
* Simplify given path.
|
||||
* This method simplifies the path removing reduntant parts.
|
||||
* @param path Path string to simplify.
|
||||
* @return simplified path.
|
||||
*/
|
||||
static std::string simplifyPath(const std::string &path);
|
||||
};
|
||||
|
||||
/// @}
|
||||
|
|
Loading…
Reference in New Issue