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