2 // Inclusion of standard header file
8 // Inclusion of system header file
11 #include <sys/types.h>
14 // Inclusion of library header file
16 #include <boost/filesystem/fstream.hpp>
17 #include <boost/filesystem/operations.hpp>
18 #include <boost/filesystem/path.hpp>
21 // Inclusion of local header file
23 #include "binary_compare.hpp"
24 #include "islibfunc.hpp"
29 // Definition of function
31 // Return value: 0: file path 1 and file path 2 equal to each other.
32 // < 0: file path 1 is less than file path 2.
33 // > 0: file path 1 is greater than file path 2.
38 boost::filesystem::path const &file_path_1,
39 boost::filesystem::path const &file_path_2
47 boost::filesystem::path const &file_path_1_,
48 boost::filesystem::path const &file_path_2_
51 throw_if ( std::invalid_argument ( "islib::binary_compare: file_path_1_.empty ()" ), file_path_1_.empty () );
52 throw_if ( std::invalid_argument ( "islib::binary_compare: file_path_2_.empty ()" ), file_path_2_.empty () );
53 throw_if ( std::invalid_argument ( "islib::binary_compare: !boost::filesystem::is_regular ( file_path_1_ )" ), !boost::filesystem::is_regular ( file_path_1_ ) );
54 throw_if ( std::invalid_argument ( "islib::binary_compare: !boost::filesystem::is_regular ( file_path_2_ )" ), !boost::filesystem::is_regular ( file_path_2_ ) );
64 a_local ( file_path_1, file_path_2 );
66 if ( file_path_1 == file_path_2 )
69 struct stat file_status_1;
70 struct stat file_status_2;
72 islib::stat_ ( file_path_1, &file_status_1 );
73 islib::stat_ ( file_path_2, &file_status_2 );
75 if ( file_status_1.st_dev == file_status_2.st_dev && file_status_1.st_ino == file_status_2.st_ino )
78 if ( file_status_1.st_size != file_status_2.st_size )
79 return static_cast < int > ( file_status_1.st_size ) - static_cast < int > ( file_status_2.st_size );
81 boost::filesystem::ifstream file_stream_1 ( file_path_1 );
82 boost::filesystem::ifstream file_stream_2 ( file_path_2 );
83 off_t number_of_read_bytes = 0;
87 if ( number_of_read_bytes == file_status_1.st_size )
90 throw_if ( std::runtime_error ( "islib::binary_compare: !file_stream_1 || !file_stream_2" ), !file_stream_1 || !file_stream_2 );
95 file_stream_1.get ( character_1 );
96 file_stream_2.get ( character_2 );
98 if ( character_1 != character_2 )
99 return static_cast < int > ( static_cast < unsigned int > ( character_1 ) ) - static_cast < int > ( static_cast < unsigned int > ( character_2 ) );
101 ++number_of_read_bytes;