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 #ifdef HAVE_SYS_TYPES_H
43 # include <sys/types.h>
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
59 unsigned long sum(FileName const & file)
61 LYXERR(Debug::FILES) << "lyx::sum() using mmap (lightning fast)"
64 int fd = open(file.toFilesystemEncoding().c_str(), O_RDONLY);
71 void * mm = mmap(0, info.st_size, PROT_READ,
73 // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
74 if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
79 char * beg = static_cast<char*>(mm);
80 char * end = beg + info.st_size;
82 boost::crc_32_type crc;
83 crc.process_block(beg, end);
84 unsigned long result = crc.checksum();
86 munmap(mm, info.st_size);
92 } // namespace support
103 template<typename InputIterator>
105 unsigned long do_crc(InputIterator first, InputIterator last)
107 boost::crc_32_type crc;
108 crc = std::for_each(first, last, crc);
109 return crc.checksum();
119 #if HAVE_DECL_ISTREAMBUF_ITERATOR
120 using std::istreambuf_iterator;
122 unsigned long sum(FileName const & file)
124 LYXERR(Debug::FILES) << "lyx::sum() using istreambuf_iterator (fast)"
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))
131 ifstream ifs(filename.c_str());
135 istreambuf_iterator<char> beg(ifs);
136 istreambuf_iterator<char> end;
138 return do_crc(beg,end);
142 using std::istream_iterator;
145 unsigned long sum(FileName const & file)
148 << "lyx::sum() using istream_iterator (slow as a snail)"
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))
155 ifstream ifs(filename.c_str());
159 ifs.unsetf(ios::skipws);
160 istream_iterator<char> beg(ifs);
161 istream_iterator<char> end;
163 return do_crc(beg,end);
167 } // namespace support