vfsdirectory.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <fstream>
00024
00025
00026 #include <boost/filesystem/operations.hpp>
00027 #include <boost/filesystem/path.hpp>
00028 #include <boost/version.hpp>
00029
00030
00031
00032
00033
00034 #include "vfs/raw/rawdata.h"
00035 #include "vfs/raw/rawdatafile.h"
00036 #include "util/log/logger.h"
00037 #include "util/base/exception.h"
00038 #include "vfsdirectory.h"
00039
00040 namespace bfs = boost::filesystem;
00041
00042 namespace
00043 {
00044
00045
00046 #define BOOST_MAJOR_VERSION BOOST_VERSION / 100000
00047 #define BOOST_MINOR_VERSION BOOST_VERSION / 100 % 1000
00048
00049 #if (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 46)
00050
00051
00052
00053 #define USE_BOOST_FILESYSTEM_V3
00054 #elif (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 36)
00055
00056
00057
00058 #define USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2
00059 #endif
00060 }
00061
00062 namespace FIFE {
00063 static Logger _log(LM_VFS);
00064
00065 VFSDirectory::VFSDirectory(VFS* vfs, const std::string& root) : VFSSource(vfs), m_root(root) {
00066 FL_DBG(_log, LMsg("VFSDirectory created with root path ") << m_root);
00067 if(!m_root.empty() && *(m_root.end() - 1) != '/')
00068 m_root.append(1,'/');
00069 }
00070
00071
00072 VFSDirectory::~VFSDirectory() {
00073 }
00074
00075
00076 bool VFSDirectory::fileExists(const std::string& name) const {
00077 std::string fullpath = m_root + name;
00078 std::ifstream file(fullpath.c_str());
00079 if (file)
00080 return true;
00081
00082 return false;
00083 }
00084
00085 RawData* VFSDirectory::open(const std::string& file) const {
00086 return new RawData(new RawDataFile(m_root + file));
00087 }
00088
00089 std::set<std::string> VFSDirectory::listFiles(const std::string& path) const {
00090 return list(path, false);
00091 }
00092
00093 std::set<std::string> VFSDirectory::listDirectories(const std::string& path) const {
00094 return list(path, true);
00095 }
00096
00097 std::set<std::string> VFSDirectory::list(const std::string& path, bool directorys) const {
00098 std::set<std::string> list;
00099 std::string dir = m_root;
00100
00101
00102 if(path[0] == '/' && m_root[m_root.size()-1] == '/') {
00103 dir.append(path.substr(1));
00104 }
00105 else {
00106 dir.append(path);
00107 }
00108
00109 try {
00110 bfs::path boost_path(dir);
00111 if (!bfs::exists(boost_path) || !bfs::is_directory(boost_path))
00112 return list;
00113
00114 bfs::directory_iterator end;
00115 for (bfs::directory_iterator i(boost_path); i != end; ++i) {
00116 if (bfs::is_directory(*i) != directorys)
00117 continue;
00118
00119 #if defined(USE_BOOST_FILESYSTEM_V3)
00120
00121
00122
00123
00124 bfs::path filenamePath = i->path().filename();
00125 list.insert(filenamePath.string());
00126 #elif defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2)
00127
00128
00129
00130 list.insert(i->path().filename());
00131 #else
00132
00133
00134
00135 list.insert(i->leaf());
00136 #endif
00137 }
00138 }
00139 catch (const bfs::filesystem_error& ex) {
00140 throw Exception(ex.what());
00141 }
00142
00143 return list;
00144 }
00145 }