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
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
00048 mAmMaster = PetscTools::AmMaster();
00049 mDirectory = GetOutputDirectoryFullPath(rDirectory);
00050
00051 if (rDirectory != "" && rDirectory.find("..") == std::string::npos)
00052 {
00053
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
00059 mkdir(directory_to_move_to.c_str(), 0775);
00060 CHECK_SYSTEM("mv " + mDirectory + " " + directory_to_move_to);
00061
00062
00063 mkdir(mDirectory.c_str(), 0775);
00064 }
00065
00066 struct stat st;
00067 while (stat(mDirectory.c_str(), &st) != 0)
00068 {
00069
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
00081 directory_root = "./testoutput/";
00082 }
00083 else
00084 {
00085 directory_root = std::string(chaste_test_output);
00086
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
00102 if (mAmMaster)
00103 {
00104 CHECK_SYSTEM("mkdir -p " + directory);
00105 }
00106
00107
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 }