cmOutputConverter: split ConvertToRelativePath

Split the ConvertToRelativePath funtion into ConvertToRelativePath and
ForceToRelativePath.  Both functions take the local path as a string
rather than a vector of path segments.  Reimplement the old interface on
top of the two new functions for interface compatibility.
This commit is contained in:
Daniel Pfeifer 2016-06-16 21:37:57 +02:00 committed by Stephen Kelly
parent 00fd64d126
commit 149af87b86
2 changed files with 69 additions and 25 deletions

View File

@ -128,41 +128,67 @@ std::string cmOutputConverter::ConvertToRelativePath(
const std::vector<std::string>& local, const std::string& in_remote, const std::vector<std::string>& local, const std::string& in_remote,
bool force) const bool force) const
{ {
// The path should never be quoted. std::string local_path = cmSystemTools::JoinPath(local);
assert(in_remote[0] != '\"'); return force ? this->ForceToRelativePath(local_path, in_remote)
: this->ConvertToRelativePath(local_path, in_remote);
}
std::string cmOutputConverter::ConvertToRelativePath(
std::string const& local_path, std::string const& remote_path) const
{
// The paths should never be quoted.
assert(local_path[0] != '\"');
assert(remote_path[0] != '\"');
// The local path should never have a trailing slash. // The local path should never have a trailing slash.
assert(!local.empty() && !(local[local.size() - 1] == "")); assert(local_path.empty() || local_path[local_path.size() - 1] != '/');
// If the path is already relative then just return the path. // If the path is already relative then just return the path.
if (!cmSystemTools::FileIsFullPath(in_remote.c_str())) { if (!cmSystemTools::FileIsFullPath(remote_path.c_str())) {
return in_remote; return remote_path;
} }
if (!force) { // Skip conversion if the path and local are not both in the source
// Skip conversion if the path and local are not both in the source // or both in the binary tree.
// or both in the binary tree. if (!((cmOutputConverterNotAbove(
std::string local_path = cmSystemTools::JoinPath(local); local_path.c_str(),
if (!((cmOutputConverterNotAbove( this->StateSnapshot.GetDirectory().GetRelativePathTopBinary()) &&
local_path.c_str(), cmOutputConverterNotAbove(
this->StateSnapshot.GetDirectory().GetRelativePathTopBinary()) && remote_path.c_str(),
cmOutputConverterNotAbove( this->StateSnapshot.GetDirectory().GetRelativePathTopBinary())) ||
in_remote.c_str(), (cmOutputConverterNotAbove(
this->StateSnapshot.GetDirectory().GetRelativePathTopBinary())) || local_path.c_str(),
(cmOutputConverterNotAbove( this->StateSnapshot.GetDirectory().GetRelativePathTopSource()) &&
local_path.c_str(), cmOutputConverterNotAbove(
this->StateSnapshot.GetDirectory().GetRelativePathTopSource()) && remote_path.c_str(),
cmOutputConverterNotAbove(in_remote.c_str(), this->StateSnapshot.GetDirectory().GetRelativePathTopSource())))) {
this->StateSnapshot.GetDirectory() return remote_path;
.GetRelativePathTopSource())))) { }
return in_remote;
} return this->ForceToRelativePath(local_path, remote_path);
}
std::string cmOutputConverter::ForceToRelativePath(
std::string const& local_path, std::string const& remote_path)
{
// The paths should never be quoted.
assert(local_path[0] != '\"');
assert(remote_path[0] != '\"');
// The local path should never have a trailing slash.
assert(local_path.empty() || local_path[local_path.size() - 1] != '/');
// If the path is already relative then just return the path.
if (!cmSystemTools::FileIsFullPath(remote_path.c_str())) {
return remote_path;
} }
// Identify the longest shared path component between the remote // Identify the longest shared path component between the remote
// path and the local path. // path and the local path.
std::vector<std::string> local;
cmSystemTools::SplitPath(local_path, local);
std::vector<std::string> remote; std::vector<std::string> remote;
cmSystemTools::SplitPath(in_remote, remote); cmSystemTools::SplitPath(remote_path, remote);
unsigned int common = 0; unsigned int common = 0;
while (common < remote.size() && common < local.size() && while (common < remote.size() && common < local.size() &&
cmSystemTools::ComparePath(remote[common], local[common])) { cmSystemTools::ComparePath(remote[common], local[common])) {
@ -171,7 +197,7 @@ std::string cmOutputConverter::ConvertToRelativePath(
// If no part of the path is in common then return the full path. // If no part of the path is in common then return the full path.
if (common == 0) { if (common == 0) {
return in_remote; return remote_path;
} }
// If the entire path is in common then just return a ".". // If the entire path is in common then just return a ".".

View File

@ -145,6 +145,24 @@ public:
const std::string& in_remote, const std::string& in_remote,
bool force = false) const; bool force = false) const;
/**
* Convert the given remote path to a relative path with respect to
* the given local path. Both paths must use forward slashes and not
* already be escaped or quoted.
* The conversion is skipped if the paths are not both in the source
* or both in the binary tree.
*/
std::string ConvertToRelativePath(std::string const& local_path,
std::string const& remote_path) const;
/**
* Convert the given remote path to a relative path with respect to
* the given local path. Both paths must use forward slashes and not
* already be escaped or quoted.
*/
static std::string ForceToRelativePath(std::string const& local_path,
std::string const& remote_path);
private: private:
cmState* GetState() const; cmState* GetState() const;