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"
31 #ifndef CXX_GLOBAL_CSTD
35 using support::FileName;
37 using support::makeAbsPath;
38 using support::onlyFilename;
39 using support::suffixIs;
50 bool DepTable::dep_info::changed() const
52 return crc_prev != crc_cur && crc_cur != 0;
56 void DepTable::insert(FileName const & f, bool upd)
58 if (deplist.find(f) == deplist.end()) {
62 lyxerr[Debug::DEPEND] << " CRC..." << flush;
64 lyxerr[Debug::DEPEND] << "done." << endl;
66 stat(f.toFilesystemEncoding().c_str(), &f_info);
67 di.mtime_cur = f_info.st_mtime;
74 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
79 void DepTable::update()
81 lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
82 time_type const start_time = current_time();
84 DepList::iterator itr = deplist.begin();
85 while (itr != deplist.end()) {
86 dep_info &di = itr->second;
89 if (stat(itr->first.toFilesystemEncoding().c_str(), &f_info) == 0) {
90 if (di.mtime_cur == f_info.st_mtime) {
91 di.crc_prev = di.crc_cur;
92 lyxerr[Debug::DEPEND] << itr->first << " same mtime" << endl;
94 di.crc_prev = di.crc_cur;
95 lyxerr[Debug::DEPEND] << itr->first << " CRC... " << flush;
96 di.crc_cur = sum(itr->first);
97 lyxerr[Debug::DEPEND] << "done" << endl;
100 // file doesn't exist
101 // remove stale files - if it's re-created, it
102 // will be re-inserted by deplog.
103 lyxerr[Debug::DEPEND] << itr->first
104 << " doesn't exist. removing from DepTable." << endl;
105 DepList::iterator doomed = itr++;
106 deplist.erase(doomed);
110 if (lyxerr.debugging(Debug::DEPEND)) {
117 time_type const time_sec = current_time() - start_time;
118 lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
119 << time_sec << " sec)." << endl;
123 bool DepTable::sumchange() const
125 DepList::const_iterator cit = deplist.begin();
126 DepList::const_iterator end = deplist.end();
127 for (; cit != end; ++cit) {
128 if (cit->second.changed()) return true;
134 bool DepTable::haschanged(FileName const & fil) const
136 DepList::const_iterator cit = deplist.find(fil);
137 if (cit != deplist.end()) {
138 if (cit->second.changed())
145 bool DepTable::extchanged(string const & ext) const
147 DepList::const_iterator cit = deplist.begin();
148 DepList::const_iterator end = deplist.end();
149 for (; cit != end; ++cit) {
150 if (suffixIs(cit->first.absFilename(), ext)) {
151 if (cit->second.changed())
159 bool DepTable::ext_exist(string const & ext) const
161 DepList::const_iterator cit = deplist.begin();
162 DepList::const_iterator end = deplist.end();
163 for (; cit != end; ++cit) {
164 if (suffixIs(cit->first.absFilename(), ext)) {
172 bool DepTable::exist(FileName const & fil) const
174 return deplist.find(fil) != deplist.end();
178 void DepTable::remove_files_with_extension(string const & suf)
180 DepList::iterator cit = deplist.begin();
181 DepList::iterator end = deplist.end();
183 if (suffixIs(cit->first.absFilename(), suf)) {
184 // Can't erase the current iterator, but we
185 // can increment and then erase.
186 // Deplist is a map so only the erased
187 // iterator is invalidated.
188 DepList::iterator doomed = cit++;
189 deplist.erase(doomed);
197 void DepTable::remove_file(FileName const & filename)
199 DepList::iterator cit = deplist.begin();
200 DepList::iterator end = deplist.end();
202 if (cit->first == filename) {
203 // Can't erase the current iterator, but we
204 // can increment and then erase.
205 // deplist is a map so only the erased
206 // iterator is invalidated.
207 DepList::iterator doomed = cit++;
208 deplist.erase(doomed);
216 void DepTable::write(FileName const & f) const
218 ofstream ofs(f.toFilesystemEncoding().c_str());
219 DepList::const_iterator cit = deplist.begin();
220 DepList::const_iterator end = deplist.end();
221 for (; cit != end; ++cit) {
222 if (lyxerr.debugging(Debug::DEPEND)) {
223 // Store the second (most recently calculated)
225 // The older one is effectively set to 0 upon re-load.
226 lyxerr << "Write dep: "
227 << cit->second.crc_cur << ' '
228 << cit->second.mtime_cur << ' '
229 << cit->first << endl;
231 ofs << cit->second.crc_cur << ' '
232 << cit->second.mtime_cur << ' '
233 << cit->first << endl;
238 bool DepTable::read(FileName const & f)
240 ifstream ifs(f.toFilesystemEncoding().c_str());
243 // This doesn't change through the loop.
246 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
248 if (lyxerr.debugging(Debug::DEPEND)) {
249 lyxerr << "Read dep: "
251 << di.mtime_cur << ' '
254 deplist[FileName(nome)] = di;
256 return deplist.size();