Not so soon, I guess, since that FIXME was from r6305.
[lyx.git] / src / DepTable.cpp
blobeb2e556d007b705bb1369010ac48aafaee5e952a
1 /**
2 * \file DepTable.cpp
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
8 * \author Ben Stanley
10 * Full author contact details are available in file CREDITS.
13 #include <config.h>
15 #include "DepTable.h"
17 #include "support/debug.h"
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
20 #include "support/lyxtime.h"
22 #include <sys/stat.h>
24 #include <fstream>
26 using namespace std;
27 using namespace lyx::support;
29 namespace lyx {
32 inline
33 bool DepTable::dep_info::changed() const
35 return crc_prev != crc_cur && crc_cur != 0;
39 void DepTable::insert(FileName const & f, bool upd)
41 if (deplist.find(f) == deplist.end()) {
42 dep_info di;
43 di.crc_prev = 0;
44 if (upd) {
45 LYXERR(Debug::DEPEND, " CRC...");
46 di.crc_cur = f.checksum();
47 LYXERR(Debug::DEPEND, "done.");
48 struct stat f_info;
49 stat(f.toFilesystemEncoding().c_str(), &f_info);
50 di.mtime_cur = long(f_info.st_mtime);
51 } else {
52 di.crc_cur = 0;
53 di.mtime_cur = 0;
55 deplist[f] = di;
56 } else {
57 LYXERR(Debug::DEPEND, " Already in DepTable");
62 void DepTable::update()
64 LYXERR(Debug::DEPEND, "Updating DepTable...");
65 time_t const start_time = current_time();
67 DepList::iterator itr = deplist.begin();
68 while (itr != deplist.end()) {
69 dep_info &di = itr->second;
71 struct stat f_info;
72 if (stat(itr->first.toFilesystemEncoding().c_str(), &f_info) == 0) {
73 if (di.mtime_cur == f_info.st_mtime) {
74 di.crc_prev = di.crc_cur;
75 LYXERR(Debug::DEPEND, itr->first << " same mtime");
76 } else {
77 di.crc_prev = di.crc_cur;
78 LYXERR(Debug::DEPEND, itr->first << " CRC... ");
79 di.crc_cur = itr->first.checksum();
80 LYXERR(Debug::DEPEND, "done");
82 } else {
83 // file doesn't exist
84 // remove stale files - if it's re-created, it
85 // will be re-inserted by deplog.
86 LYXERR(Debug::DEPEND, itr->first
87 << " doesn't exist. removing from DepTable.");
88 DepList::iterator doomed = itr++;
89 deplist.erase(doomed);
90 continue;
93 if (lyxerr.debugging(Debug::DEPEND)) {
94 if (di.changed())
95 lyxerr << " +";
96 lyxerr << endl;
98 ++itr;
100 time_t const time_sec = current_time() - start_time;
101 LYXERR(Debug::DEPEND, "Finished updating DepTable ("
102 << long(time_sec) << " sec).");
106 bool DepTable::sumchange() const
108 DepList::const_iterator cit = deplist.begin();
109 DepList::const_iterator end = deplist.end();
110 for (; cit != end; ++cit) {
111 if (cit->second.changed()) return true;
113 return false;
117 bool DepTable::haschanged(FileName const & fil) const
119 DepList::const_iterator cit = deplist.find(fil);
120 if (cit != deplist.end()) {
121 if (cit->second.changed())
122 return true;
124 return false;
128 bool DepTable::extchanged(string const & ext) const
130 DepList::const_iterator cit = deplist.begin();
131 DepList::const_iterator end = deplist.end();
132 for (; cit != end; ++cit) {
133 if (suffixIs(cit->first.absFilename(), ext)) {
134 if (cit->second.changed())
135 return true;
138 return false;
142 bool DepTable::ext_exist(string const & ext) const
144 DepList::const_iterator cit = deplist.begin();
145 DepList::const_iterator end = deplist.end();
146 for (; cit != end; ++cit) {
147 if (suffixIs(cit->first.absFilename(), ext)) {
148 return true;
151 return false;
155 bool DepTable::exist(FileName const & fil) const
157 return deplist.find(fil) != deplist.end();
161 void DepTable::remove_files_with_extension(string const & suf)
163 DepList::iterator cit = deplist.begin();
164 DepList::iterator end = deplist.end();
165 while (cit != end) {
166 if (suffixIs(cit->first.absFilename(), suf)) {
167 // Can't erase the current iterator, but we
168 // can increment and then erase.
169 // Deplist is a map so only the erased
170 // iterator is invalidated.
171 DepList::iterator doomed = cit++;
172 deplist.erase(doomed);
173 continue;
175 ++cit;
180 void DepTable::remove_file(FileName const & filename)
182 DepList::iterator cit = deplist.begin();
183 DepList::iterator end = deplist.end();
184 while (cit != end) {
185 if (cit->first == filename) {
186 // Can't erase the current iterator, but we
187 // can increment and then erase.
188 // deplist is a map so only the erased
189 // iterator is invalidated.
190 DepList::iterator doomed = cit++;
191 deplist.erase(doomed);
192 continue;
194 ++cit;
199 void DepTable::write(FileName const & f) const
201 ofstream ofs(f.toFilesystemEncoding().c_str());
202 DepList::const_iterator cit = deplist.begin();
203 DepList::const_iterator end = deplist.end();
204 for (; cit != end; ++cit) {
205 // Store the second (most recently calculated)
206 // CRC value.
207 // The older one is effectively set to 0 upon re-load.
208 LYXERR(Debug::DEPEND, "Write dep: "
209 << cit->second.crc_cur << ' '
210 << cit->second.mtime_cur << ' '
211 << cit->first);
213 ofs << cit->second.crc_cur << ' '
214 << cit->second.mtime_cur << ' '
215 << cit->first << endl;
220 bool DepTable::read(FileName const & f)
222 ifstream ifs(f.toFilesystemEncoding().c_str());
223 string nome;
224 dep_info di;
225 // This doesn't change through the loop.
226 di.crc_prev = 0;
228 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
229 nome = ltrim(nome);
231 LYXERR(Debug::DEPEND, "Read dep: "
232 << di.crc_cur << ' ' << di.mtime_cur << ' ' << nome);
234 deplist[FileName(nome)] = di;
236 return deplist.size();
240 } // namespace lyx