Skip to content

Commit a7a1a7a

Browse files
authored
No fs::path return (#8149)
1 parent 5bfbce7 commit a7a1a7a

6 files changed

Lines changed: 109 additions & 61 deletions

File tree

libs/openFrameworks/graphics/ofImage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ static bool loadImage(ofPixels_<PixelType> & pix, const of::filesystem::path & _
251251
if(fif == FIF_JPEG) {
252252
option = getJpegOptionFromImageLoadSetting(settings);
253253
}
254-
auto fileName = ofToDataPath(_fileName);
254+
auto fileName = ofToDataPathFS(_fileName);
255255

256256
#ifdef OF_OS_WINDOWS
257257
bmp = FreeImage_LoadU(fif, fileName.c_str(), option | settings.freeImageFlags);
@@ -420,7 +420,7 @@ static bool saveImage(const ofPixels_<PixelType> & _pix, const of::filesystem::p
420420
}
421421

422422
ofFilePath::createEnclosingDirectory(_fileName);
423-
auto fileName = ofToDataPath(_fileName);
423+
auto fileName = ofToDataPathFS(_fileName);
424424
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
425425
#ifdef OF_OS_WINDOWS
426426
fif = FreeImage_GetFileTypeU(fileName.c_str(), 0);

libs/openFrameworks/graphics/ofTrueTypeFont.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ static of::filesystem::path linuxFontPathByName(const string & fontname) {
368368
static bool loadFontFace(const string & _fontname, FT_Face & face,
369369
of::filesystem::path & _filename, int index){
370370
auto fontname = _fontname;
371-
auto filename = ofToDataPath(fontname);
371+
auto filename = ofToDataPathFS(fontname);
372372
int fontID = index;
373373
if(!of::filesystem::exists(filename)){
374374
#ifdef TARGET_LINUX

libs/openFrameworks/utils/ofFileUtils.cpp

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ namespace{
2929
fs::path defaultDataPath(){
3030
#if defined TARGET_OSX
3131
try {
32-
return fs::canonical(ofFilePath::getCurrentExeDir() / "../../../data/");
32+
return fs::canonical(ofFilePath::getCurrentExeDirFS() / "../../../data/");
3333
} catch(...) {
34-
return ofFilePath::getCurrentExeDir() / "../../../data/";
34+
return ofFilePath::getCurrentExeDirFS() / "../../../data/";
3535
}
3636
#elif defined TARGET_ANDROID
3737
return string("sdcard/");
3838
#else
3939
try {
40-
return fs::canonical(ofFilePath::getCurrentExeDir() / "data/").make_preferred();
40+
return fs::canonical(ofFilePath::getCurrentExeDirFS() / "data/").make_preferred();
4141
} catch(...) {
42-
return (ofFilePath::getCurrentExeDir() / "data/");
42+
return (ofFilePath::getCurrentExeDirFS() / "data/");
4343
}
4444
#endif
4545
}
4646

4747
//--------------------------------------------------
4848
fs::path & defaultWorkingDirectory() {
49-
static auto * defaultWorkingDirectory = new fs::path(ofFilePath::getCurrentExeDir());
49+
static auto * defaultWorkingDirectory = new fs::path(ofFilePath::getCurrentExeDirFS());
5050
return * defaultWorkingDirectory;
5151
}
5252

@@ -558,7 +558,7 @@ bool ofFile::openStream(Mode _mode, bool _binary){
558558
//------------------------------------------------------------------------------------------------------------
559559
bool ofFile::open(const fs::path & _path, Mode _mode, bool binary){
560560
close();
561-
myFile = ofToDataPath(_path);
561+
myFile = ofToDataPathFS(_path);
562562
return openStream(_mode, binary);
563563
}
564564

@@ -648,10 +648,15 @@ bool ofFile::exists() const {
648648
}
649649

650650
//------------------------------------------------------------------------------------------------------------
651-
fs::path ofFile::path() const {
651+
fs::path ofFile::pathFS() const {
652652
return myFile;
653653
}
654654

655+
//------------------------------------------------------------------------------------------------------------
656+
std::string ofFile::path() const {
657+
return ofPathToString(pathFS());
658+
}
659+
655660
//------------------------------------------------------------------------------------------------------------
656661
// FIXME: Deprecate
657662
string ofFile::getExtension() const {
@@ -675,12 +680,14 @@ string ofFile::getBaseName() const {
675680
}
676681

677682
//------------------------------------------------------------------------------------------------------------
678-
fs::path ofFile::getEnclosingDirectory() const {
683+
// MARK: - near future FS
684+
std::string ofFile::getEnclosingDirectory() const {
679685
return ofFilePath::getEnclosingDirectory(path());
680686
}
681687

682688
//------------------------------------------------------------------------------------------------------------
683-
fs::path ofFile::getAbsolutePath() const {
689+
// MARK: - near future FS
690+
std::string ofFile::getAbsolutePath() const {
684691
return ofFilePath::getAbsolutePath(path());
685692
}
686693

@@ -921,7 +928,7 @@ bool ofFile::copyTo(const fs::path & _path, bool bRelativeToData, bool overwrite
921928

922929
//bRelativeToData is handled here for the destination path - so we pass false to static functions below
923930
if(bRelativeToData){
924-
path = ofToDataPath(path);
931+
path = ofToDataPathFS(path);
925932
}
926933

927934
if(ofFile::doesFileExist(path, false)){
@@ -971,7 +978,7 @@ bool ofFile::moveTo(const fs::path& _path, bool bRelativeToData, bool overwrite)
971978
}
972979

973980
if(bRelativeToData){
974-
path = ofToDataPath(path);
981+
path = ofToDataPathFS(path);
975982
}
976983
if(ofFile::doesFileExist(path, false)){
977984

@@ -1157,14 +1164,14 @@ ofDirectory::ofDirectory(const fs::path & path){
11571164

11581165
//------------------------------------------------------------------------------------------------------------
11591166
void ofDirectory::open(const fs::path & path){
1160-
originalDirectory = ofFilePath::getPathForDirectory(path);
1167+
originalDirectory = ofFilePath::getPathForDirectoryFS(path);
11611168
files.clear();
1162-
myDir = ofToDataPath(originalDirectory);
1169+
myDir = ofToDataPathFS(originalDirectory);
11631170
}
11641171

11651172
//------------------------------------------------------------------------------------------------------------
11661173
void ofDirectory::openFromCWD(const fs::path & path){
1167-
originalDirectory = ofFilePath::getPathForDirectory(path);
1174+
originalDirectory = ofFilePath::getPathForDirectoryFS(path);
11681175
files.clear();
11691176
myDir = originalDirectory;
11701177
}
@@ -1206,14 +1213,20 @@ std::string ofDirectory::path() const {
12061213
}
12071214

12081215
//------------------------------------------------------------------------------------------------------------
1209-
fs::path ofDirectory::getAbsolutePath() const {
1216+
fs::path ofDirectory::getAbsolutePathFS() const {
12101217
try {
12111218
return fs::canonical(fs::absolute(myDir));
12121219
} catch(...) {
12131220
return fs::absolute(myDir);
12141221
}
12151222
}
12161223

1224+
// MARK: - near future FS
1225+
//------------------------------------------------------------------------------------------------------------
1226+
std::string ofDirectory::getAbsolutePath() const {
1227+
return ofPathToString(ofDirectory::getAbsolutePathFS());
1228+
}
1229+
12171230
//------------------------------------------------------------------------------------------------------------
12181231
bool ofDirectory::canRead() const {
12191232
return ofFile(myDir,ofFile::Reference).canRead();
@@ -1283,7 +1296,7 @@ bool ofDirectory::copyTo(const fs::path & _path, bool bRelativeToData, bool over
12831296
}
12841297

12851298
if(bRelativeToData){
1286-
path = ofToDataPath(path, bRelativeToData);
1299+
path = ofToDataPathFS(path, bRelativeToData);
12871300
}
12881301

12891302
if(ofDirectory::doesDirectoryExist(path, false)){
@@ -1587,7 +1600,7 @@ bool ofDirectory::createDirectory(const fs::path& _dirPath, bool bRelativeToData
15871600
auto dirPath = _dirPath;
15881601

15891602
if(bRelativeToData){
1590-
dirPath = ofToDataPath(dirPath);
1603+
dirPath = ofToDataPathFS(dirPath);
15911604
}
15921605

15931606
// on OSX,fs::create_directories seems to return false *if* the path has folders that already exist
@@ -1621,7 +1634,7 @@ bool ofDirectory::doesDirectoryExist(const fs::path& _dirPath, bool bRelativeToD
16211634
auto dirPath = _dirPath;
16221635
try {
16231636
if (bRelativeToData) {
1624-
dirPath = ofToDataPath(dirPath);
1637+
dirPath = ofToDataPathFS(dirPath);
16251638
}
16261639
return fs::exists(dirPath) && fs::is_directory(dirPath);
16271640
}
@@ -1635,7 +1648,7 @@ bool ofDirectory::doesDirectoryExist(const fs::path& _dirPath, bool bRelativeToD
16351648
bool ofDirectory::isDirectoryEmpty(const fs::path& _dirPath, bool bRelativeToData){
16361649
auto dirPath = _dirPath;
16371650
if(bRelativeToData){
1638-
dirPath = ofToDataPath(dirPath);
1651+
dirPath = ofToDataPathFS(dirPath);
16391652
}
16401653

16411654
if(!dirPath.empty() && fs::exists(dirPath) && fs::is_directory(dirPath)){
@@ -1750,7 +1763,7 @@ std::string ofFilePath::removeExt(const fs::path & _filename){
17501763
}
17511764

17521765
//------------------------------------------------------------------------------------------------------------
1753-
fs::path ofFilePath::getPathForDirectory(const fs::path & path){
1766+
fs::path ofFilePath::getPathForDirectoryFS(const fs::path & path){
17541767
// if a trailing slash is missing from a path, this will clean it up
17551768
// if it's a windows-style "\" path it will add a "\"
17561769
// if it's a unix-style "/" path it will add a "/"
@@ -1769,11 +1782,18 @@ fs::path ofFilePath::getPathForDirectory(const fs::path & path){
17691782
}
17701783

17711784

1785+
//------------------------------------------------------------------------------------------------------------
1786+
// FIXME: Deprecate this seems over complicated and not useful anymore, using filesystem
1787+
string ofFilePath::getPathForDirectory(const fs::path & path){
1788+
return ofPathToString(ofFilePath::getPathForDirectoryFS(path));
1789+
}
1790+
1791+
17721792
//------------------------------------------------------------------------------------------------------------
17731793
// FIXME: - re-avail
17741794
string ofFilePath::removeTrailingSlash(const fs::path & _path){
17751795
auto path = ofPathToString(_path);
1776-
if(!path.empty() && (path[path.length() - 1] == '/' || path[path.length() - 1] == '\\')){
1796+
if(path.length() > 0 && (path[path.length() - 1] == '/' || path[path.length() - 1] == '\\')){
17771797
path = path.substr(0, path.length() - 1);
17781798
}
17791799
return path;
@@ -1797,7 +1817,9 @@ string ofFilePath::getBaseName(const fs::path & filePath){
17971817
}
17981818

17991819
//------------------------------------------------------------------------------------------------------------
1800-
fs::path ofFilePath::getEnclosingDirectory(const fs::path & _filePath, bool bRelativeToData){
1820+
// MARK: - near future FS
1821+
//fs::path ofFilePath::getEnclosingDirectoryFS(const fs::path & _filePath, bool bRelativeToData){
1822+
std::string ofFilePath::getEnclosingDirectory(const fs::path & _filePath, bool bRelativeToData){
18011823
auto fp = _filePath;
18021824
if(bRelativeToData){
18031825
fp = ofToDataPath(fp);
@@ -1811,7 +1833,8 @@ bool ofFilePath::createEnclosingDirectory(const fs::path& filePath, bool bRelati
18111833
}
18121834

18131835
//------------------------------------------------------------------------------------------------------------
1814-
fs::path ofFilePath::getAbsolutePath(const fs::path & path, bool bRelativeToData){
1836+
// MARK: - near future FS
1837+
fs::path ofFilePath::getAbsolutePathFS(const fs::path & path, bool bRelativeToData){
18151838
if(bRelativeToData){
18161839
return ofToDataPath(path, true);
18171840
}else{
@@ -1823,6 +1846,11 @@ fs::path ofFilePath::getAbsolutePath(const fs::path & path, bool bRelativeToData
18231846
}
18241847
}
18251848

1849+
//------------------------------------------------------------------------------------------------------------
1850+
std::string ofFilePath::getAbsolutePath(const fs::path& path, bool bRelativeToData){
1851+
return ofPathToString(ofFilePath::getAbsolutePathFS(path, bRelativeToData));
1852+
}
1853+
18261854
//------------------------------------------------------------------------------------------------------------
18271855
bool ofFilePath::isAbsolute(const fs::path& path){
18281856
return fs::path(path).is_absolute();
@@ -1841,7 +1869,7 @@ std::string ofFilePath::join(const fs::path& path1, const fs::path& path2){
18411869
}
18421870

18431871
//------------------------------------------------------------------------------------------------------------
1844-
fs::path ofFilePath::getCurrentExePath(){
1872+
fs::path ofFilePath::getCurrentExePathFS(){
18451873
#if defined(TARGET_LINUX) || defined(TARGET_ANDROID)
18461874
char buff[FILENAME_MAX];
18471875
ssize_t size = readlink("/proc/self/exe", buff, sizeof(buff) - 1);
@@ -1860,27 +1888,31 @@ fs::path ofFilePath::getCurrentExePath(){
18601888
}
18611889
return path;
18621890
#elif defined(TARGET_WIN32)
1863-
wchar_t filename[MAX_PATH];
1864-
DWORD result = ::GetModuleFileName(
1865-
nullptr, // retrieve path of current process .EXE
1866-
filename,
1867-
_countof(filename)
1868-
);
1891+
vector<char> executablePath(MAX_PATH);
1892+
DWORD result = ::GetModuleFileNameA(nullptr, &executablePath[0], static_cast<DWORD>(executablePath.size()));
18691893
if (result == 0) {
1870-
// Error
1871-
ofLogError("ofFilePath") << "getCurrentExePath(): couldn't get path, GetModuleFileName failed";
1894+
ofLogError("ofFilePath") << "getCurrentExePath(): couldn't get path, GetModuleFileNameA failed";
1895+
} else {
1896+
return string(executablePath.begin(), executablePath.begin() + result);
18721897
}
1873-
return filename;
18741898
#endif
1875-
return {};
1899+
return "";
18761900
}
18771901

1902+
//------------------------------------------------------------------------------------------------------------
1903+
std::string ofFilePath::getCurrentExePath(){
1904+
return ofPathToString(getCurrentExePathFS());
1905+
}
18781906

18791907
//------------------------------------------------------------------------------------------------------------
1880-
fs::path ofFilePath::getCurrentExeDir(){
1881-
return ofFilePath::getCurrentExePath().parent_path() / "";
1908+
fs::path ofFilePath::getCurrentExeDirFS(){
1909+
return ofFilePath::getCurrentExePathFS().parent_path() / "";
18821910
}
18831911

1912+
//------------------------------------------------------------------------------------------------------------
1913+
std::string ofFilePath::getCurrentExeDir(){
1914+
return ofPathToString(getCurrentExeDirFS());
1915+
}
18841916

18851917
//------------------------------------------------------------------------------------------------------------
18861918
string ofFilePath::getUserHomeDir(){
@@ -1929,7 +1961,7 @@ void ofSetDataPathRoot(const fs::path& newRoot){
19291961
}
19301962

19311963
//--------------------------------------------------
1932-
fs::path ofToDataPath(const fs::path & path, bool makeAbsolute){
1964+
fs::path ofToDataPathFS(const fs::path & path, bool makeAbsolute){
19331965
if (makeAbsolute && path.is_absolute()) {
19341966
return path;
19351967
}
@@ -2019,6 +2051,10 @@ fs::path ofToDataPath(const fs::path & path, bool makeAbsolute){
20192051
}
20202052
}
20212053

2054+
//--------------------------------------------------
2055+
std::string ofToDataPath(const fs::path & path, bool makeAbsolute){
2056+
return ofPathToString(ofToDataPathFS(path, makeAbsolute));
2057+
}
20222058

20232059
//--------------------------------------------------
20242060
// Function used internally in OF core. API can change later

0 commit comments

Comments
 (0)