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 get_relative_path() to base filesystem methods #96

Merged
merged 1 commit into from
Jun 18, 2024

Conversation

gui-marc
Copy link
Contributor

Feature Description

This PR introduces the get_relative_path function, which computes the relative path from a given base path to a specified file. The function signature is as follows:

std::string get_relative_path(const std::string& filename, const std::string& base_path);

Functionality

The get_relative_path function takes two parameters:

  • filename: The absolute path of the target file.
  • base_path: The absolute path of the base directory.

The function returns a string representing the relative path from base_path to filename. This is useful in scenarios where relative paths are needed for portability, readability, or other path manipulations.

Usage Examples

Below are some examples demonstrating how to use the get_relative_path function:

std::string filename = "/home/user/projects/my_project/src/main.cpp";
std::string base_path = "/home/user/projects/my_project";

std::string relative_path = get_relative_path(filename, base_path);

// Output: "src/main.cpp"
std::string filename = "/var/www/html/index.html";
std::string base_path = "/var/www/xpto";

std::string relative_path = get_relative_path(filename, base_path);

// Output: "../html/index.html"

Testing

Comprehensive tests have been added to ensure the correctness of the get_relative_path function. The tests cover various scenarios, including:

  • Standard use cases with different base and target paths.
  • Edge cases where the paths are identical.
  • Cases with paths involving different directory levels.

Related to aseprite/aseprite#4389

I agree that my contributions are licensed under the MIT License.
You can find a copy of this license at https://opensource.org/licenses/MIT

@aseprite-bot
Copy link
Collaborator

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Member

@dacap dacap left a comment

Choose a reason for hiding this comment

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

Thanks @gui-marc for this, just some minor changes requested.

base/fs.cpp Outdated Show resolved Hide resolved
base/fs.cpp Outdated Show resolved Hide resolved
base/fs.cpp Outdated Show resolved Hide resolved
@gui-marc gui-marc force-pushed the get-relative-path branch 3 times, most recently from b8c2591 to 6d0a79a Compare June 18, 2024 21:13
We can use this function to get the relative path between a base path
and a filename

Related to aseprite/aseprite#4389
@aseprite-bot
Copy link
Collaborator

clang-tidy review says "All clean, LGTM! 👍"

@dacap
Copy link
Member

dacap commented Jun 18, 2024

Thanks for the PR and the fixes @gui-marc 👍

@dacap dacap merged commit 36abe03 into aseprite:main Jun 18, 2024
8 checks passed
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.

3 participants