]> git.lyx.org Git - lyx.git/blob - src/support/lyxsum.C
support for wasy symbols
[lyx.git] / src / support / lyxsum.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *           Copyright 2001 The LyX Team.
6  *
7  * ======================================================
8  */
9
10
11 #include <config.h>
12
13 #include "support/lyxlib.h"
14 #include "debug.h"
15
16 #include <algorithm>
17 #include <boost/crc.hpp>
18
19 using std::endl;
20
21 // Various implementations of lyx::sum(), depending on what methods
22 // are available. Order is faster to slowest.
23 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
24
25 // Make sure we get modern version of mmap and friends with void*,
26 // not `compatibility' version with caddr_t.
27 #define _POSIX_C_SOURCE 199506L
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <sys/mman.h>
34
35
36 unsigned long lyx::sum(string const & file)
37 {
38         lyxerr[Debug::FILES] << "lyx::sum() using mmap (lightning fast)"
39                              << endl;
40
41         int fd = open(file.c_str(), O_RDONLY);
42         if (!fd)
43                 return 0;
44
45         struct stat info;
46         fstat(fd, &info);
47
48         void * mm = mmap(0, info.st_size, PROT_READ,
49                          MAP_PRIVATE, fd, 0);
50         // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
51         if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
52                 close(fd);
53                 return 0;
54         }
55
56         char * beg = static_cast<char*>(mm);
57         char * end = beg + info.st_size;
58
59         boost::crc_32_type crc;
60         crc.process_block(beg, end);
61         unsigned long result = crc.checksum();
62
63         munmap(mm, info.st_size);
64         close(fd);
65
66         return result;
67 }
68 #else // No mmap
69
70 #include <fstream>
71 #include <iterator>
72
73 using std::for_each;
74
75 namespace {
76
77 template<typename InputIterator>
78 inline
79 unsigned long do_crc(InputIterator first, InputIterator last)
80 {
81         boost::crc_32_type crc;
82         crc = for_each(first, last, crc);
83         return crc.checksum();
84 }
85
86 } // namespace
87
88 #if HAVE_DECL_ISTREAMBUF_ITERATOR
89 using std::ifstream;
90 using std::istreambuf_iterator;
91
92 unsigned long lyx::sum(string const & file)
93 {
94         lyxerr[Debug::FILES] << "lyx::sum() using istreambuf_iterator (fast)"
95                              << endl;
96
97         ifstream ifs(file.c_str());
98         if (!ifs) return 0;
99
100         istreambuf_iterator<char> beg(ifs);
101         istreambuf_iterator<char> end;
102
103         return do_crc(beg,end);
104 }
105 #else
106
107 using std::istream_iterator;
108 using std::ios;
109
110 unsigned long lyx::sum(string const & file)
111 {
112         lyxerr[Debug::FILES]
113                 << "lyx::sum() using istream_iterator (slow as a snail)"
114                 << endl;
115
116         ifstream ifs(file.c_str());
117         if (!ifs) return 0;
118
119         ifs.unsetf(ios::skipws);
120         istream_iterator<char> beg(ifs);
121         istream_iterator<char> end;
122
123         return do_crc(beg,end);
124 }
125 #endif
126 #endif // mmap