OutputFileHandler.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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 
00030 #include "OutputFileHandler.hpp"
00031 
00032 #include <cstdlib>
00033 #include <sys/stat.h>
00034 
00035 #include "PetscTools.hpp"
00036 #include "Exception.hpp"
00037 
00038 #include "ArchiveLocationInfo.hpp"
00039 
00040 
00041 #define CHECK_SYSTEM(cmd) EXPECT0(system, cmd)
00042 
00043 
00044 OutputFileHandler::OutputFileHandler(const std::string &rDirectory,
00045                                      bool cleanOutputDirectory)
00046 {
00047     // Are we the master process?  Only the master should make any new directories
00048     mAmMaster = PetscTools::AmMaster();
00049     mDirectory = GetOutputDirectoryFullPath(rDirectory);
00050     //Is it a valid request for a directory?
00051     if (rDirectory != "" && rDirectory.find("..") == std::string::npos)
00052     {
00053         // Are we the master process?  Only the master should make any new directories
00054         if (cleanOutputDirectory && mAmMaster)
00055         {
00056             std::string directory_to_move_to = GetOutputDirectoryFullPath("last_cleaned_directory");
00057             IGNORE_RET(system, "rm -rf " + directory_to_move_to);
00058             // Re-create the special directory
00059             mkdir(directory_to_move_to.c_str(), 0775);
00060             CHECK_SYSTEM("mv " + mDirectory + " " + directory_to_move_to);
00061             //system(("rm -rf " + mDirectory).c_str());
00062             // Re-create the output directory
00063             mkdir(mDirectory.c_str(), 0775);
00064         }
00065 //This not always collective so we can't have a barrier       PetscTools::Barrier();
00066         struct stat st;
00067         while (stat(mDirectory.c_str(), &st) != 0)
00068         {
00069             //Wait until directory becomes available
00070         }
00071     }
00072 }
00073 
00074 std::string OutputFileHandler::GetChasteTestOutputDirectory()
00075 {
00076     char *chaste_test_output = getenv("CHASTE_TEST_OUTPUT");
00077     std::string directory_root;
00078     if (chaste_test_output == NULL || *chaste_test_output == 0)
00079     {
00080         // Default to 'testoutput' folder within the current directory
00081         directory_root = "./testoutput/";
00082     }
00083     else
00084     {
00085         directory_root = std::string(chaste_test_output);
00086         // Add a trailing slash if not already there
00087         if (! ( *(directory_root.end()-1) == '/'))
00088         {
00089             directory_root = directory_root + "/";
00090         }
00091     }
00092 
00093     return directory_root;
00094 }
00095 
00096 
00097 std::string OutputFileHandler::GetOutputDirectoryFullPath(const std::string& rDirectory)
00098 {
00099     std::string directory_root = GetChasteTestOutputDirectory();
00100     std::string directory = directory_root + rDirectory;
00101     // Make sure it exists (ish)
00102     if (mAmMaster)
00103     {
00104         CHECK_SYSTEM("mkdir -p " + directory);
00105     }
00106 
00107     // Add a trailing slash if not already there
00108     if (! ( *(directory.end()-1) == '/'))
00109     {
00110         directory = directory + "/";
00111     }
00112     return directory;
00113 }
00114 
00115 
00116 std::string OutputFileHandler::GetOutputDirectoryFullPath()
00117 {
00118     return mDirectory;
00119 }
00120 
00121 out_stream OutputFileHandler::OpenOutputFile(const std::string& rFileName,
00122                                              std::ios_base::openmode mode)
00123 {
00124     out_stream p_output_file(new std::ofstream((mDirectory+rFileName).c_str(), mode));
00125     if (!p_output_file->is_open())
00126     {
00127         EXCEPTION("Could not open file " + rFileName + " in " + mDirectory);
00128     }
00129     return p_output_file;
00130 }
00131 
00132 
00133 out_stream OutputFileHandler::OpenOutputFile(const std::string& rFileName,
00134                                              unsigned number,
00135                                              const std::string& rFileFormat,
00136                                              std::ios_base::openmode mode)
00137 {
00138     std::stringstream string_stream;
00139     string_stream << rFileName << number << rFileFormat;
00140     return OpenOutputFile(string_stream.str(), mode);
00141 }
00142 
00143 bool OutputFileHandler::IsMaster()
00144 {
00145     return mAmMaster;
00146 }
00147 
00148 void OutputFileHandler::SetArchiveDirectory()
00149 {
00150     ArchiveLocationInfo::SetArchiveDirectory(GetOutputDirectoryFullPath());
00151 }

Generated on Tue Aug 4 16:10:21 2009 for Chaste by  doxygen 1.5.5