FileComparison.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 #ifndef FILECOMPARISON_HPP_
00036 #define FILECOMPARISON_HPP_
00037
00038 #include "AbstractFileComparison.hpp"
00039 #include <vector>
00040 #include <boost/foreach.hpp>
00041
00047 class FileComparison : public AbstractFileComparison
00048 {
00049 private:
00051 bool mIgnoreCommentLines;
00052
00057 std::vector<std::string> mCommentLineStarts;
00058
00060 std::vector<std::string> mIgnorableContent;
00061
00062 public:
00072 FileComparison(std::string fileName1, std::string fileName2, bool calledCollectively=true, bool suppressOutput = false)
00073 : AbstractFileComparison(fileName1, fileName2, calledCollectively, suppressOutput),
00074 mIgnoreCommentLines(true)
00075 {
00076 SetupCommentLines();
00077 }
00078
00088 FileComparison(const FileFinder& rFileName1, const FileFinder& rFileName2, bool calledCollectively=true, bool suppressOutput = false)
00089 : AbstractFileComparison(rFileName1, rFileName2, calledCollectively, suppressOutput),
00090 mIgnoreCommentLines(true)
00091 {
00092 SetupCommentLines();
00093 }
00094
00100 void SetupCommentLines()
00101 {
00102 mCommentLineStarts.push_back("#");
00103 mCommentLineStarts.push_back("!");
00104 mCommentLineStarts.push_back("Created by Chaste");
00105 mCommentLineStarts.push_back("<!-- Created by Chaste");
00106 }
00107
00115 void SetIgnoreCommentLines(bool ignore=true)
00116 {
00117 mIgnoreCommentLines = ignore;
00118 if (!ignore)
00119 {
00120 mCommentLineStarts.clear();
00121 }
00122 }
00123
00130 void SetIgnoreLinesBeginningWith(std::string lineStart)
00131 {
00132 mIgnoreCommentLines = true;
00133 mCommentLineStarts.push_back(lineStart);
00134 }
00135
00142 void IgnoreLinesContaining(const std::string& rIgnorableText)
00143 {
00144 mIgnorableContent.push_back(rIgnorableText);
00145 }
00146
00153 bool CompareFiles(unsigned ignoreFirstFewLines=0, bool doTsAssert=true)
00154 {
00155
00156 if (mCalledCollectively && !PetscTools::AmMaster())
00157 {
00158 return true;
00159 }
00160
00161 std::string data1;
00162 std::string data2;
00163 unsigned failures = 0;
00164 unsigned max_display_failures = 10;
00165
00166 SkipHeaderLines(ignoreFirstFewLines);
00167
00168 bool files_empty = false;
00169 do
00170 {
00171 std::string buffer1;
00172 std::string buffer2;
00173 getline(*mpFile1,buffer1);
00174 getline(*mpFile2,buffer2);
00175
00176 if (mIgnoreCommentLines)
00177 {
00178 bool skip_this_line = false;
00179 for (unsigned i=0; i<mCommentLineStarts.size(); i++)
00180 {
00181
00182 size_t found1 = buffer1.find(mCommentLineStarts[i]);
00183 size_t found2 = buffer2.find(mCommentLineStarts[i]);
00184 if (found1 == 0 && found2 == 0)
00185 {
00186 skip_this_line = true;
00187 break;
00188 }
00189 }
00190 if (skip_this_line)
00191 {
00192 continue;
00193 }
00194 }
00195
00196
00197 if (!mIgnorableContent.empty())
00198 {
00199 bool skip_this_line = false;
00200 BOOST_FOREACH(const std::string& rText, mIgnorableContent)
00201 {
00202 size_t found1 = buffer1.find(rText);
00203 size_t found2 = buffer2.find(rText);
00204 if (found1 != std::string::npos && found2 != std::string::npos)
00205 {
00206 skip_this_line = true;
00207 break;
00208 }
00209 }
00210 if (skip_this_line)
00211 {
00212 continue;
00213 }
00214 }
00215
00216 if (!(buffer1==buffer2) && !files_empty)
00217 {
00218 if (failures++ < max_display_failures && !mSuppressOutput)
00219 {
00220
00221 std::stringstream message;
00222 message << "Line " << mLineNum << " differs in files " << mFilename1 << " and " << mFilename2;
00223
00224 TS_TRACE(message.str());
00225 TS_TRACE( buffer1 );
00226 TS_TRACE( buffer2 );
00227 }
00228 }
00229 mLineNum++;
00230 }
00231 while (mpFile1->good() && mpFile2->good());
00232
00233
00234 if (doTsAssert)
00235 {
00236
00237 TS_ASSERT_EQUALS(failures, 0u);
00238
00239 if (failures > 0u && !mSuppressOutput)
00240 {
00241 #define COVERAGE_IGNORE
00242 TS_TRACE("Files " + mFilename1 + " and " + mFilename2 + " differ.");
00243 #undef COVERAGE_IGNORE
00244 }
00245 }
00246
00247 ResetFiles();
00248
00249 return (failures==0);
00250 }
00251 };
00252
00253 #endif