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
32 using lyx::support::ltrim;
33 using lyx::support::MakeAbsPath;
34 using lyx::support::OnlyFilename;
35 using lyx::support::suffixIs;
36 using lyx::support::sum;
46 bool DepTable::dep_info::changed() const
48 return crc_prev != crc_cur && crc_cur != 0;
52 void DepTable::insert(string const & fi, bool upd)
54 // not quite sure if this is the correct place for MakeAbsPath
55 string const f = MakeAbsPath(fi);
56 if (deplist.find(f) == deplist.end()) {
60 lyxerr[Debug::DEPEND] << " CRC..." << flush;
62 lyxerr[Debug::DEPEND] << "done." << endl;
64 stat(fi.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 lyx::time_type const start_time = lyx::current_time();
82 DepList::iterator itr = deplist.begin();
83 while (itr != deplist.end()) {
84 dep_info &di = itr->second;
87 if (stat(itr->first.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 lyx::time_type const time_sec = lyx::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(string const & f) const
134 // not quite sure if this is the correct place for MakeAbsPath
135 string const fil = MakeAbsPath(f);
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, 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, ext)) {
172 bool DepTable::exist(string 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, 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(string const & filename)
199 DepList::iterator cit = deplist.begin();
200 DepList::iterator end = deplist.end();
202 if (OnlyFilename(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(string const & f) const
218 ofstream ofs(f.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(string const & f)
240 ifstream ifs(f.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 << ' '
256 return deplist.size();