Skip to content

Commit 9ee16dd

Browse files
authored
findAncestor* path (#2334)
* 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
1 parent 51c39a5 commit 9ee16dd

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

include/cinder/Utilities.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ CI_API fs::path getDocumentsDirectory();
4747
//! Removes all files beyond maxFileCount.
4848
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 ); } );
4949

50+
//! 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.
51+
CI_API fs::path findAncestorDir( const fs::path& start, const fs::path& relativeSearch, int maxDepth = 10 );
52+
//! 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.
53+
CI_API fs::path findAncestorFile( const fs::path& start, const fs::path& relativeSearch, int maxDepth = 10 );
54+
5055
//! Launches a path in a web browser
5156
CI_API void launchWebBrowser( const Url &url );
5257

src/cinder/Utilities.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,30 @@ void limitDirectoryFileCount( const fs::path& directoryPath, size_t maxFileCount
7878
}
7979
}
8080

81+
fs::path findAncestorFile( const fs::path& start, const fs::path& relativeSearch, int maxDepth )
82+
{
83+
size_t parentCt = 0;
84+
for( fs::path curPath = start; curPath.has_parent_path() && parentCt <= maxDepth; curPath = curPath.parent_path(), ++parentCt ) {
85+
const fs::path testDir = curPath / relativeSearch;
86+
if( fs::exists( testDir ) && fs::is_regular_file( testDir ) )
87+
return testDir;
88+
}
89+
90+
return fs::path();
91+
}
92+
93+
fs::path findAncestorDir( const fs::path& start, const fs::path& relativeSearch, int maxDepth )
94+
{
95+
size_t parentCt = 0;
96+
for( fs::path curPath = start; curPath.has_parent_path() && parentCt <= maxDepth; curPath = curPath.parent_path(), ++parentCt ) {
97+
const fs::path testDir = curPath / relativeSearch;
98+
if( fs::exists( testDir ) && fs::is_directory( testDir ) )
99+
return testDir;
100+
}
101+
102+
return fs::path();
103+
}
104+
81105
void launchWebBrowser( const Url &url )
82106
{
83107
app::Platform::get()->launchWebBrowser( url );

src/cinder/app/Platform.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "cinder/app/Platform.h"
2525
#include "cinder/CinderAssert.h"
26+
#include "cinder/Utilities.h"
2627

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

146147
void Platform::findAndAddDefaultAssetPath()
147148
{
148-
// first search the local directory, then its parent, up to ASSET_SEARCH_DEPTH levels up
149-
// check at least the app path, even if it has no parent directory
150-
auto execPath = getExecutablePath();
151-
size_t parentCt = 0;
152-
for( fs::path curPath = execPath; curPath.has_parent_path() || ( curPath == execPath ); curPath = curPath.parent_path(), ++parentCt ) {
153-
if( parentCt >= ASSET_SEARCH_DEPTH )
154-
break;
155-
156-
const fs::path curAssetDir = curPath / fs::path( "assets" );
157-
if( fs::exists( curAssetDir ) && fs::is_directory( curAssetDir ) ) {
158-
addAssetDirectory( curAssetDir );
159-
break;
160-
}
161-
}
149+
fs::path assetDir = findAncestorDir( getExecutablePath(), "assets", ASSET_SEARCH_DEPTH );
150+
if( ! assetDir.empty() )
151+
addAssetDirectory( assetDir );
162152
}
163153

164154
std::ostream& Platform::console()

0 commit comments

Comments
 (0)