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