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/FileName.h"
19 #include "support/lstrings.h"
20 #include "support/lyxtime.h"
25 using namespace lyx::support;
31 bool DepTable::dep_info::changed() const
33 return crc_prev != crc_cur && crc_cur != 0;
37 void DepTable::insert(FileName const & f, bool upd)
39 if (deplist.find(f) == deplist.end()) {
42 if (upd && f.exists()) {
43 LYXERR(Debug::DEPEND, " CRC...");
44 di.crc_cur = f.checksum();
45 LYXERR(Debug::DEPEND, "done.");
46 di.mtime_cur = f.lastModified();
53 LYXERR(Debug::DEPEND, " Already in DepTable");
58 void DepTable::update()
60 LYXERR(Debug::DEPEND, "Updating DepTable...");
61 time_t const start_time = current_time();
63 DepList::iterator itr = deplist.begin();
64 while (itr != deplist.end()) {
65 FileName const & fn = itr->first;
66 dep_info &di = itr->second;
69 if (di.mtime_cur == fn.lastModified()) {
70 di.crc_prev = di.crc_cur;
71 LYXERR(Debug::DEPEND, itr->first << " same mtime");
73 di.crc_prev = di.crc_cur;
74 LYXERR(Debug::DEPEND, itr->first << " CRC... ");
75 di.crc_cur = fn.checksum();
76 LYXERR(Debug::DEPEND, "done");
80 // remove stale files - if it's re-created, it
81 // will be re-inserted by deplog.
82 LYXERR(Debug::DEPEND, fn << " doesn't exist. removing from DepTable.");
83 DepList::iterator doomed = itr++;
84 deplist.erase(doomed);
88 if (lyxerr.debugging(Debug::DEPEND)) {
95 time_t const time_sec = current_time() - start_time;
96 LYXERR(Debug::DEPEND, "Finished updating DepTable ("
97 << long(time_sec) << " sec).");
101 bool DepTable::sumchange() const
103 DepList::const_iterator cit = deplist.begin();
104 DepList::const_iterator end = deplist.end();
105 for (; cit != end; ++cit) {
106 if (cit->second.changed())
113 bool DepTable::haschanged(FileName const & fil) const
115 DepList::const_iterator cit = deplist.find(fil);
116 if (cit != deplist.end()) {
117 if (cit->second.changed())
124 bool DepTable::extchanged(string const & ext) const
126 DepList::const_iterator cit = deplist.begin();
127 DepList::const_iterator end = deplist.end();
128 for (; cit != end; ++cit) {
129 if (suffixIs(cit->first.absFileName(), ext)) {
130 if (cit->second.changed())
138 bool DepTable::ext_exist(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))
150 bool DepTable::exist(FileName const & fil) const
152 return deplist.find(fil) != deplist.end();
156 void DepTable::remove_files_with_extension(string const & suf)
158 DepList::iterator cit = deplist.begin();
159 DepList::iterator end = deplist.end();
161 if (suffixIs(cit->first.absFileName(), suf)) {
162 // Can't erase the current iterator, but we
163 // can increment and then erase.
164 // Deplist is a map so only the erased
165 // iterator is invalidated.
166 DepList::iterator doomed = cit++;
167 deplist.erase(doomed);
175 void DepTable::remove_file(FileName const & filename)
177 DepList::iterator cit = deplist.begin();
178 DepList::iterator end = deplist.end();
180 if (cit->first == filename) {
181 // Can't erase the current iterator, but we
182 // can increment and then erase.
183 // deplist is a map so only the erased
184 // iterator is invalidated.
185 DepList::iterator doomed = cit++;
186 deplist.erase(doomed);
194 void DepTable::write(FileName const & f) const
196 ofstream ofs(f.toFilesystemEncoding().c_str());
197 DepList::const_iterator cit = deplist.begin();
198 DepList::const_iterator end = deplist.end();
199 for (; cit != end; ++cit) {
200 // Store the second (most recently calculated)
202 // The older one is effectively set to 0 upon re-load.
203 LYXERR(Debug::DEPEND, "Write dep: "
204 << cit->second.crc_cur << ' '
205 << cit->second.mtime_cur << ' '
208 ofs << cit->second.crc_cur << ' '
209 << cit->second.mtime_cur << ' '
210 << cit->first << endl;
215 bool DepTable::read(FileName const & f)
217 ifstream ifs(f.toFilesystemEncoding().c_str());
220 // This doesn't change through the loop.
223 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
226 LYXERR(Debug::DEPEND, "Read dep: "
227 << di.crc_cur << ' ' << di.mtime_cur << ' ' << nome);
229 deplist[FileName(nome)] = di;
231 return !deplist.empty();