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