26 #include <boost/functional.hpp>
27 #include <boost/regex.hpp>
28 #include <boost/algorithm/string.hpp>
34 #include "util/base/exception.h"
35 #include "util/log/logger.h"
38 #include "vfssource.h"
39 #include "vfssourceprovider.h"
42 static Logger _log(LM_VFS);
52 type_sources sources = m_sources;
53 type_sources::const_iterator end = sources.end();
54 for (type_sources::iterator i = sources.begin(); i != end; ++i)
57 type_providers::const_iterator end2 = m_providers.end();
58 for (type_providers::iterator j = m_providers.begin(); j != end2; ++j)
66 m_providers.push_back(provider);
67 FL_LOG(_log,
LMsg(
"new provider: ") << provider->
getName());
71 if ( m_usedfiles.count(path) ) {
72 FL_WARN(_log,
LMsg(path) <<
" is already used as VFS source");
76 type_providers::const_iterator end = m_providers.end();
77 for (type_providers::const_iterator i = m_providers.begin(); i != end; ++i) {
84 m_usedfiles.insert(path);
87 FL_WARN(_log,
LMsg(provider->
getName()) <<
" thought it could load " << path <<
" but didn't succeed (" << ex.
what() <<
")");
90 FL_WARN(_log,
LMsg(provider->
getName()) <<
" thought it could load " << path <<
" but didn't succeed (unkown exception)");
95 FL_WARN(_log,
LMsg(
"no provider for ") << path <<
" found");
104 FL_WARN(_log,
LMsg(
"Failed to add new VFS source: ") << path);
109 m_sources.push_back(source);
113 type_sources::iterator i = std::find(m_sources.begin(), m_sources.end(), source);
114 if (i != m_sources.end())
118 VFSSource* VFS::getSourceForFile(
const std::string& file)
const {
119 type_sources::const_iterator i = std::find_if(m_sources.begin(), m_sources.end(),
121 if (i == m_sources.end()) {
122 FL_WARN(_log,
LMsg(
"no source for ") << file <<
" found");
130 return getSourceForFile(file) != 0;
134 std::vector<std::string> tokens;
136 const std::string newpath = path +
"/";
137 boost::algorithm::split(tokens, newpath, boost::algorithm::is_any_of(
"/"));
139 std::string currentpath =
"/";
140 std::vector<std::string>::const_iterator token=tokens.begin();
141 while (token != tokens.end()) {
143 if (*token !=
"." && *token !=
".." &&
listDirectories(currentpath, *token).size() == 0) {
146 currentpath += *token +
"/";
156 FL_DBG(_log,
LMsg(
"Opening: ") << path);
158 VFSSource* source = getSourceForFile(path);
160 throw NotFound(path);
162 return source->
open(path);
166 std::set<std::string> list;
167 type_sources::const_iterator end = m_sources.end();
168 for (type_sources::const_iterator i = m_sources.begin(); i != end; ++i) {
169 std::set<std::string> sourcelist = (*i)->listFiles(pathstr);
170 list.insert(sourcelist.begin(), sourcelist.end());
176 std::set<std::string>
VFS::listFiles(
const std::string& path,
const std::string& filterregex)
const {
177 std::set<std::string> list =
listFiles(path);
178 return filterList(list, filterregex);
182 std::set<std::string> list;
183 type_sources::const_iterator end = m_sources.end();
184 for (type_sources::const_iterator i = m_sources.begin(); i != end; ++i) {
185 std::set<std::string> sourcelist = (*i)->listDirectories(pathstr);
186 list.insert(sourcelist.begin(), sourcelist.end());
194 return filterList(list, filterregex);
197 std::set<std::string> VFS::filterList(
const std::set<std::string>& list,
const std::string& fregex)
const {
198 std::set<std::string> results;
199 boost::regex regex(fregex);
200 std::set<std::string>::const_iterator end = list.end();
201 for (std::set<std::string>::const_iterator i = list.begin(); i != end;) {
203 if (boost::regex_match((*i).c_str(), match, regex)) {
bool exists(const std::string &file) const
std::set< std::string > listFiles(const std::string &path) const
void addProvider(VFSSourceProvider *provider)
virtual const char * what() const
virtual bool fileExists(const std::string &file) const =0
const std::string & getName() const
RawData * open(const std::string &path)
virtual VFSSource * createSource(const std::string &file) const =0
virtual bool isReadable(const std::string &file) const =0
VFSSource * createSource(const std::string &path) const
void removeSource(VFSSource *source)
void addSource(VFSSource *source)
bool isDirectory(const std::string &path) const
void addNewSource(const std::string &path)
virtual RawData * open(const std::string &file) const =0
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...
std::set< std::string > listDirectories(const std::string &path) const