comparison engine/core/vfs/vfsdirectory.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2008 by the FIFE team *
3 * http://www.fifengine.de *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program 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 *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23 #include <fstream>
24
25 // 3rd party library includes
26 #include <boost/filesystem/operations.hpp>
27 #include <boost/filesystem/path.hpp>
28
29 // FIFE includes
30 // These includes are split up in two parts, separated by one empty line
31 // First block: files included from the FIFE root src directory
32 // Second block: files included from the same folder
33 #include "vfs/raw/rawdata.h"
34 #include "vfs/raw/rawdatafile.h"
35 #include "util/log/logger.h"
36
37 #include "vfsdirectory.h"
38
39 namespace bfs = boost::filesystem;
40 namespace FIFE {
41 static Logger _log(LM_VFS);
42
43 VFSDirectory::VFSDirectory(VFS* vfs, const std::string& root) : VFSSource(vfs), m_root(root) {
44 std::transform(m_root.begin(), m_root.end(), m_root.begin(), tolower);
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 fullpath = m_root + name;
57 std::ifstream file(fullpath.c_str());
58 if (file)
59 return true;
60
61 return false;
62 }
63
64 RawData* VFSDirectory::open(const std::string& file) const {
65 return new RawData(new RawDataFile(m_root + file));
66 }
67
68 std::set<std::string> VFSDirectory::listFiles(const std::string& path) const {
69 std::string dir = m_root;
70 // Avoid double slashes
71 if(path[0] == '/' && m_root[m_root.size()-1] == '/') {
72 dir.append(path.substr(1));
73 }
74 else {
75 dir.append(path);
76 }
77
78 return list(dir, false);
79 }
80
81 std::set<std::string> VFSDirectory::listDirectories(const std::string& path) const {
82 std::string dir = m_root;
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 return list(dir, true);
92 }
93
94 std::set<std::string> VFSDirectory::list(const std::string& path, bool directorys) const {
95 std::set<std::string> list;
96 bfs::path boost_path(m_root + path);
97 if (!bfs::exists(boost_path) || !bfs::is_directory(boost_path))
98 return list;
99
100 bfs::directory_iterator end;
101 for (bfs::directory_iterator i(boost_path); i != end; ++i) {
102 if (bfs::is_directory(*i) != directorys)
103 continue;
104
105 list.insert(i->leaf());
106 }
107
108 return list;
109 }
110 }