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 #define CHECK_SYSTEM(cmd) EXPECT0(system, cmd)
00039
00040
00041 OutputFileHandler::OutputFileHandler(const std::string &rDirectory,
00042 bool rCleanOutputDirectory)
00043 {
00044
00045 mAmMaster = PetscTools::AmMaster();
00046 mDirectory = GetOutputDirectoryFullPath(rDirectory);
00047
00048
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
00055 mkdir(directory_to_move_to.c_str(), 0775);
00056 CHECK_SYSTEM("mv " + mDirectory + " " + directory_to_move_to);
00057
00058
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
00070 directory_root = "./testoutput/";
00071 }
00072 else
00073 {
00074 directory_root = std::string(chaste_test_output);
00075
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
00091 if (mAmMaster)
00092 {
00093 CHECK_SYSTEM("mkdir -p " + directory);
00094 }
00095
00096
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 }