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
36 using support::makeAbsPath;
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(string const & fi, bool upd)
57 // not quite sure if this is the correct place for MakeAbsPath
58 string const f = makeAbsPath(fi);
59 if (deplist.find(f) == deplist.end()) {
63 lyxerr[Debug::DEPEND] << " CRC..." << flush;
65 lyxerr[Debug::DEPEND] << "done." << endl;
67 stat(fi.c_str(), &f_info);
68 di.mtime_cur = f_info.st_mtime;
75 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
80 void DepTable::update()
82 lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
83 time_type const start_time = current_time();
85 DepList::iterator itr = deplist.begin();
86 while (itr != deplist.end()) {
87 dep_info &di = itr->second;
90 if (stat(itr->first.c_str(), &f_info) == 0) {
91 if (di.mtime_cur == f_info.st_mtime) {
92 di.crc_prev = di.crc_cur;
93 lyxerr[Debug::DEPEND] << itr->first << " same mtime" << endl;
95 di.crc_prev = di.crc_cur;
96 lyxerr[Debug::DEPEND] << itr->first << " CRC... " << flush;
97 di.crc_cur = sum(itr->first);
98 lyxerr[Debug::DEPEND] << "done" << endl;
101 // file doesn't exist
102 // remove stale files - if it's re-created, it
103 // will be re-inserted by deplog.
104 lyxerr[Debug::DEPEND] << itr->first
105 << " doesn't exist. removing from DepTable." << endl;
106 DepList::iterator doomed = itr++;
107 deplist.erase(doomed);
111 if (lyxerr.debugging(Debug::DEPEND)) {
118 time_type const time_sec = current_time() - start_time;
119 lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
120 << time_sec << " sec)." << endl;
124 bool DepTable::sumchange() const
126 DepList::const_iterator cit = deplist.begin();
127 DepList::const_iterator end = deplist.end();
128 for (; cit != end; ++cit) {
129 if (cit->second.changed()) return true;
135 bool DepTable::haschanged(string const & f) const
137 // not quite sure if this is the correct place for MakeAbsPath
138 string const fil = makeAbsPath(f);
139 DepList::const_iterator cit = deplist.find(fil);
140 if (cit != deplist.end()) {
141 if (cit->second.changed())
148 bool DepTable::extchanged(string const & ext) const
150 DepList::const_iterator cit = deplist.begin();
151 DepList::const_iterator end = deplist.end();
152 for (; cit != end; ++cit) {
153 if (suffixIs(cit->first, ext)) {
154 if (cit->second.changed())
162 bool DepTable::ext_exist(string const & ext) const
164 DepList::const_iterator cit = deplist.begin();
165 DepList::const_iterator end = deplist.end();
166 for (; cit != end; ++cit) {
167 if (suffixIs(cit->first, ext)) {
175 bool DepTable::exist(string const & fil) const
177 return deplist.find(fil) != deplist.end();
181 void DepTable::remove_files_with_extension(string const & suf)
183 DepList::iterator cit = deplist.begin();
184 DepList::iterator end = deplist.end();
186 if (suffixIs(cit->first, suf)) {
187 // Can't erase the current iterator, but we
188 // can increment and then erase.
189 // Deplist is a map so only the erased
190 // iterator is invalidated.
191 DepList::iterator doomed = cit++;
192 deplist.erase(doomed);
200 void DepTable::remove_file(string const & filename)
202 DepList::iterator cit = deplist.begin();
203 DepList::iterator end = deplist.end();
205 if (onlyFilename(cit->first) == filename) {
206 // Can't erase the current iterator, but we
207 // can increment and then erase.
208 // deplist is a map so only the erased
209 // iterator is invalidated.
210 DepList::iterator doomed = cit++;
211 deplist.erase(doomed);
219 void DepTable::write(string const & f) const
221 ofstream ofs(f.c_str());
222 DepList::const_iterator cit = deplist.begin();
223 DepList::const_iterator end = deplist.end();
224 for (; cit != end; ++cit) {
225 if (lyxerr.debugging(Debug::DEPEND)) {
226 // Store the second (most recently calculated)
228 // The older one is effectively set to 0 upon re-load.
229 lyxerr << "Write dep: "
230 << cit->second.crc_cur << ' '
231 << cit->second.mtime_cur << ' '
232 << cit->first << endl;
234 ofs << cit->second.crc_cur << ' '
235 << cit->second.mtime_cur << ' '
236 << cit->first << endl;
241 bool DepTable::read(string const & f)
243 ifstream ifs(f.c_str());
246 // This doesn't change through the loop.
249 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
251 if (lyxerr.debugging(Debug::DEPEND)) {
252 lyxerr << "Read dep: "
254 << di.mtime_cur << ' '
259 return deplist.size();