]> git.lyx.org Git - lyx.git/blob - src/support/lyxsum.C
fix bug 175 (minipage in minipage); fix some warnings; apply external.diff from Herbert
[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 <algorithm>
14 #include <boost/crc.hpp>
15
16 #include "support/lyxlib.h"
17 #include "debug.h"
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 unsigned long lyx::sum(string const & file)
36 {
37         lyxerr[Debug::FILES] << "lyx::sum() using mmap (lightning fast)"
38                              << endl;
39         
40         int fd = open(file.c_str(), O_RDONLY);
41         if(!fd)
42                 return 0;
43         
44         struct stat info;
45         fstat(fd, &info);
46         
47         void * mm = mmap(0, info.st_size, PROT_READ,
48                          MAP_PRIVATE, fd, 0);
49         // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
50         if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
51                 close(fd);
52                 return 0;
53         }
54         
55         char * beg = static_cast<char*>(mm);
56         char * end = beg + info.st_size;
57         
58         boost::crc_32_type crc;
59         crc.process_block(beg, end);
60         unsigned long result = crc.checksum();
61         
62         munmap(mm, info.st_size);
63         close(fd);
64         
65         return result;
66 }
67 #else // No mmap
68
69 #include <fstream>
70 #include <iterator>
71
72 namespace {
73
74 template<typename InputIterator>
75 inline
76 unsigned long do_crc(InputIterator first, InputIterator last)
77 {
78         boost::crc_32_type crc;
79         crc = std::for_each(first, last, crc);
80         return crc.checksum();
81 }
82
83 } // namespace
84
85 #if HAVE_DECL_ISTREAMBUF_ITERATOR
86 unsigned long lyx::sum(string const & file)
87 {
88         lyxerr[Debug::FILES] << "lyx::sum() using istreambuf_iterator (fast)"
89                              << endl;
90
91         std::ifstream ifs(file.c_str());
92         if (!ifs) return 0;
93         
94         std::istreambuf_iterator<char> beg(ifs);
95         std::istreambuf_iterator<char> end;
96         
97         return do_crc(beg,end);
98 }
99 #else
100 unsigned long lyx::sum(string const & file)
101 {
102         lyxerr[Debug::FILES]
103                 << "lyx::sum() using istream_iterator (slow as a snail)"
104                 << endl;
105         
106         std::ifstream ifs(file.c_str());
107         if (!ifs) return 0;
108         
109         ifs.unsetf(std::ios::skipws);
110         std::istream_iterator<char> beg(ifs);
111         std::istream_iterator<char> end;
112         
113         return do_crc(beg,end);
114 }
115 #endif
116 #endif // mmap