]> git.lyx.org Git - lyx.git/blob - src/support/lyxsum.cpp
some de-boostification
[lyx.git] / src / support / lyxsum.cpp
1 /**
2  * \file lyxsum.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/lyxlib.h"
14 #include "support/debug.h"
15 #include "support/FileName.h"
16
17 #include <boost/crc.hpp>
18
19 #include <algorithm>
20 #include <iomanip>
21
22 using std::endl;
23 using std::string;
24
25 // OK, this is ugly, but it is the only workaround I found to compile
26 // with gcc (any version) on a system which uses a non-GNU toolchain.
27 // The problem is that gcc uses a weak symbol for a particular
28 // instantiation and that the system linker usually does not
29 // understand those weak symbols (seen on HP-UX, tru64, AIX and
30 // others). Thus we force an explicit instanciation of this particular
31 // template (JMarc)
32 template struct boost::detail::crc_table_t<32, 0x04C11DB7, true>;
33
34 // Various implementations of lyx::sum(), depending on what methods
35 // are available. Order is faster to slowest.
36 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
37
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
40 #endif
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
43 #endif
44 #include <fcntl.h>
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48
49 #include <sys/mman.h>
50
51
52 namespace lyx {
53 namespace support {
54
55 unsigned long sum(FileName const & file)
56 {
57         LYXERR(Debug::FILES, "lyx::sum() using mmap (lightning fast)");
58
59         int fd = open(file.toFilesystemEncoding().c_str(), O_RDONLY);
60         if (!fd)
61                 return 0;
62
63         struct stat info;
64         fstat(fd, &info);
65
66         void * mm = mmap(0, info.st_size, PROT_READ,
67                          MAP_PRIVATE, fd, 0);
68         // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
69         if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
70                 close(fd);
71                 return 0;
72         }
73
74         char * beg = static_cast<char*>(mm);
75         char * end = beg + info.st_size;
76
77         boost::crc_32_type crc;
78         crc.process_block(beg, end);
79         unsigned long result = crc.checksum();
80
81         munmap(mm, info.st_size);
82         close(fd);
83
84         return result;
85 }
86
87 } // namespace support
88 } // namespace lyx
89
90 #else // No mmap
91
92 #include <fstream>
93 #include <iterator>
94
95
96 namespace {
97
98 template<typename InputIterator>
99 inline
100 unsigned long do_crc(InputIterator first, InputIterator last)
101 {
102         boost::crc_32_type crc;
103         crc = std::for_each(first, last, crc);
104         return crc.checksum();
105 }
106
107 } // namespace anon
108
109
110 namespace lyx {
111 namespace support {
112
113 using std::ifstream;
114 #if HAVE_DECL_ISTREAMBUF_ITERATOR
115 using std::istreambuf_iterator;
116
117 unsigned long sum(FileName const & file)
118 {
119         LYXERR(Debug::FILES, "lyx::sum() using istreambuf_iterator (fast)");
120
121         // a directory may be passed here so we need to test it. (bug 3622)
122         if (file.isDirectory())
123                 return 0;
124         string filename = file.toFilesystemEncoding();
125         ifstream ifs(filename.c_str(), std::ios_base::in | std::ios_base::binary);
126         if (!ifs)
127                 return 0;
128
129         istreambuf_iterator<char> beg(ifs);
130         istreambuf_iterator<char> end;
131
132         return do_crc(beg,end);
133 }
134 #else
135
136 using std::istream_iterator;
137 using std::ios;
138
139 unsigned long sum(FileName const & file)
140 {
141         LYXERR(Debug::FILES, "lyx::sum() using istream_iterator (slow as a snail)");
142
143         // a directory may be passed here so we need to test it. (bug 3622)
144         if (file.isDirectory())
145                 return 0;
146
147         string filename = file.toFilesystemEncoding();
148         ifstream ifs(filename.c_str(), std::ios_base::in | std::ios_base::binary);
149         if (!ifs)
150                 return 0;
151
152         ifs.unsetf(ios::skipws);
153         istream_iterator<char> beg(ifs);
154         istream_iterator<char> end;
155
156         return do_crc(beg,end);
157 }
158 #endif
159
160 } // namespace support
161 } // namespace lyx
162
163 #endif // mmap