]> git.lyx.org Git - lyx.git/blob - src/DepTable.C
Various trivial bits 'n' bats about config.h, header blurb etc.
[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/stat.h>
24
25 #include <fstream>
26
27 #ifndef CXX_GLOBAL_CSTD
28 using std::time;
29 #endif
30
31 using namespace lyx::support;
32
33 using std::endl;
34 using std::flush;
35 using std::getline;
36
37 using std::ofstream;
38 using std::ifstream;
39
40 inline
41 bool DepTable::dep_info::changed() const
42 {
43         return crc_prev != crc_cur && crc_cur != 0;
44 }
45
46
47 void DepTable::insert(string const & fi, bool upd)
48 {
49         // not quite sure if this is the correct place for MakeAbsPath
50         string const f = MakeAbsPath(fi);
51         if (deplist.find(f) == deplist.end()) {
52                 dep_info di;
53                 di.crc_prev = 0;
54                 if (upd) {
55                         lyxerr[Debug::DEPEND] << " CRC..." << flush;
56                         di.crc_cur = sum(f);
57                         lyxerr[Debug::DEPEND] << "done." << endl;
58                         struct stat f_info;
59                         stat(fi.c_str(), &f_info);
60                         di.mtime_cur = f_info.st_mtime;
61                 } else {
62                         di.crc_cur = 0;
63                         di.mtime_cur = 0;
64                 }
65                 deplist[f] = di;
66         } else {
67                 lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
68         }
69 }
70
71
72 void DepTable::update()
73 {
74         lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
75         lyx::time_type const start_time = lyx::current_time();
76
77         DepList::iterator itr = deplist.begin();
78         while (itr != deplist.end()) {
79                 dep_info &di = itr->second;
80
81                 struct stat f_info;
82                 if (stat(itr->first.c_str(), &f_info) == 0) {
83                         if (di.mtime_cur == f_info.st_mtime) {
84                                 di.crc_prev = di.crc_cur;
85                                 lyxerr[Debug::DEPEND] << itr->first << " same mtime";
86                         } else {
87                                 di.crc_prev = di.crc_cur;
88                                 lyxerr[Debug::DEPEND] << itr->first << " CRC... ";
89                                 di.crc_cur = sum(itr->first);
90                                 lyxerr[Debug::DEPEND] << "done";
91                         }
92                 } else {
93                         // file doesn't exist
94                         // remove stale files - if it's re-created, it
95                         // will be re-inserted by deplog.
96                         lyxerr[Debug::DEPEND] << itr->first
97                                 << " doesn't exist. removing from DepTable." << endl;
98                         DepList::iterator doomed = itr++;
99                         deplist.erase(doomed);
100                         continue;
101                 }
102
103                 if (lyxerr.debugging(Debug::DEPEND)) {
104                         if (di.changed())
105                                 lyxerr << " +";
106                         lyxerr << endl;
107                 }
108                 ++itr;
109         }
110         lyx::time_type const time_sec = lyx::current_time() - start_time;
111         lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
112                 << time_sec << " sec)." << endl;
113 }
114
115
116 bool DepTable::sumchange() const
117 {
118         DepList::const_iterator cit = deplist.begin();
119         DepList::const_iterator end = deplist.end();
120         for (; cit != end; ++cit) {
121                 if (cit->second.changed()) return true;
122         }
123         return false;
124 }
125
126
127 bool DepTable::haschanged(string const & f) const
128 {
129         // not quite sure if this is the correct place for MakeAbsPath
130         string const fil = MakeAbsPath(f);
131         DepList::const_iterator cit = deplist.find(fil);
132         if (cit != deplist.end()) {
133                 if (cit->second.changed())
134                         return true;
135         }
136         return false;
137 }
138
139
140 bool DepTable::extchanged(string const & ext) const
141 {
142         DepList::const_iterator cit = deplist.begin();
143         DepList::const_iterator end = deplist.end();
144         for (; cit != end; ++cit) {
145                 if (suffixIs(cit->first, ext)) {
146                         if (cit->second.changed())
147                                 return true;
148                 }
149         }
150         return false;
151 }
152
153
154 bool DepTable::ext_exist(string const & ext) const
155 {
156         DepList::const_iterator cit = deplist.begin();
157         DepList::const_iterator end = deplist.end();
158         for (; cit != end; ++cit) {
159                 if (suffixIs(cit->first, ext)) {
160                         return true;
161                 }
162         }
163         return false;
164 }
165
166
167 bool DepTable::exist(string const & fil) const
168 {
169         return deplist.find(fil) != deplist.end();
170 }
171
172
173 void DepTable::remove_files_with_extension(string const & suf)
174 {
175         DepList::iterator cit = deplist.begin();
176         DepList::iterator end = deplist.end();
177         while (cit != end) {
178                 if (suffixIs(cit->first, suf)) {
179                         // Can't erase the current iterator, but we
180                         // can increment and then erase.
181                         // Deplist is a map so only the erased
182                         // iterator is invalidated.
183                         DepList::iterator doomed = cit++;
184                         deplist.erase(doomed);
185                         continue;
186                 }
187                 ++cit;
188         }
189 }
190
191
192 void DepTable::remove_file(string const & filename)
193 {
194         DepList::iterator cit = deplist.begin();
195         DepList::iterator end = deplist.end();
196         while (cit != end) {
197                 if (OnlyFilename(cit->first) == filename) {
198                         // Can't erase the current iterator, but we
199                         // can increment and then erase.
200                         // deplist is a map so only the erased
201                         // iterator is invalidated.
202                         DepList::iterator doomed = cit++;
203                         deplist.erase(doomed);
204                         continue;
205                 }
206                 ++cit;
207         }
208 }
209
210
211 void DepTable::write(string const & f) const
212 {
213         ofstream ofs(f.c_str());
214         DepList::const_iterator cit = deplist.begin();
215         DepList::const_iterator end = deplist.end();
216         for (; cit != end; ++cit) {
217                 if (lyxerr.debugging(Debug::DEPEND)) {
218                         // Store the second (most recently calculated)
219                         // CRC value.
220                         // The older one is effectively set to 0 upon re-load.
221                         lyxerr << "Write dep: "
222                                << cit->second.crc_cur << ' '
223                                << cit->second.mtime_cur << ' '
224                                << cit->first << endl;
225                 }
226                 ofs << cit->second.crc_cur << ' '
227                     << cit->second.mtime_cur << ' '
228                     << cit->first << endl;
229         }
230 }
231
232
233 bool DepTable::read(string const & f)
234 {
235         ifstream ifs(f.c_str());
236         string nome;
237         dep_info di;
238         // This doesn't change through the loop.
239         di.crc_prev = 0;
240
241         while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
242                 nome = ltrim(nome);
243                 if (lyxerr.debugging(Debug::DEPEND)) {
244                         lyxerr << "Read dep: "
245                                << di.crc_cur << ' '
246                                << di.mtime_cur << ' '
247                                << nome << endl;
248                 }
249                 deplist[nome] = di;
250         }
251         return deplist.size();
252 }