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/filetools.h"
19 #include "support/lstrings.h"
20 #include "support/lyxtime.h"
27 using namespace lyx::support;
33 bool DepTable::dep_info::changed() const
35 return crc_prev != crc_cur && crc_cur != 0;
39 void DepTable::insert(FileName const & f, bool upd)
41 if (deplist.find(f) == deplist.end()) {
45 LYXERR(Debug::DEPEND, " CRC...");
46 di.crc_cur = f.checksum();
47 LYXERR(Debug::DEPEND, "done.");
49 stat(f.toFilesystemEncoding().c_str(), &f_info);
50 di.mtime_cur = long(f_info.st_mtime);
57 LYXERR(Debug::DEPEND, " Already in DepTable");
62 void DepTable::update()
64 LYXERR(Debug::DEPEND, "Updating DepTable...");
65 time_t const start_time = current_time();
67 DepList::iterator itr = deplist.begin();
68 while (itr != deplist.end()) {
69 dep_info &di = itr->second;
72 if (stat(itr->first.toFilesystemEncoding().c_str(), &f_info) == 0) {
73 if (di.mtime_cur == f_info.st_mtime) {
74 di.crc_prev = di.crc_cur;
75 LYXERR(Debug::DEPEND, itr->first << " same mtime");
77 di.crc_prev = di.crc_cur;
78 LYXERR(Debug::DEPEND, itr->first << " CRC... ");
79 di.crc_cur = itr->first.checksum();
80 LYXERR(Debug::DEPEND, "done");
84 // remove stale files - if it's re-created, it
85 // will be re-inserted by deplog.
86 LYXERR(Debug::DEPEND, itr->first
87 << " doesn't exist. removing from DepTable.");
88 DepList::iterator doomed = itr++;
89 deplist.erase(doomed);
93 if (lyxerr.debugging(Debug::DEPEND)) {
100 time_t const time_sec = current_time() - start_time;
101 LYXERR(Debug::DEPEND, "Finished updating DepTable ("
102 << long(time_sec) << " sec).");
106 bool DepTable::sumchange() const
108 DepList::const_iterator cit = deplist.begin();
109 DepList::const_iterator end = deplist.end();
110 for (; cit != end; ++cit) {
111 if (cit->second.changed())
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))
155 bool DepTable::exist(FileName const & fil) const
157 return deplist.find(fil) != deplist.end();
161 void DepTable::remove_files_with_extension(string const & suf)
163 DepList::iterator cit = deplist.begin();
164 DepList::iterator end = deplist.end();
166 if (suffixIs(cit->first.absFileName(), suf)) {
167 // Can't erase the current iterator, but we
168 // can increment and then erase.
169 // Deplist is a map so only the erased
170 // iterator is invalidated.
171 DepList::iterator doomed = cit++;
172 deplist.erase(doomed);
180 void DepTable::remove_file(FileName const & filename)
182 DepList::iterator cit = deplist.begin();
183 DepList::iterator end = deplist.end();
185 if (cit->first == filename) {
186 // Can't erase the current iterator, but we
187 // can increment and then erase.
188 // deplist is a map so only the erased
189 // iterator is invalidated.
190 DepList::iterator doomed = cit++;
191 deplist.erase(doomed);
199 void DepTable::write(FileName const & f) const
201 ofstream ofs(f.toFilesystemEncoding().c_str());
202 DepList::const_iterator cit = deplist.begin();
203 DepList::const_iterator end = deplist.end();
204 for (; cit != end; ++cit) {
205 // Store the second (most recently calculated)
207 // The older one is effectively set to 0 upon re-load.
208 LYXERR(Debug::DEPEND, "Write dep: "
209 << cit->second.crc_cur << ' '
210 << cit->second.mtime_cur << ' '
213 ofs << cit->second.crc_cur << ' '
214 << cit->second.mtime_cur << ' '
215 << cit->first << endl;
220 bool DepTable::read(FileName const & f)
222 ifstream ifs(f.toFilesystemEncoding().c_str());
225 // This doesn't change through the loop.
228 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
231 LYXERR(Debug::DEPEND, "Read dep: "
232 << di.crc_cur << ' ' << di.mtime_cur << ' ' << nome);
234 deplist[FileName(nome)] = di;
236 return deplist.size();