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 #include <sys/types.h>
32 unsigned long lyx::sum(string const & file)
34 int fd = open(file.c_str(), O_RDONLY);
41 void * mm = mmap(0, info.st_size, PROT_READ,
43 if (mm == MAP_FAILED) {
48 char * beg = static_cast<char*>(mm);
49 char * end = beg + info.st_size;
51 boost::crc_32_type crc;
52 crc.process_block(beg, end);
53 unsigned long result = crc.checksum();
55 munmap(mm, info.st_size);
67 template<typename InputIterator>
69 unsigned long do_crc(InputIterator first, InputIterator last)
71 boost::crc_32_type crc;
72 crc = std::for_each(first, last, crc);
73 return crc.checksum();
78 #if HAVE_DECL_ISTREAMBUF_ITERATOR
80 #warning lyx::sum() using istreambuf_iterator (fast)
82 unsigned long lyx::sum(string const & file)
84 std::ifstream ifs(file.c_str());
87 std::istreambuf_iterator<char> beg(ifs);
88 std::istreambuf_iterator<char> end;
90 return do_crc(beg,end);
94 #warning lyx::sum() using istream_iterator (slow as a snail)
96 unsigned long lyx::sum(string const & file)
98 std::ifstream ifs(file.c_str());
101 ifs.unsetf(std::ios::skipws);
102 std::istream_iterator<char> beg(ifs);
103 std::istream_iterator<char> end;
105 return do_crc(beg,end);