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 #warning lyx::sum() using mmap (lightning fast)
26 // Make sure we get modern version of mmap and friends with void*,
27 // not `compatibility' version with caddr_t.
28 #define _POSIX_C_SOURCE 199506L
30 #include <sys/types.h>
36 unsigned long lyx::sum(string const & file)
38 int fd = open(file.c_str(), O_RDONLY);
45 void * mm = mmap(0, info.st_size, PROT_READ,
47 // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
48 if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
53 char * beg = static_cast<char*>(mm);
54 char * end = beg + info.st_size;
56 boost::crc_32_type crc;
57 crc.process_block(beg, end);
58 unsigned long result = crc.checksum();
60 munmap(mm, info.st_size);
72 template<typename InputIterator>
74 unsigned long do_crc(InputIterator first, InputIterator last)
76 boost::crc_32_type crc;
77 crc = std::for_each(first, last, crc);
78 return crc.checksum();
83 #if HAVE_DECL_ISTREAMBUF_ITERATOR
85 #warning lyx::sum() using istreambuf_iterator (fast)
87 unsigned long lyx::sum(string const & file)
89 std::ifstream ifs(file.c_str());
92 std::istreambuf_iterator<char> beg(ifs);
93 std::istreambuf_iterator<char> end;
95 return do_crc(beg,end);
99 #warning lyx::sum() using istream_iterator (slow as a snail)
101 unsigned long lyx::sum(string const & file)
103 std::ifstream ifs(file.c_str());
106 ifs.unsetf(std::ios::skipws);
107 std::istream_iterator<char> beg(ifs);
108 std::istream_iterator<char> end;
110 return do_crc(beg,end);