FileFinder.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include "FileFinder.hpp"
00030 
00031 #include "ChasteBuildRoot.hpp"
00032 #include "OutputFileHandler.hpp"
00033 #include "Exception.hpp"
00034 #include "GetCurrentWorkingDirectory.hpp"
00035 #include "PetscTools.hpp"
00036 #include <fstream>
00037 #include <cassert>
00038 #include <sys/stat.h>
00039 
00040 
00041 bool FileFinder::msFaking = false;
00042 
00043 RelativeTo::Value FileFinder::msFakeWhat = RelativeTo::Absolute;
00044 
00045 std::string FileFinder::msFakePath = "";
00046 
00047 
00048 FileFinder::FileFinder()
00049     : mAbsPath("UNSET!")
00050 {
00051 }
00052 
00053 FileFinder::FileFinder(const std::string& rRelativePath, RelativeTo::Value relativeTo)
00054 {
00055     SetPath(rRelativePath, relativeTo);
00056 }
00057 
00058 
00059 void FileFinder::SetPath(const std::string& rRelativePath, RelativeTo::Value relativeTo)
00060 {
00061     switch (relativeTo)
00062     {
00063         case RelativeTo::ChasteSourceRoot:
00064             mAbsPath = ChasteBuildRootDir() + rRelativePath;
00065             break;
00066 
00067         case RelativeTo::ChasteTestOutput:
00068             mAbsPath = OutputFileHandler::GetChasteTestOutputDirectory() + rRelativePath;
00069             break;
00070 
00071         case RelativeTo::CWD:
00072             mAbsPath = GetCurrentWorkingDirectory() + "/" + rRelativePath;
00073             break;
00074 
00075         case RelativeTo::Absolute:
00076             mAbsPath = rRelativePath;
00077             break;
00078 
00079         case RelativeTo::AbsoluteOrCwd:
00080             if (FileFinder::IsAbsolutePath(rRelativePath))
00081             {
00082                 mAbsPath = rRelativePath;
00083             }
00084             else
00085             {
00086                 mAbsPath = GetCurrentWorkingDirectory() + "/" + rRelativePath;
00087             }
00088             break;
00089 
00090         default:
00091             // Getting here is impossible
00092             NEVER_REACHED;
00093             break;
00094     }
00095 
00096     if (msFaking && msFakeWhat == relativeTo)
00097     {
00098         // Fake the resulting path
00099         mAbsPath = msFakePath + "/" + rRelativePath;
00100     }
00101 
00102     if (IsDir())
00103     {
00104         if (*(mAbsPath.end()-1) != '/')
00105         {
00106             mAbsPath = mAbsPath + "/";
00107         }
00108     }
00109 }
00110 
00111 bool FileFinder::Exists() const
00112 {
00113     struct stat our_stats;
00114     int retcode = stat(GetAbsolutePath().c_str(), &our_stats);
00115     return (retcode == 0);
00116 }
00117 
00118 bool FileFinder::IsFile() const
00119 {
00120     bool result;
00121     struct stat our_stats;
00122     int retcode = stat(GetAbsolutePath().c_str(), &our_stats);
00123     if (retcode == 0)
00124     {
00125         result = S_ISREG(our_stats.st_mode);
00126     }
00127     else
00128     {
00129         // If it doesn't exist, it isn't a file
00130         result = false;
00131     }
00132     return result;
00133 }
00134 
00135 bool FileFinder::IsDir() const
00136 {
00137     bool result;
00138     struct stat our_stats;
00139     int retcode = stat(GetAbsolutePath().c_str(), &our_stats);
00140     if (retcode == 0)
00141     {
00142         result = S_ISDIR(our_stats.st_mode);
00143     }
00144     else
00145     {
00146         // If it doesn't exist, it isn't a directory
00147         result = false;
00148     }
00149     return result;
00150 }
00151 
00152 std::string FileFinder::GetAbsolutePath() const
00153 {
00154     return mAbsPath;
00155 }
00156 
00157 bool FileFinder::IsNewerThan(const FileFinder& rOtherEntity) const
00158 {
00159     assert(Exists());
00160     assert(rOtherEntity.Exists());
00161     struct stat our_stats, other_stats;
00162     stat(GetAbsolutePath().c_str(), &our_stats);
00163     stat(rOtherEntity.GetAbsolutePath().c_str(), &other_stats);
00164     return our_stats.st_mtime > other_stats.st_mtime;
00165 }
00166 
00167 std::string FileFinder::GetLeafName() const
00168 {
00169     std::string full_name = GetAbsolutePath();
00170     size_t slash = full_name.rfind('/');
00171     assert(slash != std::string::npos);
00172     return full_name.substr(slash+1);
00173 }
00174 
00175 bool FileFinder::IsAbsolutePath(const std::string& rPath)
00176 {
00177     return rPath[0]=='/';
00178 }
00179 
00180 void FileFinder::FakePath(RelativeTo::Value fakeWhat, const std::string& rFakePath)
00181 {
00182     msFakeWhat = fakeWhat;
00183     msFakePath = rFakePath;
00184     msFaking = true;
00185 }
00186 
00187 void FileFinder::StopFaking()
00188 {
00189     msFaking = false;
00190 }

Generated on Tue May 31 14:31:41 2011 for Chaste by  doxygen 1.5.5