Skip to content

Commit

Permalink
ofFileUtils: relative path calulation now removes extra . in paths. C…
Browse files Browse the repository at this point in the history
…loses #4564
  • Loading branch information
arturoc committed Nov 22, 2015
1 parent 5001f9f commit 1882a92
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
23 changes: 13 additions & 10 deletions libs/openFrameworks/utils/ofFileUtils.cpp
Expand Up @@ -894,7 +894,7 @@ ofDirectory::ofDirectory(const std::filesystem::path & path){
void ofDirectory::open(const std::filesystem::path & path){
originalDirectory = ofFilePath::getPathForDirectory(path.string());
files.clear();
myDir = std::filesystem::path(ofToDataPath(originalDirectory));
myDir = std::filesystem::path(ofToDataPath(originalDirectory));
}

//------------------------------------------------------------------------------------------------------------
Expand All @@ -908,9 +908,9 @@ bool ofDirectory::create(bool recursive){
if(!myDir.string().empty()){
try{
if(recursive){
std::filesystem::create_directories(myDir);
std::filesystem::create_directories(myDir);
}else{
std::filesystem::create_directory(myDir);
std::filesystem::create_directory(myDir);
}
}
catch(std::exception & except){
Expand Down Expand Up @@ -1057,9 +1057,9 @@ bool ofDirectory::remove(bool recursive){

try{
if(recursive){
std::filesystem::remove_all(myDir);
std::filesystem::remove_all(std::filesystem::canonical(myDir));
}else{
std::filesystem::remove(myDir);
std::filesystem::remove(std::filesystem::canonical(myDir));
}
}catch(std::exception & except){
ofLogError("ofDirectory") << "remove(): unable to remove file/directory: " << except.what();
Expand Down Expand Up @@ -1523,14 +1523,17 @@ string ofFilePath::makeRelative(const std::string & from, const std::string & to
// Find common base
for( std::filesystem::path::const_iterator toEnd( pathTo.end() ), fromEnd( pathFrom.end() ) ; itrFrom != fromEnd && itrTo != toEnd && *itrFrom == *itrTo; ++itrFrom, ++itrTo );
// Navigate backwards in directory to reach previously found base
for( std::filesystem::path::const_iterator fromEnd( pathFrom.end() ); itrFrom != fromEnd; ++itrFrom )
{
if( (*itrFrom) != "." )
for( std::filesystem::path::const_iterator fromEnd( pathFrom.end() ); itrFrom != fromEnd; ++itrFrom ){
if( (*itrFrom) != "." ){
ret /= "..";
}
}
// Now navigate down the directory branch
for( ; itrTo != pathTo.end() ; ++itrTo )
ret /= *itrTo;
for( ; itrTo != pathTo.end() ; ++itrTo ){
if( itrTo->string() != "."){
ret /= *itrTo;
}
}

return ret.string();
}
11 changes: 11 additions & 0 deletions tests/utils/fileUtils/src/main.cpp
Expand Up @@ -7,6 +7,8 @@ std::filesystem::path initial_cwd;
class ofApp: public ofxUnitTestsApp{
void run(){
ofDirectory dir(".");
dir.create(true);
dir.exists();
for(auto f: dir){
f.setWriteable(true);
if(f.isDirectory()){
Expand Down Expand Up @@ -201,6 +203,7 @@ class ofApp: public ofxUnitTestsApp{
test_eq(ofToDataPath(""), "../../../data/", "#4563 test3");
#endif


//========================================================================
// clean test files
dir.open(".");
Expand All @@ -212,6 +215,14 @@ class ofApp: public ofxUnitTestsApp{
f.remove();
}
}

//========================================================================
ofLogNotice() << "#4564";
dir.remove(true);
ofDirectory currentVideoDirectory(ofToDataPath("../../../video", true));
auto path = currentVideoDirectory.path();
std::string pathEnd("data/../../../video/");
test_eq(path.substr(path.size()-pathEnd.size()), pathEnd, "#4564");
}
};

Expand Down

0 comments on commit 1882a92

Please sign in to comment.