1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
5 * Copyright 1995 Matthias Ettrich
6 * Copyright 1995-2001 The LyX Team.
8 * This file is Copyright 1996-2001
12 * ======================================================
18 #pragma implementation
24 #include "support/lyxlib.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
28 #include <sys/types.h>
43 bool DepTable::dep_info::changed() const
45 return crc_prev != crc_cur && crc_cur != 0;
49 void DepTable::insert(string const & fi, bool upd)
51 // not quite sure if this is the correct place for MakeAbsPath
52 string const f = MakeAbsPath(fi);
53 if (deplist.find(f) == deplist.end()) {
57 lyxerr[Debug::DEPEND] << " CRC..." << flush;
58 di.crc_cur = lyx::sum(f);
59 lyxerr[Debug::DEPEND] << "done." << endl;
61 stat(fi.c_str(), &f_info);
62 di.mtime_cur = f_info.st_mtime;
69 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
74 void DepTable::update()
76 lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
77 time_t start_time = time(0);
79 DepList::iterator itr = deplist.begin();
80 while (itr != deplist.end()) {
81 dep_info &di = itr->second;
84 if (stat(itr->first.c_str(), &f_info) == 0) {
85 if (di.mtime_cur == f_info.st_mtime) {
86 di.crc_prev = di.crc_cur;
87 lyxerr[Debug::DEPEND] << itr->first << " same mtime";
89 di.crc_prev = di.crc_cur;
90 lyxerr[Debug::DEPEND] << itr->first << " CRC... ";
91 di.crc_cur = lyx::sum(itr->first);
92 lyxerr[Debug::DEPEND] << "done";
96 // remove stale files - if it's re-created, it
97 // will be re-inserted by deplog.
98 lyxerr[Debug::DEPEND] << itr->first
99 << " doesn't exist. removing from DepTable." << endl;
100 DepList::iterator doomed = itr++;
101 deplist.erase(doomed);
105 if (lyxerr.debugging(Debug::DEPEND)) {
112 time_t time_sec = time(0) - start_time;
113 lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
114 << time_sec << " sec)." << endl;
118 bool DepTable::sumchange() const
120 DepList::const_iterator cit = deplist.begin();
121 DepList::const_iterator end = deplist.end();
122 for (; cit != end; ++cit) {
123 if (cit->second.changed()) return true;
129 bool DepTable::haschanged(string const & f) const
131 // not quite sure if this is the correct place for MakeAbsPath
132 string const fil = MakeAbsPath(f);
133 DepList::const_iterator cit = deplist.find(fil);
134 if (cit != deplist.end()) {
135 if (cit->second.changed())
142 bool DepTable::extchanged(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, ext)) {
148 if (cit->second.changed())
156 bool DepTable::ext_exist(string const & ext) const
158 DepList::const_iterator cit = deplist.begin();
159 DepList::const_iterator end = deplist.end();
160 for (; cit != end; ++cit) {
161 if (suffixIs(cit->first, ext)) {
169 bool DepTable::exist(string const & fil) const
171 return deplist.find(fil) != deplist.end();
175 void DepTable::remove_files_with_extension(string const & suf)
177 DepList::iterator cit = deplist.begin();
178 DepList::iterator end = deplist.end();
180 if (suffixIs(cit->first, suf)) {
181 // Can't erase the current iterator, but we
182 // can increment and then erase.
183 // Deplist is a map so only the erased
184 // iterator is invalidated.
185 DepList::iterator doomed = cit++;
186 deplist.erase(doomed);
194 void DepTable::remove_file(string const & filename)
196 DepList::iterator cit = deplist.begin();
197 DepList::iterator end = deplist.end();
199 if (OnlyFilename(cit->first) == filename) {
200 // Can't erase the current iterator, but we
201 // can increment and then erase.
202 // deplist is a map so only the erased
203 // iterator is invalidated.
204 DepList::iterator doomed = cit++;
205 deplist.erase(doomed);
213 void DepTable::write(string const & f) const
215 ofstream ofs(f.c_str());
216 DepList::const_iterator cit = deplist.begin();
217 DepList::const_iterator end = deplist.end();
218 for (; cit != end; ++cit) {
219 if (lyxerr.debugging(Debug::DEPEND)) {
220 // Store the second (most recently calculated)
222 // The older one is effectively set to 0 upon re-load.
223 lyxerr << "Write dep: "
225 << cit->second.crc_cur << " "
226 << cit->second.mtime_cur << endl;
228 ofs << cit->first << " "
229 << cit->second.crc_cur << " "
230 << cit->second.mtime_cur << endl;
235 void DepTable::read(string const & f)
237 ifstream ifs(f.c_str());
240 // This doesn't change through the loop.
243 while (ifs >> nome >> di.crc_cur >> di.mtime_cur) {
244 if (lyxerr.debugging(Debug::DEPEND)) {
245 lyxerr << "Read dep: "
248 << di.mtime_cur << endl;