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.
18 #include "support/lyxlib.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/lyxtime.h"
27 #ifndef CXX_GLOBAL_CSTD
31 using namespace lyx::support;
41 bool DepTable::dep_info::changed() const
43 return crc_prev != crc_cur && crc_cur != 0;
47 void DepTable::insert(string const & fi, bool upd)
49 // not quite sure if this is the correct place for MakeAbsPath
50 string const f = MakeAbsPath(fi);
51 if (deplist.find(f) == deplist.end()) {
55 lyxerr[Debug::DEPEND] << " CRC..." << flush;
57 lyxerr[Debug::DEPEND] << "done." << endl;
59 stat(fi.c_str(), &f_info);
60 di.mtime_cur = f_info.st_mtime;
67 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
72 void DepTable::update()
74 lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
75 lyx::time_type const start_time = lyx::current_time();
77 DepList::iterator itr = deplist.begin();
78 while (itr != deplist.end()) {
79 dep_info &di = itr->second;
82 if (stat(itr->first.c_str(), &f_info) == 0) {
83 if (di.mtime_cur == f_info.st_mtime) {
84 di.crc_prev = di.crc_cur;
85 lyxerr[Debug::DEPEND] << itr->first << " same mtime";
87 di.crc_prev = di.crc_cur;
88 lyxerr[Debug::DEPEND] << itr->first << " CRC... ";
89 di.crc_cur = sum(itr->first);
90 lyxerr[Debug::DEPEND] << "done";
94 // remove stale files - if it's re-created, it
95 // will be re-inserted by deplog.
96 lyxerr[Debug::DEPEND] << itr->first
97 << " doesn't exist. removing from DepTable." << endl;
98 DepList::iterator doomed = itr++;
99 deplist.erase(doomed);
103 if (lyxerr.debugging(Debug::DEPEND)) {
110 lyx::time_type const time_sec = lyx::current_time() - start_time;
111 lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
112 << time_sec << " sec)." << endl;
116 bool DepTable::sumchange() const
118 DepList::const_iterator cit = deplist.begin();
119 DepList::const_iterator end = deplist.end();
120 for (; cit != end; ++cit) {
121 if (cit->second.changed()) return true;
127 bool DepTable::haschanged(string const & f) const
129 // not quite sure if this is the correct place for MakeAbsPath
130 string const fil = MakeAbsPath(f);
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, 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, ext)) {
167 bool DepTable::exist(string 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, 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(string const & filename)
194 DepList::iterator cit = deplist.begin();
195 DepList::iterator end = deplist.end();
197 if (OnlyFilename(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(string const & f) const
213 ofstream ofs(f.c_str());
214 DepList::const_iterator cit = deplist.begin();
215 DepList::const_iterator end = deplist.end();
216 for (; cit != end; ++cit) {
217 if (lyxerr.debugging(Debug::DEPEND)) {
218 // Store the second (most recently calculated)
220 // The older one is effectively set to 0 upon re-load.
221 lyxerr << "Write dep: "
222 << cit->second.crc_cur << ' '
223 << cit->second.mtime_cur << ' '
224 << cit->first << endl;
226 ofs << cit->second.crc_cur << ' '
227 << cit->second.mtime_cur << ' '
228 << cit->first << endl;
233 bool DepTable::read(string const & f)
235 ifstream ifs(f.c_str());
238 // This doesn't change through the loop.
241 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
243 if (lyxerr.debugging(Debug::DEPEND)) {
244 lyxerr << "Read dep: "
246 << di.mtime_cur << ' '
251 return deplist.size();