# PhasicFlow: src/phasicFlow/fileSystem/fileSystem.cpp Source File

fileSystem.cpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
#include "fileSystem.hpp"
#include "error.hpp"
#include "iOstream.hpp"
bool pFlow::fileSystem::checkFileName(const word& name)
{
if(!validFileName(name))
{
fatalErrorInFunction <<
"Invalid file name supplied " << name <<
"the following characters are not allowd: " <<
notPermittedCharsFile << endl;
fatalExit;
return false;
}
return true;
}
pFlow::fileSystem::fileSystem( const word& dir, const word& file)
{
isDir_ = file.empty();
if( !isDir_)
{
checkFileName(file);
}
try
{
path_ = pathType(dir)/file;
}
catch (std::filesystem::filesystem_error & ec)
{
fatalErrorInFunction<<
"Invalid fileSystem input:" <<
ec.what()<<endl;
fatalExit;
}
}
pFlow::fileSystem::fileSystem( const char* dir, const char* file)
:
fileSystem(word(dir),word(file))
{
}
pFlow::fileSystem::fileSystem( const pathType& path )
:
path_(path)
{
isDir_ = isDirectory(*this);
}
pFlow::fileSystem pFlow::fileSystem::dirPath() const
{
if( isDir())
{
return *this;
}
else
{
return fileSystem(path_.parent_path().c_str());
}
}
pFlow::word pFlow::fileSystem::fileName() const
{
if( isDir())
return word("");
try
{
return word( path_.filename());
}
catch (std::filesystem::filesystem_error & ec)
{
fatalErrorInFunction<<
ec.what()<<endl;
fatalExit;
}
return word("");
}
pFlow::fileSystem pFlow::fileSystem::absolute
(
) const
{
std::error_code ec;
pathType abPath;
if( abPath = std::filesystem::absolute( path_, ec); ec )
{
fatalErrorInFunction <<
"The absolute path of "<< path_.c_str() <<" is invalid: "<<
ec.message()<<endl;
fatalExit;
}
fileSystem res;
res.path_ = abPath;
res.isDir_ = isDir_;
return res;
}
pFlow::fileSystem pFlow::fileSystem::canonical
(
)const
{
std::error_code ec;
pathType cnPath;
if( cnPath = std::filesystem::canonical( path_, ec); ec )
{
fatalErrorInFunction <<
"The canonical path of "<< path_.c_str() <<" cannot be obtained: "<<
ec.message()<<endl;
fatalExit;
}
fileSystem res;
res.path_ = cnPath;
res.isDir_ = isDir_;
return res;
}
bool pFlow::fileSystem::dirExist
(
) const
{
if(!isDir())
{
fatalErrorInFunction<<
"This function only operates on dir path, the path is "<<
path_.c_str()<<endl;
fatalExit;
return false;
}
return exist();
}
bool pFlow::fileSystem::exist
(
)const
{
try
{
return std::filesystem::exists(path_);
}
catch (std::filesystem::filesystem_error & ec)
{
fatalErrorInFunction<<
ec.what()<<endl;
fatalExit;
return false;
}
}
pFlow::fileSystem pFlow::fileSystem::createDirs
(
) const
{
if(! isDir())
{
fatalErrorInFunction<<
"This function only operates on dir path, the path is "<<
path_.c_str()<<endl;
fatalExit;
}
try
{
std::filesystem::create_directories(path_);
return canonical();
}
catch (std::filesystem::filesystem_error & ec)
{
fatalErrorInFunction<<
ec.what()<<endl;
fatalExit;
return *this;
}
}
pFlow::fileSystem pFlow::fileSystem::operator()
(
bool retDir
) const
{
if(retDir)
return dirPath();
else
return *this;
}
void pFlow::fileSystem::operator += (const word& fileName)
{
checkFileName(fileName);
if( isDir())
{
path_ /= fileName;
isDir_ = false;
}
else
{
path_ = path_.parent_path()/fileName;
}
}
void pFlow::fileSystem::operator /=
(
const fileSystem& fs
)
{
if(!isDir())
{
fatalErrorInFunction<<
"This operator should be used on dir path only"<<endl;
fatalExit;
}
path_/= fs.dirPath().path_;
}
pFlow::fileSystem pFlow::operator /
(
const fileSystem& fs1,
const fileSystem& fs2
)
{
fileSystem::pathType cmbPath(fs1.dirPath().path_ / fs2.dirPath().path_);
return fileSystem( cmbPath.c_str() );
}
pFlow::fileSystem pFlow::operator +
(
const fileSystem& fs1,
const word fName
)
{
fileSystem path = fs1;
path+= fName;
return path;
}
pFlow::iOstream& pFlow::operator << (iOstream& os, fileSystem fs)
{
os << fs.path_.c_str();
return os;
}
std::ostream& pFlow::operator << (std::ostream& os, fileSystem fs)
{
os << fs.path_.c_str();
return os;
}
bool pFlow::isDirectory
(
const fileSystem& path
)
{
return std::filesystem::is_directory(path.path());
}
bool pFlow::isRegularFile(const fileSystem& path)
{
return std::filesystem::is_regular_file(path.path());
}
pFlow::fileSystemList pFlow::subDirectories
(
const fileSystem& path
)
{
fileSystemList dirs;
if( isDirectory(path) )
{
auto dOps = std::filesystem::directory_options::skip_permission_denied;
for( auto& subPath: std::filesystem::directory_iterator(path.path(), dOps) )
{
if(isDirectory( subPath.path() ) )
{
dirs.emplace_back(subPath.path());
}
}
}
return dirs;
}
pFlow::fileSystemList pFlow::containingFiles
(
const fileSystem& path
)
{
fileSystemList files;
if( isDirectory(path) )
{
auto dOps = std::filesystem::directory_options::skip_permission_denied;
for( auto& subPath: std::filesystem::directory_iterator(path.path(), dOps) )
{
if( std::filesystem::is_regular_file(subPath.path()) )
{
files.emplace_back(subPath.path());
}
}
}
return files;
}
pFlow::List
Definition: List.hpp:39
pFlow::fileSystem::canonical
fileSystem canonical() const
Definition: fileSystem.cpp:140
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::fileSystem::fileName
word fileName() const
Definition: fileSystem.cpp:96
pFlow::fileSystem::fileSystem
fileSystem()
Definition: fileSystem.hpp:96
pFlow::fileSystem::dirExist
bool dirExist() const
Definition: fileSystem.cpp:162
pFlow::fileSystem::exist
bool exist() const
Definition: fileSystem.cpp:179
pFlow::fileSystem::operator+=
void operator+=(const word &fileName)
Definition: fileSystem.cpp:233
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::containingFiles
fileSystemList containingFiles(const fileSystem &path)
Definition: fileSystem.cpp:335
pFlow::fileSystem::isDir_
bool isDir_
Definition: fileSystem.hpp:68
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
fileSystem.hpp
pFlow::isRegularFile
bool isRegularFile(const fileSystem &path)
Definition: fileSystem.cpp:307
pFlow::fileSystem
Definition: fileSystem.hpp:63
pFlow::subDirectories
fileSystemList subDirectories(const fileSystem &path)
Definition: fileSystem.cpp:313
pFlow::fileSystem::notPermittedCharsFile
static word notPermittedCharsFile
Definition: fileSystem.hpp:72
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::fileSystem::path
const pathType & path() const
Definition: fileSystem.hpp:121
pFlow::fileSystem::dirPath
fileSystem dirPath() const
Definition: fileSystem.cpp:82
pFlow::fileSystem::path_
std::filesystem::path path_
Definition: fileSystem.hpp:67
pFlow::isDirectory
bool isDirectory(const fileSystem &path)
Definition: fileSystem.cpp:300
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::fileSystem::absolute
fileSystem absolute() const
Definition: fileSystem.cpp:116
pFlow::fileSystem::validFileName
static bool validFileName(const word &name)
Definition: fileSystem.hpp:76
pFlow::fileSystem::checkFileName
static bool checkFileName(const word &name)
Definition: fileSystem.cpp:27
iOstream.hpp
pFlow::fileSystem::pathType
std::filesystem::path pathType
Definition: fileSystem.hpp:93
pFlow::fileSystem::createDirs
fileSystem createDirs() const
Definition: fileSystem.cpp:196
pFlow::iOstream
Definition: iOstream.hpp:53
error.hpp