Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 70 additions & 43 deletions libs/openFrameworks/utils/ofFileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,17 @@ void ofFile::close(){

//------------------------------------------------------------------------------------------------------------
bool ofFile::create(){
return create(path());
}

//------------------------------------------------------------------------------------------------------------
bool ofFile::create(const std::filesystem::path & path){
bool success = false;

if(!myFile.string().empty()){
auto oldmode = this->mode;
auto oldpath = path();
success = open(path(),ofFile::WriteOnly,binary);
auto oldpath = this->path();
success = open(path,ofFile::WriteOnly,binary);
close();
open(oldpath,oldmode,binary);
}
Expand Down Expand Up @@ -648,30 +653,46 @@ bool ofFile::isHidden() const {

//------------------------------------------------------------------------------------------------------------
void ofFile::setWriteable(bool flag){
setReadOnly(!flag);
try{
if(flag){
std::filesystem::permissions(myFile,std::filesystem::perms::owner_write | std::filesystem::perms::add_perms);
}else{
std::filesystem::permissions(myFile,std::filesystem::perms::owner_write | std::filesystem::perms::remove_perms);
}
}catch(std::exception & e){
ofLogError() << "Couldn't set write permission on " << myFile << ": " << e.what();
}
}

//------------------------------------------------------------------------------------------------------------
// deprecated
void ofFile::setReadOnly(bool flag){
setWriteable(!flag);
}

//------------------------------------------------------------------------------------------------------------
void ofFile::setReadable(bool flag){
try{
if(flag){
std::filesystem::permissions(myFile,std::filesystem::perms::owner_write | std::filesystem::perms::remove_perms);
std::filesystem::permissions(myFile,std::filesystem::perms::owner_write | std::filesystem::perms::remove_perms);
std::filesystem::permissions(myFile,std::filesystem::perms::owner_write | std::filesystem::perms::remove_perms);
std::filesystem::permissions(myFile,std::filesystem::perms::owner_read | std::filesystem::perms::add_perms);
}else{
std::filesystem::permissions(myFile,std::filesystem::perms::owner_write | std::filesystem::perms::add_perms);
std::filesystem::permissions(myFile,std::filesystem::perms::owner_read | std::filesystem::perms::remove_perms);
}
}catch(std::exception & e){
ofLogError() << "Couldn't set write permission on " << myFile << ": " << e.what();
ofLogError() << "Couldn't set read permission on " << myFile << ": " << e.what();
}
}

//------------------------------------------------------------------------------------------------------------
void ofFile::setExecutable(bool flag){
try{
std::filesystem::permissions(myFile, std::filesystem::perms::owner_exe | std::filesystem::perms::add_perms);
if(flag){
std::filesystem::permissions(myFile, std::filesystem::perms::owner_exe | std::filesystem::perms::add_perms);
} else{
std::filesystem::permissions(myFile, std::filesystem::perms::owner_exe | std::filesystem::perms::remove_perms);
}
}catch(std::exception & e){
ofLogError() << "Couldn't set write permission on " << myFile << ": " << e.what();
ofLogError() << "Couldn't set executable permission on " << myFile << ": " << e.what();
}
}

Expand Down Expand Up @@ -899,7 +920,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 @@ -913,9 +934,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 @@ -972,8 +993,14 @@ void ofDirectory::setWriteable(bool flag){
}

//------------------------------------------------------------------------------------------------------------
// deprecated
void ofDirectory::setReadOnly(bool flag){
return ofFile(myDir,ofFile::Reference).setReadOnly(flag);
setWriteable(!flag);
}

//------------------------------------------------------------------------------------------------------------
void ofDirectory::setReadable(bool flag){
return ofFile(myDir,ofFile::Reference).setReadable(flag);
}

//------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1062,9 +1089,9 @@ bool ofDirectory::remove(bool recursive){

try{
if(recursive){
std::filesystem::remove_all(std::filesystem::canonical(myDir));
std::filesystem::remove_all(std::filesystem::canonical(myDir));
}else{
std::filesystem::remove(std::filesystem::canonical(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 @@ -1190,18 +1217,18 @@ static bool natural(const ofFile& a, const ofFile& b) {

//------------------------------------------------------------------------------------------------------------
void ofDirectory::sort(){
if(files.empty() && !myDir.empty()){
listDir();
}
if(files.empty() && !myDir.empty()){
listDir();
}
ofSort(files, natural);
}

//------------------------------------------------------------------------------------------------------------
ofDirectory ofDirectory::getSorted(){
ofDirectory sorted(*this);
sorted.listDir();
sorted.sort();
return sorted;
ofDirectory sorted(*this);
sorted.listDir();
sorted.sort();
return sorted;
}

//------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1521,24 +1548,24 @@ string ofFilePath::getUserHomeDir(){
}

string ofFilePath::makeRelative(const std::string & from, const std::string & to){
auto pathFrom = std::filesystem::absolute( from );
auto pathTo = std::filesystem::absolute( to );
std::filesystem::path ret;
std::filesystem::path::const_iterator itrFrom( pathFrom.begin() ), itrTo( pathTo.begin() );
// 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) != "." ){
ret /= "..";
}
}
// Now navigate down the directory branch
for( ; itrTo != pathTo.end() ; ++itrTo ){
if( itrTo->string() != "."){
ret /= *itrTo;
}
}

return ret.string();
auto pathFrom = std::filesystem::absolute( from );
auto pathTo = std::filesystem::absolute( to );
std::filesystem::path ret;
std::filesystem::path::const_iterator itrFrom( pathFrom.begin() ), itrTo( pathTo.begin() );
// 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) != "." ){
ret /= "..";
}
}
// Now navigate down the directory branch
for( ; itrTo != pathTo.end() ; ++itrTo ){
if( itrTo->string() != "."){
ret /= *itrTo;
}
}

return ret.string();
}
Loading