Chaste Release::3.1
AbstractFileComparison.hpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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             // We want to reset the files to allow this method to be called again, with different tolerances for instance.
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()); // Here we assume there are at least "ignoreFirstFewLines" lines...
00149                 TS_ASSERT(!mpFile2->fail()); // ...and that they are lines of no more than 1024 characters
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             // If it doesn't exist - throw exception
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             // If it doesn't exist - throw exception
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_