]> git.lyx.org Git - lyx.git/blob - src/support/lyxsum.C
hopefully fix tex2lyx linking.
[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 #ifdef HAVE_SYS_TYPES_H
43 # include <sys/types.h>
44 #endif
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
47 #endif
48 #include <fcntl.h>
49 #ifdef HAVE_UNISTD_H
50 # include <unistd.h>
51 #endif
52
53 #include <sys/mman.h>
54
55
56 namespace lyx {
57
58 unsigned long support::sum(string const & file)
59 {
60         lyxerr[Debug::FILES] << "lyx::sum() using mmap (lightning fast)"
61                              << endl;
62
63         int fd = open(file.c_str(), O_RDONLY);
64         if (!fd)
65                 return 0;
66
67         struct stat info;
68         fstat(fd, &info);
69
70         void * mm = mmap(0, info.st_size, PROT_READ,
71                          MAP_PRIVATE, fd, 0);
72         // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
73         if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
74                 close(fd);
75                 return 0;
76         }
77
78         char * beg = static_cast<char*>(mm);
79         char * end = beg + info.st_size;
80
81         boost::crc_32_type crc;
82         crc.process_block(beg, end);
83         unsigned long result = crc.checksum();
84
85         munmap(mm, info.st_size);
86         close(fd);
87
88         return result;
89 }
90
91 } // namespace lyx
92
93 #else // No mmap
94
95 #include <fstream>
96 #include <iterator>
97
98
99 namespace {
100
101 template<typename InputIterator>
102 inline
103 unsigned long do_crc(InputIterator first, InputIterator last)
104 {
105         boost::crc_32_type crc;
106         crc = std::for_each(first, last, crc);
107         return crc.checksum();
108 }
109
110 } // namespace anon
111
112
113 namespace lyx {
114
115 using std::ifstream;
116 #if HAVE_DECL_ISTREAMBUF_ITERATOR
117 using std::istreambuf_iterator;
118
119 unsigned long support::sum(string const & file)
120 {
121         lyxerr[Debug::FILES] << "lyx::sum() using istreambuf_iterator (fast)"
122                              << endl;
123
124         ifstream ifs(file.c_str());
125         if (!ifs) return 0;
126
127         istreambuf_iterator<char> beg(ifs);
128         istreambuf_iterator<char> end;
129
130         return do_crc(beg,end);
131 }
132 #else
133
134 using std::istream_iterator;
135 using std::ios;
136
137 unsigned long support::sum(string const & file)
138 {
139         lyxerr[Debug::FILES]
140                 << "lyx::sum() using istream_iterator (slow as a snail)"
141                 << endl;
142
143         ifstream ifs(file.c_str());
144         if (!ifs) return 0;
145
146         ifs.unsetf(ios::skipws);
147         istream_iterator<char> beg(ifs);
148         istream_iterator<char> end;
149
150         return do_crc(beg,end);
151 }
152 #endif
153
154 } // namespace lyx
155
156 #endif // mmap