AbstractFileComparison.hpp
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
00031
00032
00033
00034
00035
00036 #ifndef ABSTRACTFILECOMPARISON_HPP_
00037 #define ABSTRACTFILECOMPARISON_HPP_
00038
00039 #include <string>
00040 #include "FileFinder.hpp"
00041 #include "OutputFileHandler.hpp"
00042 #include "PetscTools.hpp"
00043
00047 class AbstractFileComparison
00048 {
00049 public:
00050
00060 AbstractFileComparison(const FileFinder& rFileFinder1, const FileFinder& rFileFinder2, bool calledCollectively, bool suppressOutput):
00061 mFilename1(rFileFinder1.GetAbsolutePath()),
00062 mFilename2(rFileFinder2.GetAbsolutePath()),
00063 mCalledCollectively(calledCollectively),
00064 mSuppressOutput(suppressOutput)
00065 {
00066 Setup();
00067 }
00068
00078 AbstractFileComparison(std::string fileName1, std::string fileName2, bool calledCollectively, bool suppressOutput):
00079 mFilename1(fileName1),
00080 mFilename2(fileName2),
00081 mCalledCollectively(calledCollectively),
00082 mSuppressOutput(suppressOutput)
00083 {
00084 Setup();
00085 }
00086
00090 virtual ~AbstractFileComparison()
00091 {
00092 if (mpFile1)
00093 {
00094 mpFile1->close();
00095 delete mpFile1;
00096 }
00097 if (mpFile2)
00098 {
00099 mpFile2->close();
00100 delete mpFile2;
00101 }
00102 }
00103
00104 protected:
00105
00106 std::string mFilename1;
00107 std::string mFilename2;
00109 std::ifstream* mpFile1;
00110 std::ifstream* mpFile2;
00112 unsigned mLineNum;
00114 bool mCalledCollectively;
00117 bool mSuppressOutput;
00118
00122 void ResetFiles()
00123 {
00124 if (!mCalledCollectively || PetscTools::AmMaster())
00125 {
00126
00127 mpFile1->close();
00128 mpFile2->close();
00129 mpFile1->open(mFilename1.c_str());
00130 mpFile2->open(mFilename2.c_str());
00131 mLineNum = 1u;
00132 }
00133 }
00134
00139 void SkipHeaderLines(unsigned numLinesToSkip)
00140 {
00141 if (!mCalledCollectively || PetscTools::AmMaster())
00142 {
00143 for (unsigned line_number=0; line_number<numLinesToSkip; line_number++)
00144 {
00145 char buffer[1024];
00146 mpFile1->getline(buffer, 1024);
00147 mpFile2->getline(buffer, 1024);
00148 TS_ASSERT(!mpFile1->fail());
00149 TS_ASSERT(!mpFile2->fail());
00150 mLineNum++;
00151 }
00152 }
00153 }
00154
00155 private:
00159 void Setup()
00160 {
00161 if (mCalledCollectively)
00162 {
00163 PetscTools::Barrier("AbstractFileComparison::Setup");
00164 }
00165 if (!mCalledCollectively || PetscTools::AmMaster())
00166 {
00167 mpFile1 = new std::ifstream(mFilename1.c_str());
00168
00169
00170 if (!mpFile1->is_open())
00171 {
00172 delete mpFile1;
00173 mpFile1 = NULL;
00174 EXCEPTION("Couldn't open file: " + mFilename1);
00175 }
00176
00177 mpFile2 = new std::ifstream(mFilename2.c_str());
00178
00179
00180 if (!mpFile2->is_open())
00181 {
00182 mpFile1->close();
00183 delete mpFile1;
00184 mpFile1 = NULL;
00185 delete mpFile2;
00186 mpFile2 = NULL;
00187 EXCEPTION("Couldn't open file: " + mFilename2);
00188 }
00189
00190 mLineNum = 1u;
00191 }
00192 else
00193 {
00194 mpFile1 = NULL;
00195 mpFile2 = NULL;
00196 }
00197 }
00198
00199 };
00200
00201
00202
00203 #endif // ABSTRACTFILECOMPARISON_HPP_