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