vfsdirectory.cpp

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 
00024 // 3rd party library includes
00025 
00026 // FIFE includes
00027 // These includes are split up in two parts, separated by one empty line
00028 // First block: files included from the FIFE root src directory
00029 // Second block: files included from the same folder
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         // Avoid double slashes
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 }