MEPP2 Project
utils_identical_text_based_files.hpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as
6 // published by the Free Software Foundation; either version 3 of
7 // the License, or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #pragma once
12 
13 #include <string>
14 #include <vector>
15 #include <fstream>
16 #include <iostream>
17 
18 /*
19  * \brief Compare two text based files in search possible differences.
20  * \param filenameA Filename of first file.
21  * \param filenameB Filename of second file.
22  * \param skip skip lines beginning with the provided strings.
23  * \result true when files are identical false otherwise
24  */
25 inline
26 bool
27 identical_text_based_files(std::string filename_a,
28  std::string filename_b,
29  const std::vector< std::string > &skip =
30  std::vector< std::string >())
31 {
32  std::ifstream file_a(filename_a);
33  if(!file_a)
34  {
35  std::cout << "Unable to read first file." << filename_a << std::endl;
36  return false;
37  }
38 
39  std::ifstream file_b(filename_b);
40  if(!file_b)
41  {
42  std::cout << "Unable to read second file " << filename_b << std::endl;
43  return false;
44  }
45 
46  bool result = true;
47  while((!file_a.eof()) && (!file_b.eof()))
48  {
49  std::string line_a, line_b;
50 
51  // read one line from each file
52  getline(file_a, line_a);
53  getline(file_b, line_b);
54 
55  // skip lines if requested
56  bool skip_line = false;
57  for(auto &skip_str : skip)
58  {
59  if((line_a.substr(0, skip_str.size()) == skip_str) &&
60  (line_b.substr(0, skip_str.size()) == skip_str))
61  {
62  skip_line = true;
63  break;
64  }
65  }
66  if(skip_line)
67  {
68  std::cout << "skip line_a '" << line_a << "'" << std::endl;
69  std::cout << "skip line_b '" << line_b << "'" << std::endl;
70  continue; // stop processing the current line
71  }
72 
73  // remove DOS end of line extra character if any
74  if((!line_a.empty()) && (line_a.back() == '\r'))
75  line_a.pop_back();
76  if((!line_b.empty()) && (line_b.back() == '\r'))
77  line_b.pop_back();
78 
79  // compare the 2 lines
80  if(line_a != line_b)
81  {
82  result = false;
83  std::cout << "Comparison with reference file failed" << std::endl;
84  std::cout << "<line_a '" << line_a << "'" << std::endl;
85  std::cout << "---" << std::endl;
86  std::cout << ">line_b '" << line_b << "'" << std::endl;
87  break;
88  }
89  }
90 
91  file_a.close();
92  file_b.close();
93 
94  return result;
95 }
identical_text_based_files
bool identical_text_based_files(std::string filename_a, std::string filename_b, const std::vector< std::string > &skip=std::vector< std::string >())
Definition: utils_identical_text_based_files.hpp:27