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