Skip to content

Instantly share code, notes, and snippets.

@LesleyLai
Created November 11, 2022 09:19
Show Gist options
  • Save LesleyLai/c7674f9f4764b462453b2d8c10aa94c6 to your computer and use it in GitHub Desktop.
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.
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