]> git.lyx.org Git - lyx.git/blob - src/DepTable.C
Continue to improve GtkLengthEntry
[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 #ifndef CXX_GLOBAL_CSTD
29 using std::time;
30 #endif
31
32 using lyx::support::ltrim;
33 using lyx::support::MakeAbsPath;
34 using lyx::support::OnlyFilename;
35 using lyx::support::suffixIs;
36 using lyx::support::sum;
37
38 using std::endl;
39 using std::flush;
40 using std::getline;
41 using std::string;
42 using std::ofstream;
43 using std::ifstream;
44
45 inline
46 bool DepTable::dep_info::changed() const
47 {
48         return crc_prev != crc_cur && crc_cur != 0;
49 }
50
51
52 void DepTable::insert(string const & fi, bool upd)
53 {
54         // not quite sure if this is the correct place for MakeAbsPath
55         string const f = MakeAbsPath(fi);
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(fi.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         lyx::time_type const start_time = lyx::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.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         lyx::time_type const time_sec = lyx::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(string const & f) const
133 {
134         // not quite sure if this is the correct place for MakeAbsPath
135         string const fil = MakeAbsPath(f);
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, 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, ext)) {
165                         return true;
166                 }
167         }
168         return false;
169 }
170
171
172 bool DepTable::exist(string 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, 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(string const & filename)
198 {
199         DepList::iterator cit = deplist.begin();
200         DepList::iterator end = deplist.end();
201         while (cit != end) {
202                 if (OnlyFilename(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(string const & f) const
217 {
218         ofstream ofs(f.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(string const & f)
239 {
240         ifstream ifs(f.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[nome] = di;
255         }
256         return deplist.size();
257 }