aseprite / laf

A C++ library to create desktop applications
https://aseprite.github.io/laf/
MIT License
274 stars 58 forks source link

Add get_relative_path() to base filesystem methods #96

Closed gui-marc closed 1 month ago

gui-marc commented 1 month ago

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:

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:

Related to https://github.com/aseprite/aseprite/pull/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 commented 1 month ago

clang-tidy review says "All clean, LGTM! :+1:"

aseprite-bot commented 1 month ago

clang-tidy review says "All clean, LGTM! :+1:"

dacap commented 1 month ago

Thanks for the PR and the fixes @gui-marc :+1: