Skip to content

Commit

Permalink
Support absolute Win paths with forward slashes in common::FindFile (#…
Browse files Browse the repository at this point in the history
…389)

Signed-off-by: Silvio Traversaro <silvio@traversaro.it>
  • Loading branch information
traversaro authored Jul 11, 2022
1 parent edb3509 commit 038c7fa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/SystemPaths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -400,38 +400,43 @@ std::string SystemPaths::FindFile(const std::string &_filename,
if (filename.empty())
return path;

// Handle as URI
if (ignition::common::URI::Valid(filename))
{
path = this->FindFileURI(ignition::common::URI(filename));
}
// Handle as local absolute path
else if (filename[0] == '/')
{
path = filename;
}
#ifdef _WIN32
// Handle as Windows absolute path
else if (filename.length() >= 2 && filename[1] == ':')
// First of all, try if filename as a Windows absolute path exists
// The Windows absolute path is tried first as a Windows drive such as
// C:/ is also a valid URI scheme
if (filename.length() >= 2 && filename[1] == ':' && exists(filename))
{
path = filename;
}
#endif // _WIN32
// Try appending to local paths
else
{
auto cwdPath = joinPaths(cwd(), filename);
if (_searchLocalPath && exists(cwdPath))
// If the filename is not an existing absolute Windows path, try others
if (path.empty()) {
// Handle as URI
if (ignition::common::URI::Valid(filename))
{
path = cwdPath;
path = this->FindFileURI(ignition::common::URI(filename));
}
else if ((filename[0] == '.' || _searchLocalPath) && exists(filename))
// Handle as local absolute path
else if (filename[0] == '/')
{
path = filename;
}
else if (this->dataPtr->findFileCB)
// Try appending to local paths
else
{
path = this->dataPtr->findFileCB(filename);
auto cwdPath = joinPaths(cwd(), filename);
if (_searchLocalPath && exists(cwdPath))
{
path = cwdPath;
}
else if ((filename[0] == '.' || _searchLocalPath) && exists(filename))
{
path = filename;
}
else if (this->dataPtr->findFileCB)
{
path = this->dataPtr->findFileCB(filename);
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/SystemPaths_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,21 @@ TEST_F(SystemPathsFixture, FindFile)
EXPECT_EQ("", sp.FindFile(uriBadDir));
}

// Windows path with only one forward slash
#ifdef _WIN32
const auto tmpDirForwardSlash = "C:/Windows";
const auto homeDirForwardSlash = "C:/Users";
const auto badDirForwardSlash = "C:/bad";
{
// We do not expect equality as sp.FindFile could normalize the
// path and return a different string
// The behaviour tested here is just that C:/Windows, C:/Users are found
EXPECT_NE("", sp.FindFile(tmpDirForwardSlash));
EXPECT_NE("", sp.FindFile(homeDirForwardSlash));
EXPECT_EQ("", sp.FindFile(badDirForwardSlash));
}
#endif

// Custom callback
{
auto tmpCb = [tmpDir](const std::string &_s)
Expand Down

0 comments on commit 038c7fa

Please sign in to comment.