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>
35 unsigned long lyx::sum(string const & file)
37 lyxerr[Debug::FILES] << "lyx::sum() using mmap (lightning fast)"
40 int fd = open(file.c_str(), O_RDONLY);
47 void * mm = mmap(0, info.st_size, PROT_READ,
49 // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
50 if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
55 char * beg = static_cast<char*>(mm);
56 char * end = beg + info.st_size;
58 boost::crc_32_type crc;
59 crc.process_block(beg, end);
60 unsigned long result = crc.checksum();
62 munmap(mm, info.st_size);
74 template<typename InputIterator>
76 unsigned long do_crc(InputIterator first, InputIterator last)
78 boost::crc_32_type crc;
79 crc = std::for_each(first, last, crc);
80 return crc.checksum();
85 #if HAVE_DECL_ISTREAMBUF_ITERATOR
86 unsigned long lyx::sum(string const & file)
88 lyxerr[Debug::FILES] << "lyx::sum() using istreambuf_iterator (fast)"
91 std::ifstream ifs(file.c_str());
94 std::istreambuf_iterator<char> beg(ifs);
95 std::istreambuf_iterator<char> end;
97 return do_crc(beg,end);
100 unsigned long lyx::sum(string const & file)
103 << "lyx::sum() using istream_iterator (slow as a snail)"
106 std::ifstream ifs(file.c_str());
109 ifs.unsetf(std::ios::skipws);
110 std::istream_iterator<char> beg(ifs);
111 std::istream_iterator<char> end;
113 return do_crc(beg,end);