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