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 * ======================================================
20 #include "support/lyxlib.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "support/lyxtime.h"
25 #include <sys/types.h>
32 #ifndef CXX_GLOBAL_CSTD
45 bool DepTable::dep_info::changed() const
47 return crc_prev != crc_cur && crc_cur != 0;
51 void DepTable::insert(string const & fi, bool upd)
53 // not quite sure if this is the correct place for MakeAbsPath
54 string const f = MakeAbsPath(fi);
55 if (deplist.find(f) == deplist.end()) {
59 lyxerr[Debug::DEPEND] << " CRC..." << flush;
60 di.crc_cur = lyx::sum(f);
61 lyxerr[Debug::DEPEND] << "done." << endl;
63 stat(fi.c_str(), &f_info);
64 di.mtime_cur = f_info.st_mtime;
71 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
76 void DepTable::update()
78 lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
79 lyx::time_type const start_time = lyx::current_time();
81 DepList::iterator itr = deplist.begin();
82 while (itr != deplist.end()) {
83 dep_info &di = itr->second;
86 if (stat(itr->first.c_str(), &f_info) == 0) {
87 if (di.mtime_cur == f_info.st_mtime) {
88 di.crc_prev = di.crc_cur;
89 lyxerr[Debug::DEPEND] << itr->first << " same mtime";
91 di.crc_prev = di.crc_cur;
92 lyxerr[Debug::DEPEND] << itr->first << " CRC... ";
93 di.crc_cur = lyx::sum(itr->first);
94 lyxerr[Debug::DEPEND] << "done";
98 // remove stale files - if it's re-created, it
99 // will be re-inserted by deplog.
100 lyxerr[Debug::DEPEND] << itr->first
101 << " doesn't exist. removing from DepTable." << endl;
102 DepList::iterator doomed = itr++;
103 deplist.erase(doomed);
107 if (lyxerr.debugging(Debug::DEPEND)) {
114 lyx::time_type const time_sec = lyx::current_time() - start_time;
115 lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
116 << time_sec << " sec)." << endl;
120 bool DepTable::sumchange() const
122 DepList::const_iterator cit = deplist.begin();
123 DepList::const_iterator end = deplist.end();
124 for (; cit != end; ++cit) {
125 if (cit->second.changed()) return true;
131 bool DepTable::haschanged(string const & f) const
133 // not quite sure if this is the correct place for MakeAbsPath
134 string const fil = MakeAbsPath(f);
135 DepList::const_iterator cit = deplist.find(fil);
136 if (cit != deplist.end()) {
137 if (cit->second.changed())
144 bool DepTable::extchanged(string const & ext) const
146 DepList::const_iterator cit = deplist.begin();
147 DepList::const_iterator end = deplist.end();
148 for (; cit != end; ++cit) {
149 if (suffixIs(cit->first, ext)) {
150 if (cit->second.changed())
158 bool DepTable::ext_exist(string const & ext) const
160 DepList::const_iterator cit = deplist.begin();
161 DepList::const_iterator end = deplist.end();
162 for (; cit != end; ++cit) {
163 if (suffixIs(cit->first, ext)) {
171 bool DepTable::exist(string const & fil) const
173 return deplist.find(fil) != deplist.end();
177 void DepTable::remove_files_with_extension(string const & suf)
179 DepList::iterator cit = deplist.begin();
180 DepList::iterator end = deplist.end();
182 if (suffixIs(cit->first, suf)) {
183 // Can't erase the current iterator, but we
184 // can increment and then erase.
185 // Deplist is a map so only the erased
186 // iterator is invalidated.
187 DepList::iterator doomed = cit++;
188 deplist.erase(doomed);
196 void DepTable::remove_file(string const & filename)
198 DepList::iterator cit = deplist.begin();
199 DepList::iterator end = deplist.end();
201 if (OnlyFilename(cit->first) == filename) {
202 // Can't erase the current iterator, but we
203 // can increment and then erase.
204 // deplist is a map so only the erased
205 // iterator is invalidated.
206 DepList::iterator doomed = cit++;
207 deplist.erase(doomed);
215 void DepTable::write(string const & f) const
217 ofstream ofs(f.c_str());
218 DepList::const_iterator cit = deplist.begin();
219 DepList::const_iterator end = deplist.end();
220 for (; cit != end; ++cit) {
221 if (lyxerr.debugging(Debug::DEPEND)) {
222 // Store the second (most recently calculated)
224 // The older one is effectively set to 0 upon re-load.
225 lyxerr << "Write dep: "
226 << cit->second.crc_cur << ' '
227 << cit->second.mtime_cur << ' '
228 << cit->first << endl;
230 ofs << cit->second.crc_cur << ' '
231 << cit->second.mtime_cur << ' '
232 << cit->first << endl;
237 bool DepTable::read(string const & f)
239 ifstream ifs(f.c_str());
242 // This doesn't change through the loop.
245 while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
247 if (lyxerr.debugging(Debug::DEPEND)) {
248 lyxerr << "Read dep: "
250 << di.mtime_cur << ' '
255 return deplist.size();