00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "vfs/raw/rawdata.h"
00031 #include "vfs/raw/rawdatafile.h"
00032 #include "util/log/logger.h"
00033 #include "util/base/exception.h"
00034
00035 #include "fife_boost_filesystem.h"
00036 #include "vfsdirectory.h"
00037
00038 namespace FIFE {
00039 static Logger _log(LM_VFS);
00040
00041 VFSDirectory::VFSDirectory(VFS* vfs, const std::string& root) : VFSSource(vfs), m_root(root) {
00042 FL_DBG(_log, LMsg("VFSDirectory created with root path ") << m_root);
00043 if(!m_root.empty() && *(m_root.end() - 1) != '/')
00044 m_root.append(1,'/');
00045 }
00046
00047
00048 VFSDirectory::~VFSDirectory() {
00049 }
00050
00051
00052 bool VFSDirectory::fileExists(const std::string& name) const {
00053 std::string fullFilename = m_root + name;
00054
00055 bfs::path fullPath(fullFilename);
00056 std::ifstream file(fullPath.string().c_str());
00057
00058 if (file)
00059 return true;
00060
00061 return false;
00062 }
00063
00064 RawData* VFSDirectory::open(const std::string& file) const {
00065 return new RawData(new RawDataFile(m_root + file));
00066 }
00067
00068 std::set<std::string> VFSDirectory::listFiles(const std::string& path) const {
00069 return list(path, false);
00070 }
00071
00072 std::set<std::string> VFSDirectory::listDirectories(const std::string& path) const {
00073 return list(path, true);
00074 }
00075
00076 std::set<std::string> VFSDirectory::list(const std::string& path, bool directorys) const {
00077 std::set<std::string> list;
00078 std::string dir = m_root;
00079
00080
00081 if(path[0] == '/' && m_root[m_root.size()-1] == '/') {
00082 dir.append(path.substr(1));
00083 }
00084 else {
00085 dir.append(path);
00086 }
00087
00088 try {
00089 bfs::path boost_path(dir);
00090 if (!bfs::exists(boost_path) || !bfs::is_directory(boost_path))
00091 return list;
00092
00093 bfs::directory_iterator end;
00094 for (bfs::directory_iterator i(boost_path); i != end; ++i) {
00095 if (bfs::is_directory(*i) != directorys)
00096 continue;
00097
00098 std::string filename = GetFilenameFromDirectoryIterator(i);
00099 if (!filename.empty())
00100 {
00101 list.insert(filename);
00102 }
00103 }
00104 }
00105 catch (const bfs::filesystem_error& ex) {
00106 throw Exception(ex.what());
00107 }
00108
00109 return list;
00110 }
00111 }