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;
44 bool DepTable::dep_info::changed() const
46 return crc_prev != crc_cur && crc_cur != 0;
50 void DepTable::insert(FileName const & f, bool upd)
52 if (deplist.find(f) == deplist.end()) {
56 LYXERR(Debug::DEPEND, " CRC...");
58 LYXERR(Debug::DEPEND, "done.");
60 stat(f.toFilesystemEncoding().c_str(), &f_info);
61 di.mtime_cur = long(f_info.st_mtime);
68 LYXERR(Debug::DEPEND, " Already in DepTable");
73 void DepTable::update()
75 LYXERR(Debug::DEPEND, "Updating DepTable...");
76 time_type const start_time = current_time();
78 DepList::iterator itr = deplist.begin();
79 while (itr != deplist.end()) {
80 dep_info &di = itr->second;
83 if (stat(itr->first.toFilesystemEncoding().c_str(), &f_info) == 0) {
84 if (di.mtime_cur == f_info.st_mtime) {
85 di.crc_prev = di.crc_cur;
86 LYXERR(Debug::DEPEND, itr->first << " same mtime");
88 di.crc_prev = di.crc_cur;
89 LYXERR(Debug::DEPEND, itr->first << " CRC... ");
90 di.crc_cur = sum(itr->first);
91 LYXERR(Debug::DEPEND, "done");
95 // remove stale files - if it's re-created, it
96 // will be re-inserted by deplog.
97 LYXERR(Debug::DEPEND, itr->first
98 << " doesn't exist. removing from DepTable.");
99 DepList::iterator doomed = itr++;
100 deplist.erase(doomed);
104 if (lyxerr.debugging(Debug::DEPEND)) {
111 time_type const time_sec = current_time() - start_time;
112 LYXERR(Debug::DEPEND, "Finished updating DepTable ("
113 << time_sec << " sec).");
117 bool DepTable::sumchange() const
119 DepList::const_iterator cit = deplist.begin();
120 DepList::const_iterator end = deplist.end();
121 for (; cit != end; ++cit) {
122 if (cit->second.changed()) return true;
128 bool DepTable::haschanged(FileName const & fil) const
130 DepList::const_iterator cit = deplist.find(fil);
131 if (cit != deplist.end()) {
132 if (cit->second.changed())
139 bool DepTable::extchanged(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)) {
145 if (cit->second.changed())
153 bool DepTable::ext_exist(string const & ext) const
155 DepList::const_iterator cit = deplist.begin();
156 DepList::const_iterator end = deplist.end();
157 for (; cit != end; ++cit) {
158 if (suffixIs(cit->first.absFilename(), ext)) {
166 bool DepTable::exist(FileName const & fil) const
168 return deplist.find(fil) != deplist.end();
172 void DepTable::remove_files_with_extension(string const & suf)
174 DepList::iterator cit = deplist.begin();
175 DepList::iterator end = deplist.end();
177 if (suffixIs(cit->first.absFilename(), suf)) {
178 // Can't erase the current iterator, but we
179 // can increment and then erase.
180 // Deplist is a map so only the erased
181 // iterator is invalidated.
182 DepList::iterator doomed = cit++;
183 deplist.erase(doomed);
191 void DepTable::remove_file(FileName const & filename)
193 DepList::iterator cit = deplist.begin();
194 DepList::iterator end = deplist.end();
196 if (cit->first == filename) {
197 // Can't erase the current iterator, but we
198 // can increment and then erase.
199 // deplist is a map so only the erased
200 // iterator is invalidated.
201 DepList::iterator doomed = cit++;
202 deplist.erase(doomed);
210 void DepTable::write(FileName const & f) const
212 ofstream ofs(f.toFilesystemEncoding().c_str());
213 DepList::const_iterator cit = deplist.begin();
214 DepList::const_iterator end = deplist.end();
215 for (; cit != end; ++cit) {
216 // Store the second (most recently calculated)
218 // The older one is effectively set to 0 upon re-load.
219 LYXERR(Debug::DEPEND, "Write dep: "
220 << cit->second.crc_cur << ' '
221 << cit->second.mtime_cur << ' '
224 ofs << cit->second.crc_cur << ' '
225 << cit->second.mtime_cur << ' '
226 << cit->first << endl;
231 bool DepTable::read(FileName const & f)
233 ifstream ifs(f.toFilesystemEncoding().c_str());
236 // This doesn't change through the loop.
239 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
242 LYXERR(Debug::DEPEND, "Read dep: "
243 << di.crc_cur << ' ' << di.mtime_cur << ' ' << nome);
245 deplist[FileName(nome)] = di;
247 return deplist.size();