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