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 #define CHECK_SYSTEM(cmd) EXPECT0(system, cmd)
00039 
00040 
00041 OutputFileHandler::OutputFileHandler(const std::string &rDirectory,
00042                                      bool rCleanOutputDirectory)
00043 {
00044     // Are we the master process?  Only the master should do any writing to disk
00045     mAmMaster = PetscTools::AmMaster();
00046     mDirectory = GetOutputDirectoryFullPath(rDirectory);
00047 
00048     // Clean the output dir?
00049     if (rCleanOutputDirectory && mAmMaster &&
00050         rDirectory != "" && rDirectory.find("..") == std::string::npos)
00051     {
00052         std::string directory_to_move_to = GetOutputDirectoryFullPath("last_cleaned_directory");
00053         IGNORE_RET(system, "rm -rf " + directory_to_move_to);
00054         // Re-create the special directory
00055         mkdir(directory_to_move_to.c_str(), 0775);
00056         CHECK_SYSTEM("mv " + mDirectory + " " + directory_to_move_to);
00057         //system(("rm -rf " + mDirectory).c_str());
00058         // Re-create the output directory
00059         mkdir(mDirectory.c_str(), 0775);
00060     }
00061 }
00062 
00063 std::string OutputFileHandler::GetChasteTestOutputDirectory()
00064 {
00065     char *chaste_test_output = getenv("CHASTE_TEST_OUTPUT");
00066     std::string directory_root;
00067     if (chaste_test_output == NULL || *chaste_test_output == 0)
00068     {
00069         // Default to 'testoutput' folder within the current directory
00070         directory_root = "./testoutput/";
00071     }
00072     else
00073     {
00074         directory_root = std::string(chaste_test_output);
00075         // Add a trailing slash if not already there
00076         if (! ( *(directory_root.end()-1) == '/'))
00077         {
00078             directory_root = directory_root + "/";
00079         }
00080     }
00081 
00082     return directory_root;
00083 }
00084 
00085 
00086 std::string OutputFileHandler::GetOutputDirectoryFullPath(std::string directory)
00087 {
00088     std::string directory_root = GetChasteTestOutputDirectory();
00089     directory = directory_root + directory;
00090     // Make sure it exists (ish)
00091     if (mAmMaster)
00092     {
00093         CHECK_SYSTEM("mkdir -p " + directory);
00094     }
00095 
00096     // Add a trailing slash if not already there
00097     if (! ( *(directory.end()-1) == '/'))
00098     {
00099         directory = directory + "/";
00100     }
00101     return directory;
00102 }
00103 
00104 
00105 std::string OutputFileHandler::GetOutputDirectoryFullPath()
00106 {
00107     return mDirectory;
00108 }
00109 
00110 
00111 out_stream OutputFileHandler::OpenOutputFile(std::string fileName,
00112                                              std::ios_base::openmode mode)
00113 {
00114     out_stream p_output_file(new std::ofstream((mDirectory+fileName).c_str(), mode));
00115     if (!p_output_file->is_open())
00116     {
00117         EXCEPTION("Could not open file " + fileName + " in " + mDirectory);
00118     }
00119     return p_output_file;
00120 }
00121 
00122 
00123 out_stream OutputFileHandler::OpenOutputFile(std::string fileName,
00124                                              unsigned number,
00125                                              std::string fileFormat,
00126                                              std::ios_base::openmode mode)
00127 {
00128     std::stringstream string_stream;
00129     string_stream << fileName << number << fileFormat;
00130     return OpenOutputFile(string_stream.str(), mode);
00131 }
00132 
00133 bool OutputFileHandler::IsMaster()
00134 {
00135     return mAmMaster;
00136 }

Generated on Wed Mar 18 12:51:50 2009 for Chaste by  doxygen 1.5.5