]> git.lyx.org Git - lyx.git/blob - src/support/lyxsum.C
ws cleanup
[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 <algorithm>
14 #include <boost/crc.hpp>
15
16 #include "support/lyxlib.h"
17 #include "debug.h"
18
19 using std::endl;
20
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)
24
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
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <sys/mman.h>
34
35 using std::ifstream;
36 using std::for_each;
37 using std::istreambuf_iterator;
38 using std::istream_iterator;
39 using std::ios;
40
41
42 unsigned long lyx::sum(string const & file)
43 {
44         lyxerr[Debug::FILES] << "lyx::sum() using mmap (lightning fast)"
45                              << endl;
46
47         int fd = open(file.c_str(), O_RDONLY);
48         if (!fd)
49                 return 0;
50
51         struct stat info;
52         fstat(fd, &info);
53
54         void * mm = mmap(0, info.st_size, PROT_READ,
55                          MAP_PRIVATE, fd, 0);
56         // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
57         if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
58                 close(fd);
59                 return 0;
60         }
61
62         char * beg = static_cast<char*>(mm);
63         char * end = beg + info.st_size;
64
65         boost::crc_32_type crc;
66         crc.process_block(beg, end);
67         unsigned long result = crc.checksum();
68
69         munmap(mm, info.st_size);
70         close(fd);
71
72         return result;
73 }
74 #else // No mmap
75
76 #include <fstream>
77 #include <iterator>
78
79 namespace {
80
81 template<typename InputIterator>
82 inline
83 unsigned long do_crc(InputIterator first, InputIterator last)
84 {
85         boost::crc_32_type crc;
86         crc = for_each(first, last, crc);
87         return crc.checksum();
88 }
89
90 } // namespace
91
92 #if HAVE_DECL_ISTREAMBUF_ITERATOR
93 using std::ifstream;
94 using std::istreambuf_iterator;
95
96 unsigned long lyx::sum(string const & file)
97 {
98         lyxerr[Debug::FILES] << "lyx::sum() using istreambuf_iterator (fast)"
99                              << endl;
100
101         ifstream ifs(file.c_str());
102         if (!ifs) return 0;
103
104         istreambuf_iterator<char> beg(ifs);
105         istreambuf_iterator<char> end;
106
107         return do_crc(beg,end);
108 }
109 #else
110 unsigned long lyx::sum(string const & file)
111 {
112         lyxerr[Debug::FILES]
113                 << "lyx::sum() using istream_iterator (slow as a snail)"
114                 << endl;
115
116         ifstream ifs(file.c_str());
117         if (!ifs) return 0;
118
119         ifs.unsetf(ios::skipws);
120         istream_iterator<char> beg(ifs);
121         istream_iterator<char> end;
122
123         return do_crc(beg,end);
124 }
125 #endif
126 #endif // mmap