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 #include <fstream>
24 
25 // 3rd party library includes
26 #include <boost/filesystem/operations.hpp>
27 #include <boost/filesystem/path.hpp>
28 #include <boost/version.hpp>
29 
30 // FIFE includes
31 // These includes are split up in two parts, separated by one empty line
32 // First block: files included from the FIFE root src directory
33 // Second block: files included from the same folder
34 #include "vfs/raw/rawdata.h"
35 #include "vfs/raw/rawdatafile.h"
36 #include "util/log/logger.h"
37 #include "util/base/exception.h"
38 #include "vfsdirectory.h"
39 
40 namespace bfs = boost::filesystem;
41 
42 namespace
43 {
44  // grab the major and minor version of boost,
45  // calculations taken from boost/version.hpp
46  #define BOOST_MAJOR_VERSION BOOST_VERSION / 100000
47  #define BOOST_MINOR_VERSION BOOST_VERSION / 100 % 1000
48 
49 #if (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 46)
50  // this define will tell us to use boost filesystem
51  // version 3 since this is the default version of the library
52  // starting in boost version 1.46 and above
53  #define USE_BOOST_FILESYSTEM_V3
54 #elif (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 36)
55  // this define will tell us not to use the deprecated functions
56  // in boost filesystem version 2 library which were introduced
57  // in boost version 1.36 and above
58  #define USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2
59 #endif
60 }
61 
62 namespace FIFE {
63  static Logger _log(LM_VFS);
64 
65  VFSDirectory::VFSDirectory(VFS* vfs, const std::string& root) : VFSSource(vfs), m_root(root) {
66  FL_DBG(_log, LMsg("VFSDirectory created with root path ") << m_root);
67  if(!m_root.empty() && *(m_root.end() - 1) != '/')
68  m_root.append(1,'/');
69  }
70 
71 
73  }
74 
75 
76  bool VFSDirectory::fileExists(const std::string& name) const {
77  std::string fullpath = m_root + name;
78  std::ifstream file(fullpath.c_str());
79  if (file)
80  return true;
81 
82  return false;
83  }
84 
85  RawData* VFSDirectory::open(const std::string& file) const {
86  return new RawData(new RawDataFile(m_root + file));
87  }
88 
89  std::set<std::string> VFSDirectory::listFiles(const std::string& path) const {
90  return list(path, false);
91  }
92 
93  std::set<std::string> VFSDirectory::listDirectories(const std::string& path) const {
94  return list(path, true);
95  }
96 
97  std::set<std::string> VFSDirectory::list(const std::string& path, bool directorys) const {
98  std::set<std::string> list;
99  std::string dir = m_root;
100 
101  // Avoid double slashes
102  if(path[0] == '/' && m_root[m_root.size()-1] == '/') {
103  dir.append(path.substr(1));
104  }
105  else {
106  dir.append(path);
107  }
108 
109  try {
110  bfs::path boost_path(dir);
111  if (!bfs::exists(boost_path) || !bfs::is_directory(boost_path))
112  return list;
113 
114  bfs::directory_iterator end;
115  for (bfs::directory_iterator i(boost_path); i != end; ++i) {
116  if (bfs::is_directory(*i) != directorys)
117  continue;
118 
119 #if defined(USE_BOOST_FILESYSTEM_V3)
120  // boost version 1.46 and above uses
121  // boost filesystem version 3 as the default
122  // which has yet a different way of getting
123  // a filename string
124  bfs::path filenamePath = i->path().filename();
125  list.insert(filenamePath.string());
126 #elif defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2)
127  // the new way in boost filesystem version 2
128  // to get a filename string
129  //(this is for boost version 1.36 and above)
130  list.insert(i->path().filename());
131 #else
132  // the old way in boost filesystem version 2
133  // to get a filename string
134  //(this is for boost version 1.35 and below)
135  list.insert(i->leaf());
136 #endif
137  }
138  }
139  catch (const bfs::filesystem_error& ex) {
140  throw Exception(ex.what());
141  }
142 
143  return list;
144  }
145 }