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"
17 #include "support/FileName.h"
19 #include <boost/crc.hpp>
20 #include <boost/filesystem/operations.hpp>
27 namespace fs = boost::filesystem;
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
36 template struct boost::detail::crc_table_t<32, 0x04C11DB7, true>;
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)
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
46 #ifdef HAVE_SYS_TYPES_H
47 # include <sys/types.h>
49 #ifdef HAVE_SYS_STAT_H
50 # include <sys/stat.h>
63 unsigned long sum(FileName const & file)
65 LYXERR(Debug::FILES) << "lyx::sum() using mmap (lightning fast)"
68 int fd = open(file.toFilesystemEncoding().c_str(), O_RDONLY);
75 void * mm = mmap(0, info.st_size, PROT_READ,
77 // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
78 if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
83 char * beg = static_cast<char*>(mm);
84 char * end = beg + info.st_size;
86 boost::crc_32_type crc;
87 crc.process_block(beg, end);
88 unsigned long result = crc.checksum();
90 munmap(mm, info.st_size);
96 } // namespace support
107 template<typename InputIterator>
109 unsigned long do_crc(InputIterator first, InputIterator last)
111 boost::crc_32_type crc;
112 crc = std::for_each(first, last, crc);
113 return crc.checksum();
123 #if HAVE_DECL_ISTREAMBUF_ITERATOR
124 using std::istreambuf_iterator;
126 unsigned long sum(FileName const & file)
128 LYXERR(Debug::FILES) << "lyx::sum() using istreambuf_iterator (fast)"
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))
135 ifstream ifs(filename.c_str());
139 istreambuf_iterator<char> beg(ifs);
140 istreambuf_iterator<char> end;
142 return do_crc(beg,end);
146 using std::istream_iterator;
149 unsigned long sum(FileName const & file)
152 << "lyx::sum() using istream_iterator (slow as a snail)"
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))
159 ifstream ifs(filename.c_str());
163 ifs.unsetf(ios::skipws);
164 istream_iterator<char> beg(ifs);
165 istream_iterator<char> end;
167 return do_crc(beg,end);
171 } // namespace support