Chaste  Release::3.4
AbstractFileComparison.hpp
1 /*
2 
3 Copyright (c) 2005-2016, 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 
36 #ifndef ABSTRACTFILECOMPARISON_HPP_
37 #define ABSTRACTFILECOMPARISON_HPP_
38 
39 #include <string>
40 #include "FileFinder.hpp"
41 #include "OutputFileHandler.hpp"
42 #include "PetscTools.hpp"
43 
48 {
49 public:
50 
60  AbstractFileComparison(const FileFinder& rFileFinder1,
61  const FileFinder& rFileFinder2,
62  bool calledCollectively,
63  bool suppressOutput):
64  mFilename1(rFileFinder1.GetAbsolutePath()),
65  mFilename2(rFileFinder2.GetAbsolutePath()),
66  mCalledCollectively(calledCollectively),
67  mSuppressOutput(suppressOutput)
68  {
69  Setup();
70  }
71 
81  AbstractFileComparison(std::string fileName1,
82  std::string fileName2,
83  bool calledCollectively,
84  bool suppressOutput):
85  mFilename1(fileName1),
86  mFilename2(fileName2),
87  mCalledCollectively(calledCollectively),
88  mSuppressOutput(suppressOutput)
89  {
90  Setup();
91  }
92 
97  {
98  if (mpFile1)
99  {
100  mpFile1->close();
101  delete mpFile1;
102  }
103  if (mpFile2)
104  {
105  mpFile2->close();
106  delete mpFile2;
107  }
108  }
109 
110 protected:
111 
112  std::string mFilename1;
113  std::string mFilename2;
115  std::ifstream* mpFile1;
116  std::ifstream* mpFile2;
118  unsigned mLineNum;
124 
128  void ResetFiles()
129  {
131  {
132  // We want to reset the files to allow this method to be called again, with different tolerances for instance.
133  mpFile1->close();
134  mpFile2->close();
135  mpFile1->open(mFilename1.c_str());
136  mpFile2->open(mFilename2.c_str());
137  mLineNum = 1u;
138  }
139  }
140 
145  void SkipHeaderLines(unsigned numLinesToSkip)
146  {
148  {
149  for (unsigned line_number=0; line_number<numLinesToSkip; line_number++)
150  {
151  char buffer[1024];
152  mpFile1->getline(buffer, 1024);
153  mpFile2->getline(buffer, 1024);
154  TS_ASSERT(!mpFile1->fail()); // Here we assume there are at least "ignoreFirstFewLines" lines...
155  TS_ASSERT(!mpFile2->fail()); // ...and that they are lines of no more than 1024 characters
156  mLineNum++;
157  }
158  }
159  }
160 
161 private:
165  void Setup()
166  {
168  {
169  PetscTools::Barrier("AbstractFileComparison::Setup");
170  }
172  {
173  mpFile1 = new std::ifstream(mFilename1.c_str());
174 
175  // If it doesn't exist - throw exception
176  if (!mpFile1->is_open())
177  {
178  delete mpFile1;
179  mpFile1 = NULL;
180  EXCEPTION("Couldn't open file: " + mFilename1);
181  }
182 
183  mpFile2 = new std::ifstream(mFilename2.c_str());
184 
185  // If it doesn't exist - throw exception
186  if (!mpFile2->is_open())
187  {
188  mpFile1->close();
189  delete mpFile1;
190  mpFile1 = NULL;
191  delete mpFile2;
192  mpFile2 = NULL;
193  EXCEPTION("Couldn't open file: " + mFilename2);
194  }
195 
196  mLineNum = 1u;
197  }
198  else
199  {
200  mpFile1 = NULL;
201  mpFile2 = NULL;
202  }
203  }
204 
205 };
206 
207 
208 
209 #endif // ABSTRACTFILECOMPARISON_HPP_
AbstractFileComparison(std::string fileName1, std::string fileName2, bool calledCollectively, bool suppressOutput)
static void Barrier(const std::string callerId="")
Definition: PetscTools.cpp:134
void SkipHeaderLines(unsigned numLinesToSkip)
#define EXCEPTION(message)
Definition: Exception.hpp:143
static bool AmMaster()
Definition: PetscTools.cpp:120
AbstractFileComparison(const FileFinder &rFileFinder1, const FileFinder &rFileFinder2, bool calledCollectively, bool suppressOutput)