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"
19 // Various implementations of lyx::sum(), depending on what methods
20 // are available. Order is faster to slowest.
21 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
23 // Make sure we get modern version of mmap and friends with void*,
24 // not `compatibility' version with caddr_t.
25 #define _POSIX_C_SOURCE 199506L
27 #include <sys/types.h>
36 using std::istreambuf_iterator;
37 using std::istream_iterator;
41 unsigned long lyx::sum(string const & file)
43 lyxerr[Debug::FILES] << "lyx::sum() using mmap (lightning fast)"
46 int fd = open(file.c_str(), O_RDONLY);
53 void * mm = mmap(0, info.st_size, PROT_READ,
55 // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
56 if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
61 char * beg = static_cast<char*>(mm);
62 char * end = beg + info.st_size;
64 boost::crc_32_type crc;
65 crc.process_block(beg, end);
66 unsigned long result = crc.checksum();
68 munmap(mm, info.st_size);
80 template<typename InputIterator>
82 unsigned long do_crc(InputIterator first, InputIterator last)
84 boost::crc_32_type crc;
85 crc = for_each(first, last, crc);
86 return crc.checksum();
91 #if HAVE_DECL_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);
106 unsigned long lyx::sum(string const & file)
109 << "lyx::sum() using istream_iterator (slow as a snail)"
112 ifstream ifs(file.c_str());
115 ifs.unsetf(ios::skipws);
116 istream_iterator<char> beg(ifs);
117 istream_iterator<char> end;
119 return do_crc(beg,end);