]> git.lyx.org Git - lyx.git/blob - src/DepTable.C
do not create invalid .lyx files when importing \i{}, \j{}, \l{} or \L{}.
[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::makeAbsPath;
38 using support::onlyFilename;
39 using support::suffixIs;
40 using support::sum;
41
42 using std::endl;
43 using std::flush;
44 using std::getline;
45 using std::string;
46 using std::ofstream;
47 using std::ifstream;
48
49 inline
50 bool DepTable::dep_info::changed() const
51 {
52         return crc_prev != crc_cur && crc_cur != 0;
53 }
54
55
56 void DepTable::insert(FileName const & f, bool upd)
57 {
58         if (deplist.find(f) == deplist.end()) {
59                 dep_info di;
60                 di.crc_prev = 0;
61                 if (upd) {
62                         lyxerr[Debug::DEPEND] << " CRC..." << flush;
63                         di.crc_cur = sum(f);
64                         lyxerr[Debug::DEPEND] << "done." << endl;
65                         struct stat f_info;
66                         stat(f.toFilesystemEncoding().c_str(), &f_info);
67                         di.mtime_cur = f_info.st_mtime;
68                 } else {
69                         di.crc_cur = 0;
70                         di.mtime_cur = 0;
71                 }
72                 deplist[f] = di;
73         } else {
74                 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
75         }
76 }
77
78
79 void DepTable::update()
80 {
81         lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
82         time_type const start_time = current_time();
83
84         DepList::iterator itr = deplist.begin();
85         while (itr != deplist.end()) {
86                 dep_info &di = itr->second;
87
88                 struct stat f_info;
89                 if (stat(itr->first.toFilesystemEncoding().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" << endl;
93                         } else {
94                                 di.crc_prev = di.crc_cur;
95                                 lyxerr[Debug::DEPEND] << itr->first << " CRC... " << flush;
96                                 di.crc_cur = sum(itr->first);
97                                 lyxerr[Debug::DEPEND] << "done" << endl;
98                         }
99                 } else {
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);
107                         continue;
108                 }
109
110                 if (lyxerr.debugging(Debug::DEPEND)) {
111                         if (di.changed())
112                                 lyxerr << " +";
113                         lyxerr << endl;
114                 }
115                 ++itr;
116         }
117         time_type const time_sec = current_time() - start_time;
118         lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
119                 << time_sec << " sec)." << endl;
120 }
121
122
123 bool DepTable::sumchange() const
124 {
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;
129         }
130         return false;
131 }
132
133
134 bool DepTable::haschanged(FileName const & fil) const
135 {
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;
142 }
143
144
145 bool DepTable::extchanged(string const & ext) const
146 {
147         DepList::const_iterator cit = deplist.begin();
148         DepList::const_iterator end = deplist.end();
149         for (; cit != end; ++cit) {
150                 if (suffixIs(cit->first.absFilename(), ext)) {
151                         if (cit->second.changed())
152                                 return true;
153                 }
154         }
155         return false;
156 }
157
158
159 bool DepTable::ext_exist(string const & ext) const
160 {
161         DepList::const_iterator cit = deplist.begin();
162         DepList::const_iterator end = deplist.end();
163         for (; cit != end; ++cit) {
164                 if (suffixIs(cit->first.absFilename(), ext)) {
165                         return true;
166                 }
167         }
168         return false;
169 }
170
171
172 bool DepTable::exist(FileName const & fil) const
173 {
174         return deplist.find(fil) != deplist.end();
175 }
176
177
178 void DepTable::remove_files_with_extension(string const & suf)
179 {
180         DepList::iterator cit = deplist.begin();
181         DepList::iterator end = deplist.end();
182         while (cit != end) {
183                 if (suffixIs(cit->first.absFilename(), 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         }
194 }
195
196
197 void DepTable::remove_file(FileName const & filename)
198 {
199         DepList::iterator cit = deplist.begin();
200         DepList::iterator end = deplist.end();
201         while (cit != end) {
202                 if (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         }
213 }
214
215
216 void DepTable::write(FileName const & f) const
217 {
218         ofstream ofs(f.toFilesystemEncoding().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         }
235 }
236
237
238 bool DepTable::read(FileName const & f)
239 {
240         ifstream ifs(f.toFilesystemEncoding().c_str());
241         string nome;
242         dep_info di;
243         // This doesn't change through the loop.
244         di.crc_prev = 0;
245
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[FileName(nome)] = di;
255         }
256         return deplist.size();
257 }
258
259
260 } // namespace lyx