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>
39 inline bool DepTable::dep_info::changed() const
41 return crc_prev != crc_cur && crc_cur != 0;
44 void DepTable::insert(string const & fi,
47 // not quite sure if this is the correct place for MakeAbsPath
48 string f = MakeAbsPath(fi);
49 if (deplist.find(f) == deplist.end()) {
53 lyxerr[Debug::DEPEND] << " CRC..." << std::flush;
54 di.crc_cur = lyx::sum(f);
55 lyxerr[Debug::DEPEND] << "done." << endl;
57 stat(fi.c_str(), &f_info);
58 di.mtime_cur = f_info.st_mtime;
65 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
70 void DepTable::update()
72 lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
73 time_t start_time = time(0);
75 DepList::iterator itr = deplist.begin();
76 while (itr != deplist.end()) {
77 dep_info &di = itr->second;
80 if (0 == stat(itr->first.c_str(), &f_info) ) {
81 if (di.mtime_cur == f_info.st_mtime) {
82 di.crc_prev = di.crc_cur;
83 lyxerr[Debug::DEPEND] << itr->first << " same mtime";
85 di.crc_prev = di.crc_cur;
86 lyxerr[Debug::DEPEND] << itr->first << " CRC... ";
87 di.crc_cur = lyx::sum(itr->first);
88 lyxerr[Debug::DEPEND] << "done";
92 // remove stale files - if it's re-created, it
93 // will be re-inserted by deplog.
94 lyxerr[Debug::DEPEND] << itr->first
95 << " doesn't exist. removing from DepTable." << endl;
96 DepList::iterator doomed = itr++;
97 deplist.erase(doomed);
101 if (lyxerr.debugging(Debug::DEPEND)) {
108 time_t time_sec = time(0) - start_time;
109 lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
110 << time_sec << " sec)." << endl;
114 bool DepTable::sumchange() const
116 for (DepList::const_iterator cit = deplist.begin();
117 cit != deplist.end();
119 if ((*cit).second.changed()) return true;
125 bool DepTable::haschanged(string const & f) const
127 // not quite sure if this is the correct place for MakeAbsPath
128 string fil = MakeAbsPath(f);
129 DepList::const_iterator cit = deplist.find(fil);
130 if (cit != deplist.end()) {
131 if (cit->second.changed())
138 bool DepTable::extchanged(string const & ext) const
140 for (DepList::const_iterator cit = deplist.begin();
141 cit != deplist.end();
143 if (suffixIs(cit->first, ext)) {
144 if (cit->second.changed())
152 bool DepTable::ext_exist(const string& ext ) const
154 for (DepList::const_iterator cit = deplist.begin();
155 cit != deplist.end(); ++cit ) {
157 if ( suffixIs(cit->first, ext) ) {
164 bool DepTable::exist(string const & fil) const
166 return deplist.find(fil) != deplist.end();
170 void DepTable::remove_files_with_extension(string const & suf)
172 DepList::iterator cit = deplist.begin();
173 while (cit != deplist.end()) {
174 if (suffixIs(cit->first, suf)) {
175 // Can't erase the current iterator, but we can increment and then erase.
176 // deplist is a map so only the erased iterator is invalidated.
177 DepList::iterator doomed = cit++;
178 deplist.erase(doomed);
186 void DepTable::remove_file(string const & filename)
188 DepList::iterator cit = deplist.begin();
189 while (cit != deplist.end()) {
190 if (OnlyFilename(cit->first) == filename) {
191 // Can't erase the current iterator, but we can increment and then erase.
192 // deplist is a map so only the erased iterator is invalidated.
193 DepList::iterator doomed = cit++;
194 deplist.erase(doomed);
202 void DepTable::write(string const & f) const
204 ofstream ofs(f.c_str());
205 for (DepList::const_iterator cit = deplist.begin();
206 cit != deplist.end(); ++cit) {
207 if (lyxerr.debugging(Debug::DEPEND)) {
208 // Store the second (most recently calculated) CRC value.
209 // The older one is effectively set to 0 upon re-load.
210 lyxerr << "Write dep: "
212 << cit->second.crc_cur << " "
213 << cit->second.mtime_cur << endl;
215 ofs << cit->first << " "
216 << cit->second.crc_cur << " "
217 << cit->second.mtime_cur << endl;
222 void DepTable::read(string const & f)
224 ifstream ifs(f.c_str());
227 // This doesn't change through the loop.
230 while (ifs >> nome >> di.crc_cur >> di.mtime_cur) {
231 if (lyxerr.debugging(Debug::DEPEND)) {
232 lyxerr << "Read dep: "
235 << di.mtime_cur << endl;