Consider the case where there is not any layout name.
[lyx.git] / src / DepTable.C
blob2718bcacf934fb4d4565f341fa376f4bcb95ebe4
1 /**
2  * \file DepTable.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Ben Stanley
9  *
10  * Full author contact details are available in file CREDITS.
11  */
13 #include <config.h>
15 #include "DepTable.h"
17 #include "debug.h"
19 #include "support/lyxlib.h"
20 #include "support/filetools.h"
21 #include "support/lstrings.h"
22 #include "support/lyxtime.h"
24 #include <sys/stat.h>
26 #include <fstream>
28 #ifndef CXX_GLOBAL_CSTD
29 using std::time;
30 #endif
32 using lyx::support::ltrim;
33 using lyx::support::MakeAbsPath;
34 using lyx::support::OnlyFilename;
35 using lyx::support::suffixIs;
36 using lyx::support::sum;
38 using std::endl;
39 using std::flush;
40 using std::getline;
41 using std::string;
42 using std::ofstream;
43 using std::ifstream;
45 inline
46 bool DepTable::dep_info::changed() const
48         return crc_prev != crc_cur && crc_cur != 0;
52 void DepTable::insert(string const & fi, bool upd)
54         // not quite sure if this is the correct place for MakeAbsPath
55         string const f = MakeAbsPath(fi);
56         if (deplist.find(f) == deplist.end()) {
57                 dep_info di;
58                 di.crc_prev = 0;
59                 if (upd) {
60                         lyxerr[Debug::DEPEND] << " CRC..." << flush;
61                         di.crc_cur = sum(f);
62                         lyxerr[Debug::DEPEND] << "done." << endl;
63                         struct stat f_info;
64                         stat(fi.c_str(), &f_info);
65                         di.mtime_cur = f_info.st_mtime;
66                 } else {
67                         di.crc_cur = 0;
68                         di.mtime_cur = 0;
69                 }
70                 deplist[f] = di;
71         } else {
72                 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
73         }
77 void DepTable::update()
79         lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
80         lyx::time_type const start_time = lyx::current_time();
82         DepList::iterator itr = deplist.begin();
83         while (itr != deplist.end()) {
84                 dep_info &di = itr->second;
86                 struct stat f_info;
87                 if (stat(itr->first.c_str(), &f_info) == 0) {
88                         if (di.mtime_cur == f_info.st_mtime) {
89                                 di.crc_prev = di.crc_cur;
90                                 lyxerr[Debug::DEPEND] << itr->first << " same mtime" << endl;
91                         } else {
92                                 di.crc_prev = di.crc_cur;
93                                 lyxerr[Debug::DEPEND] << itr->first << " CRC... " << flush;
94                                 di.crc_cur = sum(itr->first);
95                                 lyxerr[Debug::DEPEND] << "done" << endl;
96                         }
97                 } else {
98                         // file doesn't exist
99                         // remove stale files - if it's re-created, it
100                         // will be re-inserted by deplog.
101                         lyxerr[Debug::DEPEND] << itr->first
102                                 << " doesn't exist. removing from DepTable." << endl;
103                         DepList::iterator doomed = itr++;
104                         deplist.erase(doomed);
105                         continue;
106                 }
108                 if (lyxerr.debugging(Debug::DEPEND)) {
109                         if (di.changed())
110                                 lyxerr << " +";
111                         lyxerr << endl;
112                 }
113                 ++itr;
114         }
115         lyx::time_type const time_sec = lyx::current_time() - start_time;
116         lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
117                 << time_sec << " sec)." << endl;
121 bool DepTable::sumchange() const
123         DepList::const_iterator cit = deplist.begin();
124         DepList::const_iterator end = deplist.end();
125         for (; cit != end; ++cit) {
126                 if (cit->second.changed()) return true;
127         }
128         return false;
132 bool DepTable::haschanged(string const & f) const
134         // not quite sure if this is the correct place for MakeAbsPath
135         string const fil = MakeAbsPath(f);
136         DepList::const_iterator cit = deplist.find(fil);
137         if (cit != deplist.end()) {
138                 if (cit->second.changed())
139                         return true;
140         }
141         return false;
145 bool DepTable::extchanged(string const & ext) const
147         DepList::const_iterator cit = deplist.begin();
148         DepList::const_iterator end = deplist.end();
149         for (; cit != end; ++cit) {
150                 if (suffixIs(cit->first, ext)) {
151                         if (cit->second.changed())
152                                 return true;
153                 }
154         }
155         return false;
159 bool DepTable::ext_exist(string const & ext) const
161         DepList::const_iterator cit = deplist.begin();
162         DepList::const_iterator end = deplist.end();
163         for (; cit != end; ++cit) {
164                 if (suffixIs(cit->first, ext)) {
165                         return true;
166                 }
167         }
168         return false;
172 bool DepTable::exist(string const & fil) const
174         return deplist.find(fil) != deplist.end();
178 void DepTable::remove_files_with_extension(string const & suf)
180         DepList::iterator cit = deplist.begin();
181         DepList::iterator end = deplist.end();
182         while (cit != end) {
183                 if (suffixIs(cit->first, suf)) {
184                         // Can't erase the current iterator, but we
185                         // can increment and then erase.
186                         // Deplist is a map so only the erased
187                         // iterator is invalidated.
188                         DepList::iterator doomed = cit++;
189                         deplist.erase(doomed);
190                         continue;
191                 }
192                 ++cit;
193         }
197 void DepTable::remove_file(string const & filename)
199         DepList::iterator cit = deplist.begin();
200         DepList::iterator end = deplist.end();
201         while (cit != end) {
202                 if (OnlyFilename(cit->first) == filename) {
203                         // Can't erase the current iterator, but we
204                         // can increment and then erase.
205                         // deplist is a map so only the erased
206                         // iterator is invalidated.
207                         DepList::iterator doomed = cit++;
208                         deplist.erase(doomed);
209                         continue;
210                 }
211                 ++cit;
212         }
216 void DepTable::write(string const & f) const
218         ofstream ofs(f.c_str());
219         DepList::const_iterator cit = deplist.begin();
220         DepList::const_iterator end = deplist.end();
221         for (; cit != end; ++cit) {
222                 if (lyxerr.debugging(Debug::DEPEND)) {
223                         // Store the second (most recently calculated)
224                         // CRC value.
225                         // The older one is effectively set to 0 upon re-load.
226                         lyxerr << "Write dep: "
227                                << cit->second.crc_cur << ' '
228                                << cit->second.mtime_cur << ' '
229                                << cit->first << endl;
230                 }
231                 ofs << cit->second.crc_cur << ' '
232                     << cit->second.mtime_cur << ' '
233                     << cit->first << endl;
234         }
238 bool DepTable::read(string const & f)
240         ifstream ifs(f.c_str());
241         string nome;
242         dep_info di;
243         // This doesn't change through the loop.
244         di.crc_prev = 0;
246         while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
247                 nome = ltrim(nome);
248                 if (lyxerr.debugging(Debug::DEPEND)) {
249                         lyxerr << "Read dep: "
250                                << di.crc_cur << ' '
251                                << di.mtime_cur << ' '
252                                << nome << endl;
253                 }
254                 deplist[nome] = di;
255         }
256         return deplist.size();