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