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>
42 bool DepTable::dep_info::changed() const
44 return crc_prev != crc_cur && crc_cur != 0;
48 void DepTable::insert(string const & fi, bool upd)
50 // not quite sure if this is the correct place for MakeAbsPath
51 string const f = MakeAbsPath(fi);
52 if (deplist.find(f) == deplist.end()) {
56 lyxerr[Debug::DEPEND] << " CRC..." << flush;
57 di.crc_cur = lyx::sum(f);
58 lyxerr[Debug::DEPEND] << "done." << endl;
60 stat(fi.c_str(), &f_info);
61 di.mtime_cur = f_info.st_mtime;
68 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
73 void DepTable::update()
75 lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
76 time_t start_time = time(0);
78 DepList::iterator itr = deplist.begin();
79 while (itr != deplist.end()) {
80 dep_info &di = itr->second;
83 if (stat(itr->first.c_str(), &f_info) == 0) {
84 if (di.mtime_cur == f_info.st_mtime) {
85 di.crc_prev = di.crc_cur;
86 lyxerr[Debug::DEPEND] << itr->first << " same mtime";
88 di.crc_prev = di.crc_cur;
89 lyxerr[Debug::DEPEND] << itr->first << " CRC... ";
90 di.crc_cur = lyx::sum(itr->first);
91 lyxerr[Debug::DEPEND] << "done";
95 // remove stale files - if it's re-created, it
96 // will be re-inserted by deplog.
97 lyxerr[Debug::DEPEND] << itr->first
98 << " doesn't exist. removing from DepTable." << endl;
99 DepList::iterator doomed = itr++;
100 deplist.erase(doomed);
104 if (lyxerr.debugging(Debug::DEPEND)) {
111 time_t time_sec = time(0) - start_time;
112 lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
113 << time_sec << " sec)." << endl;
117 bool DepTable::sumchange() const
119 DepList::const_iterator cit = deplist.begin();
120 DepList::const_iterator end = deplist.end();
121 for (; cit != end; ++cit) {
122 if (cit->second.changed()) return true;
128 bool DepTable::haschanged(string const & f) const
130 // not quite sure if this is the correct place for MakeAbsPath
131 string const fil = MakeAbsPath(f);
132 DepList::const_iterator cit = deplist.find(fil);
133 if (cit != deplist.end()) {
134 if (cit->second.changed())
141 bool DepTable::extchanged(string const & ext) const
143 DepList::const_iterator cit = deplist.begin();
144 DepList::const_iterator end = deplist.end();
145 for (; cit != end; ++cit) {
146 if (suffixIs(cit->first, ext)) {
147 if (cit->second.changed())
155 bool DepTable::ext_exist(string const & ext) const
157 DepList::const_iterator cit = deplist.begin();
158 DepList::const_iterator end = deplist.end();
159 for (; cit != end; ++cit) {
160 if (suffixIs(cit->first, ext)) {
168 bool DepTable::exist(string const & fil) const
170 return deplist.find(fil) != deplist.end();
174 void DepTable::remove_files_with_extension(string const & suf)
176 DepList::iterator cit = deplist.begin();
177 DepList::iterator end = deplist.end();
179 if (suffixIs(cit->first, suf)) {
180 // Can't erase the current iterator, but we
181 // can increment and then erase.
182 // Deplist is a map so only the erased
183 // iterator is invalidated.
184 DepList::iterator doomed = cit++;
185 deplist.erase(doomed);
193 void DepTable::remove_file(string const & filename)
195 DepList::iterator cit = deplist.begin();
196 DepList::iterator end = deplist.end();
198 if (OnlyFilename(cit->first) == filename) {
199 // Can't erase the current iterator, but we
200 // can increment and then erase.
201 // deplist is a map so only the erased
202 // iterator is invalidated.
203 DepList::iterator doomed = cit++;
204 deplist.erase(doomed);
212 void DepTable::write(string const & f) const
214 ofstream ofs(f.c_str());
215 DepList::const_iterator cit = deplist.begin();
216 DepList::const_iterator end = deplist.end();
217 for (; cit != end; ++cit) {
218 if (lyxerr.debugging(Debug::DEPEND)) {
219 // Store the second (most recently calculated)
221 // The older one is effectively set to 0 upon re-load.
222 lyxerr << "Write dep: "
224 << cit->second.crc_cur << " "
225 << cit->second.mtime_cur << endl;
227 ofs << cit->first << " "
228 << cit->second.crc_cur << " "
229 << cit->second.mtime_cur << endl;
234 void DepTable::read(string const & f)
236 ifstream ifs(f.c_str());
239 // This doesn't change through the loop.
242 while (ifs >> nome >> di.crc_cur >> di.mtime_cur) {
243 if (lyxerr.debugging(Debug::DEPEND)) {
244 lyxerr << "Read dep: "
247 << di.mtime_cur << endl;