]> git.lyx.org Git - lyx.git/blob - src/support/lyxsum.cpp
fix warning on possibly(?) unused precompiled headers due to different -fPic settings...
[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 #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
53 #include <sys/mman.h>
54
55
56 namespace lyx {
57 namespace support {
58
59 unsigned long sum(FileName const & file)
60 {
61         LYXERR(Debug::FILES) << "lyx::sum() using mmap (lightning fast)"
62                              << endl;
63
64         int fd = open(file.toFilesystemEncoding().c_str(), O_RDONLY);
65         if (!fd)
66                 return 0;
67
68         struct stat info;
69         fstat(fd, &info);
70
71         void * mm = mmap(0, info.st_size, PROT_READ,
72                          MAP_PRIVATE, fd, 0);
73         // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
74         if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
75                 close(fd);
76                 return 0;
77         }
78
79         char * beg = static_cast<char*>(mm);
80         char * end = beg + info.st_size;
81
82         boost::crc_32_type crc;
83         crc.process_block(beg, end);
84         unsigned long result = crc.checksum();
85
86         munmap(mm, info.st_size);
87         close(fd);
88
89         return result;
90 }
91
92 } // namespace support
93 } // namespace lyx
94
95 #else // No mmap
96
97 #include <fstream>
98 #include <iterator>
99
100
101 namespace {
102
103 template<typename InputIterator>
104 inline
105 unsigned long do_crc(InputIterator first, InputIterator last)
106 {
107         boost::crc_32_type crc;
108         crc = std::for_each(first, last, crc);
109         return crc.checksum();
110 }
111
112 } // namespace anon
113
114
115 namespace lyx {
116 namespace support {
117
118 using std::ifstream;
119 #if HAVE_DECL_ISTREAMBUF_ITERATOR
120 using std::istreambuf_iterator;
121
122 unsigned long sum(FileName const & file)
123 {
124         LYXERR(Debug::FILES) << "lyx::sum() using istreambuf_iterator (fast)"
125                              << endl;
126
127         string filename = file.toFilesystemEncoding();
128         // a directory may be passed here so we need to test it. (bug 3622)
129         if (fs::is_directory(filename))
130                 return 0;
131         ifstream ifs(filename.c_str());
132         if (!ifs)
133                 return 0;
134
135         istreambuf_iterator<char> beg(ifs);
136         istreambuf_iterator<char> end;
137
138         return do_crc(beg,end);
139 }
140 #else
141
142 using std::istream_iterator;
143 using std::ios;
144
145 unsigned long sum(FileName const & file)
146 {
147         LYXERR(Debug::FILES)
148                 << "lyx::sum() using istream_iterator (slow as a snail)"
149                 << endl;
150
151         string filename = file.toFilesystemEncoding();
152         // a directory may be passed here so we need to test it. (bug 3622)
153         if (fs::is_directory(filename))
154                 return 0;
155         ifstream ifs(filename.c_str());
156         if (!ifs)
157                 return 0;
158
159         ifs.unsetf(ios::skipws);
160         istream_iterator<char> beg(ifs);
161         istream_iterator<char> end;
162
163         return do_crc(beg,end);
164 }
165 #endif
166
167 } // namespace support
168 } // namespace lyx
169
170 #endif // mmap