Skip to content
Open
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
57 changes: 40 additions & 17 deletions Sofa/framework/Helper/src/sofa/helper/system/FileRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void FileRepository::addFirstPath(const std::string& p)
}
p0 = p1+1;
}
vpath.insert(vpath.begin(), entries.begin(), entries.end());
m_vpath.insert(m_vpath.begin(), entries.begin(), entries.end());
}

void FileRepository::addLastPath(const std::string& p)
Expand All @@ -208,7 +208,7 @@ void FileRepository::addLastPath(const std::string& p)
}
p0 = p1+1;
}
vpath.insert(vpath.end(), entries.begin(), entries.end());
m_vpath.insert(m_vpath.end(), entries.begin(), entries.end());
}

void FileRepository::removePath(const std::string& path)
Expand All @@ -229,19 +229,19 @@ void FileRepository::removePath(const std::string& path)
for(std::vector<std::string>::iterator it=entries.begin();
it!=entries.end(); ++it)
{
vpath.erase( find(vpath.begin(), vpath.end(), *it) );
m_vpath.erase( find(m_vpath.begin(), m_vpath.end(), *it) );
}
}

void FileRepository::clear()
{
vpath.clear();
m_vpath.clear();
}

std::string FileRepository::getFirstPath()
{
if (vpath.size() > 0)
return vpath.front();
if (m_vpath.size() > 0)
return m_vpath.front();
else return "";
}

Expand Down Expand Up @@ -274,23 +274,46 @@ bool FileRepository::findFile(std::string& filename, const std::string& basedir,
if (findFileIn(filename, currentDir)) return true;

if (SetDirectory::IsAbsolute(filename)) return false; // absolute file path

// if the given filename starts with dot entries,
// each path from the list should be searched
// and in the current dir as a fallback
if (filename.substr(0,2)=="./" || filename.substr(0,3)=="../")
{
const std::string original = filename;
for(const auto& path : m_vpath)
{
std::string candidate = original;
if (findFileIn(candidate, path))
{
filename = candidate;
return true;
}
}

// fallback to currentDir (aka current working dir)
// update filename with current dir
filename = SetDirectory::GetRelativeFromDir(filename.c_str(), currentDir.c_str());
filename = SetDirectory::GetRelativeFromDir(original.c_str(), currentDir.c_str());
return false; // local file path
}
for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
if (findFileIn(filename, *it)) return true;

for(const auto& path : m_vpath)
{
if (findFileIn(filename, path))
return true;
}

if (errlog)
{
// hack to use logging rather than directly writing in std::cerr/std::cout
// @todo do something cleaner

std::stringstream tmplog;
tmplog << "File "<<filename<<" NOT FOUND in "<<basedir;
for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
tmplog << ':'<<*it;

for(const auto& path : m_vpath)
tmplog << ':'<< path;

if( errlog==&std::cerr || errlog==&std::cout)
msg_error("FileRepository") << tmplog.str();
else
Expand All @@ -317,10 +340,10 @@ void FileRepository::findAllFilesInRepository(const std::string& path, std::vect
}
};

for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
for (const auto& repopath : m_vpath)
{
// Get the full / absolute path (vpath + path)
const std::string fullPath = SetDirectory::GetRelativeFromDir(path.c_str(), it->c_str());
// Get the full / absolute path (repopath + path)
const std::string fullPath = SetDirectory::GetRelativeFromDir(path.c_str(), repopath.c_str());
fs::path p = fs::path(fullPath);

if (fs::exists(p) && fs::is_directory(p))
Expand All @@ -345,8 +368,8 @@ void FileRepository::findAllFilesInRepository(const std::string& path, std::vect

void FileRepository::print()
{
for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
std::cout << *it << std::endl;
for (const auto& path : m_vpath)
std::cout << path << std::endl;
}

const std::string FileRepository::getPathsJoined()
Expand All @@ -356,7 +379,7 @@ const std::string FileRepository::getPathsJoined()
#ifdef WIN32
delim = ";";
#endif
std::copy(vpath.begin(), vpath.end(), std::ostream_iterator<std::string>(imploded, delim.c_str()));
std::copy(m_vpath.begin(), m_vpath.end(), std::ostream_iterator<std::string>(imploded, delim.c_str()));
std::string implodedStr = imploded.str();
implodedStr = implodedStr.substr(0, implodedStr.size()-1); // remove trailing separator
return implodedStr;
Expand Down
11 changes: 6 additions & 5 deletions Sofa/framework/Helper/src/sofa/helper/system/FileRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class SOFA_HELPER_API FileRepository
/// Otherwise returns path.
static std::string relativeToPath(std::string path, std::string refPath);

const std::vector< std::string > &getPaths() const {return vpath;}
const std::vector< std::string > &getPaths() const {return m_vpath;}

const std::string getPathsJoined();

Expand Down Expand Up @@ -167,9 +167,10 @@ class SOFA_HELPER_API FileRepository
friend std::ostream& operator << (std::ostream& _flux, FileRepository _fr)
{
_flux<< "FileRepository vpath :"<<std::endl;
for(std::vector<std::string>::iterator it = _fr.vpath.begin(); it!=_fr.vpath.end(); it++)
_flux<<(*it)<<std::endl;


for(const auto& vpath : _fr.m_vpath)
_flux << vpath <<std::endl;

return _flux;
}

Expand All @@ -188,7 +189,7 @@ class SOFA_HELPER_API FileRepository
std::string directAccessProtocolPrefix;

/// Vector of paths.
std::vector<std::string> vpath;
std::vector<std::string> m_vpath;

/// Search file in a given path.
static bool findFileIn(std::string& filename, const std::string& path);
Expand Down
Loading