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"
37 using support::FileName;
39 using support::onlyFilename;
40 using support::suffixIs;
43 bool DepTable::dep_info::changed() const
45 return crc_prev != crc_cur && crc_cur != 0;
49 void DepTable::insert(FileName const & f, bool upd)
51 if (deplist.find(f) == deplist.end()) {
55 LYXERR(Debug::DEPEND, " CRC...");
56 di.crc_cur = f.checksum();
57 LYXERR(Debug::DEPEND, "done.");
59 stat(f.toFilesystemEncoding().c_str(), &f_info);
60 di.mtime_cur = long(f_info.st_mtime);
67 LYXERR(Debug::DEPEND, " Already in DepTable");
72 void DepTable::update()
74 LYXERR(Debug::DEPEND, "Updating DepTable...");
75 time_t const start_time = current_time();
77 DepList::iterator itr = deplist.begin();
78 while (itr != deplist.end()) {
79 dep_info &di = itr->second;
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");
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");
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);
103 if (lyxerr.debugging(Debug::DEPEND)) {
110 time_t const time_sec = current_time() - start_time;
111 LYXERR(Debug::DEPEND, "Finished updating DepTable ("
112 << long(time_sec) << " sec).");
116 bool DepTable::sumchange() const
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;
127 bool DepTable::haschanged(FileName const & fil) const
129 DepList::const_iterator cit = deplist.find(fil);
130 if (cit != deplist.end()) {
131 if (cit->second.changed())
138 bool DepTable::extchanged(string const & ext) const
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())
152 bool DepTable::ext_exist(string const & ext) const
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)) {
165 bool DepTable::exist(FileName const & fil) const
167 return deplist.find(fil) != deplist.end();
171 void DepTable::remove_files_with_extension(string const & suf)
173 DepList::iterator cit = deplist.begin();
174 DepList::iterator end = deplist.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);
190 void DepTable::remove_file(FileName const & filename)
192 DepList::iterator cit = deplist.begin();
193 DepList::iterator end = deplist.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);
209 void DepTable::write(FileName const & f) const
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)
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 << ' '
223 ofs << cit->second.crc_cur << ' '
224 << cit->second.mtime_cur << ' '
225 << cit->first << endl;
230 bool DepTable::read(FileName const & f)
232 ifstream ifs(f.toFilesystemEncoding().c_str());
235 // This doesn't change through the loop.
238 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
241 LYXERR(Debug::DEPEND, "Read dep: "
242 << di.crc_cur << ' ' << di.mtime_cur << ' ' << nome);
244 deplist[FileName(nome)] = di;
246 return deplist.size();