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

Add method FilesystemPath::parent() #1287

Merged
merged 2 commits into from
Mar 13, 2025
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
8 changes: 8 additions & 0 deletions NAS2D/FilesystemPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ FilesystemPath FilesystemPath::operator/(const FilesystemPath& path) const
}


FilesystemPath FilesystemPath::parent() const
{
// Keep the trailing "/" as part of the folder name
// This is contrary to how <filesystem> behaves, which strips the trailing "/"
return ((std::filesystem::path{mPath} / "").parent_path().remove_filename()).string();
}


FilesystemPath FilesystemPath::stem() const
{
return std::filesystem::path{mPath}.stem().string();
Expand Down
1 change: 1 addition & 0 deletions NAS2D/FilesystemPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace NAS2D
bool operator<(const FilesystemPath& other) const;
FilesystemPath operator/(const FilesystemPath& path) const;

FilesystemPath parent() const;
FilesystemPath stem() const;
const std::string& string() const;
private:
Expand Down
24 changes: 24 additions & 0 deletions test/FilesystemPath.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ TEST(FilesystemPath, operatorSlash) {
EXPECT_EQ(NAS2D::FilesystemPath{"a/b/c/filename"}, path / "filename");
}

TEST(FilesystemPath, parentFoldersRelative) {
const auto path = NAS2D::FilesystemPath{"a/b/c/"};
EXPECT_EQ(NAS2D::FilesystemPath{"a/b/"}, path.parent());
EXPECT_EQ(NAS2D::FilesystemPath{"a/"}, path.parent().parent());
EXPECT_EQ(NAS2D::FilesystemPath{""}, path.parent().parent().parent());
}

TEST(FilesystemPath, parentFoldersAbsolute) {
const auto path = NAS2D::FilesystemPath{"/a/b/c/"};
EXPECT_EQ(NAS2D::FilesystemPath{"/a/b/"}, path.parent());
EXPECT_EQ(NAS2D::FilesystemPath{"/a/"}, path.parent().parent());
EXPECT_EQ(NAS2D::FilesystemPath{"/"}, path.parent().parent().parent());
}

TEST(FilesystemPath, parentFileRelative) {
const auto path = NAS2D::FilesystemPath{"a/b/c/filename"};
EXPECT_EQ(NAS2D::FilesystemPath{"a/b/c/"}, path.parent());
}

TEST(FilesystemPath, parentFileAbsolute) {
const auto path = NAS2D::FilesystemPath{"/a/b/c/filename"};
EXPECT_EQ(NAS2D::FilesystemPath{"/a/b/c/"}, path.parent());
}

TEST(FilesystemPath, stem) {
const auto path = NAS2D::FilesystemPath{"path/to/filename.ext"};
EXPECT_EQ(NAS2D::FilesystemPath{"filename"}, path.stem());
Expand Down