]> git.lyx.org Git - lyx.git/blob - src/support/lyxsum.C
lyxserver cleanup patch + andre's small patches
[lyx.git] / src / support / lyxsum.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *           Copyright 2001 The LyX Team.
6  *
7  * ======================================================
8  */
9
10
11 #include <config.h>
12
13 #include <fstream>
14 #include <iterator>
15 #include <algorithm>
16 #include <boost/crc.hpp>
17
18 #include "support/lyxlib.h"
19
20
21 namespace {
22
23 template<typename InputIterator>
24 inline
25 unsigned long do_crc(InputIterator first, InputIterator last)
26 {
27         boost::crc_32_type crc;
28         crc = std::for_each(first, last, crc);
29         return crc.checksum();
30 }
31
32 } // namespace
33
34
35 // And this would be the file interface.
36 unsigned long lyx::sum(string const & file)
37 {
38         std::ifstream ifs(file.c_str());
39         if (!ifs) return 0;
40         
41 #ifdef HAVE_DECL_ISTREAMBUF_ITERATOR
42         // This is a lot faster...
43         std::istreambuf_iterator<char> beg(ifs);
44         std::istreambuf_iterator<char> end;
45 #else
46         // than this.
47         ifs.unsetf(std::ios::skipws);
48         std::istream_iterator<char> beg(ifs);
49         std::istream_iterator<char> end;
50 #endif
51
52         return do_crc(beg, end);
53 }