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