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/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/lyxtime.h"
26 using namespace lyx::support;
32 bool DepTable::dep_info::changed() const
34 return crc_prev != crc_cur && crc_cur != 0;
38 void DepTable::insert(FileName const & f, bool upd)
40 if (deplist.find(f) == deplist.end()) {
43 if (upd && f.exists()) {
44 LYXERR(Debug::DEPEND, " CRC...");
45 di.crc_cur = f.checksum();
46 LYXERR(Debug::DEPEND, "done.");
47 di.mtime_cur = f.lastModified();
54 LYXERR(Debug::DEPEND, " Already in DepTable");
59 void DepTable::update()
61 LYXERR(Debug::DEPEND, "Updating DepTable...");
62 time_t const start_time = current_time();
64 DepList::iterator itr = deplist.begin();
65 while (itr != deplist.end()) {
66 FileName const & fn = itr->first;
67 dep_info &di = itr->second;
70 if (di.mtime_cur == fn.lastModified()) {
71 di.crc_prev = di.crc_cur;
72 LYXERR(Debug::DEPEND, itr->first << " same mtime");
74 di.crc_prev = di.crc_cur;
75 LYXERR(Debug::DEPEND, itr->first << " CRC... ");
76 di.crc_cur = fn.checksum();
77 LYXERR(Debug::DEPEND, "done");
81 // remove stale files - if it's re-created, it
82 // will be re-inserted by deplog.
83 LYXERR(Debug::DEPEND, fn << " doesn't exist. removing from DepTable.");
84 DepList::iterator doomed = itr++;
85 deplist.erase(doomed);
89 if (lyxerr.debugging(Debug::DEPEND)) {
96 time_t const time_sec = current_time() - start_time;
97 LYXERR(Debug::DEPEND, "Finished updating DepTable ("
98 << long(time_sec) << " sec).");
102 bool DepTable::sumchange() const
104 DepList::const_iterator cit = deplist.begin();
105 DepList::const_iterator end = deplist.end();
106 for (; cit != end; ++cit) {
107 if (cit->second.changed())
114 bool DepTable::haschanged(FileName const & fil) const
116 DepList::const_iterator cit = deplist.find(fil);
117 if (cit != deplist.end()) {
118 if (cit->second.changed())
125 bool DepTable::extchanged(string const & ext) const
127 DepList::const_iterator cit = deplist.begin();
128 DepList::const_iterator end = deplist.end();
129 for (; cit != end; ++cit) {
130 if (suffixIs(cit->first.absFileName(), ext)) {
131 if (cit->second.changed())
139 bool DepTable::ext_exist(string const & ext) const
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))
151 bool DepTable::exist(FileName const & fil) const
153 return deplist.find(fil) != deplist.end();
157 void DepTable::remove_files_with_extension(string const & suf)
159 DepList::iterator cit = deplist.begin();
160 DepList::iterator end = deplist.end();
162 if (suffixIs(cit->first.absFileName(), suf)) {
163 // Can't erase the current iterator, but we
164 // can increment and then erase.
165 // Deplist is a map so only the erased
166 // iterator is invalidated.
167 DepList::iterator doomed = cit++;
168 deplist.erase(doomed);
176 void DepTable::remove_file(FileName const & filename)
178 DepList::iterator cit = deplist.begin();
179 DepList::iterator end = deplist.end();
181 if (cit->first == filename) {
182 // Can't erase the current iterator, but we
183 // can increment and then erase.
184 // deplist is a map so only the erased
185 // iterator is invalidated.
186 DepList::iterator doomed = cit++;
187 deplist.erase(doomed);
195 void DepTable::write(FileName const & f) const
197 ofstream ofs(f.toFilesystemEncoding().c_str());
198 DepList::const_iterator cit = deplist.begin();
199 DepList::const_iterator end = deplist.end();
200 for (; cit != end; ++cit) {
201 // Store the second (most recently calculated)
203 // The older one is effectively set to 0 upon re-load.
204 LYXERR(Debug::DEPEND, "Write dep: "
205 << cit->second.crc_cur << ' '
206 << cit->second.mtime_cur << ' '
209 ofs << cit->second.crc_cur << ' '
210 << cit->second.mtime_cur << ' '
211 << cit->first << endl;
216 bool DepTable::read(FileName const & f)
218 ifstream ifs(f.toFilesystemEncoding().c_str());
221 // This doesn't change through the loop.
224 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
227 LYXERR(Debug::DEPEND, "Read dep: "
228 << di.crc_cur << ' ' << di.mtime_cur << ' ' << nome);
230 deplist[FileName(nome)] = di;
232 return !deplist.empty();