3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "support/lyxlib.h"
15 #include "support/FileName.h"
17 #include <boost/crc.hpp>
24 // OK, this is ugly, but it is the only workaround I found to compile
25 // with gcc (any version) on a system which uses a non-GNU toolchain.
26 // The problem is that gcc uses a weak symbol for a particular
27 // instantiation and that the system linker usually does not
28 // understand those weak symbols (seen on HP-UX, tru64, AIX and
29 // others). Thus we force an explicit instanciation of this particular
31 template struct boost::detail::crc_table_t<32, 0x04C11DB7, true>;
33 // Various implementations of lyx::sum(), depending on what methods
34 // are available. Order is faster to slowest.
35 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
54 unsigned long sum(FileName const & file)
56 LYXERR(Debug::FILES) << "lyx::sum() using mmap (lightning fast)"
59 int fd = open(file.toFilesystemEncoding().c_str(), O_RDONLY);
66 void * mm = mmap(0, info.st_size, PROT_READ,
68 // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
69 if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
74 char * beg = static_cast<char*>(mm);
75 char * end = beg + info.st_size;
77 boost::crc_32_type crc;
78 crc.process_block(beg, end);
79 unsigned long result = crc.checksum();
81 munmap(mm, info.st_size);
87 } // namespace support
98 template<typename InputIterator>
100 unsigned long do_crc(InputIterator first, InputIterator last)
102 boost::crc_32_type crc;
103 crc = std::for_each(first, last, crc);
104 return crc.checksum();
114 #if HAVE_DECL_ISTREAMBUF_ITERATOR
115 using std::istreambuf_iterator;
117 unsigned long sum(FileName const & file)
119 LYXERR(Debug::FILES) << "lyx::sum() using istreambuf_iterator (fast)"
122 // a directory may be passed here so we need to test it. (bug 3622)
123 if (file.isDirectory())
125 string filename = file.toFilesystemEncoding();
126 ifstream ifs(filename.c_str());
130 istreambuf_iterator<char> beg(ifs);
131 istreambuf_iterator<char> end;
133 return do_crc(beg,end);
137 using std::istream_iterator;
140 unsigned long sum(FileName const & file)
143 << "lyx::sum() using istream_iterator (slow as a snail)"
146 // a directory may be passed here so we need to test it. (bug 3622)
147 if (file.isDirectory())
150 string filename = file.toFilesystemEncoding();
151 ifstream ifs(filename.c_str());
155 ifs.unsetf(ios::skipws);
156 istream_iterator<char> beg(ifs);
157 istream_iterator<char> end;
159 return do_crc(beg,end);
163 } // namespace support