Chaste  Release::2018.1
FileComparison.hpp
1 /*
2 
3 Copyright (c) 2005-2018, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 #ifndef FILECOMPARISON_HPP_
36 #define FILECOMPARISON_HPP_
37 
38 #include "AbstractFileComparison.hpp"
39 #include <vector>
40 #include <boost/foreach.hpp>
41 
48 {
49 private:
52 
55 
60  std::vector<std::string> mCommentLineStarts;
61 
63  std::vector<std::string> mIgnorableContent;
64 
65 public:
75  FileComparison(std::string fileName1, std::string fileName2, bool calledCollectively=true, bool suppressOutput = false)
76  : AbstractFileComparison(fileName1, fileName2, calledCollectively, suppressOutput),
77  mIgnoreCommentLines(true),
78  mIgnoreBlankLines(false)
79  {
81  }
82 
92  FileComparison(const FileFinder& rFileName1, const FileFinder& rFileName2, bool calledCollectively=true, bool suppressOutput = false)
93  : AbstractFileComparison(rFileName1, rFileName2, calledCollectively, suppressOutput),
94  mIgnoreCommentLines(true),
95  mIgnoreBlankLines(false)
96  {
98  }
99 
106  {
107  mCommentLineStarts.push_back("#");
108  mCommentLineStarts.push_back("!");
109  mCommentLineStarts.push_back("Created by Chaste");
110  mCommentLineStarts.push_back("<!-- Created by Chaste");
111  }
112 
120  void SetIgnoreCommentLines(bool ignore=true)
121  {
122  mIgnoreCommentLines = ignore;
123  if (!ignore)
124  {
125  mCommentLineStarts.clear();
126  }
127  }
128 
135  void IgnoreBlankLines(bool ignore=true)
136  {
137  mIgnoreBlankLines = ignore;
138  }
139 
146  void SetIgnoreLinesBeginningWith(std::string lineStart)
147  {
148  mIgnoreCommentLines = true;
149  mCommentLineStarts.push_back(lineStart);
150  }
151 
158  void IgnoreLinesContaining(const std::string& rIgnorableText)
159  {
160  mIgnorableContent.push_back(rIgnorableText);
161  }
162 
169  bool CompareFiles(unsigned ignoreFirstFewLines=0, bool doTsAssert=true)
170  {
171  // Usually only the master process does the checking, this can be switched off in the constructor.
173  {
174  return true;
175  }
176 
177  std::string data1;
178  std::string data2;
179  unsigned failures = 0;
180  unsigned max_display_failures = 10;
181 
182  SkipHeaderLines(ignoreFirstFewLines);
183 
184  bool files_empty = false;
185  do
186  {
187  std::string buffer1;
188  std::string buffer2;
189  getline(*mpFile1, buffer1);
190  getline(*mpFile2, buffer2);
191 
192  if (mIgnoreBlankLines)
193  {
194  // Keep reading lines until we see non-blank, end-of-file or read error
195  while (buffer1.empty() && mpFile1->good())
196  {
197  getline(*mpFile1, buffer1);
198  }
199  while (buffer2.empty() && mpFile2->good())
200  {
201  getline(*mpFile2, buffer2);
202  }
203  }
204 
206  {
207  bool skip_this_line = false;
208  for (unsigned i=0; i<mCommentLineStarts.size(); i++)
209  {
210  // Check for lines starting with a comment symbol
211  size_t found1 = buffer1.find(mCommentLineStarts[i]);
212  size_t found2 = buffer2.find(mCommentLineStarts[i]);
213  if (found1 == 0 && found2 == 0)
214  {
215  skip_this_line = true;
216  break;
217  }
218  }
219  if (skip_this_line)
220  {
221  continue;
222  }
223  }
224 
225  // Check for lines containing ignorable text
226  if (!mIgnorableContent.empty())
227  {
228  bool skip_this_line = false;
229  BOOST_FOREACH(const std::string& rText, mIgnorableContent)
230  {
231  size_t found1 = buffer1.find(rText);
232  size_t found2 = buffer2.find(rText);
233  if (found1 != std::string::npos && found2 != std::string::npos)
234  {
235  skip_this_line = true;
236  break;
237  }
238  }
239  if (skip_this_line)
240  {
241  continue;
242  }
243  }
244 
245  if (!(buffer1==buffer2) && !files_empty)
246  {
247  if (failures++ < max_display_failures && !mSuppressOutput)
248  {
249  // Display error
250  std::stringstream message;
251  message << "Line " << mLineNum << " differs in files " << mFilename1 << " and " << mFilename2;
252 
253  TS_TRACE(message.str());
254  TS_TRACE( buffer1 );
255  TS_TRACE( buffer2 );
256  }
257  }
258  mLineNum++;
259  }
260  while (mpFile1->good() && mpFile2->good());
261  // If either is not good(), then it means that there's nothing to read from the file, or a file input error.
262 
263  if (doTsAssert)
264  {
265  // Force CxxTest error if there were any major differences
266  TS_ASSERT_EQUALS(failures, 0u);
267  // If that assertion tripped...
268  if (failures > 0u && !mSuppressOutput)
269  {
270  // Report the paths to the files
271  TS_TRACE("Files " + mFilename1 + " and " + mFilename2 + " differ.");
272  }
273  }
274 
275  ResetFiles();
276 
277  return (failures==0);
278  }
279 };
280 
281 #endif /*FILECOMPARISON_HPP_*/
void IgnoreBlankLines(bool ignore=true)
void SkipHeaderLines(unsigned numLinesToSkip)
FileComparison(std::string fileName1, std::string fileName2, bool calledCollectively=true, bool suppressOutput=false)
static bool AmMaster()
Definition: PetscTools.cpp:120
FileComparison(const FileFinder &rFileName1, const FileFinder &rFileName2, bool calledCollectively=true, bool suppressOutput=false)
std::vector< std::string > mIgnorableContent
void SetIgnoreCommentLines(bool ignore=true)
void SetIgnoreLinesBeginningWith(std::string lineStart)
bool CompareFiles(unsigned ignoreFirstFewLines=0, bool doTsAssert=true)
std::vector< std::string > mCommentLineStarts
void IgnoreLinesContaining(const std::string &rIgnorableText)