Skip to content

Commit 3fccc6d

Browse files
authored
More of::filesystem : streamline path inside core (#7383)
1 parent bd83cd2 commit 3fccc6d

File tree

3 files changed

+46
-67
lines changed

3 files changed

+46
-67
lines changed

libs/openFrameworks/utils/ofFileUtils.cpp

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,27 @@ namespace{
2525
bool enableDataPath = true;
2626

2727
//--------------------------------------------------
28-
// MARK: - near future
29-
// of::filesystem::path defaultDataPath(){
30-
std::string defaultDataPath(){
28+
of::filesystem::path defaultDataPath(){
3129
#if defined TARGET_OSX
3230
try{
33-
return of::filesystem::canonical(ofFilePath::getCurrentExeDir() / of::filesystem::path("../../../data/")).string();
31+
return of::filesystem::canonical(ofFilePath::getCurrentExeDirFS() / of::filesystem::path("../../../data/"));
3432
}catch(...){
35-
return (ofFilePath::getCurrentExeDir() / of::filesystem::path("../../../data/")).string();
33+
return (ofFilePath::getCurrentExeDirFS() / of::filesystem::path("../../../data/"));
3634
}
3735
#elif defined TARGET_ANDROID
3836
return string("sdcard/");
3937
#else
4038
try{
41-
return of::filesystem::canonical(ofFilePath::join(ofFilePath::getCurrentExeDir(), "data/")).make_preferred().string();
39+
return of::filesystem::canonical(ofFilePath::join(ofFilePath::getCurrentExeDirFS(), "data/")).make_preferred();
4240
}catch(...){
43-
return (ofFilePath::getCurrentExeDir() / of::filesystem::path("data/")).string();
41+
return (ofFilePath::getCurrentExeDirFS() / of::filesystem::path("data/"));
4442
}
4543
#endif
4644
}
4745

4846
//--------------------------------------------------
4947
of::filesystem::path & defaultWorkingDirectory(){
50-
static auto * defaultWorkingDirectory = new of::filesystem::path(ofFilePath::getCurrentExeDir());
48+
static auto * defaultWorkingDirectory = new of::filesystem::path(ofFilePath::getCurrentExeDirFS());
5149
return * defaultWorkingDirectory;
5250
}
5351

@@ -1399,10 +1397,9 @@ std::size_t ofDirectory::listDir(){
13991397
return 0;
14001398
}
14011399

1402-
of::filesystem::directory_iterator end_iter;
14031400
if ( of::filesystem::exists(myDir) && of::filesystem::is_directory(myDir)){
1404-
for( of::filesystem::directory_iterator dir_iter(myDir) ; dir_iter != end_iter ; ++dir_iter){
1405-
files.emplace_back(dir_iter->path().string(), ofFile::Reference);
1401+
for (const auto & f : of::filesystem::directory_iterator{ myDir }) {
1402+
files.emplace_back(f.path(), ofFile::Reference);
14061403
}
14071404
}else{
14081405
ofLogError("ofDirectory") << "listDir:() source directory does not exist: " << myDir ;
@@ -1723,7 +1720,8 @@ vector<ofFile>::const_reverse_iterator ofDirectory::rend() const{
17231720

17241721

17251722
//------------------------------------------------------------------------------------------------------------
1726-
string ofFilePath::addLeadingSlash(const of::filesystem::path& _path){
1723+
// FIXME: - re-avail
1724+
string ofFilePath::addLeadingSlash(const of::filesystem::path & _path){
17271725
auto path = _path.string();
17281726
auto sep = of::filesystem::path("/").make_preferred();
17291727
if(!path.empty()){
@@ -1735,13 +1733,10 @@ string ofFilePath::addLeadingSlash(const of::filesystem::path& _path){
17351733
}
17361734

17371735
//------------------------------------------------------------------------------------------------------------
1738-
// MARK: - near future
1739-
//of::filesystem::path ofFilePath::addTrailingSlash(const of::filesystem::path & _path){
1736+
// FIXME: - re-avail
17401737
std::string ofFilePath::addTrailingSlash(const of::filesystem::path & _path){
17411738
#if OF_USING_STD_FS && !OF_USE_EXPERIMENTAL_FS
1742-
if(_path.string().empty()) return "";
1743-
// FIXME: Remove .string() here and following
1744-
// return (of::filesystem::path(_path).make_preferred() / "");
1739+
if(_path.empty()) return "";
17451740
return (of::filesystem::path(_path).make_preferred() / "").string();
17461741
#else
17471742
auto path = of::filesystem::path(_path).make_preferred();
@@ -1751,29 +1746,26 @@ std::string ofFilePath::addTrailingSlash(const of::filesystem::path & _path){
17511746
path = (path / sep);
17521747
}
17531748
}
1754-
// return path;
17551749
return path.string();
17561750
#endif
17571751
}
17581752

17591753

17601754
//------------------------------------------------------------------------------------------------------------
1761-
string ofFilePath::getFileExt(const of::filesystem::path& filename){
1755+
// FIXME: - start using of::filesystem::path.extension()
1756+
string ofFilePath::getFileExt(const of::filesystem::path & filename){
17621757
return ofFile(filename,ofFile::Reference).getExtension();
17631758
}
17641759

17651760
//------------------------------------------------------------------------------------------------------------
1766-
// FIXME: remove const and copy
1767-
// MARK: - near future
1768-
// of::filesystem::path ofFilePath::removeExt(const of::filesystem::path& _filename){
1769-
std::string ofFilePath::removeExt(const of::filesystem::path& _filename){
1761+
// FIXME: - suggest replace_extension instead
1762+
std::string ofFilePath::removeExt(const of::filesystem::path & _filename){
17701763
auto filename = _filename;
1771-
// return filename.replace_extension();
17721764
return filename.replace_extension().string();
17731765
}
17741766

17751767
//------------------------------------------------------------------------------------------------------------
1776-
string ofFilePath::getPathForDirectory(const of::filesystem::path& path){
1768+
string ofFilePath::getPathForDirectory(const of::filesystem::path & path){
17771769
// if a trailing slash is missing from a path, this will clean it up
17781770
// if it's a windows-style "\" path it will add a "\"
17791771
// if it's a unix-style "/" path it will add a "/"
@@ -1795,8 +1787,8 @@ string ofFilePath::getPathForDirectory(const of::filesystem::path& path){
17951787
}
17961788

17971789
//------------------------------------------------------------------------------------------------------------
1798-
// FIXME: convert to of::filesystem::path
1799-
string ofFilePath::removeTrailingSlash(const of::filesystem::path& _path){
1790+
// FIXME: - re-avail
1791+
string ofFilePath::removeTrailingSlash(const of::filesystem::path & _path){
18001792
auto path = _path.string();
18011793
if(path.length() > 0 && (path[path.length() - 1] == '/' || path[path.length() - 1] == '\\')){
18021794
path = path.substr(0, path.length() - 1);
@@ -1806,7 +1798,7 @@ string ofFilePath::removeTrailingSlash(const of::filesystem::path& _path){
18061798

18071799

18081800
//------------------------------------------------------------------------------------------------------------
1809-
// FIXME: is this still useful? if yes convert to of::filesystem::path
1801+
// FIXME: - re-avail
18101802
string ofFilePath::getFileName(const of::filesystem::path& _filePath, bool bRelativeToData){
18111803
auto filePath = _filePath;
18121804

@@ -1819,9 +1811,9 @@ string ofFilePath::getFileName(const of::filesystem::path& _filePath, bool bRela
18191811
}
18201812

18211813
//------------------------------------------------------------------------------------------------------------
1822-
string ofFilePath::getBaseName(const of::filesystem::path& filePath){
1823-
// FIXME: is this still useful?
1824-
return ofFile(filePath,ofFile::Reference).getBaseName();
1814+
// FIXME: - suggest using stem() instead
1815+
string ofFilePath::getBaseName(const of::filesystem::path & filePath){
1816+
return filePath.stem().string();
18251817
}
18261818

18271819
//------------------------------------------------------------------------------------------------------------
@@ -1879,7 +1871,7 @@ std::string ofFilePath::join(const of::filesystem::path& path1, const of::filesy
18791871
}
18801872

18811873
//------------------------------------------------------------------------------------------------------------
1882-
string ofFilePath::getCurrentExePath(){
1874+
of::filesystem::path ofFilePath::getCurrentExePathFS(){
18831875
#if defined(TARGET_LINUX) || defined(TARGET_ANDROID)
18841876
char buff[FILENAME_MAX];
18851877
ssize_t size = readlink("/proc/self/exe", buff, sizeof(buff) - 1);
@@ -1900,20 +1892,30 @@ string ofFilePath::getCurrentExePath(){
19001892
#elif defined(TARGET_WIN32)
19011893
vector<char> executablePath(MAX_PATH);
19021894
DWORD result = ::GetModuleFileNameA(nullptr, &executablePath[0], static_cast<DWORD>(executablePath.size()));
1903-
if(result == 0) {
1895+
if (result == 0) {
19041896
ofLogError("ofFilePath") << "getCurrentExePath(): couldn't get path, GetModuleFileNameA failed";
1905-
}else{
1897+
} else {
19061898
return string(executablePath.begin(), executablePath.begin() + result);
19071899
}
19081900
#endif
19091901
return "";
19101902
}
19111903

19121904
//------------------------------------------------------------------------------------------------------------
1913-
// MARK: - near future
1914-
//of::filesystem::path ofFilePath::getCurrentExeDir(){
1905+
std::string ofFilePath::getCurrentExePath(){
1906+
return getCurrentExePathFS().string();
1907+
}
1908+
1909+
//------------------------------------------------------------------------------------------------------------
1910+
of::filesystem::path ofFilePath::getCurrentExeDirFS(){
1911+
return ofFilePath::getCurrentExePathFS().parent_path();
1912+
}
1913+
1914+
//------------------------------------------------------------------------------------------------------------
19151915
std::string ofFilePath::getCurrentExeDir(){
1916-
return ofFilePath::getEnclosingDirectory(ofFilePath::getCurrentExePath(), false);
1916+
1917+
// std::string sep = of::filesystem::path::preferred_separator;
1918+
return getCurrentExeDirFS().string() + of::filesystem::path("/").make_preferred().string();
19171919
}
19181920

19191921
//------------------------------------------------------------------------------------------------------------

libs/openFrameworks/utils/ofFileUtils.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class ofFilePath {
363363
/// "../../"
364364
/// \returns filename
365365
///
366-
// FIXME: Deprecate / Remove this.
366+
// FIXME: Deprecate / Remove this. Find instances in OF Core to remove
367367
static std::string getFileName(const of::filesystem::path & filePath, bool bRelativeToData = true);
368368

369369
/// Get a file name without its extension,
@@ -372,6 +372,8 @@ class ofFilePath {
372372
///
373373
/// \param filePath file path
374374
/// \returns basename
375+
376+
// FIXME: Deprecate / Remove this. Find instances in OF Core to remove
375377
static std::string getBaseName(const of::filesystem::path & filePath);
376378

377379
/// Get the enclosing parent directory of a path,
@@ -428,6 +430,7 @@ class ofFilePath {
428430
/// Linux: the binary file itself
429431
///
430432
/// \returns current executable path
433+
static of::filesystem::path getCurrentExePathFS();
431434
static std::string getCurrentExePath();
432435

433436
/// Get the full path to the application's parent directory.
@@ -436,8 +439,7 @@ class ofFilePath {
436439
/// Mac: the Contents/MacOS folder within the application's .app bundle
437440
///
438441
/// \returns current executable directory
439-
// MARK: - near future
440-
// static of::filesystem::path getCurrentExeDir();
442+
static of::filesystem::path getCurrentExeDirFS();
441443
static std::string getCurrentExeDir();
442444

443445
/// Get the absolute path to the user's home directory.

tests/utils/fileUtils/src/main.cpp

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#include "ofUtils.h"
33
#include "ofxUnitTests.h"
44

5-
using namespace std;
6-
75
of::filesystem::path initial_cwd;
86

97
class ofApp: public ofxUnitTestsApp{
@@ -180,10 +178,8 @@ class ofApp: public ofxUnitTestsApp{
180178
ofxTestEq(ofFilePath::getBaseName("test/test.txt"),"test","ofFilePath::getBaseName",ofFilePath::getBaseName("test/test.txt"));
181179
ofxTestEq(ofFilePath::getBaseName(ofFilePath::removeTrailingSlash(ofFilePath::getEnclosingDirectory("testdir/test.txt"))),"testdir","ofFilePath::getEnclosingDirectory",ofFilePath::getBaseName(ofFilePath::getEnclosingDirectory("testdir/test.txt")));
182180
#ifdef TARGET_WIN32
183-
// ofxTestEq(ofFilePath::join("d1","d2").string(),"d1\\d2","ofFilePath::join",ofFilePath::join("d1","d2").string());
184181
ofxTestEq(ofFilePath::join("d1","d2"),"d1\\d2","ofFilePath::join",ofFilePath::join("d1","d2"));
185182
#else
186-
// ofxTestEq(ofFilePath::join("d1","d2").string(),"d1/d2","ofFilePath::join",ofFilePath::join("d1","d2").string());
187183
ofxTestEq(ofFilePath::join("d1","d2"),"d1/d2","ofFilePath::join",ofFilePath::join("d1","d2"));
188184
#endif
189185

@@ -220,22 +216,12 @@ class ofApp: public ofxUnitTestsApp{
220216
ofLogNotice() << "";
221217
ofLogNotice() << "tests #4462";
222218
if(ofGetTargetPlatform()==OF_TARGET_WINVS || ofGetTargetPlatform()==OF_TARGET_MINGW){
223-
// ofxTestEq(ofToDataPath("movies\\",true).string().back(), '\\', "absolute ofToDataPath with \\ should end in \\");
224-
// ofxTestEq(ofToDataPath("movies",true).string().back(), 's', "absolute ofToDataPath without \\ should not end in \\");
225-
// ofDirectory("movies").create();
226-
// ofxTestEq(ofToDataPath("movies\\",true).string().back(), '\\', "absolute ofToDataPath with \\ should end in \\");
227-
// ofxTestEq(ofToDataPath("movies",true).string().back(), 's', "absolute ofToDataPath without \\ should not end in \\");
228219
ofxTestEq(ofToDataPath("movies\\",true).back(), '\\', "absolute ofToDataPath with \\ should end in \\");
229220
ofxTestEq(ofToDataPath("movies",true).back(), 's', "absolute ofToDataPath without \\ should not end in \\");
230221
ofDirectory("movies").create();
231222
ofxTestEq(ofToDataPath("movies\\",true).back(), '\\', "absolute ofToDataPath with \\ should end in \\");
232223
ofxTestEq(ofToDataPath("movies",true).back(), 's', "absolute ofToDataPath without \\ should not end in \\");
233224
}else{
234-
// ofxTestEq(ofToDataPath("movies/",true).string().back(), '/', "absolute ofToDataPath with / should end in /");
235-
// ofxTestEq(ofToDataPath("movies",true).string().back(), 's', "absolute ofToDataPath without / should not end in /");
236-
// ofDirectory("movies").create();
237-
// ofxTestEq(ofToDataPath("movies/",true).string().back(), '/', "absolute ofToDataPath with / should end in /");
238-
// ofxTestEq(ofToDataPath("movies",true).string().back(), 's', "absolute ofToDataPath without / should not end in /");
239225
ofxTestEq(ofToDataPath("movies/",true).back(), '/', "absolute ofToDataPath with / should end in /");
240226
ofxTestEq(ofToDataPath("movies",true).back(), 's', "absolute ofToDataPath without / should not end in /");
241227
ofDirectory("movies").create();
@@ -247,21 +233,11 @@ class ofApp: public ofxUnitTestsApp{
247233
//========================================================================
248234
ofLogNotice() << "";
249235
ofLogNotice() << "tests #4598";
250-
// ofxTestEq(ofToDataPath("").string().back(), of::filesystem::path::preferred_separator, "ofToDataPath with empty string shouldn't crash");
251236
ofxTestEq(ofToDataPath("").back(), of::filesystem::path::preferred_separator, "ofToDataPath with empty string shouldn't crash");
252237

253238
//========================================================================
254239
ofLogNotice() << "";
255240
ofLogNotice() << "tests #4563";
256-
// #ifdef TARGET_LINUX
257-
// ofxTestEq(ofToDataPath("a.txt").string(), "data/a.txt","#4563 test1");
258-
// ofxTestEq(ofToDataPath("data.txt").string(), "data/data.txt", "#4563 test2");
259-
// ofxTestEq(ofToDataPath("").string(), "data/", "#4563 test3");
260-
// #elif defined(TARGET_OSX)
261-
// ofxTestEq(ofToDataPath("a.txt").string(), "../../../data/a.txt","#4563 test1");
262-
// ofxTestEq(ofToDataPath("data.txt").string(), "../../../data/data.txt", "#4563 test2");
263-
// ofxTestEq(ofToDataPath("").string(), "../../../data/", "#4563 test3");
264-
// #endif
265241
#ifdef TARGET_LINUX
266242
ofxTestEq(ofToDataPath("a.txt"), "data/a.txt","#4563 test1");
267243
ofxTestEq(ofToDataPath("data.txt"), "data/data.txt", "#4563 test2");
@@ -272,6 +248,7 @@ class ofApp: public ofxUnitTestsApp{
272248
ofxTestEq(ofToDataPath(""), "../../../data/", "#4563 test3");
273249
#endif
274250

251+
275252
//========================================================================
276253
// clean test files
277254
dir.open(".");
@@ -289,13 +266,11 @@ class ofApp: public ofxUnitTestsApp{
289266
dir.remove(true);
290267
if(ofGetTargetPlatform()==OF_TARGET_WINVS || ofGetTargetPlatform()==OF_TARGET_MINGW){
291268
ofDirectory currentVideoDirectory(ofToDataPath("..\\..\\..\\video", true));
292-
// auto path = currentVideoDirectory.path().string();
293269
auto path = currentVideoDirectory.path();
294270
std::string pathEnd("data\\..\\..\\..\\video\\");
295271
ofxTestEq(path.substr(path.size()-pathEnd.size()), pathEnd, "#4564");
296272
}else{
297273
ofDirectory currentVideoDirectory(ofToDataPath("../../../video", true));
298-
// auto path = currentVideoDirectory.path().string();
299274
auto path = currentVideoDirectory.path();
300275
std::string pathEnd("data/../../../video/");
301276
ofxTestEq(path.substr(path.size()-pathEnd.size()), pathEnd, "#4564");
@@ -314,4 +289,4 @@ int main( ){
314289
auto app = std::make_shared<ofApp>();
315290
ofRunApp(window, app);
316291
return ofRunMainLoop();
317-
}
292+
}

0 commit comments

Comments
 (0)