Created
November 11, 2022 09:19
-
-
Save LesleyLai/c7674f9f4764b462453b2d8c10aa94c6 to your computer and use it in GitHub Desktop.
Little C++ utility to searching upward until finding a "assets" subdirectory. Useful during development when I don't want to copy the assets to build directory every time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto locate_asset_path(const std::filesystem::path& current_path) | |
-> std::optional<std::filesystem::path> | |
{ | |
namespace fs = std::filesystem; | |
std::optional<fs::path> result = std::nullopt; | |
for (auto path = current_path; path != current_path.root_path(); | |
path = path.parent_path()) { | |
const auto assets_path = path / "assets"; | |
if (exists(assets_path) && is_directory(assets_path)) { | |
result = assets_path; | |
} | |
} | |
return result.has_value() ? std::optional{absolute(*result)} : std::nullopt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment