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