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"
28 #ifndef CXX_GLOBAL_CSTD
41 using support::FileName;
43 using support::onlyFilename;
44 using support::suffixIs;
48 bool DepTable::dep_info::changed() const
50 return crc_prev != crc_cur && crc_cur != 0;
54 void DepTable::insert(FileName const & f, bool upd)
56 if (deplist.find(f) == deplist.end()) {
60 LYXERR(Debug::DEPEND) << " CRC..." << flush;
62 LYXERR(Debug::DEPEND) << "done." << endl;
64 stat(f.toFilesystemEncoding().c_str(), &f_info);
65 di.mtime_cur = f_info.st_mtime;
72 LYXERR(Debug::DEPEND) << " Already in DepTable" << endl;
77 void DepTable::update()
79 LYXERR(Debug::DEPEND) << "Updating DepTable..." << endl;
80 time_type const start_time = current_time();
82 DepList::iterator itr = deplist.begin();
83 while (itr != deplist.end()) {
84 dep_info &di = itr->second;
87 if (stat(itr->first.toFilesystemEncoding().c_str(), &f_info) == 0) {
88 if (di.mtime_cur == f_info.st_mtime) {
89 di.crc_prev = di.crc_cur;
90 LYXERR(Debug::DEPEND) << itr->first << " same mtime" << endl;
92 di.crc_prev = di.crc_cur;
93 LYXERR(Debug::DEPEND) << itr->first << " CRC... " << flush;
94 di.crc_cur = sum(itr->first);
95 LYXERR(Debug::DEPEND) << "done" << endl;
99 // remove stale files - if it's re-created, it
100 // will be re-inserted by deplog.
101 LYXERR(Debug::DEPEND) << itr->first
102 << " doesn't exist. removing from DepTable." << endl;
103 DepList::iterator doomed = itr++;
104 deplist.erase(doomed);
108 if (lyxerr.debugging(Debug::DEPEND)) {
115 time_type const time_sec = current_time() - start_time;
116 LYXERR(Debug::DEPEND) << "Finished updating DepTable ("
117 << time_sec << " sec)." << endl;
121 bool DepTable::sumchange() const
123 DepList::const_iterator cit = deplist.begin();
124 DepList::const_iterator end = deplist.end();
125 for (; cit != end; ++cit) {
126 if (cit->second.changed()) return true;
132 bool DepTable::haschanged(FileName const & fil) const
134 DepList::const_iterator cit = deplist.find(fil);
135 if (cit != deplist.end()) {
136 if (cit->second.changed())
143 bool DepTable::extchanged(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)) {
149 if (cit->second.changed())
157 bool DepTable::ext_exist(string const & ext) const
159 DepList::const_iterator cit = deplist.begin();
160 DepList::const_iterator end = deplist.end();
161 for (; cit != end; ++cit) {
162 if (suffixIs(cit->first.absFilename(), ext)) {
170 bool DepTable::exist(FileName const & fil) const
172 return deplist.find(fil) != deplist.end();
176 void DepTable::remove_files_with_extension(string const & suf)
178 DepList::iterator cit = deplist.begin();
179 DepList::iterator end = deplist.end();
181 if (suffixIs(cit->first.absFilename(), suf)) {
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::remove_file(FileName const & filename)
197 DepList::iterator cit = deplist.begin();
198 DepList::iterator end = deplist.end();
200 if (cit->first == filename) {
201 // Can't erase the current iterator, but we
202 // can increment and then erase.
203 // deplist is a map so only the erased
204 // iterator is invalidated.
205 DepList::iterator doomed = cit++;
206 deplist.erase(doomed);
214 void DepTable::write(FileName const & f) const
216 ofstream ofs(f.toFilesystemEncoding().c_str());
217 DepList::const_iterator cit = deplist.begin();
218 DepList::const_iterator end = deplist.end();
219 for (; cit != end; ++cit) {
220 // Store the second (most recently calculated)
222 // The older one is effectively set to 0 upon re-load.
223 LYXERR(Debug::DEPEND) << "Write dep: "
224 << cit->second.crc_cur << ' '
225 << cit->second.mtime_cur << ' '
226 << cit->first << endl;
228 ofs << cit->second.crc_cur << ' '
229 << cit->second.mtime_cur << ' '
230 << cit->first << endl;
235 bool DepTable::read(FileName const & f)
237 ifstream ifs(f.toFilesystemEncoding().c_str());
240 // This doesn't change through the loop.
243 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
246 LYXERR(Debug::DEPEND) << "Read dep: "
247 << di.crc_cur << ' ' << di.mtime_cur << ' ' << nome << endl;
249 deplist[FileName(nome)] = di;
251 return deplist.size();