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 base::get_absolute_path_from_relative() function #119

Closed
wants to merge 1 commit into from

Conversation

Gasparoken
Copy link
Member

Part of aseprite/aseprite#4851

It's wanted to reconstruct the absolute path starting from the relative/absolute filename in the filename entry of 'Export As' dialog.

Copy link
Collaborator

@aseprite-bot aseprite-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

// If the first element is empty, it means that the relative
// file name starts with a separator and it also means that
// it's an absolute address.
if (relativeParts.size() >= 1 && relativeParts[0] == "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]

Suggested change
if (relativeParts.size() >= 1 && relativeParts[0] == "")
if (!relativeParts.empty() && relativeParts[0] == "")
Additional context

/usr/include/c++/13/bits/stl_vector.h:1087: method 'vector'::empty() defined here

      empty() const _GLIBCXX_NOEXCEPT
      ^

// If the first element is empty, it means that the relative
// file name starts with a separator and it also means that
// it's an absolute address.
if (relativeParts.size() >= 1 && relativeParts[0] == "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: the 'empty' method should be used to check for emptiness instead of comparing to an empty object [readability-container-size-empty]

Suggested change
if (relativeParts.size() >= 1 && relativeParts[0] == "")
if (relativeParts.size() >= 1 && relativeParts[0].empty())
Additional context

/usr/include/c++/13/bits/basic_string.h:1219: method 'basic_string'::empty() defined here

      empty() const _GLIBCXX_NOEXCEPT
      ^

This addition is part of the solution for aseprite/aseprite#4851
which was a bug introduced in aseprite/asseprite#4389.
@dacap
Copy link
Member

dacap commented Dec 16, 2024

Hi @Gasparoken, just in case, is possible that base::get_absolute_path() does the job? (it removes ".." elements, etc.)

@Gasparoken
Copy link
Member Author

Gasparoken commented Dec 16, 2024

base::get_absolute_path() uses the current folder instead of an arbitrary base_path. That's why I implemented this new function. But maybe base::get_absolute_path() is enough and I just need to change the approach in Aseprite (particularly on std::string ExportFileWindow::outputFilenameValue() const). Let me investigate.

@dacap
Copy link
Member

dacap commented Dec 16, 2024

base::get_absolute_path() uses the current folder instead of an arbitrary base_path

It should work as a path normalizer if the given path is an absolute path; you can check out the tests:

laf/base/fs_tests.cpp

Lines 246 to 250 in c84b89b

#if LAF_WINDOWS
EXPECT_EQ("C:\\file", get_absolute_path("C:/path/../file"));
#else
EXPECT_EQ("/file", get_absolute_path("/path/../file"));
#endif

So you should be able to use something like base::get_absolute_path(base::join_path(base_path, relative_path)). Other way is using base::normalize_path() directly.

@Gasparoken
Copy link
Member Author

That's great! However, I found that base::get_absolute_path() can only handle one "../", if there are two or more "../", the function does not get to the correct folder level. Therefore, the following line fails:

EXPECT_EQ("/user/file", get_absolute_path("/user/name/path/../../file"));

I'll adopt this function to resolve aseprite/aseprite#4851, but I will need to fix the indicated bug first.

@Gasparoken
Copy link
Member Author

I'm closing this and will take the base::get_absolute_path() approach instead.

@Gasparoken Gasparoken closed this Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants