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