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