FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
vfsdirectory.cpp
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 *
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  static Logger _log(LM_VFS);
40 
41  VFSDirectory::VFSDirectory(VFS* vfs, const std::string& root) : VFSSource(vfs), m_root(root) {
42  FL_DBG(_log, LMsg("VFSDirectory created with root path ") << m_root);
43  if(!m_root.empty() && *(m_root.end() - 1) != '/')
44  m_root.append(1,'/');
45  }
46 
47 
49  }
50 
51 
52  bool VFSDirectory::fileExists(const std::string& name) const {
53  std::string fullFilename = m_root + name;
54 
55  bfs::path fullPath(fullFilename);
56  std::ifstream file(fullPath.string().c_str());
57 
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  return list(path, false);
70  }
71 
72  std::set<std::string> VFSDirectory::listDirectories(const std::string& path) const {
73  return list(path, true);
74  }
75 
76  std::set<std::string> VFSDirectory::list(const std::string& path, bool directorys) const {
77  std::set<std::string> list;
78  std::string dir = m_root;
79 
80  // Avoid double slashes
81  if(path[0] == '/' && m_root[m_root.size()-1] == '/') {
82  dir.append(path.substr(1));
83  }
84  else {
85  dir.append(path);
86  }
87 
88  try {
89  bfs::path boost_path(dir);
90  if (!bfs::exists(boost_path) || !bfs::is_directory(boost_path))
91  return list;
92 
93  bfs::directory_iterator end;
94  for (bfs::directory_iterator i(boost_path); i != end; ++i) {
95  if (bfs::is_directory(*i) != directorys)
96  continue;
97 
98  std::string filename = GetFilenameFromDirectoryIterator(i);
99  if (!filename.empty())
100  {
101  list.insert(filename);
102  }
103  }
104  }
105  catch (const bfs::filesystem_error& ex) {
106  throw Exception(ex.what());
107  }
108 
109  return list;
110  }
111 }
VFSDirectory(VFS *vfs, const std::string &root="./")
virtual RawData * open(const std::string &filename) const
std::string GetFilenameFromDirectoryIterator(const bfs::directory_iterator &iter)
virtual bool fileExists(const std::string &filename) const
std::set< std::string > listFiles(const std::string &path) const
virtual ~VFSDirectory()
std::set< std::string > listDirectories(const std::string &path) const
Definition: vfs.h:58