3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS
13 #include "support/lyxlib.h"
17 #include <boost/crc.hpp>
21 // Various implementations of lyx::sum(), depending on what methods
22 // are available. Order is faster to slowest.
23 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
25 // Make sure we get modern version of mmap and friends with void*,
26 // not `compatibility' version with caddr_t.
27 #define _POSIX_C_SOURCE 199506L
29 #include <sys/types.h>
36 unsigned long lyx::sum(string const & file)
38 lyxerr[Debug::FILES] << "lyx::sum() using mmap (lightning fast)"
41 int fd = open(file.c_str(), O_RDONLY);
48 void * mm = mmap(0, info.st_size, PROT_READ,
50 // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
51 if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
56 char * beg = static_cast<char*>(mm);
57 char * end = beg + info.st_size;
59 boost::crc_32_type crc;
60 crc.process_block(beg, end);
61 unsigned long result = crc.checksum();
63 munmap(mm, info.st_size);
77 template<typename InputIterator>
79 unsigned long do_crc(InputIterator first, InputIterator last)
81 boost::crc_32_type crc;
82 crc = for_each(first, last, crc);
83 return crc.checksum();
88 #if HAVE_DECL_ISTREAMBUF_ITERATOR
90 using std::istreambuf_iterator;
92 unsigned long lyx::sum(string const & file)
94 lyxerr[Debug::FILES] << "lyx::sum() using istreambuf_iterator (fast)"
97 ifstream ifs(file.c_str());
100 istreambuf_iterator<char> beg(ifs);
101 istreambuf_iterator<char> end;
103 return do_crc(beg,end);
107 using std::istream_iterator;
110 unsigned long lyx::sum(string const & file)
113 << "lyx::sum() using istream_iterator (slow as a snail)"
116 ifstream ifs(file.c_str());
119 ifs.unsetf(ios::skipws);
120 istream_iterator<char> beg(ifs);
121 istream_iterator<char> end;
123 return do_crc(beg,end);