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
48 bool DepTable::dep_info::changed() const
50 return crc_prev != crc_cur && crc_cur != 0;
54 void DepTable::insert(string const & fi, bool upd)
56 // not quite sure if this is the correct place for MakeAbsPath
57 string const f = MakeAbsPath(fi);
58 if (deplist.find(f) == deplist.end()) {
62 lyxerr[Debug::DEPEND] << " CRC..." << flush;
63 di.crc_cur = lyx::sum(f);
64 lyxerr[Debug::DEPEND] << "done." << endl;
66 stat(fi.c_str(), &f_info);
67 di.mtime_cur = f_info.st_mtime;
74 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
79 void DepTable::update()
81 lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
82 time_t start_time = time(0);
84 DepList::iterator itr = deplist.begin();
85 while (itr != deplist.end()) {
86 dep_info &di = itr->second;
89 if (stat(itr->first.c_str(), &f_info) == 0) {
90 if (di.mtime_cur == f_info.st_mtime) {
91 di.crc_prev = di.crc_cur;
92 lyxerr[Debug::DEPEND] << itr->first << " same mtime";
94 di.crc_prev = di.crc_cur;
95 lyxerr[Debug::DEPEND] << itr->first << " CRC... ";
96 di.crc_cur = lyx::sum(itr->first);
97 lyxerr[Debug::DEPEND] << "done";
100 // file doesn't exist
101 // remove stale files - if it's re-created, it
102 // will be re-inserted by deplog.
103 lyxerr[Debug::DEPEND] << itr->first
104 << " doesn't exist. removing from DepTable." << endl;
105 DepList::iterator doomed = itr++;
106 deplist.erase(doomed);
110 if (lyxerr.debugging(Debug::DEPEND)) {
117 time_t time_sec = time(0) - start_time;
118 lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
119 << time_sec << " sec)." << endl;
123 bool DepTable::sumchange() const
125 DepList::const_iterator cit = deplist.begin();
126 DepList::const_iterator end = deplist.end();
127 for (; cit != end; ++cit) {
128 if (cit->second.changed()) return true;
134 bool DepTable::haschanged(string const & f) const
136 // not quite sure if this is the correct place for MakeAbsPath
137 string const fil = MakeAbsPath(f);
138 DepList::const_iterator cit = deplist.find(fil);
139 if (cit != deplist.end()) {
140 if (cit->second.changed())
147 bool DepTable::extchanged(string const & ext) const
149 DepList::const_iterator cit = deplist.begin();
150 DepList::const_iterator end = deplist.end();
151 for (; cit != end; ++cit) {
152 if (suffixIs(cit->first, ext)) {
153 if (cit->second.changed())
161 bool DepTable::ext_exist(string const & ext) const
163 DepList::const_iterator cit = deplist.begin();
164 DepList::const_iterator end = deplist.end();
165 for (; cit != end; ++cit) {
166 if (suffixIs(cit->first, ext)) {
174 bool DepTable::exist(string const & fil) const
176 return deplist.find(fil) != deplist.end();
180 void DepTable::remove_files_with_extension(string const & suf)
182 DepList::iterator cit = deplist.begin();
183 DepList::iterator end = deplist.end();
185 if (suffixIs(cit->first, suf)) {
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);
199 void DepTable::remove_file(string const & filename)
201 DepList::iterator cit = deplist.begin();
202 DepList::iterator end = deplist.end();
204 if (OnlyFilename(cit->first) == filename) {
205 // Can't erase the current iterator, but we
206 // can increment and then erase.
207 // deplist is a map so only the erased
208 // iterator is invalidated.
209 DepList::iterator doomed = cit++;
210 deplist.erase(doomed);
218 void DepTable::write(string const & f) const
220 ofstream ofs(f.c_str());
221 DepList::const_iterator cit = deplist.begin();
222 DepList::const_iterator end = deplist.end();
223 for (; cit != end; ++cit) {
224 if (lyxerr.debugging(Debug::DEPEND)) {
225 // Store the second (most recently calculated)
227 // The older one is effectively set to 0 upon re-load.
228 lyxerr << "Write dep: "
229 << cit->second.crc_cur << ' '
230 << cit->second.mtime_cur << ' '
231 << cit->first << endl;
233 ofs << cit->second.crc_cur << ' '
234 << cit->second.mtime_cur << ' '
235 << cit->first << endl;
240 void DepTable::read(string const & f)
242 ifstream ifs(f.c_str());
245 // This doesn't change through the loop.
248 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
250 if (lyxerr.debugging(Debug::DEPEND)) {
251 lyxerr << "Read dep: "
253 << di.mtime_cur << ' '