3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
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"
28 using namespace lyx::support;
34 bool DepTable::dep_info::changed() const
36 return crc_prev != crc_cur && crc_cur != 0;
40 void DepTable::insert(FileName const & f, bool upd)
42 if (deplist.find(f) == deplist.end()) {
46 LYXERR(Debug::DEPEND, " CRC...");
47 di.crc_cur = f.checksum();
48 LYXERR(Debug::DEPEND, "done.");
50 stat(f.toFilesystemEncoding().c_str(), &f_info);
51 di.mtime_cur = long(f_info.st_mtime);
58 LYXERR(Debug::DEPEND, " Already in DepTable");
63 void DepTable::update()
65 LYXERR(Debug::DEPEND, "Updating DepTable...");
66 time_t const start_time = current_time();
68 DepList::iterator itr = deplist.begin();
69 while (itr != deplist.end()) {
70 dep_info &di = itr->second;
73 if (stat(itr->first.toFilesystemEncoding().c_str(), &f_info) == 0) {
74 if (di.mtime_cur == f_info.st_mtime) {
75 di.crc_prev = di.crc_cur;
76 LYXERR(Debug::DEPEND, itr->first << " same mtime");
78 di.crc_prev = di.crc_cur;
79 LYXERR(Debug::DEPEND, itr->first << " CRC... ");
80 di.crc_cur = itr->first.checksum();
81 LYXERR(Debug::DEPEND, "done");
85 // remove stale files - if it's re-created, it
86 // will be re-inserted by deplog.
87 LYXERR(Debug::DEPEND, itr->first
88 << " doesn't exist. removing from DepTable.");
89 DepList::iterator doomed = itr++;
90 deplist.erase(doomed);
94 if (lyxerr.debugging(Debug::DEPEND)) {
101 time_t const time_sec = current_time() - start_time;
102 LYXERR(Debug::DEPEND, "Finished updating DepTable ("
103 << long(time_sec) << " sec).");
107 bool DepTable::sumchange() const
109 DepList::const_iterator cit = deplist.begin();
110 DepList::const_iterator end = deplist.end();
111 for (; cit != end; ++cit) {
112 if (cit->second.changed()) return true;
118 bool DepTable::haschanged(FileName const & fil) const
120 DepList::const_iterator cit = deplist.find(fil);
121 if (cit != deplist.end()) {
122 if (cit->second.changed())
129 bool DepTable::extchanged(string const & ext) const
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())
143 bool DepTable::ext_exist(string const & ext) const
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)) {
156 bool DepTable::exist(FileName const & fil) const
158 return deplist.find(fil) != deplist.end();
162 void DepTable::remove_files_with_extension(string const & suf)
164 DepList::iterator cit = deplist.begin();
165 DepList::iterator end = deplist.end();
167 if (suffixIs(cit->first.absFilename(), suf)) {
168 // Can't erase the current iterator, but we
169 // can increment and then erase.
170 // Deplist is a map so only the erased
171 // iterator is invalidated.
172 DepList::iterator doomed = cit++;
173 deplist.erase(doomed);
181 void DepTable::remove_file(FileName const & filename)
183 DepList::iterator cit = deplist.begin();
184 DepList::iterator end = deplist.end();
186 if (cit->first == filename) {
187 // Can't erase the current iterator, but we
188 // can increment and then erase.
189 // deplist is a map so only the erased
190 // iterator is invalidated.
191 DepList::iterator doomed = cit++;
192 deplist.erase(doomed);
200 void DepTable::write(FileName const & f) const
202 ofstream ofs(f.toFilesystemEncoding().c_str());
203 DepList::const_iterator cit = deplist.begin();
204 DepList::const_iterator end = deplist.end();
205 for (; cit != end; ++cit) {
206 // Store the second (most recently calculated)
208 // The older one is effectively set to 0 upon re-load.
209 LYXERR(Debug::DEPEND, "Write dep: "
210 << cit->second.crc_cur << ' '
211 << cit->second.mtime_cur << ' '
214 ofs << cit->second.crc_cur << ' '
215 << cit->second.mtime_cur << ' '
216 << cit->first << endl;
221 bool DepTable::read(FileName const & f)
223 ifstream ifs(f.toFilesystemEncoding().c_str());
226 // This doesn't change through the loop.
229 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
232 LYXERR(Debug::DEPEND, "Read dep: "
233 << di.crc_cur << ' ' << di.mtime_cur << ' ' << nome);
235 deplist[FileName(nome)] = di;
237 return deplist.size();