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::onlyFilename;
38 using support::suffixIs;
49 bool DepTable::dep_info::changed() const
51 return crc_prev != crc_cur && crc_cur != 0;
55 void DepTable::insert(FileName const & f, bool upd)
57 if (deplist.find(f) == deplist.end()) {
61 lyxerr[Debug::DEPEND] << " CRC..." << flush;
63 lyxerr[Debug::DEPEND] << "done." << endl;
65 stat(f.toFilesystemEncoding().c_str(), &f_info);
66 di.mtime_cur = f_info.st_mtime;
73 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
78 void DepTable::update()
80 lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
81 time_type const start_time = current_time();
83 DepList::iterator itr = deplist.begin();
84 while (itr != deplist.end()) {
85 dep_info &di = itr->second;
88 if (stat(itr->first.toFilesystemEncoding().c_str(), &f_info) == 0) {
89 if (di.mtime_cur == f_info.st_mtime) {
90 di.crc_prev = di.crc_cur;
91 lyxerr[Debug::DEPEND] << itr->first << " same mtime" << endl;
93 di.crc_prev = di.crc_cur;
94 lyxerr[Debug::DEPEND] << itr->first << " CRC... " << flush;
95 di.crc_cur = sum(itr->first);
96 lyxerr[Debug::DEPEND] << "done" << endl;
100 // remove stale files - if it's re-created, it
101 // will be re-inserted by deplog.
102 lyxerr[Debug::DEPEND] << itr->first
103 << " doesn't exist. removing from DepTable." << endl;
104 DepList::iterator doomed = itr++;
105 deplist.erase(doomed);
109 if (lyxerr.debugging(Debug::DEPEND)) {
116 time_type const time_sec = current_time() - start_time;
117 lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
118 << time_sec << " sec)." << endl;
122 bool DepTable::sumchange() const
124 DepList::const_iterator cit = deplist.begin();
125 DepList::const_iterator end = deplist.end();
126 for (; cit != end; ++cit) {
127 if (cit->second.changed()) return true;
133 bool DepTable::haschanged(FileName const & fil) const
135 DepList::const_iterator cit = deplist.find(fil);
136 if (cit != deplist.end()) {
137 if (cit->second.changed())
144 bool DepTable::extchanged(string const & ext) const
146 DepList::const_iterator cit = deplist.begin();
147 DepList::const_iterator end = deplist.end();
148 for (; cit != end; ++cit) {
149 if (suffixIs(cit->first.absFilename(), ext)) {
150 if (cit->second.changed())
158 bool DepTable::ext_exist(string const & ext) const
160 DepList::const_iterator cit = deplist.begin();
161 DepList::const_iterator end = deplist.end();
162 for (; cit != end; ++cit) {
163 if (suffixIs(cit->first.absFilename(), ext)) {
171 bool DepTable::exist(FileName const & fil) const
173 return deplist.find(fil) != deplist.end();
177 void DepTable::remove_files_with_extension(string const & suf)
179 DepList::iterator cit = deplist.begin();
180 DepList::iterator end = deplist.end();
182 if (suffixIs(cit->first.absFilename(), suf)) {
183 // Can't erase the current iterator, but we
184 // can increment and then erase.
185 // Deplist is a map so only the erased
186 // iterator is invalidated.
187 DepList::iterator doomed = cit++;
188 deplist.erase(doomed);
196 void DepTable::remove_file(FileName const & filename)
198 DepList::iterator cit = deplist.begin();
199 DepList::iterator end = deplist.end();
201 if (cit->first == filename) {
202 // Can't erase the current iterator, but we
203 // can increment and then erase.
204 // deplist is a map so only the erased
205 // iterator is invalidated.
206 DepList::iterator doomed = cit++;
207 deplist.erase(doomed);
215 void DepTable::write(FileName const & f) const
217 ofstream ofs(f.toFilesystemEncoding().c_str());
218 DepList::const_iterator cit = deplist.begin();
219 DepList::const_iterator end = deplist.end();
220 for (; cit != end; ++cit) {
221 if (lyxerr.debugging(Debug::DEPEND)) {
222 // Store the second (most recently calculated)
224 // The older one is effectively set to 0 upon re-load.
225 lyxerr << "Write dep: "
226 << cit->second.crc_cur << ' '
227 << cit->second.mtime_cur << ' '
228 << cit->first << endl;
230 ofs << cit->second.crc_cur << ' '
231 << cit->second.mtime_cur << ' '
232 << cit->first << endl;
237 bool DepTable::read(FileName const & f)
239 ifstream ifs(f.toFilesystemEncoding().c_str());
242 // This doesn't change through the loop.
245 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
247 if (lyxerr.debugging(Debug::DEPEND)) {
248 lyxerr << "Read dep: "
250 << di.mtime_cur << ' '
253 deplist[FileName(nome)] = di;
255 return deplist.size();