Skip to content

Commit

Permalink
findAncestor* path (cinder#2334)
Browse files Browse the repository at this point in the history
* Adding findAncestorFile() and findAncestorDir() to Utilities

* Reimplementing findAndAddDefaultAssetPath() in terms of findAncestorDir()

* Changing std::filesystem::path to fs::path to accommodate macOS

* Start searching for default asset dir in executable path itself
  • Loading branch information
andrewfb authored Jul 30, 2024
1 parent 51c39a5 commit 9ee16dd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
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

0 comments on commit 9ee16dd

Please sign in to comment.