FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
vfsdirectory.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 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 {
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 
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 }
VFSDirectory(VFS *vfs, const std::string &root="./")
Constructor Creates the given file system&#39;s VFS Source, Uses boost_filesystem to achieve Plattform in...
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
VFSSource abstract baseclass.
Definition: vfssource.h:46
virtual RawData * open(const std::string &filename) const
Opens a file.
std::string GetFilenameFromDirectoryIterator(const bfs::directory_iterator &iter)
Helper function to retrieve a filename string from a directory iterator.
static Logger _log(LM_AUDIO)
Exception base class.
Definition: exception.h:43
virtual bool fileExists(const std::string &filename) const
Tests whether a file can be opened.
A RawDataSource for a file on the host system.
Definition: rawdatafile.h:43
std::set< std::string > list(const std::string &path, bool directorys) const
std::set< std::string > listFiles(const std::string &path) const
List files in a directory.
std::string m_root
Definition: vfsdirectory.h:80
virtual ~VFSDirectory()
Destructor.
Definition: modules.h:59
std::set< std::string > listDirectories(const std::string &path) const
List directories in a directory.
the main VFS (virtual file system) class
Definition: vfs.h:58
#define FL_DBG(logger, msg)
Definition: logger.h:70
Used to access diffrent kinds of data.
Definition: rawdata.h:48