FileFinder.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
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 <fstream>
00036 #include <sys/stat.h>
00037 
00038 FileFinder::FileFinder(const cp::path_type& rPath)
00039 {
00040     SetAbsolutePath(rPath);
00041 }
00042 
00043 FileFinder::FileFinder(const std::string& rPath, cp::relative_to_type relativeTo)
00044 {
00045     cp::path_type path(rPath);
00046     path.relative_to(relativeTo);
00047     SetAbsolutePath(path);
00048 }
00049 
00050 
00051 void FileFinder::SetAbsolutePath(const cp::path_type& rPath)
00052 {
00053     std::string leaf_path(rPath);
00054 
00055     switch (rPath.relative_to())
00056     {
00057         case cp::relative_to_type::chaste_source_root:
00058             mAbsPath = ChasteBuildRootDir() + leaf_path;
00059             break;
00060 
00061         case cp::relative_to_type::chaste_test_output:
00062             mAbsPath = OutputFileHandler::GetChasteTestOutputDirectory() + leaf_path;
00063             break;
00064 
00065         case cp::relative_to_type::cwd:
00066             mAbsPath = GetCurrentWorkingDirectory() + "/" + leaf_path;
00067             break;
00068 
00069         case cp::relative_to_type::absolute:
00070             mAbsPath = leaf_path;
00071             break;
00072 
00073         default:
00074             // Getting here is impossible due to the schema
00075             NEVER_REACHED;
00076             break;
00077     }
00078 }
00079 
00080 bool FileFinder::Exists() const
00081 {
00082     std::ifstream file(mAbsPath.c_str());
00083     bool exists = file.is_open();
00084     file.close();
00085 
00086     return exists;
00087 }
00088 
00089 std::string FileFinder::GetAbsolutePath() const
00090 {
00091     return mAbsPath;
00092 }
00093 
00094 bool FileFinder::IsNewerThan(const FileFinder& rOtherFile) const
00095 {
00096     assert(Exists());
00097     assert(rOtherFile.Exists());
00098     struct stat our_stats, other_stats;
00099     stat(GetAbsolutePath().c_str(), &our_stats);
00100     stat(rOtherFile.GetAbsolutePath().c_str(), &other_stats);
00101     return our_stats.st_mtime > other_stats.st_mtime;
00102 }

Generated by  doxygen 1.6.2