]> git.lyx.org Git - lyx.git/blob - src/DepTable.cpp
Fixed some lines that were too long. It compiled afterwards.
[lyx.git] / src / DepTable.cpp
1 /**
2  * \file DepTable.cpp
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 #ifndef CXX_GLOBAL_CSTD
29 using std::time;
30 #endif
31 using std::endl;
32 using std::flush;
33 using std::getline;
34 using std::string;
35 using std::ofstream;
36 using std::ifstream;
37
38
39 namespace lyx {
40
41 using support::FileName;
42 using support::ltrim;
43 using support::onlyFilename;
44 using support::suffixIs;
45 using support::sum;
46
47 inline
48 bool DepTable::dep_info::changed() const
49 {
50         return crc_prev != crc_cur && crc_cur != 0;
51 }
52
53
54 void DepTable::insert(FileName const & f, bool upd)
55 {
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(f.toFilesystemEncoding().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         }
74 }
75
76
77 void DepTable::update()
78 {
79         LYXERR(Debug::DEPEND) << "Updating DepTable..." << endl;
80         time_type const start_time = current_time();
81
82         DepList::iterator itr = deplist.begin();
83         while (itr != deplist.end()) {
84                 dep_info &di = itr->second;
85
86                 struct stat f_info;
87                 if (stat(itr->first.toFilesystemEncoding().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                 }
107
108                 if (lyxerr.debugging(Debug::DEPEND)) {
109                         if (di.changed())
110                                 lyxerr << " +";
111                         lyxerr << endl;
112                 }
113                 ++itr;
114         }
115         time_type const time_sec = current_time() - start_time;
116         LYXERR(Debug::DEPEND) << "Finished updating DepTable ("
117                 << time_sec << " sec)." << endl;
118 }
119
120
121 bool DepTable::sumchange() const
122 {
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;
129 }
130
131
132 bool DepTable::haschanged(FileName const & fil) const
133 {
134         DepList::const_iterator cit = deplist.find(fil);
135         if (cit != deplist.end()) {
136                 if (cit->second.changed())
137                         return true;
138         }
139         return false;
140 }
141
142
143 bool DepTable::extchanged(string const & ext) const
144 {
145         DepList::const_iterator cit = deplist.begin();
146         DepList::const_iterator end = deplist.end();
147         for (; cit != end; ++cit) {
148                 if (suffixIs(cit->first.absFilename(), ext)) {
149                         if (cit->second.changed())
150                                 return true;
151                 }
152         }
153         return false;
154 }
155
156
157 bool DepTable::ext_exist(string const & ext) const
158 {
159         DepList::const_iterator cit = deplist.begin();
160         DepList::const_iterator end = deplist.end();
161         for (; cit != end; ++cit) {
162                 if (suffixIs(cit->first.absFilename(), ext)) {
163                         return true;
164                 }
165         }
166         return false;
167 }
168
169
170 bool DepTable::exist(FileName const & fil) const
171 {
172         return deplist.find(fil) != deplist.end();
173 }
174
175
176 void DepTable::remove_files_with_extension(string const & suf)
177 {
178         DepList::iterator cit = deplist.begin();
179         DepList::iterator end = deplist.end();
180         while (cit != end) {
181                 if (suffixIs(cit->first.absFilename(), suf)) {
182                         // Can't erase the current iterator, but we
183                         // can increment and then erase.
184                         // Deplist is a map so only the erased
185                         // iterator is invalidated.
186                         DepList::iterator doomed = cit++;
187                         deplist.erase(doomed);
188                         continue;
189                 }
190                 ++cit;
191         }
192 }
193
194
195 void DepTable::remove_file(FileName const & filename)
196 {
197         DepList::iterator cit = deplist.begin();
198         DepList::iterator end = deplist.end();
199         while (cit != end) {
200                 if (cit->first == filename) {
201                         // Can't erase the current iterator, but we
202                         // can increment and then erase.
203                         // deplist is a map so only the erased
204                         // iterator is invalidated.
205                         DepList::iterator doomed = cit++;
206                         deplist.erase(doomed);
207                         continue;
208                 }
209                 ++cit;
210         }
211 }
212
213
214 void DepTable::write(FileName const & f) const
215 {
216         ofstream ofs(f.toFilesystemEncoding().c_str());
217         DepList::const_iterator cit = deplist.begin();
218         DepList::const_iterator end = deplist.end();
219         for (; cit != end; ++cit) {
220                 // Store the second (most recently calculated)
221                 // CRC value.
222                 // The older one is effectively set to 0 upon re-load.
223                 LYXERR(Debug::DEPEND) << "Write dep: "
224                        << cit->second.crc_cur << ' '
225                        << cit->second.mtime_cur << ' '
226                        << cit->first << endl;
227
228                 ofs << cit->second.crc_cur << ' '
229                     << cit->second.mtime_cur << ' '
230                     << cit->first << endl;
231         }
232 }
233
234
235 bool DepTable::read(FileName const & f)
236 {
237         ifstream ifs(f.toFilesystemEncoding().c_str());
238         string nome;
239         dep_info di;
240         // This doesn't change through the loop.
241         di.crc_prev = 0;
242
243         while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
244                 nome = ltrim(nome);
245
246                 LYXERR(Debug::DEPEND) << "Read dep: "
247                        << di.crc_cur << ' ' << di.mtime_cur << ' ' << nome << endl;
248
249                 deplist[FileName(nome)] = di;
250         }
251         return deplist.size();
252 }
253
254
255 } // namespace lyx