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>
35 #ifndef CXX_GLOBAL_CSTD
47 bool DepTable::dep_info::changed() const
49 return crc_prev != crc_cur && crc_cur != 0;
53 void DepTable::insert(string const & fi, bool upd)
55 // not quite sure if this is the correct place for MakeAbsPath
56 string const f = MakeAbsPath(fi);
57 if (deplist.find(f) == deplist.end()) {
61 lyxerr[Debug::DEPEND] << " CRC..." << flush;
62 di.crc_cur = lyx::sum(f);
63 lyxerr[Debug::DEPEND] << "done." << endl;
65 stat(fi.c_str(), &f_info);
66 di.mtime_cur = f_info.st_mtime;
73 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
78 void DepTable::update()
80 lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
81 time_t start_time = time(0);
83 DepList::iterator itr = deplist.begin();
84 while (itr != deplist.end()) {
85 dep_info &di = itr->second;
88 if (stat(itr->first.c_str(), &f_info) == 0) {
89 if (di.mtime_cur == f_info.st_mtime) {
90 di.crc_prev = di.crc_cur;
91 lyxerr[Debug::DEPEND] << itr->first << " same mtime";
93 di.crc_prev = di.crc_cur;
94 lyxerr[Debug::DEPEND] << itr->first << " CRC... ";
95 di.crc_cur = lyx::sum(itr->first);
96 lyxerr[Debug::DEPEND] << "done";
100 // remove stale files - if it's re-created, it
101 // will be re-inserted by deplog.
102 lyxerr[Debug::DEPEND] << itr->first
103 << " doesn't exist. removing from DepTable." << endl;
104 DepList::iterator doomed = itr++;
105 deplist.erase(doomed);
109 if (lyxerr.debugging(Debug::DEPEND)) {
116 time_t time_sec = time(0) - start_time;
117 lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
118 << time_sec << " sec)." << endl;
122 bool DepTable::sumchange() const
124 DepList::const_iterator cit = deplist.begin();
125 DepList::const_iterator end = deplist.end();
126 for (; cit != end; ++cit) {
127 if (cit->second.changed()) return true;
133 bool DepTable::haschanged(string const & f) const
135 // not quite sure if this is the correct place for MakeAbsPath
136 string const fil = MakeAbsPath(f);
137 DepList::const_iterator cit = deplist.find(fil);
138 if (cit != deplist.end()) {
139 if (cit->second.changed())
146 bool DepTable::extchanged(string const & ext) const
148 DepList::const_iterator cit = deplist.begin();
149 DepList::const_iterator end = deplist.end();
150 for (; cit != end; ++cit) {
151 if (suffixIs(cit->first, ext)) {
152 if (cit->second.changed())
160 bool DepTable::ext_exist(string const & ext) const
162 DepList::const_iterator cit = deplist.begin();
163 DepList::const_iterator end = deplist.end();
164 for (; cit != end; ++cit) {
165 if (suffixIs(cit->first, ext)) {
173 bool DepTable::exist(string const & fil) const
175 return deplist.find(fil) != deplist.end();
179 void DepTable::remove_files_with_extension(string const & suf)
181 DepList::iterator cit = deplist.begin();
182 DepList::iterator end = deplist.end();
184 if (suffixIs(cit->first, suf)) {
185 // Can't erase the current iterator, but we
186 // can increment and then erase.
187 // Deplist is a map so only the erased
188 // iterator is invalidated.
189 DepList::iterator doomed = cit++;
190 deplist.erase(doomed);
198 void DepTable::remove_file(string const & filename)
200 DepList::iterator cit = deplist.begin();
201 DepList::iterator end = deplist.end();
203 if (OnlyFilename(cit->first) == filename) {
204 // Can't erase the current iterator, but we
205 // can increment and then erase.
206 // deplist is a map so only the erased
207 // iterator is invalidated.
208 DepList::iterator doomed = cit++;
209 deplist.erase(doomed);
217 void DepTable::write(string const & f) const
219 ofstream ofs(f.c_str());
220 DepList::const_iterator cit = deplist.begin();
221 DepList::const_iterator end = deplist.end();
222 for (; cit != end; ++cit) {
223 if (lyxerr.debugging(Debug::DEPEND)) {
224 // Store the second (most recently calculated)
226 // The older one is effectively set to 0 upon re-load.
227 lyxerr << "Write dep: "
229 << cit->second.crc_cur << ' '
230 << cit->second.mtime_cur << endl;
232 ofs << cit->first << ' '
233 << cit->second.crc_cur << ' '
234 << cit->second.mtime_cur << endl;
239 void DepTable::read(string const & f)
241 ifstream ifs(f.c_str());
244 // This doesn't change through the loop.
247 while (ifs >> nome >> di.crc_cur >> di.mtime_cur) {
248 if (lyxerr.debugging(Debug::DEPEND)) {
249 lyxerr << "Read dep: "
252 << di.mtime_cur << endl;