]> git.lyx.org Git - lyx.git/blob - src/DepTable.cpp
prepare Qt 5.6 builds
[lyx.git] / src / DepTable.cpp
1 /**
2  * \file DepTable.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  * \author Jean-Marc Lasgouttes
8  * \author Ben Stanley
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "DepTable.h"
16
17 #include "support/debug.h"
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
20 #include "support/lyxtime.h"
21
22 #include <sys/stat.h>
23
24 #include <fstream>
25
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30
31
32 inline
33 bool DepTable::dep_info::changed() const
34 {
35         return crc_prev != crc_cur && crc_cur != 0;
36 }
37
38
39 void DepTable::insert(FileName const & f, bool upd)
40 {
41         if (deplist.find(f) == deplist.end()) {
42                 dep_info di;
43                 di.crc_prev = 0;
44                 if (upd) {
45                         LYXERR(Debug::DEPEND, " CRC...");
46                         di.crc_cur = f.checksum();
47                         LYXERR(Debug::DEPEND, "done.");
48                         struct stat f_info;
49                         stat(f.toFilesystemEncoding().c_str(), &f_info);
50                         di.mtime_cur = long(f_info.st_mtime);
51                 } else {
52                         di.crc_cur = 0;
53                         di.mtime_cur = 0;
54                 }
55                 deplist[f] = di;
56         } else {
57                 LYXERR(Debug::DEPEND, " Already in DepTable");
58         }
59 }
60
61
62 void DepTable::update()
63 {
64         LYXERR(Debug::DEPEND, "Updating DepTable...");
65         time_t const start_time = current_time();
66
67         DepList::iterator itr = deplist.begin();
68         while (itr != deplist.end()) {
69                 dep_info &di = itr->second;
70
71                 struct stat f_info;
72                 if (stat(itr->first.toFilesystemEncoding().c_str(), &f_info) == 0) {
73                         if (di.mtime_cur == f_info.st_mtime) {
74                                 di.crc_prev = di.crc_cur;
75                                 LYXERR(Debug::DEPEND, itr->first << " same mtime");
76                         } else {
77                                 di.crc_prev = di.crc_cur;
78                                 LYXERR(Debug::DEPEND, itr->first << " CRC... ");
79                                 di.crc_cur = itr->first.checksum();
80                                 LYXERR(Debug::DEPEND, "done");
81                         }
82                 } else {
83                         // file doesn't exist
84                         // remove stale files - if it's re-created, it
85                         // will be re-inserted by deplog.
86                         LYXERR(Debug::DEPEND, itr->first
87                                 << " doesn't exist. removing from DepTable.");
88                         DepList::iterator doomed = itr++;
89                         deplist.erase(doomed);
90                         continue;
91                 }
92
93                 if (lyxerr.debugging(Debug::DEPEND)) {
94                         if (di.changed())
95                                 lyxerr << " +";
96                         lyxerr << endl;
97                 }
98                 ++itr;
99         }
100         time_t const time_sec = current_time() - start_time;
101         LYXERR(Debug::DEPEND, "Finished updating DepTable ("
102                 << long(time_sec) << " sec).");
103 }
104
105
106 bool DepTable::sumchange() const
107 {
108         DepList::const_iterator cit = deplist.begin();
109         DepList::const_iterator end = deplist.end();
110         for (; cit != end; ++cit) {
111                 if (cit->second.changed())
112                         return true;
113         }
114         return false;
115 }
116
117
118 bool DepTable::haschanged(FileName const & fil) const
119 {
120         DepList::const_iterator cit = deplist.find(fil);
121         if (cit != deplist.end()) {
122                 if (cit->second.changed())
123                         return true;
124         }
125         return false;
126 }
127
128
129 bool DepTable::extchanged(string const & ext) const
130 {
131         DepList::const_iterator cit = deplist.begin();
132         DepList::const_iterator end = deplist.end();
133         for (; cit != end; ++cit) {
134                 if (suffixIs(cit->first.absFileName(), ext)) {
135                         if (cit->second.changed())
136                                 return true;
137                 }
138         }
139         return false;
140 }
141
142
143 bool DepTable::ext_exist(string const & ext) const
144 {
145         DepList::const_iterator cit = deplist.begin();
146         DepList::const_iterator end = deplist.end();
147         for (; cit != end; ++cit) {
148                 if (suffixIs(cit->first.absFileName(), ext))
149                         return true;
150         }
151         return false;
152 }
153
154
155 bool DepTable::exist(FileName const & fil) const
156 {
157         return deplist.find(fil) != deplist.end();
158 }
159
160
161 void DepTable::remove_files_with_extension(string const & suf)
162 {
163         DepList::iterator cit = deplist.begin();
164         DepList::iterator end = deplist.end();
165         while (cit != end) {
166                 if (suffixIs(cit->first.absFileName(), suf)) {
167                         // Can't erase the current iterator, but we
168                         // can increment and then erase.
169                         // Deplist is a map so only the erased
170                         // iterator is invalidated.
171                         DepList::iterator doomed = cit++;
172                         deplist.erase(doomed);
173                         continue;
174                 }
175                 ++cit;
176         }
177 }
178
179
180 void DepTable::remove_file(FileName const & filename)
181 {
182         DepList::iterator cit = deplist.begin();
183         DepList::iterator end = deplist.end();
184         while (cit != end) {
185                 if (cit->first == filename) {
186                         // Can't erase the current iterator, but we
187                         // can increment and then erase.
188                         // deplist is a map so only the erased
189                         // iterator is invalidated.
190                         DepList::iterator doomed = cit++;
191                         deplist.erase(doomed);
192                         continue;
193                 }
194                 ++cit;
195         }
196 }
197
198
199 void DepTable::write(FileName const & f) const
200 {
201         ofstream ofs(f.toFilesystemEncoding().c_str());
202         DepList::const_iterator cit = deplist.begin();
203         DepList::const_iterator end = deplist.end();
204         for (; cit != end; ++cit) {
205                 // Store the second (most recently calculated)
206                 // CRC value.
207                 // The older one is effectively set to 0 upon re-load.
208                 LYXERR(Debug::DEPEND, "Write dep: "
209                        << cit->second.crc_cur << ' '
210                        << cit->second.mtime_cur << ' '
211                        << cit->first);
212
213                 ofs << cit->second.crc_cur << ' '
214                     << cit->second.mtime_cur << ' '
215                     << cit->first << endl;
216         }
217 }
218
219
220 bool DepTable::read(FileName const & f)
221 {
222         ifstream ifs(f.toFilesystemEncoding().c_str());
223         string nome;
224         dep_info di;
225         // This doesn't change through the loop.
226         di.crc_prev = 0;
227
228         while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
229                 nome = ltrim(nome);
230
231                 LYXERR(Debug::DEPEND, "Read dep: "
232                        << di.crc_cur << ' ' << di.mtime_cur << ' ' << nome);
233
234                 deplist[FileName(nome)] = di;
235         }
236         return deplist.size();
237 }
238
239
240 } // namespace lyx