compare_trees.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // compare_trees.cpp -------------------------------------------------------//
  2. // Copyright Beman Dawes 2008
  3. // Use, modification, and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //----------------------------------------------------------------------------//
  7. #include <boost/filesystem.hpp>
  8. #include <iostream>
  9. #include <fstream>
  10. #include <string>
  11. using namespace boost::filesystem;
  12. using std::string;
  13. namespace
  14. {
  15. // load_file ---------------------------------------------------------------//
  16. void load_file( const path & p, string & target )
  17. {
  18. std::ifstream fin( p.string().c_str(),
  19. std::ios_base::in|std::ios_base::binary );
  20. if ( !fin )
  21. std::cout << "could not open input file: " << p.string() << '\n';
  22. std::getline( fin, target, '\0' ); // read the whole file
  23. }
  24. }
  25. // main --------------------------------------------------------------------//
  26. int main( int argc, char * argv[] )
  27. {
  28. if ( argc != 3 )
  29. {
  30. std::cout << "Usage: compare_trees [options ...] path1 path2\n";
  31. return 1;
  32. }
  33. path p1(argv[1]);
  34. path p2(argv[2]);
  35. recursive_directory_iterator it( p1 );
  36. recursive_directory_iterator end;
  37. for ( ; it != end; ++it )
  38. {
  39. //std::cout << it->string() << "\n";
  40. if ( it->filename()[0] == '.' )
  41. {
  42. it.no_push();
  43. continue;
  44. }
  45. string s = it->string();
  46. s.erase(0, p1.string().size());
  47. path t(p2);
  48. t /= s;
  49. if ( !exists(t) )
  50. {
  51. std::cout << t.string() << " does not exist\n";
  52. if ( is_directory( it.status() ) )
  53. it.no_push();
  54. }
  55. else if ( is_regular_file( it.status() ) )
  56. {
  57. if ( file_size(*it) != file_size(t) )
  58. {
  59. std::cout << t.string() << " different size\n";
  60. }
  61. // size of the two files is the same, so compare contents
  62. else
  63. {
  64. string contents1, contents2;
  65. load_file(*it, contents1);
  66. load_file(t, contents2);
  67. if ( contents1 != contents2 )
  68. {
  69. std::cout << t.string() << " different content\n";
  70. }
  71. }
  72. }
  73. }
  74. return 0;
  75. }
粤ICP备19079148号