Debian Codegraph @

engine/core/vfs/fife_boost_filesystem.cpp

raw
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 #include <string>
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
35 #include "fife_boost_filesystem.h"
36
37 namespace
38 {
39 // grab the major and minor version of boost,
40 // calculations taken from boost/version.hpp
41 #define BOOST_MAJOR_VERSION BOOST_VERSION / 100000
42 #define BOOST_MINOR_VERSION BOOST_VERSION / 100 % 1000
43
44 #if (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 44 && defined(BOOST_FILESYSTEM_VERSION))
45 #if (BOOST_FILESYSTEM_VERSION == 2)
46 // if this macro is defined to 2 the the user wants to
47 // force the use of boost filesystem version 2 so we
48 // will set our internal macros correctly to do that
49 #define USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2
50 #elif (BOOST_FILESYSTEM_VERSION == 3)
51 // if this macro is set to 3 then the user wants to force
52 // the use of boost filesystem version 3 so we will set
53 // our internal macros correctly to do that
54 #define USE_BOOST_FILESYSTEM_V3
55 #endif
56 #elif (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 46)
57 // this define will tell us to use boost filesystem
58 // version 3 since this is the default version of the library
59 // starting in boost version 1.46 and above
60 #define USE_BOOST_FILESYSTEM_V3
61 #elif (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 36)
62 // this define will tell us not to use the deprecated functions
63 // in boost filesystem version 2 library which were introduced
64 // in boost version 1.36 and above
65 #define USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2
66 #endif
67 }
68
69 namespace FIFE {
70
71 bool HasParentPath(const bfs::path& path) {
72 #if defined(USE_BOOST_FILESYSTEM_V3) || defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2)
73 return path.has_parent_path();
74 #else
75 return path.has_branch_path();
76 #endif
77 }
78
79 bfs::path GetParentPath(const bfs::path& path) {
80 #if defined(USE_BOOST_FILESYSTEM_V3) || defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2)
81 return path.parent_path();
82 #else
83 return path.branch_path();
84 #endif
85 }
86
87 std::string GetFilenameFromPath(const bfs::path& path) {
88 #if defined(USE_BOOST_FILESYSTEM_V3)
89 // boost version 1.46 and above uses
90 // boost filesystem version 3 as the default
91 // which has yet a different way of getting
92 // a filename string
93 return path.filename().string();
94 #elif defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2)
95 // the new way in boost filesystem version 2
96 // to get a filename string
97 //(this is for boost version 1.36 and above)
98 return path.filename();
99 #else
100 // the old way in boost filesystem version 2
101 // to get a filename string
102 //(this is for boost version 1.35 and below)
103 return path.leaf();
104 #endif
105 }
106
107 std::string GetFilenameFromDirectoryIterator(const bfs::directory_iterator& iter) {
108 bfs::directory_iterator badIter;
109
110 // early exit for bad directory_iterator parameter
111 if (iter == badIter) {
112 return "";
113 }
114
115 #if defined(USE_BOOST_FILESYSTEM_V3)
116 // boost version 1.46 and above uses
117 // boost filesystem version 3 as the default
118 // which has yet a different way of getting
119 // a filename string
120 return iter->path().filename().string();
121 #elif defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2)
122 // the new way in boost filesystem version 2
123 // to get a filename string
124 //(this is for boost version 1.36 and above)
125 return iter->path().filename();
126 #else
127 // the old way in boost filesystem version 2
128 // to get a filename string
129 //(this is for boost version 1.35 and below)
130 return iter->leaf();
131 #endif
132 }
133
134 std::string GetPathIteratorAsString(const bfs::path::iterator& pathIter)
135 {
136 #if defined(USE_BOOST_FILESYSTEM_V3)
137 // in boost::filesystem v3 the path iterator is now
138 // represented by a path object internally so we
139 // must now additionally call the .string() method
140 // to get an std::string representation
141 return (*pathIter).string();
142 #else
143 // in boost::filesystem v2 the path iterator
144 // holds the string representation so we can
145 // use it directly
146 return (*pathIter);
147 #endif
148 }
149
150 bfs::path GetAbsolutePath(const std::string& path) {
151 return GetAbsolutePath(bfs::path(path));
152 }
153
154 bfs::path GetAbsolutePath(const bfs::path& path) {
155 #if defined(USE_BOOST_FILESYSTEM_V3)
156 return bfs::absolute(path);
157 #else
158 return bfs::complete(path);
159 #endif
160 }
161
162 bool HasExtension(const std::string& path) {
163 return HasExtension(bfs::path(path));
164 }
165
166 bool HasExtension(const bfs::path& path) {
167 // not sure this gives the same results as below
168 // because the "." will be included in the extension
169 // meaning that this may return true for a path that
170 // is simply has an empty extension
171 // see link for more information:
172 // http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#path-extension
173 //return path.has_extension();
174
175 std::string extension = GetExtension(path);
176 if (extension.empty() || extension == ".") {
177 return false;
178 }
179 else {
180 return true;
181 }
182 }
183
184 std::string GetExtension(const std::string& path) {
185 return GetExtension(bfs::path(path));
186 }
187
188 std::string GetExtension(const bfs::path& path) {
189 #if defined(USE_BOOST_FILESYSTEM_V3)
190 return path.extension().string();
191 #else
192 return bfs::extension(path);
193 #endif
194 }
195
196 std::string GetStem(const std::string& path){
197 return GetStem(bfs::path(path));
198 }
199
200 std::string GetStem(const bfs::path& path) {
201 #if defined(USE_BOOST_FILESYSTEM_V3)
202 if (!HasExtension(path)) {
203 // if no extension return empty string
204 return "";
205 }
206 else {
207 return path.stem().string();
208 }
209 #else
210 if (!HasExtension(path)) {
211 // if no extension, return empty string
212 return "";
213 }
214 else {
215 return path.stem();
216 }
217 #endif
218 }
219 }