FIFE
2008.0
|
00001 /*************************************************************************** 00002 * Copyright (C) 2005-2008 by the FIFE team * 00003 * http://www.fifengine.de * 00004 * This file is part of FIFE. * 00005 * * 00006 * FIFE is free software; you can redistribute it and/or * 00007 * modify it under the terms of the GNU Lesser General Public * 00008 * License as published by the Free Software Foundation; either * 00009 * version 2.1 of the License, or (at your option) any later version. * 00010 * * 00011 * This library is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00014 * Lesser General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU Lesser General Public * 00017 * License along with this library; if not, write to the * 00018 * Free Software Foundation, Inc., * 00019 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00020 ***************************************************************************/ 00021 00022 // Standard C++ library includes 00023 #include <fstream> 00024 00025 // 3rd party library includes 00026 #include <boost/filesystem/operations.hpp> 00027 #include <boost/filesystem/path.hpp> 00028 #include <boost/version.hpp> 00029 00030 // FIFE includes 00031 // These includes are split up in two parts, separated by one empty line 00032 // First block: files included from the FIFE root src directory 00033 // Second block: files included from the same folder 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 // grab the major and minor version of boost, 00045 // calculations taken from boost/version.hpp 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 // this define will tell us to use boost filesystem 00051 // version 3 since this is the default version of the library 00052 // starting in boost version 1.46 and above 00053 #define USE_BOOST_FILESYSTEM_V3 00054 #elif (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 36) 00055 // this define will tell us not to use the deprecated functions 00056 // in boost filesystem version 2 library which were introduced 00057 // in boost version 1.36 and above 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 // Avoid double slashes 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 // boost version 1.46 and above uses 00121 // boost filesystem version 3 as the default 00122 // which has yet a different way of getting 00123 // a filename string 00124 bfs::path filenamePath = i->path().filename(); 00125 list.insert(filenamePath.string()); 00126 #elif defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2) 00127 // the new way in boost filesystem version 2 00128 // to get a filename string 00129 //(this is for boost version 1.36 and above) 00130 list.insert(i->path().filename()); 00131 #else 00132 // the old way in boost filesystem version 2 00133 // to get a filename string 00134 //(this is for boost version 1.35 and below) 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 }