]> git.lyx.org Git - lyx.git/blob - src/DepTable.C
typos
[lyx.git] / src / DepTable.C
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  */
12
13 #include <config.h>
14
15 #include "DepTable.h"
16
17 #include "debug.h"
18
19 #include "support/lyxlib.h"
20 #include "support/filetools.h"
21 #include "support/lstrings.h"
22 #include "support/lyxtime.h"
23
24 #include <sys/stat.h>
25
26 #include <fstream>
27
28
29 namespace lyx {
30
31 #ifndef CXX_GLOBAL_CSTD
32 using std::time;
33 #endif
34
35 using support::FileName;
36 using support::ltrim;
37 using support::onlyFilename;
38 using support::suffixIs;
39 using support::sum;
40
41 using std::endl;
42 using std::flush;
43 using std::getline;
44 using std::string;
45 using std::ofstream;
46 using std::ifstream;
47
48 inline
49 bool DepTable::dep_info::changed() const
50 {
51         return crc_prev != crc_cur && crc_cur != 0;
52 }
53
54
55 void DepTable::insert(FileName const & f, bool upd)
56 {
57         if (deplist.find(f) == deplist.end()) {
58                 dep_info di;
59                 di.crc_prev = 0;
60                 if (upd) {
61                         lyxerr[Debug::DEPEND] << " CRC..." << flush;
62                         di.crc_cur = sum(f);
63                         lyxerr[Debug::DEPEND] << "done." << endl;
64                         struct stat f_info;
65                         stat(f.toFilesystemEncoding().c_str(), &f_info);
66                         di.mtime_cur = f_info.st_mtime;
67                 } else {
68                         di.crc_cur = 0;
69                         di.mtime_cur = 0;
70                 }
71                 deplist[f] = di;
72         } else {
73                 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
74         }
75 }
76
77
78 void DepTable::update()
79 {
80         lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
81         time_type const start_time = current_time();
82
83         DepList::iterator itr = deplist.begin();
84         while (itr != deplist.end()) {
85                 dep_info &di = itr->second;
86
87                 struct stat f_info;
88                 if (stat(itr->first.toFilesystemEncoding().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" << endl;
92                         } else {
93                                 di.crc_prev = di.crc_cur;
94                                 lyxerr[Debug::DEPEND] << itr->first << " CRC... " << flush;
95                                 di.crc_cur = sum(itr->first);
96                                 lyxerr[Debug::DEPEND] << "done" << endl;
97                         }
98                 } else {
99                         // file doesn't exist
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);
106                         continue;
107                 }
108
109                 if (lyxerr.debugging(Debug::DEPEND)) {
110                         if (di.changed())
111                                 lyxerr << " +";
112                         lyxerr << endl;
113                 }
114                 ++itr;
115         }
116         time_type const time_sec = current_time() - start_time;
117         lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
118                 << time_sec << " sec)." << endl;
119 }
120
121
122 bool DepTable::sumchange() const
123 {
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;
128         }
129         return false;
130 }
131
132
133 bool DepTable::haschanged(FileName const & fil) const
134 {
135         DepList::const_iterator cit = deplist.find(fil);
136         if (cit != deplist.end()) {
137                 if (cit->second.changed())
138                         return true;
139         }
140         return false;
141 }
142
143
144 bool DepTable::extchanged(string const & ext) const
145 {
146         DepList::const_iterator cit = deplist.begin();
147         DepList::const_iterator end = deplist.end();
148         for (; cit != end; ++cit) {
149                 if (suffixIs(cit->first.absFilename(), ext)) {
150                         if (cit->second.changed())
151                                 return true;
152                 }
153         }
154         return false;
155 }
156
157
158 bool DepTable::ext_exist(string const & ext) const
159 {
160         DepList::const_iterator cit = deplist.begin();
161         DepList::const_iterator end = deplist.end();
162         for (; cit != end; ++cit) {
163                 if (suffixIs(cit->first.absFilename(), ext)) {
164                         return true;
165                 }
166         }
167         return false;
168 }
169
170
171 bool DepTable::exist(FileName const & fil) const
172 {
173         return deplist.find(fil) != deplist.end();
174 }
175
176
177 void DepTable::remove_files_with_extension(string const & suf)
178 {
179         DepList::iterator cit = deplist.begin();
180         DepList::iterator end = deplist.end();
181         while (cit != end) {
182                 if (suffixIs(cit->first.absFilename(), 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);
189                         continue;
190                 }
191                 ++cit;
192         }
193 }
194
195
196 void DepTable::remove_file(FileName const & filename)
197 {
198         DepList::iterator cit = deplist.begin();
199         DepList::iterator end = deplist.end();
200         while (cit != end) {
201                 if (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);
208                         continue;
209                 }
210                 ++cit;
211         }
212 }
213
214
215 void DepTable::write(FileName const & f) const
216 {
217         ofstream ofs(f.toFilesystemEncoding().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)
223                         // CRC value.
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;
229                 }
230                 ofs << cit->second.crc_cur << ' '
231                     << cit->second.mtime_cur << ' '
232                     << cit->first << endl;
233         }
234 }
235
236
237 bool DepTable::read(FileName const & f)
238 {
239         ifstream ifs(f.toFilesystemEncoding().c_str());
240         string nome;
241         dep_info di;
242         // This doesn't change through the loop.
243         di.crc_prev = 0;
244
245         while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
246                 nome = ltrim(nome);
247                 if (lyxerr.debugging(Debug::DEPEND)) {
248                         lyxerr << "Read dep: "
249                                << di.crc_cur << ' '
250                                << di.mtime_cur << ' '
251                                << nome << endl;
252                 }
253                 deplist[FileName(nome)] = di;
254         }
255         return deplist.size();
256 }
257
258
259 } // namespace lyx