00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00092 NEVER_REACHED;
00093 break;
00094 }
00095
00096 if (msFaking && msFakeWhat == relativeTo)
00097 {
00098
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
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
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 }