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