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

findAncestor* path #2334

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/cinder/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ CI_API fs::path getDocumentsDirectory();
//! Removes all files beyond maxFileCount.
CI_API void limitDirectoryFileCount( const fs::path& directoryPath, size_t maxFileCount, std::function<bool(const fs::path&, const fs::path&)> sortFn = []( const fs::path& p1, const fs::path& p2 ) -> bool { return fs::last_write_time( p1 ) > fs::last_write_time( p2 ); } );

//! Searches upwards from \a start (file or directory) for an ancestor directory where \a relativeSearch exists, up to \a maxDepth levels; returns absolute path if found, otherwise empty path.
CI_API fs::path findAncestorDir( const fs::path& start, const fs::path& relativeSearch, int maxDepth = 10 );
//! Searches upwards from \a start (file or directory) for an ancestor file where \a relativeSearch exists, up to \a maxDepth levels; returns absolute path if found, otherwise empty path.
CI_API fs::path findAncestorFile( const fs::path& start, const fs::path& relativeSearch, int maxDepth = 10 );

//! Launches a path in a web browser
CI_API void launchWebBrowser( const Url &url );

Expand Down
24 changes: 24 additions & 0 deletions src/cinder/Utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ void limitDirectoryFileCount( const fs::path& directoryPath, size_t maxFileCount
}
}

fs::path findAncestorFile( const fs::path& start, const fs::path& relativeSearch, int maxDepth )
{
size_t parentCt = 0;
for( fs::path curPath = start; curPath.has_parent_path() && parentCt <= maxDepth; curPath = curPath.parent_path(), ++parentCt ) {
const fs::path testDir = curPath / relativeSearch;
if( fs::exists( testDir ) && fs::is_regular_file( testDir ) )
return testDir;
}

return fs::path();
}

fs::path findAncestorDir( const fs::path& start, const fs::path& relativeSearch, int maxDepth )
{
size_t parentCt = 0;
for( fs::path curPath = start; curPath.has_parent_path() && parentCt <= maxDepth; curPath = curPath.parent_path(), ++parentCt ) {
const fs::path testDir = curPath / relativeSearch;
if( fs::exists( testDir ) && fs::is_directory( testDir ) )
return testDir;
}

return fs::path();
}

void launchWebBrowser( const Url &url )
{
app::Platform::get()->launchWebBrowser( url );
Expand Down
18 changes: 4 additions & 14 deletions src/cinder/app/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "cinder/app/Platform.h"
#include "cinder/CinderAssert.h"
#include "cinder/Utilities.h"

#if defined( CINDER_COCOA )
#include "cinder/app/cocoa/PlatformCocoa.h"
Expand Down Expand Up @@ -145,20 +146,9 @@ const vector<fs::path>& Platform::getAssetDirectories() const

void Platform::findAndAddDefaultAssetPath()
{
// first search the local directory, then its parent, up to ASSET_SEARCH_DEPTH levels up
// check at least the app path, even if it has no parent directory
auto execPath = getExecutablePath();
size_t parentCt = 0;
for( fs::path curPath = execPath; curPath.has_parent_path() || ( curPath == execPath ); curPath = curPath.parent_path(), ++parentCt ) {
if( parentCt >= ASSET_SEARCH_DEPTH )
break;

const fs::path curAssetDir = curPath / fs::path( "assets" );
if( fs::exists( curAssetDir ) && fs::is_directory( curAssetDir ) ) {
addAssetDirectory( curAssetDir );
break;
}
}
fs::path assetDir = findAncestorDir( getExecutablePath(), "assets", ASSET_SEARCH_DEPTH );
if( ! assetDir.empty() )
addAssetDirectory( assetDir );
}

std::ostream& Platform::console()
Expand Down
Loading