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.
19 #include "support/lyxlib.h"
20 #include "support/filetools.h"
21 #include "support/lstrings.h"
22 #include "support/lyxtime.h"
38 using support::FileName;
40 using support::onlyFilename;
41 using support::suffixIs;
45 bool DepTable::dep_info::changed() const
47 return crc_prev != crc_cur && crc_cur != 0;
51 void DepTable::insert(FileName const & f, bool upd)
53 if (deplist.find(f) == deplist.end()) {
57 LYXERR(Debug::DEPEND) << " CRC..." << flush;
59 LYXERR(Debug::DEPEND) << "done." << endl;
61 stat(f.toFilesystemEncoding().c_str(), &f_info);
62 di.mtime_cur = long(f_info.st_mtime);
69 LYXERR(Debug::DEPEND) << " Already in DepTable" << endl;
74 void DepTable::update()
76 LYXERR(Debug::DEPEND) << "Updating DepTable..." << endl;
77 time_type const start_time = current_time();
79 DepList::iterator itr = deplist.begin();
80 while (itr != deplist.end()) {
81 dep_info &di = itr->second;
84 if (stat(itr->first.toFilesystemEncoding().c_str(), &f_info) == 0) {
85 if (di.mtime_cur == f_info.st_mtime) {
86 di.crc_prev = di.crc_cur;
87 LYXERR(Debug::DEPEND) << itr->first << " same mtime" << endl;
89 di.crc_prev = di.crc_cur;
90 LYXERR(Debug::DEPEND) << itr->first << " CRC... " << flush;
91 di.crc_cur = sum(itr->first);
92 LYXERR(Debug::DEPEND) << "done" << endl;
96 // remove stale files - if it's re-created, it
97 // will be re-inserted by deplog.
98 LYXERR(Debug::DEPEND) << itr->first
99 << " doesn't exist. removing from DepTable." << endl;
100 DepList::iterator doomed = itr++;
101 deplist.erase(doomed);
105 if (lyxerr.debugging(Debug::DEPEND)) {
112 time_type const time_sec = current_time() - start_time;
113 LYXERR(Debug::DEPEND) << "Finished updating DepTable ("
114 << time_sec << " sec)." << endl;
118 bool DepTable::sumchange() const
120 DepList::const_iterator cit = deplist.begin();
121 DepList::const_iterator end = deplist.end();
122 for (; cit != end; ++cit) {
123 if (cit->second.changed()) return true;
129 bool DepTable::haschanged(FileName const & fil) const
131 DepList::const_iterator cit = deplist.find(fil);
132 if (cit != deplist.end()) {
133 if (cit->second.changed())
140 bool DepTable::extchanged(string const & ext) const
142 DepList::const_iterator cit = deplist.begin();
143 DepList::const_iterator end = deplist.end();
144 for (; cit != end; ++cit) {
145 if (suffixIs(cit->first.absFilename(), ext)) {
146 if (cit->second.changed())
154 bool DepTable::ext_exist(string const & ext) const
156 DepList::const_iterator cit = deplist.begin();
157 DepList::const_iterator end = deplist.end();
158 for (; cit != end; ++cit) {
159 if (suffixIs(cit->first.absFilename(), ext)) {
167 bool DepTable::exist(FileName const & fil) const
169 return deplist.find(fil) != deplist.end();
173 void DepTable::remove_files_with_extension(string const & suf)
175 DepList::iterator cit = deplist.begin();
176 DepList::iterator end = deplist.end();
178 if (suffixIs(cit->first.absFilename(), suf)) {
179 // Can't erase the current iterator, but we
180 // can increment and then erase.
181 // Deplist is a map so only the erased
182 // iterator is invalidated.
183 DepList::iterator doomed = cit++;
184 deplist.erase(doomed);
192 void DepTable::remove_file(FileName const & filename)
194 DepList::iterator cit = deplist.begin();
195 DepList::iterator end = deplist.end();
197 if (cit->first == filename) {
198 // Can't erase the current iterator, but we
199 // can increment and then erase.
200 // deplist is a map so only the erased
201 // iterator is invalidated.
202 DepList::iterator doomed = cit++;
203 deplist.erase(doomed);
211 void DepTable::write(FileName const & f) const
213 ofstream ofs(f.toFilesystemEncoding().c_str());
214 DepList::const_iterator cit = deplist.begin();
215 DepList::const_iterator end = deplist.end();
216 for (; cit != end; ++cit) {
217 // Store the second (most recently calculated)
219 // The older one is effectively set to 0 upon re-load.
220 LYXERR(Debug::DEPEND) << "Write dep: "
221 << cit->second.crc_cur << ' '
222 << cit->second.mtime_cur << ' '
223 << cit->first << endl;
225 ofs << cit->second.crc_cur << ' '
226 << cit->second.mtime_cur << ' '
227 << cit->first << endl;
232 bool DepTable::read(FileName const & f)
234 ifstream ifs(f.toFilesystemEncoding().c_str());
237 // This doesn't change through the loop.
240 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
243 LYXERR(Debug::DEPEND) << "Read dep: "
244 << di.crc_cur << ' ' << di.mtime_cur << ' ' << nome << endl;
246 deplist[FileName(nome)] = di;
248 return deplist.size();