]> git.lyx.org Git - lyx.git/blob - src/support/lyxsum.C
* lyxfunctional.h: delete compare_memfun and helper classes
[lyx.git] / src / support / lyxsum.C
1 /**
2  * \file lyxsum.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/lyxlib.h"
14 #include "debug.h"
15
16 #include <algorithm>
17 #include <boost/crc.hpp>
18
19 using std::endl;
20
21 using std::string;
22
23
24 // OK, this is ugly, but it is the only workaround I found to compile
25 // with gcc (any version) on a system which uses a non-GNU toolchain.
26 // The problem is that gcc uses a weak symbol for a particular
27 // instantiation and that the system linker usually does not
28 // understand those weak symbols (seen on HP-UX, tru64, AIX and
29 // others). Thus we force an explicit instanciation of this particular
30 // template (JMarc)
31 template class boost::detail::crc_table_t<32, 0x04C11DB7, true>;
32
33 // Various implementations of lyx::sum(), depending on what methods
34 // are available. Order is faster to slowest.
35 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
36
37 // Make sure we get modern version of mmap and friends with void*,
38 // not `compatibility' version with caddr_t.
39 #define _POSIX_C_SOURCE 199506L
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <sys/mman.h>
46
47
48 unsigned long lyx::support::sum(string const & file)
49 {
50         lyxerr[Debug::FILES] << "lyx::sum() using mmap (lightning fast)"
51                              << endl;
52
53         int fd = open(file.c_str(), O_RDONLY);
54         if (!fd)
55                 return 0;
56
57         struct stat info;
58         fstat(fd, &info);
59
60         void * mm = mmap(0, info.st_size, PROT_READ,
61                          MAP_PRIVATE, fd, 0);
62         // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
63         if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
64                 close(fd);
65                 return 0;
66         }
67
68         char * beg = static_cast<char*>(mm);
69         char * end = beg + info.st_size;
70
71         boost::crc_32_type crc;
72         crc.process_block(beg, end);
73         unsigned long result = crc.checksum();
74
75         munmap(mm, info.st_size);
76         close(fd);
77
78         return result;
79 }
80 #else // No mmap
81
82 #include <fstream>
83 #include <iterator>
84
85
86 namespace {
87
88 template<typename InputIterator>
89 inline
90 unsigned long do_crc(InputIterator first, InputIterator last)
91 {
92         boost::crc_32_type crc;
93         crc = std::for_each(first, last, crc);
94         return crc.checksum();
95 }
96
97 } // namespace
98
99 #if HAVE_DECL_ISTREAMBUF_ITERATOR
100 using std::ifstream;
101 using std::istreambuf_iterator;
102
103 unsigned long lyx::support::sum(string const & file)
104 {
105         lyxerr[Debug::FILES] << "lyx::sum() using istreambuf_iterator (fast)"
106                              << endl;
107
108         ifstream ifs(file.c_str());
109         if (!ifs) return 0;
110
111         istreambuf_iterator<char> beg(ifs);
112         istreambuf_iterator<char> end;
113
114         return do_crc(beg,end);
115 }
116 #else
117
118 using std::istream_iterator;
119 using std::ios;
120
121 unsigned long lyx::support::sum(string const & file)
122 {
123         lyxerr[Debug::FILES]
124                 << "lyx::sum() using istream_iterator (slow as a snail)"
125                 << endl;
126
127         ifstream ifs(file.c_str());
128         if (!ifs) return 0;
129
130         ifs.unsetf(ios::skipws);
131         istream_iterator<char> beg(ifs);
132         istream_iterator<char> end;
133
134         return do_crc(beg,end);
135 }
136 #endif
137 #endif // mmap