]> git.lyx.org Git - lyx.git/blob - src/DepTable.C
If I ever see another licence blurb again, it'll be too soon...
[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 #include "debug.h"
17
18 #include "support/lyxlib.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/lyxtime.h"
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include <fstream>
28 #include <ctime>
29
30 #ifndef CXX_GLOBAL_CSTD
31 using std::time;
32 #endif
33
34 using namespace lyx::support;
35
36 using std::make_pair;
37 using std::ofstream;
38 using std::ifstream;
39 using std::flush;
40 using std::endl;
41 using std::getline;
42
43
44 inline
45 bool DepTable::dep_info::changed() const
46 {
47         return crc_prev != crc_cur && crc_cur != 0;
48 }
49
50
51 void DepTable::insert(string const & fi, bool upd)
52 {
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()) {
56                 dep_info di;
57                 di.crc_prev = 0;
58                 if (upd) {
59                         lyxerr[Debug::DEPEND] << " CRC..." << flush;
60                         di.crc_cur = sum(f);
61                         lyxerr[Debug::DEPEND] << "done." << endl;
62                         struct stat f_info;
63                         stat(fi.c_str(), &f_info);
64                         di.mtime_cur = f_info.st_mtime;
65                 } else {
66                         di.crc_cur = 0;
67                         di.mtime_cur = 0;
68                 }
69                 deplist[f] = di;
70         } else {
71                 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
72         }
73 }
74
75
76 void DepTable::update()
77 {
78         lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
79         lyx::time_type const start_time = lyx::current_time();
80
81         DepList::iterator itr = deplist.begin();
82         while (itr != deplist.end()) {
83                 dep_info &di = itr->second;
84
85                 struct stat f_info;
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";
90                         } else {
91                                 di.crc_prev = di.crc_cur;
92                                 lyxerr[Debug::DEPEND] << itr->first << " CRC... ";
93                                 di.crc_cur = sum(itr->first);
94                                 lyxerr[Debug::DEPEND] << "done";
95                         }
96                 } else {
97                         // file doesn't exist
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);
104                         continue;
105                 }
106
107                 if (lyxerr.debugging(Debug::DEPEND)) {
108                         if (di.changed())
109                                 lyxerr << " +";
110                         lyxerr << endl;
111                 }
112                 ++itr;
113         }
114         lyx::time_type const time_sec = lyx::current_time() - start_time;
115         lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
116                 << time_sec << " sec)." << endl;
117 }
118
119
120 bool DepTable::sumchange() const
121 {
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;
126         }
127         return false;
128 }
129
130
131 bool DepTable::haschanged(string const & f) const
132 {
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())
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, 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, ext)) {
164                         return true;
165                 }
166         }
167         return false;
168 }
169
170
171 bool DepTable::exist(string 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, 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(string const & filename)
197 {
198         DepList::iterator cit = deplist.begin();
199         DepList::iterator end = deplist.end();
200         while (cit != 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);
208                         continue;
209                 }
210                 ++cit;
211         }
212 }
213
214
215 void DepTable::write(string const & f) const
216 {
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)
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(string const & f)
238 {
239         ifstream ifs(f.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[nome] = di;
254         }
255         return deplist.size();
256 }