OutputFileHandler.cpp
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 #include "ArchiveLocationInfo.hpp"
00038 #include "GetCurrentWorkingDirectory.hpp"
00039
00040 OutputFileHandler::OutputFileHandler(const std::string &rDirectory,
00041 bool cleanOutputDirectory)
00042 {
00043
00044 if (rDirectory.find("..") != std::string::npos)
00045 {
00046 EXCEPTION("Will not create directory: " + rDirectory +
00047 " due to it potentially being above, and cleaning, CHASTE_TEST_OUTPUT.");
00048 }
00049
00050 mDirectory = MakeFoldersAndReturnFullPath(rDirectory);
00051
00052
00053 if (rDirectory != "" && cleanOutputDirectory)
00054 {
00055 std::string command = "test -e " + mDirectory + ".chaste_deletable_folder";
00056 int return_value = system(command.c_str());
00057 if (return_value != 0)
00058 {
00059 EXCEPTION("Cannot delete " + mDirectory + " because signature file \".chaste_deletable_folder\" is not present.");
00060 }
00061
00062
00063 if (PetscTools::AmMaster())
00064 {
00065
00066
00067 EXPECT0(system, "rm -rf " + mDirectory + "/*");
00068 }
00069
00070 PetscTools::Barrier("OutputFileHandler");
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 = GetCurrentWorkingDirectory() + "/testoutput/";
00082 }
00083 else
00084 {
00085 if (*chaste_test_output != '/')
00086 {
00087
00088 directory_root = GetCurrentWorkingDirectory() + "/" + chaste_test_output;
00089 }
00090 else
00091 {
00092 #define COVERAGE_IGNORE
00093
00094 directory_root = std::string(chaste_test_output);
00095 #undef COVERAGE_IGNORE
00096 }
00097 }
00098 AddTrailingSlash(directory_root);
00099
00100 return directory_root;
00101 }
00102
00103
00104 std::string OutputFileHandler::MakeFoldersAndReturnFullPath(const std::string& rDirectory)
00105 {
00106 std::string directory_root = GetChasteTestOutputDirectory();
00107 std::string directory = directory_root + rDirectory;
00108 AddTrailingSlash(directory);
00109
00110
00111 if (PetscTools::AmMaster())
00112 {
00113
00114 std::string command = "test -d " + directory;
00115 int return_value = system(command.c_str());
00116 if (return_value!=0)
00117 {
00118
00119 EXPECT0(system,"mkdir -p " + directory);
00120
00122 EXPECT0(system,"touch " + directory + ".chaste_deletable_folder");
00123 }
00124 }
00125
00126 PetscTools::Barrier("OutputFileHandler::MakeFoldersAndReturnFullPath");
00127
00128 return directory;
00129 }
00130
00131
00132 std::string OutputFileHandler::GetOutputDirectoryFullPath()
00133 {
00134 return mDirectory;
00135 }
00136
00137 out_stream OutputFileHandler::OpenOutputFile(const std::string& rFileName,
00138 std::ios_base::openmode mode)
00139 {
00140 out_stream p_output_file(new std::ofstream((mDirectory+rFileName).c_str(), mode));
00141 if (!p_output_file->is_open())
00142 {
00143 EXCEPTION("Could not open file \"" + rFileName + "\" in " + mDirectory);
00144 }
00145 return p_output_file;
00146 }
00147
00148
00149 out_stream OutputFileHandler::OpenOutputFile(const std::string& rFileName,
00150 unsigned number,
00151 const std::string& rFileFormat,
00152 std::ios_base::openmode mode)
00153 {
00154 std::stringstream string_stream;
00155 string_stream << rFileName << number << rFileFormat;
00156 return OpenOutputFile(string_stream.str(), mode);
00157 }
00158
00159 void OutputFileHandler::SetArchiveDirectory()
00160 {
00161 ArchiveLocationInfo::SetArchiveDirectory(GetOutputDirectoryFullPath());
00162 }
00163
00164 void OutputFileHandler::AddTrailingSlash(std::string& rDirectory)
00165 {
00166
00167 if (! ( *(rDirectory.end()-1) == '/'))
00168 {
00169 rDirectory = rDirectory + "/";
00170 }
00171 }