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>
25 // OK, this is ugly, but it is the only workaround I found to compile
26 // with gcc (any version) on a system which uses a non-GNU toolchain.
27 // The problem is that gcc uses a weak symbol for a particular
28 // instantiation and that the system linker usually does not
29 // understand those weak symbols (seen on HP-UX, tru64, AIX and
30 // others). Thus we force an explicit instanciation of this particular
32 template struct boost::detail::crc_table_t<32, 0x04C11DB7, true>;
34 // Various implementations of lyx::sum(), depending on what methods
35 // are available. Order is faster to slowest.
36 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
38 // Make sure we get modern version of mmap and friends with void*,
39 // not `compatibility' version with caddr_t.
40 #define _POSIX_C_SOURCE 199506L
42 #ifdef HAVE_SYS_TYPES_H
43 # include <sys/types.h>
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
58 unsigned long support::sum(string const & file)
60 lyxerr[Debug::FILES] << "lyx::sum() using mmap (lightning fast)"
63 int fd = open(file.c_str(), O_RDONLY);
70 void * mm = mmap(0, info.st_size, PROT_READ,
72 // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
73 if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
78 char * beg = static_cast<char*>(mm);
79 char * end = beg + info.st_size;
81 boost::crc_32_type crc;
82 crc.process_block(beg, end);
83 unsigned long result = crc.checksum();
85 munmap(mm, info.st_size);
101 template<typename InputIterator>
103 unsigned long do_crc(InputIterator first, InputIterator last)
105 boost::crc_32_type crc;
106 crc = std::for_each(first, last, crc);
107 return crc.checksum();
116 #if HAVE_DECL_ISTREAMBUF_ITERATOR
117 using std::istreambuf_iterator;
119 unsigned long support::sum(string const & file)
121 lyxerr[Debug::FILES] << "lyx::sum() using istreambuf_iterator (fast)"
124 ifstream ifs(file.c_str());
127 istreambuf_iterator<char> beg(ifs);
128 istreambuf_iterator<char> end;
130 return do_crc(beg,end);
134 using std::istream_iterator;
137 unsigned long support::sum(string const & file)
140 << "lyx::sum() using istream_iterator (slow as a snail)"
143 ifstream ifs(file.c_str());
146 ifs.unsetf(ios::skipws);
147 istream_iterator<char> beg(ifs);
148 istream_iterator<char> end;
150 return do_crc(beg,end);