Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an isRelativePath to determine if path is relative #312

Merged
merged 2 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/ignition/common/Filesystem.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ namespace ignition
/// \return True if _path is a file.
bool IGNITION_COMMON_VISIBLE isFile(const std::string &_path);

/// \brief Check if the given path is relative.
/// \param[in] _path Path.
/// \return True if _path is relative.
bool IGNITION_COMMON_VISIBLE isRelativePath(const std::string &_path);

/// \brief Create a new directory on the filesystem. Intermediate
/// directories must already exist.
/// \param[in] _path The new directory path to create
Expand Down
6 changes: 6 additions & 0 deletions src/Filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ bool ignition::common::isFile(const std::string &_path)
return fs::is_regular_file(_path);
}

/////////////////////////////////////////////////
bool ignition::common::isRelativePath(const std::string &_path)
{
return fs::path(_path).is_relative();
}

/////////////////////////////////////////////////
bool ignition::common::createDirectory(const std::string &_path)
{
Expand Down
14 changes: 14 additions & 0 deletions src/Filesystem_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ TEST_F(FilesystemTest, exists)
EXPECT_FALSE(isDirectory(absoluteSubdir));
}

/////////////////////////////////////////////////
TEST_F(FilesystemTest, relative)
{
#ifndef _WIN32
std::string absPath {"/tmp/fstest"};
std::string relPath {"../fstest"};
#else
std::string absPath {"C:\\Users\\user\\Desktop\\test.txt"};
std::string relPath {"user\\Desktop\\test.txt"};
#endif
EXPECT_FALSE(isRelativePath(absPath));
EXPECT_TRUE(isRelativePath(relPath));
}

// The symlink tests require special permissions to work on Windows,
// so they will be disabled by default. For more information, see:
// https://github.com/ignitionrobotics/ign-common/issues/21
Expand Down