1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
5 * Copyright 2001 The LyX Team.
7 * ======================================================
14 #include <boost/crc.hpp>
16 #include "support/lyxlib.h"
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>
37 using std::istreambuf_iterator;
38 using std::istream_iterator;
42 unsigned long lyx::sum(string const & file)
44 lyxerr[Debug::FILES] << "lyx::sum() using mmap (lightning fast)"
47 int fd = open(file.c_str(), O_RDONLY);
54 void * mm = mmap(0, info.st_size, PROT_READ,
56 // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
57 if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
62 char * beg = static_cast<char*>(mm);
63 char * end = beg + info.st_size;
65 boost::crc_32_type crc;
66 crc.process_block(beg, end);
67 unsigned long result = crc.checksum();
69 munmap(mm, info.st_size);
81 template<typename InputIterator>
83 unsigned long do_crc(InputIterator first, InputIterator last)
85 boost::crc_32_type crc;
86 crc = for_each(first, last, crc);
87 return crc.checksum();
92 #if HAVE_DECL_ISTREAMBUF_ITERATOR
94 using std::istreambuf_iterator;
96 unsigned long lyx::sum(string const & file)
98 lyxerr[Debug::FILES] << "lyx::sum() using istreambuf_iterator (fast)"
101 ifstream ifs(file.c_str());
104 istreambuf_iterator<char> beg(ifs);
105 istreambuf_iterator<char> end;
107 return do_crc(beg,end);
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);