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"
31 using support::FileName;
33 using support::onlyFilename;
34 using support::suffixIs;
37 bool DepTable::dep_info::changed() const
39 return crc_prev != crc_cur && crc_cur != 0;
43 void DepTable::insert(FileName const & f, bool upd)
45 if (deplist.find(f) == deplist.end()) {
49 LYXERR(Debug::DEPEND, " CRC...");
50 di.crc_cur = f.checksum();
51 LYXERR(Debug::DEPEND, "done.");
53 stat(f.toFilesystemEncoding().c_str(), &f_info);
54 di.mtime_cur = long(f_info.st_mtime);
61 LYXERR(Debug::DEPEND, " Already in DepTable");
66 void DepTable::update()
68 LYXERR(Debug::DEPEND, "Updating DepTable...");
69 time_t const start_time = current_time();
71 DepList::iterator itr = deplist.begin();
72 while (itr != deplist.end()) {
73 dep_info &di = itr->second;
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");
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");
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);
97 if (lyxerr.debugging(Debug::DEPEND)) {
104 time_t const time_sec = current_time() - start_time;
105 LYXERR(Debug::DEPEND, "Finished updating DepTable ("
106 << long(time_sec) << " sec).");
110 bool DepTable::sumchange() const
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;
121 bool DepTable::haschanged(FileName const & fil) const
123 DepList::const_iterator cit = deplist.find(fil);
124 if (cit != deplist.end()) {
125 if (cit->second.changed())
132 bool DepTable::extchanged(string const & ext) const
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())
146 bool DepTable::ext_exist(string const & ext) const
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)) {
159 bool DepTable::exist(FileName const & fil) const
161 return deplist.find(fil) != deplist.end();
165 void DepTable::remove_files_with_extension(string const & suf)
167 DepList::iterator cit = deplist.begin();
168 DepList::iterator end = deplist.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);
184 void DepTable::remove_file(FileName const & filename)
186 DepList::iterator cit = deplist.begin();
187 DepList::iterator end = deplist.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);
203 void DepTable::write(FileName const & f) const
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)
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 << ' '
217 ofs << cit->second.crc_cur << ' '
218 << cit->second.mtime_cur << ' '
219 << cit->first << endl;
224 bool DepTable::read(FileName const & f)
226 ifstream ifs(f.toFilesystemEncoding().c_str());
229 // This doesn't change through the loop.
232 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
235 LYXERR(Debug::DEPEND, "Read dep: "
236 << di.crc_cur << ' ' << di.mtime_cur << ' ' << nome);
238 deplist[FileName(nome)] = di;
240 return deplist.size();