diff --git a/trunk/src/comparator.cpp b/trunk/src/comparator.cpp new file mode 100644 index 0000000..87464b8 --- /dev/null +++ b/trunk/src/comparator.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +#include "comparator.h" + +void comparator::next(const typed_file& filename1, const typed_file& filename2) { + if (fs::file_size(filename1.path()) != fs::file_size(filename2.path())) { + return; + } + const int buf_size = 4096; + char buf1[buf_size], buf2[buf_size]; + fs::ifstream file1(filename1.path()), file2(filename2.path()); + while (true) { + file1.read(buf1, buf_size); + file2.read(buf2, buf_size); + assert(file1.gcount() == file2.gcount()); + if (file1.gcount() == file2.gcount()) { + if (std::memcmp(buf1, buf2, file1.gcount()) != 0) { + return; + } + assert(file1.eof() == file2.eof()); + if (file1.eof() && file2.eof()) { + (*this)(compare_result(filename1.path(), filename2.path())); + return; + } else + if (!file1.eof() && !file2.eof()) { + continue; + } + } + return; + } +} + +std::ostream& operator<<(std::ostream& o, const compare_result& r) { + o << "files " << r.file1() << " and " << r.file2() << " are equal"; + return o; +} diff --git a/trunk/src/comparator.h b/trunk/src/comparator.h new file mode 100644 index 0000000..5b031b8 --- /dev/null +++ b/trunk/src/comparator.h @@ -0,0 +1,23 @@ +#ifndef _COMPARATOR_H_ +#define _COMPARATOR_H_ + +#include "filesystem.h" +#include "kleisli.h" +#include "file_type.h" + +class compare_result { + const fs::path& _file1; + const fs::path& _file2; +public: + compare_result(const fs::path& f1, const fs::path& f2): _file1(f1), _file2(f2) {} + const fs::path& file1() const { return _file1; } + const fs::path& file2() const { return _file2; } +}; + +std::ostream& operator<<(std::ostream& o, const compare_result& r); + +struct comparator: category::kleisli::arr< std::pair, compare_result> { + void next(const typed_file& file1, const typed_file& file2); +}; + +#endif diff --git a/trunk/src/filesystem.cpp b/trunk/src/filesystem.cpp new file mode 100644 index 0000000..c7818e2 --- /dev/null +++ b/trunk/src/filesystem.cpp @@ -0,0 +1,25 @@ +#include + +#include "filesystem.h" +#include "logger.h" +#include "functor_iterator.h" + +namespace fs { + +void recursive::next(const std::string& value) { + if (!exists(value)) { + logger::std_stream() << value << " does not exists" << std::endl; + } else + if (is_directory(value)) { + remove_copy_if(recursive_directory_iterator(value), recursive_directory_iterator(), + functor_iterator(*this), + std::not1(std::ptr_fun((bool(*)(const path&))(&is_regular_file)))); + } else + if (is_regular_file(value)) { + (*this)(value); + } else { + logger::std_stream() << value << " neither directory nor regular file" << std::endl; + } +} + +}