fife_boost_filesystem.cpp

00001 /***************************************************************************
00002 *   Copyright (C) 2005-2008 by the FIFE team                              *
00003 *   http://www.fifengine.de                                               *
00004 *   This file is part of FIFE.                                            *
00005 *                                                                         *
00006 *   FIFE is free software; you can redistribute it and/or                 *
00007 *   modify it under the terms of the GNU Lesser General Public            *
00008 *   License as published by the Free Software Foundation; either          *
00009 *   version 2.1 of the License, or (at your option) any later version.    *
00010 *                                                                         *
00011 *   This library is distributed in the hope that it will be useful,       *
00012 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014 *   Lesser General Public License for more details.                       *
00015 *                                                                         *
00016 *   You should have received a copy of the GNU Lesser General Public      *
00017 *   License along with this library; if not, write to the                 *
00018 *   Free Software Foundation, Inc.,                                       *
00019 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020 ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 #include <string>
00024 
00025 // 3rd party library includes
00026 #include <boost/filesystem/operations.hpp>
00027 #include <boost/filesystem/path.hpp>
00028 #include <boost/version.hpp>
00029 
00030 // FIFE includes
00031 // These includes are split up in two parts, separated by one empty line
00032 // First block: files included from the FIFE root src directory
00033 // Second block: files included from the same folder
00034 
00035 #include "fife_boost_filesystem.h"
00036 
00037 namespace 
00038 {
00039     // grab the major and minor version of boost, 
00040     // calculations taken from boost/version.hpp
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         // if this macro is defined to 2 the the user wants to
00047         // force the use of boost filesystem version 2 so we
00048         // will set our internal macros correctly to do that
00049         #define USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2
00050     #elif (BOOST_FILESYSTEM_VERSION == 3)
00051         // if this macro is set to 3 then the user wants to force
00052         // the use of boost filesystem version 3 so we will set
00053         // our internal macros correctly to do that
00054         #define USE_BOOST_FILESYSTEM_V3
00055     #endif
00056 #elif (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 46)
00057     // this define will tell us to use boost filesystem
00058     // version 3 since this is the default version of the library
00059     // starting in boost version 1.46 and above
00060     #define USE_BOOST_FILESYSTEM_V3
00061 #elif (BOOST_MAJOR_VERSION >= 1 && BOOST_MINOR_VERSION >= 36)
00062     // this define will tell us not to use the deprecated functions
00063     // in boost filesystem version 2 library which were introduced
00064     // in boost version 1.36 and above
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             // boost version 1.46 and above uses
00090             // boost filesystem version 3 as the default
00091             // which has yet a different way of getting
00092             // a filename string
00093             return path.filename().string();
00094         #elif defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2)
00095             // the new way in boost filesystem version 2
00096             // to get a filename string
00097             //(this is for boost version 1.36 and above)
00098             return path.filename();
00099         #else
00100             // the old way in boost filesystem version 2
00101             // to get a filename string 
00102             //(this is for boost version 1.35 and below)
00103             return path.leaf();
00104         #endif
00105     }
00106 
00107     std::string GetFilenameFromDirectoryIterator(const bfs::directory_iterator& iter) {
00108         bfs::directory_iterator badIter;
00109 
00110         // early exit for bad directory_iterator parameter
00111         if (iter == badIter) {
00112             return "";
00113         }
00114 
00115         #if defined(USE_BOOST_FILESYSTEM_V3)
00116             // boost version 1.46 and above uses
00117             // boost filesystem version 3 as the default
00118             // which has yet a different way of getting
00119             // a filename string
00120             return iter->path().filename().string();
00121         #elif defined(USE_NON_DEPRECATED_BOOST_FILESYSTEM_V2)
00122             // the new way in boost filesystem version 2
00123             // to get a filename string
00124             //(this is for boost version 1.36 and above)
00125             return iter->path().filename();
00126         #else
00127             // the old way in boost filesystem version 2
00128             // to get a filename string 
00129             //(this is for boost version 1.35 and below)
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             // in boost::filesystem v3 the path iterator is now
00138             // represented by a path object internally so we
00139             // must now additionally call the .string() method
00140             // to get an std::string representation
00141             return (*pathIter).string();
00142         #else
00143             // in boost::filesystem v2 the path iterator 
00144             // holds the string representation so we can
00145             // use it directly
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             // not sure this gives the same results as below
00168             // because the "." will be included in the extension
00169             // meaning that this may return true for a path that
00170             // is simply has an empty extension
00171             // see link for more information:
00172             // http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#path-extension
00173             //return path.has_extension();
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                 // if no extension return empty string
00204                 return "";
00205             }
00206             else {
00207                 return path.stem().string();
00208             }
00209         #else
00210             if (!HasExtension(path)) {
00211                 // if no extension, return empty string
00212                 return "";
00213             }
00214             else {
00215                 return path.stem();
00216             }
00217         #endif
00218     }
00219 }