| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
// Standard C++ library includes |
| 23 |
|
| 24 |
// 3rd party library includes |
| 25 |
|
| 26 |
// FIFE includes |
| 27 |
// These includes are split up in two parts, separated by one empty line |
| 28 |
// First block: files included from the FIFE root src directory |
| 29 |
// Second block: files included from the same folder |
| 30 |
#include "vfs/raw/rawdata.h" |
| 31 |
#include "vfs/raw/rawdatafile.h" |
| 32 |
#include "util/log/logger.h" |
| 33 |
#include "util/base/exception.h" |
| 34 |
|
| 35 |
#include "fife_boost_filesystem.h" |
| 36 |
#include "vfsdirectory.h" |
| 37 |
|
| 38 |
namespace FIFE { |
| 39 |
/** Logger to use for this source file. |
| 40 |
* @relates Logger |
| 41 |
*/ |
| 42 |
static Logger _log(LM_VFS); |
| 43 |
|
| 44 |
VFSDirectory::VFSDirectory(VFS* vfs, const std::string& root) : VFSSource(vfs), m_root(root) { |
| 45 |
FL_DBG(_log, LMsg("VFSDirectory created with root path ") << m_root); |
| 46 |
if(!m_root.empty() && *(m_root.end() - 1) != '/') |
| 47 |
m_root.append(1,'/'); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
VFSDirectory::~VFSDirectory() { |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
bool VFSDirectory::fileExists(const std::string& name) const { |
| 56 |
std::string fullFilename = m_root + name; |
| 57 |
|
| 58 |
bfs::path fullPath(fullFilename); |
| 59 |
std::ifstream file(fullPath.string().c_str()); |
| 60 |
|
| 61 |
if (file) |
| 62 |
return true; |
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
RawData* VFSDirectory::open(const std::string& file) const { |
| 68 |
return new RawData(new RawDataFile(m_root + file)); |
| 69 |
} |
| 70 |
|
| 71 |
std::set<std::string> VFSDirectory::listFiles(const std::string& path) const { |
| 72 |
return list(path, false); |
| 73 |
} |
| 74 |
|
| 75 |
std::set<std::string> VFSDirectory::listDirectories(const std::string& path) const { |
| 76 |
return list(path, true); |
| 77 |
} |
| 78 |
|
| 79 |
std::set<std::string> VFSDirectory::list(const std::string& path, bool directorys) const { |
| 80 |
std::set<std::string> list; |
| 81 |
std::string dir = m_root; |
| 82 |
|
| 83 |
// Avoid double slashes |
| 84 |
if(path[0] == '/' && m_root[m_root.size()-1] == '/') { |
| 85 |
dir.append(path.substr(1)); |
| 86 |
} |
| 87 |
else { |
| 88 |
dir.append(path); |
| 89 |
} |
| 90 |
|
| 91 |
try { |
| 92 |
bfs::path boost_path(dir); |
| 93 |
if (!bfs::exists(boost_path) || !bfs::is_directory(boost_path)) |
| 94 |
return list; |
| 95 |
|
| 96 |
bfs::directory_iterator end; |
| 97 |
for (bfs::directory_iterator i(boost_path); i != end; ++i) { |
| 98 |
if (bfs::is_directory(*i) != directorys) |
| 99 |
continue; |
| 100 |
|
| 101 |
std::string filename = GetFilenameFromDirectoryIterator(i); |
| 102 |
if (!filename.empty()) |
| 103 |
{ |
| 104 |
list.insert(filename); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
catch (const bfs::filesystem_error& ex) { |
| 109 |
throw Exception(ex.what()); |
| 110 |
} |
| 111 |
|
| 112 |
return list; |
| 113 |
} |
| 114 |
} |