00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <string>
00024
00025
00026 #include <boost/filesystem/operations.hpp>
00027 #include <boost/filesystem/path.hpp>
00028 #include <boost/version.hpp>
00029
00030
00031
00032
00033
00034
00035 #include "fife_boost_filesystem.h"
00036
00037 namespace
00038 {
00039
00040
00041 #define BOOST_MAJOR_VERSION BOOST_VERSION / 100000
00042 #define BOOST_MINOR_VERSION BOOST_VERSION / 100 % 1000
00043
00044 #if (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 44 && defined(BOOST_FILESYSTEM_VERSION))
00045 #if (BOOST_FILESYSTEM_VERSION == 2)
00046
00047
00048
00049 #define USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2
00050 #elif (BOOST_FILESYSTEM_VERSION == 3)
00051
00052
00053
00054 #define USE_BOOST_FILESYSTEM_V3
00055 #endif
00056 #elif (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 46)
00057
00058
00059
00060 #define USE_BOOST_FILESYSTEM_V3
00061 #elif (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 36)
00062
00063
00064
00065 #define USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2
00066 #endif
00067 }
00068
00069 namespace FIFE {
00070
00071 bool HasParentPath(const bfs::path& path) {
00072 #if defined(USE_BOOST_FILESYSTEM_V3) || defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2)
00073 return path.has_parent_path();
00074 #else
00075 return path.has_branch_path();
00076 #endif
00077 }
00078
00079 bfs::path GetParentPath(const bfs::path& path) {
00080 #if defined(USE_BOOST_FILESYSTEM_V3) || defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2)
00081 return path.parent_path();
00082 #else
00083 return path.branch_path();
00084 #endif
00085 }
00086
00087 std::string GetFilenameFromPath(const bfs::path& path) {
00088 #if defined(USE_BOOST_FILESYSTEM_V3)
00089
00090
00091
00092
00093 return path.filename().string();
00094 #elif defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2)
00095
00096
00097
00098 return path.filename();
00099 #else
00100
00101
00102
00103 return path.leaf();
00104 #endif
00105 }
00106
00107 std::string GetFilenameFromDirectoryIterator(const bfs::directory_iterator& iter) {
00108 bfs::directory_iterator badIter;
00109
00110
00111 if (iter == badIter) {
00112 return "";
00113 }
00114
00115 #if defined(USE_BOOST_FILESYSTEM_V3)
00116
00117
00118
00119
00120 return iter->path().filename().string();
00121 #elif defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2)
00122
00123
00124
00125 return iter->path().filename();
00126 #else
00127
00128
00129
00130 return iter->leaf();
00131 #endif
00132 }
00133
00134 std::string GetPathIteratorAsString(const bfs::path::iterator& pathIter)
00135 {
00136 #if defined(USE_BOOST_FILESYSTEM_V3)
00137
00138
00139
00140
00141 return (*pathIter).string();
00142 #else
00143
00144
00145
00146 return (*pathIter);
00147 #endif
00148 }
00149
00150 bfs::path GetAbsolutePath(const std::string& path) {
00151 return GetAbsolutePath(bfs::path(path));
00152 }
00153
00154 bfs::path GetAbsolutePath(const bfs::path& path) {
00155 #if defined(USE_BOOST_FILESYSTEM_V3)
00156 return bfs::absolute(path);
00157 #else
00158 return bfs::complete(path);
00159 #endif
00160 }
00161
00162 bool HasExtension(const std::string& path) {
00163 return HasExtension(bfs::path(path));
00164 }
00165
00166 bool HasExtension(const bfs::path& path) {
00167
00168
00169
00170
00171
00172
00173
00174
00175 std::string extension = GetExtension(path);
00176 if (extension.empty() || extension == ".") {
00177 return false;
00178 }
00179 else {
00180 return true;
00181 }
00182 }
00183
00184 std::string GetExtension(const std::string& path) {
00185 return GetExtension(bfs::path(path));
00186 }
00187
00188 std::string GetExtension(const bfs::path& path) {
00189 #if defined(USE_BOOST_FILESYSTEM_V3)
00190 return path.extension().string();
00191 #else
00192 return bfs::extension(path);
00193 #endif
00194 }
00195
00196 std::string GetStem(const std::string& path){
00197 return GetStem(bfs::path(path));
00198 }
00199
00200 std::string GetStem(const bfs::path& path) {
00201 #if defined(USE_BOOST_FILESYSTEM_V3)
00202 if (!HasExtension(path)) {
00203
00204 return "";
00205 }
00206 else {
00207 return path.stem().string();
00208 }
00209 #else
00210 if (!HasExtension(path)) {
00211
00212 return "";
00213 }
00214 else {
00215 return path.stem();
00216 }
00217 #endif
00218 }
00219 }